<?php
// تقرير لعميل معين
//تقارير العملاء (الدفعات)
//the global file operation
include("../public/impOpreation.php");
include("../library/uploadImages.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");
//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');

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

//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');

//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.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');

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

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

//Faida
require_once('../models/dao/FaidaDAO.class.php');
require_once('../models/dto/Faida.class.php');
require_once('../models/mysql/FaidaMySqlDAO.class.php');
require_once('../models/mysql/ext/FaidaMySqlExtDAO.class.php');


//Faida
require_once('../models/dao/FaidaproductDAO.class.php');
require_once('../models/dto/Faidaproduct.class.php');
require_once('../models/mysql/FaidaproductMySqlDAO.class.php');
require_once('../models/mysql/ext/FaidaproductMySqlExtDAO.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');

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

//
require_once('../models/dao/ProductingredientsDAO.class.php');
require_once('../models/dto/Productingredient.class.php');
require_once('../models/mysql/ProductingredientsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductingredientsMySqlExtDAO.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/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');
//Onlinetempstoredetail
require_once('../models/dao/OnlinetempstoredetailDAO.class.php');
require_once('../models/dto/Onlinetempstoredetail.class.php');
require_once('../models/mysql/OnlinetempstoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/OnlinetempstoredetailMySqlExtDAO.class.php');

//Onlinestoresetting
require_once('../models/dao/OnlinestoresettingDAO.class.php');
require_once('../models/dto/Onlinestoresetting.class.php');
require_once('../models/mysql/OnlinestoresettingMySqlDAO.class.php');
require_once('../models/mysql/ext/OnlinestoresettingMySqlExtDAO.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');

//Store
$myStoreRecord = new StoreMySqlDAO();

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

//Billname
$myBillnameRecord = new BillnameMySqlDAO();
$myBillnameEx = new BillnameMySqlExtDAO();

//Billsetting
$myBillsettingEx = new BillsettingsMySqlExtDAO();

//Client
$myClientRecord = new ClientMySqlDAO();
$myClient = new Client();
$myClientEx = new ClientMySqlExtDAO();

//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//Faida
$myFaida = new Faida();
$myFaidaDAO = new FaidaMySqlDAO();
$myFaidaEx = new FaidaMySqlExtDAO();

//Faida_product
$myFaidaProduct = new Faidaproduct();
$myFaidaProductDAO = new FaidaproductMySqlDAO();
$myFaidaProductEx = new FaidaproductMySqlExtDAO();

//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();

$myProductunitEx = new ProductunitMySqlExtDAO();

//Storedetail
$myStoredetailEx = $StoredetailEX = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();

//Unit
$myUnitRecord = new UnitMySqlDAO();

//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productEX = new ProductMySqlExtDAO();
$buyBillDetailExt = new BuybilldetailMySqlExtDAO();

//Productingredient
$productIngredients = new Productingredient();
$productIngredientsDAO = new ProductingredientsMySqlDAO();
$productIngredientsEX = new ProductingredientsMySqlExtDAO();

//
$quickProfitStore = new Quickprofitstore();
$quickProfitStoreDAO = new QuickprofitstoreMySqlDAO();
$quickProfitStoreEX = new QuickprofitstoreMySqlExtDAO();

//
$quickProfitProduct = new Quickprofitproduct();
$quickProfitProductDAO = new QuickprofitproductMySqlDAO();
$quickProfitProductEX = new QuickprofitproductMySqlExtDAO();

//Onlinetempstoredetail
$onlineTempStoreDetail = new Onlinetempstoredetail();
$onlineTempStoreDetailDAO = new OnlinetempstoredetailMySqlDAO();
$onlineTempStoreDetailEX = new OnlinetempstoredetailMySqlExtDAO();

////
$onlineStoreSetting = new Onlinestoresetting();
$onlineStoreSettingDAO = new OnlinestoresettingMySqlDAO();
$onlineStoreSettingEX = new OnlinestoresettingMySqlExtDAO();

//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();

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

//Save
//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 :- clientReportsController

  OPERTATION in Controller

  1- تقرير مديونية عميل
  ======================== */

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

//here goes the instances and general variables


$today = date("Y-m-d");

//check and use the condition that suite this action
//used for quick profit
$sellCostsArray = array();

