<?php
require_once("../config/config.php");
$docid=base64_decode($_REQUEST[id]);
$po_sql="SELECT * FROM billing_master where challan_no='".$docid."'";
$po_res=mysqli_query($link1,$po_sql);
$po_row=mysqli_fetch_assoc($po_res);
///// after hitting receive button ///
if($_POST){
 if ($_POST['upd']=='Receive'){
	$ref_no=base64_decode($_POST['refno']);
	mysqli_autocommit($link1, false);
	$flag = true;
	////// run data cycle of invoice and get posted value of receive qty
	$sql_inv_data="select * from billing_model_data where challan_no='".$ref_no."'";
    $res_invData=mysqli_query($link1,$sql_inv_data)or die("error1".mysqli_error($link1));
    while($row_invData=mysqli_fetch_assoc($res_invData)){
		  ///// initialize posted variables
		  $reqqty="req_qty".$row_invData['id'];
		  $okqty="ok_qty".$row_invData['id'];
		  $damageqty="damage_qty".$row_invData['id'];
		  $missqty="miss_qty".$row_invData['id'];
		  
		  ///// update stock in inventory //
		  if(mysqli_num_rows(mysqli_query($link1,"select partcode from stock_status where partcode='".$row_invData['prod_code']."' and asc_code='".$_POST['billto']."'"))>0){
			 ///if product is exist in inventory then update its qty 
			 $result=mysqli_query($link1,"update stock_status set qty=qty+'".$_POST[$reqqty]."',okqty=okqty+'".$_POST[$okqty]."',broken=broken+'".$_POST[$damageqty]."',missing=missing+'".$_POST[$missqty]."',updatedate='".$datetime."' where partcode='".$row_invData['prod_code']."' and asc_code='".$_POST['billto']."'");
		  }
		  else{
			 //// if product is not exist then add in inventory
			 $result=mysqli_query($link1,"insert into stock_status set asc_code='".$_POST['billto']."',partcode='".$row_invData['prod_code']."',qty=qty+'".$_POST[$reqqty]."',okqty='".$_POST[$okqty]."',broken='".$_POST[$damageqty]."',missing='".$_POST[$missqty]."',uom='PCS',updatedate='".$datetime."'");
		  }
		   //// check if query is not executed
		   if (!$result) {
	           $flag = false;
               echo "Error details1: " . mysqli_error($link1) . ".";
           }
		   ////// insert in stock ledger////
		   ### CASE 1 if user enter somthing in ok qty
		   if($_POST[$okqty]!="" && $_POST[$okqty]!=0 && $_POST[$okqty]!=0.00){
		      $flag=stockLedger($ref_no,$today,$row_invData['prod_code'],$_POST['billfrom'],$_POST['billto'],$_POST['billto'],"IN","OK",$_POST['invtype'],$_POST[$okqty],$row_invData['price'],$_SESSION['userid'],$today,$currtime,$ip,$link1,$flag);
		   }
		   ### CASE 2 if user enter somthing in damage qty
		   if($_POST[$damageqty]!="" && $_POST[$damageqty]!=0 && $_POST[$damageqty]!=0.00){
		      $flag=stockLedger($ref_no,$today,$row_invData['prod_code'],$_POST['billfrom'],$_POST['billto'],$_POST['billto'],"IN","DAMAGE",$_POST['invtype'],$_POST[$damageqty],$row_invData['price'],$_SESSION['userid'],$today,$currtime,$ip,$link1,$flag);
		   }
		   ### CASE 3 if user enter somthing in missing qty
		   if($_POST[$missqty]!="" && $_POST[$missqty]!=0 && $_POST[$missqty]!=0.00){
		      $flag=stockLedger($ref_no,$today,$row_invData['prod_code'],$_POST['billfrom'],$_POST['billto'],$_POST['billto'],"IN","MISSING",$_POST['invtype'],$_POST[$missqty],$row_invData['price'],$_SESSION['userid'],$today,$currtime,$ip,$link1,$flag);
		   }
		   ///// update data table
           $result=mysqli_query($link1,"update billing_model_data set okqty='".$_POST[$okqty]."',damageqty='".$_POST[$damageqty]."',missingqty='".$_POST[$missqty]."' where id='".$row_invData['id']."'");
		   //// check if query is not executed
		   if (!$result) {
	           $flag = false;
               echo "Error details2: " . mysqli_error($link1) . ".";
		   }		   
	}//// close while loop
	//// Update status in  master table
    $result=mysqli_query($link1,"update billing_master set status='Received',receive_date='".$today."',receive_time='".$currtime."',receive_by='".$_SESSION['userid']."',receive_remark='".$_POST['rcv_rmk']."',receive_ip='".$ip."' where challan_no='".$ref_no."'");
	//// check if query is not executed
    if (!$result) {
	   $flag = false;
	   echo "Error details3: " . mysqli_error($link1) . ".";
    }

  ///// update billing_imei_data  table set ownercode equal to to location ////////////////////////
	$result_data=mysqli_query($link1,"update billing_imei_data set owner_code ='".$_POST['to_location']."' where doc_no='".$ref_no."'");
	//// check if query is not executed
    if (!$result_data) {
	   $flag = false;

	   echo "Error details3: " . mysqli_error($link1) . ".";
    }
	


	////// insert in activity table////
	$flag=dailyActivity($_SESSION['userid'],$ref_no,$_POST['invtype'],"RECEIVE",$ip,$link1,$flag);
	///// check both master and data query are successfully executed
	if ($flag) {
        mysqli_commit($link1);
        $msg = "Invoice is successfully received against ".$ref_no;
    } else {
		mysqli_rollback($link1);
		$msg = "Request could not be processed. Please try again.";
	} 
    mysqli_close($link1);
	///// move to parent page
    header("location:receiveInvoice.php?msg=".$msg."".$pagenav);
    exit;
 }
}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <script src="../js/jquery.min.js"></script>
 <link href="../css/font-awesome.min.css" rel="stylesheet">
 <link href="../css/abc.css" rel="stylesheet">
 <script src="../js/bootstrap.min.js"></script>
 <link href="../css/abc2.css" rel="stylesheet">
 <link rel="stylesheet" href="../css/bootstrap.min.css">

 <link rel="stylesheet" href="../css/jquery.dataTables.min.css">
 <script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
 <script type="text/javascript">
