<?php

use app\libraries\CommonHelper;

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * @property Receipt_approval_model $Receipt_approval_model
 * @property Menu_badge_notification_model $Menu_badge_notification_model
 */
class Receipt_approval extends MY_BackendController
{

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

    public function index()
    {
        $this->load->model('Menu_badge_notification_model');
        $this->Menu_badge_notification_model->read($this->Menu_badge_notification_model::NAME_RECEIPT);
        $this->view('backend/receipt_approval/index', []);
    }

    public function add()
    {
        $this->view('backend/receipt_approval/add', []);
    }

    public function insert()
    {
        $input = $this->input->post();
        if (!empty($input)) {
            if (count($_FILES) > 0) { //mandatory
                $fileExt = pathinfo($_FILES['receipt_approval']['name'], PATHINFO_EXTENSION);
                $tenantno = $this->session->userdata('tenant_no');
                //                log_message('debug','ext:'.$fileExt);//pdf
                $data = array(
                    'unit_id' => $this->input->post('unit_id'),
                    'title' => $this->input->post('title'),
                    'remark' => $this->input->post('remark'),
                    'tenant_no' => $tenantno,
                    'ext' => $fileExt,
                );
                $this->load->model('Receipt_approval_model');
                $receipt_approvalCode = $this->Receipt_approval_model->insertDB($this->Receipt_approval_model->database, $this->Receipt_approval_model->tbl, $data);
                //                $receipt_approvalCode = $this->receipt_approvals_model->add($data);
                $newName = UPLOADSPATH . DS . $tenantno . DS . "receipt_approvals" . DS . $receipt_approvalCode . "." . $fileExt;
                //        $newName = "..\\..\\cache\\" . $receipt_approvalCode . "." . $fileExt;
                //                log_message('debug', 'path:' . $newName);
                rename($_FILES['receipt_approval']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            header('location: /receipt_approval');
        }
    }

    public function edit()
    {
        $this->load->model('Invoicing_model');
        $this->load->model('Receipt_model');
        $this->load->model('Receipt_item_model');
        $data = $this->Receipt_approval_model->detail($this->input->post('id'));
        $invoice = $this->Invoicing_model->detailforread($data['invoice_id']);
        $isinvoiceinmerge = CommonHelper::checkIfIsInvoiceInMerge($invoice);
        if ($isinvoiceinmerge) {
            $receipt = $this->Receipt_model->getByPayTypeInvoiceIds(PaymentType::ATM, $invoice['id']);
            unset($invoice['id']);
            $data = array_merge($invoice, $data);
            //			print_r('amountpaid:'. CommonHelper::money($amountpaid));
            $data['amount_paid'] = $receipt['atm_amount'];
            $data = array_merge(['amountpaidtxt' => CommonHelper::money($receipt['atm_amount'])], $data);
        }
        //		log_message('debug','isinvoiceinmerge:'.$isinvoiceinmerge);
        $this->view('backend/receipt_approval/edit', ['data' => $data, 'isinvoiceinmerge' => $isinvoiceinmerge]);
    }

    // Insert / Update
    public function save()
    {
        $input = $this->input->post();
        $id = $input['id'];
        $data = [
            'atm_amount' => $input['amount_paid'],
            'amount_paid' => $input['amount_paid']
        ];

        $this->load->model('Receipt_approval_model');
        $this->Receipt_approval_model->saveAmt($data, $id);


        // $this->Receipt_approval_model->trigger_push_notification($id, $input['amount_paid']);
        header('location:/receipt_approval');
    }

    public function delete()
    {
        $this->load->model('Receipt_approval_model');
        $this->Receipt_approval_model->delete($this->input->post('id'));
        header("location:/receipt_approval");
    }

    // public function deleteall()
    // {
    //     $this->load->model('Receipt_approval_model');
    //     $this->Receipt_approval_model->deleteAll();
    //     header("location:/receipt_approval");
    // }
}