if (empty($do)) {

    include_once("../public/authentication.php");

    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    $clientData = loadClient();
    $smarty->assign("clientData", $clientData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $Programsettingdata->vatValue = (float) $Programsettingdata->vatValue;
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $billnameData = $myBillnameRecord->load(3);
    $smarty->assign("billnameData", $billnameData);

    $billsettingsData = loadBillProperty(3);
    $smarty->assign("billsettingsData", $billsettingsData);

    $smarty->assign("billnameid", 3);

    //get units
    $unitsData = loadUnit();
    $smarty->assign("unitsData", $unitsData);


    $smarty->display("faidaview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");
    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
//        echo $e;
        header("location:?do=error");
    }
} elseif ($do == "show") {

    $allfaida = show();

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

    $smarty->assign("allfaida", $allfaida);
    $smarty->display("faidaview/show.html");
} elseif ($do == "details") {
    include_once("../public/authentication.php");

    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    $clientData = loadClient();
    $smarty->assign("clientData", $clientData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $Programsettingdata->vatValue = (float) $Programsettingdata->vatValue;
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $billnameData = $myBillnameRecord->load(3);
    $smarty->assign("billnameData", $billnameData);

    $billsettingsData = loadBillProperty(3);
    $smarty->assign("billsettingsData", $billsettingsData);

    $smarty->assign("billnameid", 3);

    //get units
    $unitsData = loadUnit();
    $smarty->assign("unitsData", $unitsData);

    $Faidaid = $_GET['id'];
    $allfaida = $myFaidaDAO->load($Faidaid);
    $smarty->assign("allfaida", $allfaida);

    $allFaidaProduct = $myFaidaProductEx->queryWithFaidaId($Faidaid);
    $faidatotal = 0;
    foreach ($allFaidaProduct as $item) {
        $faidatotal += $item->faidaproducttotalprice;
    }
    $smarty->assign("allFaidaProduct", $allFaidaProduct);
    $smarty->assign("faidatotal", $faidatotal);

    $smarty->display("faidaview/details.html");
} elseif ($do == "edit") {
    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    $clientData = loadClient();
    $smarty->assign("clientData", $clientData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $Programsettingdata->vatValue = (float) $Programsettingdata->vatValue;
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $billnameData = $myBillnameRecord->load(3);
    $smarty->assign("billnameData", $billnameData);

    $billsettingsData = loadBillProperty(3);
    $smarty->assign("billsettingsData", $billsettingsData);

    $smarty->assign("billnameid", 3);

    //get units
    $unitsData = loadUnit();
    $smarty->assign("myunitdata", $unitsData);

    $Faidaid = $_GET['id'];
    $allfaida = $myFaidaEx->queryWithFaidaId($Faidaid);
    $smarty->assign("allfaida", $allfaida);

    $allFaidaProduct = $myFaidaProductEx->queryWithFaidaId($Faidaid);
    $smarty->assign("allFaidaProduct", $allFaidaProduct);
    $faidatotal = 0;
    foreach ($allFaidaProduct as $item) {
        $faidatotal += $item->faidaproducttotalprice;
        $item->nametotal = $item->productName . '/' . $item->productCatName;
    }
    $smarty->assign("faidatotal", $faidatotal);
    $smarty->assign("faidaitr", count($allFaidaProduct));

    $smarty->display("faidaview/edit.html");
    $smarty->assign("customeditsellbill", 1);
} elseif ($do == "update") {
    include_once("../public/authentication.php");
    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
//        echo $e;
        header("location:?do=error");
    }
} elseif ($do == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} 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->assign("faidaScript", 1);
$smarty->assign("customValidation", 1);
$smarty->assign("customsellbill", 1);


$smarty->display("footer.html");


/* ===============================
  function in this CONTROLLER
  ================================ */

function add() {
    global $myFaida;
    global $myFaidaDAO;


    $client_id = filter_input(INPUT_POST, 'supplier');
    $premium_time = filter_input(INPUT_POST, 'premium');
    $Installment_duration = filter_input(INPUT_POST, 'Installment_duration');
    $total_amount = filter_input(INPUT_POST, 'total_amount');
    $comment = filter_input(INPUT_POST, 'textDetails');
    $storeid = filter_input(INPUT_POST, 'storeid');
    $presenter = (int) filter_input(INPUT_POST, 'presenter');


    $myFaida->clientId = $client_id;
    $myFaida->premmium = $premium_time;
    $myFaida->installment_duration = $Installment_duration;
    $myFaida->total_amount = $total_amount;
    $myFaida->comment = $comment;
    $myFaida->user_id = $_SESSION['userid'];
    $myFaida->storeId = $storeid;
    $myFaida->conditions = 0;
    $myFaida->presenter = $presenter;
    $myFaidaId = $myFaidaDAO->insert($myFaida);
    addFaida($myFaidaId);
}

function addFaida($myFaidaId) {
    global $myFaidaProduct;
    global $myFaidaProductDAO;
    global $sizeColorStoreDetailEX;


    $storeid = filter_input(INPUT_POST, 'storeid');
    $productItr = filter_input(INPUT_POST, 'hidden_itr');
    for ($i = 1; $i <= $productItr; $i++) {
        $parcode = filter_input(INPUT_POST, 'parcode' . $i);
        $comment = filter_input(INPUT_POST, 'comment' . $i);
        $productunit = filter_input(INPUT_POST, 'productunit' . $i);
        $pronum = filter_input(INPUT_POST, 'pronum' . $i);
        $scunitname = filter_input(INPUT_POST, 'scunitname' . $i);
        $bouns = filter_input(INPUT_POST, 'bouns' . $i);
        $pricetypepro = filter_input(INPUT_POST, 'pricetypepro' . $i);
        $proprice = filter_input(INPUT_POST, 'proprice' . $i);
        $discounttype = filter_input(INPUT_POST, 'discounttype' . $i);
        $product_id = filter_input(INPUT_POST, 'product' . $i);
        $faidaproducttotalprice = filter_input(INPUT_POST, 'hidden_prototal' . $i);


        $myFaidaProduct->parcode = $parcode;
        $myFaidaProduct->comment = $comment;
        $myFaidaProduct->productunit = $productunit;
        $myFaidaProduct->pronum = $pronum;
        $myFaidaProduct->scunitname = $scunitname;
        $myFaidaProduct->bouns = $bouns;
        $myFaidaProduct->pricetypepro = $pricetypepro;
        $myFaidaProduct->proprice = $proprice;
        $myFaidaProduct->discounttype = $discounttype;
        $myFaidaProduct->faidaId = $myFaidaId;
        $myFaidaProduct->productId = $product_id;
        $myFaidaProduct->conditions = 0;
        $myFaidaProduct->faidaproducttotalprice = $faidaproducttotalprice;
//        var_dump($myFaidaProduct);
        $detailId = $myFaidaProductDAO->insert($myFaidaProduct);



        $sizeId = 0;
        $colorId = 0;
        $sizeColorStoreDetailId = 0;
        if (strpos($product_id, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $product_id));
            $product_id = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($product_id, $storeid, $sizeId, $colorId);
        }
        $productunitData = loadProductUnitWithProductAndUnit($product_id, $productunit);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $pronum * $productnumber;

        //get store productquantity before change
        $storedetailData = getStoreDetails($storeid, $product_id, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

        $allStoredetailData = $storedetailData[0];
        $storedetailId = $storedetailData[1];
        $productquantityBefore = $storedetailData[2];

        //print("qty: ".$finalquantity.'<br>');
        if (count($allStoredetailData) > 0) {


            //update productquantity in storedetail
            $productquantityAfter = decreaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity, "", "", $product_id, "", "", "", "", 0, 1, $sizeColorStoreDetailId, $sizeId, $colorId);

            //insert in storereport
            insertStorereportupdate($product_id, $storeid, $finalquantity, $productquantityBefore, $productquantityAfter, 1, $myFaidaId, "اضافة فاتورة فايدة", "FaidaController.php", date("Y-m-d H:i:s"), $sizeId, $colorId);
        } else {
            $specialQuantityAfter = $finalquantity * -1;

            //insert in storedetail tbl
            insertStoredetailupdate($storeid, $product_id, $specialQuantityAfter, date("Y-m-d H:i:s"), "", '', $product_id, "", "", "", "", 0, 1, $sizeColorStoreDetailId, $sizeId, $colorId);

            //insert in storereport
            insertStorereportupdate($product_id, $storeid, $finalquantity, 0, $specialQuantityAfter, 1, $myFaidaId, "اضافة فاتورة فايدة", "FaidaController.php", date("Y-m-d H:i:s"), $sizeId, $colorId);
        }
    }
//    var_dump($myFaidaProduct);
}

