<?php

class Tasklist extends MY_Controller
{
    var $db;
    public function index()
    {
        echo "API LISTING TASK";
        // $cek = $this->db->query("select database() as current_db")->result();
        // print_r($cek);
    }
    public function __construct()
    {
        parent::__construct();
    }

    public function search()
    {
        if (! $this->isLogin) {
            $this->sys_error("Invalid Token");
            exit;
        }
        $prm = $this->sys_input;

        $user = $this->sys_user;
        $regionalID = $user['S_RegionalID'];
        $branchCode = $user['M_BranchCode'];
        $userID = $user['M_UserID'];
        $loginType = $user['M_UserLocationFlag'];
        $sqlBranch = '';
        if ($loginType == 'B' || $loginType == 'RB') {
            $sqlBranch = "AND PurchaseRequestM_BranchCode = '{$branchCode}'";
        }

        $params = [$regionalID, $regionalID, $regionalID];
        $sqlSelect = "SELECT COUNT(*) as total,
                    GROUP_CONCAT(PaymentVoucherNumber SEPARATOR ', ') as number,
                    MIN(DATE(PaymentVoucherDate)) as date,
                    PaymentVoucherStatus as status,
                    'PAID PR' as type,
                    'p' as flag
                FROM payment_voucher
                WHERE PaymentVoucherIsActive = 'Y'
                AND PaymentVoucherStatus = 'Draft'

                UNION

                SELECT count(*) as total,
                    GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as number,
                    MIN(DATE(SupplierInvoiceDraftPaymentDate)) as date,
                    SupplierPaymentIsConfirm as status,
                    'PAYMENT CASHIER' as type,
                    'pc' as flag
                FROM supplier_invoice
                JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber
                LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y'
                LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID
                JOIN purchase_order ON PurchaseOrderID = SupplierInvoicePurchaseOrderID
                WHERE SupplierInvoiceIsActive = 'Y'
                AND SupplierPaymentIsApproved = 'Y'
                AND SupplierPaymentIsVerif = 'Y'
                AND SupplierPaymentIsConfirm = 'N'
                ";

        $qry_end = $this->db->query($sqlSelect, []);
        if ($qry_end) {
            $rows = $qry_end->result_array();
        } else {
            $this->sys_error_db("Error searching");
            exit;
        }

        // echo $this->db->last_query();
        // exit;

        $result = array(
            'records' => $rows,
            // "qry" => $this->db->last_query()
        );

        $this->sys_ok($result);
    }
}
