<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');?>
<?php
	if (isset($_POST['op_invoice'])) {
	 	$invoice      = $_POST['op_invoice'];
	 	$item_gid     = $_POST['item_gid'];
	 	$item_cid     = $_POST['item_cid'];
	 	$item_brnd    = $_POST['item_brnd'];
	 	$item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
	 	$item_code    = $_POST['item_code'];
	    $p_price      = $_POST['p_price'];
	 	$min_sale     = $_POST['min_sale'];
	 	$max_sale     = $_POST['max_sale'];
	 	$item_details = mysqli_real_escape_string($conn, $_POST['item_details']);

	 	$item_sku     = $_POST['item_sku'];
	 	$location     = $_POST['location'];
	 	$u_type       = explode(',',$_POST['u_type'],-1)[0];
	 	$p_tax        = ($_POST['p_tax']>0)?$_POST['p_tax']:0;
	 	$s_tax        = ($_POST['s_tax']>0)?$_POST['s_tax']:0;
	 	$batch_id     = ($_POST['batch_id']>0)?$_POST['batch_id']:0;
	 	$item_notif   = ($_POST['item_notif']>0)?$_POST['item_notif']:0;
	 	@$imei_status  = ($_POST['imei_status']>0)?$_POST['imei_status']:0;
	 	$item_weight  = $_POST['item_weight'];
	 	$item_disc    = $_POST['item_disc'];
	 	$war_type     = $_POST['war_type'];
	 	$war_val      = ($_POST['war_type']=='N')?0:$_POST['war_val'];

        $war_id       = $_POST['war_id'];
	 	$op_qty       = $_POST['op_in_qty'];
	 	$u_price      = $_POST['u_price'];
	 	$op_val       = $_POST['op_val'];

	 	$op_date      = $co_f_year_start.' '.$time;

 if ($u_price >0){
	$op_qtyy=$op_qty;
}else{
	$op_qtyy=0;
}
        mysqli_query($conn,"INSERT INTO `item`(`coid`, `bid`, `brid`, `pgid`, `pcid`, `pbid`,`location`, `pro_batchid`, `item_code`, `item_sku`, `pro_type`, `item_name`, `item_details`, `pro_unit`, `mrp_purchase`, `min_sales`, `mrp_sales`, `item_disc`, `pro_ptax`, `pro_stax`, `item_notif`, `war_type`, `wa_dmy`, `item_weight`, `item_op_date`,`imei_status`,`active`) 
	        VALUES ('$coid','$bid','$brid','$item_gid', '$item_cid', '$item_brnd', '$location', '$batch_id', '$item_code', '$item_sku', 'P','$item_name','$item_details','$u_type','$p_price','$min_sale','$max_sale','$item_disc','$p_tax','$s_tax','$item_notif','$war_type','$war_val','$item_weight','$op_date','$imei_status', '1')") or die(mysqli_error($conn));
        $litem = mysqli_insert_id($conn);


	 	$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 {

		 $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'];

		 $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_acid = $inv_row['acid'];
         $dr_gcatid = $inv_row['gcatid'];         

		   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`, `datetime`, `active`) 
		   	VALUES ('$coid','$bid','$brid','$cr_acid', '$invoice', 'SOP', '$war_id', '$litem', '$item_code', 'P','+','$op_qtyy','$op_qtyy','$p_price','$u_price','$max_sale','$max_sale','$u_price',($u_price*$op_qtyy),($u_price*$op_qtyy),'Opining Item','$uid','$op_date','1')") or die(mysqli_error($conn));

	       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_acid', '$cr_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$invoice', 'SOP','$item_name','opening','$op_val','$op_date','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));

	       $dr_balance = balance($dr_acid)['ac_balance'];
	       $cr_balance = balance($cr_acid)['ac_balance'];
	       mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid ='$dr_acid'") 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));

		}
	 }
?>



<?php
		if (isset($_POST['item_id'])) {
		 	$item_id      = $_POST['item_id'];
		 	$item_gid     = $_POST['item_gid'];
		 	$item_cid     = $_POST['item_cid'];
		 	$item_brnd    = $_POST['item_brnd'];
	    	$item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
		 	$item_code    = $_POST['item_code'];
		 	$p_price      = $_POST['p_price'];
		 	$min_sale     = $_POST['min_sale'];
		 	$max_sale     = $_POST['max_sale'];
	 	    $item_details = mysqli_real_escape_string($conn, $_POST['item_details']);
		 	$item_sku     = $_POST['item_sku'];
		 	$location     = $_POST['location'];
		    $u_type       = explode(',',$_POST['u_type'],-1)[0];
		 	$p_tax        = $_POST['p_tax'];
		 	$s_tax        = $_POST['s_tax'];
		 	$batch_id     = $_POST['batch_id'];
		 	$item_notif   = $_POST['item_notif'];
		 	$item_weight  = $_POST['item_weight'];
		 	$item_disc    = $_POST['item_disc'];
		 	$war_type     = $_POST['war_type'];
		 	$war_val      = $_POST['war_val'];
		 	$imei_status  = ($_POST['imei_status']>0)?$_POST['imei_status']:0;


	        mysqli_query($conn,"UPDATE `item` SET  `pgid`='$item_gid',`pcid`='$item_cid',`pbid`='$item_brnd',`location`='$location', `pro_batchid`='$batch_id', `item_code`='$item_code', `item_sku`='$item_sku', `item_name`='$item_name', `item_details`='$item_details', `pro_unit`='$u_type', `mrp_purchase`='$p_price', `min_sales`='$min_sale', `mrp_sales`='$max_sale', `item_disc`='$item_disc', `pro_ptax`='$p_tax', `pro_stax`='$s_tax', `item_notif`='$item_notif', `war_type`='$war_type', `wa_dmy`='$war_val', `item_weight`='$item_weight',`imei_status`='$imei_status' WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND pro_id='$item_id'") or die(mysqli_error($conn));
	 }	
	 ?>