<?php

class PurchaseRequestDirectAdjusment extends MY_Controller {
    var $db;
    public function index() {
        echo "Purchase Request Direct Adjustment";
    }

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

    function search() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $param = $this->sys_input;

            $keyword = '%';
            if ($param['search'] != '') {
                $keyword = $param['search'] . '%';
            }

            $sql = "SELECT pv.* ,
                M_BranchID,
                M_BranchCode,
                M_BranchName,
                CONCAT(cs.coaAccountNo, ' | ', cs.coaDescription) as CoaSource,
                CONCAT(ct.coaAccountNo, ' | ', ct.coaDescription) as CoaTemporary,
                cs.coaDescription coaDescriptionSource,
                ct.coaDescription coaDescriptionTemporary,
                cs.coaAccountNo coaAccountNoSource,
                ct.coaAccountNo coaAccountNoTemporary
            FROM payment_voucher as pv 
            LEFT JOIN m_branch ON M_BranchCode = PaymentVoucherM_BranchCode 
            JOIN coa cs ON cs.coaID = PaymentVoucherCoaSourceID 
            JOIN coa ct ON ct.coaID = PaymentVoucherCoaTemporaryID 
            WHERE PaymentVoucherIsActive = 'Y' 
                AND DATE(PaymentVoucherDate) BETWEEN DATE(?) AND DATE(?)
                AND PaymentVoucherNumber LIKE ?";
            $que = $this->db->query($sql, [$param['startdate'], $param['enddate'], $keyword]);
            if (!$que) {
                $this->sys_error_db("[Error] get data voucher");
                exit;
            }
            $data = $que->result_array();
            $result = array(
                'records' => $data
            );

            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function searchDetail() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $param = $this->sys_input;

            $sql = "SELECT payment_voucher_detail.*, 
                ROW_NUMBER() OVER(ORDER BY PaymentVoucherDetailID) RowNumber,
                PurchaseRequestDirectID,
                PurchaseRequestDirectNumber,
                PurchaseRequestDirectTotalRealitation,
                PurchaseRequestDirectAdjustment,
                PurchaseRequestDirectAdjustmentAccount,
                PurchaseRequestDirectAccountFilled
            FROM payment_voucher_detail 
            LEFT JOIN purchase_request_direct ON PurchaseRequestDirectID = PaymentVoucherDetailPurchaseRequestDirectID
            WHERE PaymentVoucherDetailIsActive = 'Y' 
                AND PaymentVoucherDetailPaymentVoucherID = ?";
            $que = $this->db->query($sql, [$param['ID']]);
            if (!$que) {
                $this->sys_error_db("[Error] get data voucher");
                exit;
            }


            $result = array(
                'records' => $que->result_array()
            );

            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function updateAdjustment() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $param = $this->sys_input;
            $user = $this->sys_user;

            $this->db->trans_begin();
            $sql = "UPDATE purchase_request_direct SET 
                PurchaseRequestDirectAdjustment = ?,
                PurchaseRequestDirectAdjustmentAccount = ?,
                PurchaseRequestLastUpdatedUserID = ?,
                PurchaseRequestLastUpdated = NOW()
            WHERE PurchaseRequestDirectID = ?";
            $que = $this->db->query($sql, [
                $param['price_adj'], $param['account_adj'],
                $user['M_UserID'], $param['prd_id']
            ]);
            if (!$que) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] get data voucher");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok('success');
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function approveRealisasi() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $param = $this->sys_input;

            $this->db->trans_begin();
            $sql = "UPDATE payment_voucher SET 
                PaymentVoucherRealitationApproved = 'Y'
            WHERE PaymentVoucherID = ?";
            $que = $this->db->query($sql, [$param['ID']]);
            if (!$que) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] aprrove realisasi");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok("Success approve realisasi");
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    public function getListAccount() {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $sql = "SELECT *
            FROM coa 
            WHERE coaIsActive = 'Y'
                AND coaIsInput = 'Y'
                AND coaAccountNo LIKE '111%'
            ORDER BY coaAccountNo ASC";
            $que = $this->db->query($sql, []);
            if (!$que) {
                $this->sys_error_db("[Error] get listing of accounts");
                exit;
            }
            $data = $que->result_array();

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