<?php

class PurchaseOrderAset extends MY_Controller {
    var $db;
    public function index() {
        echo "Purchase Order Aset API";
    }

    public function __construct()
    {
        parent::__construct();
    }

    ## QUERY ##
    public function getListSupplier() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $sql = "SELECT
                SupplierID,
                SupplierName
            FROM supplier 
            WHERE SupplierIsActive = 'Y'";
            $que = $this->db->query($sql, []);
            if (!$que) {
                $this->sys_error_db("[Error] get data supplier");
                exit;
            }

            $data = $que->result_array();
            $this->sys_ok($data);
        } catch (Exception $exc) {
            $this->sys_error($exc->getMessage());
        }
    }

    public function getListCabang() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $user = $this->sys_user;

            $sql = "SELECT 
                M_BranchID,
                M_BranchCode,
                M_BranchName
            FROM m_branch 
            WHERE M_BranchIsActive = 'Y' 
                AND M_BranchS_RegionalID = ?";
            $que = $this->db->query($sql, [$user['S_RegionalID']]);
            if (!$que) {
                $this->sys_error_db("[Error] failed get list cabang");
                exit;
            }
            
            $data = $que->result_array();
            $this->sys_ok($data);
        } catch (Exception $exc) {
            $this->sys_error($exc->getMessage());
        }
    }

    public function searchRequestAset() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $para = $this->sys_input;
            
            $keyword = "%";
            if ($para['search'] != '') {
                $keyword .= $para['search'] . "%";
            }

            $limit = 10;
            $offset = 0;
            if ($para['currpage'] > 0) {
                $offset = ($para['currpage'] - 1) * $limit;
            }

            $sql = "SELECT 
                PurchaseRequestID,
                PurchaseRequestNumber,
                PurchaseRequestDetailID,
                PurchaseRequestFlagID,
                PurchaseRequestFlagM_BranchCode AS BranchCode,
                PurchaseRequestItemCategoryID AS ItemCategoryID, 
                PurchaseRequestFlagQtyRest - PurchaseRequestFlagQtyProses AS UnprocessFlagQty,
                PurchaseRequestDetailQty AS RequestQty,
                PurchaseRequestDetailQty AS OriginalQty,
                M_BranchName,
                M_ItemID,
                M_ItemCode,
                M_ItemDesc,
                ItemUnitID,
                ItemUnitName,
                SupplierPricePrice as SupplierPrice
            FROM purchase_request
            JOIN purchase_request_detail ON PurchaseRequestDetailPurchaseRequestID = PurchaseRequestID
                AND PurchaseRequestDetailIsActive = 'Y'
                AND PurchaseRequestM_BranchCode = ?
                AND PurchaseRequestItemCategoryID = '3' -- id category item asset
                AND PurchaseRequestNumber LIKE ?
            JOIN purchase_request_flag ON PurchaseRequestFlagPurchaseRequestDetailID = PurchaseRequestDetailID
                AND PurchaseRequestFlagStatus = 'PO'
                AND PurchaseRequestFlagIsActive = 'Y'
            JOIN m_item ON M_ItemID = PurchaseRequestDetailM_ItemID
            JOIN itemunit ON ItemUnitID = PurchaseRequestDetailItemUnitID
            JOIN supplier_price ON SupplierPriceSupplierID = ? 
                AND SupplierPriceM_ItemID = M_ItemID
                AND SupplierPriceItemUnitID = ItemUnitID
                AND SupplierPriceIsActive = 'Y'
            JOIN m_branch ON M_BranchCode = PurchaseRequestFlagM_BranchCode
                AND M_BranchIsActive = 'Y'
            WHERE NOT EXISTS (
                SELECT 1
                FROM purchase_order_detail 
                JOIN purchase_order ON PurchaseOrderDetailPurchaseOrderID = PurchaseOrderID
                    AND PurchaseOrderStatus = 'Approved'
                    AND PurchaseOrderIsActive = 'Y'
                    AND PurchaseOrderDetailIsActive = 'Y'
                WHERE PurchaseOrderDetailPurchaseRequestDetailID = PurchaseRequestDetailID
            )";
            
            $sql_data = $sql . " LIMIT ? OFFSET ? ";
            $que_data = $this->db->query($sql_data, [
                $para['branchcode'], $keyword, $para['supplierID'],
                $limit, $offset
            ]);
            if (!$que_data) {
                $this->sys_error_db("[Error] get daftar request data");
                exit;
            }
            $data = $que_data->result_array();

            $sql_total = "SELECT COUNT(*) AS total FROM ($sql) AS x";
            $que_total = $this->db->query($sql_total, [
                $para['branchcode'], $keyword, $para['supplierID']
            ]);
            if (!$que_total) {
                $this->sys_error_db("[Error] get total request aset");
                exit;
            }
            $total = $que_total->row_array()['total'];

            $out = [
                "records" => $data,
                "total" => $total
            ];

            $this->sys_ok($out);
        } catch (Exception $exc) {
            $this->sys_error($exc->getMessage());
        }
    }

    public function getUserApproveLevel() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $user = $this->sys_user;
            $sql = "SELECT M_UserM_ApproveLevelID FROM m_user 
            WHERE M_UserIsActive = 'Y' AND M_UserID = ? ";
            $que = $this->db->query($sql, [$user['M_UserID']]);
            if (!$que) {
                $this->sys_error_db("[Error] failed get approval level user");
                exit;
            }
            $data = $que->row_array();
            $this->sys_ok($data);
        } catch (Exception $exc) {
            $this->sys_error($exc->getMessage());
        }
    }
}