function show() {

    global $myFaidaDAO;
    global $myClientEx;
    global $userDAO;
    global $myFaidaEx;

    $allfaidas = $myFaidaEx->queryAllByuserId();
    foreach ($allfaidas as $item) {
        $user = $userDAO->load($item->user_id);
        $item->username = $user->username;


        $client_id = $item->clientId;
        $clientData = $myClientEx->loadExt($client_id);
        foreach ($clientData as $single)
            $item->clientname = $single->clientname;
    }
    return $allfaidas;
}

function update() {
    global $myFaida;
    global $myFaidaDAO;
    global $myFaidaProductDAO;

    $client_id = filter_input(INPUT_POST, 'supplier');
    $premium_time = filter_input(INPUT_POST, 'premium');
    $Installment_duration = filter_input(INPUT_POST, 'Installment_duration');
    $total_amount = filter_input(INPUT_POST, 'total_amount');
    $comment = filter_input(INPUT_POST, 'textDetails');
    $storeid = filter_input(INPUT_POST, 'storeid');
    $FaidaId = filter_input(INPUT_POST, 'faida_id');
    $presenter = filter_input(INPUT_POST, 'presenter');


    $myFaida->id = $FaidaId;
    $myFaida->clientId = $client_id;
    $myFaida->premmium = $premium_time;
    $myFaida->installment_duration = $Installment_duration;
    $myFaida->total_amount = $total_amount;
    $myFaida->comment = $comment;
    $myFaida->user_id = $_SESSION['userid'];
    $myFaida->storeId = $storeid;
    $myFaida->conditions = 0;
    $myFaida->presenter = $presenter;

    $myFaidaDAO->update($myFaida);

    $myFaidaProductDAO->deleteByFaidaId($myFaida->id);
    addFaida($myFaida->id);
}

