<?php require_once('../auth/auth.php');
require_once('../auth/acid_balance.php');
require_once('../auth/function.php');

$pritner_setup_query = mysqli_query($conn, "SELECT *,company.signature,company.co_logo FROM printer_setup 
  INNER JOIN company ON (printer_setup.coid = company.coid)
  LEFT JOIN branch b ON(printer_setup.brid=b.brid)
 WHERE printer_setup.coid ='$coid' AND printer_setup.bid ='$bid' AND printer_setup.brid='$brid' AND layout=1 LIMIT 1 ") or die(mysql_error());
$rowps = mysqli_fetch_assoc($pritner_setup_query);
$psid = $rowps['psid'];
$co_logo = $rowps['co_logo'];


?>

<?php
$invoice = $_GET['invoice'];
$g_acid = (isset($_GET['g_acid'])) ? $_GET['g_acid'] : 0;
$pinvoice_query = mysqli_query($conn, "SELECT 
      co_name,co_email,co_phone,co_address,b.brid,branch_name,branch_number,branch_email,branch_address,t.tid,t.invoice,t.m_invoice,date(t.date) tdate,t.date date_time,t.amount,t.details,t.em_id,a.acid,a.ac_name,a.ac_number,a.ac_details,a.ac_email,a.outlet_id,a.ac_area
      FROM transaction t 
      LEFT JOIN company c ON(t.coid=c.coid)
      LEFT JOIN branch b ON(t.brid=b.brid)
      LEFT JOIN accounts a ON(a.acid=t.dr_acid)
      WHERE t.coid='$coid' AND t.invoice ='$invoice' AND ttype='INSL' LIMIT 1") or die(mysqli_error($conn));
$pin_row = mysqli_fetch_assoc($pinvoice_query);

$co_name        = $pin_row['co_name'];
$co_address     = $pin_row['co_address'];
$co_phone       = $pin_row['co_phone'];
$co_email       = $pin_row['co_email'];


$get_brid           = $pin_row['brid'];
$branch_name    = $pin_row['branch_name'];
$branch_address = $pin_row['branch_address'];
$branch_number  = $pin_row['branch_number'];
$branch_email   = $pin_row['branch_email'];

$cacid          = $pin_row['acid'];
$ac_name        = $pin_row['ac_name'];
$ac_number      = $pin_row['ac_number'];
$ac_address     = $pin_row['ac_details'];
$ac_email       = $pin_row['ac_email'];
$outlet_id      = $pin_row['outlet_id'];
$ac_area_id      = $pin_row['ac_area'];
$route_name   =  mysqli_fetch_assoc(mysqli_query($conn, "SELECT route_name FROM route_area WHERE  roid='$ac_area_id' LIMIT 1 "))['route_name'];


$em_id         = $pin_row['em_id'];
$em_name = mysqli_fetch_assoc(mysqli_query($conn, "SELECT em_acname FROM em_employee WHERE  em_id='$em_id' LIMIT 1 "))['em_acname'];
$getwhare_house = mysqli_fetch_assoc(mysqli_query($conn, "SELECT wh_id FROM order_list WHERE  invoice='$invoice' LIMIT 1 "))['wh_id'];
$warehouse_code = mysqli_fetch_assoc(mysqli_query($conn, "SELECT warehouse_code FROM business_w_house WHERE  bwid='$getwhare_house' LIMIT 1 "))['warehouse_code'];
$d_man   =  mysqli_fetch_assoc(mysqli_query($conn, "SELECT d_man FROM order_list WHERE  invoice='$invoice' LIMIT 1 "))['d_man'];
$tid            = $pin_row['tid'];
$tdate          = $pin_row['tdate'];
$m_invoice      = $pin_row['m_invoice'];
$date_time      = $pin_row['date_time'];
$details        = $pin_row['details'];

$chbll = invbalance($cacid, $tdate, $tid);
$pr_due = ($chbll) ? $chbll['pr_due'] : 0;

$delivery  = 0;
$discount = 0;
$payment  = 0;
?>
<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 #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
        <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title">Invoice Preview</h3>
          <div class="card-tools">
            <button type="button" onclick="pdfMe('#m_receipt');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            <button type="button" class="btn btn-tool" onclick="printMe('#m_receipt')" 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="POS Invoice" data-dismiss="modal" onclick="$('#modal-sm').modal({backdrop: 'static'});pagecall('../sales/c_sales_invoice_pos.php?invoice=<?php echo $invoice; ?>','#smdview');"><i class="fas fa-print"></i> POS Invoice</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="m_receipt" style="overflow:hidden;position: relative;">
                      <input type="hidden" id="vounameset" value="<?php echo $pin_row['ac_name'] . '/' . $outlet_id; ?> Installment Invoice ">

          <div class="secondary_style" style="background-color: #f5f6fa;padding: 10px;">
            <!-- <img class="bgimg" src="../../dist/img/a.png"> -->
            <table class="invoice_table" cellpadding="0" cellspacing="0" style="<?php if ($rowps['bg_color'] !== "") {
                                                                                  echo 'border-top:10px solid' . $rowps['bg_color'];
                                                                                } ?>!important">
              <thead>
                <tr>
                  <th class="px-3" colspan="8">
                    <div class="invoice_head mt-1">
                      <div class="invoice_left">
                        <?php if ($rowps['invoice_logo'] == "On") {  ?>
                          <div class="tm_logo">
                            <?php if ($co_logo != "") : ?>
                              <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo; ?>" style="width:80px;height:75px;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                                                                                    } ?>!important">
                            <?php else : ?>
                              <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:75px;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                                                                            } ?>!important">
                          </div>
                        <?php endif; ?>

                      </div>
                    <?php } ?>
                    </div>
                    <div class="invoice_middle" style="margin-left:7px;">
                      <sss class="invoice_add_head lanbn" style="font-size:<?php echo $rowps['com_font_size']; ?>px;color:<?php echo $rowps['com_color']; ?>!important;"><?php echo $rowps['company_name']; ?></sss><br>
                      <sss class="lanbn"><?php echo $rowps['company_email']; ?></sss> #
                      <sss class="lanbn"><?php echo $rowps['company_phone']; ?></sss><br>
                      <sss class="lanbn"><?php echo $rowps['company_address']; ?></sss><br>
                      <?php if ($rowps['invoice_tin'] == "On") {  ?>
                        <sss class="lanbn">Tin Number : <?php echo $rowps['invoice_tin']; ?></sss>
                      <?php } ?>
                    </div>
                    <div class="invoice_right heading_color" style="margin-top:19px;text-align:center;<?php if ($rowps['border_color'] !== "") {
                                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                                      } ?> !important;">
                      <div>
                        <b class="invoice_name" style="color:<?php echo $rowps['com_color']; ?>!important;">Sale Invoice</b>
                      </div>
                    </div>
                    <div class="shape  shape_bg shape_border" style="<?php if ($rowps['head_bg'] !== "") {
                                                                        echo 'background-color:' . $rowps['head_bg'];
                                                                      } ?>!important;;<?php if ($rowps['border_color'] !== "") {
                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                      } ?> !important;"></div>
                    <div class="shape1 shape_bg shape_border" style="<?php if ($rowps['head_bg'] !== "") {
                                                                        echo 'background-color:' . $rowps['head_bg'];
                                                                      } ?>!important;;<?php if ($rowps['border_color'] !== "") {
                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                      } ?> !important;"></div>

          </div>
          <div class="p-3 mt-1 shape_border" style="<?php if ($rowps['border_color'] !== "") {
                                                      echo 'border-color:' . $rowps['border_color'];
                                                    } ?> !important;">



            <?php if ($rowps['branch_info'] == "On") {  ?>
              <div class="tm_grid_row" style="grid-template-columns: repeat(3, 1fr);">
                <div class="border_right" style="<?php if ($rowps['border_color'] !== "") {
                                                    echo 'border-color:' . $rowps['border_color'];
                                                  } ?> !important;">
                  <sss class="invoice_add_head heading_color" style="<?php if ($rowps['com_color'] !== "") {
                                                                        echo 'color:' . $rowps['com_color'];
                                                                      } ?>!important">From</sss><br>
                  <sss class="invoice_address lanbn">
                    <?php echo $rowps['branch_name']; ?> <br>
                    <?php echo $rowps['branch_number']; ?> <br>
                    <?php echo $rowps['branch_email']; ?> <br>
                    <?php echo $rowps['branch_address']; ?>
                  </sss>
                </div>
              <?php } else {  ?>
                <div class="tm_grid_row" style="grid-template-columns: repeat(2, 1fr);">
                <?php } ?>
                <div class="border_right" style="<?php if ($rowps['border_color'] !== "") {
                                                    echo 'border-color:' . $rowps['border_color'];
                                                  } ?> !important;">
                  <sss class="invoice_add_head heading_color" style="<?php if ($rowps['com_color'] !== "") {
                                                                        echo 'color:' . $rowps['com_color'];
                                                                      } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                      } ?> !important;">Bill To</sss><br>
                  Outlet Code : <sss class="invoice_address lanbn"><?php echo $outlet_id; ?> </sss><br>
                  Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <sss class="invoice_address lanbn"> <?php echo $ac_name; ?></sss><br>
                  Number &nbsp; : <sss class="invoice_address lanbn"> <?php echo $ac_number; ?></sss><br>

                  <?php if ($rowps['cemail_info'] == "On") {  ?>
                    Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <sss class="invoice_address lanbn"><?php echo $ac_email; ?> </sss><br>
                  <?php } ?>
                  Address &nbsp;&nbsp;: <sss class="invoice_address lanbn"><?php echo $ac_address; ?> </sss><br>
                  <?php if ($rowps['route_info'] == "On") {  ?>
                    Route &nbsp;&nbsp;: <sss class="invoice_address lanbn"><?php echo  $route_name; ?> </sss><br>
                  <?php } ?>

                </div>

                <div>
                  <?php if ($rowps['m_invoice'] == "On") {  ?>
                    Memo No &nbsp;&nbsp;&nbsp; : <sss class="invoice_address lanbn"><?php echo $m_invoice; ?></sss><br>
                  <?php } ?>
                  <?php if ($rowps['print_time'] == "On") {  ?>
                    Print time &nbsp; : <sss class="invoice_address lanbn"><?php echo date('d/m/y h:i:s a', strtotime($datetime)); ?></sss><br>
                  <?php } ?>
                  Created Date &nbsp; : <sss class="invoice_address lanbn"><?php echo date('d/m/y h:i:s a', strtotime($date_time)); ?></sss><br>
                  Created &nbsp; : <sss class="invoice_address lanbn"> <?php echo $em_name; ?></sss><br>
                  <?php if ($rowps['wh_info'] == "On") {  ?>
                    wharehouse Code &nbsp;&nbsp;&nbsp; : <sss class="invoice_address lanbn"><?php echo $warehouse_code; ?></sss><br>
                  <?php } ?>

                  <?php if ($rowps['dman_info'] == "On") {  ?>
                    Delivery Man &nbsp;&nbsp;&nbsp; : <sss class="invoice_address lanbn"><?php echo $d_man; ?></sss><br>
                  <?php } ?>


                  Invoice : <sss class="invoice_address lanbn"><?php echo $invoice; ?>
                  </sss>
                </div>

                </div>
              </div>
              </th>
              </tr>
              <tr>
                <th colspan="8">&nbsp;</th>
              </tr>
              <tr>
                <th style="width:15px;<?php if ($rowps['border_color'] !== "") {
                                        echo 'border-color:' . $rowps['border_color'];
                                      } ?> !important;"></th>
                <th class="text-center shape_border thstyle w-5" style="padding: 10px 15px;border-right:;<?php if ($rowps['text_color'] !== "") {
                                                                                                            echo 'color:' . $rowps['text_color'];
                                                                                                          } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                                                                          } ?> !important;
            <?php if ($rowps['bg_color'] !== "") {
              echo 'background:' . $rowps['bg_color'];
            } ?> !important; <?php if ($rowps['border_color'] !== "") {
                                echo 'border-color:' . $rowps['border_color'];
                              } ?> !important;"> S/L</th>
                <th class="shape_border thstyle" style="padding: 10px 15px;border-right:;border-left:none;<?php if ($rowps['text_color'] !== "") {
                                                                                                            echo 'color:' . $rowps['text_color'];
                                                                                                          } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                            echo 'background:' . $rowps['bg_color'];
                                                                                                                          } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                                                                                          } ?> !important;">Item Description</th>
                <th class="text-center shape_border thstyle w-10" style="padding: 10px 15px;border-right:;border-left:none;<?php if ($rowps['text_color'] !== "") {
                                                                                                                              echo 'color:' . $rowps['text_color'];
                                                                                                                            } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                                              echo 'background:' . $rowps['bg_color'];
                                                                                                                                            } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                                                                                            } ?> !important;">QTY</th>
                <th class="text-center shape_border thstyle w-15" style="padding: 10px 15px;border-right:;border-left:none;<?php if ($rowps['text_color'] !== "") {
                                                                                                                              echo 'color:' . $rowps['text_color'];
                                                                                                                            } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                                              echo 'background:' . $rowps['bg_color'];
                                                                                                                                            } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                                                                                            } ?> !important;">Price</th>
                <th class="text-right shape_border thstyle w-10" style="padding: 10px 15px;border-right:;border-left:none;<?php if ($rowps['text_color'] !== "") {
                                                                                                                            echo 'color:' . $rowps['text_color'];
                                                                                                                          } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                                            echo 'background:' . $rowps['bg_color'];
                                                                                                                                          } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                                                                                                          } ?> !important;">Discount</th>
                <th class="text-right shape_border thstyle w-15" style="padding: 10px 15px;border-left:none;<?php if ($rowps['text_color'] !== "") {
                                                                                                              echo 'color:' . $rowps['text_color'];
                                                                                                            } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                              echo 'background:' . $rowps['bg_color'];
                                                                                                                            } ?>!important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                                                                            } ?> !important;">Total</th>
                <th style="width:15px;"></th>
              </tr>
              </thead>
              <tbody>
                <?php
                $order_list_query = mysqli_query($conn, "SELECT i.imei_status,i.item_name,i.item_details,i.item_code,i.war_type,i.wa_dmy,
                      o.oid,o.pro_id,o.qty,o.net_amount,o.amount,o.dis_amount,o.amount,o.wh_id,
                      item_group.pgroup_name,item_catagory.pcat_name,item_brand.pbrand_name  FROM order_list o 
                      LEFT JOIN item i ON(o.pro_id=i.pro_id)
                      LEFT JOIN item_group ON(i.pgid=item_group.pgid)
                      LEFT JOIN item_catagory ON (i.pcid=item_catagory.pcid)
                      LEFT JOIN item_brand ON (i.pbid=item_brand.pbid)


                      
                      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_price = $orw['amount'];
                  $row_bill  = $orw['net_amount'];
                  $imei_status = $orw['imei_status'];
                  $item_id  = $orw['pro_id'];
                  $invoice_bill += $row_bill;

                ?>


                  <tr>


                    <td style="width:15px;<?php if ($rowps['border_color'] !== "") {
                                            echo 'border-color:' . $rowps['border_color'];
                                          } ?> !important;"></td>

                    <td class="text-center shape_border" style="padding: 5px 15px;border-right:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                    } ?> !important;">
                      <dt><?php echo ++$i; ?></dt>
                    </td>

                    <td class=" shape_border" style="padding: 5px 15px;border-right:none;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                                                          } ?> !important;">
                      <?php if ($rowps['item_group'] == "On") {
                        echo $orw['pgroup_name'] . " ";
                      }
                      if ($rowps['item_brand'] == "On") {
                        echo $orw['pbrand_name'] . " ";
                      }
                      if ($rowps['item_catagory'] == "On") {
                        echo $orw['pcat_name'] . " ";
                      }; ?>
                      <?php echo $orw['item_name']; ?> <?php echo $orw['item_details']; ?> , <?php if ($rowps['item_code'] == "On") {
                                                                                                echo $orw['item_code'];
                                                                                              } ?>
                      <?php if ($rowps['item_warranty'] == "On") {
                        if ($orw['war_type'] == 'N') {
                          echo $warrenty = '';
                        } else if ($orw['war_type'] == 'D') {
                          echo $warrenty = $orw['wa_dmy'] . 'Days';
                        } else if ($orw['war_type'] == 'M') {
                          echo $warrenty = $orw['wa_dmy'] . ' Month';
                        } else if ($orw['war_type'] == 'Y') {
                          echo $warrenty = $orw['wa_dmy'] . ' Years';
                        } else if ($orw['war_type'] == 'L') {
                          echo  $warrenty = $orw['wa_dmy'] . ' Lifetime';
                        }
                      } ?>

                      <?php
                      if ($imei_status == "1") {
                        echo  $rowps['imei_sl_text'] . '<br>';
                        $iteq = mysqli_query($conn, "SELECT * FROM imei_tracker WHERE  coid='$coid' AND bid='$bid' AND brid='$brid' AND pro_id='$item_id' AND active='1' AND invoice='$invoice'  ") or die(mysqli_error($conn));
                        while ($iter = mysqli_fetch_assoc($iteq)) {
                          echo $iter['imei_code'] . '(' . $iter['imei_color'] . ')' . ';  ';
                        }
                      }
                      ?>
                    </td>
                    <td class="text-center shape_border" style="padding: 5px 15px;border-right:none;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                                                      } ?> !important;">
                      <dt><?php echo number_format($orw['qty'], 2); ?></dt>
                    </td>
                    <td class="text-center shape_border" style="padding: 5px 15px;border-right:none;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                                                      } ?> !important;">
                      <dt> <?php echo number_format($row_price, 2); ?></dt>
                    </td>
                    <td class="text-center shape_border" style="padding: 5px 15px;border-right:none;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                                        echo 'border-color:' . $rowps['border_color'];
                                                                                                                      } ?> !important;">
                      <dt><?php echo number_format($orw['dis_amount'], 2); ?></dt>
                    </td>
                    <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                    echo 'border-color:' . $rowps['border_color'];
                                                                                                  } ?> !important;">
                      <dt><?php echo number_format($row_bill, 2); ?></dt>
                    </td>
                    <td style="width:15px;"></td>
                  </tr>

                <?php } ?>
              </tbody>
              <tfoot>
                <tr>
                  <td style="width:15px;"></td>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>Total </dt>
                  </td>
                  <td class="text-center shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                } ?> !important;">
                    <dt><?php echo number_format($sum_qty); ?></dt>
                  </td>
                  <td colspan="2" class="text-center shape_border" style="padding: 5px 15px;border-right:none !important;<?php if ($rowps['border_color'] !== "") {
                                                                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                                                                          } ?> !important;">
                    <dt></dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                } ?> !important;">
                    <dt><?php echo number_format($invoice_bill); ?></dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>
                <tr>
                  <th class="pt-1" colspan="8"></th>
                </tr>
                <tr>
                  <td style="width:15px;" rowspan="7"></td>
                  <td colspan="2" rowspan="7" class="" style="vertical-align: top;">
                    <?php if ($rowps['ins_info'] == "On") {  ?>

                      <table class="invoice_table" cellpadding="0" cellspacing="0" style="<?php if ($rowps['border_color'] !== "") {
                                                                                            echo 'border-top:10px solid' . $rowps['border_color'];
                                                                                          } ?>!important">
                        <thead>
                          <tr>
                            <th title="No. of ins" class="shape_border text-center  w-10" style="<?php if ($rowps['border_color'] !== "") {
                                                                                                    echo 'border-color:' . $rowps['border_color'];
                                                                                                  } ?> !important;"><?php echo language(15); ?></th>
                            <th title="Installment Date" class=" shape_border text-center  w-50" style="<?php if ($rowps['border_color'] !== "") {
                                                                                                          echo 'border-color:' . $rowps['border_color'];
                                                                                                        } ?> !important;"><?php echo language(362); ?></th>
                            <th title="Per Ins" class="text-center shape_border  w-40" style="<?php if ($rowps['border_color'] !== "") {
                                                                                                echo 'border-color:' . $rowps['border_color'];
                                                                                              } ?> !important;"><?php echo language(357); ?></th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                          $ins_q = mysqli_query($conn, "SELECT ins_qty,DATE_FORMAT(ins_date, '%d/%m/%Y') ins_date,per_ins FROM intallment WHERE coid='$coid' AND invoice='$invoice' ORDER BY ins_qty ASC") or die(mysqli_error($conn));
                          while ($insr = mysqli_fetch_assoc($ins_q)) {
                          ?>
                            <tr>
                              <td class="text-center shape_border" style="<?php if ($rowps['border_color'] !== "") {
                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                          } ?> !important;">
                                <dt><?php echo $insr['ins_qty']; ?></dt>
                              </td>
                              <td class="text-center shape_border" style="<?php if ($rowps['border_color'] !== "") {
                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                          } ?> !important;">
                                <dt><?php echo $insr['ins_date']; ?></dt>
                              </td>
                              <td class="text-center shape_border" style="<?php if ($rowps['border_color'] !== "") {
                                                                            echo 'border-color:' . $rowps['border_color'];
                                                                          } ?> !important;">
                                <dt><?php echo $insr['per_ins']; ?></dt>
                              </td>
                            </tr>
                          <?php } ?>
                        </tbody>
                      </table>
                    <?php } ?>

                  </td>
                  <td rowspan="7"></td>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>Delivery Charge :</dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                                } ?> !important;">
                    <dt>
                      <?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);
                      ?>
                    </dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>
                <tr>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>Discount :</dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                                } ?> !important;">
                    <dt>
                      <?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);
                      ?>
                    </dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>
                <tr>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>Sub Total :</dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                                } ?> !important;">
                    <dt><?php echo number_format(($invoice_bill + $delivery) - $discount); ?></dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>
                <tr>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>Payment :</dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                                } ?> !important;">
                    <dt>
                      <?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_I' AND t.cr_acid='$cacid' AND note='esale_payment' LIMIT 1"))['amount'];
                      echo number_format($payment);
                      ?>
                    </dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>
                <?php if ($rowps['current_in_due'] == "On") { ?>
                  <tr>
                    <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                                echo 'border-color:' . $rowps['border_color'];
                                                                                              } ?> !important;">
                      <dt>Current Invoice Due :</dt>
                    </td>
                    <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                    echo 'border-color:' . $rowps['border_color'];
                                                                                                  } ?> !important;">
                      <dt><?php echo number_format((($invoice_bill + $delivery) - $discount) - $payment); ?></dt>
                    </td>
                    <td style="width:15px;"></td>
                  </tr>
                <?php } else { ?>
                  <tr>
                    <td colspan="2"></td>
                    <td></td>
                    <td></td>
                  </tr>
                <?php } ?>

                <?php if ($rowps['previous_due'] == "On") { ?>
                  <tr>
                    <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                                echo 'border-color:' . $rowps['border_color'];
                                                                                              } ?> !important;">
                      <dt>Previous Balance :</dt>
                    </td>
                    <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                    echo 'border-color:' . $rowps['border_color'];
                                                                                                  } ?> !important;">
                      <dt><?php echo number_format($pr_due); ?></dt>
                    </td>
                    <td style="width:15px;"></td>
                  </tr>
                <?php } else { ?>
                  <tr>
                    <td colspan="2"></td>
                    <td></td>
                    <td></td>
                  </tr>
                <?php } ?>

                <tr>
                  <td colspan="2" class="text-right shape_border" style="padding: 5px 15px;<?php if ($rowps['border_color'] !== "") {
                                                                                              echo 'border-color:' . $rowps['border_color'];
                                                                                            } ?> !important;">
                    <dt>current due :</dt>
                  </td>
                  <td class="text-right shape_border" style="padding: 5px 15px;border-left:none;<?php if ($rowps['border_color'] !== "") {
                                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                                } ?> !important;">
                    <dt>
                      <?php
                      if ($pr_due > 0) {
                        echo number_format(((($invoice_bill + $delivery) - $discount) - $payment) + $pr_due);
                      } else if ($pr_due < 0) {
                        echo number_format(((($invoice_bill + $delivery) - $discount) - $payment) + ($pr_due));
                      } else {
                        echo number_format((($invoice_bill + $delivery) - $discount) - $payment);
                      }
                      ?>
                    </dt>
                  </td>
                  <td style="width:15px;"></td>
                </tr>





                <tr>
                  <th colspan="8" style="padding-top: 8px;">
                    <dt><span style="font-weight: 600;margin-left: 15px;">In Word :</span> <?php echo inword($invoice_bill); ?></dt>
                    <dt>&nbsp;</dt>
                    <dt>&nbsp;</dt>
                    <dt>&nbsp;</dt>
                  </th>
                </tr>

                <?php if ($rowps['invoice_narrtion'] == "On") { ?>
                  <tr>
                    <td style="width:15px;"></td>

                    <td colspan="6" class="shape_border lanbn" style="padding: 5px 15px;vertical-align: text-top;<?php if ($rowps['border_color'] !== "") {
                                                                                                                    echo 'border-color:' . $rowps['border_color'];
                                                                                                                  } ?> !important;">
                      <dt> Narration :</dt>
                      <?php echo $details; ?>
                    </td>
                    <td style="width:15px;<?php if ($rowps['border_color'] !== "") {
                                            echo 'border-color:' . $rowps['border_color'];
                                          } ?> !important;"></td>

                  </tr>
                <?php  }  ?>

                <tr>
                  <th colspan="8" style="padding-top: 8px;">
                    <dt><span style="font-weight: 600;margin-left: 15px;">
                    <dt>&nbsp;</dt>
                    <dt>&nbsp;</dt>
                    <dt>&nbsp;</dt>
                  </th>
                </tr>
                <?php if ($rowps['invoice_trams'] == "On") { ?>
                  <tr>
                    <td style="width:15px; padding-top:5px;<?php if ($rowps['border_color'] !== "") {
                                                              echo 'border-color:' . $rowps['border_color'];
                                                            } ?> !important;"></td>
                    <td colspan="6" class="lanbn">
                      <dt class="invoice_add_head heading_color " style="<?php if ($rowps['com_color'] !== "") {
                                                                            echo 'color:' . $rowps['com_color'];
                                                                          } ?>;<?php if ($rowps['border_color'] !== "") {
                                                                                  echo 'border-color:' . $rowps['border_color'];
                                                                                } ?> !important;"> Terms & Condition : </dt>
                      <?php echo $rowps['terms']; ?>
                    </td>
                    <td style="width:15px;<?php if ($rowps['border_color'] !== "") {
                                            echo 'border-color:' . $rowps['border_color'];
                                          } ?> !important;"></td>
                  </tr>
                <?php } ?>
                
                
                    <tr>
                    <th colspan="6">&nbsp;</th>
                    <th class="text-center">
                        <img style="margin-bottom: -16px;" src="https://chart.apis.google.com/chart?cht=qr&chs=150x150&chl=http://welldiv.com/erp/qr_invoice/index.php?invoice=<?php echo $invoice; ?>" alt=" Scan Now Your Invoice">
                    <span style=""> Scan Now Your Invoice </span>
                    </th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
            

                <tr>
                  <th>

                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td class="text-center" style="<?php if ($rowps['border_color'] !== "") {
                                                    echo 'border-color:' . $rowps['border_color'];
                                                  } ?> !important;">
                    <?php if ($rowps['sig_nature'] == "On") { ?>
                      <img id="img_data" style="height: 100px;width:150px;" src="data:image/gif;base64,<?php echo $rowps['signature']; ?>" alt="User signature ">
                      <span> <?php echo $date; ?> </span>
                    <?php } ?>
                    
                    
                  </td>
                  </th>
                </tr>

            



                <tr>


                  <td colspan="2" style="font-size: 12px; text-align:center;font-weight:700 ; border-top:1px dotted;<?php if ($rowps['border_color'] !== "") {
                                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                                    } ?> !important; ">
                    <?php echo $rowps['fop_one']; ?>
                  </td>


                  <td colspan="2" style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted;<?php if ($rowps['border_color'] !== "") {
                                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                                    } ?> !important; ">

                    <?php echo $rowps['fop_two']; ?>
                  </td>
                  <td colspan="2" style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted;<?php if ($rowps['border_color'] !== "") {
                                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                                    } ?> !important; ">

                    <?php echo $rowps['fop_three']; ?>
                  </td>
                  <td colspan="2" style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted;<?php if ($rowps['border_color'] !== "") {
                                                                                                                      echo 'border-color:' . $rowps['border_color'];
                                                                                                                    } ?> !important; ">
                    <?php echo $rowps['fop_four']; ?>
                  </td>
                </tr>

                <tr>
                  <th colspan="8">&nbsp;</th>
                </tr>
                <tr>
                  <th class="powerby text-center" colspan="8" style="font-size:10px;<?php if ($rowps['text_color'] !== "") {
                                                                                      echo 'color:' . $rowps['text_color'];
                                                                                    } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                      echo 'background:' . $rowps['bg_color'];
                                                                                                    } ?>!important;<?php if ($rowps['bg_color'] !== "") {
                                                                                                                      echo 'border-color:' . $rowps['bg_color'];
                                                                                                                    } ?> !important;"> <?php echo $welldiv ?> </th>

                </tr>
              </tfoot>
              </table>
          </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);
              pagecall('../accounts/ac_profile.php?acid=<?php echo $g_acid ?>', '#ac_view', 0);
              $(".modal").modal("hide");
            });
        }
      })

    });
  });
</script>