<?php

require APPPATH . 'libraries/RestController.php';

use App\RestServer\RestController;

class StoreFrontAPIController extends RestController
{
    public function __construct()
    {
            parent::__construct();

            // Set timezone to Philippines
            date_default_timezone_set('Asia/Manila');
    }

    public function baseImgURL()
    {
        return "https://itemimg.karatworld.net/";
    }

    public function index_get() {
        // Fetch data logic here
        $data = ['message' => 'GET request received'];
        $this->response($data, 200);
    }

    public function itemModel_get($modelno) {
        $this->load->model("Items");
        $this->load->model("BranchItemLedger");

        // testing mode
        //$row = $this->Items->getItemByModelNo('DB-422');
        
        // disabled during testing
        $row = $this->Items->getItemByModelNo($modelno);
        
        if($row != null)
        {
            $branches = [];

            $resultBranches = $this->BranchItemLedger->getDataByItemcodeWithBranchExcempt($row->itemcode);

            foreach($resultBranches->result() as $rowBranches )
            {
                $branches[] = [
                    'Itemcode' => $rowBranches->itemcode,
                    'Branch' => $rowBranches->brCode,
                    'QTY' => $rowBranches->endingqty
                ];
            }

            $item = [
                'Itemcode' => $row->itemcode,
                'Model No' => $row->modelno,
                'Series' => $row->seriescode,
                'Description' => $row->item_desc,
                'Karat' => $row->karat,
                'Size' => $row->size,
                'Grams' => $row->grams,
                'cts' => $row->cts,
                'SRP' => number_format($row->srp, 2),
                //'mrp' => $row->mrp,
                //'MRP' => '0.00', ---------- requested to remove MRP
                'pic' => $this->baseImgURL().basename(str_replace('\\', '/', $row->pic)),
                'Category' => $row->catCode,
                'Gold ID' => $row->goldID,
                'Supplier' => $row->supCode,
                'modifieddate' => $row->modifieddate,
                'others' => '',
                'remarks' => '',
                'branches' => $branches
            ];

            $data = ['status' => 'FOUND', 
                     'data' => $item];
        }
        else
            $data = ['status' => 'NOTFOUND', 
                     'data' => null];
        
        $this->response($data, 200);
    }

    public function itemCode_get($itemcode) {
        $this->load->model("Items");
        $this->load->model("BranchItemLedger");

        //$row = $this->Items->getItemByItemcode(1093218);
        
        // disabled during testing
        $row = $this->Items->getItemByItemcode($itemcode);
        
        if($row != null)
        {
            $branches = [];

            $resultBranches = $this->BranchItemLedger->getDataByItemcodeWithBranchExcempt($row->itemcode);

            foreach($resultBranches->result() as $rowBranches )
            {
                $branches[] = [
                    'Itemcode' => $rowBranches->itemcode,
                    'Branch' => $rowBranches->brCode,
                    'QTY' => $rowBranches->endingqty
                ];
            }

            $item = [
                'Itemcode' => $row->itemcode,
                'Model No' => $row->modelno,
                'Series' => $row->seriescode,
                'Description' => $row->item_desc,
                'Karat' => $row->karat,
                'Size' => $row->size,
                'Grams' => $row->grams,
                'cts' => $row->cts,
                'SRP' => number_format($row->srp, 2),
                //'mrp' => $row->mrp,
                //'MRP' => '0.00', ---------- requested to remove MRP
                'pic' => $this->baseImgURL().basename(str_replace('\\', '/', $row->pic)),
                'Category' => $row->catCode,
                'Gold ID' => $row->goldID,
                'Supplier' => $row->supCode,
                'modifieddate' => $row->modifieddate,
                'others' => '',
                'remarks' => '',
                'branches' => $branches
            ];

            $data = ['status' => 'FOUND', 
                     'data' => $item];
        }
        else
            $data = ['status' => 'NOTFOUND', 
                     'data' => null];
        
        $this->response($data, 200);
    }

    public function branchesActive_get() 
    {
        $this->load->model("Branch");

        $data = [];
        $result = $this->Branch->getbrCodeAndDescWithBranchExcempt();

        foreach($result->result() as $row)
            $data[] = ['Branch Code' => $row->brCode, 'Discription' => $row->brCode.' ['.$row->brDesc.']'];

        $this->response($data, 200);
    }

    public function auditLedgerByBranch_get($brCode)
    {
        $this->load->model("BranchItemLedger");

        $result = $this->BranchItemLedger->getDataViewByBrCodeNoZero($brCode);

        $data = [];
        
        if($result != null)
        {
            foreach($result->result() as $row)
            {
                $item[] = [
                    'Itemcode' => $row->itemcode,
                    'Model No' => $row->modelno,
                    'Series' => $row->seriescode,
                    'Description' => $row->item_desc,
                    'Karat' => $row->karat,
                    'Size' => $row->size,
                    'Grams' => $row->grams,
                    'cts' => $row->cts,
                    'SRP' => number_format($row->srp, 2),
                    'pic' => $this->baseImgURL().basename(str_replace('\\', '/', $row->pic)),
                    'Category' => $row->catCode,
                    'Gold ID' => $row->goldID,
                    'Supplier' => $row->supCode,
                    'Modified Date' => $row->modifieddate,
                    'Others' => '',
                    'Remarks' => '',
                    'Quantity' => $row->endingqty
                ];
            }

            $data = ['status' => 'FOUND', 
                     'data' => $item];
        }
        else
            $data = ['status' => 'NOTFOUND', 
                     'data' => null];
        
        $this->response($data, 200);
    }

    public function itemLedgerByBranch_get($brCode)
    {
        $this->load->model("ItemLedger");

        $result = $this->ItemLedger->getItemLedgerByBrCodeNoZero($brCode);

        $data = [];
        
        if($result != null)
        {
            foreach($result->result() as $row)
            {
                $item[] = [
                    'Itemcode' => $row->itemcode,
                    'Model No' => $row->modelno,
                    'Series' => $row->seriescode,
                    'Description' => $row->item_desc,
                    'Karat' => $row->karat,
                    'Size' => $row->size,
                    'Grams' => $row->grams,
                    'cts' => $row->cts,
                    'SRP' => number_format($row->srp, 2),
                    'pic' => $this->baseImgURL().basename(str_replace('\\', '/', $row->pic)),
                    'Category' => $row->catCode,
                    'Gold ID' => $row->goldID,
                    'Supplier' => $row->supCode,
                    'Modified Date' => $row->modifieddate,
                    'Others' => '',
                    'Remarks' => '',
                    'Quantity' => $row->endingqty
                ];
            }

            $data = ['status' => 'FOUND', 
                     'data' => $item];
        }
        else
            $data = ['status' => 'NOTFOUND', 
                     'data' => null];
        
        $this->response($data, 200);
    }
}