File: /home/mostafedeg/public_html/erp/controllers/freebuys.php
<?php
//the global file operation
include("../public/impOpreation.php");
// get the config file
include_once("../public/config.php");
//here the db files that include in the file
include("../public/include_dao.php");
//here the db files that include in the file
include("reportfunctions.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');
//Buybilldetail
require_once('../models/dao/BuybilldetailDAO.class.php');
require_once('../models/dto/Buybilldetail.class.php');
require_once('../models/mysql/BuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybilldetailMySqlExtDAO.class.php');
//Returnbuybilldetail
require_once('../models/dao/ReturnbuybilldetailDAO.class.php');
require_once('../models/dto/Returnbuybilldetail.class.php');
require_once('../models/mysql/ReturnbuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybilldetailMySqlExtDAO.class.php');
//Buyandruternbilldetail
require_once('../models/dao/BuyandruternbilldetailDAO.class.php');
require_once('../models/dto/Buyandruternbilldetail.class.php');
require_once('../models/mysql/BuyandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbilldetailMySqlExtDAO.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');
require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');
$do = $_GET['do'];
/* ======================
Controller Name :- storedetailCTRL تقرير مخزون اول مدة
OPERTATION in Controller
1-load save data
2-display show form
======================== */
//here the global templates
$smarty->display("header.html");
//here goes the instances and general variables
//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();
//Buybilldetail
$buyBillDetail = new Buybilldetail();
$buyBillDetailDAO = new BuybilldetailMySqlDAO();
$buyBillDetailEX = new BuybilldetailMySqlExtDAO();
//Returnbuybilldetail
$returnBuyBillDetail = new Returnbuybilldetail();
$returnBuyBillDetailDAO = new ReturnbuybilldetailMySqlDAO();
$returnBuyBillDetailEX = new ReturnbuybilldetailMySqlExtDAO();
//Buyandruternbilldetail
$buyAndRuternBillDetail = new Buyandruternbilldetail();
$buyAndRuternBillDetailDAO = new BuyandruternbilldetailMySqlDAO();
$buyAndRuternBillDetailEX = new BuyandruternbilldetailMySqlExtDAO();
##################################
///////////////product///////////////
$ProductDAO = new ProductMySqlDAO();
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
$lastLevelCatIDS = array();
$catsIDS = "";
//check and use the condition that suite this action
if (empty($do)) {
//here the permission check
include_once("../public/authentication.php");
##########################################################################
//prepare search tools
##التصنيفات
$catDataReport = getCategoryChilds();
$smarty->assign("catDataReport", $catDataReport[1]);
###########################################################################
//search
$datefrom = filter_input(INPUT_POST, 'datefrom');
$dateto = filter_input(INPUT_POST, 'dateto');
$datefromold = $datefrom;
$datetoold = $dateto;
if (empty($datefromold)) {
$datefromold = date('Y-m-d');
}
if (empty($datetoold)) {
$datetoold = date('Y-m-d');
}
$smarty->assign("olddatefrom", $datefromold);
$smarty->assign("olddateto", $datetoold);
$level = filter_input(INPUT_POST, 'level');
$productCatId = filter_input(INPUT_POST, 'productCatId' . $level);
if (empty($productCatId) || $productCatId == -1) {
$productCatId = filter_input(INPUT_POST, 'productCatId' . ($level - 1));
}
$productId = filter_input(INPUT_POST, 'productId');
$searchtype = filter_input(INPUT_POST, 'searchtype');
$isOptic = filter_input(INPUT_POST, 'proIsOptic');
if (!isset($isOptic) || empty($isOptic)) {
$isOptic = 0;
}
if ($isOptic == 2 && $searchtype == 1) {
$productCatId = $productId;
}
$queryString1 = " where 1 "; //buy
$queryString1R = " where 1 "; //ret
$queryString1SR = " where 1 "; //buyandret
##date
if (isset($datefrom) && !empty($datefrom)) {
$queryString1 .= 'and date(buybill.buybilldate) >= "' . $datefrom . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) >= "' . $datefrom . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) >= "' . $datefrom . '" ';
}
if (isset($dateto) && !empty($dateto)) {
$queryString1 .= 'and date(buybill.buybilldate) <= "' . $dateto . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) <= "' . $dateto . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) <= "' . $dateto . '" ';
}
if ($searchtype == 0) {
##productId
if (isset($productId) && !empty($productId) && $productId != -1) {
if ($isOptic == 2) {
$productsOfCat = $ProductEX->queryByProductCatIdIn($productId);
$productId = '0';
foreach ($productsOfCat as $value) {
$productId .= ',' . $value->productId;
}
}
$queryString1 .= 'and buybilldetail.buybilldetailproductid in( ' . $productId . ' ) ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailproductid in( ' . $productId . ' ) ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailproductid in( ' . $productId . ' ) ';
} elseif (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
##get subcat of cat
$catsIDS = '' . $productCatId;
getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
$productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
$IDS = '0';
foreach ($productsOfCat as $value) {
$IDS .= ',' . $value->productId;
}
$queryString1 .= 'and buybilldetail.buybilldetailproductid in (' . $IDS . ') ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailproductid in (' . $IDS . ') ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailproductid in (' . $IDS . ') ';
}
getData($queryString1, $queryString1R, $queryString1SR, $searchtype, $productCatId, $theStore);
}
// $smarty->assign("salesreport", 1);
$youtubes = $youtubeLinkDAO->queryAll();
$smarty->assign("youtubes", $youtubes);
//here the smarty templates
$smarty->display("freebuysview/show.html");
//$smarty->assign("settlementstoreshow", 1);
} elseif ($do == "detail") {
//here the permission check
include_once("../public/authentication.php");
//search
$datefrom = filter_input(INPUT_GET, 'datefrom');
$dateto = filter_input(INPUT_GET, 'dateto');
$productId = filter_input(INPUT_GET, 'id');
// $isOptic = filter_input(INPUT_GET, 'proIsOptic');
// if (!isset($isOptic) || empty($isOptic)) {
// $isOptic = 0;
// }
$queryString1 = " where 1 "; //buy
$queryString1R = " where 1 "; //ret
$queryString1SR = " where 1 "; //buyandret
##date
if (isset($datefrom) && !empty($datefrom)) {
$queryString1 .= 'and date(buybill.buybilldate) >= "' . $datefrom . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) >= "' . $datefrom . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) >= "' . $datefrom . '" ';
}
if (isset($dateto) && !empty($dateto)) {
$queryString1 .= 'and date(buybill.buybilldate) <= "' . $dateto . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) <= "' . $dateto . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) <= "' . $dateto . '" ';
}
if ($searchtype == 0) {
##productId
if (isset($productId) && !empty($productId) && $productId != -1) {
if ($isOptic == 2) {
$productsOfCat = $ProductEX->queryByProductCatIdIn($productId);
$productId = '0';
foreach ($productsOfCat as $value) {
$productId .= ',' . $value->productId;
}
}
$queryString1 .= 'and buybilldetail.buybilldetailproductid in( ' . $productId . ' ) ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailproductid in( ' . $productId . ' ) ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailproductid in( ' . $productId . ' ) ';
} elseif (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
##get subcat of cat
$catsIDS = '' . $productCatId;
getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
$productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
$IDS = '0';
foreach ($productsOfCat as $value) {
$IDS .= ',' . $value->productId;
}
$queryString1 .= 'and buybilldetail.buybilldetailproductid in (' . $IDS . ') ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailproductid in (' . $IDS . ') ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailproductid in (' . $IDS . ') ';
}
getDetails($queryString1, $queryString1R, $queryString1SR, $searchtype, $productCatId, $theStore);
}
//here the smarty templates
$smarty->display("freebuysview/detail.html");
//$smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");
/* ===============================
function in this CONTROLLER
================================ */
function getData($queryString1, $queryString1R, $queryString1SR, $searchtype, $productCatId, $theStore) {
global $buyBillDetailEX;
global $returnBuyBillDetailEX;
global $buyAndRuternBillDetailEX;
global $ProductDAO;
global $productCatDAO;
global $ProductEX;
global $smarty;
global $lastLevelCatIDS;
global $catsIDS;
class productData {
public $id;
public $productName;
public $soldNo = 0;
public $soldVal = 0;
public $returnNo = 0;
public $returnVal = 0;
public $netNo = 0;
public $netVal = 0;
public $realCost = 0;
public $netProfit = 0;
public $buyPrice = 0;
public $currentQuantity = 0;
}
$existId = array();
$allDataArr = array();
$totalsObj = new productData();
$resultsCount = 0;
if ($searchtype == 0) {
##if no search make it today
if ($queryString == " where 1 ") {
##date
$today = date("Y-m-d");
$queryString1 .= 'and date(buybill.buybilldate) = "' . $today . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) = "' . $today . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) = "' . $today . '" ';
}
$queryString1 .= 'and buybilldetail.buybilldetailtotalprice =0 ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailtotalprice =0 ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailtotalprice =0 ';
$buyBillData = $buyBillDetailEX->queryAllGeneral($queryString1);
$buyBillDataReturn = $returnBuyBillDetailEX->queryAllGeneral($queryString1R);
$buyBillDatabuyAndReturn = $buyAndRuternBillDetailEX->queryAllGeneral($queryString1SR);
$resultsCount = count($buyBillData) + count($buyBillDataReturn) + count($buyBillDatabuyAndReturn);
$smarty->assign("resultsCount", $resultsCount);
if ($resultsCount > 0) {
##unique products
##فاتورة المبيعات
foreach ($buyBillData as $value) {
if (in_array($value->buybilldetailproductid, $existId)) {
$key = array_search($value->buybilldetailproductid, $existId);
$myproduct = $allDataArr[$key];
} else {
$key = -1;
$myproduct = new productData();
$myproduct->id = $value->buybilldetailproductid;
array_push($existId, $value->buybilldetailproductid);
}
$myproduct->productName = $value->productName;
#########get quantity بالقطعة
$quantity = $value->buybilldetailquantity;
$productId = $value->buybilldetailproductid;
$productunitId = $value->productunitid;
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##############
$myproduct->soldNo += $finalquantity; //العدد بالقطعة
######get discount
$dicount = 0;
$billNoOfProduct = 0;
$dicount = $value->parcode - $value->note;
if ($dicount != 0) {
$billpecies = $buyBillDetailEX->queryBillNoOfPecies($value->buybillid);
$billNoOfProduct = $billpecies->note;
}
$theDiscount = ($finalquantity * $dicount) / $billNoOfProduct;
$theDiscount -= $value->discountvalue;
$theDiscount = round($theDiscount, 2);
################
$myproduct->soldVal += $value->buybilldetailtotalprice - $theDiscount;
if ($key == -1) {
array_push($allDataArr, $myproduct);
}
}
##فاتورة مردود المبيعات
foreach ($buyBillDataReturn as $value) {
if (in_array($value->returnbuybilldetailproductid, $existId)) {
$key = array_search($value->returnbuybilldetailproductid, $existId);
$myproduct = $allDataArr[$key];
} else {
$key = -1;
$myproduct = new productData();
$myproduct->id = $value->returnbuybilldetailproductid;
array_push($existId, $value->returnbuybilldetailproductid);
}
$myproduct->productName = $value->productName;
#########get quantity بالقطعة
$quantity = $value->returnbuybilldetailquantity;
$productId = $value->returnbuybilldetailproductid;
$productunitId = $value->productunitid;
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##############
$myproduct->returnNo += $finalquantity; //العدد بالقطعة
######get discount
$dicount = 0;
$billNoOfProduct = 0;
$dicount = $value->parcode - $value->note;
if ($dicount != 0) {
$billpecies = $returnBuyBillDetailEX->queryBillNoOfPecies($value->returnbuybillid);
$billNoOfProduct = $billpecies->note;
}
$theDiscount = ($finalquantity * $dicount) / $billNoOfProduct;
$theDiscount = round($theDiscount, 2);
################
$myproduct->returnVal += $value->returnbuybilldetailtotalprice - $theDiscount;
if ($key == -1) {
array_push($allDataArr, $myproduct);
}
}
##فاتورة مبيعات و مردود
foreach ($buyBillDatabuyAndReturn as $value) {
if (in_array($value->buybilldetailproductid, $existId)) {
$key = array_search($value->buybilldetailproductid, $existId);
$myproduct = $allDataArr[$key];
} else {
$key = -1;
$myproduct = new productData();
$myproduct->id = $value->buybilldetailproductid;
array_push($existId, $value->buybilldetailproductid);
}
$myproduct->productName = $value->productName;
#########get quantity بالقطعة
$quantity = $value->buybilldetailquantity;
$productId = $value->buybilldetailproductid;
$productunitId = $value->productunitid;
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;
##############
######get discount
$dicount = 0;
$billNoOfProduct = 0;
$dicount = $value->parcode - $value->note;
if ($dicount != 0) {
$billpecies = $buyAndRuternBillDetailEX->queryBillNoOfPecies($value->buybillid);
$billNoOfProduct = $billpecies->note;
}
$theDiscount = ($finalquantity * $dicount) / $billNoOfProduct;
$theDiscount -= $value->discountvalue;
$theDiscount = round($theDiscount, 2);
################
if ($value->billtype == 0) {
$myproduct->soldNo += $finalquantity; //العدد بالقطعة
$myproduct->soldVal += $value->buybilldetailtotalprice - $theDiscount;
} else {
$myproduct->returnNo += $finalquantity; //العدد بالقطعة
$myproduct->returnVal += $value->buybilldetailtotalprice - $theDiscount;
}
if ($key == -1) {
array_push($allDataArr, $myproduct);
}
}
#############finally get net , real cost ,net profit
$myQuery = "";
foreach ($allDataArr as $data) {
$data->netNo = $data->soldNo - $data->returnNo;
$data->netVal = $data->soldVal - $data->returnVal;
##realcost
$buyDatail = $ProductDAO->load($data->id); ## i may use ->loadext() to get quantity
$data->realCost = $data->netNo * $buyDatail->productBuyPrice;
$data->netProfit = $data->netVal - $data->realCost;
if ($buyDatail->isOptic == 2) {
$catName = $ProductEX->loadProductCatNameOnly($data->id);
$data->productName = $data->productName . "/" . $catName;
}
##currentQuantity
$buyDatail = $ProductEX->getProductQuantity($data->id, $myQuery);
$data->currentQuantity = $buyDatail->sumProductQuantity;
##totals
$totalsObj->soldNo += $data->soldNo;
$totalsObj->soldVal += $data->soldVal;
$totalsObj->returnNo += $data->returnNo;
$totalsObj->returnVal += $data->returnVal;
$totalsObj->netNo += $data->netNo;
$totalsObj->netVal += $data->netVal;
$totalsObj->realCost += $data->realCost;
$totalsObj->netProfit += $data->netProfit;
$totalsObj->currentQuantity += $data->currentQuantity;
}
$smarty->assign("totalsObj", $totalsObj);
}
}
$smarty->assign("resultsCount", $resultsCount);
$smarty->assign("allDataArr", $allDataArr);
}
function getDetails($queryString1, $queryString1R, $queryString1SR, $searchtype, $productCatId, $theStore) {
global $buyBillDetailEX;
global $returnBuyBillDetailEX;
global $buyAndRuternBillDetailEX;
global $ProductDAO;
global $productCatDAO;
global $ProductEX;
global $smarty;
global $lastLevelCatIDS;
global $catsIDS;
global $myProductunitEx;
class productDetails {
public $billid = 0;
public $billURL;
public $billdate;
public $productno = 0;
public $clientname;
public $storename;
}
$allDataArr = array();
##if no search make it today
if ($queryString == " where 1 ") {
##date
$today = date("Y-m-d");
$queryString1 .= 'and date(buybill.buybilldate) = "' . $today . '" ';
$queryString1R .= 'and date(returnbuybill.returnbuybilldate) = "' . $today . '" ';
$queryString1SR .= 'and date(buyandruternbill.buybilldate) = "' . $today . '" ';
}
$queryString1 .= 'and buybilldetail.buybilldetailtotalprice =0 ';
$queryString1R .= 'and returnbuybilldetail.returnbuybilldetailtotalprice =0 ';
$queryString1SR .= 'and buyandruternbilldetail.buybilldetailtotalprice =0 ';
$buyBillData = $buyBillDetailEX->queryAllGeneralSimple($queryString1);
$buyBillDataReturn = $returnBuyBillDetailEX->queryAllGeneralSimple($queryString1R);
$buyBillDatabuyAndReturn = $buyAndRuternBillDetailEX->queryAllGeneralSimple($queryString1SR);
$resultsCount = count($buyBillData) + count($buyBillDataReturn) + count($buyBillDatabuyAndReturn);
$smarty->assign("resultsCount", $resultsCount);
if ($resultsCount > 0) {
##unique products
##فاتورة المبيعات
foreach ($buyBillData as $value) {
$myproduct = new productDetails();
$myproduct->billid = $value->buybillid;
$myproduct->billURL = "buyBillController.php?do=details&id=" . $value->buybillid;
$myproduct->billdate = $value->buybilldate;
#########get quantity بالقطعة
$quantity = $value->buybilldetailquantity;
$productunitId = $value->productunitid;
$productnumber = $myProductunitEx->getProductNumber($productunitId);
$finalquantity = $quantity * $productnumber;
##############
$myproduct->productno = $finalquantity;
$myproduct->clientname = $value->discountvalue;
$myproduct->storename = $value->note;
array_push($allDataArr, $myproduct);
}
##فاتورة مردود المبيعات
foreach ($buyBillDataReturn as $value) {
$myproduct = new productDetails();
$myproduct->billid = $value->returnbuybillid;
$myproduct->billURL = "returnBuyBillController.php?do=details&id=" . $value->returnbuybillid;
$myproduct->billdate = $value->returnbuybilldate;
#########get quantity بالقطعة
$quantity = $value->returnbuybilldetailquantity;
$productunitId = $value->productunitid;
$productnumber = $myProductunitEx->getProductNumber($productunitId);
$finalquantity = $quantity * $productnumber;
##############
$myproduct->productno = $finalquantity;
$myproduct->clientname = $value->discountvalue;
$myproduct->storename = $value->note;
array_push($allDataArr, $myproduct);
}
##فاتورة مبيعات و مردود
foreach ($buyBillDatabuyAndReturn as $value) {
$myproduct = new productDetails();
$myproduct->billid = $value->buybillid;
$myproduct->billURL = "buyAndReturnBillController.php?do=details&id=" . $value->buybillid;
$myproduct->billdate = $value->buybilldate;
#########get quantity بالقطعة
$quantity = $value->buybilldetailquantity;
$productunitId = $value->productunitid;
$productnumber = $myProductunitEx->getProductNumber($productunitId);
$finalquantity = $quantity * $productnumber;
##############
$myproduct->productno = $finalquantity;
$myproduct->clientname = $value->discountvalue;
$myproduct->storename = $value->note;
array_push($allDataArr, $myproduct);
}
}
$smarty->assign("resultsCount", $resultsCount);
$smarty->assign("allDataArr", $allDataArr);
}
function getAllSubCat($catid, $mode) {
global $productCatExt;
global $productCatDAO;
global $catsIDS;
global $lastLevelCatIDS;
//mode = 1 get all sub cats
//mode = 2 get last level cats
$result = $productCatExt->queryByParentExt($catid);
if (count($result) > 0) {
foreach ($result as $data) {
if ($mode == 1) {
$catsIDS .= "," . $data->productCatId;
getAllSubCat($data->productCatId, $mode);
} elseif ($mode == 2) {
$childData = $productCatExt->queryByParentExt($data->productCatId);
if (count($childData) > 0) {
getAllSubCat($data->productCatId, $mode);
} else {
if (!empty($data->productCatId)) {
array_push($lastLevelCatIDS, $data->productCatId);
}
}
}
}
} else {
// if ($mode == 2) {
// $childData = $productCatExt->queryByParentExt($data->productCatId);
// if (count($childData) > 0) {
// getAllSubCat($data->productCatId, $mode);
// } else {
// if (!empty($data->productCatId)) {
// array_push($lastLevelCatIDS, $data->productCatId);
// }
// }
// }
}
}
?>