<?php
	require('../config/dbcon.php');
	$data = [];	
	for($i=0; $i < count($_POST['mem_id']); $i++){
		if (!empty($_POST['order_qty'][$i])) {
			$order_number = $_POST['order_number'];
	        $srno = $_POST['order_number'];
	        $srnoupd = $_POST['srno'] + 1;
	        
	        $orgDate = $_POST['order_date'];  
    		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
    		$order_date = $newDate;
	        
			$supplier_name = $_POST['supplier_name'];
			$supplier_code = $_POST['supplier_code'];
			$supplier_place = $_POST['supplier_place'];
			$supplier_gstno = $_POST['supplier_gstno'];
            
			$partno =$_POST['partno'][$i];
			$description_one =$_POST['description_one'][$i];
			$list =$_POST['list'][$i];
			$stk_qty =$_POST['stk_qty'][$i];
			$ly_sales =$_POST['ly_sales'][$i];
			$cy_sales =$_POST['cy_sales'][$i];
			$pending =$_POST['pending'][$i];
			$dispatch1 =$_POST['dispatch1'][$i];
			$moq =$_POST['moq'][$i];
			$order_qty =$_POST['order_qty'][$i];
			$boxqty =$_POST['boxqty'][$i];
			$disc_per =$_POST['disc_per'][$i];
			$disc_amt =$_POST['disc_amt'][$i];
			$cost =$_POST['cost'][$i];
			$segment =$_POST['segment'][$i];
			$hsn_code =$_POST['hsn_code'][$i];
			$partvalue = $order_qty * $cost;
            
            $sql = "insert into order_mst(order_number,order_date,supplier_name,supplier_code,supplier_place,supplier_gstno,partno,description_one,list,stk_qty,ly_sales,cy_sales,pending,dispatch1,moq,order_qty,boxqty,disc_per,disc_amt,cost,segment,hsn_code,part_value) values ('".$order_number."','".$order_date."','".$supplier_name."','".$supplier_code."','".$supplier_place."','".$supplier_gstno."','".$partno."','".$description_one."',".$list.",".$stk_qty.",".$ly_sales.",".$cy_sales.",".$pending.",".$dispatch1.",".$moq.",".$order_qty.",".$boxqty.",".$disc_per.",".$disc_amt.",".$cost.",'".$segment."','".$hsn_code."',".$partvalue.")";
		    $result = mysqli_query($connect, $sql);
		    
		    $query1 = mysqli_query($connect, "select partno,description_one,op_stk,pending,dispatch1,bal_qty from item_mst where partno = '" . $partno . "'") or die(mysqli_error());
				while($fetch1 = mysqli_fetch_array($query1)){
			        $op_stk = $fetch1['op_stk'];
			        $pending = $fetch1['pending'];
			        $totalpending = $pending + $order_qty;
			        $dispatch1 = $fetch1['dispatch1'];
			        $balqty = $totalpending - $dispatch1;
			        mysqli_query($connect, "update item_mst set pending = '" . number_format($totalpending, 2, '.', '') . "', bal_qty = '" . number_format($balqty, 2, '.', '') . "' WHERE partno = '" . $partno . "'");
				}
		}
		    if($result)
            {
                $update="update control_mst set srno = '".$srnoupd."' where description='ORDER'";
		        mysqli_query($connect, $update) or die(mysqli_error());
            }
            else
            {
            echo "Error";
            }
	}	
	header('location: index');
?>