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

//Employeepersonnel
require_once('../models/dao/EmployeepersonnelDAO.class.php');
require_once('../models/dto/Employeepersonnel.class.php');
require_once('../models/mysql/EmployeepersonnelMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeepersonnelMySqlExtDAO.class.php');

//Salaryreport
require_once('../models/dao/SalaryreportDAO.class.php');
require_once('../models/dto/Salaryreport.class.php');
require_once('../models/mysql/SalaryreportMySqlDAO.class.php');
require_once('../models/mysql/ext/SalaryreportMySqlExtDAO.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');


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');
//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();
$sellBillAndRutern = new Sellbillandrutern();
//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();
//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();
$SellandruternbilldetailDAO = new SellandruternbilldetailMySqlDAO();
$sellAndRruternBillDetails = new Sellandruternbilldetail();
//Expenses
$myExpensesEx = new ExpensesMySqlExtDAO();
//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();
//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
$myProductunitRecord = new ProductunitMySqlDAO();

//Salaryreport
$Salaryreport = new Salaryreport();
$SalaryreportDAO = new SalaryreportMySqlDAO();
$SalaryreportEX = new SalaryreportMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();


//Employeepersonnel
$Employeepersonnel = new Employeepersonnel();
$EmployeepersonnelDAO = new EmployeepersonnelMySqlDAO();
$EmployeepersonnelEX = new EmployeepersonnelMySqlExtDAO();

//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();
//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();

//bill
$billsReturn = new Billsreturn();
$billsReturnDAO = new BillsreturnMySqlDAO();
$billsReturnEX = new BillsreturnMySqlExtDAO();
//billsproducts
$billsReturnProducts = new Billsreturnproduct();
$billsReturnProductsDAO = new BillsreturnproductsMySqlDAO();
$billsReturnProductsEX = new BillsreturnproductsMySqlExtDAO();

//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();

$programsettingsdata = $Programsetting = $ProgramsettingDAO->load(1);
$smarty->assign('programsettingsdata', $programsettingsdata);
$vatValue = 1 + ($programsettingsdata->vatValue / 100);

//Breadcrumb
$breadcrumbObject = new Breadcrumb();

