File: /home/mostafedeg/public_html/erp/controllers/calcquickprofit.php
<?php
//global varable
global $showoutside;
//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");
//here the db files that include in the file
include("../public/include_dao.php");
//Transaction
include_once("../models/sql/Transaction.class.php");
//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');
//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');
//Storereport
require_once('../models/dao/StorereportDAO.class.php');
require_once('../models/dto/Storereport.class.php');
require_once('../models/mysql/StorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/StorereportMySqlExtDAO.class.php');
//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');
//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.class.php');
//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');
//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');
//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');
//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');
//Productunit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');
//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');
//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');
//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');
//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');
////
require_once('../models/dao/QuickprofitclientDAO.class.php');
require_once('../models/dto/Quickprofitclient.class.php');
require_once('../models/mysql/QuickprofitclientMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitclientMySqlExtDAO.class.php');
require_once('../models/dao/QuickprofitdayDAO.class.php');
require_once('../models/dto/Quickprofitday.class.php');
require_once('../models/mysql/QuickprofitdayMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitdayMySqlExtDAO.class.php');
require_once('../models/dao/QuickprofitgeneralDAO.class.php');
require_once('../models/dto/Quickprofitgeneral.class.php');
require_once('../models/mysql/QuickprofitgeneralMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitgeneralMySqlExtDAO.class.php');
require_once('../models/dao/QuickprofitproductDAO.class.php');
require_once('../models/dto/Quickprofitproduct.class.php');
require_once('../models/mysql/QuickprofitproductMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitproductMySqlExtDAO.class.php');
require_once('../models/dao/QuickprofitstoreDAO.class.php');
require_once('../models/dto/Quickprofitstore.class.php');
require_once('../models/mysql/QuickprofitstoreMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitstoreMySqlExtDAO.class.php');
require_once('../models/dao/LastbillidsquickprofitDAO.class.php');
require_once('../models/dto/Lastbillidsquickprofit.class.php');
require_once('../models/mysql/LastbillidsquickprofitMySqlDAO.class.php');
require_once('../models/mysql/ext/LastbillidsquickprofitMySqlExtDAO.class.php');
//get the do the action
$do = $_GET['do'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
//here the global templates
//$smarty->display("header.html");
//here goes the instances and general variables
//Sellbill
$mySellbill = new Sellbill();
$mySellbillRecord = new SellbillMySqlDAO();
$mySellbillEx = new SellbillMySqlExtDAO();
//Sellbilldetail
$mySellbilldetail = new Sellbilldetail();
$mySellbilldetailRecord = new SellbilldetailMySqlDAO();
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();
//Returnsellbill
$myReturnsellbill = new Returnsellbill();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();
//Returnsellbilldetail
$myReturnsellbilldetail = new Returnsellbilldetail();
$myReturnsellbilldetailRecord = new ReturnsellbilldetailMySqlDAO();
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();
//Sellbillandrutern
$mySellbillandrutern = new Sellbillandrutern();
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetail = new Sellandruternbilldetail();
$mySellandruternbilldetailRecord = new SellandruternbilldetailMySqlDAO();
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();
//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();
//Storedetail
$myStoredetailEx = $StoredetailEX = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();
//Store
$myStoreRecord = new StoreMySqlDAO();
//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();
//save
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();
//savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();
$mySavedailyEx = new SavedailyMySqlExtDAO();
//Client
$myClientRecord = new ClientMySqlDAO();
$myClient = new Client();
$myClientEx = new ClientMySqlExtDAO();
//Clientdebtchange
$myClientdebtchange = new Clientdebtchange();
$myClientdebtchangeRecord = new ClientdebtchangeMySqlDAO();
//User
$myUserEx = new UserMySqlExtDAO();
//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
//$Productunit= new Productunit();
//$ProductunitDAO= new ProductunitDAO();
//Productcat
$myProductcatRecord = new ProductcatMySqlDAO();
$myProductcatEx = new ProductcatMySqlExtDAO();
//Unit
$myUnitRecord = new UnitMySqlDAO();
//Productserial
$myProductserial = new Productserial();
$myProductserialEx = new ProductserialMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();
require_once('../models/dao/ClientareaDAO.class.php');
require_once('../models/dto/Clientarea.class.php');
require_once('../models/mysql/ClientareaMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientareaMySqlExtDAO.class.php');
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();
$ClientareaEX = new ClientareaMySqlExtDAO();
//Costcenter
$Costcenter = new Costcenter();
$CostcenterDAO = new CostcenterMySqlDAO();
$CostcenterEX = new CostcenterMySqlExtDAO();
////////////////////////////////////////////////////////////////////////////////
//Relusergrouppropertie
$myRelusergrouppropertie = new Relusergrouppropertie();
$myRelusergrouppropertieRecord = new RelusergrouppropertiesMySqlDAO();
$myRelusergrouppropertieExt = new RelusergrouppropertiesMySqlExtDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
$buyBillDetailExt = new BuybilldetailMySqlExtDAO();
////
$quickProfitClient = new Quickprofitclient();
$quickProfitClientDAO = new QuickprofitclientMySqlDAO();
$quickProfitClientEX = new QuickprofitclientMySqlExtDAO();
//
$quickProfitDay = new Quickprofitday();
$quickProfitDayDAO = new QuickprofitdayMySqlDAO();
$quickProfitDayEX = new QuickprofitdayMySqlExtDAO();
//
$quickProfitGeneral = new Quickprofitgeneral();
$quickProfitGeneralDAO = new QuickprofitgeneralMySqlDAO();
$quickProfitGeneralEX = new QuickprofitgeneralMySqlExtDAO();
//
$quickProfitProduct = new Quickprofitproduct();
$quickProfitProductDAO = new QuickprofitproductMySqlDAO();
$quickProfitProductEX = new QuickprofitproductMySqlExtDAO();
//
$quickProfitStore = new Quickprofitstore();
$quickProfitStoreDAO = new QuickprofitstoreMySqlDAO();
$quickProfitStoreEX = new QuickprofitstoreMySqlExtDAO();
$lastBillIdsQuickProfit = new Lastbillidsquickprofit();
$lastBillIdsQuickProfitDAO = new LastbillidsquickprofitMySqlDAO();
$lastBillIdsQuickProfitEX = new LastbillidsquickprofitMySqlExtDAO();
$lastBillIdsQuickProfit = $lastBillIdsQuickProfitDAO->load(1);
if (!isset($lastBillIdsQuickProfit->id) || empty($lastBillIdsQuickProfit->id)) {
$lastBillIdsQuickProfit->sell = 0;
$lastBillIdsQuickProfit->sellDetail = 0;
$lastBillIdsQuickProfit->sellandret = 0;
$lastBillIdsQuickProfit->sellandretDetail = 0;
$lastBillIdsQuickProfit->retsell = 0;
$lastBillIdsQuickProfit->retsellDetail = 0;
$lastBillIdsQuickProfitDAO->insert($lastBillIdsQuickProfit);
}
$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
//used for quick profit
$sellCostsArray = array();
//check and use the condition that suites this action
if (empty($do)) {
// ####################### 0- truncate table as we are going to calc it for all bills
// $quickProfitStoreEX->truncate();
// $quickProfitProductEX->truncate();
// $quickProfitDayEX->truncate();
// $quickProfitClientEX->truncate();
// $quickProfitGeneralEX->truncate();
####################### 0- we will continue calculating starting from last bill ...so if bill stop calculating profit in thee middle reverse steps
if ($lastBillIdsQuickProfit->sellandretDetail != 0) {//0 means all bill finished calculating sucessfully so check if not to reverse affect
$bill = $mySellbillandruternRecord->load($lastBillIdsQuickProfit->sellandret);
calcProfits("sellAndRet", $bill, 0);
}
if ($lastBillIdsQuickProfit->sell != 0) {//0 means all bill finished calculating sucessfully so check if not to reverse affect
$bill = $mySellbillRecord->load($lastBillIdsQuickProfit->sell);
calcProfits("sell", $bill, 0);
}
if ($lastBillIdsQuickProfit->retsell != 0) {//0 means all bill finished calculating sucessfully so check if not to reverse affect
$bill = $myReturnsellbillRecord->load($lastBillIdsQuickProfit->retsell);
calcProfits("ret", $bill, 0);
}
####################### 1- sell and ret bill
$count = 0;
$start = 0;
$limit = 30;
$end = $start + $limit;
$sellAndRetCount = $mySellbillandruternEx->getBillsCountNotDel(" and sellbillid > $lastBillIdsQuickProfit->sellandret ");
for ($i = $start; $i <= $sellAndRetCount->sellbillid; $i+=$limit) {
$allbills = $mySellbillandruternEx->queryAllOrderedAscLimitedSimple($start, $limit, " and sellbillid > $lastBillIdsQuickProfit->sellandret ");
foreach ($allbills as $value) {
calcProfits("sellAndRet", $value, 1);
}
//
$start += $limit;
$end += $limit;
}
####################### 2- sellbill
$count = 0;
$start = 0;
$limit = 30;
$end = $start + $limit;
$sellCount = $mySellbillEx->getBillsCountNotDel(" and sellbillid > $lastBillIdsQuickProfit->sell ");
for ($i = $start; $i <= $sellCount->sellbillid; $i+=$limit) {
$allbills = $mySellbillEx->queryAllOrderedAscLimitedSimple($start, $limit, " and sellbillid > $lastBillIdsQuickProfit->sell ");
foreach ($allbills as $value) {
calcProfits("sell", $value, 1);
}
//
$start += $limit;
$end += $limit;
}
####################### 3- ret sellbill
$count = 0;
$start = 0;
$limit = 30;
$end = $start + $limit;
$retCount = $myReturnsellbillEx->getBillsCountNotDel(" and returnsellbillid > $lastBillIdsQuickProfit->retsell ");
for ($i = $start; $i <= $retCount->returnsellbillid; $i+=$limit) {
$allbills = $myReturnsellbillEx->queryAllOrderedAscLimitedSimple($start, $limit, " and returnsellbillid > $lastBillIdsQuickProfit->retsell ");
foreach ($allbills as $value) {
calcProfits("ret", $value, 1);
}
//
$start += $limit;
$end += $limit;
}
echo "Done.";
} elseif ($do == "sucess") {
//here the smarty templates
$smarty->display("succes.html");
} elseif ($do == "error") {
//here the smarty templates
$smarty->display("error.html");
}
//here the global templates
//$smarty->display("footer.html");
/* ===============================
function in this CONTROLLER
================================ */
// calcProfits of bill and store it
function calcProfits($billType, $bill, $isadd) {
global $mySellbilldetailRecord;
global $myReturnsellbilldetailRecord;
global $mySellandruternbilldetailRecord;
global $mySellbilldetailEx;
global $myReturnsellbilldetailEx;
global $mySellandruternbilldetailEx;
global $productDAO;
global $sellCostsArray;
global $lastBillIdsQuickProfit;
global $lastBillIdsQuickProfitDAO;
//Transaction
$mytransactions = new Transaction();
try {
//reset it for every bill
$sellCostsArray = array();
switch ($billType) {
// مبيعات و ومردودات
case "sellAndRet":
$billDate = $bill->sellbilldate;
$clientId = $bill->sellbillclientid;
##get $billTotalBeforeDiscount
$billTotalBeforeDiscount = $bill->sellbillaftertotalbill;
##get $billDiscountVal
$sellbillDiscountType = $bill->sellbilldiscounttype;
$billDiscountVal = $bill->sellbilldiscount;
if ($sellbillDiscountType == "2") {
$billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
$billDiscountVal = round($billDiscountVal, 2);
}
####################################################################
if ($isadd == 1) {
//get all rows to calculate profit
$details = $mySellandruternbilldetailRecord->queryBySellbillid($bill->sellbillid);
} else {
//0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
//get some only
$details = $mySellandruternbilldetailEx->queryBySellbillidEX($bill->sellbillid, " and sellbilldetailid <= $lastBillIdsQuickProfit->sellandretDetail order by sellbilldetailid asc ");
}
foreach ($details as $oneDetail) {
if ($oneDetail->conditions == 0) {//if not del
$productId = $oneDetail->sellbilldetailproductid;
$productunitId = $oneDetail->productunitid;
$quantity = $oneDetail->sellbilldetailquantity;
$prototal = $oneDetail->sellbilldetailtotalprice; //after discount
$storeId = $oneDetail->storeid;
##
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##
$buyProduct = new stdClass();
$buyProduct->productId = $productId;
$buyProduct->productBuyPrice = $oneDetail->buyprice;
$buyProduct->lastbuyprice = $oneDetail->lastbuyprice;
$buyProduct->meanbuyprice = $oneDetail->meanbuyprice;
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice_withDiscount;
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice_withDiscount;
$productData = $productDAO->load($buyProduct->productId);
$buyProduct->overAllAveragePrice = $productData->overAllAveragePrice;
if (empty($buyProduct->lastbuyprice_withDiscount) || $buyProduct->lastbuyprice_withDiscount === NULL) {
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice;
}
if (empty($buyProduct->meanbuyprice_withDiscount) || $buyProduct->meanbuyprice_withDiscount === NULL) {
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice;
}
if (empty($buyProduct->overAllAveragePrice) || $buyProduct->overAllAveragePrice === NULL) {
$buyProduct->overAllAveragePrice = $productData->productBuyPrice; //$oneDetail->buyprice;
}
##
$isreturn = 0;
if ($oneDetail->selltype == 1) {
$isreturn = 1;
}
//$isadd = 1;
quickProfitRow($buyProduct, $finalquantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId, $billDate);
$lastBillIdsQuickProfit->sellandretDetail = $oneDetail->sellbilldetailid;
$lastBillIdsQuickProfit->sellandret = $oneDetail->sellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
}
if ($isadd == 1) {// as if it is 0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
quickProfitBill($bill->sellbillprice, $bill->returnsellbillprice, $billDiscountVal, $clientId, 1, $billDate);
$lastBillIdsQuickProfit->sellandretDetail = 0;
$lastBillIdsQuickProfit->sellandret = $bill->sellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
break;
//مبيعات
case "sell":
$billDate = $bill->sellbilldate;
$clientId = $bill->sellbillclientid;
##get $billTotalBeforeDiscount
$billTotalBeforeDiscount = $bill->sellbillaftertotalbill;
##get $billDiscountVal
$sellbillDiscountType = $bill->sellbilldiscounttype;
$billDiscountVal = $bill->sellbilldiscount;
if ($sellbillDiscountType == "2") {
$billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
$billDiscountVal = round($billDiscountVal, 2);
}
####################################################################
if ($isadd == 1) {
//get all rows to calculate profit
$details = $mySellbilldetailRecord->queryBySellbillid($bill->sellbillid);
} else {
//0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
//get some only
$details = $mySellbilldetailEx->queryBySellbillidEX($bill->sellbillid, " and sellbilldetailid <= $lastBillIdsQuickProfit->sellDetail order by sellbilldetailid asc ");
}
foreach ($details as $oneDetail) {
if ($oneDetail->conditions == 0) {//if not del
$productId = $oneDetail->sellbilldetailproductid;
$productunitId = $oneDetail->productunitid;
$quantity = $oneDetail->sellbilldetailquantity;
$prototal = $oneDetail->sellbilldetailtotalprice; //after discount
$storeId = $oneDetail->storeid;
##
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##
$buyProduct = new stdClass();
$buyProduct->productId = $productId;
$buyProduct->productBuyPrice = $oneDetail->buyprice;
$buyProduct->lastbuyprice = $oneDetail->lastbuyprice;
$buyProduct->meanbuyprice = $oneDetail->meanbuyprice;
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice_withDiscount;
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice_withDiscount;
$productData = $productDAO->load($buyProduct->productId);
$buyProduct->overAllAveragePrice = $productData->overAllAveragePrice;
if (empty($buyProduct->lastbuyprice_withDiscount) || $buyProduct->lastbuyprice_withDiscount === NULL) {
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice;
}
if (empty($buyProduct->meanbuyprice_withDiscount) || $buyProduct->meanbuyprice_withDiscount === NULL) {
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice;
}
if (empty($buyProduct->overAllAveragePrice) || $buyProduct->overAllAveragePrice === NULL) {
$buyProduct->overAllAveragePrice = $productData->productBuyPrice; //$oneDetail->buyprice;
}
##
$isreturn = 0;
//$isadd = 1;
quickProfitRow($buyProduct, $finalquantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId, $billDate);
$lastBillIdsQuickProfit->sellDetail = $oneDetail->sellbilldetailid;
$lastBillIdsQuickProfit->sell = $oneDetail->sellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
}
if ($isadd == 1) {// as if it is 0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
quickProfitBill($bill->sellbilltotalbill, 0, $billDiscountVal, $clientId, 1, $billDate);
$lastBillIdsQuickProfit->sellDetail = 0;
$lastBillIdsQuickProfit->sell = $bill->sellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
break;
//مردودات مبيعات
case "ret":
$billDate = $bill->returnsellbilldate;
$clientId = $bill->returnsellbillclientid;
##get $billTotalBeforeDiscount
$billTotalBeforeDiscount = $bill->returnsellbillaftertotalbill;
##get $billDiscountVal
$sellbillDiscountType = $bill->returnsellbilldiscounttype;
$billDiscountVal = $bill->returnsellbilldiscount;
if ($sellbillDiscountType == "2") {
$billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
$billDiscountVal = round($billDiscountVal, 2);
}
####################################################################
if ($isadd == 1) {
//get all rows to calculate profit
$details = $myReturnsellbilldetailRecord->queryByReturnsellbillid($bill->returnsellbillid);
} else {
//0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
//get some only
$details = $myReturnsellbilldetailEx->queryByReturnsellbillidEX($bill->returnsellbillid, " and returnsellbillid <= $lastBillIdsQuickProfit->retsellDetail order by returnsellbillid asc ");
}
foreach ($details as $oneDetail) {
if ($oneDetail->conditions == 0) {//if not del
$productId = $oneDetail->returnsellbilldetailproductid;
$productunitId = $oneDetail->productunitid;
$quantity = $oneDetail->returnsellbilldetailquantity;
$prototal = $oneDetail->returnsellbilldetailtotalprice; //after discount
$storeId = $oneDetail->storeid;
##
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##
$buyProduct = new stdClass();
$buyProduct->productId = $productId;
$buyProduct->productBuyPrice = $oneDetail->buyprice;
$buyProduct->lastbuyprice = $oneDetail->lastbuyprice;
$buyProduct->meanbuyprice = $oneDetail->meanbuyprice;
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice_withDiscount;
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice_withDiscount;
$productData = $productDAO->load($buyProduct->productId);
$buyProduct->overAllAveragePrice = $productData->overAllAveragePrice;
if (empty($buyProduct->lastbuyprice_withDiscount) || $buyProduct->lastbuyprice_withDiscount === NULL) {
$buyProduct->lastbuyprice_withDiscount = $oneDetail->lastbuyprice;
}
if (empty($buyProduct->meanbuyprice_withDiscount) || $buyProduct->meanbuyprice_withDiscount === NULL) {
$buyProduct->meanbuyprice_withDiscount = $oneDetail->meanbuyprice;
}
if (empty($buyProduct->overAllAveragePrice) || $buyProduct->overAllAveragePrice === NULL) {
$buyProduct->overAllAveragePrice = $productData->productBuyPrice; //$oneDetail->buyprice;
}
##
$isreturn = 1;
//$isadd = 1;
quickProfitRow($buyProduct, $finalquantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId, $billDate);
$lastBillIdsQuickProfit->retsellDetail = $oneDetail->returnsellbilldetailid;
$lastBillIdsQuickProfit->retsell = $oneDetail->returnsellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
}
if ($isadd == 1) {// as if it is 0 means we are reversing affect of uncomplted bill means that quickProfitBill() was not used so dont use it now
quickProfitBill(0, $bill->returnsellbilltotalbill, $billDiscountVal, $clientId, 1, $billDate);
$lastBillIdsQuickProfit->retsellDetail = 0;
$lastBillIdsQuickProfit->retsell = $bill->returnsellbillid;
$lastBillIdsQuickProfitDAO->update($lastBillIdsQuickProfit);
}
break;
}
$mytransactions->commit();
} catch (Exception $ex) {
$mytransactions->rollback();
}
}
//quickProfitRow($buyProduct, $productChangeAmount, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId);
function quickProfitRow($myproduct, $finalQuantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId, $billDate) {
//$isreturn = 1 is return part ,0 is sell part
//$isadd = 1 is add ,0 is del
global $quickProfitStoreEX;
global $quickProfitProductEX;
global $sellCostsArray;
$prototalWithDiscount = $prototal - ($billDiscountVal * ($prototal / $billTotalBeforeDiscount));
$obj->productId = $myproduct->productId;
$obj->theDate = date("Y-m-d", strtotime($billDate)); //date('Y-m-d'); //date("Y-m-d", strtotime("+1 day"));
if ($isreturn == 0) {
$obj->sellVal = $prototalWithDiscount;
$obj->sellCostBuyPrice = $myproduct->productBuyPrice * $finalQuantity;
$obj->sellCostLastBuyPrice = $myproduct->lastbuyprice * $finalQuantity;
$obj->sellCostMeanBuyPrice = $myproduct->meanbuyprice * $finalQuantity;
$obj->sellCostLastBuyPricewithDiscount = $myproduct->lastbuyprice_withDiscount * $finalQuantity;
$obj->sellCostMeanBuyPricewithDiscount = $myproduct->meanbuyprice_withDiscount * $finalQuantity;
$obj->sellCostOverAllAveragePrice = $myproduct->overAllAveragePrice * $finalQuantity;
$obj->returnSellVal = 0;
$obj->returnSellCostBuyPrice = 0;
$obj->returnSellCostLastBuyPrice = 0;
$obj->returnSellCostMeanBuyPrice = 0;
$obj->returnSellCostLastBuyPricewithDiscount = 0;
$obj->returnSellCostMeanBuyPricewithDiscount = 0;
$obj->returnSellCostOverAllAveragePrice = 0;
$obj->netQuantity = $finalQuantity;
$obj->netQuantityRet = 0;
} elseif ($isreturn == 1) {
$obj->sellVal = 0;
$obj->sellCostBuyPrice = 0;
$obj->sellCostLastBuyPrice = 0;
$obj->sellCostMeanBuyPrice = 0;
$obj->sellCostLastBuyPricewithDiscount = 0;
$obj->sellCostMeanBuyPricewithDiscount = 0;
$obj->sellCostOverAllAveragePrice = 0;
$obj->returnSellVal = $prototalWithDiscount;
$obj->returnSellCostBuyPrice = $myproduct->productBuyPrice * $finalQuantity;
$obj->returnSellCostLastBuyPrice = $myproduct->lastbuyprice * $finalQuantity;
$obj->returnSellCostMeanBuyPrice = $myproduct->meanbuyprice * $finalQuantity;
$obj->returnSellCostLastBuyPricewithDiscount = $myproduct->lastbuyprice_withDiscount * $finalQuantity;
$obj->returnSellCostMeanBuyPricewithDiscount = $myproduct->meanbuyprice_withDiscount * $finalQuantity;
$obj->returnSellCostOverAllAveragePrice = $myproduct->overAllAveragePrice * $finalQuantity;
$obj->netQuantity = 0;
$obj->netQuantityRet = $finalQuantity;
}
$obj->storeId = $storeId;
if ($isadd == 1) {
$quickProfitProductEX->insertOrUpdatePlusEX($obj);
$quickProfitStoreEX->insertOrUpdatePlusEX($obj);
} else {
$quickProfitProductEX->insertOrUpdateMinusEX($obj);
$quickProfitStoreEX->insertOrUpdateMinusEX($obj);
}
//for totals
$sellCostsArray["sellCostBuyPrice"] += $obj->sellCostBuyPrice;
$sellCostsArray["sellCostLastBuyPrice"] += $obj->sellCostLastBuyPrice;
$sellCostsArray["sellCostMeanBuyPrice"] += $obj->sellCostMeanBuyPrice;
$sellCostsArray["sellCostLastBuyPricewithDiscount"] += $obj->sellCostLastBuyPricewithDiscount;
$sellCostsArray["sellCostMeanBuyPricewithDiscount"] += $obj->sellCostMeanBuyPricewithDiscount;
$sellCostsArray["sellCostOverAllAveragePrice"] += $obj->sellCostOverAllAveragePrice;
$sellCostsArray["returnSellCostBuyPrice"] += $obj->returnSellCostBuyPrice;
$sellCostsArray["returnSellCostLastBuyPrice"] += $obj->returnSellCostLastBuyPrice;
$sellCostsArray["returnSellCostMeanBuyPrice"] += $obj->returnSellCostMeanBuyPrice;
$sellCostsArray["returnSellCostLastBuyPricewithDiscount"] += $obj->returnSellCostLastBuyPricewithDiscount;
$sellCostsArray["returnSellCostMeanBuyPricewithDiscount"] += $obj->returnSellCostMeanBuyPricewithDiscount;
$sellCostsArray["returnSellCostOverAllAveragePrice"] += $obj->returnSellCostOverAllAveragePrice;
}
function quickProfitBill($sellbillPrice, $returnsellbillPrice, $billDiscountVal, $clientId, $isadd, $billDate) {
//$isadd = 1 is add ,0 is del
global $quickProfitDayEX;
global $quickProfitClientEX;
global $quickProfitGeneralEX;
global $sellCostsArray;
//
$obj->theDate = date("Y-m-d", strtotime($billDate)); //date('Y-m-d'); //date("Y-m-d", strtotime("+1 day"));
$obj->sellVal = abs($sellbillPrice);
$obj->returnSellVal = abs($returnsellbillPrice);
if ($obj->sellVal >= $obj->returnSellVal) {
$obj->sellVal = $obj->sellVal - $billDiscountVal;
} else {
$obj->returnSellVal = $obj->returnSellVal - $billDiscountVal;
}
$obj->sellCostBuyPrice = (float) $sellCostsArray["sellCostBuyPrice"];
$obj->sellCostLastBuyPrice = (float) $sellCostsArray["sellCostLastBuyPrice"];
$obj->sellCostMeanBuyPrice = (float) $sellCostsArray["sellCostMeanBuyPrice"];
$obj->sellCostLastBuyPricewithDiscount = (float) $sellCostsArray["sellCostLastBuyPricewithDiscount"];
$obj->sellCostMeanBuyPricewithDiscount = (float) $sellCostsArray["sellCostMeanBuyPricewithDiscount"];
$obj->sellCostOverAllAveragePrice = (float) $sellCostsArray["sellCostOverAllAveragePrice"];
$obj->returnSellCostBuyPrice = (float) $sellCostsArray["returnSellCostBuyPrice"];
$obj->returnSellCostLastBuyPrice = (float) $sellCostsArray["returnSellCostLastBuyPrice"];
$obj->returnSellCostMeanBuyPrice = (float) $sellCostsArray["returnSellCostMeanBuyPrice"];
$obj->returnSellCostLastBuyPricewithDiscount = (float) $sellCostsArray["returnSellCostLastBuyPricewithDiscount"];
$obj->returnSellCostMeanBuyPricewithDiscount = (float) $sellCostsArray["returnSellCostMeanBuyPricewithDiscount"];
$obj->returnSellCostOverAllAveragePrice = (float) $sellCostsArray["returnSellCostOverAllAveragePrice"];
$obj->clientId = $clientId;
//quickProfitGeneral
$objGeneral->netSellVal = $obj->sellVal - $obj->returnSellVal;
$objGeneral->netSellCostBuyPrice = $obj->sellCostBuyPrice - $obj->returnSellCostBuyPrice;
$objGeneral->netSellCostLastBuyPrice = $obj->sellCostLastBuyPrice - $obj->returnSellCostLastBuyPrice;
$objGeneral->netSellCostMeanBuyPrice = $obj->sellCostMeanBuyPrice - $obj->returnSellCostMeanBuyPrice;
$objGeneral->netSellCostLastBuyPricewithDiscount = $obj->sellCostLastBuyPricewithDiscount - $obj->returnSellCostLastBuyPricewithDiscount;
$objGeneral->netSellCostMeanBuyPricewithDiscount = $obj->sellCostMeanBuyPricewithDiscount - $obj->returnSellCostMeanBuyPricewithDiscount;
$objGeneral->netSellCostOverAllAveragePrice = $obj->sellCostOverAllAveragePrice - $obj->returnSellCostOverAllAveragePrice;
if ($isadd == 1) {
$quickProfitDayEX->insertOrUpdatePlusEX($obj);
$quickProfitClientEX->insertOrUpdatePlusEX($obj);
$quickProfitGeneralEX->insertOrUpdatePlusEX($objGeneral);
} else {
$quickProfitDayEX->insertOrUpdateMinusEX($obj);
$quickProfitClientEX->insertOrUpdateMinusEX($obj);
$quickProfitGeneralEX->insertOrUpdateMinusEX($objGeneral);
}
}
function loadProductUnitWithProductAndUnit($productId, $unitId) {
//to use the variable out side the funcion
global $myProductunitEx;
// select all data from productunit tbl
$productunitData = $myProductunitEx->queryWithProductIdAndUnitId($productId, $unitId);
return $productunitData;
}
?>