<?php
ob_start();
session_name('CAKEPHP'); session_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
include('cocoon_config.php');
// $s=new db_query();
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  <title>GST-Output Register Report</title>
</head>

<body>


  <div class="wrapper">
    <div class="div_first">

    </div>

    <div style="width:100%; height:360px; float:left; overflow:auto;">
      <table width="180%" border="1" cellspacing="0" cellpadding="3" style="border: solid 1px #808080; border-collapse:collapse;">
        <thead>
          <tr>
            <th width="2%">S. No.</th>
            <!-- <th width="4%">CIN No.</th>
    <th width="4%">GSTIN</th> -->
            <th width="4%">Bill No.</th>
            <!--<th width="4%">Line Item</th> -->
            <th width="4%">Date</th>
            <th width="4%">Patient Code</th>
            <th>Patient Name</th>
            <th width="4%">State Code</th>
            <th width="4%">State</th>
            <th width="4%">Product Code</th>
            <th width="4%">Part Name</th>
            <th width="4%">HSN Code</th>
            <th width="4%" style="text-align:center;">Qty.</th>
            <th width="4%" style="text-align:center;">Rate</th>
            <th width="4%" style="text-align:center;">Product Value</th>
            <th width="4%" style="text-align:center;">Discount(Amt.)</th>
            <th width="4%" style="text-align:center;">Product Value after Discount</th>
            <th width="4%" style="text-align:center;">Tax %</th>
            <th width="4%" style="text-align:center;">Tax Amt.</th>
            <th width="4%" style="text-align:center;">SGST</th>
            <th width="4%" style="text-align:center;">CGST</th>
            <th width="4%" style="text-align:center;">IGST</th>
            <th width="4%" style="text-align:center;">UGST</th>
            <th width="4%" style="text-align:center;">Total Value on Invoice</th>
          </tr>
        </thead>
        <tbody bgcolor="#ffffff"><?php

                                  $GetRecord = $_SESSION['gst_output_register'];



                                  $result = mysqli_query($link1, $GetRecord);
                                  $i = 1;
                                  while ($in_fetch = mysqli_fetch_array($result)) {
                                    $sql_outpatients = mysqli_fetch_array(mysqli_query($link1, "select mrno,state,state_code from outpatients inner join addresses on outpatients.registration_id=addresses.registration_id  where op_number='" . $in_fetch['patient_no'] . "'"));

                                    $hsn = DB_fetch_array(DB_query("select hsn_code from item_master where item_name='" . $in_fetch['item_name'] . "'", $db));

                                  ?>
            <tr>
              <td><?php echo $i; ?></td>
              <!-- <td><?php //echo $sql_outpatients[0]; 
                        ?></td>
      <td><?php //echo $s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'gst_no'); 
          ?></td> -->
              <td><?php echo $in_fetch['voucher_no']; ?></td>
              <!--<td></td> -->
              <td><?php
                                    $date = strtotime($in_fetch['issue_date']);
                                    echo date('Y-m-d', $date);
                  ?></td>
              <td><?php echo $in_fetch['patient_no']; ?></td>
              <td><?php echo $in_fetch['patient_name']; ?></td>
              <td style="text-align:center;"><?php
                                              $stateCode = DB_fetch_array(DB_query("select * from state_master where state='" . $sql_outpatients[1] . "'", $db));
                                              echo $stateCode['state_code'];
                                              ?>
              </td>
              <td><?php echo $sql_outpatients[1]; ?></td>
              <td><?php echo $in_fetch['item_code']; ?></td>
              <td><?php echo $in_fetch['item_name']; ?></td>
              <td><?php echo $hsn['hsn_code']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['issue_qty']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['rate']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['pre_discount_amount'];
                                            $basic[] = $in_fetch['pre_discount_amount']; ?></td>
              <td style="text-align:right;"><?php

                                            /*  $discount_amount=$in_fetch['discount_actual_amount'];
                             if($in_fetch['pre_discount_amount']!='0' && $in_fetch['pre_discount_amount']!='')
                             {
                                $dis_perc1=$discount_amount*100;
                                echo $dis_per=number_format((float)$dis_perc1/$in_fetch['pre_discount_amount'],2);
                                //echo $dis_perc1."#####".$in_fetch['pre_discount_amount'];

                             }   
                             else
                             {
                               $dis_per='0.00';
                             } 
                              */
                                            echo $in_fetch['discount_amount_tot'];


                                            ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['pre_discount_amount'] - $in_fetch['discount_amount_tot'];
                                            $val_after_disc = $in_fetch['pre_discount_amount'] - $in_fetch['discount_amount_tot'];
                                            $disc_arr[] = $in_fetch['pre_discount_amount'] - $in_fetch['discount_amount_tot']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['tax_rate']; ?>
              </td>
              <td style="text-align:right;"><?php echo $in_fetch['tax_rate_amount'];
                                            $vat_arr[] = $in_fetch['vat_amt']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['sgst'];
                                            $sgst_arr[] = $in_fetch['sgst']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['cgst'];
                                            $cgst_arr[] = $in_fetch['cgst']; ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['igst'];
                                            $igst_arr[] = $in_fetch['igst'];  ?></td>
              <td style="text-align:right;"><?php echo $in_fetch['utgst'];
                                            $ugst_arr[] = $in_fetch['ugst']; ?></td>
              <td style="text-align:right;">
                <?php
                                    /*   if($in_fetch['vat_amt']!='0.00')
           {
              $total_invoice=$val_after_disc+$in_fetch['vat_amt'];
              echo number_format((float)$total_invoice,2,'.',',');
           }
           else
           { */
                                    $total_invoice = $val_after_disc + $in_fetch['sgst'] + $in_fetch['cgst'] + $in_fetch['igst'] + $in_fetch['utgst'];
                                    echo number_format((float)$total_invoice, 2, '.', ',');
                                    // }
                                    $tot_inv[] = $total_invoice;

                ?></td>
            </tr><?php $i++;
                                  } ?>

          <tr>
            <td colspan="12" style="text-align:right;"><strong>Total</strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($basic)) {
                                                    echo number_format((float)array_sum($basic), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"></td>
            <td style="text-align:right;"><strong><?php if (!empty($disc_arr)) {
                                                    echo number_format((float)array_sum($disc_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"></td>
            <td style="text-align:right;"><strong><?php if (!empty($vat_arr)) {
                                                    echo number_format((float)array_sum($vat_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($sgst_arr)) {
                                                    echo number_format((float)array_sum($sgst_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($cgst_arr)) {
                                                    echo number_format((float)array_sum($cgst_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($igst_arr)) {
                                                    echo number_format((float)array_sum($igst_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($ugst_arr)) {
                                                    echo number_format((float)array_sum($ugst_arr), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
            <td style="text-align:right;"><strong><?php if (!empty($tot_inv)) {
                                                    echo number_format((float)array_sum($tot_inv), 2, '.', ',');
                                                  } else {
                                                    echo "0.00";
                                                  } ?></strong></td>
          </tr>




        </tbody>
      </table>

    </div>
    <script type="text/javascript" language="javascript">
      function toggle_visibility(id) {
        var e = document.getElementById(id);
        e.style.display = 'none';
        //alert(e.style.display);
        window.print();
      }
    </script>

  </div>
</body>

</html>
<?php
$header = '';
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Export_gst_output_register_report.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>