<?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><?php echo $vchdt->grn_no; ?> - <?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">
                        <form  action="<?php echo site_url('StockManagement/payadvice_email_sending');?>" class="uk-form-stacked" method="post" autocomplete="off">
                        <div class="row" style="margin-left: 4px!important;">
                            <?php
								$tr_date = date('d-m-Y');
								if($vch->approval_status == 'Approved' && $vch->transaction_detail == '') {
							?>
                            <div class="col-md-4 hidden-print">
                            		<input type="text" class="form-control" name="tr_detail" id="tr_detail" placeholder="Transactional Detail" required >
									<input type="hidden" class="form-control" name="vid" id="vid" value="<?php echo $vch->voucher_id; ?>" >
									<input type="hidden" class="form-control" name="ph_no" id="ph_no" value="<?php echo $vch->contact_no; ?>" >
									<input type="hidden" class="form-control" name="email_id" id="email_id" value="<?php echo $vch->email_id; ?>" >
									<input type="hidden" class="form-control" name="vendor" id="vendor" value="<?php echo $vch->vendor_name; ?>" >
									<input type="hidden" class="form-control" name="tdate" id="tdate" value="<?php echo $tr_date; ?>" >
									<input type="hidden" class="form-control" name="pay_mode" id="pay_mode" value="<?php echo $vch->pay_mode; ?>" >
									<input type="hidden" class="form-control" name="bk_name" id="bk_name" value="<?php echo $vch->bank_name; ?>" >
									<input type="hidden" class="form-control" name="ttl_amt" id="ttl_amt" value="<?php echo $vch->final_amt; ?>" >
									<input type="hidden" class="form-control" name="address" id="address" value="<?php echo $vch->address; ?>" >
									<input type="hidden" class="form-control" name="address1" id="address1" value="<?php echo $vch->address1; ?>" >
									<?php 
								       foreach($voucherparti as $vp){
								    ?>
								    <input type="hidden" class="form-control" name="inv_no[]" id="inv_no" value="<?php echo $vp->invoice_no; ?>" >
								    <input type="hidden" class="form-control" name="inv_date[]" id="inv_date" value="<?php echo $vp->invoice_date; ?>" >
								    <input type="hidden" class="form-control" name="inv_amt[]" id="inv_amt" value="<?php echo $vp->grn_pay_amt; ?>" >
									<?php
								        }
									?>
                    		</div>  
                    		<div class="col-md-4 hidden-print">
                    		    <input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="tr_date"  id="tr_date" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="Transaction Date">
                    		</div>
                    		<?php } ?>
                    		
                    		<?php
        					    $user = 'CEO';
        						$q1 = $this->db->query("SELECT * FROM user where username='$user'");
        						foreach($q1->result_array() as $r1)
        						$phone_no = $r1['ph_no'];
    						?>
    						<input type="hidden" name="ph_no1" id="ph_no1" value="<?php echo $phone_no; ?>" >
    						<input type="hidden" class="form-control" name="vch_no" id="vch_no" value="<?php echo $vch->voucher_no; ?>" >
							<input type="hidden" class="form-control" name="ttl_amt1" id="ttl_amt1" value="<?php echo $vch->total_amt; ?>" >
							<input type="hidden" class="form-control" name="vnd_name" id="vnd_name" value="<?php echo $vch->vendor_name; ?>" >
                    		<div class="col-md-4 hidden-print">
                    		    <?php
										if($vch->approval_status == 'Approved' && $vch->transaction_detail == '') {
									?>
										<input type="submit" name="submit" id="pay_advice" class="btn btn-primary" value="Generate Pay Advice">
								<?php
								}
								?>
                    		</div>
                    		<div class="col-md-4 hidden-print">
                		    	<?php
									if($vch->approval_status == 'Pending') {
								?>
									<td><i class="uk-icon-spinner uk-icon-medium uk-icon-spin" ></i>Approval Pending</td>
								<?php
								} else {
								?>
									<!--<i class="md-icon material-icons" id="print">&#xE8ad;</i>-->
									<button type="button" id="print_receipt" class="btn btn-info btn-sm my-1 print_receipt"><i class="la la-paper-plane-o mr-50"></i>Print            Invoice</button>
								<?php
								}
								?>
                    		</div>
                    	</div>
                    	</form>
                    </div>
                </div>
            </div>
        </div>
        
			<br/>
			<div class="row hidden-print">
				<div class="col-md-12">
					<?php if($vch->chq_uploaded != ""){ ?>
						<!--<span class="click-file" id="click-file" target="1"> View File</span>-->
						<a href="#demo1" class='btn btn-primary category_list' data-toggle='collapse'>View Cheque</a>
						<!--<div class="show-document" id="div1">-->
						<div id="demo1" class='collapse'>
							<img src="<?php echo $vch->chq_uploaded; ?>" class="img-rounded" alt="Cheque">
						</div>
					<?php } ?>
				</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>
		$(window).load(function() {
			voucher_pending();
		});
	</script>
<script>
    $("#pay_advice").click(function() {
		var tr_detail = $('#tr_detail').val();
		//alert(tr_detail);
		if(tr_detail != '')
		{
			pay_advice();
		}	
	});
    
	$("#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>