<?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>State Wise GST(MIS)</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%"><strong>From Date</strong></td>
          <td width="35%"><input name="fromdate" id="fromdate" type="text" class="datepicker" value="<?php if ($_REQUEST['fromdate'] != '') {
                                                                                                        echo $_REQUEST['fromdate'];
                                                                                                      } else {
                                                                                                        echo ConvertSQLDate($_SESSION['financialYrStart']);
                                                                                                      } ?>" /></td>
          <td width="13%"><strong>To Date</strong></td>
          <td><input name="todate" id="todate" type="text" class="datepicker" value="<?php if ($_REQUEST['todate'] != '') {
                                                                                        echo $_REQUEST['todate'];
                                                                                      } else {
                                                                                        echo ConvertSQLDate($_SESSION['financialYrEnd']);
                                                                                      } ?>" onchange="return date_check();" /></td>
        </tr>
        <tr class="row1">
          <td colspan="4" style="text-align:center;"><input name="Search" type="submit" class="search" value="Search" /></td>
        </tr>
      </table>
    </form>
    <!-- <p align="right"><a href="gst_input_register_print.php" class="search" target="_blank">Print</a></p> -->
    <div style="width:100%; height:360px; float:left; overflow:auto;">
      <table width="100%" border="0" cellspacing="0" cellpadding="3">
        <tr>
          <td width="50%" valign="top">
            <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border: solid 1px #808080; border-collapse:collapse;">
              <thead>
                <tr>
                  <th width="2%" rowspan="2">S. No.</th>
                  <th width="5%" rowspan="2">State Name</th>
                  <th width="5%" rowspan="2">State Code</th>
                  <th width="40%" colspan="4" style="text-align:center;">Purchase</th>
                </tr>
                <tr>
                  <th width="8%">SGST</th>
                  <th width="8%">CGST</th>
                  <th width="8%">IGST</th>
                  <th width="8%">UGST</th>
                </tr>
              </thead>
              <tbody bgcolor="#ffffff">
                <?php

                if ($_REQUEST['Search']) {
                  $filter = array();

                  $fromdate = ConvertSQLDate($_REQUEST['fromdate']);
                  $todate = ConvertSQLDate($_REQUEST['todate']);

                  if ($_REQUEST['fromdate'] != '' && $_REQUEST['todate'] != '') {
                    $filter[] = "bill_book_date between '" . $fromdate . "' and '" . $todate . "'";
                  }
                  if (!empty($filter)) {
                    $filter1 = implode(" and ", $filter);
                  } else {
                    $filter1 = "1=1";
                  }


                  $GetRecord = "SELECT sum(sgst_amt) as sgst_amt,sum(cgst_amt) as cgst_amt,sum(igst_amt) as igst_amt,sum(ugst_amt) as ugst_amt,state_master.state,state_master.state_code,place_supply from purchase_billbook inner join state_master on state_master.id=purchase_billbook.place_supply where $filter1 group by place_supply ";



                  $result = DB_query($GetRecord, $db);
                  $i = 1;
                  while ($in_fetch = DB_fetch_array($result)) {

                ?>
                    <tr>
                      <td><?php echo $i; ?></td>
                      <td><?php echo $in_fetch['state']; ?></td>
                      <td><?php echo $in_fetch['state_code']; ?></td>
                      <td><?php if ($in_fetch['sgst_amt'] != '0') {
                            echo $in_fetch['sgst_amt'];
                          } else {
                            echo "0.00";
                          } ?></td>
                      <td><?php echo $in_fetch['cgst_amt']; ?></td>
                      <td><?php echo $in_fetch['igst_amt']; ?></td>
                      <td><?php echo $in_fetch['ugst_amt']; ?></td>
                      <!--<td></td> -->
                    </tr><?php $i++;
                        } ?>

                <?php } else { ?>
                  <tr>
                    <td colspan="7"><strong>No Records Found</strong></td>
                  </tr>
                <?php } ?>
              </tbody>
            </table>
          </td>
          <td valign="top">
            <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border: solid 1px #808080; border-collapse:collapse;">
              <thead>
                <tr>
                  <th width="2%" rowspan="2">S. No.</th>
                  <th width="5%" rowspan="2">State Name</th>
                  <th width="5%" rowspan="2">State Code</th>
                  <th width="40%" colspan="4" style="text-align:center;">Sales</th>
                </tr>
                <tr>
                  <th width="8%">SGST</th>
                  <th width="8%">CGST</th>
                  <th width="8%">IGST</th>
                  <th width="8%">UGST</th>
                </tr>
              </thead>
              <tbody bgcolor="#ffffff">
                <?php

                if ($_REQUEST['Search']) {
                  $filter2 = array();

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

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

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


                  $GetRecord_sales = "SELECT sum(sgst) as sgst,sum(cgst) as cgst,sum(igst) as igst,sum(utgst),states.description,states.code from drugissues inner join drugissue_details on drugissues.id=drugissue_details.drugissue_id inner join locations on locations.loc_code=drugissue_details.locationname inner join states on states.id=locations.state_id where $filter3 group by state_id ";
                  $result_sales = mysqli_query($link1, $GetRecord_sales);
                  // echo mysqli_num_rows($result_sales);
                  $i = 1;
                  while ($in_fetch_sales = mysqli_fetch_array($result_sales)) {

                ?>
                    <tr>
                      <td><?php echo $i; ?></td>
                      <td><?php echo $in_fetch_sales['description']; ?></td>
                      <td><?php echo $in_fetch_sales['code']; ?></td>
                      <td><?php if ($in_fetch_sales['sgst'] != '') {
                            echo $in_fetch_sales['sgst'];
                          } else {
                            echo "0.00";
                          } ?></td>
                      <td><?php if ($in_fetch_sales['cgst'] != '') {
                            echo $in_fetch_sales['cgst'];
                          } else {
                            echo "0.00";
                          } ?></td>
                      <td><?php if ($in_fetch_sales['igst'] != '') {
                            echo $in_fetch_sales['igst'];
                          } else {
                            echo "0.00";
                          } ?></td>
                      <td><?php if ($in_fetch_sales['ugst'] != '') {
                            echo $in_fetch_sales['ugst'];
                          } else {
                            echo "0.00";
                          } ?></td>
                      <!--<td></td> -->
                    </tr><?php $i++;
                        } ?>

                <?php } else { ?>
                  <tr>
                    <td colspan="7"><strong>No Records Found</strong></td>
                  </tr>
                <?php } ?>
              </tbody>
            </table>
          </td>
        </tr>
      </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>