<?php

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

class Wakafsetup extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Useraccess');
        $query = $this->Useraccess->useraccess_id_module2('7', 'setup');
        if (!$query) {
            echo '<script>alert("Not authorised..");
				window.location.replace("' . base_url() . 'Error");
				</script>';
        }
        $this->load->library('layouts');
        $this->load->model('menu');
    }
    
    public function listWakaf(){
        echo "List wakaf";
    }

    public function newWakaf() {
        $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
        
        $this->load->model('master/masterCode_model');
	$data['waqifList'] = $this->masterCode_model->get_all(array('masterID'=>22));
        
        
        $this->load->model('waqaf/waqafCenter');
	$data['waqfcenterList'] = $this->waqafCenter->group_all();
        
        
        
        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                ->add_includes('js/accounting.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->load->model('ZakatGroup');
        
        $query = $this->ZakatGroup->wakafBeneficiaries_all();
            $data['datatbls'] = $query;
            
         $data['zakatGroup'] = $this->ZakatGroup->group_all();
         $this->lang->load("WakafSetup", $this->session->userdata('language'));
        
        $this->layouts->view('zakat/setups/newwakaf', array('latest' => 'sidebar/latest'), $data);
    }
    
    public function wakaf_pre_setup(){
         $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
        
       
        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                ->add_includes('js/accounting.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->load->model('ZakatGroup');
        
        $this->lang->load("WakafSetup", $this->session->userdata('language'));
        
        $this->layouts->view('zakat/setups/preSetupWakaf', array('latest' => 'sidebar/latest'), $data);
    }
    
     public function saveOneRowWakaf() {
            $this->load->model('ZakatGroup');
            $dataI = array(
                    "salesAccountID" => $this->input->post('acctNo'),
                    "wakafPercent" => $this->input->post('wakafPercent'),
                    "from" => date("Y-m-d", strtotime($this->input->post('from'))),
                    "to" => date("Y-m-d", strtotime($this->input->post('to'))),
                    'companyID' => element('compID', $this->session->userdata('logged_in')),
                    "createdBy" => element('role', $this->session->userdata('logged_in')),
                );
                echo  $this->ZakatGroup->insert("wakaf_acrual", $dataI);
            
        }
        
   
        public function getwakafAcrualAmount(){
            $this->load->model('ZakatGroup');
            echo  $this->ZakatGroup->getwakafAcrualAmount(element('compID', $this->session->userdata('logged_in')));
        }

        public function savewakafAcrual(){
        $this->load->model('ZakatGroup');
            $wdetail = $this->jsonToArray($this->input->post('wDetail'));
            $n = sizeof($wdetail ['rs']);
            $successN = 0;
            //   $pSuccessId=array()

            for ($i = 0; $i < $n; $i++) {
                $dataI = array(
                    "salesAccountID" => $wdetail ['rs'] [$i]->salesAccountID,
                    "wakafPercent" => $wdetail ['rs'] [$i]->wakafPercent,
                    "from" => date("Y-m-d", strtotime($wdetail ['rs'] [$i]->from)),
                    "to" => date("Y-m-d", strtotime($wdetail ['rs'] [$i]->to)),
                    'companyID' => element('compID', $this->session->userdata('logged_in')),
                    "createdBy" => element('role', $this->session->userdata('logged_in')),
                );
                $idD = $this->ZakatGroup->insert("wakaf_acrual", $dataI);
                if ($idD > 0) {
                    // $pSuccessId = $row->invoiceID.'"';
                    $successN++;
                }
            }
             echo $successN;
    }
    
    public function wakaf_acrual_list(){
          $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
        
       
        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                ->add_includes('js/accounting.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->load->model('ZakatGroup');
        
        $sDate = ($this->uri->segment(3)) ? $this->uri->segment(3):NULL;
        $eDate = ($this->uri->segment(4)) ? $this->uri->segment(4):NULL;
                
        $this->lang->load("WakafSetup", $this->session->userdata('language'));
        $data['listWkaf']= $this->ZakatGroup->getWakafPreSetupList($sDate, $eDate);
        $this->layouts->view('zakat/setups/preSetupWakafList', array('latest' => 'sidebar/latest'), $data);
    }
    
    public function updatewakafAcrual(){
        $this->load->model('ZakatGroup');
        $dataUpd = array(
                    "salesAccountID" => $this->input->post('acct'),
                    "wakafPercent" => $this->input->post('percent'),
                    "from" => date("Y-m-d", strtotime($this->input->post('from'))),
                    "to" => date("Y-m-d", strtotime($this->input->post('to'))),
                    'updatedby' => element('compID', $this->session->userdata('logged_in')),
                );
                echo $this->ZakatGroup->UpdateByTname("wakaf_acrual", $this->input->post('rowId'), $dataUpd);
    }
    
    
    public function deleteWakafAcrual(){
        $this->load->model('ZakatGroup');
        echo $this->ZakatGroup->ZakatGroup_delByTname("wakaf_acrual", $this->input->post('rowId'));
    }

    public function Wakaf() {
        //var_dump($this->input->post());
        $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();

        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                ->add_includes('js/accounting.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->load->model('ZakatGroup');
        if ($this->input->post('mysubmit')) {
            if ($this->input->post('ID') == NULL) {
                $query = $this->ZakatGroup->wakaf_Insert();
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.location.replace("groupList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            } else {
                $updatedata = array(
                    'name' => $this->input->post('name'),
                    'description' => $this->input->post('description'),
                    'parentID' => $this->input->post('parentID'),
                    'sequence' => $this->input->post('sequence'),
                    'updatedBy' => element('role', $this->session->userdata('logged_in')),
                );
                $query = $this->ZakatGroup->ZakatGroup_save($this->input->post('ID'), $updatedata);
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.
                                location.replace("groupList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            }
        } else {
            $ID = $this->uri->segment(3);
            $this->lang->load("WakafSetup", $this->session->userdata('language'));
            $query = $this->ZakatGroup->wakafBeneficiaries_all();
            $data['datatbls'] = $query;

            $data['zakatGroup'] = $this->ZakatGroup->group_all();
            if ($ID) {
                $query = $this->ZakatGroup->ZakatGroup_ID($ID);
                $data['zakatData'] = $query[0];
                $this->layouts->view('zakat/setups/groupEdit', array('latest' => 'sidebar/latest'), $data);
            } else {
                $data['zakatData'] = NULL;
                $this->layouts->view('zakat/setups/wakaf', array('latest' => 'sidebar/latest'), $data);
            }
        }
    }
	
	public function wakafList() {
		//$ID = isset($_REQUEST['ID'])?$_REQUEST['ID']:"";
		//$mode = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->lang->load("wakafSetup",$this->session->userdata('language'));
		$this->load->model('company/FormSetup_model');
		$this->load->model('ZakatGroup');
		
		
//		$dateFrom = $this->input->post('selDateFrom');
//		$dateTo = $this->input->post('selDateTo');

		//$data['mode'] = $mode; 
		
		$dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		//$ID = $this->input->post('ID');
		//$query = $this->company_info_model->updatePosting($ID);
		
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['datatbls'] = $this->ZakatGroup->wakafListDate($dateFrom, $dateTo);
		
//		echo $this->db->last_query();
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('waqaf/wakafList', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function wakafList_print($mode = false) 
	{
		//$mode = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->load->model('company/FormSetup_model');
		$this->load->model('ZakatGroup');
		
		$this->lang->load("wakafSetup",$this->session->userdata('language'));
		
		$dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['datatbls'] = $this->ZakatGroup->wakafListDate($dateFrom, $dateTo);
		//$supplierInfo = $this->Supplier_model->suppliermaster_id($data['transInfo'][0]->supplierID);
		//echo $this->db->last_query();
		//var_dump($data['datatbls2'][0]);           
             //$headerData['rightContentHeader'] = "Name supplier/ ot";
             $headerData['rptTitle'] = $this->lang->line("rptTitle");
             /*$headerData['leftContent'] = array(
												"<b>".$data['transInfo'][0]->customerName."</b>",
												$data['transInfo'][0]->customerCompanyNo,
												$data['transInfo'][0]->line1,
												$data['transInfo'][0]->line2,
												$data['transInfo'][0]->line3,
												$data['transInfo'][0]->customerPostCode.' '.$data['transInfo'][0]->city
											);*/
			 $headerData['rightContent'] = array(
												//"<b>".$this->lang->line("formNo").":".$data['transInfo'][0]->creditNoteNo."</b>",
												$this->lang->line("from")." ".date('d-m-Y', strtotime($dateFrom))." ".$this->lang->line("to")." ".date('d-m-Y', strtotime($dateTo)),
												//$this->lang->line("currency").":".$data['transInfo'][0]->fldcurr_code,
												//$this->lang->line("terms").":".$data['transInfo'][0]->termName
											);
             
          //   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $data['datatbls'];
			$bodyData['tableHeader'] = array($this->lang->line("wakafCenter"), $this->lang->line("saleAmount"), $this->lang->line("amountAllocated"), $this->lang->line("createdTS")); 
			$bodyData['selectFields'] = array("name","saleAmount","amountAllocated","datewakaf");
			$bodyData ['tdAmount'] = array("saleAmount","amountAllocated");
			//$bodyData['tableFooter'] = array ("footer : value");
			$bodyData['tableFooter'] = array ("");
			$bodyData['excelTitle'] = "Location Transfer Detail";
			
				
		if($mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			//var_dump($bodyData2['selectFields']);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}

    /* public function wakafBeneficiaries(){
      $this->load->model('ZakatGroup');
      $this->lang->load("WakafSetup",$this->session->userdata('language'));
      $this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
      $data['wakafBeneficiaries']  = $this->ZakatGroup->wakafBeneficiaries_all();
      $this->layouts->view('zakat/setups/wakaf', array('latest' => 'sidebar/latest'), $data);
      } */

    public function groupDelete() {
        $this->lang->load("zakatGroup", $this->session->userdata('language'));
        $this->load->model('ZakatGroup');
        $query = $this->ZakatGroup->ZakatGroup_Delete($this->input->post('fldid'));
        if ($query) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function NisabDetails() {
        $ID = $this->uri->segment(3);
        ;
        $this->lang->load("zakatDetails", $this->session->userdata('language'));
        $this->load->model('ZakatGroup');
        $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');
        if ($ID) {
            $query = $this->ZakatGroup->ZakatNisabDetails_GroupID($ID);
            $data['zakatData'] = $query[0];
        } else {
            $query = $this->ZakatGroup->ZakatNisabDetails_GroupID(1);
            if ($query) {
                $data['zakatData'] = $query[0];
            } else {
                $data['zakatData'] = NULL;
            }
        }
        $this->layouts->view('zakat/setups/nisabDetails', array('latest' => 'sidebar/latest'), $data);
    }

    public function nisabDetailsSearch() {
        $this->load->model('ZakatGroup');
        $query = $this->ZakatGroup->ZakatNisabDetails_GroupID($this->input->post('ID'));
        if ($query) {
            print json_encode(array("status" => "success", "message" => $query));
        } else {
            print json_encode(array("status" => "failed", "message" => ''));
        }
    }

    public function nisabDetailsDelete() {
        $this->load->model('ZakatGroup');
        $query = $this->ZakatGroup->ZakatNisabDetails_Delete($this->input->post('fldid'));
        if ($query) {
            print json_encode(array("status" => "success", "message" => $query));
        } else {
            print json_encode(array("status" => "failed", "message" => ''));
        }
    }

    public function nisabDetailsInsert() {
        $this->load->model('ZakatGroup');
        $query = $this->ZakatGroup->ZakatNisabDetails_Insert();
        if ($query) {
            $query1 = $this->ZakatGroup->ZakatNisabDetails_ID($query);
            print json_encode(array("status" => "success", "message" => $query1[0]));
        } else {
            print json_encode(array("status" => "failed", "message" => 'Journal details saved..'));
        }
    }

    public function Nisab() {
        $this->load->model('ZakatNisab');
        if ($this->input->post('mysubmit')) {
            if ($this->input->post('ID') == NULL) {
                $query = $this->ZakatNisab->ZakatNisab_insert();
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.location.replace("' . base_url() . 'zakatReport/nisabList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            } else {
                $updatedata = array(
                    'year' => $this->input->post('year'),
                    'nisab' => $this->input->post('nisab'),
                    'updatedBy' => element('role', $this->session->userdata('logged_in')),
                );
                $query = $this->ZakatNisab->ZakatNisab_save($this->input->post('ID'), $updatedata);
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.location.replace("' . base_url() . 'zakatReport/nisabList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            }
        } else {
            $ID = $this->uri->segment(3);
            ;
            $this->lang->load("zakatNisab", $this->session->userdata('language'));
            $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                    ->add_includes('js/validationengine/jquery.validationEngine.js');
            $this->load->model('ZakatCenter');
            $data['ZakatCenter'] = $this->ZakatCenter->group_all();
            if ($ID) {
                $query = $this->ZakatNisab->ZakatNisab_ID($ID);
                $data['zakatData'] = $query[0];
                $this->layouts->view('zakat/setups/nisabEdit', array('latest' => 'sidebar/latest'), $data);
            } else {
                $data['selyear'] = date("Y");
                $data['zakatData'] = NULL;
                $this->layouts->view('zakat/setups/nisab', array('latest' => 'sidebar/latest'), $data);
            }
        }
    }

    public function nisabDelete() {
        $this->load->model('ZakatNisab');
        $query = $this->ZakatNisab->ZakatNisab_Delete($this->input->post('fldid'));
        if ($query) {
            print json_encode(array("status" => "success", "message" => $query));
        } else {
            print json_encode(array("status" => "failed", "message" => ''));
        }
    }

// Zakat Center

    public function Center() {
        $this->load->model('ZakatCenter');
        if ($this->input->post('mysubmit')) {
            if ($this->input->post('ID') == NULL) {
                $query = $this->ZakatCenter->ZakatCenter_insert();
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.location.replace("' . base_url() . 'zakatReport/centerList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            } else {
                $updatedata = array(
                    'shortName' => $this->input->post('shortName'),
                    'name' => $this->input->post('name'),
                    'line1' => $this->input->post('line1'),
                    'line2' => $this->input->post('line2'),
                    'line3' => $this->input->post('line3'),
                    'city' => $this->input->post('city'),
                    'postCode' => $this->input->post('postCode'),
                    'stateID' => $this->input->post('state'),
                    'countryID' => $this->input->post('country'),
                    'noTel' => $this->input->post('noTel'),
                    'noFax' => $this->input->post('noFax'),
                    'email' => $this->input->post('email'),
                    'website' => $this->input->post('website'),
                    'updatedBy' => element('role', $this->session->userdata('logged_in')),
                );
                $query = $this->ZakatCenter->ZakatCenter_save($this->input->post('ID'), $updatedata);
                if ($query) {
                    echo '<script>alert("Insert Data Success..");
				window.location.replace("' . base_url() . 'zakatReport/centerList");
				</script>';
                } else {
                    echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
                }
            }
        } else {
            $ID = $this->uri->segment(3);
            ;
            $this->lang->load("zakatCenter", $this->session->userdata('language'));
            $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                    ->add_includes('js/validationengine/jquery.validationEngine.js');
            $this->load->model('Country');
            $this->load->model('State');
            $data['country'] = $this->Country->country_all();
//		$data['ZakatCenter'] = $this->ZakatCenter->group_all();
            if ($ID) {
                $query = $this->ZakatCenter->ZakatCenter_ID($ID);
                $data['zakatData'] = $query[0];
                $data['state'] = $this->State->state_region($query[0]->countryID);
                $this->layouts->view('zakat/setups/centerEdit', array('latest' => 'sidebar/latest'), $data);
            } else {
                $data['state'] = $this->State->state_region('458');
                $data['zakatData'] = NULL;
                $this->layouts->view('zakat/setups/center', array('latest' => 'sidebar/latest'), $data);
            }
        }
    }
    
    public function getSaleAmount(){
        $this->load->model('ZakatCenter');
        echo $this->ZakatCenter->getSaleAmountForWakaf($this->input->post('year'), $this->input->post('from'), $this->input->post('to'), $this->input->post('acctno'));
     
    }
    
    
     public function jsonToArray($json) {
            $object = json_decode ( $json );
            if (is_object ( $object )) {
                    foreach ( $object as $key => $value ) {
                            $array [$key] = $value;
                    }
            } else {
                    $array = $object;
            }
            return $array;
	}
        
        
        public function saveNewWakaf() {
        $this->load->model('ZakatGroup');

        $dataInsert = array();

        if ($this->input->post('option') == "bySale") {
            $dataInsert = array(
                'companyID' => element('compID', $this->session->userdata('logged_in')),
                "createdBy" => element('role', $this->session->userdata('logged_in')),
                "waqifID" => $this->input->post('waqif'),
                "wakafCenterID" => $this->input->post('wakafCenterID'),
                "periodYear" => $this->input->post('year'),
                "periodFrom" => $this->input->post('from'),
                "periodTo" => $this->input->post('to'),
                "amountAllocated" => $this->input->post('wakafAmount'),
                "allocation" => 1
            );
        } else
            if ($this->input->post('option') == "accrual") {
                 $dataInsert = array(
                'companyID' => element('compID', $this->session->userdata('logged_in')),
                "createdBy" => element('role', $this->session->userdata('logged_in')),
                "wakafCenterID" => $this->input->post('wakafCenterID'),
                "waqifID" => $this->input->post('waqif'),
                "amountAllocated" => $this->input->post('wakafAmount'),
                "allocation" => 2
                     );
            }else {
            $dataInsert = array(
                'companyID' => element('compID', $this->session->userdata('logged_in')),
                "createdBy" => element('role', $this->session->userdata('logged_in')),
                "wakafCenterID" => $this->input->post('wakafCenterID'),
                "waqifID" => $this->input->post('waqif'),
                "amountAllocated" => $this->input->post('wakafAmount'),
                "allocation" => 0
            );
        }
        
        
       

        $id = $this->ZakatGroup->insert("wakaf", $dataInsert);
        if ($id > 0) {


            $wdetail = $this->jsonToArray($this->input->post('wDetail'));
            $n = sizeof($wdetail ['rs']);
            $successN = 0;
            //   $pSuccessId=array()

            for ($i = 0; $i < $n; $i++) {
                $dataI = array(
                    "wakafID" => $id,
                    "beneficiariesID" => $wdetail ['rs'] [$i]->beneficiariesID,
                    "description" => $wdetail ['rs'] [$i]->description,
                    "contributionPercentage" => $wdetail ['rs'] [$i]->contributionPercentage,
                    "amount" => $wdetail ['rs'] [$i]->amount,
                    "createdBy" => element('role', $this->session->userdata('logged_in')),
                );
                $idD = $this->ZakatGroup->insert("wakaf_details_distribution", $dataI);
                if ($idD > 0) {
                    // $pSuccessId = $row->invoiceID.'"';
                    $successN++;
                }
            }

            if ($this->input->post('option') == "bySale") {
                $wSaleAcct = $this->jsonToArray($this->input->post('wSaleAcct'));
                $n = sizeof($wSaleAcct ['rs']);
                $saleAccSuccess = 0;
                //   $pSuccessId=array()

                for ($i = 0; $i < $n; $i++) {
                    $dataS = array(
                        "wakafID" => $id,
                        "accountID" => $wSaleAcct ['rs'] [$i]->accountID,
                        "saleAmount" => $wSaleAcct ['rs'] [$i]->saleAmount,
                        "contributionPercentage" => $wSaleAcct ['rs'] [$i]->contributionPercentage,
                        "amount" => $wSaleAcct ['rs'] [$i]->amount,
                        "createdBy" => element('role', $this->session->userdata('logged_in')),
                    );
                    $idD = $this->ZakatGroup->insert("wakaf_details_allocation", $dataS);
                    if ($idD > 0) {
                        // $pSuccessId = $row->invoiceID.'"';
                        $saleAccSuccess++;
                    }
                }
            }
        }
        //  echo $successN;

        echo $id;
    }

    public function editwakaf($id){
          $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
        
        $this->load->model('master/masterCode_model');
	$data['waqifList'] = $this->masterCode_model->get_all(array('masterID'=>22));
        
        $this->load->model('waqaf/waqafCenter');
	$data['waqfcenterList'] = $this->waqafCenter->group_all();
        
        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-' . $this->session->userdata('language') . '.js')
                ->add_includes('js/accounting.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->load->model('ZakatGroup');
        
        $query = $this->ZakatGroup->wakafBeneficiaries_all();
        $data['datatbls'] = $query;
            
         $data['zakatGroup'] = $this->ZakatGroup->group_all();
         $this->lang->load("WakafSetup", $this->session->userdata('language'));
         
         $data['master'] = $this->ZakatGroup->getWakafMaster($id);
        
         $data['saleAllocationDetail'] = $this->ZakatGroup->getWakafSaleDetails($id);
         
         $data['details'] = $this->ZakatGroup->getWakafDetail($id);
        
        $this->layouts->view('zakat/setups/editwakaf', array('latest' => 'sidebar/latest'), $data);
        
        
    }
    
    function deleteBeneRow(){
          $this->load->model('ZakatGroup');
          $this->ZakatGroup->ZakatGroup_delByTname("wakaf_details_distribution", $this->input->post('rowId'));
    }
    
    function addNewRowWakafBene(){
         $this->load->model('ZakatGroup');
         $dataI = array(
                        "wakafID" => $this->input->post('masterId'),
                        "beneficiariesID" => $this->input->post('bene'),
                        "description" =>  $this->input->post('desc'), 
                        "contributionPercentage" => $this->input->post('t'), 
                        "amount" => $this->input->post('amt'), 
                        "createdBy" => element('role', $this->session->userdata('logged_in')),
                        );
                    $idD = $this->ZakatGroup->insert("wakaf_details_distribution",$dataI);
        echo $idD;
        }
        
     function uptRowWakafBene(){
          $this->load->model('ZakatGroup');
         $dataI = array(
                        "beneficiariesID" => $this->input->post('bene'),
                        "description" =>  $this->input->post('desc'), 
                        "contributionPercentage" => $this->input->post('t'), 
                        "amount" => $this->input->post('amt'), 
                        "updatedBy" => element('role', $this->session->userdata('logged_in')),
                        );
                    $idD = $this->ZakatGroup->ZakatGroup_UpdateByTname("wakaf_details_distribution",$this->input->post('rowId'), $dataI);
        echo $idD;
     }
     
     function updatewakafMasterOnly(){
         
          $this->load->model('ZakatGroup');
          
         $dataInsert = array();
        
        if($this->input->post('option')=="bySale"){
         $data= array(
                            "updatedBy" => element('role', $this->session->userdata('logged_in')),
                            "waqifID" => $this->input->post('waqif'),
                           
                            "amountAllocated" => $this->input->post('wakafAmount'),
                            "allocation" => 1
            );
        }
        else
            if($this->input->post('option')=="accrual"){
                $data= array(
                            'companyID' => element('compID', $this->session->userdata('logged_in')),
                            "updatedBy" => element('role', $this->session->userdata('logged_in')),
                            "waqifID" => $this->input->post('waqif'),
                            "amountAllocated" => $this->input->post('wakafAmount'),
                            "allocation" => 2
               );
            }else{
            $data= array(
                            'companyID' => element('compID', $this->session->userdata('logged_in')),
                            "updatedBy" => element('role', $this->session->userdata('logged_in')),
                            "waqifID" => $this->input->post('waqif'),
                            "amountAllocated" => $this->input->post('wakafAmount'),
                            "allocation" => 0
            );
        }
         $res = $this->ZakatGroup->ZakatGroup_UpdateByTname("wakaf",$this->input->post('rowId'), $data);
      
        echo $this->ZakatGroup->ZakatGroup_UpdateWakafDetail($this->input->post('masterId'),$this->input->post('wakafAmount'));
     }
     
     public function deleteWakafSaleAllowance(){
         $this->load->model('ZakatGroup');
        $rs=  $this->ZakatGroup->ZakatGroup_delByTname("wakaf_details_allocation", $this->input->post('rowId'));
          //if($rs){
             $this->ZakatGroup->ZakatGroup_UpdateByTname("wakaf", $this->input->post('masterId'), array ("amountAllocated"=>$this->input->post('wakafAmount'))); 
             $this->ZakatGroup->ZakatGroup_UpdateWakafDetail($this->input->post('masterId'),$this->input->post('wakafAmount'));
             //}
             echo $rs;
     }

          public function insertWakafSaleOnly(){
          $this->load->model('ZakatGroup');
        $dataS = array(
            "wakafID" => $this->input->post('wakafID'),
            "accountID" => $this->input->post('accountID'),
            "saleAmount" => $this->input->post('saleAmount'),
            "contributionPercentage" => $this->input->post('contributionPercentage'),
            "amount" =>$this->input->post('amount'),
            "createdBy" => element('role', $this->session->userdata('logged_in')),
        );
        $idD = $this->ZakatGroup->insert("wakaf_details_allocation", $dataS);
        if($idD>0){
            $updt = array(
                "periodYear" => $this->input->post('year'),
                "periodFrom" => $this->input->post('from'),
                "periodTo" => $this->input->post('to'),
                "amountAllocated" => $this->input->post('wakafAmount')
            );
            $this->ZakatGroup->ZakatGroup_UpdateByTname("wakaf", $this->input->post('wakafID'), $updt);
             $this->ZakatGroup->ZakatGroup_UpdateWakafDetail($this->input->post('wakafID'), $this->input->post('wakafAmount'));
             echo $idD;
             
        } 
     }

    /************Sayem (21-7-2014_3:02_pm)*************/

    //Wakaf letter
    public function wakafLetter($id){
        $this->lang->load("wakafletter", $this->session->userdata('language'));
        $this->load->model("WaqafCenter");    

        //Get wakaf information 
        $data = $this->WaqafCenter->wakafBy_id($id);
        //Header text of the letter.
        $data['letterHeader'] = $this->lang->line('letterHdr');

        //Name & Address of Waqf Institution.
        $data['nameNaddress'] = array(
                                    $data['info'][0]->name,
                                    $data['info'][0]->line1,
                                    $data['info'][0]->line2,
                                    $data['info'][0]->line3,
                                    $data['info'][0]->city.", ".$data['info'][0]->postCode,
                                );
        //Donation detail
        $data["donationDetail"] = $this->lang->line('donation');

        //Beneficiary list
        $amount = 0;
        foreach($data['detail'] as $row){
            $beneficiary[] = array($row->shortName,$row->description,$row->contributionPercentage,$row->amount);
            $amount += $row->amount;
        }
        $data["total"] = $amount;
        if(!empty($beneficiary)) {$data['beneficiary'] = $beneficiary;}

        //Cash Paid
        /*$data['cashPaidDate'] = date('d-m-Y');
        $data['cashPaidAmount'] = 4532;*/

        //Cash Payable
        /*$data['cashPayableDate'] = date('d-m-Y');
        $data['cashPayableAmount'] = 4532;*/

        $this->load->view('layouts/letter/header',$data);
        $this->load->view('layouts/letter/body',$data);
        $this->load->view('layouts/letter/footer');
    }

    
    //Wakaf Detail
    public function wakafDetail($id,$mode = false){        
        $this->lang->load("wakafDetail", $this->session->userdata('language'));
        $this->load->model('ZakatGroup');
        $this->load->model('Compfinancialyear');

        $this->load->model('WaqafCenter');
        $data = $this->WaqafCenter->wakafBy_id($id);


        $query1 = $this->Compfinancialyear->financialYear_yearperiod($data['info'][0]->periodYear, $data['info'][0]->periodFrom);
        $query2 = $this->Compfinancialyear->financialYear_yearperiod($data['info'][0]->periodYear, $data['info'][0]->periodTo);

        $amount1 = 0;
        foreach($data['detail_allocation'] as $row){
            $amount1 += $row->amount;
        }
        $amount2 = 0;
        foreach($data['detail'] as $row){
            $amount2 += $row->amount;
        }
        //Report 
        $headerData['rptTitle'] = $this->lang->line('title');
        

        $headerData['leftContentHeader'] = $data['info'][0]->name;
        $headerData['leftContent'] = array(
                                        $data['info'][0]->line1,
                                        $data['info'][0]->line2,
                                        $data['info'][0]->line3,
                                        $data['info'][0]->city.", ".$data['info'][0]->postCode,

                                    );

        //$headerData['rightContentHeader'] = "Name supplier/ ot";
        $headerData['rightContent'] = array(
                                        "Period: ".date('d/m/Y',strtotime($query1[0]->startDate))."-".date('d/m/Y',strtotime($query2[0]->endDate))
                                    );

        //Wakaf Allocation
        $bodyData1['rptDataDetail'] = $data['detail_allocation'];
        $bodyData1['tableHeader'] = array(
                                        $this->lang->line("saleAcct"),
                                        $this->lang->line("saleAmount"), 
                                        $this->lang->line("perAllocation"), 
                                        $this->lang->line("amountAllocation")
                                    ); 
        $bodyData1['selectFields'] = array("saleName","saleAmount","contributionPercentage","amount");
        $bodyData1 ['tdAmount'] = array("saleAmount","contributionPercentage","amount");
        $bodyData1['tableFooter'] = array ("Total : ".number_format($amount1,2));

        //Wakaf Distribution
        $bodyData2['rptDataDetail'] = $data['detail'];
        $bodyData2['tableHeader'] = array(
                                        $this->lang->line("beneficiary"),
                                        $this->lang->line("description"), 
                                        $this->lang->line("contributionPercentage"), 
                                        $this->lang->line("amount")
                                    ); 
        $bodyData2['selectFields'] = array("name","description","contributionPercentage","amount");
        $bodyData2 ['tdAmount'] = array("contributionPercentage","amount");
        $bodyData2['tableFooter'] = array ("Total : ".number_format($amount2,2));
        $bodyData2['excelTitle'] = $this->lang->line('title');
                
        if(!$mode){
            //print
            $this->load->view('layouts/report/header', $headerData);
            $this->load->view('layouts/report/body', $bodyData1);
            $this->load->view('layouts/report/body', $bodyData2);
            $this->load->view('layouts/report/footer','');
        }else{
            //download excell
            $this->load->helper('excel');
            excel($bodyData1);
        }           
    }
    /**********************end***********************/

}