$(document).ready(function(){
    $('#myTable').dataTable();
});
$(document).ready(function(){
    $("#frm2").validate();
});
</script>
<script type="text/javascript">
function checkRecQty(a){
	var reqqty=0;
	var okqty=0;
	var damageqty=0;
	//// check requested qty
    if(document.getElementById("req_qty"+a).value==""){
       reqqty=0;
	}else{
	   reqqty=parseInt(document.getElementById("req_qty"+a).value);
	}
	//// check enter ok qty
    if(document.getElementById("ok_qty"+a).value==""){
       okqty=0;
    }else{
       okqty=parseInt(document.getElementById("ok_qty"+a).value);
    }
	//// check enter damage qty
    if(document.getElementById("damage_qty"+a).value==""){
       damageqty=0;
    }else{
       damageqty=parseInt(document.getElementById("damage_qty"+a).value);
    }
	//// check enter qty should not be greater than requested qty
    if(reqqty < (okqty + damageqty)){
       alert("Ok Qty & Damage Qty can not more than requested Qty!");
		document.getElementById("miss_qty"+a).value=0;
		document.getElementById("damage_qty"+a).value=0;
		//document.getElementById("ok_qty"+a).focus();
		document.getElementById("upd").disabled=true;
    }else{
		document.getElementById("miss_qty"+a).value=(reqqty - (okqty + damageqty));
		//document.getElementById("miss_qty"+a).focus();
		document.getElementById("upd").disabled=false;
	}
}
</script>
<script src="../js/frmvalidate.js"></script>
<script type="text/javascript" src="../js/jquery.validate.js"></script>
<script type="text/javascript" src="../js/common_js.js"></script>
</head>
<body>
<div class="container-fluid">
 <div class="row content">
	<?php 
    include("../includes/leftnav2.php");
    ?>
   <div class="col-sm-9">
      <h2 align="center"><i class="fa fa-level-down"></i> Receive Invoice Details</h2>
      <h4 align="center"><?php echo $po_row['type']." ".$po_row['document_type'];?></h4>
   <div class="panel-group">
   <form id="frm2" name="frm2" class="form-horizontal" action="" method="post">
    <div class="panel panel-default table-responsive">
        <div class="panel-heading heading1">Party Information</div>
         <div class="panel-body">
          <table class="table table-bordered" width="100%">
            <tbody>
              <tr>
                <td width="20%"><label class="control-label">Billing To</label></td>
                <td width="30%"><?php echo str_replace("~",",",getLocationDetails($po_row['to_location'],"name,city,state",$link1));?><input name="billto" id="billto" type="hidden" value="<?=$po_row['to_location']?>"/></td>
                <td width="20%"><label class="control-label">Billing From</label></td>
                <td width="30%"><?php echo str_replace("~",",",getLocationDetails($po_row['from_location'],"name,city,state",$link1));?><input name="billfrom" id="billfrom" type="hidden" value="<?=$po_row['from_location']?>"/></td>
              </tr>
              <tr>
                <td><label class="control-label">Invoice No.</label></td>
                <td><?php echo $po_row['challan_no'];?><input name="invtype" id="invtype" type="hidden" value="<?php echo $po_row['type']." ".$po_row['document_type'];?>"/></td>
                <td><label class="control-label">Billing Date</label></td>
                <td><?php echo $po_row['sale_date'];?></td>
              </tr>
              <?php if($po_row['ref_no']!='' && $po_row['document_type']=="INVOICE"){ ?>
              <tr>
                <td><label class="control-label">Ref. Invoice No.</label></td>
                <td><?php echo $po_row['ref_no'];?></td>
                <td><label class="control-label">Ref. Invoice Date</label></td>
                <td><?php echo $po_row['ref_date'];?></td>
              </tr>
              <?php } ?>
              <tr>
                <td><label class="control-label">Entry By</label></td>
                <td><?php echo getAdminDetails($po_row['entry_by'],"name",$link1);?></td>
                <td><label class="control-label">Status</label></td>
                <td><?php echo $po_row['status'];?></td>
              </tr>
              <tr>
                <td><label class="control-label">Discount Type</label></td>
                <td><?php echo getDiscountType($po_row['discountfor']);?></td>
                <td><label class="control-label">Tax Type</label></td>
                <td><?php echo getTaxType($po_row['taxfor']);?></td>
              </tr>
            </tbody>
          </table>
        </div><!--close panel body-->
    </div><!--close panel-->
    <br><br>
    <div class="panel panel-default table-responsive">
      <div class="panel-heading heading1">Items Information</div>
      <div class="panel-body">
       <table class="table table-bordered" width="100%">
            <thead>
              <tr class="<?=$tableheadcolor?>" >
                <th style="text-align:center" width="3%">#</th>
                <th style="text-align:center" width="15%">Product</th>
                <th style="text-align:center" width="10%">Disp. Qty</th>
                <th style="text-align:center" width="10%">Ok Qty</th>
                <th style="text-align:center" width="10%">Damage Qty</th>
                <th style="text-align:center" width="10%">Missing Qty</th>
                <th style="text-align:center" width="7%">Price</th>
                <th style="text-align:center" width="10%">Value</th>
                <th style="text-align:center" width="5%">Disc./ Unit</th>
                <th style="text-align:center" width="10%">Tax Amount</th>
                <th style="text-align:center" width="10%">Total</th>
              </tr>
            </thead>
            <tbody>
            <?php
			$i=1;
			$podata_sql="SELECT * FROM billing_model_data where challan_no='".$docid."'";
			$podata_res=mysqli_query($link1,$podata_sql);
			while($podata_row=mysqli_fetch_assoc($podata_res)){
				$proddet=explode("~",getProductDetails($podata_row['prod_code'],"productname,productcolor",$link1));
			?>
              <tr>
                <td><?=$i?></td>
                <td><?=$proddet[0]." (".$proddet[1].")"?></td>
                <td style="text-align:right"><input type="hidden" name="req_qty<?=$podata_row[id]?>" id="req_qty<?=$i?>" value="<?=$podata_row['qty']?>"><?=$podata_row['qty']?></td>
                <td style="text-align:right"><input type="text" class="digits form-control" style="width:80px;" name="ok_qty<?=$podata_row[id]?>" id="ok_qty<?=$i?>"  autocomplete="off" required onBlur="checkRecQty('<?=$i?>');myFunction(this.value,'<?=$i?>','ok_qty');" onKeyPress="return onlyNumbers(this.value);" value="<?=round($podata_row['qty'])?>" ></td>
                <td style="text-align:right"><input type="text" class="digits form-control" style="width:80px;" name="damage_qty<?=$podata_row[id]?>" id="damage_qty<?=$i?>"  autocomplete="off" required onBlur="checkRecQty('<?=$i?>');myFunction(this.value,'<?=$i?>','damage_qty');" onKeyPress="return onlyNumbers(this.value);" value="0" ></td>
                <td style="text-align:right"><input type="text" class="digits form-control" style="width:80px;" name="miss_qty<?=$podata_row[id]?>" id="miss_qty<?=$i?>"  autocomplete="off" required onBlur="checkRecQty('<?=$i?>');myFunction(this.value,'<?=$i?>','miss_qty');" onKeyPress="return onlyNumbers(this.value);" value="0" readonly ></td>
                <td style="text-align:right"><?=$podata_row['price']?></td>
                <td style="text-align:right"><?=$podata_row['value']?></td>
                <td style="text-align:right"><?=$podata_row['discount']?></td>
                <td style="text-align:right"><?=$podata_row['tax_amt']?></td>
                <td style="text-align:right"><?=$podata_row['totalvalue'];?></td>
              </tr>
            <?php
			$i++;
			}
			?>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
    <br><br>
    <div class="panel panel-default table-responsive">
      <div class="panel-heading heading1">Amount Information</div>
      <div class="panel-body">
        <table class="table table-bordered" width="100%">
            <tbody>
              <tr>
                <td width="20%"><label class="control-label">Sub Total</label></td>
                <td width="30%"><?php echo $po_row['basic_cost'];?></td>
                <td width="20%"><label class="control-label">Total Discount</label></td>
                <td width="30%"><?php echo $po_row['discount_amt'];?></td>
              </tr>
              <tr>
                <td><label class="control-label">Grand Total</label></td>
                <td><?php echo currencyFormat($po_row['total_cost']);?><input type="hidden" name="grand_total" id="grand_total" value="<?=$po_row['total_cost']?>" readonly/></td>
                <td><label class="control-label">Total Tax</label></td>
                <td><?php echo currencyFormat($po_row['tax_cost']);?></td>
              </tr>
               <tr>
                <td><label class="control-label">Delivery Address</label></td>
                <td><?php echo $po_row['deliv_addrs'];?></td>
                <td><label class="control-label">Remark</label></td>
                <td><?php echo $po_row['disp_rmk'];?></td>
              </tr>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
    <div class="panel panel-default table-responsive">
      <div class="panel-heading">Logistic Information</div>
      <div class="panel-body">
        <table class="table table-bordered" width="100%">
            <tbody>
              <tr>
                <td width="20%"><label class="control-label">Logistic Name</label></td>
                <td width="30%"><?php echo getLogistic($po_row['diesel_code'],$link1);?></td>
                <td width="20%"><label class="control-label">Docket No.</label></td>
                <td width="30%"><?php echo $po_row['docket_no'];?></td>
              </tr>
              <tr>
                <td><label class="control-label">Logistic Person</label></td>
                <td><?php echo $po_row['logistic_person'];?></td>
                <td><label class="control-label">Contact No.</label></td>
                <td><?php echo $po_row['logistic_contact'];?></td>
              </tr>
               <tr>
                 <td><label class="control-label">Carrier No.</label></td>
                 <td><?php echo $po_row['vehical_no'];?></td>
                 <td><label class="control-label">Dispatch Date</label></td>
                 <td><?php echo $po_row['dc_date'];?></td>
               </tr>
               <tr>
                <td><label class="control-label">Dispatch Remark</label></td>
                <td colspan="3"><?php echo $po_row['disp_rmk'];?></td>
                </tr>
               <tr>
                 <td><label class="control-label">Receive Remark <span style="color:#F00">*</span></label></td>
                 <td colspan="3"><textarea name="rcv_rmk" id="rcv_rmk" class="form-control required" style="resize:none;width:500px;" required></textarea></td>
               </tr>
               <tr>
                 <td colspan="4" align="center">
                 <input type="submit" class="btn<?=$btncolor?>" name="upd" id="upd" value="Receive" title="Receive This Invoice">&nbsp;
                    <input name="refno" id="refno" type="hidden" value="<?=base64_encode($po_row['challan_no'])?>"/>
                 <input name="to_location" id="to_location" type="hidden" value="<?=$po_row['to_location']?>"/>
                 <input title="Back" type="button" class="btn<?=$btncolor?>" value="Back" onClick="window.location.href='receiveInvoice.php?<?=$pagenav?>'"></td>
                </tr>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
    <br><br>
    </form>
  </div><!--close panel group-->
 </div><!--close col-sm-9-->
</div><!--close row content-->
</div><!--close container-fluid-->
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>