<?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>HSN(12)</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 width="10%">HSN Code</th>
            <th style="text-align:center;">Description</th>
            <th width="10%" style="text-align:center;">Sub Group</th>
            <th width="6%" style="text-align:center;">UOM</th>
            <th width="9%" style="text-align:center;">Total Quantity</th>
            <th width="9%" style="text-align:center;">Total Value</th>
            <th width="9%" style="text-align:center;">Taxable Value</th>
            <th width="9%" style="text-align:center;">Integrated Tax Amount</th>
            <th width="9%" style="text-align:center;">Central Tax Amount</th>
            <th width="9%" style="text-align:center;">State/UT Tax Amount</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 `im`.`hsn_code` , sum(`dd`.`issue_qty`) as issue_qty  , sum(`dd`.`amount`) as tot_val , sum(`dd`.`cgst`) as tot_cgst , sum(`dd`.`igst`) as tot_igst , sum(`dd`.`utgst`) as tot_utgst ,sum(`dd`.`sgst`) as tot_sgst,sum(`dd`.`aft_discount_amount_tot`) as tot_tax,im.SubCategoryID as subcategory,im.mst_unitid,im.conv_unit
FROM `aeh_erp_live`.`item_master` `im`
JOIN `aeh_hmis_live`.`drugissue_details` `dd` ON `im`.`StockID` = `dd`.`item_code`
JOIN `aeh_hmis_live`.`drugissues` `di` ON `di`.`id` = `dd`.`drugissue_id` where $filter3
 group by im.hsn_code";

                  // $GetRecord="SELECT GROUP_CONCAT(StockID) as abc, hsn_code,mst_unitid,conv_unit,SubCategoryID FROM item_master GROUP BY hsn_code ORDER BY hsn_code";
                  $_SESSION['gst_sales_summary'] = $GetRecord;
                  $result = DB_query($GetRecord, $db);
                  $i = 1;
                  while ($rs = DB_fetch_array($result)) {
                    $pri_unit = $s->GetFieldValue('unitsofmeasure', "unitid='" . $rs['mst_unitid'] . "'", 'unitname');
                    $sec_unit = $s->GetFieldValue('unitsofmeasure', "unitid='" . $rs['conv_unit'] . "'", 'unitname');
                    $sub_category = $s->GetFieldValue('stockcategory', "id='" . $rs['subcategory'] . "'", 'categorydescription');
                    /* echo "SELECT sum(issue_qty) as issue_qty,sum(di.pre_discount_amount) as tot_val,sum(sgst) as tot_sgst,sum(cgst) as tot_cgst,sum(igst) as tot_igst,sum(utgst) as tot_utgst,sum(tax_rate_amount) as tot_tax FROM drugissues d INNER JOIN drugissue_details di ON d.id=di.drugissue_id where $filter3 and item_code in('".$items."') ";*/

                    if ($rs['issue_qty'] != '') {
                ?>
                <tr>
                  <td><?php echo $rs['hsn_code']; ?></td>
                  <td></td>
                  <td><?php echo $sub_category;  ?></td>
                  <td><?php echo $pri_unit . "-" . $sec_unit;  ?></td>
                  <td style="text-align:right;"><?php echo $rs['issue_qty'];  ?></td>
                  <td style="text-align:right;"><?php echo $rs['tot_val'];
                                                $grand_tot[] = $rs['tot_val']; ?></td>
                  <td style="text-align:right;"><?php echo $rs['tot_tax'];
                                                $grand_tot_tax[] = $rs['tot_tax'];    ?></td>
                  <td style="text-align:right;"><?php echo $rs['tot_igst'];
                                                $grand_tot_igst[] = $rs['tot_igst'];  ?></td>
                  <td style="text-align:right;"><?php echo $rs['tot_cgst'];
                                                $grand_tot_cgst[] = $rs['tot_cgst'];  ?></td>
                  <td style="text-align:right;"><?php echo $rs['tot_sgst'] + $rs['tot_utgst'];
                                                $grand_tot_sgst[] = $rs['tot_sgst'] + $GetRecord_item['tot_utgst'];  ?></td>
                </tr><?php $i++;
                    }
                  } ?>
            <tr>
              <td colspan="4"><strong>No. of HSN: <?php echo $i - 1; ?></strong></td>
              <td style="text-align:right;"><strong>Grand Total</strong></td>
              <td style="text-align:right;"><strong><?php if (!empty($grand_tot)) {
                                                      echo array_sum($grand_tot);
                                                    } else {
                                                      echo "0.00";
                                                    } ?></strong></td>
              <td style="text-align:right;"><strong><?php if (!empty($grand_tot_tax)) {
                                                      echo array_sum($grand_tot_tax);
                                                    } else {
                                                      echo "0.00";
                                                    }  ?></strong></td>
              <td style="text-align:right;"><strong><?php if (!empty($grand_tot_igst)) {
                                                      echo array_sum($grand_tot_igst);
                                                    } else {
                                                      echo "0.00";
                                                    }  ?></strong></td>
              <td style="text-align:right;"><strong><?php if (!empty($grand_tot_cgst)) {
                                                      echo array_sum($grand_tot_cgst);
                                                    } else {
                                                      echo "0.00";
                                                    } ?></strong></td>
              <td style="text-align:right;"><strong><?php if (!empty($grand_tot_sgst)) {
                                                      echo array_sum($grand_tot_sgst);
                                                    } else {
                                                      echo "0.00";
                                                    }  ?></strong></td>
            </tr>
          <?php } else { ?>
            <tr>
              <td colspan="10"><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>