function delete() {
    global $myFaidaDAO;
    global $myFaidaProductDAO;

    $id = $_GET['id'];
    $myFaidaProductDAO->deleteByFaidaId($id);
    $myFaidaDAO->delete($id);
}

//function updateFaida($myFaidaId) {
//    global $myFaidaProduct;
//    global $myFaidaProductDAO;
//
//
//    $productItr = filter_input(INPUT_POST, 'hidden_itr');
//    for ($i = 1; $i <= $productItr; $i++) {
//        $parcode = filter_input(INPUT_POST, 'parcode' . $i);
//        $comment = filter_input(INPUT_POST, 'comment' . $i);
//        $productunit = filter_input(INPUT_POST, 'productunit' . $i);
//        $pronum = filter_input(INPUT_POST, 'pronum' . $i);
//        $scunitname = filter_input(INPUT_POST, 'scunitname' . $i);
//        $bouns = filter_input(INPUT_POST, 'bouns' . $i);
//        $pricetypepro = filter_input(INPUT_POST, 'pricetypepro' . $i);
//        $proprice = filter_input(INPUT_POST, 'proprice' . $i);
//        $discounttype = filter_input(INPUT_POST, 'discounttype' . $i);
//        $product_id = filter_input(INPUT_POST, 'product' . $i);
//        $faidaproducttotalprice = filter_input(INPUT_POST, 'hidden_prototal' . $i);
//
//        $myFaidaProduct->parcode = $parcode;
//        $myFaidaProduct->comment = $comment;
//        $myFaidaProduct->productunit = $productunit;
//        $myFaidaProduct->pronum = $pronum;
//        $myFaidaProduct->scunitname = $scunitname;
//        $myFaidaProduct->bouns = $bouns;
//        $myFaidaProduct->pricetypepro = $pricetypepro;
//        $myFaidaProduct->proprice = $proprice;
//        $myFaidaProduct->discounttype = $discounttype;
//        $myFaidaProduct->faidaId = $myFaidaId;
//        $myFaidaProduct->productId = $product_id;
//        $myFaidaProduct->conditions = 0 ;
//        $myFaidaProduct->faidaproducttotalprice = $faidaproducttotalprice ;
//        $myFaidaProductDAO->update($myFaidaProduct);
//    }
////    var_dump($myFaidaProduct);
//
//}
// select from Billname tbl
function loadBillProperty($billnameid) {
    global $myBillsettingEx;

    // select all data from Billname
    $billsettingsData = $myBillsettingEx->queryWithBillnameId($billnameid);
    return $billsettingsData;
}

function loadClient() {
    global $myClientRecord;

    $clientData = $myClientRecord->queryByCondition(0);
    return $clientData;
}

function loadUnit() {
    global $myUnitRecord;

    $unitsData = $myUnitRecord->queryByCondition(0);
    return $unitsData;
}

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;
}

