<?php 
	$header_file=APPPATH.'views/include/header.php';
	require($header_file);
	$sidenav_file=APPPATH.'views/include/sidenav.php';
	require($sidenav_file);
	require('currency_to_words.php');	
?>
<!-- BEGIN: Content-->
<style type="text/css">
		.gridtable {
		border:none;
		width:100%;
		
		}
		.gridtable td {
		border:none;
		padding:3px 0px;
		}
		.pborder {
		border-bottom:1px solid!important;
		}
		.border_left {
		border-left:1px solid!important;
		}
		.border_top {
		border-top:1px solid!important;
		}
		.pdleft {
		padding-left: 40px!important;
		}
		.pr_pdleft {
		padding-left: 80px!important;
		}
		.twdith {
		width: 33.3%;
		}
		#printThisElement{
		visibility:visible;
		height: 100%;
		}
		
		@media print {
		@page {
		size : A5;
		margin:0px;
		}
		body * {
		visibility:hidden;
		}
		#printThisElement, #printThisElement * {
		visibility:visible;
		}
		}
	</style>
    <div class="app-content content">
      <div class="content-overlay"></div>
      <div class="content-wrapper">
        <div class="content-header row">
          <div class="content-header-left col-md-6 col-12 mb-2 breadcrumb-new">
            <h3 class="content-header-title mb-0 d-inline-block">Voucher</h3>
            <div class="row breadcrumbs-top d-inline-block">
              <div class="breadcrumb-wrapper col-12">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Home</a>
                  </li>
                  <li class="breadcrumb-item"><a href="#">Voucher</a>
                  </li>
                  <li class="breadcrumb-item active">Voucher View
                  </li>
                </ol>
              </div>
            </div>
          </div>
         
        </div>
        <div class="content-body">
        <div class="card" id="invoice-template">
            <div  class="card-body p-4">
                <div class="card-content collapse show">
                    <div class="card-dashboard">
                        <div class="table-responsive col-12">
                		<?php foreach($voucher as $vch) ?>
                          <table class="gridtable ">
                            <tbody>
                				<?php if($vch->pay_mode == 'Cash') { ?>
                				<tr>
                					<td colspan="6" align="center" style="font-size: medium;"><strong>Cash Payment Voucher</strong></td>
                				</tr>
                				<?php } else { ?>
                				<?php if($vch->branch=='CW') { ?>
                				<tr>
                					<td colspan="6" align="center" class="main_heading"><strong>BHATTAS FOODS PVT. LTD.</strong></td>
                				</tr>
                				<?php } else if($vch->branch=='ESHY') { ?>
                				<tr>
                					<td colspan="6" align="center" class="main_heading"><strong>ESHANYA HOTEL PVT. LTD.</strong></td>
                				</tr>
                				<?php } else if($vch->branch=='MC') { ?>
                				<tr>
                					<td colspan="6" align="center" class="main_heading"><strong>MELTING CAKES LLP.</strong></td>
                				</tr>
                				<?php } ?>
                				<tr>
                					<td colspan="6"  align="center" class="sub2_heading">#33, 36th Cross, 26th Main,</td>
                				</tr>
                				<tr>
                					<td colspan="6"  align="center" class="sub2_heading">4th T Block, Jayanagar, BANGALORE - 560 041</td>
                				</tr>
                				<tr>
                					<td colspan="6"  align="center" class="sub2_heading">State Name : Karnataka, Code : 29</td>
                				</tr>
                				<tr>
                					<td colspan="6"  align="center" class="sub2_heading">Payment Voucher</td>
                				</tr>
                				<?php } ?>
                				<tr>
                					<td colspan="4">No : <strong><?php echo $vch->voucher_no; ?></strong></td>
                					<td colspan="2" class="text-right">Dated : <strong><?php echo $vch->vdate; ?></strong></td>
                				</tr>
                				<tr>
                					<td colspan="5" class="pborder pdleft border_top">Particulars
                					</td>
                					<td class="pborder border_left text-right" >Amount</td>
                				</tr>
                				<tr>
                					<td colspan="5"><strong>Account :</strong></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td class="pdleft" colspan="5"><?php echo $vch->vendor_name; ?></td>
                					<td class="border_left" ></td>
                				</tr>
                				<?php foreach($voucher_detail as $vchdt) { ?>
                				<tr>
                					<td colspan="3" class="pr_pdleft">GRN : <strong><a href="<?php echo site_url('Purchase/ceo_agrn_view/'.$vchdt->grn_id); ?>" target="_blank" ><?php echo $vchdt->grn_no; ?></a> - <?php echo $vchdt->grn_date; ?></strong></td>
                					<td colspan="2">Invoice No : <strong><?php echo $vchdt->invoice_no; ?></strong></td>
                					<td class="border_left text-right" ><strong><?php echo $vchdt->grn_pay_amt; ?></strong></td>	
                				</tr>
                				<?php } ?>
                				<tr>
                					<td colspan="5" style="visibility:hidden">1</td>
                					<td class="border_left"></td>
                				</tr>
                				<?php if($vch->pay_mode == 'Cash') { ?>
                				<tr>
                					<td colspan="5"><strong>Payment Mode :</strong></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td  class="pdleft" colspan="5"><?php echo $vch->pay_mode; ?></td>
                					<td class="border_left"></td>
                				</tr>
                				<?php } else { ?>	
                				<tr>
                					<td colspan="5"><strong>Through :</strong></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td  class="pdleft" colspan="5"><?php echo $vch->bank_name; ?></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td colspan="5"><strong>On Account of :</strong></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td  class="pdleft" colspan="5">Ch. No. <?php echo $vch->transaction_no; ?>. Being payment made towards outstanding balance.</td>
                					<td class="border_left" colspan=""></td>
                				</tr>
                				<?php } ?>
                				<tr>
                					<td  class="numeric border_none" style="text-align: left!important;" colspan="5"><strong>Amount (in words) :</strong></td>
                					<td class="numeric border_left" style="text-align: left!important;"  colspan=""></td>
                				</tr>
                				<tr>
                					<td  class="pdleft" colspan="5"><?php echo $currency_object->get_bd_amount_in_text($vch->final_amt); ?></td>
                					<td class="border_left"></td>
                				</tr>
                				<tr>
                					<td colspan="5"></td>
                					<td class="border_left pborder border_top text-right"><strong><?php echo $vch->final_amt; ?><strong></td>
                				</tr>
                				<tr>
                					<td colspan="6" style="visibility:hidden">1</td>
                				</tr>
                				<tr>
                					<td colspan="6" style="visibility:hidden">2</td>
                				</tr>
                				<tr>
                					<td colspan="2" class="text-left"></td>
                					<td colspan="2" class="text-center"></td>
                						<?php if($vch->branch=='MC') { ?>
                					<td colspan="2" class="text-right"><strong>Adarsh GR</strong></td>
                					<?php } else { ?>
                					<td colspan="2" class="text-right"><strong>Suhas</strong></td>
                					<?php } ?>
                					<br/>
                				</tr>
                				<tr>
                					<td colspan="2" class="text-left">Receiver’s Signature :</td>
                					<td colspan="2" class="text-center"></td>
                					<td colspan="2" class="text-right">Authorised Signatory</td>
                					<br/>
                				</tr>
                				<tr>
                					<td colspan="6" style="visibility:hidden">1</td>
                				</tr>
                				<tr>
                					<td colspan="6" style="visibility:hidden">2</td>
                				</tr>
                				<tr>
                					<td colspan="1" class="text-left"><strong>Manoj</strong></td>
                					<td colspan="3" class="text-left"><strong>Manoj</strong></td>
                						<?php if($vch->branch=='MC') { ?>
                					<td colspan="2" class="text-right"><strong>Adarsh GR</strong></td>
                					<?php } else { ?>
                					<td colspan="2" class="text-right"><strong>Suhas</strong></td>
                					<?php } ?>
                				</tr>
                				<tr>
                					<td colspan="1" class="text-left">Prepared By</td>
                					<td colspan="3" class="text-left">Checked By</td>
                					<td colspan="2" class="text-right">Verified By</td>
                				</tr>
                            </tbody>
                          </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card" >
            <div  class="card-body" style="padding: 0.5rem!important;">
                <div class="card-content collapse show">
                    <div class="card-dashboard">
                        <div class="row" style="margin-left: 4px!important;">
                            
                    		  <div class="md-card-toolbar-actions hidden-print text-center" >
                    			<?php
                    			if($vch->approval_status == 'Pending') { ?>
                    			<form class="form" autocomplete="off" action="<?php echo site_url('StockManagement/approveVoucher');?>" method="post" enctype="multipart/form-data">
                    				<?php
                    					$cur_date = date("d-m-Y h:i");
                    				?>
                    				<input name="approval_date" id="approval_date" type="hidden" class="textbox" value="<?php echo $cur_date; ?>">
                    				<input type="hidden" name="voucher_id" id="voucher_id" value="<?php echo $vch->voucher_id; ?>" >
                    				<input type="submit" name="submit" id="submit" class="btn btn-info btn-lg my-1"  value="Approve">
                    			</form>
                    			<?php } else { ?>
                    				<img src="<?php echo base_url(); ?>app-assets/images/right.png" alt="" class="img_main"/> Approved
                    			<?php } ?>
                    		  </div>
                    		
                    	</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card" id="invoice-items-details" >
            <div  class="card-body p-4">
                <div class="card-content collapse show">
                    <div class="card-dashboard">
                        <div class="table-responsive col-12">
            				<h3 class="heading_b uk-margin-bottom">Pending GRN</h3>
            				<table width="100%" class="table table-bordered" >
            					<thead>
                                    <tr>
            							<th><strong>GRN No</strong></th>
                						<th><strong>Date</strong></th>
                						<th><strong>Pay Status</strong></th>
                						<th><strong>Total Amt</strong></th>
            						</tr>
            					</thead>
            					<tbody>
            						<?php 
            							$ttl_amt = 0;
            							$vendor_name = $vch->vendor_name;
            						    if($vch->branch=='MC') {
            							    $q1 = $this->db->query("select *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date from gnr_creation where vendor_name='$vendor_name' and payment_status='Pending' and branchname='CW4' order by gnr_id desc");
            							}
            							else if($vch->branch=='CW5')
            							{
            							    $q1 = $this->db->query("select *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date from gnr_creation where vendor_name='$vendor_name' and payment_status='Pending' and branchname='CW5' order by gnr_id desc");
            							} else {
            							   $q1 = $this->db->query("select *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date from gnr_creation where vendor_name='$vendor_name' and payment_status='Pending' and branchname!='CW4' order by gnr_id desc"); 
            							}
            							$vc_res = $q1->result();
            							foreach($vc_res as $vc_row)
            							{
            								$ttl_amt = $ttl_amt + $vc_row->total_amount;
            						?>
            						<tr>
            							<td><a href="<?php echo site_url('Purchase/ceo_agrn_view/'.  $vc_row->gnr_id); ?>" target="_blank" ><?php echo $vc_row->grn_no; ?></a></td>
            							<td><?php echo $vc_row->grn_date; ?></td>
            							<td><?php echo $vc_row->payment_status; ?></td>
            							<td><?php echo $vc_row->total_amount; ?></td>
            						</tr>
            						<?php
            							}
            						?>
            						<tr>
            							<td colspan="3"><strong>Total</strong></td>
            							<td><strong><?php echo round($ttl_amt); ?></strong></td>
            						</tr>
            					</tbody>
            				</table>
            			</div>
                    </div>
                </div>
            </div>
        </div>


  </div>
