<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php   $getins_invoice = $_GET['ins_invoice'];

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




    $pinvoice_query= mysqli_query($conn,"SELECT 
      co_name,co_email,co_phone,co_address,co_logo,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.userid,a.acid,a.ac_name,a.ac_number,a.ac_details,a.ac_email,a.ac_alise,a.outlet_id,a.ac_area,a.acsub_type
      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 ='$getins_invoice' LIMIT 1") or die(mysqli_error($conn));
    $pin_row= mysqli_fetch_assoc($pinvoice_query);

    $userid         = $pin_row['userid'];
    $em_id=mysqli_fetch_assoc(mysqli_query($conn,"SELECT us_em_id FROM user WHERE   us_brid ='$brid' AND uid='$userid' LIMIT 1 "))['us_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'];
    $ac_name        = $pin_row['ac_name'];
    $ac_number      = $pin_row['ac_number'];
    $ac_address     = $pin_row['ac_details'];
    $ac_email       = $pin_row['ac_email'];
    $ac_alise       = $pin_row['ac_alise'];
    $outlet_id      = $pin_row['outlet_id'];
    $ac_area        = $pin_row['ac_area'];
    $acsub_type     = $pin_row['acsub_type'];
    $co_logo        = $pin_row['co_logo'];
  
    $route_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT route_name FROM route_area WHERE  roid='$ac_area' LIMIT 1 "))['route_name'];

?>
<section class="content">
  <div class="row">

<!--     <div class="col-md-1"> </div>
 -->    <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"><?php echo language(49); ?></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>
          <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;">
        <div class="well col-xs-12 col-sm-12 col-md-12 col-xs-offset-1 col-sm-offset-1 col-md-offset-3">
            <div class="row">
                <div class="col-xs-4 col-sm-4 col-md-4">
                    <address>
                        <strong style="font-size:<?php echo $rowps['com_font_size'];?>px;color:<?php echo $rowps['com_color'];?>;"> <?php echo $rowps['company_name'];?></strong>
                        <br>
                        Dhaka bangladdesh is best
                        <br>
                       <?php echo $rowps['company_email'];?> #<?php echo $rowps['company_phone'];?>
                        <br>
                        <abbr title="Phone"></abbr> <?php echo $rowps['company_address'];?><br>

                        
                    </address>
                </div>

                <div class="col-xs-4 col-sm-4 col-md-4 text-center">
                    <address>
                          <div class="tm_logo mb-2">
                        <?php if($co_logo != ""): ?>
                            <img class="profile-user-img img-fluid img-circle  mt-3" src="data:image/gif;base64,<?php echo $co_logo;?>" style="width:80px;height:75px;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">
                        <?php else : ?>
                           <img class="profile-user-img img-fluid img-circle  mt-3" src="../../dist/img/a.png" style="width:80px;height:75px;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>"></div>
                        <?php endif; ?>
                        <br>

                        <?php if ($rowps['invoice_tin']=="On") {  ?>
                         <sss>Tin Number : <?php echo $rowps['invoice_tin'];?></sss>
                        <?php } ?>
                    </address>
                </div>

                <div class="col-xs-4 col-sm-4 col-md-4 text-left">
                    <br>
                    <div style="float:right;">
                     <?php if ($rowps['print_time']=="On") {  ?>
                    Print time &nbsp;&nbsp;  : <sss class="invoice_address"><?php echo date('d/m/y h:i:s a', strtotime($datetime));?></sss><br>
                    <?php } ?>

    


                    create Date  : <sss class="invoice_address"><?php echo date('d/m/y h:i:s a', strtotime($date));?></sss><br>
                    Create By  &nbsp;&nbsp; : <sss class="invoice_address"><?php echo $em_name;?></sss><br>
                    Invoice No.&nbsp;  : <sss class="invoice_address" style="font-size:10px;"><?php echo  $getins_invoice; ?></sss>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-4 col-sm-4 col-md-4">
                    <address>
                     Name  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:   <?php echo $ac_name;?> <br>
                     address &nbsp;:        <?php echo $ac_address;?> <br>
                     phone &nbsp;&nbsp;&nbsp;&nbsp;:        <?php echo $ac_number;?> <br>
                     Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:      <?php echo $ac_email;?>
                    </address>
                </div>

                <div class="col-xs-4 col-sm-4 col-md-4 text-center">
                    <address style="<?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>;border: 1px solid white;padding: 5px;font-size: 16px;margin-top: 32px;color: #fff;border-radius: 10px">
                        <strong > Opening Installment </strong>
                    </address>
                </div>
               <div class="col-xs-4 col-sm-4 col-md-4 text-left" >

                  <div style="float:right;">
                    Alias &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  : <sss class="invoice_address"><?php echo $ac_alise ;?></sss><br>
                    Outlet id &nbsp;&nbsp;&nbsp;&nbsp; : <sss class="invoice_address"><?php echo $outlet_id;?></sss><br>
                    catagory   &nbsp;&nbsp;&nbsp;&nbsp; : <sss class="invoice_address">
                    <?php 
                      if ($acsub_type=='retail') {
                            echo "Retail";
                        }else if($acsub_type=='ws'){
                            echo "WholeSaler";
                        }else if($acsub_type=='distri'){
                            echo "Dristributor";
                        }else if($acsub_type=='emi'){
                            echo "EMI (installment)";
                        }else if($acsub_type=='B2B'){
                            echo "B2B (sales)";
                        }
                    ?>
                  </sss><br>
                    Area Name &nbsp;: <sss class="invoice_address"><?php echo  $route_name; ?></sss>
                </div>
                </div>
            </div>
            <div class="row">
                <table class="table table-sm table-hover">
                    <thead>
                        <tr>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Sl</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Service</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Vouchar</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Per Ins</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Payment</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Due Ins</th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Ins Type </th>
                            <th class="text-center text-white" style=" <?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>;<?php if ($rowps['bg_color'] !== "" ) { echo 'border-color:'.$rowps['bg_color'];} ?>">Ins Date </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $accat= mysqli_query($conn,"SELECT *,accounts.ac_name,accounts.acid,accounts.ac_alise,accounts.ac_number,accounts.balance FROM (intallment
                            INNER JOIN accounts ON intallment.acid = accounts.acid)
                            WHERE intallment.coid='$coid' AND intallment.bid='$bid' AND intallment.brid='$brid' AND intallment.invoice='$getins_invoice'  ORDER BY intallment.ins_date  ") or die(mysqli_error($conn));
                            $i=0;
                            $Total_due=0;
                            while($orw= mysqli_fetch_assoc($accat)){
                            $Total_due +=$orw['due_ins'];
                        ?>
                        <tr>
                           <td class="text-center"><?php echo ++$i;?></td>
                           <td class="text-center"> <?php echo $orw['service_name']; ?> </td>
                           <td class="text-center"> <?php if ($orw['in_type']=='OPINS') { echo 'opening Installment';}else{echo 'Sales Installment';} ?> </td>
                           <td class="text-center"> <dt> <?php echo $orw['due_ins'];?> </dt></td>
                           <td class="text-center"> <dt> <?php echo $orw['payment'];?> </dt> </td>
                           <td class="text-center"> <dt> <?php echo $orw['due_ins'];?> </dt></td>
                           <td class="text-center"> <dt> <?php echo $orw['ins_type'];?> </dt></td>
                           <td class="text-center"> <?php echo $orw["ins_date"]; ?>  </td>
                        </tr>

                        <?php } ?>

                        <tr>
                            <th colspan="7" class="text-right"><strong>Total ins :</strong>  </th>
                            <th class="text-center">
                               <?php 
                                     $installment_query=mysqli_query($conn, " SELECT  insid   FROM  intallment WHERE  invoice='$getins_invoice' " );
                                  echo $count  = mysqli_num_rows($installment_query);
                                ?>  
                            </th>
                        </tr>

                        <tr>
                            <th colspan="7" class="text-right"><strong>Subtotal :</strong>  </th>
                            <th class="text-center"><?php echo $Total_due; ?></th>
                        </tr>

                        <tr>
                           <th style="padding: 5px 15px;" colspan="8">
                           <span style="font-weight: 600;">In Word :</span> <?php  inword($Total_due);?></th>
                        </tr>
           
                        <tr>
                            <th colspan="8" style="border:none;">&nbsp;</th>
                        </tr>

                        <tr>
                          <th colspan="8" style="border:none;">&nbsp;</th>
                        </tr>

                        <tr>
                           <td colspan="2"  style="font-size: 12px; text-align:center;font-weight:700 ; border-top:1px dotted; "> DSR</td>
                           <td colspan="2"  style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted "> Receiver </td>
                           <td colspan="2"  style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted "> RSO</td>
                           <td colspan="2"  style="font-size: 12px; text-align: center;font-weight:700 ;border-top:1px dotted ">Authorized</td>
                        </tr>
   
                        <tr>
                           <th class="powerby text-center text-white" colspan="8" style="font-size:10px;<?php if ($rowps['bg_color'] !== "" ) { echo 'background:'.$rowps['bg_color'];} ?>"> Powered by : www.welldiv.com</th>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
       </div>
<!--            <div class="col-md-1"> </div>
 -->
   </div>
</section>