<?php  session_start();  if(!isset($_SESSION['sales'])){ header('location:login.php'); die();}include 'dbconn.php'; 
include 'inc/functions.php';
include 'xyz.php';


$totalSum = array_sum($_POST['tCost']);
$myCode =code_pics(3); $receiptNo = code_pics(4);$addCost =draw_in('realCost');
$paying = collect('paying'); $diff = $addCost-$paying; 
$cus_id='';
if(is_numeric(draw_in('userName'))){ $cus_id =draw_in('userName'); }else{
	$data = array('frm_name'=>draw_in('userName'),'frm_phone'=>draw_in('userPhone'),'frm_email'=>draw_in('userEmail'),'frm_month'=>date('m'),'frm_year'=>date('Y'),'frm_date'=>date('Y-m-d'),'frm_time'=>time(),'staff_code'=>$_SESSION['sales'],'gender'=>draw_in('eqSex'),'cus_id'=>$myCode,'address'=>draw_in('myAddr'),'cus_type'=>draw_in('prog_type'));
	$linkr->insert('customers',$data);
	$cus_id =get_std_ID($myCode);
}

// $inData = array('cus_id'=>$cus_id,'amt'=>$addCost,'item'=>implode(',',array_filter($_POST['Item'])),'qty'=>implode(',',array_filter($_POST['QTY'])),'s_price'=>implode(',',array_filter($_POST['singleCost'])),'bulk_price'=>$implode(',',array_filter($_POST['tCost'])),'amt_paid'=>$paying,'amt_before'=>$addCost,'date_paid'=>date('Y-m-d'),'pay_mth'=>draw_in('pay_mth'),'out_standing'=>$diff,'month'=>date('m'),'year'=>date('Y'),'receipt_id'=>$receiptNo,'type'=>draw_in('prog_type'),'onboarded'=>$_SESSION['sales'],'pay_status'=>1,'real_cost'=>$addCost,'discount'=>'','time_paid'=>time());
if($diff>0){
QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','".implode(',',array_filter($_POST['Item']))."','".implode(',',array_filter($_POST['QTY']))."','".implode(',',array_filter($_POST['singleCost']))."','".implode(',',array_filter($_POST['tCost']))."','".$_POST['paying']."','$addCost','".date('Y-m-d')."','{$_POST['pay_mth']}','$diff','".date('m')."','".date('Y')."','$receiptNo','".draw_in('prog_type')."','".$_SESSION['sales']."','1','$addCost','','".time()."','1','".date('Y-m-d')."','$my_branch_code') ");

QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','".implode(',',array_filter($_POST['Item']))."','".implode(',',array_filter($_POST['QTY']))."','".implode(',',array_filter($_POST['singleCost']))."','".implode(',',array_filter($_POST['tCost']))."','','$diff','','','$diff','','','$receiptNo','".draw_in('prog_type')."','".$_SESSION['sales']."','0','$addCost','','".time()."','1','".date('Y-m-d')."','$my_branch_code') ");
	$linkr->insert('biller',array('user_id'=>$cus_id,'receipt_id'=>$receiptNo,'organization'=>draw_in('myAddr'),'date_time'=>date('Y-m-d H:i:s'),'cus_type'=>draw_in('prog_type')));
	echo $receiptNo;
}else if($diff==0){
QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','".implode(',',array_filter($_POST['Item']))."','".implode(',',array_filter($_POST['QTY']))."','".implode(',',array_filter($_POST['singleCost']))."','".implode(',',array_filter($_POST['tCost']))."','".$_POST['paying']."','$addCost','".date('Y-m-d H:i:s')."','{$_POST['pay_mth']}','$diff','".date('m')."','".date('Y')."','$receiptNo','".draw_in('prog_type')."','".$_SESSION['sales']."','1','$addCost','','".time()."','1','".date('Y-m-d')."','$my_branch_code') ");
$linkr->insert('biller',array('user_id'=>$cus_id,'receipt_id'=>$receiptNo,'organization'=>draw_in('myAddr'),'date_time'=>date('Y-m-d H:i:s'),'cus_type'=>draw_in('prog_type')));
echo $receiptNo;
}else if($paying>$addCost){
	echo 'nil';
}

//insert into biller






?>