<?php
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
include('cocoon_config.php');

?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
  <div class="tab_div">
    <h1>B2CS</h1>
    <form action="" name="main_account" method="post" onsubmit="return validate()">
      <table width="100%" border="0" cellpadding="3" cellspacing="0">
        <tr class="row1">
          <td width="13%" align="right">Store</td>
          <td><select name="store" id="store">
              <option value="0">All</option>
              <?php
              $sql_locations = mysqli_query($link1, "select store_code,store_desc from store_masters order by store_code");
              while ($result_locations = mysqli_fetch_array($sql_locations)) {
              ?>
                <option value="<?php echo $result_locations['store_code']; ?>" <?php if ($_REQUEST['store'] == $result_locations['store_code']) { ?> selected="selected" <?php } ?>><?php echo $result_locations['store_desc']; ?></option>
              <?php } ?>
            </select>
          </td>
          <td width="13%" align="right">From Date</td>
          <td width="20%"><input name="fromdate" id="fromdate" type="text" readonly class="datepicker" value="<?php if ($_REQUEST['fromdate'] != '') {
                                                                                                                echo $_REQUEST['fromdate'];
                                                                                                              } else {
                                                                                                                echo ConvertSQLDate($_SESSION['financialYrStart']);
                                                                                                              } ?>" /></td>
          <td width="13%" align="right">To Date</td>
          <td width="20%"><input name="todate" id="todate" type="text" class="datepicker" readonly value="<?php if ($_REQUEST['todate'] != '') {
                                                                                                            echo $_REQUEST['todate'];
                                                                                                          } else {
                                                                                                            echo ConvertSQLDate($_SESSION['financialYrEnd']);
                                                                                                          } ?>" onchange="return date_check();" /></td>
        </tr>
        <tr class="row1">
          <td colspan="6" style="text-align:center;"><input name="Search" type="submit" class="search" value="Search" /></td>
        </tr>
      </table>
    </form>
    <br>
    <!-- <div>
  <p align="right"><a href="gst_sales_summary_print.php" class="search" target="_blank">Print</a>
  <a href="gst_sales_summary_export.php" class="search" target="_blank">Export</a></p> </div> -->
    <div style="width:100%; height:320px; float:left; overflow:auto;">
      <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
        <thead>
          <tr>
            <th>Invoice Number</th>
            <th width="8%" style="text-align:center;">Invoice Date</th>
            <th width="10%" style="text-align:center;">Invoice Value</th>
            <th width="18%" style="text-align:center;">Place of Supply</th>
            <th width="10%" style="text-align:center;">Rate</th>
            <th width="10%" style="text-align:center;">Taxable Value</th>
            <th width="10%" style="text-align:center;">Cess Amount</th>
            <th width="10%" style="text-align:center;">E-Commerce GSTIN</th>
          </tr>

        </thead>
        <tbody><?php
                if ($_REQUEST['Search']) {
                  unset($_SESSION['gst_hsn_!2']);
                  $filter = array();

                  $fromdate1 = explode("/", $_REQUEST['fromdate']);
                  $fromdate = $fromdate1[2] . "-" . $fromdate1[1] . "-" . $fromdate1[0];

                  $todate1 = explode("/", $_REQUEST['todate']);
                  $todate = $todate1[2] . "-" . $todate1[1] . "-" . $todate1[0];



                  //$fromdate=ConvertSQLDate($_REQUEST['fromdate']);
                  //$todate=ConvertSQLDate($_REQUEST['todate']);
                  //$fromdate1=explode("/")
                  if ($_REQUEST['store'] != '0') {
                    $filter2[] = "di.store_code='" . $_REQUEST['store'] . "'";
                  }

                  if ($_REQUEST['fromdate'] != '' && $_REQUEST['todate'] != '') {
                    $filter2[] = "date_format(di.issue_date,'%Y-%m-%d') between '" . $fromdate . "' and '" . $todate . "'";
                  }


                  if (!empty($filter2)) {
                    $filter3 = implode(" and ", $filter2);
                  } else {
                    $filter3 = "1=1";
                  }
                  $_SESSION['filter3'] = $filter3;

                  $GetRecord = "SELECT di.issue_no  as issue_no,di.issue_date as issue_date,sum(dd.aft_discount_amount_tot) as invoice_val,`dd`.`tax_rate` as rate,aft_discount_amount_tot as taxable_val,sum(dd.tax_rate_amount) as tot_tax_amt from drugissue_details dd
               JOIN `drugissues` `di` ON `di`.`id` = `dd`.`drugissue_id`
                left join drugissue_amounts da on di.id=da.drugissue_id 
                where $filter3 and da.net_amount>250000 group by dd.drugissue_id,dd.tax_rate";
                  $result = mysqli_query($link1, $GetRecord);
                  $i = 1;
                  if (mysqli_num_rows($result) > 0) {
                    while ($rs = mysqli_fetch_array($result)) {

                ?>
                <tr>
                  <td><?php echo $rs['issue_no']; ?></td>
                  <td><?php echo date('d/m/Y', strtotime($rs['issue_date'])); ?></td>
                  <td style="text-align:right;"><?php echo $rs['invoice_val'] + $rs['tot_tax_amt'];
                                                $grand_invoice[] = $rs['invoice_val'] + $rs['tot_tax_amt']; ?></td>
                  <td style="text-align:right;">Uttar Pradesh</td>
                  <td style="text-align:right;"><?php echo $rs['rate']; ?></td>
                  <td style="text-align:right;"><?php echo $rs['taxable_val'];
                                                $grand_taxable[] = $rs['taxable_val'];  ?></td>
                  <td style="text-align:right;">0.00</td>
                  <td style="text-align:right;"></td>
                </tr><?php $i++;
                    } ?>
              <tr>
                <td colspan="2" style="text-align:right;"><strong>Total</strong></td>
                <td style="text-align:right;"><strong><strong><?php if (!empty($grand_invoice)) {
                                                                echo array_sum($grand_invoice);
                                                              } else {
                                                                echo "0.00";
                                                              } ?></strong></td>
                <td colspan="2"></td>
                <td style="text-align:right;"><strong><strong><?php if (!empty($grand_taxable)) {
                                                                echo array_sum($grand_taxable);
                                                              } else {
                                                                echo "0.00";
                                                              } ?></strong></td>
                <td colspan="2"></td>
              </tr>
            <?php } else { ?>
              <tr>
                <td colspan="6"><strong>No Records Found</strong></td>
              </tr>

          <?php }
                }  ?>


        </tbody>
      </table>

    </div>
  </div>
  <script>
    function validate() {

      if (document.getElementById('todate').value == '') {
        alert("please Enter From Date");
        document.getElementById('todate').focus();
        return false;
      }

      if (document.getElementById('fromdate').value == '') {
        alert("please Enter To Date");
        document.getElementById('fromdate').focus();
        return false;
      }
    }
  </script>