<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');?>
<?php
	if (isset($_POST['up_imei_id'])) {
		$up_imei_id    = $_POST['up_imei_id'];
	 	$invoice       = $_POST['op_invoice'];
	 	$litem         = $_POST['item_id'];
	 	$imei_code     = $_POST['imei_code'];
	 	$imei_color    = $_POST['imei_color'];
	 	$mfg_date      = $_POST['mfg_date'];
	 	$exp_date      = $_POST['exp_date'];
	 	$op_qty        = 1;
	 	$op_date      = $co_f_year_start.' '.$time;


	   $op_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM item WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND  pro_id='$litem' LIMIT 1"));
                      
           $item_code = $op_row['item_code'];
           $p_price   = $op_row['mrp_purchase'];
           $u_price   = $op_row['mrp_purchase'];
           $mrp_sales = $op_row['mrp_sales'];
           $op_val    = $op_qty * $p_price;


		   $war_id = mysqli_fetch_assoc(mysqli_query($conn,"SELECT bwid FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND   brid='$brid' AND active=1 AND default_set='primary' LIMIT 1"))['bwid'];

               if ($up_imei_id==0) {
             
           $ch_imei=mysqli_query($conn,"SELECT imei_code FROM imei_tracker WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND imei_code='$imei_code' LIMIT 1");
				if (mysqli_num_rows($ch_imei)>0)  {echo "Al Ready Have IMEI EXIT.";} else {
                    $chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE coid='$coid' AND invoice='$invoice' LIMIT 1");if (mysqli_num_rows($chinvoice)>0 || $litem <1 || $op_val<0.01) {}else {


                    // Get Owner Claim Account id
					 $op_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='opening' LIMIT 1"));
			         $cr_acid   = $op_row['acid'];
			         $cr_gcatid = $op_row['gcatid'];


		           mysqli_query($conn,"INSERT INTO `imei_tracker`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `imei_code`, `imei_color`, `mfg_date`, `exp_date`, `plus`, `pu_price`, `sale_price`, `date`, `active`) 
			        VALUES ('$coid','$bid','$brid','$cr_acid', '$invoice','SOP','$war_id','$litem', '$imei_code','$imei_color','$mfg_date','$exp_date','+','$p_price','$mrp_sales','$op_date','1')") or die(mysqli_error($conn));
 
                          
                    // Load Order List Row By Item Id
				    mysqli_query($conn,"INSERT INTO `order_list`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`,`change_slprice`, `amount`, `net_amount`, `t_amount`, `details`, `userid`,`em_id`,`datetime`, `active`) 
				    	VALUES ('$coid','$bid','$brid','$cr_acid', '$invoice', 'SOP', '$war_id', '$litem', '$item_code', 'P','+','$op_qty','$op_qty','$p_price','$u_price','$mrp_sales','$mrp_sales','$u_price','$u_price',($u_price*$op_qty),'Opining IMEI Item','$uid','$us_em_id','$op_date','1')") or die(mysqli_error($conn));

                    // Get inventory Account id
					 $inv_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='stock' LIMIT 1"));
			           $dr_acidddd = $inv_row['acid'];
			         $dr_gcatid = $inv_row['gcatid'];  

                    // Item Valuation Ladger create By Inventory Dr && Owner Claim Cr
			        mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
				        VALUES ('$coid','$bid','$brid','$dr_acidddd', '$cr_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$invoice', 'SOP','$imei_code','opening','$op_val','$op_date','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));

                    //  Inventory Dr && Owner Claim Cr Valuation Update
				       $dr_balance = balance($dr_acidddd)['ac_balance'];
				       $cr_balance = balance($cr_acid)['ac_balance'];
				       mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid ='$dr_acidddd'") or die(mysqli_error($conn));
				       mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance' WHERE coid='$coid' AND acid ='$cr_acid'") or die(mysqli_error($conn));

		    } // Invoice Duplicate Check Close
	    } // imei code Duplicate Check Close
	} // IMEI insert Query End 
	else if($up_imei_id >0) {
		mysqli_query($conn,"UPDATE imei_tracker SET imei_code='$imei_code',imei_color='$imei_color',mfg_date='$mfg_date',exp_date='$exp_date' WHERE coid='$coid' AND imei_id='$up_imei_id'") or die(mysqli_error($conn));

	} 


	}  // isset Close
?>