<?php

class InventoryModel extends mainModel {

    public function __construct() {
        $registry = Registry::getInstance();
        $registry->Product = new Product();
        $registry->Currency = new Currency();
        $registry->Date = new Date();
        $registry->vendor = new Supplier;
        $registry->customer = new Customer;
        $registry->invoice = new Invoice;
        $registry->jv = new JV;
        $registry->purchase = new Purchase;
        $registry->organization = new Organization;
        $registry->material = new Material;
        $registry->depot = new WarehouseDepot;
        $registry->countries = new Countries;
        $registry->LoadingOrder = new LoadingOrder;
        $registry->vehicle = new Vehicle;
        $registry->expenses = new Expense;
        $registry->payment = new Payment;
        $registry->return  = new ReturnInvoice;
        $registry->Count = new Count;
        $registry->delivery = new DeliveryNote;
        $registry->invoicepos = new InvoiceposSMCCustom;
        $registry->user = new User;
        $registry->sales = new sales;
        $registry->production = new production;
        $registry->report = new Report;
        $registry->salesPurchase = new SalesPurchase;
        $registry->restaurantPos = new restaurant_pos;
        $registry->station = new Station;
        $registry->jobOrder = new JobOrder;
        $registry->subscriptions = new Subscriptions;
        $registry->shipping = new Shipping;
        $registry->common_scripts = new common_scripts;
        $registry->common_inventory = new CommonInventory;
        $registry->salesman = new salesman;
//        $registry->common_inventory = new common_inventory;
    }

    public static function getmsg($msg) {
        return CommonLanguage::getMessage($msg);
    }

    public static function DailyProductSummary($flag = null) {
        $actual = is_null($flag) ? false : true;
        $product = new Product;
        $per_days = array();
        $today = Date::getToday();

        $year = date("Y", strtotime($today)); // get the year from today
        $month = date("m", strtotime($today));
        for ($day = 01; $day < 32; $day++) {
            $daily_sold_quantity = $actual ? $product->viewSoldQuantity(null,$year,$month,$day,null,1) : $product->viewSoldQuantity(null, $year, $month, $day);
            if (!is_null($daily_sold_quantity))
                array_push($per_days, $daily_sold_quantity);
            else
                array_push($per_days, 0);
        }
        $per_months = array();
        for ($month2 = 01; $month2 < 13; $month2++) {
            $monthly_sold_quantity = $actual ? $product->viewSoldQuantity(null,$year,$month2,null,null,1) : $product->viewSoldQuantity(null, $year, $month2);
            if (!is_null($monthly_sold_quantity))
                array_push($per_months, $monthly_sold_quantity);
            else
                array_push($per_months, 0);
        }
        return [$per_days, $per_months];
    }

    public static function ProductSummaryByCat($flag = null) {
        $actual = is_null($flag) ? false : true;
        $product = new Product;
        $today = Date::getToday();

        $year = date("Y", strtotime($today)); // get the year from today
        $month = date("m", strtotime($today));
        $soldQuantityByCat = $actual ? $product->soldQuantityByCatId($year,$month,1) : $product->soldQuantityByCatId($year, $month);

        $arrName = array();
        $arrVal = array();
        for ($i = 0; $i < count($soldQuantityByCat); $i++) {
            if (!is_null($soldQuantityByCat)) {
                array_push($arrName, $soldQuantityByCat[$i]['Cat_Name']);
                array_push($arrVal, $soldQuantityByCat[$i]['Total_Quantity']);
            } else {
                array_push($arrName, 0);
                array_push($arrVal, 0);
            }
        }
        return [$arrName, $arrVal];
    }

    public static function ReturnSoldQuantity() {
        $product = new Product;
        $url = explode('/', $_GET['url']);
        $Product_ID = $url[count($url) - 1];
        $today = Date::getToday();
        $year = date("Y", strtotime($today)); // get the year from today
        $month = date("m", strtotime($today)); // get the month from today
        $ReturnSoldQuantity = $product->SoldQuantityPerDay($Product_ID, $year, $month);
        $arr = array();
        foreach ($ReturnSoldQuantity as $value) {
            if (!is_null($value))
                array_push($arr, $value);
            else
                array_push($arr, 0);
        }
        $ReturnSoldQuantityPerYear = $product->SoldQuantityPerDay($Product_ID, $year);
        $arrYear = array();
        foreach ($ReturnSoldQuantityPerYear as $value) {
            if (!is_null($value))
                array_push($arrYear, $value);
            else
                array_push($arrYear, 0);
        }

        $ReturnSoldQuantityPerCustomerYear = $product->SoldQuantityPerCustomer($Product_ID, 0, $year);
        $arrCustomerPerYear = array();
        foreach ($ReturnSoldQuantityPerCustomerYear as $value) {
            if (!is_null($value))
                array_push($arrCustomerPerYear, $value);
            else
                array_push($arrCustomerPerYear, 0);
        }
        return [$arr, $arrYear,$arrCustomerPerYear];
    }
    public static function inventoryDashboard(){
        $dashboard = new AccountingDashboard();
         $today = Date::getToday();
        $year = date("Y", strtotime($today)); // get the year from today
        $month = date("m", strtotime($today)); // get the month from today
        $PieTopSold = $dashboard->PieChartInventory($year, $month, 1);
        $arrName1 = array();
        for ($i = 0; $i < count($PieTopSold) / 2; $i++) {
            array_push($arrName1, $PieTopSold[$i]);
        }
        $arrValue1 = array();
        for ($i = count($PieTopSold) / 2; $i < count($PieTopSold); $i++) {
            array_push($arrValue1, $PieTopSold[$i]);
        }
        return [$arrName1,$arrValue1];
    }

}
