CasperSecurity
<?php
session_start();
include '../../../../web/connection/connectionv2.php';
include '../../../../web/connection/con.php';
include '../../../../web/connection/encryption.php';
$userId = $_SESSION['USER_ID'];
$db_handle = new DBController();
$ku= $db_handle-> runQuery("SELECT * FROM user WHERE user_id = '$userId' ");
foreach($ku as $vu){
$employee_idu = $vu['employee_id'];
}
$db_handle = new DBController();
$kc= $db_handle-> runQuery("SELECT * FROM counter_sale WHERE employee_id = '$employee_idu' ");
foreach($kc as $vc){
$employeecs_id = $vc['employee_id'];
$counter_nos = $vc['counter_no'];
$from_time = $vc['from_time'];
$to_time = $vc['to_time'];
}
$customer_mobileno = $_POST['customer_id'];
$orderno = $_REQUEST['orderno'];
if($orderno){
$orderno = $orderno;
}else{
$orderno=0;
}
$db_handle = new DBController();
$custdetails = $db_handle-> runQuery("SELECT * FROM student WHERE registration_no = '$customer_mobileno' ");
foreach($custdetails as $vc) {
$customer_id = $vc['student_registrations_id'];
}
/*$db_handle = new DBController();
$custdetailsss = $db_handle-> runQuery("SELECT * FROM customer_order WHERE customer_order_no = '$orderno' ");
foreach($custdetailsss as $vcss) {
$customer_order_id = $vcss['customer_order_id'];
}
if(!empty($customer_order_id)){
$sorderid = $customer_order_id;
}else{
$sorderid = '0';
}*/
$paymenttype = $_POST['pmode'];
if($paymenttype == 'Cash Payment'){
$sale_type = 'Cash';
$payment_status = 'paid';
}elseif ($paymenttype == 'Online Payment'){
$sale_type = 'Online';
$payment_status = 'paid';
}else{
$sale_type = 'Credit';
$payment_status = 'paid';
}
$sales_invoice_date = date("Y-m-d");
// $cashpayment.'-'.$onlinepayment.'-'.$cardpayment.'-'.$walletpayment;
$coupon_id = $_POST['coupon_id'];
$coupon_amount = $_POST['coupon_amount'];
$voucherid = $_POST['voucherid'];
$payable_amt = $_POST['payable_amt'];
$gross_amount = $_POST['gross_amount'];
$tax_amount = $_POST['tax_amount'];
$net_amount = $_POST['net_amount'];
$bill_discountpes = $_POST['bill_discountpes'];
$returnamtval = $_POST['returnamtval'];
$discountrated = $_POST['discount_rate'];
if(empty($discountrated)){
$discountrate='0.00';
}else{
$discountrate = $_POST['discount_rate'];
}
$creditnotepayment = $_POST['creditnotepayment']; //creditvoucherno
$db_handle = new DBController();
$custorders = $db_handle->runQuery("SELECT * FROM sales ORDER BY sales_id DESC LIMIT 1");
$salenums = $db_handle->numRows("SELECT * FROM sales ORDER BY sales_id DESC LIMIT 1");
if($salenums == 0){
$sales_id="SINV-.'$year.'-'.0001";
}else {
$sales_id=1;
}
foreach($custorders as $custorder){
$sales_id =$custorder['sales_id'];
$sales_id++;
// $sales_id =$custorder['sales_id']+1;
$zero = 0;
$qut = 'SINV';
$year = date("Y");
if(($sales_id >= '10') && ($sales_id < '100')){
$sale_invoice_no = $qut.'-'.$year.'-'.$zero.''.$zero.''.$zero.''.$sales_id;
}elseif(($sales_id >= '100')&&($sales_id < '1000')){
$sale_invoice_no = $qut.'-'.$year.'-'.$zero.''.$zero.''.$sales_id;
}elseif($sales_id >= '1000'){
$sale_invoice_no = $qut.'-'.$year.'-'.$sales_id;
}else{
$sale_invoice_no = $sale_invoice_noyy;
}
}
//echo $sale_invoice_no;
$db_handle = new DBController();
$vouchercoupons = $db_handle->runQuery("SELECT * FROM voucher_manage");
$vouchernums = $db_handle->numRows("SELECT * FROM voucher_manage");
if($vouchernums == 0){
$vcode="BMPV001";
}else {
$vcode = 1;
}
foreach($vouchercoupons as $vcoupon){
$vcode =$vcoupon['id'];
$vcode++;
if($vcode < 10){
$vcode="BMPV00".$vcode;
}elseif($vcode < 100){
$vcode="BMPV0".$vcode;
}else{
$vcode="BMPV".$vcode;
}
}
date_default_timezone_set("Asia/Kolkata");
$date = date("Y-m-d H:i:s");
$active = 'yes';
// $wallet_balance = '0.00';
$status = '1';
//echo $vcode;
$db_handle = new DBController();
$custdetailsnum = $db_handle-> numRows("SELECT * FROM student WHERE registration_no = '$customer_mobileno' ");
if($custdetailsnum == '0'){
$_SESSION['ERROR_MSG'] = "Please Add Customer";
$_SESSION['MSG_ALRT'] = "FALSE";
header('location:../../../../web/home/modules/university/Canteen/direct_sales/direct_sales_grid.php');
}else {
//wallet//
$walletpaymentd = $_POST['walletpayment'];
if(empty($walletpaymentd)){
$walletpayment = '0.00';
}else{
$walletpayment = $_POST['walletpayment'];
}
$db_handle = new DBController();
$custdetails = $db_handle->runQuery("SELECT * FROM customer_wallet_rules WHERE status = 'yes' ORDER BY id DESC LIMIT 1");
foreach ($custdetails as $vc) {
$minimum_amount = $vc['minimum_amount'];
}
$wallet_point = $payable_amt / $minimum_amount;
if ($walletpayment !="0") {
$wallettransaction_type = 'debit';
} else {
$wallettransaction_type = 'credit';
}
$db_handle = new DBController();
$selectwrule = $db_handle->numRows("SELECT * FROM customer_wallets WHERE customer_id = '$customer_id'");
if($selectwrule==1){
if ($wallettransaction_type =="debit") {
$db_handle = new DBController();
$walletdetails = $db_handle->runQuery("SELECT * FROM customer_wallets
WHERE customer_id = '$customer_id'");
foreach ($walletdetails as $wd) {
//$transaction_type = $wd['transaction_type'];
$customer_wallet_point = $wd['wallet_balance'];
}
$wbalance = $customer_wallet_point - $walletpayment;
$db_handle = new DBController();
$newUpdate1 = $db_handle->runMyUpdate("UPDATE customer_wallets SET wallet_balance = '$wbalance', updated_at='$date',updated_by='$userId' WHERE customer_id = '$customer_id'");
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO customer_wallet_transactions(customer_id,transaction_type,customer_order_id,customer_order_net_amount,customer_wallet_point,status,created_by,created_at)
VALUES ('$customer_id','debit','$sorderid','$payable_amt','$walletpayment','$status','$userId','$date')");
$remainingwallet_point = ($payable_amt - $walletpayment) / $minimum_amount;
$finalbal=$wbalance+$remainingwallet_point;
$db_handle = new DBController();
$newUpdate1 = $db_handle->runMyUpdate("UPDATE customer_wallets SET wallet_balance = '$finalbal', updated_at='$date',updated_by='$userId' WHERE customer_id = '$customer_id'");
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO customer_wallet_transactions(customer_id,transaction_type,customer_order_id,customer_order_net_amount,customer_wallet_point,status,created_by,created_at)
VALUES ('$customer_id','credit','$sorderid','$payable_amt','$remainingwallet_point','$status','$userId','$date')");
}else{
$db_handle = new DBController();
$walletdetails = $db_handle->runQuery("SELECT * FROM customer_wallets
WHERE customer_id = '$customer_id' ORDER BY customer_id desc LIMIT 1");
foreach ($walletdetails as $wd) {
$transaction_type = $wd['transaction_type'];
$customer_wallet_point = $wd['customer_wallet_point'];
$wallet_balance = $wd['wallet_balance'];
}
$wbalance = $wallet_balance + $wallet_point;
$db_handle = new DBController();
$newUpdate1 = $db_handle->runMyUpdate("UPDATE customer_wallets SET wallet_balance = '$wbalance', updated_at='$date',updated_by='$userId' WHERE customer_id = '$customer_id'");
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO customer_wallet_transactions(customer_id,transaction_type,customer_order_id,customer_order_net_amount,customer_wallet_point,status,created_by,created_at)
VALUES ('$customer_id','credit','$sorderid','$payable_amt','$wallet_point','$status','$userId','$date')");
}
}
else{
//Credit//
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO customer_wallet_transactions(customer_id,transaction_type,customer_order_id,customer_order_net_amount,customer_wallet_point,status,created_by,created_at)
VALUES ('$customer_id','credit','$sorderid','$payable_amt','$wallet_point','$status','$userId','$date')");
$db_handle = new DBController();
$newwalletinsertt = $db_handle->tableinsert("INSERT INTO customer_wallets(customer_id,wallet_id,wallet_balance,status,created_by,created_at)
VALUES ('$customer_id','$customer_mobileno','$wallet_point','$status','$userId','$date')");
}
//wallet//
$db_handle = new DBController();
$newInsert = $db_handle->tableinsert("INSERT INTO sales(sales_order_id,sale_type,saleInvoice_no,sale_invioce_date,customer_id,employee_id,counter_no,from_time,to_time,gross_amount,tax_amount,discount_amount,bill_dicount_rate,net_amount,loyalty_redeem_amount,payable_amount,sales_status,payment_status,payment_mode,created_on,created_by,is_active)
VALUES ('$sorderid','$sale_type','$sale_invoice_no','$sales_invoice_date','$customer_id','$employeecs_id','$counter_nos','$from_time','$to_time','$gross_amount','$tax_amount','$bill_discountpes','$discountrate','$net_amount','$walletpayment','$payable_amt','$payment_status','$payment_status','$paymenttype','$date','$userId','$active')");
//coupon//
if($coupon_amount) {
$db_handle = new DBController();
$couponslabdetails = $db_handle->runQuery("SELECT * FROM coupon_slab_details WHERE id='$coupon_id'");
foreach($couponslabdetails as $couponslab) {
$couponID=$couponslab['id'];
$coupon_code=$couponslab['coupon_code'];
}
$db_handle = new DBController();
$newInsert = $db_handle->tableinsert("INSERT INTO coupon_manage(customer_id,saleid,customer_mob,total_bill_amount,coupon_id,coupon_code,status,created_on,created_by,is_active)
VALUES ('$customer_id','$newInsert','$customer_mobileno','$payable_amt','$couponID','$coupon_code','applied','$date','$userId','$active')");
}
//coupon//
//voucher//
if($voucherid){
$db_handle = new DBController();
$voucherslabdetails = $db_handle->runQuery("SELECT * FROM voucher_slab_details WHERE status='active' AND id='$voucherid'");
foreach($voucherslabdetails as $vslab) {
$voucherslab_id = $vslab['id'];
$voucher_min_amount = $vslab['min_amount'];
$voucher_valid_to = $vslab['voucher_valid_to'];
$curr_date = date('Y-m-d');
$db_handle = new DBController();
$newInsertvoucher = $db_handle->tableinsert("INSERT INTO voucher_manage(invoice_no,invoice_amount,customer_id,voucher_id,voucher_code,status,created_on,created_by,is_active)
VALUES ('$sale_invoice_no','$payable_amt','$customer_id','$voucherslab_id','$vcode','new','$date','$userId','$active')");
}
}
//voucher//
if($coupon_amount){
$type='coupon';
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$coupon_amount','$date','$userId')");
}
$cashpayment = $_POST['cashpayment'];
if($cashpayment){
$type='cash';
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$cashpayment','$date','$userId')");
}
$onlinepayment = $_POST['onlinepayment'];
if($onlinepayment){
$type='online';
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$onlinepayment','$date','$userId')");
}
$cardpayment = $_POST['cardpayment'];
if($cardpayment){
$type='card';
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$cardpayment','$date','$userId')");
}
$walletpayment = $_POST['walletpayment'];
if(!empty($walletpayment)){
$type='wallet';
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$walletpayment','$date','$userId')");
}
//creditvoucher//
if($creditnotepayment){
$type='credit note';
$db_handle = new DBController();
$creditnotedetails = $db_handle-> runQuery("SELECT * FROM sales_return WHERE credit_voucher_no = '$creditnotepayment'");
foreach($creditnotedetails as $crdetails) {
$returnamount = $crdetails['total_amount'];
$credit_voucher_no = $crdetails['credit_voucher_no'];
}
$db_handle = new DBController();
$newwalletinsert = $db_handle->tableinsert("INSERT INTO salepaymentdetails(saleid,customerid,type,amount,created_at,created_by)
VALUES ('$newInsert','$customer_id','$type','$returnamount','$date','$userId')");
$db_handle = new DBController();
$newUpdate1 = $db_handle->runMyUpdate("UPDATE sales_return SET adjustment_slip_no = '$sale_invoice_no', modified_on='$date',modified_by='$userId' WHERE credit_voucher_no = '$creditnotepayment'");
}
//creditvoucher//
foreach($_SESSION['cart_items'] as $k => $v) {
$product = $v['itm'];
$db_handle = new DBController();
$product = $db_handle-> runQuery("SELECT * FROM product WHERE product_name ='$product'");
foreach($product as $productt) {
$product_id = $productt['product_id'];
}
$batch = $v['batch'];
$sales_quantity = $v['qt'];
$unit_rate = $v['rt'];
$amountz = $v['amt'];
$sale_discount_rate = '0.00';
$discountamt = '0.00';
$tax = $v['tax'];
$cgst_ratee = $v['cgstrt'];
if(!empty($cgst_ratee)){
$cgst_rate = $cgst_ratee;
}else{
$cgst_rate = 0;
}
$sgst_ratee = $v['sgstrt'];
if(!empty($sgst_ratee)){
$sgst_rate = $sgst_ratee;
}else{
$sgst_rate = 0;
}
$cgstamt = $v['cgstamt'];
$sgstamt = $v['sgstamt'];
$totalgstamt = $cgstamt + $sgstamt;
$net_amt = $v['namt'];
$puritem_id = $v['puritem_id'];
$purchase_qty = $v['purqty'];
$soldqty = $v['soldqty'];
$totalsalesqtyz = $soldqty + $sales_quantity;
$db_handle = new DBController();
$newUpdate = $db_handle->runMyUpdate("UPDATE canteen_purchase_item SET sold_quantity = '$totalsalesqtyz' WHERE purchase_item_id = '$puritem_id'");
$db_handle = new DBController();
$newInsert2 = $db_handle->tableinsert("INSERT INTO sales_item(sales_id,purchase_item_id,sale_quantity,sale_mrp,sale_discount_rate,sale_discount_amount,sale_amount,sale_cgst_rate,sale_sgst_rate,cgst_amt,sgst_amt,sale_gst_amount,sale_net_amount)
VALUES ('$newInsert','$puritem_id','$sales_quantity','$unit_rate','$sale_discount_rate','$discountamt','$amountz','$cgst_rate','$sgst_rate','$cgstamt','$sgstamt','$totalgstamt','$net_amt')");
$db_handle = new DBController();
$selectpurchase_inv = $db_handle-> runQuery("SELECT *,canteen_purchase_item.purchase_item_id,SUM(canteen_purchase_item.mrp) AS Totalmrpamt,SUM(item_selling_price.selling_price) AS Totalselling_priceamt FROM (canteen_purchase_item
INNER JOIN item_selling_price ON canteen_purchase_item.purchase_item_id = item_selling_price.purchase_item_id)
WHERE canteen_purchase_item.purchase_item_id = '$puritem_id'");
foreach($selectpurchase_inv as $respurinv) {
$MRP = $respurinv['Totalmrpamt'];
$SP = $respurinv['Totalselling_priceamt'];
$QTY = $sales_quantity;
$sale_mrp = $itemd['sale_mrp'];
$sale_netamount = $itemd['sale_net_amount'];
$sale_mrpp += $sale_netamount;
// $sale_mrpp += $sale_mrp;
$sale_mrppamt = number_format("$sale_mrpp",2);
$amtt = $QTY * $sale_mrp;
$amttt = number_format("$amtt",2);
$amttttotal += $amttt;
$amttttot = number_format("$amttttotal",2);
$mrp = $respurinv['mrp'];
$tmrp = $mrp * $QTY;
$Totalmrpamt += $tmrp;
$selling_price = $respurinv['selling_price'];
$tselling_price = $selling_price * $QTY;
$Totalbmpamt += $tselling_price;
$salesInvoiceamtz =$Totalmrpamt - $Totalbmpamt;
$salesInvoiceamt = number_format("$salesInvoiceamtz",2);
}
$db_handle = new DBController();
$newUpdate = $db_handle->runMyUpdate("UPDATE sales SET save_amount = '$salesInvoiceamtz' WHERE sales_id = '$newInsert'");
}
$saleinvoicenoenct = amebi_crypt($sale_invoice_no,e);
$returnamtvale = amebi_crypt($returnamtval,e);
if($newInsert2 == TRUE){
$_SESSION['ERROR_MSG'] = "Success";
$_SESSION['MSG_ALRT'] = "TRUE";
unset($_SESSION["cart_items"]);
header('location:../../../../web/home/modules/university/Canteen/direct_sales/sales_print_first.php?salesinvoiceno='.$saleinvoicenoenct.'&returnamtval='.$returnamtvale);
}else{
$_SESSION['ERROR_MSG'] = "Unsuccess";
$_SESSION['MSG_ALRT'] = "FALSE";
unset($_SESSION["cart_items"]);
header('location:../../../../web/home/modules/university/Canteen/direct_sales/sales_print_first.php?salesinvoiceno='.$saleinvoicenoenct.'&returnamtval='.$returnamtvale);
}
}
?>