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

function filterAndTypecastNumbers($value) {
    // Typecast to number (integer or float)
    $number = $value + 0; // This will typecast the value to a number

    // Check if the value is not empty and not zero
    return !empty($number) || $number === 0;
}

if(isset($_POST['serveWine'])) {$cus_id ='';
if(is_numeric(draw_in('selCusName'))){ $cus_id =draw_in('selCusName'); }
$itemID = implode(',',$_POST['mergedArrays']['itemid']);
$quantity = implode(',',$_POST['mergedArrays']['quantity']);
$singleCost = implode(',',$_POST['mergedArrays']['singleCost']);
$bulkCost = array_filter($_POST['mergedArrays']['bulkcost'],'filterAndTypecastNumbers');
if(count($bulkCost)==1){ $bulkCost = (int)$bulkCost[0];  }else{
$bulkCost = implode(',',$bulkCost);
}
$totalSum = array_sum($_POST['mergedArrays']['bulkcost']);
$pay_mth =$_POST['pay_mth'];
$paying =  $_POST['paying'];
$addCost = $_POST['realCost'];
$selCusName = $_POST['selCusName'];
$myAddr = $_POST['myAddr'];
 //selCusName




$myCode =code_pics(3); $receiptNo = code_pics(4);
$diff = $addCost-$paying; 
$cus_id='';
if(is_numeric($selCusName)){ $cus_id =$selCusName; }else{
    $data = array('frm_name'=>$selCusName,'frm_phone'=>'','frm_email'=>'','frm_month'=>date('m'),'frm_year'=>date('Y'),'frm_date'=>date('Y-m-d'),'frm_time'=>time(),'staff_code'=>$_SESSION['sales'],'gender'=>'','cus_id'=>$myCode,'address'=>$myAddr,'cus_type'=>1);
    $linkr->insert('customers',$data);
    $cus_id =get_std_ID($myCode);
}


if($diff>0){
QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','$itemID','$quantity','$singleCost','$bulkCost','".$paying."','$addCost','".date('Y-m-d')."','$pay_mth','$diff','".date('m')."','".date('Y')."','$receiptNo','1','".$_SESSION['sales']."','1','$addCost','','".time()."','1','".date('Y-m-d')."','$my_branch_code') ");

QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','$itemID','$quantity','$singleCost','$bulkCost','','$diff','','','$diff','','','$receiptNo','1','".$_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'=>$myAddr,'date_time'=>date('Y-m-d H:i:s'),'cus_type'=>1));
   echo $receiptNo;
}else if($diff==0){
QueryDB("INSERT INTO payment VALUES('','".$cus_id."','".$addCost."','$itemID','$quantity','$singleCost','$bulkCost','".$paying."','$addCost','".date('Y-m-d H:i:s')."','$pay_mth','$diff','".date('m')."','".date('Y')."','$receiptNo','1','".$_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'=>$myAddr,'date_time'=>date('Y-m-d H:i:s'),'cus_type'=>1));
echo $receiptNo;
}else if($paying>$addCost){
   echo 'nil';
}



 }

?>
