<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class zakatTransaction extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('7', 'transaction');
		if(!$query){
			echo '<script>alert("Not authorised..");
				window.location.replace("'.base_url().'Error");
				</script>';
		}
		$this->load->library('layouts');
		$this->load->model('menu');
	}
	
	public function Calculation(){
		$this->lang->load("zakatCalculation",$this->session->userdata('language'));
 	  	$this->load->model('ZakatGroup');
 	  	$this->load->model('ZakatCenter');
		$data['ZakatCenter'] = $this->ZakatCenter->group_all();
		$data['zakatGroup'] = $this->ZakatGroup->group_all();
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/datatables/jquery.dataTables.min.js')
				->add_includes('js/accounting.js')
				;
		$this->layouts->view('zakat/transactions/calculation', array('latest' => 'sidebar/latest'), $data);
	}
	public function zakatCalculation(){
 	  	$this->load->model('ZakatCalculate');
		if($this->input->post('methodID')){
			$query = $this->ZakatCalculate->method1();
			$queryAmount = $this->ZakatCalculate->method1_1();
			$queryAmountZakat = $this->ZakatCalculate->method1_2();
			if($query){
   				print json_encode(array("status"=>"success", "message"=>$query, "details"=>$queryAmount, "zakatAmount"=> $queryAmountZakat));
			}else{
   				print json_encode(array("status"=>"failed", "message"=>''));
			}
		}else{
			$query = $this->ZakatCalculate->method2();
		}
	}
	public function zakat_save(){
 	  	$this->load->model('ZakatSummary');
		$this->lang->load("zakatCalculation",$this->session->userdata('language'));
		$where = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'year'=>$this->input->post('nisabYear'),
		);
		$query = $this->ZakatSummary->ZakatSummary_search($where);
		if($query){
			$updateData = array(
				'centerID' => $this->input->post('centerID'),
				'totalAmount'=>$this->input->post('zakatPerniagaan'),
				'totalZakatPaid'=>$this->input->post('zakatPay'),
				'updatedBy' => element('role', $this->session->userdata('logged_in'))
			);
			$queryUpdate = $this->ZakatSummary->ZakatSummary_save($query[0]->ID, $updateData);
			if($queryUpdate){
   				print json_encode(array("status"=>"success", "message"=>$this->lang->line('message2')));
			}else{
   				print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message4')));
			}
		}else{
			$queryInsert = $this->ZakatSummary->ZakatSummary_Insert();
			if($queryInsert){
   				print json_encode(array("status"=>"success"));
			}else{
   				print json_encode(array("status"=>"failed"));
			}
		}
	}
}