<?php require_once('../auth/auth.php');?>
<?php
if (isset($_POST['oinvoice'])) {
	$item_id  = $_POST['item_id'];
	$oinvoice = $_POST['oinvoice'];
	
    $eoid=mysqli_fetch_assoc(mysqli_query($conn,"SELECT eoid FROM order_list_edit WHERE coid='$coid' AND invoice='$oinvoice' AND pro_id='$item_id' LIMIT 1"))['eoid'];
	if ($eoid>0) {

     mysqli_query($conn,"UPDATE order_list_edit LEFT JOIN pro_unit ON (order_list_edit.pro_unit=pro_unit.puid) SET qty =(qty+1),t_amount=(amount*qty*unit_convart),net_amount=(t_amount-dis_amount) WHERE order_list_edit.coid='$coid' AND invoice='$oinvoice' AND pro_id = '$item_id' AND eoid='$eoid'")or die(mysqli_error($conn));

	} else {

    mysqli_query($conn,"INSERT INTO `order_list_edit`(`coid`, `bid`, `brid`, `invoice`, `order_type`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `pro_unit`, `unit_val`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `amount`, `t_amount`, `net_amount`,`details`, `userid`, `active`) 
     	SELECT '$coid' , '$bid' , '$brid' , '$oinvoice' , 'EADJ' , pro_id , item_code , pro_type , '+' , 1 , item.pro_unit , unit_convart , mrp_purchase , avg_cost , mrp_sales , mrp_sales , mrp_purchase , (mrp_purchase*unit_convart) , (mrp_purchase*unit_convart) ,location, '$uid' , 0 FROM item LEFT JOIN pro_unit ON (item.pro_unit=pro_unit.puid) WHERE pro_id='$item_id' AND item.coid ='$coid' LIMIT 1") or die(mysqli_error($conn));
 }
}

// for scan 
if (isset($_POST['sinvoice'])) {
	$item_code  = $_POST['item_code'];
	$oinvoice   = $_POST['sinvoice'];
	
	$item_id = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pro_id FROM item WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND item_code='$item_code' LIMIT 1"))['pro_id'];

    $eoid=mysqli_fetch_assoc(mysqli_query($conn,"SELECT eoid FROM order_list_edit WHERE coid='$coid' AND invoice='$oinvoice' AND pro_id='$item_id' LIMIT 1"))['eoid'];
	if ($eoid>0) {

     mysqli_query($conn,"UPDATE order_list_edit LEFT JOIN pro_unit ON (order_list_edit.pro_unit=pro_unit.puid) SET qty =(qty+1),t_amount=(amount*qty*unit_convart),net_amount=(t_amount-dis_amount) WHERE order_list_edit.coid='$coid' AND invoice='$oinvoice' AND pro_id = '$item_id' AND eoid='$eoid'")or die(mysqli_error($conn));

	}else {

    mysqli_query($conn,"INSERT INTO `order_list_edit`(`coid`, `bid`, `brid`, `invoice`, `order_type`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `pro_unit`, `unit_val`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `amount`, `t_amount`, `net_amount`, `userid`, `active`) 
     	SELECT '$coid' , '$bid' , '$brid' , '$oinvoice' , 'EADJ' , pro_id , item_code , pro_type , '+' , 1 , item.pro_unit , unit_convart , mrp_purchase , avg_cost , mrp_sales , mrp_sales , mrp_purchase , (mrp_purchase*unit_convart) , (mrp_purchase*unit_convart) , '$uid' , 0 FROM item LEFT JOIN pro_unit ON (item.pro_unit=pro_unit.puid) WHERE pro_id='$item_id' AND item.coid ='$coid' LIMIT 1") or die(mysqli_error($conn));
 }
}
?>