<?php require_once('../auth/auth.php');require_once('../auth/function.php');?>
<?php 
    $invoice=$_GET['invoice']; 
    $g_acid = (isset($_GET['g_acid']))?$_GET['g_acid']:0;
    $pinvoice_query= mysqli_query($conn,"SELECT 
      branch_name,branch_number,branch_email,branch_address,t.tid,t.invoice,t.amount,cs.c_id,cs.c_name,cs.c_gsm,cs.c_address
      FROM transaction t 
      LEFT JOIN branch b ON(t.brid=b.brid)
      LEFT JOIN c_customer cs ON(cs.c_id=t.e_acid)
      WHERE t.coid='$coid' AND t.invoice ='$invoice' AND ttype='ECSL' LIMIT 1") or die(mysqli_error($conn));
    $pin_row= mysqli_fetch_assoc($pinvoice_query);

    $co_name        = $pin_row['branch_name'];
    $co_address     = $pin_row['branch_address'];
    $co_phone       = $pin_row['branch_number'];
    $co_email       = $pin_row['branch_email'];

    $wacid          = $pin_row['c_id'];
    $ac_name        = $pin_row['c_name'];
    $ac_number      = $pin_row['c_gsm'];
    $ac_address     = $pin_row['c_address'];


    $tid            = $pin_row['tid'];
    
    $delivery  = 0;
    $discount  = 0;
    $payment   = 0;
?>
<script language="javascript">
  function PrintMeY(invoiceID) {
    var disp_setting="toolbar=yes,location=no,";
    disp_setting+="directories=yes,menubar=yes,";
    disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
    var content_vlue = document.getElementById(invoiceID).innerHTML;
    var docprint=window.open("","",disp_setting);
    docprint.document.open();
    docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
    docprint.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
    docprint.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">');
    docprint.document.write('<head><title>Print</title>');
    docprint.document.write('<style type="text/css">body{ margin:0px;');
    docprint.document.write('font-family:SolaimanLipi, Arial,verdana;color:#000;');
    docprint.document.write('font-family:SolaimanLipi, Arial, Geneva, sans-serif; font-size:10px;width: 288px;}');
    docprint.document.write('a{color:#000;text-decoration:none;} </style>');
    docprint.document.write('</head><body onLoad="self.print()">');
    docprint.document.write(content_vlue);
    docprint.document.write('</body></html>');
    docprint.document.close();
    docprint.focus();
  }
</script>

<section class="content">
  <div class="row">
    <div class="col-md-12">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid black;border-right:1px solid black;border-bottom:1px solid black;">
        <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title">Sales Invoice </h3>
          <div class="card-tools">
            <button type="button" class="btn btn-tool" onclick="PrintMeY('invoiceID')" title="Print Now"><i class="fas fa-print"></i></button>
            <div class="btn-group">
              <button type="button" class="btn btn-tool" data-toggle="dropdown"  title="More Tools"><i class="fa-solid fa-ellipsis-vertical"></i>
              </button>
              <ul class="dropdown-menu dropdown-menu-right">
                <li><a class="dropdown-item" title="Regular Invoice" data-dismiss="modal" onclick="$('#modal-lg').modal({backdrop: 'static'});pagecall('../sales/c_sales_invoice.php?invoice=<?php echo $invoice;?>','#mdview');"><i class="fas fa-print"></i> Regular Invoice</a></li>
                <div class="dropdown-divider m-0"></div>
            <?php if (per('inv_edit')==1) { ?>
                <li><a class="dropdown-item" title="Edit" data-dismiss="modal" onclick="$('#modal-lg').modal({backdrop: 'static'});pagecall('../sales/edit_sales_index.php?g_acid=<?php echo $g_acid?>'+'&invoice=<?php echo $invoice;?>','#mdview');"><i class="fas fa-edit"></i> Edit Now</a></li>
                <div class="dropdown-divider m-0"></div>
            <?php } if (per('inv_delete')==1) { ?>
                <li><a class="dropdown-item" title="Delete" id="delete"><i class="fas fa-trash"></i> Delete Now</a></li>
            <?php } ?> 
              </ul>
            </div>           
            <button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>   

        <div class="card-body p-1" id="invoiceID" style="overflow:hidden;position: relative;">
          <div class="col-lg-12">
            <div style="float: left; width: 100%;margin-top: -5px;text-align: center;">
              <address style="margin-bottom: 0px;font-size: 11px;margin-top: 5px;">
                <strong style="font-size: 18px;font-weight: bold"><?php echo $co_name; ?></strong><hr style="margin: 0px 0px;border: 0.5px solid #066265;">
                <?php echo $co_address; ?>
                <?php echo $co_phone; ?> # <?php echo $co_email; ?>
              </address>
              <hr style="margin:0px 0px;border:0.5px solid #066265;">
            </div>
            <div style="float:left;width: 100%;">
              <div style="padding: 0px;min-height: 20px;min-height: 20px; margin-bottom: 2px;border: 1px solid #e3e3e3;border-radius: 2px;">
                <div class="clearfix">
                  <div><strong><center><b style="font-size: 19px;">Sales Invoice</b></center></strong></div>
                </div>
              </div>
              <hr style="margin-top: 0px; margin-bottom: 2px;border: 0.5px solid #066265;">
            </div>
            <div class="col-xs-12" style="float:left;width:100%">
              <div style="padding: 5px; font-size:8px;">
                <div>
                  <div  style="float: left; width: 100%;font-size: 11px"> VOUCHER NO : <?php echo $invoice ?></div> 
                  <div  style="float: left; width: 100%;font-size: 12px"> DATE TIME  : <?php echo $datetime;?> </div>
                </div>
              </div>
            </div>
             <hr style="margin-top:0px;margin-bottom:2px;border:0.5px solid #066265;">


            <div class="col-xs-12" style="float: left; width: 100%;font-size: 12px;">
              <address style="font-size: 12px;margin-bottom: 0px; margin-top: 1px;line-height: 20px;border-bottom: 1.3px dotted;">
                Name : <?php echo $ac_name; ?>
              </address>
            </div>
            <div class="col-xs-4" style="float: left; width: 100%;font-size: 12px;">
                <address style="font-size: 12px;margin-bottom: 0px; margin-top: 1px;line-height: 20px;border-bottom: 1.3px dotted;">
                 Mobile: <?php echo $ac_number; ?>
               </address>
            </div>
            <div class="col-xs-4" style="float: left; width: 100%;font-size: 12px;">
                <address style="font-size: 12px;margin-bottom: 0px; margin-top: 1px;line-height: 20px;border-bottom: 1.3px dotted;">
                       Address : <?php echo $ac_address; ?>
                </address>
            </div>
<!-- frist part complete -->





   <div class="col-xs-12" style="float: left; width: 100%">

    <br/>
    <table cellpadding="0"  cellspacing="0"  class=" t-table" style="font-size: 10px; width: 100%; margin-bottom: 3px;margin-top: -10px;">
      <tr>
        <th style="font-size: 12px;text-align:center;background-color: black;color:white;width: 8%">S/L</th>
        <th style="font-size: 12px;text-align:left;background-color: black;color:white">Description</th>
        <th style="font-size: 12px;text-align:center;background-color: black;color:white;width: 5%">Unit</th>
        <th style="font-size: 12px;text-align:center;background-color: black;color:white;width: 15%">Price</th>
        <th style="font-size: 12px;text-align:center;background-color: black;color:white;width: 15%">Total</th>

      </tr>
    </thead>

    <tbody>

      <?php
          $order_list_query= mysqli_query($conn,"SELECT i.item_name,i.item_details,i.item_code,o.oid,o.qty,o.net_amount,o.dis_amount,o.amount FROM order_list o LEFT JOIN item i ON(o.pro_id=i.pro_id) WHERE o.coid='$coid' AND o.invoice='$invoice' ORDER BY oid ASC") or die(mysqli_error($conn));
          $i=0;
          $sum_qty=0;
          $invoice_bill=0;
        while($orw= mysqli_fetch_assoc($order_list_query)){
          $sum_qty   += $orw['qty'];
          $row_bill  = $orw['net_amount'];
          $row_price  = ($orw['net_amount'] / $orw['qty']);
          $invoice_bill += $row_bill;
      ?>

      <tr> 
        <td style="font-size: 12px;text-align: center;width: 5%;border: 0.1px solid black;padding: 0px 3px;overflow-x: hidden;overflow-y: hidden;white-space: nowrap;" ><?php echo ++$i;?></td>


        <td style="font-size: 12px;text-align:left;padding: 4px;border: 0.1px solid black; padding: 0px 3px;">
          <?php echo $orw['item_name'];?> <?php echo $orw['item_details'];?> <?php // echo $orw['item_code'];?>

        </td>

        <td style="font-size: 12px;text-align:center;padding: 4px;;border: 0.1px solid black; padding: 0px 3px;"> 
          <?php echo number_format($orw['qty']);?> </td>

        <td style="font-size: 12px;text-align:right;padding: 4px;;border: 0.1px solid black; padding: 0px 3px;"> 
          <?php  echo number_format($row_price);?> </td>


          <td style="font-size: 10px;text-align:right;width: 13%;;border: 0.1px solid black;"><?php echo number_format($row_bill);?></td>
        </tr>

<?php } ?>


      <tr>
          <td colspan="2" style="font-size: 12px;text-align:right;border-left: 0.1px solid black;border-bottom: 0.1px solid black;border-top: 0.1px solid black; padding: 0px 3px;"> Total = </td>

          <td style="font-size: 12px;text-align:center;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;border-top: 0.1px solid black;">
            <?php echo number_format($sum_qty) ;?>
          </td>

          <td style="font-size: 12px;text-align:right;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;border-top: 0.1px solid black;"></td>

          <td style="font-size: 12px;text-align:right; border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;border-right: 0.1px solid black;border-top: 0.1px solid black;">
           <b style="font-size: 13; "> <?php echo number_format($invoice_bill);?>
         </b> 
       </td>
     </tr>

     <tr>
      <td colspan="4" style="font-size: 12px;text-align: right;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">Delivery Charge</td>
      <td  style="text-align: right;border-left: 0.1px solid black;border-right: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">
        <?php 
           @$delivery= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction WHERE coid='$coid' AND invoice ='$invoice' AND ttype='R' AND note = 'esale_delivary' LIMIT 1"))['amount'];
                 echo number_format($delivery);
        ?>
      </td>
    </tr>

     <tr>
      <td colspan="4" style="font-size: 12px;text-align: right;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">Discount</td>
      <td  style="text-align: right;border-left: 0.1px solid black;border-right: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">
        <?php 
           @$discount= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction WHERE coid='$coid' AND invoice ='$invoice' AND ttype='P' AND note = 'esale_discount' LIMIT 1"))['amount'];
           echo number_format($discount);
        ?>
      </td>
    </tr>

<tr>

  <td colspan="4" style="font-size: 13px;text-align: right;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;" >Grand Total</td>
  <td  style="font-size: 13px;text-align: right;border-left: 0.1px solid black;border-right: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;"><b><?php echo number_format((($invoice_bill + $delivery)-$discount));?>
    </b>    
  </td>
</tr>

<tr>
  <td colspan="4" style="font-size: 12px;text-align: right;border-left: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">Payment</td>
  <td style="text-align: right;border-left: 0.1px solid black;border-right: 0.1px solid black;border-bottom: 0.1px solid black; padding: 0px 3px;">
    <?php
       $payment= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction t WHERE t.coid='$coid' AND t.invoice ='$invoice' AND t.ttype='R' AND note='esale_payment' LIMIT 1"))['amount'];
      echo number_format($payment);
    ?>
  </td>
</tr>
</tbody>
</table>
</div>







<div class="col-xs-12" style="font-size: 10px;float: left; width: 100%">
  <address style="font-size: 10px;margin-bottom: 0px; margin-top: 11px;margin-left:15px; line-height: 20px;border-bottom: 1.3px dotted;text-transform: uppercase;">
   In Word : <?php echo inword($invoice_bill);?>
 </address>
</div><!--end .col -->

<div class="col-xs-12" id="footer" style="float: left; width: 100%">
  <address style="margin-bottom: 0px; margin-top: 5px;line-height: 12px;">
    <table style="width: 100%;">
      <tr>
        <td class="hidden-border" style="font-size: 10px; width: 50%;text-align: center">
         Buyer s signature
       </td>
       <td class="hidden-border" style="font-size: 10px; width: 50%; text-align: center">
        Seller s signature
      </td>
    </tr>
  </table>
  <br>


  <div class="col-sm-12" style="width: 100%;background: ;color: ;">
    <ssssss style= "font-size: 8px"> <center><b>&nbsp;Powered by: WellDiv # Web: www.welldiv.com</b></center> </ssssss>
  </div>




</address>
</div>


</div>
</div>
</div>
</div>
</div>
</section>


<style type="text/css">
  @font-face { 
    font-family: SolaimanLipi; 
    src: local('SolaimanLipi'),
    url('../../fonts/solaimanlipi.woff') format('woff'),
    url('../../fonts/solaimanlipi.woff2') format('woff2');
  }


  @page { margin: 0 }
  body { margin: 0 }
  .sheet {
    margin: 0;
    overflow: hidden;
    position: relative;
    box-sizing: border-box;
    page-break-after: always;
    font-size: 10px;
  }

/** Paper sizes **/
body.A3               .sheet { width: 299mm; height: 419mm }
body.A3.landscape     .sheet { width: 420mm; height: 296mm }
body.A4               .sheet { width: 210mm; height: 296mm }
body.A4.landscape     .sheet { width: 297mm; height: 209mm }
body.A5               .sheet { width: 148mm; height: 209mm }
body.A5.landscape     .sheet { width: 210mm; height: 147mm }
body.letter           .sheet { width: 216mm; height: 279mm }
body.letter.landscape .sheet { width: 280mm; height: 215mm }
body.legal            .sheet { width: 216mm; height: 356mm }
body.legal.landscape  .sheet { width: 357mm; height: 215mm }

/** Padding area **/
.sheet.padding-10mm { padding: 10mm }
.sheet.padding-15mm { padding: 15mm }
.sheet.padding-20mm { padding: 20mm }
.sheet.padding-25mm { padding: 25mm }

/** For screen preview **/
@media screen {
  body { background: #e0e0e0 }
  .sheet {
    background: white;
    box-shadow: 0 .5mm 2mm rgba(0,0,0,.3);
    margin: 5mm auto;
  }
}

/** Fix for Chrome issue #273306 **/
@media print {
 body.A3.landscape { width: 420mm }
 body.A3, body.A4.landscape { width: 297mm }
 body.A4, body.A5.landscape { width: 210mm }
 body.A5                    { width: 148mm }
 body.letter, body.legal    { width: 216mm }
 body.letter.landscape      { width: 280mm }
 body.legal.landscape       { width: 357mm }
}

</style>
</div>
</div>
</div>
</div>
</div>

</section>


<script>
$(document).ready(function() {
$('#delete').on('click', function(e){
e.preventDefault();

  Swal.fire({
    title: 'Are you sure?',
    text: "You won't be able to revert this Invoice!",
    icon: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#d33',
    cancelButtonColor: '#3085d6',
    confirmButtonText: 'Yes, delete it!'
  }).then((result) => {
    if (result.isConfirmed) {
      Swal.fire({icon: 'success',title: 'Your Invoice is Successfully Delete',showConfirmButton: false,timer: 1500})
      $.post("../sales/invoice_delete.php",
         {oinvoice:'<?php echo $invoice;?>'},
          function(data){
             toastr.success(Successfully, yourdata);
            $(".modal").modal("hide"); 
            if($('#ac_view').length){pagecall('../accounts/ac_profile.php?acid=<?php echo $g_acid?>','#ac_view');}       
      });         
    }
  })

  });
}); 
</script>