//update and decrease Rawmaterialamount in storerawmaterialdetails tbl
function decreaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount, $colName, $detailId, $productId, $tableName, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $sizeColorStoreDetailId, $sizeId, $colorId) {

    //to use the variable out side the funcion
    global $myStoredetail;
    global $myStoredetailRecord;
    global $myStoredetailEx;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;

    //increase productquantity
    $productquantityAfter = $productquantityBefore - $productChangeAmount;

    $myStoredetail = $myStoredetailRecord->load($storedetailId);
    if (empty($sizeColorStoreDetailId) && $sizeId > 0 && $colorId > 0) {
        //try to copy from existing data in store 1
        $sizeColorStoreDetailId2 = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($myStoredetail->productid, 1, $sizeId, $colorId);
        $sizeColorStoreDetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId2);

        $sizeColorStoreDetail->storeid = $myStoredetail->storeid;
        $sizeColorStoreDetail->quantity = 0;
        $sizeColorStoreDetail->userid = $_SESSION['userid'];
        $sizeColorStoreDetail->sysdate = date("Y-m-d H:i:s");

        $sizeColorStoreDetailId = $sizeColorStoreDetailDAO->insert($sizeColorStoreDetail);
    }

    if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {
        $sizecolorstoredetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);

        $storeId = $sizecolorstoredetail->storeid;
        lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, $productquantityBefore, $productquantityAfter, $productChangeAmount, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $storeId);


        $sizecolorstoredetail->userid = $_SESSION['userid'];
        $sizecolorstoredetail->sysdate = date("Y-m-d H:i:s");
        $sizecolorstoredetail->quantity = $productquantityAfter;
        $sizeColorStoreDetailDAO->update($sizecolorstoredetail);
        onlineTempStoreDetailFunc($sizecolorstoredetail->storeid, $sizecolorstoredetail->productid, $sizecolorstoredetail->sizeid, $sizecolorstoredetail->colorid, (abs($productChangeAmount) * -1), 1);
        //affect product to make it have total quantity
        $storeDetailData = $myStoredetailEx->queryWithStoreAndProductandunit($productId, $sizecolorstoredetail->storeid, 0);
        if (!isset($storeDetailData) || empty($storeDetailData->storedetailid)) {
            insertStoredetail($sizecolorstoredetail->storeid, $productId, $productChangeAmount, date("Y-m-d"));
        } else {
            //$myStoredetailEx->updateQuantityPlusEqualORMinusEqual($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), $productChangeAmount, 0, 1);
            $myStoredetailEx->updateQuantityWithSumChild($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $sizecolorstoredetail->storeid, $sizecolorstoredetail->productid);
        }
    } else {
        $storeId = $myStoredetailEx->getStoreidByStoredetailid($storedetailId);
        lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, $productquantityBefore, $productquantityAfter, $productChangeAmount, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $storeId);

        //update productquantity in storedetail tbl
        $myStoredetail->storedetailid = $storedetailId;
        $myStoredetail->userid = $_SESSION['userid'];
        $myStoredetail->storedetaildate = date("Y-m-d H:i:s");
        $myStoredetail->productquantity = $productquantityAfter;

        $myStoredetailEx->updateProductquantity($myStoredetail);
        onlineTempStoreDetailFunc($storeId, $productId, 0, 0, (abs($productChangeAmount) * -1), 1);
    }


    return $productquantityAfter;
}

//add in storerawmaterialdetails tbl
function insertStoredetail($storeid, $productid, $productChangeAmount, $colName, $detailId, $productId, $tableName, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $sizeColorStoreDetailId, $sizeId, $colorId) {

    global $myStoredetail;
    global $myStoredetailRecord;
    global $myStoredetailEx;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;

    //increase productquantity
    if (empty($sizeColorStoreDetailId) && $sizeId > 0 && $colorId > 0) {
        //try to copy from existing data in store 1
        $sizeColorStoreDetailId2 = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productid, 1, $sizeId, $colorId);
        $sizeColorStoreDetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId2);

        $sizeColorStoreDetail->storeid = $storeid;
        $sizeColorStoreDetail->quantity = $productChangeAmount;
        $sizeColorStoreDetail->userid = $_SESSION['userid'];
        $sizeColorStoreDetail->sysdate = date("Y-m-d H:i:s");

        $sizeColorStoreDetailId = $sizeColorStoreDetailDAO->insert($sizeColorStoreDetail);
    }
    $row = $myStoredetailEx->queryByStoreidAndProductid($productid, $storeid);
    if (!isset($row->storedetailid) || $row->storedetailid < 1) {
        $myStoredetail->userid = $_SESSION['userid'];
        $myStoredetail->storedetaildate = date("Y-m-d H:i:s");
        $myStoredetail->productquantity = $productChangeAmount;
        $myStoredetail->storeid = $storeid;
        $myStoredetail->productid = $productid;

        $detailId = $myStoredetailRecord->insert($myStoredetail);
    } else {
        $myStoredetailEx->updateQuantityWithSumChild($row->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $storeid, $productid);
    }
    lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, 0, $productChangeAmount, $productChangeAmount, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $storeid);
}

