<?php  $this->load->view('master/common/header2.php',$data);
$base_name = $data['base_name'];


$queryd11 = $this->db->query("SELECT * FROM customer_invoice where bill_no='$payid'");
  $rowd11 = $queryd11->row(); 
  
  
   $queryd1 = $this->db->query("SELECT SUM(total) AS rrt FROM customer_invoice where bill_no='$payid'");
$rowd1 = $queryd1->row(); 
$total=$rowd1->rrt;

$bill_no1=$rowd11->bill_no;
$c_id=$rowd11->customer_id;

 $queryd1 = $this->db->query("SELECT SUM(paid) AS rrt FROM customer_payment where bill_no='$payid'");
 $rowd1 = $queryd1->row(); 
 $paid=$rowd1->rrt+0;
 
 
 $queryds1 = $this->db->query("SELECT * FROM tbl_customer where cust_phone='$c_id'");
 $rowds1 = $queryds1->row();  
 
 $stid=$rowds1->cust_state;
 $queryds1 = $this->db->query("SELECT * FROM tbl_state where id='$stid'");
 $state = $queryds1->row(); 
?>
<link href="<?php echo base_url();?>assets/libs/select2/select2.min.css" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>assets/libs/select2/select2.min.js"></script>
    <div class="content">
        
        <!-- Start Content-->
        <div class="container-fluid">
            
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item">Staff</li>
                                <li class="breadcrumb-item">Print Invoice</li>
                            </ol>
                        </div>
                        <h4 class="page-title text-uppercase"><?= $page;?></h4>
                    </div>
                </div>
            </div>     
            <!-- end page title --> 

         
			

<div id="invoicePrint">
                <div class="row order_list">
    <div class="col-md-12">
        <div class="card-box">
            <div class="row">
                <div class="col-md-6">
                    <h5><img src="<?= base_url('uploads/website/logo/'.$data['logo']);?>" alt="" style="width:200px;"></h5>
                    <p>
                        Y. B. S. TRADERS<br>
                        S/O BAIDHNATH RAY<br>
                        MOHAMMADPUR DARIYA, BHARAT PETROLEUM, PO+PS-GORAUL,<br>
                        Bihar, 844118<br>
                        GSTIN/UIN : 10DEBPK6909H3ZZ<br>
                        State Name : Bihar, Code : 10
                    </p>
                </div>
                
                <div class="col-md-6 text-right">
                    <table class="table table-bordered">
                        <tr><td>Invoice No.</td><td><?= $payid?></td></tr>
                        <tr><td>Dated</td><td><?= date('d-m-Y',strtotime($rowd11->addon))?></td></tr>
                        <tr><td>Mode/Terms of Payment</td><td></td></tr>
                    </table>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <strong>Buyer</strong>
                    <p>
                        <?= $rowds1->cust_name?>
                        
                        <?= $rowds1->cust_address?><br>
                        <?= $rowds1->cust_city?><br>
                        <?= $rowds1->cust_zip?><br>
                         State Name : <?= $state->state?>, Code : <?= $state->code?>
                    </p>
                </div>
               
            </div>

            <div class="row" style="margin-top: 10px;">
                <div class="col-sm-12 table-responsive">
                      <table id="key-table" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                    <thead>
                                        <tr>
                                            <th>S.no.</th>
            								<th>Product Name</th>
            								<th>Size</th>
            								<th>Color</th>
            								 <th>Price</th>
            							    <th>qty</th>
            							    <th>Inc GST %</th>
            							    <th>Total</th>
            							 <!--   <th>Action</th>-->
                                        </tr>
                                    </thead>


                                        <tbody>
                                        
								<?php 
								$i=1;
				   $query3 = $this->db->where('user_id',$_SESSION['admin']['id']);
				   $query3 = $this->db->where('bill_no',$payid);
				   $query3 = $this->db->get('customer_invoice');
                   foreach ($query3->result() as $row)
                   {

									?>
								        <tr>
                                            <td><?= $i;?></td>
        								
                                                <td><?php
                                                $pid=$row->pid;
                                                $this->db->where('p_id',$pid); $pro = $this->db->get('tbl_product')->row_array();
                                                echo $pname=$pro['p_name'];
                                                ?></td>
                                         
                                                
                                                <td><?php
                                                $size=$row->size_id;
                                                $this->db->where('size_id',$size); $pro = $this->db->get('tbl_size')->row_array();
                                                echo $sizename=$pro['size_name'];
                                                ?></td>
                                                
                                             
                                               
                                                 <td><?php
                                                $colori=$row->color_id;
                                                if(empty($colori))
                                                {
                                                  echo $colorname='N/A';
                                                }
                                                else
                                                {
                                                $this->db->where('color_id',$colori); $pro = $this->db->get('tbl_color')->row_array();
                                                echo $colorname =$pro['color_name'];
                                                }
                                                ?></td>
                                                
                                                
                                                <td><?php echo $row->mrp;?></td>
                                                
                                                <td><?php echo  $row->qty;?></td>
                                                <td><?php echo $row->pv;?></td>
                                                <td><?php echo $row->total;?></td>
                                                
                                              
                                                
                                                     
                                        
                                        <!--        
                                          <td>
        								
        									<input type="hidden"  id="pname_<?php echo $row->id;?>" value="<?= $pname?>"> 
        									<input type="hidden"  id="sizename_<?php echo $row->id;?>" value="<?= $sizename?>"> 
        									<input type="hidden"  id="colorname_<?php echo $row->id;?>" value="<?= $colorname?>"> 
        									<input type="hidden"  id="prestock_<?php echo $row->id;?>" value="<?= $prestock?>"> 
        									
        									
									         <button type="button" class="btn btn-primary btn-sm" onclick="updstock(<?php echo $row->id;?>)" data-toggle="modal" data-target="#exampleModal">
                                             <i class="far fa-edit"></i> Edit
                                             </button> 
        									</td>-->
        								</tr>
								<?php 
								$i++;
								}
								
							