//check and use the condetion that suite this action
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $buyPriceType = 'last'; //$_POST["buyPriceType"];
    if (empty($startDate) && empty($endDate)) {
        $startDate = $endDate = date('Y-m-d');
    }
    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
        } else {
            $endDatePrepared = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
        }

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


        getallsellbill($startDate, $endDate, $buyPriceType);
        getallsellandreturnbill($startDate, $endDate, $buyPriceType);
        getallreturnsellbill($startDate, $endDate, $buyPriceType);
    }

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    //here the smarty templates
    $smarty->display("invoicesearningsreportview/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
  ================================ */

function getallsellbill($startDate, $endDate, $buyPriceType) {

    global $mySellbillEx;
    global $mySellbilldetailEx;
    global $totalproft;
    global $smarty;
    global $billsEX;
    global $billsProductsEX;
    global $vatValue;




    $allsellbilldata = $mySellbillEx->queryAllforrateall($startDate, $endDate);

    //sellbillaftertotalbill
    foreach ($allsellbilldata as $myallsellbilldata) {

        $totalpayprice = 0;

        $Sellbilldetaildata = $mySellbilldetailEx->queryWithSellBillIdforproft($myallsellbilldata->sellbillid);


        foreach ($Sellbilldetaildata as $mySellbilldetaildata) {
            //$buyprice = $mySellbilldetaildata->buyprice;
            $productNumber = (float) $mySellbilldetaildata->productnumber;
            $quantity = $mySellbilldetaildata->sellbilldetailquantity * $productNumber;
            $buydiscount = $mySellbilldetaildata->buydiscount;
            $buydiscounttype = $mySellbilldetaildata->buydiscounttype;

            switch ($buyPriceType) {
                case "first":
                    $buyprice = (float) $mySellbilldetaildata->buyprice;
                    break;
                case "last":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice;
                    break;
                case "mean":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
                case "last_discount":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice_withDiscount;
                    break;
                case "mean_discount":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice_withDiscount;
                    break;
                default:
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
            }
            /* 	print_r('buyprice='. $buyprice .'<br>');
              print_r('quantity='. $quantity .'<br>');
              print_r('buydiscount='. $buydiscount .'<br>');
              print_r('buydiscounttype='. $buydiscounttype .'<br>');
             */
            //لا يوجد خصم
            if ($buydiscounttype == -1) {

                $totalpayprice += ($quantity * $buyprice);
            }

            //خصم نسبه
            elseif ($buydiscounttype == 1) {
                $pay = $buyprice - (($buydiscount / 100) * $buyprice);
                ;


                $totalpayprice += ($quantity * $pay);
            }
            //خصم مباشر
            else {

                $totalpayprice += ($quantity * ($buyprice - $buydiscount));
            }
        }

        //include vat in price
        $totalpayprice = $totalpayprice * $vatValue;
        $myallsellbilldata->tax = $totalpayprice;
        $totalproft += $totalpayprice;
    }

    $allsellOpticdata = $billsEX->queryAllforrateall($startDate, $endDate);
    foreach ($allsellOpticdata as $myallsellbilldata) {
        $totalpayprice = 0;
        $Sellbilldetaildata = $billsProductsEX->queryWithSellBillIdforproft($myallsellbilldata->id);
        foreach ($Sellbilldetaildata as $mySellbilldetaildata) {
            $buyprice = $mySellbilldetaildata->productBuyPrice;
            $quantity = $mySellbilldetaildata->productno;
            $buydiscount = $mySellbilldetaildata->buydiscount;
            $buydiscounttype = $mySellbilldetaildata->discounttype;
            //لا يوجد خصم
            if ($buydiscounttype == -1) {
                $totalpayprice += ($quantity * $buyprice);
            }
            //خصم نسبه
            elseif ($buydiscounttype == 1) {
                $pay = $buyprice - (($buydiscount / 100) * $buyprice);
                $totalpayprice += ($quantity * $pay);
            }
            //خصم مباشر
            else {
                $totalpayprice += ($quantity * ($buyprice - $buydiscount));
            }
        }
        $myallsellbilldata->tax = $totalpayprice;
        $totalproft += $totalpayprice;
    }

    $smarty->assign('allsellbilldata', $allsellbilldata);
    $smarty->assign('allsellOpticdata', $allsellOpticdata);
}

function getallsellandreturnbill($startDate, $endDate, $buyPriceType) {

    global $mySellbillandruternEx;
    global $mySellandruternbilldetailEx;

    global $smarty;

    global $totalproft;
    global $vatValue;




    $allsellandreturnsellbilldata = $mySellbillandruternEx->queryAllforrateall($startDate, $endDate);

    //sellbillaftertotalbill
    foreach ($allsellandreturnsellbilldata as $myallsellbilldata) {

        $totalpayprice = 0;
        $totalreturnpayprice = 0;

        $Sellbilldetaildata = $mySellandruternbilldetailEx->queryWithSellBillIdAndSellTypeforproft($myallsellbilldata->sellbillid);


        foreach ($Sellbilldetaildata as $mySellbilldetaildata) {
            $productNumber = (float) $mySellbilldetaildata->productnumber;
            //$buyprice = $mySellbilldetaildata->buyprice;
            $quantity = $mySellbilldetaildata->sellbilldetailquantity * $productNumber;
            $buydiscount = $mySellbilldetaildata->buydiscount;
            $buydiscounttype = $mySellbilldetaildata->buydiscounttype;
            $selltype = $mySellbilldetaildata->selltype;

            switch ($buyPriceType) {
                case "first":
                    $buyprice = (float) $mySellbilldetaildata->buyprice;
                    break;
                case "last":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice;
                    break;
                case "mean":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
                case "last_discount":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice_withDiscount;
                    break;
                case "mean_discount":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice_withDiscount;
                    break;
                default:
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
            }

            if ($selltype == 0) {
                //لا يوجد خصم
                if ($buydiscounttype == -1) {

                    $totalpayprice += ($quantity * $buyprice);
                }

                //خصم نسبه
                elseif ($buydiscounttype == 1) {
                    $pay = $buyprice - (($buydiscount / 100) * $buyprice);
                    ;


                    $totalpayprice += ($quantity * $pay);
                }
                //خصم مباشر
                else {

                    $totalpayprice += ($quantity * ($buyprice - $buydiscount));
                }
            } elseif ($selltype == 1) {
                //لا يوجد خصم
                if ($buydiscounttype == -1) {

                    $totalreturnpayprice += ($quantity * $buyprice);
                }

                //خصم نسبه
                elseif ($buydiscounttype == 1) {
                    $pay = $buyprice - (($buydiscount / 100) * $buyprice);
                    ;


                    $totalreturnpayprice += ($quantity * $pay);
                }
                //خصم مباشر
                else {

                    $totalreturnpayprice += ($quantity * ($buyprice - $buydiscount));
                }
            }
        }

        //include vat in price
        $totalpayprice = $totalpayprice * $vatValue;
        $totalreturnpayprice = $totalreturnpayprice * $vatValue;
        $myallsellbilldata->tax = $totalpayprice;
        $myallsellbilldata->returnsellQuantity = $totalreturnpayprice;

        $myallsellbilldata->sellQuantity = $totalpayprice - $totalreturnpayprice;


        $totalproft += $myallsellbilldata->sellQuantity;
    }

    /* print_r('<pre>');
      print_r($allreturnsellbilldata);
      print_r('<pre>'); */
    $smarty->assign('allsellandreturnsellbilldata', $allsellandreturnsellbilldata);
}

function getallreturnsellbill($startDate, $endDate, $buyPriceType) {

    global $myReturnsellbillEx;
    global $myReturnsellbilldetailEx;

    global $smarty;

    global $billsReturnEX;
    global $billsReturnProductsEX;
    global $vatValue;




    $allreturnsellbilldata = $myReturnsellbillEx->queryAllforrateall($startDate, $endDate);

    //sellbillaftertotalbill
    foreach ($allreturnsellbilldata as $myallsellbilldata) {

        $totalpayprice = 0;

        $Sellbilldetaildata = $myReturnsellbilldetailEx->queryWithReturnsellbillIdforproft($myallsellbilldata->returnsellbillid);


        foreach ($Sellbilldetaildata as $mySellbilldetaildata) {
            $productNumber = (float) $mySellbilldetaildata->productnumber;
            //$buyprice = $mySellbilldetaildata->buyprice;
            $quantity = $mySellbilldetaildata->returnsellbilldetailquantity * $productNumber;
            $buydiscount = $mySellbilldetaildata->buydiscount;
            $buydiscounttype = $mySellbilldetaildata->buydiscounttype;


            switch ($buyPriceType) {
                case "first":
                    $buyprice = (float) $mySellbilldetaildata->buyprice;
                    break;
                case "last":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice;
                    break;
                case "mean":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
                case "last_discount":
                    $buyprice = (float) $mySellbilldetaildata->lastbuyprice_withDiscount;
                    break;
                case "mean_discount":
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice_withDiscount;
                    break;
                default:
                    $buyprice = (float) $mySellbilldetaildata->meanbuyprice;
                    break;
            }
            //لا يوجد خصم
            if ($buydiscounttype == -1) {

                $totalpayprice += ($quantity * $buyprice);
            }

            //خصم نسبه
            elseif ($buydiscounttype == 1) {
                $pay = $buyprice - (($buydiscount / 100) * $buyprice);
                ;


                $totalpayprice += ($quantity * $pay);
            }
            //خصم مباشر
            else {

                $totalpayprice += ($quantity * ($buyprice - $buydiscount));
            }
        }

        //include vat in price
        $totalpayprice = $totalpayprice * $vatValue;
        $myallsellbilldata->tax = $totalpayprice;
    }
    $smarty->assign('allreturnsellbilldata', $allreturnsellbilldata);
    $allreturnOpticdata = $billsReturnEX->queryAllforrateall($startDate, $endDate);


    foreach ($allreturnOpticdata as $myallsellbilldata) {
        $totalpayprice = 0;
        //$Sellbilldetaildata = $billsReturnProductsEX->queryWithReturnsellbillIdforproft($myallsellbilldata->billid);
        $Sellbilldetaildata = $billsReturnProductsEX->queryWithReturnsellbillIdforproft($myallsellbilldata->id);

        foreach ($Sellbilldetaildata as $mySellbilldetaildata) {
            $buyprice = $mySellbilldetaildata->productBuyPrice;
            $quantity = $mySellbilldetaildata->productno;
            $buydiscount = $mySellbilldetaildata->buydiscount;
            $buydiscounttype = $mySellbilldetaildata->discounttype;
//            //لا يوجد خصم
//            if ($buydiscounttype == -1) {
//                $totalpayprice+=($quantity * $buyprice);
//            }
//            //خصم نسبه
//            elseif ($buydiscounttype == 1) {
//                $pay = $buyprice - (($buydiscount / 100) * $buyprice);
//                $totalpayprice+=($quantity * $pay);
//            }
//            //خصم مباشر
//            else {
//                $totalpayprice+=($quantity * ($buyprice - $buydiscount));
//            }

            $totalpayprice = $mySellbilldetaildata->productBuyPrice * $mySellbilldetaildata->productno;
        }
        //include vat in price
        $totalpayprice = $totalpayprice * $vatValue;
        $myallsellbilldata->tax = $totalpayprice;
    }

    $smarty->assign('allreturnOpticdata', $allreturnOpticdata);
}

?>