<?php
ob_start();
$PageSecurity = 4;
$PageName = 'Purchase Requisition';
include('includes/session.inc');
include('includes/function_lib.php');
include('cocoon_config.php');
$VendorId = $_REQUEST['vendorId'];

if (isset($_REQUEST['vendorId']))
  // $vendor_state=$s->getField('suppliers','id',$_REQUEST['vend'],'state');
  $vendor_state = $s->getField('suppliers', 'id', $_REQUEST['vendorId'], 'state');

if (isset($_REQUEST['site']))
  $loc_state = $s->getField('locations', 'loccode', $_REQUEST['site'], 'state');

$category = $_REQUEST['reqCat'];

$vendorType = $s->GetFieldValue('suppliers', "id='" . $VendorId . "'", 'vendor_type');
$itemids = $_REQUEST['items'];
$itemQty = $_REQUEST['qty'];
$itemArrays = explode(',', $itemids);
$mapQty = $itemQty;
$mapQtyArrays = explode(',', $mapQty);

?>
<br>
<h3>Item Details</h3>
<?php if ($vendorType != 'Domestic') { ?>

  <div style="width:100%; overflow:auto;">
    <table width="100%" border="0" cellspacing="0" cellpadding="3" style="margin-bottom:5px;" class="thborder">
      <tr>
        <th width="4%">S. No.</th>
        <th>Item Name</th>
        <th width="5%">UOM</th>
        <!--<th colspan="2"><strong>Specification</strong></th> -->
        <th width="7%">Qty.</th>
        <th width="7%">Quoted Rate</th>
        <th width="7%">Amount</th>
        <th colspan="2">Discount</th>
        <th width="7%">Basic Amount</th>
        <th width="7%">Currency</th>
      </tr>
      <?php
      $i = 0;
      for ($i = 0; $i < count($itemArrays); $i++) {
        $ItemName = $s->GetFieldValue('item_master', "id='" . $itemArrays[$i] . "'", 'item_name');
        //  $UnitId=$s->GetFieldValue('item_master',"id='".$itemArrays[$i]."'",'mst_unitid');

        $size = $s->GetFieldValue('requisitionorderdetails', "requsitionno='" . $_REQUEST['prno'] . "' and mst_itemid='" . $itemArrays[$i] . "'", 'size');
        $grade = $s->GetFieldValue('requisitionorderdetails', "requsitionno='" . $_REQUEST['prno'] . "' and mst_itemid='" . $itemArrays[$i] . "'", 'grade');
        $UnitId = $s->GetFieldValue('requisitionorderdetails', "requsitionno='" . $_REQUEST['prno'] . "' and mst_itemid='" . $itemArrays[$i] . "'", 'uom');
        $UnitName = $s->GetFieldValue('unitsofmeasure', "unitid='" . $UnitId . "'", 'unitname');
      ?>
        <tr>
          <td><?php echo ($i + 1) . '.'; ?></td>
          <td><input type="hidden" name="itemID[]" id="itemID_<?php echo $i; ?>" value="<?php echo $itemArrays[$i]; ?>" />
            <?php echo $ItemName; ?></td>
          <td><?php echo $UnitName; ?><input type="hidden" name="mstunit_id[]" value="<?php echo $UnitId; ?>"></td>
          <td><input type="hidden" name="qty[]" id="qty_<?php echo $i; ?>" value="<?php echo $mapQtyArrays[$i]; ?>" /><?php echo $mapQtyArrays[$i]; ?></td>
          <td><input type="text" autocomplete="off" name="rate[]" id="rate_<?php echo $i; ?>" onkeyup="calcTotAmt1(this.value,'<?php echo $i; ?>');" onkeypress="javascript:return isNumberKey(this,event)" maxlength="11" onblur="javascript:validate_floating(this.value,this.id)" /></td>
          <td><input type="text" readonly name="price[]" id="price_<?php echo $i; ?>" /></td>
          <td width="5%"><input type="text" style="width:50px;" name="Discount1[]" onkeyup="DiscountAmounts1(this.value,this.id)" autocomplete="off" id="Discount_<?php echo $i; ?>" onkeypress="javascript:return isNumberKey(this,event)" maxlength="11" onblur="javascript:validate_floating(this.value,this.id)" class="dis" /> </td>
          <td width="5%"><select style="width:50px;" name="DiscountType1[]" id="PerAmo_<?php echo $i; ?>" onchange="DiscountAmounts1(this.value,this.id)">
              <option value="Amount" <?php echo ($_POST['PerAmo'] == 'Amount') ? 'selected' : '' ?>>Amt</option>
              <option value="PercentAmt" <?php echo ($_POST['PerAmo'] == 'PercentAmt') ? 'selected' : '' ?>>%</option>
            </select></td>
          <td><input type="text" style="width:100%;" id="basic_<?php echo $i; ?>" name="basic_Amt1[]" readonly /></td>
          <?php
          $vendorcountry = $s->GetFieldValue('suppliers', "id='" . $VendorId . "'", 'country');
          ?>
          <td><select style="width:100%;" name="currency[]" id="currency">
              <?php $curr_sql = DB_query("Select * from currency_master where country='" . $vendorcountry . "'", $db);
              while ($curr_row = DB_fetch_array($curr_sql)) { ?>
                <option value="<?php echo $curr_row['id']; ?>" <?php echo ($_POST['currency'] == $curr_row['id']) ? 'selected' : '' ?>><?php echo $curr_row['abbreviation']; ?></option>
              <?php } ?>
            </select></td>
        </tr>
      <?php } ?>
    </table>
  </div>
  <input type="hidden" name="hiditemcount" id="hiditemcount" value="<?php echo count($itemArrays); ?>" />


  <table width="100%" cellspacing="0" cellpadding="3">
    <tr>
      <td valign="top">
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="20%" align="right" valign="top">Terms and Conditions</td>
            <td><textarea name="VenderQutation_TermCondition" id="VenderQutation_TermCondition" style="width:98%; height:80px; border:#999999 1px solid;"><?php if (isset($_REQUEST['VenderQutation_TermCondition'])) {
                                                                                                                                                            echo $_REQUEST['VenderQutation_TermCondition'];
                                                                                                                                                          } ?></textarea></td>
          </tr>
        </table>
      </td>
      <td width="34%" valign="top">
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="36%" align="right">Total Amount</td>
            <td><input type="text" readonly="readonly" name="basic_amt" id="total_item_amount" onfocus="cal_totalamount()" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Round Off</td>
            <td><input type="text" name="pluMin" readonly id="pluMin" style="width:23%;loat:left;" /> <input type="text" name="roundVal" readonly id="roundVal" style="width:74%;" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Grand Total</td>
            <td><input type="text" name="GrandTotal" readonly id="GrandTotal" onfocus="cal_grandamount()" /><input type="hidden" id="orig_grand_tot" /></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
<?php } else {  ?>
  <div style="width:100%; overflow:auto;">
    <table width="100%" border="0" cellspacing="0" cellpadding="3" id="tableDataProduction" style="margin-bottom:5px;" class="thborder">
      <tr>
        <th width="3%">S. No.</th>
        <th>Item Name</th>
        <th width="4%">UOM</th>
        <th width="5%">Qty.</th>
        <th width="5%">Free Qty.</th>
        <th width="5%">Brand</th>
        <th width="5%">Quoted Rate </th>
        <th width="5%">Total Price</th>
        <th colspan="2">Discount</th>
        <th width="5%">Basic Amount</th>
        <th width="5%">Pkg Amt.</th>
        <!-- <th width="4%">Excise flag</th>
  <th width="4%">Excise</th>
  <th width="4%">Excise Value</th>
  <th width="4%">E-Cess</th>
  <th width="4%">E-Cess Value</th>
  <th width="4%">SAH-E Cess</th>
  <th width="4%">SAH-E Cess Value</th> -->
        <th width="5%">Taxable<br />Amt.</th>
        <th width="5%">Freight and P&amp;S</th>
        <th width="5%">GST</th>
        <th width="5%">Value</th>
        <!-- <th width="4%">Surcharge</th> -->
        <th width="5%">Surcharge Amt</th>
        <th width="5%">Total<br /> Amt.</th>
      </tr>
      <?php

      //print_r($itemArrays);

      $i = 0;
      for ($i = 0; $i < count($itemArrays); $i++) {
        $ItemName = $s->GetFieldValue('item_master', "id='" . $itemArrays[$i] . "'", 'item_name');
        $UnitId = $s->GetFieldValue('requisitionorderdetails', "requsitionno='" . $_REQUEST['prno'] . "' and mst_itemid='" . $itemArrays[$i] . "'", 'uom');
        $UnitName = $s->GetFieldValue('unitsofmeasure', "unitid='" . $UnitId . "'", 'unitname');

        /* $purchase_vat=$s->GetFieldValue('item_master',"id='".$itemArrays[$i]."'",'vat_purchase');
             $purchase_cst=$s->GetFieldValue('item_master',"id='".$itemArrays[$i]."'",'cst_purchase');
             if($vendor_state==$loc_state)
             {
              $vatid=$purchase_vat;
             }
             else
             {
              $vatid=$purchase_cst;
             } */

        $purchase_gst = $s->GetFieldValue('item_master', "id='" . $itemArrays[$i] . "'", 'gst_percentage');
        // $purchase_cst=$s->GetFieldValue('item_master',"id='".$itemArrays[$i]."'",'cst_purchase');

        //$vatid=$purchase_gst;


        // $UnitName=$s->GetFieldValue('unitsofmeasure',"unitid='".$UnitId."'",'unitname');
        //echo $itemArrays[$i].' ';
        $ItemCode = $s->GetFieldValue('item_master', "id='" . $itemArrays[$i] . "'", 'StockID');
        $itemSql = "select id from itemforms where item_code='" . $ItemCode . "'";
        $item_sql = mysqli_query($link1, $itemSql);
        $item_row = mysqli_fetch_array($item_sql);
        $brndSql = "select id,brand_desc from branditems where itemform_id='" . $item_row['id'] . "'";
        $brand_sql = mysqli_query($link1, $brndSql);
      ?>
        <tr>
          <td><?php //echo ($i+1).'.'; 
              ?><input type="button" onclick="javascript:addRow(this,this.id);" id="add_<?php echo $i; ?>" value="+"></td>
          <td><input type="hidden" name="itemID[]" id="itemID_<?php echo $i; ?>" value="<?php echo $itemArrays[$i]; ?>" /><?php echo $ItemName; ?></td>
          <td><?php echo $UnitName; ?><input type="hidden" name="mstunit_id[]" value="<?php echo $UnitId; ?>"></td>
          <td><input type="hidden" name="qty[]" id="qty_<?php echo $i; ?>" value="<?php echo $mapQtyArrays[$i]; ?>" /><?php echo $mapQtyArrays[$i]; ?></td>
          <td><input type="text" name="freeqty[]" id="freeqty_<?php echo $i; ?>" onkeypress="return isNumberKey(event)" /> </td>
          <td><select name="brand[]" id="brand_<?php echo $i; ?>" style="width:100px;">
              <option value="">Brand</option><?php while ($brand_row = DB_fetch_array($brand_sql)) { ?>
                <option value="<?php echo $brand_row['id']; ?>"><?php echo $brand_row['brand_desc']; ?></option>
              <?php }  ?>
            </select></td>
          <td><input type="text" autocomplete="off" name="rate[]" onkeypress="return isNumberKey(event)" id="rate_<?php echo $i; ?>"
              onkeyup="calcTotAmtInt(this.value,this.id);" /></td>
          <td><input type="text" readonly name="price[]" id="price_<?php echo $i; ?>" style="width:100%;" class='basic' /></td>
          <td width="5%"><input type="text" value="0" style="width:50px;" name="Discount1[]" onkeypress="return isNumberKey(event)" onkeyup="DiscountAmountsInt(this.value,this.id<?php //echo $i; 
                                                                                                                                                                                  ?>)" autocomplete="off" id="Discount_<?php echo $i; ?>" class="dis" />
          </td>
          <td width="5%"><select name="DiscountType1[]" id="PerAmo_<?php echo $i; ?>" onchange="DiscountAmountsInt(this.value,this.id<?php //echo $i; 
                                                                                                                                      ?>)">
              <option value="Amount" <?php echo ($_POST['PerAmo'] == 'Amount') ? 'selected' : '' ?>>Amt</option>
              <option value="PercentAmt" <?php echo ($_POST['PerAmo'] == 'PercentAmt') ? 'selected' : '' ?>>%</option>
            </select></td>
          <td><input type="text" id="basic_<?php echo $i; ?>" name="basic_Amt1[]" readonly class="basicafterdiscount" /></td>
          <td><input type='text' id='pkgamt_<?php echo $i; ?>' onkeypress="return isNumberKey(event)" name='pkg_Amt[]' style='width:100%;' onkeyup='pkgamt(this.value,this.id);' value='0' class='pkgamt' /></td>
          <!--<td><select name="Excise_type[<?php //echo $i;
                                            ?>]" style="width:100%;" id="hidBillingType_<?php echo $i; ?>" 
    onchange="checkExcisable(this.value,this.id<?php //echo $i; 
                                                ?>)">
  <option value="">Select</option>
  <option value="NonExcisable">NonExcisable</option>
  <option value="Excisable">Excisable</option>
  <option value="IncludeTax">IncludeTax</option>
  </select></td>        
  <td><select name="Excise_Duty[<?php //echo $i; 
                                ?>]" id="exciseDuty_<?php //echo $i; 
                                                    ?>" onchange="claculateExcise(this.id,this.value)" onfocus="checkexiseflag(this.id<?php //echo $i; 
                                                                                                                                      ?>)">
         <option value="">Select</option>
       <?php
        // $excise_sql=DB_query("select * from excise_tax where ledgertype like'%excisetax%' and form_type='Purchase' group by caption" ,$db);   
        // $excise_sql=DB_query("Select * from excise_tax where caption like '%excise%' and form_type='Purchase'",$db);
        // while($excise_row=DB_fetch_array($excise_sql))
        // {
        //   $g='';
        // if($Excise_Duty==$excise_row['id'])
        //  {$g='selected';}  
        ?>
         <option value="<?php //echo $excise_row['id']; 
                        ?>"><?php //echo $excise_row['caption']; 
                            ?></option>
      <?php //}
      ?>          
    </select><input name="ExcisePercentage[<?php //echo $i; 
                                            ?>]" id="ExcisePercentage_<?php //echo $i; 
                                                                      ?>" type="hidden" readonly /> </td>
    <td><input name="exciseValue[<?php //echo $i; 
                                  ?>]" id="exciseValue_<?php //echo $i; 
                                                        ?>" readonly value="" class='excise' type="text" /></td>
    <?php
        //$cessLedgerSql1=DB_query("Select * from excise_tax where ledgertype like '%cess%' group by caption   ",$db);   
        //$cessLedgerSql2=DB_query("Select * from excise_tax where ledgertype like'%scess%' group by caption ",$db);  
    ?>
  <td><select name="Cess_ID[<?php //echo $i; 
                            ?>]" id="cessID1_<?php //echo $i; 
                                              ?>" onchange='calEcessonchange(this.id,this.value)' onfocus="checkexiseflag(this.id<?php //echo $i; 
                                                                                                                                  ?>)" style="width:100%;"><option value="0">select E-Cess</option>
  <?php //$cessLedger1=DB_fetch_array($cessLedgerSql1)
  ?>
  <option value="<?php //echo $cessLedger1['id'];
                  ?>"><?php //echo $cessLedger1['caption'];
                      ?></option> 
  </select><input name="ECessPercentage[<?php //echo $i; 
                                        ?>]" id="ECessPercentage_<?php //echo $i; 
                                                                  ?>" type="hidden" value="<?php //echo $cessLedger1['def_rate']; 
                                                                                            ?>"  readonly />
  </td>        
  <td><input name="ECessValue[<?php //echo $i; 
                              ?>]" class='ecess' readonly id="ECessValue_<?php //echo $i; 
                                                                          ?>" type="text" readonly />
  </td>        
  <td><select name="SHECess_ID[<?php //echo $i; 
                                ?>]" id="cessID2_<?php //echo $i; 
                                                  ?>" onchange='calSHEcessonchange(this.id,this.value)' onfocus="checkexiseflag(this.id<?php //echo $i; 
                                                                                                                                        ?>)"  ><option value="0">select SAH-E Cess</option>
  <?php //$cessLedger2=DB_fetch_array($cessLedgerSql2)
  ?>
    <option value="<?php //echo $cessLedger2['id'];
                    ?>"><?php //echo $cessLedger2['caption'];
                        ?></option></select>
  <input name="SAHCessPercentage[<?php //echo $i; 
                                  ?>]" id="SAHCessPercentage_<?php //echo $i; 
                                                              ?>" type="hidden" value="<?php //echo $cessLedger2['def_rate']; 
                                                                                        ?>" style="width:100%;" readonly/>
  </td>        
  <td><input name="SAHCessValue[<?php //echo $i; 
                                ?>]" class='shecess' id="SAHCessValue_<?php //echo $i; 
                                                                      ?>" type="text" readonly />
  </td>     -->
          <td><input type="text" name="taxable_amt[<?php echo $i; ?>]" id="taxableAmt_<?php echo $i; ?>" class="taxable" readonly /></td>
          <td><input type="text" name="freightItem[<?php //echo $i; 
                                                    ?>]" onkeypress="return isNumberKey(event)" class='freight' id="freightItem_<?php echo $i; ?>" onkeyup="calcfreight(this.value,this.id<?php //echo $i;
                                                                                                                                                                                          ?>)" /></td>
          <td><input type="text" name="inputVat[<?php //echo $i; 
                                                ?>]" id="inputVat_<?php echo $i; ?>" readonly value="<?php echo $purchase_gst; ?>"></td>
          <td><input type="text" name="taxvalue1[<?php //echo $i; 
                                                  ?>]" class='vat' id="taxvalue_<?php echo $i; ?>" value="" readonly="true" /></td>
          <!--<td><input type='text' name='surchargeper[]'  id='surchargeper_<?php //echo $i; 
                                                                              ?>' value='<?php //echo $s->getField('sales_tax_master','salestax_id',$vatid,'surcharge_rate'); 
                                                                                          ?>' readonly='true'/></td> -->
          <td><input type='text' name='surchargeamt[]' class='surcharge' id='surchargeamt_<?php echo $i; ?>' value='' onkeyup="cal_surchrgeamt(this.value,this.id);" /></td>
          <td><input type="text" id="TotalSpecial_<?php echo $i; ?>" name="TotalAmount[<?php //echo $i;
                                                                                        ?>]" readonly value="0" /></td>
        </tr>
      <?php  } ?>
      <input type="hidden" name="hiditemcount" id="hiditemcount" value="<?php echo count($itemArrays); ?>" />
    </table>
  </div>
  <table width="100%" cellspacing="0" cellpadding="3">
    <tr>
      <td valign="top">
        <?php $terms_conditions = $s->GetFieldValue('terms_condition', "frm_id='" . $_GET['frmid'] . "'", 'terms_conditions');  ?>
        <table width="100%" cellspacing="0" cellpadding="3" border="0">
          <tr class="row1">
            <td width="20%" align="right">Terms and Conditions:</td>
            <td><textarea name="VenderQutation_TermCondition" id="VenderQutation_TermCondition" style="width:100%; height:80px; border:#999999 1px solid;"><?php echo $terms_conditions; //if(isset($_REQUEST['VenderQutation_TermCondition'])){echo $_REQUEST['VenderQutation_TermCondition'];} 
                                                                                                                                                            ?></textarea></td>
          </tr>
        </table>
      </td>
      <td width="34%" valign="top">
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="36%" align="right">Total Basic</td>
            <td><input type="text" readonly="readonly" name="basic_amt" id="total_item_amount" onfocus="cal_totalamount()" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Pkg Charges</td>
            <td><input name="pkg_charges" readonly id="pkg_charges1" readonly type="text" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Discount</td>
            <td><input name="discount_amt_tot" id="discount_amt_tot" type="text" onkeyup="total_dis(this.value)" /></td>
          </tr>
          <!--  <tr class="row1">
        <td align="right">Excise Duty:</td>
        <td><input type="text" name="excise_duty_val" id="excise_duty_val"  value=""  readonly="true"/></td>
                </tr>
                 <tr class="row1">
        <td align="right">Ecess:</td>
         <td><input type="text" name="ecess_val" id="ecess_val"  value=""  readonly="true"/></td>
      </tr>
      <tr class="row1">
        <td align="right">SHEcess:</td>
        <td><input type="text" name="shecess_val" id="shecess_val"  value=""  readonly="true"/></td>
      </tr> -->
          <tr class="row1">
            <td align="right">Taxable Amount</td>
            <td><input name="tot_taxable_amt" readonly id="tot_taxable_amt" readonly type="text" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Freight And P&amp;S</td>
            <td><input type="text" name="Freight" id="TotalCharge" value="" readonly="true" /></td>
          </tr>
          <tr class="row1">
            <td align="right">GST Value</td>
            <td>
              <div id="taxval"><input type="text" name="taxvalue" id="taxvalue" value="" readonly="true" />
            </td>
          </tr>
          <tr class="row1">
            <td align="right">Surcharge Amount</td>
            <td><input name="surcharge_amt" readonly type="text" id="sur_oct_amt" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Insurance</td>
            <td><input name="insurance" id="insurance" type="text" onkeyup="cal_total_grand()" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Miscellaneous</td>
            <td><input type="text" name="miscellaneous" id="miscellaneous" onkeyup="cal_total_grand()" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Round Off</td>
            <td><input type="text" name="pluMin" readonly id="pluMin" style="width:23%;" /> <input type="text" name="roundVal" readonly id="roundVal" style="width:74%;" /></td>
          </tr>
          <tr class="row1">
            <td align="right">Grand Total</td>
            <td><input type="text" name="GrandTotal" readonly id="GrandTotal" onfocus="cal_total_grand();" /><input type="hidden" id="orig_grand_tot" /><!--onfocus="cal_grandamount()"--></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
<?php } ?>