$queryd111 = $this->db->query("SELECT * FROM customer_invoice_list1 where bill_no='$payid'");
  $rowd111 = $queryd111->row(); 	
								
								?>
					<?php if(!empty($rowd111->dis) && $rowd111->dis!=0){?>
							<tr>
							      <td colspan="7" class="text-right">Sub Total</td>
							      <td><?php echo $total;?></td>
							</tr>
							

								<tr>
							      <td colspan="7" class="text-right">Discount</td>
							      <td><?php echo $rowd111->dis;?></td>
							</tr>


								<tr>
							      <td colspan="7" class="text-right">Total</td>
							      <td><?php echo $final=$total-$rowd111->dis;?></td>
							</tr>

								<tr>
							      <td colspan="7" class="text-right">Paid</td>
							      <td><?php echo $paid;?></td>
							</tr>
							<tr>
							      <td colspan="7" class="text-right">Dues</td>
							      <td><?php echo $final-$paid;?></td>
							</tr>
							
							<?php } else {?>
							
								<tr>
							      <td colspan="7" class="text-right">Sub Total</td>
							      <td><?php echo $total;?></td>
							</tr>
								<tr>
							      <td colspan="7" class="text-right">Paid</td>
							      <td><?php echo $paid;?></td>
							</tr>
								<tr>
							      <td colspan="7" class="text-right">Dues</td>
							      <td><?php echo $total-$paid;?></td>
							</tr>
							<?php } ?>
							</tbody>
							</table>
                    <!--<p><strong>Amount Chargeable (in words):</strong><br><b id="amttext"></b></p>-->
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>HSN/SAC</th>
                                <th>Taxable Value</th>
                                <th>Integrated Tax Rate</th>
                                <th>Tax Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>47079000</td>
                                <td><?php echo $t1=$total/100*95;?></td>
                                <td>5%</td>
                                <td><?php echo $total-$t1;?></td>
                            </tr>
                            <tr>
                                <td colspan="3" class="text-right"><strong>Total</strong></td>
                                <td><strong><?php echo $total-$t1;?></strong></td>
                            </tr>
                        </tbody>
                    </table>
                    <!--<p><strong>Tax Amount (in words):</strong><br></p>-->
                    <p><strong>Company's Bank Details:</strong><br>
                        Name: Y.B.S.TRADERS<br>
                        Bank Name: HDFC BANK<br>
                        A/C No.: 50200089998070<br>
                        Branch & IFSC Code: HDFC0009308
                    </p>
                    <p class="text-right">For Y. B. S. TRADERS<br><br>Authorised Signatory</p>
                    <p class="text-center">This is a Computer Generated Invoice</p>
                </div>
            </div>
        </div>
    </div>
</div>

			<!-- /.row -->
		</div>
		
		<button class="btn btn-primary" onclick="printInvoice()">Print Invoice</button>
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.content -->
</div>
<!-- /.content-wrapper -->



<?php $this->load->view('master/common/footer2.php'); ?>
<script src="<?php echo base_url();?>assets/js/pages/form-advanced.init.js"></script>


<script>
    function numberToWords(<?php echo $total;?>) {
    const a = [
        '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
        'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen',
        'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'
    ];
    const b = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];

    function convert(n) {
        if (n < 20) return a[n];
        if (n < 100) return b[Math.floor(n / 10)] + (n % 10 ? ' ' + a[n % 10] : '');
        if (n < 1000) return a[Math.floor(n / 100)] + ' Hundred' + (n % 100 ? ' ' + convert(n % 100) : '');
        if (n < 100000) return convert(Math.floor(n / 1000)) + ' Thousand' + (n % 1000 ? ' ' + convert(n % 1000) : '');
        if (n < 10000000) return convert(Math.floor(n / 100000)) + ' Lakh' + (n % 100000 ? ' ' + convert(n % 100000) : '');
        return convert(Math.floor(n / 10000000)) + ' Crore' + (n % 10000000 ? ' ' + convert(n % 10000000) : '');
    }

    const [whole, fraction] = amount.toString().split(".");
    let result = convert(parseInt(whole));

    if (fraction && parseInt(fraction) > 0) {
        result += ' and ' + convert(parseInt(fraction)) + ' Paise';
    }

    $('#amttext').text(result);
}

</script>

<script>
function printInvoice() {
    var printContents = document.getElementById('invoicePrint').innerHTML;
    var originalContents = document.body.innerHTML;

    document.body.innerHTML = printContents;

    window.print();

    document.body.innerHTML = originalContents;
    location.reload(); // Optional: Reload page to restore event listeners and scripts
}
</script>