//add in Storemovement tbl
function insertStorereport($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename, $sizeId, $colorId) {
    global $myStorereport;
    global $myStorereportRecord;

    $myStorereport->processname = $processname;
    $myStorereport->productafter = $productafter;
    $myStorereport->productbefore = $productbefore;
    $myStorereport->productquantity = $productChangeAmount;
    $myStorereport->productid = $productid;
    $myStorereport->storeid = $storeid;
    $myStorereport->storereportdate = date("Y-m-d H:i:s");
    $myStorereport->storereportmodelid = $storereportmodelid;
    $myStorereport->storereporttype = $storereporttype;
    $myStorereport->tablename = $tablename;
    $myStorereport->userid = $_SESSION['userid'];
    $myStorereport->sizeid = $sizeId;
    $myStorereport->colorid = $colorId;

    $id = $myStorereportRecord->insert($myStorereport);
    $myStorereport->storereportid = $id;
    onlineTempStoreReportFunc($myStorereport);
}

$sellbillId = 0;

function getStoreDetails($storeId, $productId, $unittype, $sizeColorStoreDetailId, $sizeId, $colorId) {
    // get productquantity from storedetail tbl
    global $StoredetailEX;

    global $sizeColorStoreDetailDAO;

    if ((isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) || ($sizeId > 0 && $colorId > 0)) {//sizecolor product
        if ((!isset($sizeColorStoreDetailId) || empty($sizeColorStoreDetailId)) && ($sizeId > 0 && $colorId > 0)) {
            $storeDetailId = 0;
            $productQuantityBefore = 0;
        } else {
            $storeDetailData = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
            $storeDetailId = $storeDetailData->id;
            $productQuantityBefore = $storeDetailData->quantity;
        }
    } else {

        //select data from storedetails by storeid and productid
        $storeDetailData = $StoredetailEX->queryWithStoreAndProductandunit($productId, $storeId, $unittype);

        $storeDetailId = $storeDetailData->storedetailid;
        $productQuantityBefore = $storeDetailData->productquantity;
    }
    return array($storeDetailData, $storeDetailId, $productQuantityBefore);
}

function ifCollectiveProductReCalculateCost($buyProduct) {
    global $productIngredients;
    global $productIngredientsEX;
    global $productDAO;
    global $noOfDecimalPlaces;

    //to edit collective pro get ingridients
    $ingridients = $productIngredientsEX->queryAllProductIngridientsEX($buyProduct->productId); //final name is product id that is collective
    if (count($ingridients) > 0) {
        //reclculate cost prices
        $buyProduct->productBuyPrice = 0;
        $buyProduct->lastbuyprice = 0;
        $buyProduct->lastbuyprice_withDiscount = 0;
        $buyProduct->meanbuyprice = 0;
        $buyProduct->meanbuyprice_withDiscount = 0;
        $buyProduct->overAllAveragePrice = 0;
        foreach ($ingridients as $val) {
            $buyProduct->productBuyPrice += $val->productBuyPrice * $val->quantity * $val->productnumber;
            $buyProduct->lastbuyprice += $val->lastbuyprice * $val->quantity * $val->productnumber;
            $buyProduct->lastbuyprice_withDiscount += $val->lastbuyprice_withDiscount * $val->quantity * $val->productnumber;
            $buyProduct->meanbuyprice += $val->meanbuyprice * $val->quantity * $val->productnumber;
            $buyProduct->meanbuyprice_withDiscount += $val->meanbuyprice_withDiscount * $val->quantity * $val->productnumber;
            $buyProduct->overAllAveragePrice += $val->overAllAveragePrice * $val->quantity * $val->productnumber;
        }
        $buyProduct->productBuyPrice = round($buyProduct->productBuyPrice, $noOfDecimalPlaces);
        $buyProduct->lastbuyprice = round($buyProduct->lastbuyprice, $noOfDecimalPlaces);
        $buyProduct->lastbuyprice_withDiscount = round($buyProduct->lastbuyprice_withDiscount, $noOfDecimalPlaces);
        $buyProduct->meanbuyprice = round($buyProduct->meanbuyprice, $noOfDecimalPlaces);
        $buyProduct->meanbuyprice_withDiscount = round($buyProduct->meanbuyprice_withDiscount, $noOfDecimalPlaces);
        $buyProduct->overAllAveragePrice = round($buyProduct->overAllAveragePrice, $noOfDecimalPlaces);
        $productDAO->update($buyProduct);
    } else {
        //make no change to prices
    }
    return $buyProduct;
}

function lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, $productquantityBefore, $productquantityAfter, $productChangeAmount, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $storeId) {

    global $productDAO;
    global $buyBillDetailExt;
    global $sellbillId; //nedded for collective product##fatma
    ##the differance
    ## use $buyProduct->lastbuyprice and $buyProduct->meanbuyprice and put them in sell table
    $buyProduct = new Product();

    ##1- get mean
    $buyProduct = $productDAO->load($productId);
    if ($buyProduct->meanbuyprice == NULL || $buyProduct->meanbuyprice == 0) {
        $buyProduct->meanbuyprice = $buyProduct->productBuyPrice;
    }
    if ($buyProduct->lastbuyprice == NULL || $buyProduct->lastbuyprice == 0) {
        $buyProduct->lastbuyprice = $buyProduct->productBuyPrice;
    }
    if ($buyProduct->lastbuyprice_withDiscount == NULL || $buyProduct->lastbuyprice_withDiscount == 0) {
        $buyProduct->lastbuyprice_withDiscount = $buyProduct->productBuyPrice;
    }
    if ($buyProduct->meanbuyprice_withDiscount == NULL || $buyProduct->meanbuyprice_withDiscount == 0) {
        $buyProduct->meanbuyprice_withDiscount = $buyProduct->productBuyPrice;
    }
    if (empty($buyProduct->overAllAveragePrice) || $buyProduct->overAllAveragePrice === NULL) {
        $buyProduct->overAllAveragePrice = $buyProduct->productBuyPrice;
    }
    $buyProduct = ifCollectiveProductReCalculateCost($buyProduct);
    if (isset($detailId) && !empty($detailId)) {
        $buyBillDetailExt->updatePrices_f($tableName, $buyProduct->lastbuyprice, $buyProduct->meanbuyprice, $buyProduct->lastbuyprice_withDiscount, $buyProduct->meanbuyprice_withDiscount, $buyProduct->lastbuyprice_withTax, $buyProduct->meanbuyprice_withTax, $colName, $detailId);
    }
    quickProfitRow($buyProduct, $productChangeAmount, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId);
    if ($isreturn == 0 && $isadd == 0) {
        //ret sell - increase
        ifCollectiveIncreaseItsIngriedientsInStore($buyProduct, $productChangeAmount, $sellbillId, $storeId, $tableName);
    } elseif ($isreturn == 0 && $isadd == 1) {
        //sell - decrease
        ifCollectiveDecreaseItsIngriedientsInStore($buyProduct, $productChangeAmount, $sellbillId, $storeId, $tableName);
    } elseif ($isreturn == 1 && $isadd == 0) {
        //sell - decrease
        ifCollectiveDecreaseItsIngriedientsInStore($buyProduct, $productChangeAmount, $sellbillId, $storeId, $tableName);
    } elseif ($isreturn == 1 && $isadd == 1) {
        //ret sell - increase
        ifCollectiveIncreaseItsIngriedientsInStore($buyProduct, $productChangeAmount, $sellbillId, $storeId, $tableName);
    }
}

function quickProfitRow($myproduct, $finalQuantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId) {
    //$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'); //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 ifCollectiveDecreaseItsIngriedientsInStore($buyProduct, $productChangeAmount, $sellbillId, $sellbillstoreId, $tableName) {
    global $productIngredients;
    global $productIngredientsEX;
    global $productDAO;
    global $myStoredetail;
    global $myStoredetailEx;
    global $myStoredetailRecord;

    //to edit collective pro get ingridients
    $ingridients = $productIngredientsEX->queryAllProductIngridientsEX($buyProduct->productId);
    foreach ($ingridients as $product) {
        $productId = $product->ingridientId;
        $ingridientChangeAmount = $productChangeAmount * $product->quantity;
        $commentCollective = '';
        $controllerLink = '';
        switch ($tableName) {
            case "sellandruternbilldetail":
                $commentCollective = "فاتورة مبيعات ومردودات بها منتج تجميعى - $buyProduct->productName";
                $controllerLink = "sellbillandruternController.php";
                break;
            case "returnsellbilldetail":
                $commentCollective = "فاتورة مردودات مبيعات بها منتج تجميعى - $buyProduct->productName";
                $controllerLink = "returnsellbillController.php";
                break;
            case "sellbilldetail":
                $commentCollective = "فاتورة مبيعات بها منتج تجميعى - $buyProduct->productName";
                $controllerLink = "sellbillController.php";
                break;
        }

        $storedetailData = getStoredetailData($sellbillstoreId, $productId, 0);

        $allStoredetailData = $storedetailData[0];
        $storedetailId = $storedetailData[1];
        $productquantityBefore = $storedetailData[2];

        if (count($allStoredetailData) > 0) {
            ################
            //update productquantity in storedetail
            //$productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity);
            //decrease productquantity NOWWWWWWW Only Dont Affect Prices Again so dont use increaseProductQuantity()
            $productquantityAfter = $productquantityBefore - $ingridientChangeAmount;
            //lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, $productquantityBefore, $productquantityAfter, $ingridientChangeAmount);
            //update productquantity in storedetail tbl
            $myStoredetail->storedetailid = $storedetailId;
            $myStoredetail->userid = $_SESSION['userid'];
            $myStoredetail->storedetaildate = date("Y-m-d H:i:s");
            $myStoredetail->productquantity = $productquantityAfter;

            $myStoredetailEx->updateProductquantity($myStoredetail);
            #########################
            //insert in storereport
            insertStorereportupdate($productId, $sellbillstoreId, $ingridientChangeAmount, $productquantityBefore, $productquantityAfter, 1, $sellbillId, $commentCollective, $controllerLink, date("Y-m-d H:i:s"));
        } else {
            ################
            //insert in storedetail tbl
            //insertStoredetailupdate($sellbillstoreId, $productId, $finalquantity, date("Y-m-d H:i:s"));
            //insertStoredetail NOWWWWWWW Only Dont Affect Prices Again so dont use insertStoredetailupdate()
            $myStoredetail->userid = $_SESSION['userid'];
            $myStoredetail->storedetaildate = date("Y-m-d H:i:s");
            $myStoredetail->productquantity = $ingridientChangeAmount;
            $myStoredetail->storeid = $sellbillstoreId;
            $myStoredetail->productid = $productId;

            $myStoredetailRecord->insert($myStoredetail);
            #########################
            //insert in storereport
            insertStorereportupdate($productId, $sellbillstoreId, $ingridientChangeAmount, 0, $ingridientChangeAmount, 1, $sellbillId, $commentCollective, $controllerLink, date("Y-m-d H:i:s"));
        }
    }
}

