<?php

//-----------start content------------//
require_once('../Connections/config.php');
require_once('../FCKeditor/fckeditor.php');
ob_start();
session_start();

ini_set('memory_limit', '128M');


if (isset($_SESSION['admin'])) {
    require('../libs/Smarty.class.php');
    $smarty = new Smarty;
    $smarty->assign("account", $_SESSION['admin']);
    include("include.php");




    $smarty->display('../templates/1/header.html');
    $admin_id = $_SESSION['user_id'];
    $name = $_SESSION['userNameLogin'];
    $smarty->assign("name", $name);
    $smarty->display('../templates/1/admin_name.html');
    $smarty->display('../templates/1/logout.html');
    $smarty->display('../templates/1/navigator.html');
    mysql_select_db($database_config, $config);
//-----------start content------------//
    if (!isset($_GET['do'])) {//show the expenses cat data
        if ($_POST['pass'] != "1597530") {
            $smarty->display('../templates/1/profitandlossCTRL/pass.html');
        }

        if ($_POST['pass'] == "1597530") {
            $date = date("y-m-d");
            $smarty->assign("date", $date);
            $smarty->display('../templates/1/profitandlossCTRL/ser.html');
        }
    } elseif ($_GET['do'] == "result") {//showing the result form
        $firstDate = $_POST['startDate'];
        $secondDate = $_POST['endDate'];

        $sqlallse1ls = mysql_query("SELECT sell_bill_tbl.ID, sell_bill_tbl.sell_billDate, sell_bill_tbl.sell_billSerial, sell_bill_tbl.sell_billOptionalSerial, sell_bill_tbl.sell_billClientId, sell_bill_tbl.sell_billTotalDeptBefor, sell_bill_tbl.sell_billTotalDeptAfter, sell_bill_tbl.sell_billDiscount, sell_bill_tbl.sell_billDiscountType, sell_bill_tbl.sell_billTotalBill, sell_bill_tbl.sell_billAfterTotalBill, sell_bill_tbl.sell_billTotalPayed, sell_bill_tbl.sell_billFinalBill, sell_bill_tbl.sell_billStoreId, sell_bill_tbl.sell_billSysDate, sell_bill_tbl.sell_billUserName, sell_bill_tbl.sell_billTypeDeleted, sell_bill_tbl.sell_billDeleteReason, sell_bill_tbl.sell_billSaveId, sell_bill_tbl.sell_billPriceType, sell_bill_tbl.sell_bilDirectPayment, sell_bill_tbl.seller FROM sell_bill_tbl
	where sell_bill_tbl.sell_billSysDate   >= CAST('$firstDate' AS DATETIME) AND 	sell_bill_tbl.sell_billSysDate <=	CAST('$secondDate' AS DATETIME)
	");
        $totalsellbill = 0;
        while ($row = mysql_fetch_array($sqlallse1ls)) {
            $totalsellbill += $row['sell_billAfterTotalBill'];
            $data[] = $row;
        }


        $sqlallbackse1ls = mysql_query("SELECT back_sell_bill_tbl.ID, back_sell_bill_tbl.back_sell_billDate, back_sell_bill_tbl.back_sell_billSerial, back_sell_bill_tbl.back_sell_billOptionalSerial, back_sell_bill_tbl.back_sell_billClientId, back_sell_bill_tbl.back_sell_billTotalDeptBefor, back_sell_bill_tbl.back_sell_billTotalDeptAfter, back_sell_bill_tbl.back_sell_billDiscount, back_sell_bill_tbl.back_sell_billDiscountType, back_sell_bill_tbl.back_sell_billTotalBill, back_sell_bill_tbl.back_sell_billAfterTotalBill, back_sell_bill_tbl.back_sell_billTotalPayed, back_sell_bill_tbl.back_sell_billFinalBill, back_sell_bill_tbl.back_sell_billStoreId, back_sell_bill_tbl.back_sell_billSysDate, back_sell_bill_tbl.back_sell_billUserName, back_sell_bill_tbl.back_sell_billTypeDeleted, back_sell_bill_tbl.back_sell_billDeleteReason, back_sell_bill_tbl.back_sell_billSaveId, back_sell_bill_tbl.back_sell_billPriceType, back_sell_bill_tbl.seller FROM back_sell_bill_tbl
		where back_sell_bill_tbl.back_sell_billSysDate   >= CAST('$firstDate' AS DATETIME) AND 	back_sell_bill_tbl.back_sell_billSysDate <=	CAST('$secondDate' AS DATETIME)
		");
        $totalbacksellbill = 0;
        while ($row = mysql_fetch_array($sqlallbackse1ls)) {
            $totalbacksellbill += $row['back_sell_billAfterTotalBill'];
            $data[] = $row;
        }

        $totalbuybill = 0;

        $sqlallbackse1ls = mysql_query("SELECT buy_bill_tbl.ID, buy_bill_tbl.buy_billDate, buy_bill_tbl.buy_billSerial, buy_bill_tbl.buy_billOptionalSerial, buy_bill_tbl.buy_billSupplierId, buy_bill_tbl.buy_billTotalDeptBefor, buy_bill_tbl.buy_billTotalDeptAfter, buy_bill_tbl.buy_billDiscount, buy_bill_tbl.buy_billDiscountType, buy_bill_tbl.buy_billTotalBill, buy_bill_tbl.buy_billAfterTotalBill, buy_bill_tbl.buy_billTotalPayed, buy_bill_tbl.buy_billFinalBill, buy_bill_tbl.buy_billStoreId, buy_bill_tbl.buy_billSysDate, buy_bill_tbl.buy_billUserName, buy_bill_tbl.buy_billTypeDeleted, buy_bill_tbl.buy_billDeleteReason, buy_bill_tbl.buy_billPriceType, buy_bill_tbl.buy_billSaveId FROM buy_bill_tbl
		where  buy_bill_tbl.buy_billSysDate   >= CAST('$firstDate' AS DATETIME) AND 	buy_bill_tbl.buy_billSysDate <= 	CAST('$secondDate' AS DATETIME)
		");

        while ($row = mysql_fetch_array($sqlallbackse1ls)) {
            $totalbuybill += $row['buy_billAfterTotalBill'];
            $data[] = $row;
        }




        $totalbuybackbill = 0;

        $sqlallbackse1ls = mysql_query("SELECT back_buy_bill_tbl.ID, back_buy_bill_tbl.back_buy_billDate, back_buy_bill_tbl.back_buy_billSerial, back_buy_bill_tbl.back_buy_billOptionalSerial, back_buy_bill_tbl.back_buy_billSupplierId, back_buy_bill_tbl.back_buy_billTotalDeptBefor, back_buy_bill_tbl.back_buy_billTotalDeptAfter, back_buy_bill_tbl.back_buy_billDiscount, back_buy_bill_tbl.back_buy_billDiscountType, back_buy_bill_tbl.back_buy_billTotalBill, back_buy_bill_tbl.back_buy_billAfterTotalBill, back_buy_bill_tbl.back_buy_billTotalPayed, back_buy_bill_tbl.back_buy_billFinalBill, back_buy_bill_tbl.back_buy_billStoreId, back_buy_bill_tbl.back_buy_billSysDate, back_buy_bill_tbl.back_buy_billUserName, back_buy_bill_tbl.back_buy_billTypeDeleted, back_buy_bill_tbl.back_buy_billDeleteReason, back_buy_bill_tbl.back_buy_billPriceType, back_buy_bill_tbl.back_buy_billSaveId FROM back_buy_bill_tbl
		where  back_buy_bill_tbl.back_buy_billSysDate   >= CAST('$firstDate' AS DATETIME) AND 	back_buy_bill_tbl.back_buy_billSysDate <= 	CAST('$secondDate' AS DATETIME)
		");

        while ($row = mysql_fetch_array($sqlallbackse1ls)) {
            $totalbuybackbill += $row['back_buy_billAfterTotalBill'];
            $data[] = $row;
        }


        $totalquantitbuyprice = 0;
        $totalquantitbuybackprice = 0;
        $sqlproduct = mysql_query("SELECT product_tbl.ID,product_tbl.buyprice FROM product_tbl");
        while ($row = mysql_fetch_array($sqlproduct)) {
            $productid = $row['ID'];
            $buyprice = $row['buyprice'];
            //	print_r($buyprice);

            $sellbillq = mysql_query("SELECT sell_bill_details_tbl.ID, sell_bill_details_tbl.sell_bill_Id, sell_bill_details_tbl.sell_billCatId, sell_bill_details_tbl.sell_billProductId, sell_bill_details_tbl.sell_billQuantity, sell_bill_details_tbl.sell_billPrice, sell_bill_details_tbl.sell_billTotalPrice, sell_bill_details_tbl.sell_billPriceType, sell_bill_details_tbl.sell_billDate, sell_bill_details_tbl.buyprice FROM sell_bill_details_tbl  where  sell_bill_details_tbl.sell_billProductId='$productid' and  sell_bill_details_tbl.sell_billDate  >= CAST('$firstDate' AS DATETIME) AND sell_bill_details_tbl.sell_billDate <= CAST('$secondDate' AS DATETIME)");
            //$beginstore = 0 ;

            while ($rows = mysql_fetch_array($sellbillq)) {
                //	print_r($rows);
//				echo"-/-/-/-/-/-/-/-/-/-/-/-";


                $totalquantitbuyprice += $rows['sell_billQuantity'] * $rows['buyprice'];
            }


            $sellbillbackq = mysql_query("SELECT back_sell_bill_details_tbl.ID, back_sell_bill_details_tbl.back_sell_bill_Id, back_sell_bill_details_tbl.back_sell_billCatId, back_sell_bill_details_tbl.back_sell_billProductId, back_sell_bill_details_tbl.back_sell_billQuantity, back_sell_bill_details_tbl.back_sell_billPrice, back_sell_bill_details_tbl.back_sell_billTotalPrice, back_sell_bill_details_tbl.back_sell_billDate, back_sell_bill_details_tbl.buyprice FROM back_sell_bill_details_tbl where   back_sell_bill_details_tbl.back_sell_billProductId='$productid' and  back_sell_bill_details_tbl.back_sell_billDate  >= CAST('$firstDate' AS DATETIME) AND back_sell_bill_details_tbl.back_sell_billDate <= CAST('$secondDate' AS DATETIME)");
            //$beginstore = 0 ;

            while ($rows = mysql_fetch_array($sellbillbackq)) {
                //	print_r($rows);
//				echo"-/-/-/-/-/-/-/-/-/-/-/-";


                $totalquantitbuybackprice += $rows['back_sell_billQuantity'] * $rows['buyprice'];
            }
        }

        //إجمالي المصروفات
        $expensesQuery = mysql_query("SELECT
				SUM(expenses_tbl.`value`) as value
				FROM expenses_tbl
				where expenses_tbl.type_deleted = 0 AND  expenses_tbl.date   >= CAST('$firstDate' AS DATETIME) AND expenses_tbl.date <= CAST('$secondDate' AS DATETIME)
				");
        while ($row = mysql_fetch_array($expensesQuery)) {
            $totalExpenses = $row['value'];
        }

        //قيمة المخزون
        $store_id = 1;  //$_GET['store_id'];
        $result = mysql_query("SELECT * FROM product_setting_tbl WHERE ID ='1'");
        while ($row = mysql_fetch_array($result)) {
            $product_setting = $row[1]; //product_settingValue
        }

        $xx = "SELECT *
	   FROM product_tbl
	   join store_details_tbl
	   ON product_tbl.id = store_details_tbl.store_detailsProductId
	   join cat_tbl  on cat_tbl.ID = product_tbl.productCatId
	   WHERE store_detailsStoreId = '$store_id'
	   AND product_tbl.productTypeDeleted = 0
	   GROUP BY store_detailsProductId";

        // print_r($xx);
        $result = mysql_query($xx);
        $totalmoney = "";
        $totalquantiy = "";
        while ($row = mysql_fetch_array($result)) {
            $totalmoney += $row['store_detailsQty'] * $row['buyprice'];
        }
        $smarty->assign("totalmoney", $totalmoney);

        $smarty->assign("totalExpenses", $totalExpenses);
        $smarty->assign("totalquantitbuybackprice", $totalquantitbuybackprice);
        $smarty->assign("totalquantitbuyprice", $totalquantitbuyprice);
        $smarty->assign("totalbuybackbill", $totalbuybackbill);
        $smarty->assign("endstore", $endstore);
        $smarty->assign("beginstore", $beginstore);
        $smarty->assign("totalbuybill", $totalbuybill);
        $smarty->assign("totalbacksellbill", $totalbacksellbill);
        $smarty->assign("totalsellbill", $totalsellbill);
        $smarty->assign("data", $data);
        $smarty->display('../templates/1/profitandlossCTRL/result.html');
    } elseif ($_GET['do'] == "sucess") {//the sucess page
        $smarty->display('../templates/1/expensesCT/sucess.html');
    } elseif ($_GET['do'] == "error") {//the error page
        $smarty->display('../templates/1/expensesCT/error.html');
    }
//---------footer--------//
    $smarty->display('../templates/1/footer.html');
//---------footer--------//
?>
    <?php

} else {
    header("location:../login.php");
}
//exit();
    ?>