<?php

//the global file operation
//include("../public/impOpreation.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
// GOES HERE ....................
include("../public/include_dao.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');

//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');


//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');

//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');


//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');

//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');

//Expenses
require_once('../models/dao/ExpensesDAO.class.php');
require_once('../models/dto/Expense.class.php');
require_once('../models/mysql/ExpensesMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensesMySqlExtDAO.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');

//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');

//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');



//Breadcrumb
require_once("../library/breadcrumb.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");

/* ======================

  Controller Name :- profitreportCTRL تقرير يومية الخزينة

  OPERTATION in Controller

  1-load save data
  2-display show form
  3-display details form for save
  4-display details for operation in savedaily

  ======================== */


//here the global templates
$smarty->display("header.html");


//here goes the instances and general variables
//Sellbill
$mySellbillEx = new SellbillMySqlExtDAO();

//Returnsellbill
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();

//Sellbillandrutern
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();

//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();

//Expenses
$myExpensesEx = new ExpensesMySqlExtDAO();

//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();


//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
$myProductunitRecord = new ProductunitMySqlDAO();


//Breadcrumb
$breadcrumbObject = new Breadcrumb();



//check and use the condetion that suite this action

if ($do == "show" || empty($do)) {

    //here the permssion check
    //include_once("../public/authentication.php");


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];


    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {


        $message = "تقرير ارباح المده من  :" . $startDate . " الى تاريخ :" . $endDate;
        $smarty->assign('message', $message);


        $totalSellPrice = getTotalSellbillByDate($startDate, $endDate) + getTotalAditionalSellbillByDate($startDate, $endDate);

        $totalReturnSellPrice = getTotalReturnSellbillByDate($startDate, $endDate) + getTotalAditionalReturnSellbillByDate($startDate, $endDate);

        $totalQuantityBuyPrice = getTotalQuantityBuyPriceByDate($startDate, $endDate) + getTotalAditionalQuantityBuyPriceByDate($startDate, $endDate);

        $totalQuantityReturnBuyPrice = getTotalQuantityReturnBuyPriceByDate($startDate, $endDate) + getTotalAditionalQuantityReturnBuyPriceByDate($startDate, $endDate);

        $totalExpenses = getTotalExpensesByDate($startDate, $endDate);


        $totalSellCost = $totalSellPrice - $totalReturnSellPrice;
        $totalBuyCost = $totalQuantityBuyPrice - $totalQuantityReturnBuyPrice;

        $profitFinal = ($totalSellCost - $totalBuyCost) - $totalExpenses;


        $smarty->assign('totalSellPrice', $totalSellPrice);
        $smarty->assign('totalReturnSellPrice', $totalReturnSellPrice);
        $smarty->assign('totalQuantityBuyPrice', $totalQuantityBuyPrice);
        $smarty->assign('totalQuantityReturnBuyPrice', $totalQuantityReturnBuyPrice);
        $smarty->assign('totalExpenses', $totalExpenses);
        $smarty->assign('totalSellCost', $totalSellCost);
        $smarty->assign('totalBuyCost', $totalBuyCost);
        $smarty->assign('profitFinal', $profitFinal);
    } else {

        $smarty->assign('totalSellPrice', 0);
        $smarty->assign('totalReturnSellPrice', 0);
        $smarty->assign('totalQuantityBuyPrice', 0);
        $smarty->assign('totalQuantityReturnBuyPrice', 0);
        $smarty->assign('totalExpenses', 0);
        $smarty->assign('totalSellCost', 0);
        $smarty->assign('totalBuyCost', 0);
        $smarty->assign('profitFinal', 0);
    }



    //breadcrumb
    $breadcrumbObject->add('الادارة><a href="profitreportController.php">التقارير</a> >تفاصيل تقرير ارباح مدة', 'profitreportController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);



    //here the smarty templates
    $smarty->display("profitdetailview/show.html");
} 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
  ================================ */

// get sellbillaftertotalbill from Sellbill tbl
function getTotalSellbillByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellbillEx;
    global $smarty;

    $totalSellbill = 0;

    $sellbillData = $mySellbillEx->queryWithDateAndConditions($startDate, $endDate);


    foreach ($sellbillData as $sellbill) {
        $totalSellbill = $totalSellbill + $sellbill->sellbillaftertotalbill;
    }


    $smarty->assign('sellbillData', $sellbillData);


    return $totalSellbill;
}

// get sellbillaftertotalbill from  sellbillandrutern tbl
function getTotalAditionalSellbillByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellbillandruternEx;
    global $smarty;

    $totalSellbill = 0;

    $sellbillandruternData = $mySellbillandruternEx->queryWithDateAndConditions($startDate, $endDate);



    foreach ($sellbillandruternData as $sellbillandrutern) {
        if ($sellbillandrutern->sellbilltotalbill >= 0) {

            $totalSellbill = $totalSellbill + $sellbillandrutern->sellbillaftertotalbill;
        }
    }



    $smarty->assign('aditionalsellbillData', $sellbillandruternData);


    return $totalSellbill;
}

// get returnsellbillaftertotalbill from  returnsellbill tbl
function getTotalReturnSellbillByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $myReturnsellbillEx;
    global $smarty;

    $totalReturnSellbill = 0;

    $returnsellbillData = $myReturnsellbillEx->queryWithDateAndConditions($startDate, $endDate);

    foreach ($returnsellbillData as $returnsellbill) {
        $totalReturnSellbill = $totalReturnSellbill + $returnsellbill->returnsellbillaftertotalbill;
    }




    $smarty->assign('returnsellbillData', $returnsellbillData);


    return $totalReturnSellbill;
}

