<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Transfer extends MY_Controller {
        function __construct(){
            parent::__construct();
            if($this->session->user == ""){
                redirect('login');
            }
            $this->load->model('finance/TransactionModel');
            $this->load->model('finance/AccountModel');
//            $this->load->model('inventory/purchaseinvoice_model');
            $this->load->library('Formatslist');
        }
        public function index(){
            $data['accounts'] = $this->AccountModel->dropdown_acounts();
            $data['template'] = 'finance/add_transfer';
            $this->load->view('template',$data);
        }
        
        public function transferamount(){
            $dataTrans["TRANSACTION_DATE"]      =   date("Y-m-d",strtotime($this->input->post('TRANSACTION_DATE')));
            $dataTrans["TRANSACTION_TYPE_ID"]    =   '0'; // 0 for Deposit
            $dataTrans["PAYMENTHEAD_ID"]    =   'DEPOSIT'; // Defualt Deposit
            $dataTrans["ACCOUNT_ID"]    =   $this->input->post('accountto');
            $dataTrans["AMOUNT"]    =   $this->input->post('amount');
            $dataTrans["COMMENTS"]    =   $this->input->post('comments');
            $dataTrans["STATUS"]    =   0;
            $dataTrans["CREATED"]    =   date('Y-m-d H:i:s');
            $dataTrans["CREATED_BY"]    =   $this->session->userid;
            $dataTrans["APPROVED_BY"]    =   $this->session->userid;
            $dataTrans["ID"]    = uniqid();
            if($this->TransactionModel->add_transaction($dataTrans)){
                $dataTrans["ACCOUNT_ID"]            =   $this->input->post('accountfrom');
                $dataTrans["TRANSACTION_TYPE_ID"]   =   '1';
                $dataTrans["AMOUNT"]    =   -1*abs($this->input->post('amount'));
                $dataTrans["PAYMENTHEAD_ID"]        =   'DEPOSIT';
                $dataTrans["ID"]    = uniqid();
                if($this->TransactionModel->add_transaction($dataTrans)){
                    $this->session->set_flashdata('success_msg', 'Amount has been successfully transferred');
                    redirect('finance/transfer');
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }
?>