<?php session_start(); include 'xyz.php';


if(isset($_POST['myPID'])){ $qty = $_POST['myQTY']; 
 $selPID =$_POST['myPID'];
$myDiscount =$_POST['myDiscount'];
$itemInfo =getProduct($selPID);
$realCost = calcDiscount($itemInfo['item_cost'],$itemInfo['item_offer']);
$tamt =$realCost*$qty;		$code =md5(code_generator(15));
if(QueryDB("SELECT COUNT(*) FROM payment where cus_id='".$_SESSION['mycus']."' and pay_status=0 ")->fetchColumn()>0){
	//updating
	$pending =unPaid($_SESSION['mycus']);

	if(in_array($selPID,explode(',',$pending['item']))){
	 $index = array_search($selPID,explode(',',$pending['item']));
	 $newQty =explode(',',$pending['qty']);  $newSPrice =explode(',',$pending['s_price']);
	 $newbPrice =explode(',',$pending['bulk_price']);
	 $newQty[$index] = $qty;
	 $newSPrice[$index] = $realCost;
	 $newbPrice[$index] = $tamt;
	 $amtAll = array_sum($newbPrice);

	 QueryDB("UPDATE payment SET qty='".implode(',',$newQty)."',s_price='".implode(',',$newSPrice)."',bulk_price='".implode(',',$newbPrice)."',receipt_id='$code',amt='$amtAll',amt_before='$amtAll',real_cost='$amtAll',day='".date('Y-m-d')."' where cus_id='".$_SESSION['mycus']."' ");

	}else{

	 $newQty =explode(',',$pending['qty']);  
	 $newSPrice =explode(',',$pending['s_price']);
	 $newbPrice =explode(',',$pending['bulk_price']);
	 $newItem =explode(',',$pending['item']); 

	 $newQty[] = $qty;
	 $newSPrice[] = $realCost;
	 $newbPrice[] = $tamt;
	 $newItem[] = $selPID;
	 $amtAll = array_sum($newbPrice);

QueryDB("UPDATE payment SET  item='".implode(',',$newItem)."', qty='".implode(',',$newQty)."',s_price='".implode(',',$newSPrice)."',bulk_price='".implode(',',$newbPrice)."',receipt_id='$code',amt='$amtAll',amt_before='$amtAll',real_cost='$amtAll',day='".date('Y-m-d')."' where cus_id='".$_SESSION['mycus']."' ");

	}

echo 2;
}else{
$insert_into_payment = QueryDB("INSERT INTO payment VALUES('','".$_SESSION['mycus']."','$tamt',
'$selPID','$qty','$realCost','$tamt','','$tamt','','','','','','$code','2','','0','$tamt','$myDiscount','','','".date('Y-m-d')."','1')");
echo 1;
}

	
}


//Trash cart Item
if(isset($_POST['row'])){
$findItem = QueryDB("SELECT * FROM payment where id='".$_POST['row']."' ");
$rows = $findItem->fetch(PDO::FETCH_ASSOC);
$item = explode(',',$rows['item']);  $sPrice = explode(',',$rows['s_price']);
$blk_price = explode(',',$rows['bulk_price']); $total = explode(',',$rows['amt']);
$qty = explode(',',$rows['qty']);
$index =  $_POST['where'];
unset($item[$index]);
unset($sPrice[$index]); 
unset($blk_price[$index]);
unset($total[$index]);
unset($qty[$index]);

$item = array_values($item); 
$sPrice = array_values($sPrice);
$blk_price = array_values($blk_price);
$total = array_values($total);
$qty = array_values($qty);



$amtAll = array_sum($total);
$code =md5(code_generator(15));
QueryDB("UPDATE payment SET  item='".implode(',',$item)."', qty='".implode(',',$qty)."',s_price='".implode(',',$sPrice)."',bulk_price='".implode(',',$blk_price)."',receipt_id='$code',amt='$amtAll',amt_before='$amtAll',real_cost='$amtAll',day='".date('Y-m-d')."' where id='".$_POST['row']."' ");

echo 1;
}


//Change Price
if(isset($_POST['Mrow'])){ $rowNum = $_POST['Mrow']; $index =$_POST['colsIndex'];
$code =md5(code_generator(15));
$findItem = QueryDB("SELECT * FROM payment where id='$rowNum' ");
$rows = $findItem->fetch(PDO::FETCH_ASSOC);
$item = explode(',',$rows['item']);  $sPrice = explode(',',$rows['s_price']);
$blk_price = explode(',',$rows['bulk_price']); $total = explode(',',$rows['amt']);
$qty = explode(',',$rows['qty']);

$qty[$index]=$_POST['newQty']; $old_single_price =$sPrice[$index];
$blk_price[$index] =$old_single_price*$_POST['newQty'];
$total = array_sum($blk_price);

QueryDB("UPDATE payment SET   qty='".implode(',',$qty)."',bulk_price='".implode(',',$blk_price)."',receipt_id='$code',amt='$total',amt_before='$total',real_cost='$total',day='".date('Y-m-d')."' where id='".$_POST['Mrow']."' ");

echo 1;

}






if(isset($_POST['stateIdN'])){  //echo $_POST['stateIdN'];
    foreach(QueryDB("SELECT * FROM lga WHERE state_id ='".$_POST['stateIdN']."' and status=0 ")as $st){ echo '<option value="'.$st['name'].'" id="'.$st['id'].'">'.$st['name'].'</option>';
 } 
}



?>