<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Paymentshead extends MY_Controller {
	function __construct(){
            parent::__construct();
            if($this->session->user == ""){
                redirect('login');
            }
            $this->load->model('finance/PaymentHeadModel');
	}
	public function index(){
            $this->session->unset_userdata('editpaymentHDID');
            $data["paymentheadslist"] = $this->PaymentHeadModel->all_paymentheadsList();
            $data['template'] = 'finance/view_paymenthead';
            $this->load->view('template',$data);
	}
	public function add_paymenthead(){
            if(!empty($_POST)){
                if($this->input->post('TAXCAT')) $taxcat=$this->input->post('TAXCAT'); else $taxcat=NULL;
                if($this->input->post('id')==''){
                    $data =  array(
                        'ID' => uniqid(),
                        'PAYMENTHEAD' => $this->input->post('PAYMENTHEAD'),
                        'TAXCAT' => $taxcat
                    );
                    $this->PaymentHeadModel->add_paymenthead($data);
                }else{
                    $id = $this->input->post('id');
                    $data =  array(
                        'PAYMENTHEAD' => $this->input->post('PAYMENTHEAD'),
                        'TAXCAT' => $taxcat
                    );
                    $this->PaymentHeadModel->update_paymenthead($id, $data);
                }
                redirect('finance/paymentshead');
            } else {
                $id = $this->session->userdata('editpaymentHDID');
                $data = [];
                if ($id){
                    $account = $this->PaymentHeadModel->get_paymenthead($id);
                    $data['paymenthead'] = $account[0];
                }
                $data['tax_cat'] = $this->common->getAll('TAXCATEGORIES');
                $data['template'] = 'finance/add_paymenthead';
                $this->load->view('template',$data);
            }
	}
	public function delete_paymenthead($id=''){
            $where = array('ID' => $id);  
            if($this->common->delete('PAYMENTHEADS',$where)){
                $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
                redirect('finance/paymentshead');
            }else{
                $this->session->set_flashdata('success_msg', 'Cannot Delete');
                redirect('finance/paymentshead');
            }
	}
        public function edit_Paymenthead($id=''){
            $this->session->set_userdata(array('editpaymentHDID'  => $id));
            redirect("finance/paymentshead/add_paymenthead");
        }
        public function getPaymentheadtaxcat(){
            $paymentId  =   $this->input->post('paymentId');
            $paymentheaddet = $this->PaymentHeadModel->get_paymenthead($paymentId);
            if($paymentheaddet && $paymentheaddet[0]->TAXCAT) echo $paymentheaddet[0]->TAXCAT; else echo 'notaxcategory';
        }
    }