// get returnsellbillaftertotalbill from  sellbillandreturn tbl
function getTotalAditionalReturnSellbillByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellbillandruternEx;
    global $smarty;

    $totalReturnSellbill = 0;

    $sellbillandruternData = $mySellbillandruternEx->queryWithDateAndConditions($startDate, $endDate);

    foreach ($sellbillandruternData as $sellbillandrutern) {
        if ($sellbillandrutern->sellbilltotalbill < 0) {
            $totalReturnSellbill = $totalReturnSellbill + $sellbillandrutern->sellbillaftertotalbill;
        }
    }



    $smarty->assign('aditionalReturnSellbillData', $sellbillandruternData);


    return $totalReturnSellbill;
}

// show the form by date
function getTotalQuantityBuyPriceByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $smarty;

    $totalQuantityBuyPrice = 0;

    $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditions($startDate, $endDate);

    $h = 1;
    foreach ($sellbilldetailData as $sellbilldetail) {
        $productId = $sellbilldetail->sellbilldetailproductid;
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $productunitId = $sellbilldetail->productunitid;
        $buyPrice = $sellbilldetail->buyprice;
        $buyDiscount = $sellbilldetail->buydiscount;
        $buyDiscountType = $sellbilldetail->buydiscounttype;


        $productData = getProoductPriceByonlyProductId($productId);

        $productnumberData = $myProductunitRecord->load($productunitId);
        $productnumber = $productnumberData->productnumber;

        $productBuyPrice = $buyPrice;

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber;

        $smarty->assign('productBuyPrice' . $h . '', $productBuyPrice);

        $totalQuantityBuyPrice = $totalQuantityBuyPrice + ($quantity * $productBuyPrice);

        $h++;
    }


    $smarty->assign('sellbilldetailData', $sellbilldetailData);


    return $totalQuantityBuyPrice;
}

// show the form by date
function getTotalAditionalQuantityBuyPriceByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;
    global $myProductunitRecord;
    global $smarty;

    $totalQuantityBuyPrice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsType($startDate, $endDate, 0);


    $j = 1;
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $productId = $sellandreturndetail->sellbilldetailproductid;
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;


        $productData = getProoductPriceByonlyProductId($productId);

        $productnumberData = $myProductunitRecord->load($productunitId);
        $productnumber = $productnumberData->productnumber;

        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;


        $smarty->assign('aditionalproductBuyPrice' . $j . '', $productBuyPrice);

        $totalQuantityBuyPrice = $totalQuantityBuyPrice + ($quantity * $productBuyPrice);

        $j++;
    }



    $smarty->assign('aditionalsellbilldetailData', $sellandreturndetailData);

    return $totalQuantityBuyPrice;
}

function getTotalQuantityReturnBuyPriceByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $myReturnsellbilldetailEx;
    global $myProductunitRecord;
    global $smarty;

    $totalQuantityReturnBuyPrice = 0;

    $returnsellbilldetailData = $myReturnsellbilldetailEx->queryWithDateAndConditions($startDate, $endDate);


    $k = 1;
    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $productId = $returnsellbilldetail->returnsellbilldetailproductid;
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $productunitId = $returnsellbilldetail->productunitid;
        $buyPrice = $returnsellbilldetail->buyprice;
        $buyDiscount = $returnsellbilldetail->buydiscount;
        $buyDiscountType = $returnsellbilldetail->buydiscounttype;


        $productData = getProoductPriceByonlyProductId($productId);

        $productnumberData = $myProductunitRecord->load($productunitId);
        $productnumber = $productnumberData->productnumber;


        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;

        $smarty->assign('returnproductBuyPrice' . $k . '', $productBuyPrice);

        $totalQuantityReturnBuyPrice = $totalQuantityReturnBuyPrice + ($quantity * $productBuyPrice);

        $k++;
    }




    $smarty->assign('returnsellbilldetailData', $returnsellbilldetailData);


    return $totalQuantityReturnBuyPrice;
}

function getTotalAditionalQuantityReturnBuyPriceByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;
    global $myProductunitRecord;
    global $smarty;

    $totalQuantityReturnBuyPrice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsType($startDate, $endDate, 1);

    $l = 1;
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $productId = $sellandreturndetail->sellbilldetailproductid;
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;


        $productData = getProoductPriceByonlyProductId($productId);

        $productnumberData = $myProductunitRecord->load($productunitId);
        $productnumber = $productnumberData->productnumber;

        $productBuyPrice = $buyPrice;

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;


        $smarty->assign('aditionalreturnproductBuyPrice' . $l . '', $productBuyPrice);

        $totalQuantityReturnBuyPrice = $totalQuantityReturnBuyPrice + ($quantity * $productBuyPrice);

        $l++;
    }



    $smarty->assign('aditionalreturnsellbilldetailData', $sellandreturndetailData);


    return $totalQuantityReturnBuyPrice;
}

// get ProoductPrice from Prooduct tbl
function getProoductPriceByonlyProductId($productId) {
    //to use the variable out side the funcion
    global $myProductEx;

    // get rawmaterialprice by rawmaterialid
    $productData = $myProductEx->loadByProductId($productId);

    return $productData;
}

function getTotalExpensesByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $myExpensesEx;

    $expensesValue = 0;

    $expensesData = $myExpensesEx->queryWithDateAndConditions($startDate, $endDate);

    foreach ($expensesData as $expenses) {
        $expensesValue = $expensesValue + $expenses->expensesValue;
    }



    return $expensesValue;
}

?>