function onlineTempStoreDetailFunc($storeid, $productid, $sizeid, $colorid, $quantity, $edited) {
    global $onlineTempStoreDetailEX;
    global $onlineStoreSettingDAO;
    //
    $onlineStoreSetting = getOrHandleOnlineStoreSetting();
    $onlineStores = explode(',', $onlineStoreSetting->availableStores);
    //
    if (in_array($storeid, $onlineStores)) {
        $obj->storeid = $storeid;
        $obj->productid = $productid;
        $obj->sizeid = $sizeid;
        $obj->colorid = $colorid;
        $obj->quantity = $quantity;
        $obj->edited = $edited; //0 not edited "this value is therre online", 1 edited , 2 del
        $obj->sysdate = date("Y-m-d H:i:s");
        $obj->userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : 1;
        $onlineTempStoreDetailEX->insertOrUpdateOnDuplicate($obj);
    }
}

function getOrHandleOnlineStoreSetting() {
    global $onlineStoreSetting;
    global $onlineStoreSettingDAO;
    $onlineStoreSetting = $onlineStoreSettingDAO->load(1);
    if (count($onlineStoreSetting) == 0) {
        $onlineStoreSetting->url = "";
        $onlineStoreSetting->availableStores = "";
        $onlineStoreSetting->onlinestoreid = 2;
        $onlineStoreSetting->onlineclientareaid = 0;
        $onlineStoreSetting->updatetype = 0;
        $onlineStoreSetting->catprohour = 0;
        $onlineStoreSetting->catprominute = 0;
        $onlineStoreSetting->proquantityhour = 0;
        $onlineStoreSetting->proquantityminute = 0;
        $onlineStoreSetting->clientshour = 0;
        $onlineStoreSetting->clientsminute = 0;
        $onlineStoreSetting->ordershour = 0;
        $onlineStoreSetting->ordersminute = 0;
        $onlineStoreSetting->clientdebtchangeStartId = -1;
        $onlineStoreSettingDAO->insert($onlineStoreSetting);
    }
    return $onlineStoreSetting;
}

function insertStorereportupdate($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename, $mydate, $offline) {
    global $myStorereport;
    global $myStorereportRecord;

    if ($offline == 1) {
        $ex1 = 'bill_';
        $myStorereport->userid = filter_input(INPUT_POST, $ex1 . "userid");
    } else {
        $myStorereport->userid = $_SESSION['userid'];
    }



    $myStorereport->processname = $processname;
    $myStorereport->productafter = $productafter;
    $myStorereport->productbefore = $productbefore;
    $myStorereport->productquantity = $productChangeAmount;
    $myStorereport->productid = $productid;
    $myStorereport->storeid = $storeid;
    $myStorereport->storereportdate = $mydate;
    $myStorereport->storereportmodelid = $storereportmodelid;
    $myStorereport->storereporttype = $storereporttype;
    $myStorereport->tablename = $tablename;
//    $myStorereport->userid = $_SESSION['userid'];

    $myStorereportRecord->insert($myStorereport);
}
?>