<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
  <div class="tab_div">
    <style type="text/css">
      .body_lightbox {
        width: 100%;
        background-color: #e7e7e7;
        position: absolute;
        left: 280px;
        top: 95px;
        padding-left: 5px;
        padding-right: 5px;
        border: #666666 solid 3px;
      }
    </style>
    <?php
    $PageSecurity = 2;
    $title = _('Material return to vendor');
    include('cocoon_config.php');
    function getFieldvalue_cake($table, $condition, $val, $field)
    {
      global $db;
      //echo "select ".$field." from ".$table." where ".$condition." ='".$val."'";
      $sql = mysqli_query($db, "select " . $field . " from " . $table . " where " . $condition . " ='" . $val . "'");
      $r = mysqli_fetch_array($sql);
      //echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
      return ($r[$field]);
    }

    if ($_REQUEST['action'] == 'Delete' && $_REQUEST['id'] != '') {
      $query_hmis_batch = DB_query("select * from inv_material_return_details where parentId='" . $_REQUEST['id'] . "'", $db);
      while ($result_hmis_batch = DB_fetch_array($query_hmis_batch)) {
        $storeid = $s->getField('inv_material_return', 'id', $_REQUEST['id'], 'store');
        $storecode = $s->getField('storetype', 'id', $storeid, 'StoreCode');
        $itemcode = $s->getField('item_master', 'id', $result_hmis_batch['itemid'], 'StockID');
        $item_name = $s->getField('item_master', 'id', $result_hmis_batch['itemid'], 'item_name');
        $majorid = $s->getField('item_master', 'id', $result_hmis_batch['itemid'], 'CategoryID');
        $majormainid = $s->getField('stockmaincategory', 'id', $majorid, 'ref_major_id');
        $majorcode = getFieldvalue_cake('majors', 'id', $majormainid, 'major_code');

        $update_batch = mysqli_query($link1, "update batchdetails set qty =qty+" . $result_hmis_batch['ReturnQty'] . " where item_code='" . $itemcode . "' and store_code='" . $storecode . "' and batch_no='" . $result_hmis_batch['batch_no'] . "'");

        $update = mysqli_query($link1, "update storeitems set stock_level =stock_level+" . $result_hmis_batch['ReturnQty'] . " where item='" . $itemcode . "' and store_code='" . $storecode . "' and major_code='" . $majorcode . "'");
      }


      DB_query("delete from inv_material_return where id='" . $_REQUEST['id'] . "' ", $db);
      DB_query("delete from inv_material_return_details where parentId='" . $_REQUEST['id'] . "' ", $db);

      DB_query("delete from stock_valuation_tbl where type='MRV' and against_voucher_main_id='" . $_REQUEST['id'] . "'", $db);

      echo "<script>location.href='index.php?route=Inventory/transation/Material_Return_To_Vendor_index&modid=" . $_REQUEST['modid'] . "&frmid=" . $_REQUEST['frmid'] . "'</script>";
    }
    ?>
    <?php
    if ($_REQUEST['action'] == '') {
    ?>
      <h1>Material Return To Vendor</h1>
      <?php //if($_SESSION['frmAdd']!='1') {
      ?>
      <p align="left"><a href="index.php?route=Inventory/transation/material_return_add&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&NewIssue=yes"><input type="button" class="search" value="Add New" /></a></p>
      <?php //} 
      ?>
      <form action="" method="post">
        <table width="100%" border="0" cellpadding="3" cellspacing="0" align="center">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="35%"><select name="branch" id="branch">
                <option value="0">---Select---</option>
                <?php
                $query1 = DB_query("select * from locations where CompanyId='" . $_SESSION['ACGCompanyId'] . "'", $db);
                while ($result1 = DB_fetch_array($query1)) {
                ?>
                  <option value="<?php echo $result1['loccode']; ?>" <?php echo ($_REQUEST['branch'] == $result1['loccode'] ? 'selected' : '') ?>><?php echo $result1['locationname']; ?></option>
                <?php } ?>
              </select></td>
            <td align="right">Vendor</td>
            <td><select name="vendor" id="vendor">
                <option value="0">---Select---</option>
                <?php
                $query2 = DB_query("select * from suppliers where CompanyId='" . $_SESSION['ACGCompanyId'] . "'", $db);
                while ($result2 = DB_fetch_array($query2)) {
                ?>
                  <option value="<?php echo $result2['id']; ?>" <?php echo ($_REQUEST['vendor'] == $result2['id'] ? 'selected' : '') ?>><?php echo $result2['vendor_name']; ?></option>
                <?php } ?>
              </select>
            </td>
          </tr>
          <tr class="row1">
            <td align="right">From Date</td>
            <td><input type="text" name="fromdate" id="fromdate" class="datepicker" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" onchange="javascript:checkdate(this.value)" /></td>
            <td align="right">To Date</td>
            <td><input type="text" name="todate" class="datepicker" size="12" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']); ?>" onchange="javascript:checkdate(this.value)">
              <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" id='from'>
              <input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']); ?>" id='to'>
            </td>
          </tr>
          <tr class="row1">
            <td colspan="4" align="center"><input type="submit" name="Search" class="search" value="Search" /></td>
          </tr>
        </table>
      </form>
      <br>
      <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
        <tr>
          <th width="12%">Return Slip No.</th>
          <th width="7%">Date</th>
          <th>Vendor</th>
          <th width="5%" style="text-align:center">View</th>
          <!-- <th width="5%" style="text-align:center">Edit</th> -->
          <!-- <th width="5%" style="text-align:center">Delete</th> -->
          <th width="5%" style="text-align:center">Print</th>
          <th width="5%" style="text-align:center">Debit Note</th>
        </tr>


        <?php
        if (isset($_POST['Search'])) {
          $branch = $_REQUEST['branch'];
          $vendor = $_REQUEST['vendor'];
          $fromdate1 = explode("/", $_POST['fromdate']);
          $fromdate = $fromdate1[2] . "-" . $fromdate1[1] . "-" . $fromdate1[0];
          $todate1 = explode("/", $_POST['todate']);
          $todate = $todate1[2] . "-" . $todate1[1] . "-" . $todate1[0];

          $filter = array();

          if ($fromdate != '//' || $todate != '//') {
            $filter[] = "return_date BETWEEN '" . $fromdate . "' AND '" . $todate . "'";
          }

          if ($branch != '0') {
            $filter[] = "sitecode='" . $branch . "'";
          }
          if ($vendor != '0') {
            $filter[] = "vendor_code ='" . $vendor . "'";
          }



          if (isset($filter)) {
            if (count($filter) > 0) {
              $filter1 = implode(" and ", $filter);
              $sql = DB_query("Select * from inv_material_return where  sitecode in('" . $_SESSION['AssignedBranch'] . "') and return_date between '" . $_SESSION['financialYrStart'] . "' and '" . $_SESSION['financialYrEnd'] . "' and $filter1 order by id desc", $db);
            }
          }
        } else {
          $sql = DB_query("Select * from inv_material_return where  sitecode in('" . $_SESSION['AssignedBranch'] . "') and return_date between '" . $_SESSION['financialYrStart'] . "' and '" . $_SESSION['financialYrEnd'] . "' order by id desc", $db);
        }
        // echo $sql;

        $i = 0;
        if (DB_num_rows($sql) > 0) {
          while ($myrow = DB_fetch_array($sql)) {

        ?>
            <tr>
              <td><?php echo $myrow['MRNno']; ?></td>
              <td><?php echo ConvertSQLDate($myrow['return_date']); ?></td>
              <td><?php $sitename = $s->getField('suppliers', 'id', $myrow['vendor_code'], 'vendor_name');
                  echo $sitename; ?></td>
              <td align="center"><?php if ($_SESSION['frmView'] == '1') { ?>
                  <a href="index.php?route=Inventory/transation/material_return_vendor_edit&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&action=view&id=<?php echo $myrow['id']; ?>"><img src='images/search.gif' height='15' width='15'></a><?php } else {
                                                                                                                                                                                                                                                                                echo "No Permission";
                                                                                                                                                                                                                                                                              } ?>
              </td>
              <!-- <td align="center">
	<?php if ($_SESSION['frmEdit'] == '1') { ?>
	<a href="index.php?route=Inventory/transation/material_return_vendor_edit&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&action=Edit&id=<?php echo $myrow['id']; ?>"><img src='images/icon-edit.png' height='15' width='15'></a><?php } else {
                                                                                                                                                                                                                                                                    echo "No Permission";
                                                                                                                                                                                                                                                                  } ?>
	</td> -->
              <!-- <td align="center">
	<?php //if($_SESSION['frmDelete']=='1'){
  ?>
	<a href="index.php?route=Inventory/transation/Material_Return_To_Vendor_index&modid=<?php //echo $_REQUEST['modid'];
                                                                                      ?>&frmid=<?php //echo $_REQUEST['frmid'];
                                                                                                ?>&action=Delete&id=<?php //echo $myrow['id']; 
                                                                                                                    ?>" onclick="return confirm('Are you sure you want to delete?')"><img src='images/Icon_Delete.png' height='15' width='15'></a><?php //}else{echo "No Permission";}
                                                                                                                                                                                                                                                  ?>
	</td> -->
              <td align="center"><a href="material_return_to_vendor_print.php?id=<?php echo $myrow['id'];  ?>" target="_blank"><img src="images/icon_print.jpg" height="18" width="18"></a></td>
              <td align="center" style="width:10%"><?php if ($myrow['return_type'] == 'MRN' && $myrow['debit_note_status'] == '0') { ?><a href="index.php?route=Account/ACBillbookPayment_debit&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&action=<?php echo "addnew"; ?>&VPrefix=16&purchase_id=<?php echo $myrow['purchase_bill_id']; ?>&id=<?php echo $myrow['id'] ?>" target="_blank">Create Debit Note</a><?php } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                    echo "No Permission";
                                                                                                                                                                                                                                                                                                                                                                                                                                                  } ?></td>
            </tr>
          <?php $i++;
          }
        } else { ?>
          <tr>
            <td colspan="12" align="center"><strong>No Record Found</strong></td>
          </tr>
        <?php
        }
        ?>
      </table>

    <?php }
    ?>

    <?php
    include('includes/footer.inc');
    ?>
    <?php if (isset($_SESSION['success_msg'])) { ?>
      <script>
        alert("<?php echo ($_SESSION['success_msg']); ?>");
      </script>
    <?php
    }
    unset($_SESSION['success_msg']);
    ?>
  </div>
</div>