</section>
</section>
<!-- // Basic form layout section end -->
        </div>
      </div>
    </div>
    <?php //require('include/theme_customizer.php');?>

   <?php 
  $footer_file=APPPATH.'views/include/footer.php';
	require($footer_file);
	?>
	
<script>
	$("#print_receipt").click(function() {
		window.print();
		setTimeout("closePrintView()", 50);
	});
	function closePrintView() {
		window.location = '<?php echo site_url('StockManagement/voucherList');?>';
	} 
</script>
<script type='text/javascript'>//<![CDATA[
		window.onload=function(){
			function numToWords(number) {
				//Validates the number input and makes it a string
				if (typeof number === 'string') {
					number = parseInt(number, 10);
				}
				if (typeof number === 'number' && isFinite(number)) {
					number = number.toString(10);
					} else {
					return 'This is not a valid number';
				}
				//Creates an array with the number's digits and
				//adds the necessary amount of 0 to make it fully 
				//divisible by 3
				var digits = number.split('');
				while (digits.length % 3 !== 0) {
					digits.unshift('0');
				}
				//Groups the digits in groups of three
				var digitsGroup = [];
				var numberOfGroups = digits.length / 3;
				for (var i = 0; i < numberOfGroups; i++) {
					digitsGroup[i] = digits.splice(0, 3);
				}
				console.log(digitsGroup); //debug
				//Change the group's numerical values to text
				var digitsGroupLen = digitsGroup.length;
				var numTxt = [
				[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], //hundreds
				[null, 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'], //tens
				[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] //ones
				];
				var tenthsDifferent = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
				// j maps the groups in the digitsGroup
				// k maps the element's position in the group to the numTxt equivalent
				// k values: 0 = hundreds, 1 = tens, 2 = ones
				for (var j = 0; j < digitsGroupLen; j++) {
					for (var k = 0; k < 3; k++) {
						var currentValue = digitsGroup[j][k];
						digitsGroup[j][k] = numTxt[k][currentValue];
						if (k === 0 && currentValue !== '0') { // !==0 avoids creating a string "null hundred"
							digitsGroup[j][k] += ' hundred ';
							} else if (k === 1 && currentValue === '1') { //Changes the value in the tens place and erases the value in the ones place
							digitsGroup[j][k] = tenthsDifferent[digitsGroup[j][2]];
							digitsGroup[j][2] = 0; //Sets to null. Because it sets the next k to be evaluated, setting this to null doesn't work.
						}
					}
				}
				console.log(digitsGroup); //debug
				//Adds '-' for gramar, cleans all null values, joins the group's elements into a string
				for (var l = 0; l < digitsGroupLen; l++) {
					if (digitsGroup[l][1] && digitsGroup[l][2]) {
						digitsGroup[l][1] += '-';
					}
					digitsGroup[l].filter(function (e) {return e !== null});
					digitsGroup[l] = digitsGroup[l].join('');
				}
				console.log(digitsGroup); //debug
				//Adds thousand, millions, billion and etc to the respective string.
				var posfix = [null, 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion'];
				if (digitsGroupLen > 1) {
					var posfixRange = posfix.splice(0, digitsGroupLen).reverse();
					for (var m = 0; m < digitsGroupLen - 1; m++) { //'-1' prevents adding a null posfix to the last group
						if (digitsGroup[m]) {
							digitsGroup[m] += ' ' + posfixRange[m];
						}
					}
				}
				console.log(digitsGroup); //debug
				//Joins all the string into one and returns it
				return digitsGroup.join(' ');
			} //End of numToWords function
			function showText() {
				var inputValue = input.value;
				var hold = numToWords(inputValue);
				output.innerHTML = hold;
			}
			var button = document.getElementById('changeText');
			var output = document.getElementById('output');
			var input = document.getElementById('numinput');
			input.addEventListener('input', showText);
		}//]]> 
	</script>
	