<?php
// src/Controller/ArticlesController.php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
class RecieptController extends AppController
{
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler', 'Paginator', 'Flash');


    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
         //$this->Security->config('unlockedActions', ['sectionAjax', 'section_ajax']);
         //$this->Security->config('allowedActions', ['sectionAjax', 'section_ajax']);

        if(isset($this->Security) && ($this->request->is('ajax') || $this->request['action'] == 'index')) {
			$this->Security->config('unlockedActions', [$this->request['action']]);
		}
         
    }    
   
    public function frec()
    {
        $this->loadModel('Member');
        $member_num = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
        $this->set('member_num',$member_num);

if ($this->request->is('post')) {
    $mem_no = $this->request->getData('member_num');
    $this->set('mem_no',$mem_no);
    $month = $this->request->getData('month');
    $this->set('month',$month);
    $year = $this->request->getData('year');
    $this->set('year',$year);
    $this->loadModel('Member');
        $query = $this->Member->find('all', ['conditions' => ['Member.mem_no'=>$mem_no]] );
        $member  = $query->first(); 
    $this->loadModel('Payment');
        $query = $this->Payment->find('all', ['conditions' => ['Payment.mem_no'=>$mem_no,'Payment.paid_on'=>$member->join_date]] );
        $payment  = $query->first(); 
        $member->thrift_amt =$payment ? $payment->thrift_amt :0;
        $member->payment_id =$payment ? $payment->rec_no :0;
        $member->share_amt =$payment ? $payment->share_amt :0;
        $member->loan_pri_amt =$payment ? $payment->loan_pri_amt :0;
        $member->loan_int_amt =$payment ? $payment->loan_int_amt :0;
        $member->last_int =$payment ? $payment->last_int :0;
        $member->last_principal =$payment ? $payment->last_principal :0;
        $member->others =$payment ? $payment->others :0;
        $member->insurance =$payment ? $payment->insurance :0;
        $member->tot_pay =$payment ? $payment->tot_pay :0;
        $member->paid_on =$payment ? $payment->paid_on :0;
        $member->f_pri =$payment ? $payment->f_pri :0;
        $member->f_int =$payment ? $payment->f_int :0;
        $this->loadModel('Loan');
        $query = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0,'Loan.mem_no'=>$mem_no]] );
        $loan  = $query->first(); 
        $member->loan_no =$loan ? $loan->loan_no :0;
       
    $this->set(compact('member'));  
}
    }


   public function osrec()
    {
        $this->loadModel('Member');
        $member_num = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
        $this->set('member_num',$member_num);

if ($this->request->is('post')) {
    $mem_no = $this->request->getData('member_num');
    $this->set('mem_no',$mem_no);
  
    $this->loadModel('Member');
        $query = $this->Member->find('all', ['conditions' => ['Member.mem_no'=>$mem_no]] );
        $member  = $query->first(); 
    $this->loadModel('Payment');
        $query = $this->Payment->find('all', ['conditions' => ['Payment.mem_no'=>$mem_no,'Payment.os_pay !='=>0],'order' => array('os_rno' => 'DESC')] );
        $payment  = $query->first(); 
        $re =$payment ? $payment->mem_no :0;
        $member->thrift_amt =$payment ? $payment->thrift_amt :0;
        $member->rec_no =$payment ? $payment->rec_no :0;
        $member->share_amt =$payment ? $payment->share_amt :0;
        $member->loan_pri_amt =$payment ? $payment->loan_pri_amt :0;
        $member->loan_int_amt =$payment ? $payment->loan_int_amt :0;
        $member->last_int =$payment ? $payment->last_int :0;
        $member->last_principal =$payment ? $payment->last_principal :0;
        $member->p_int =$payment ? $payment->p_int :0;
         $member->f_pri =$payment ? $payment->f_pri :0;
        $member->f_int =$payment ? $payment->f_int :0;
        $member->f_old_pri =$payment ? $payment->f_old_pri :0;
        $member->f_old_int =$payment ? $payment->f_old_int :0;
        $member->f_p_int =$payment ? $payment->f_p_int :0;
        $member->others =$payment ? $payment->others :0;
        $member->insurance =$payment ? $payment->insurance :0;
        $member->due_by =$payment ? $payment->due_by :0;
        $member->ex_os =$payment ? $payment->ex_os :0;
        $member->f_ex_os =$payment ? $payment->f_ex_os :0;
        $member->os_pay =$payment ? $payment->os_pay :0;
        $member->f_os_pay =$payment ? $payment->f_os_pay :0;
        $member->tot_pay =$payment ? $payment->tot_pay :0;
        $member->paid_on =$payment ? $payment->paid_on :0;
        $this->loadModel('Loan');
        $query = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0,'Loan.mem_no'=>$mem_no]] );
        $loan  = $query->first(); 
        $member->loan_no =$loan ? $loan->loan_no :0;
  
    $this->set(compact('member'));  
}
    }
    public function fullrec()
    {
        $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ));
        $this->set('divisions',$divisions);
        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
    }
    public function fullview()
    {

if ($this->request->is('post')) {
    $division = $this->request->getData('division');
    $this->set('division',$division);
    $section = $this->request->getData('section');
    $this->set('section',$section);
    $month = $this->request->getData('month');
    $this->set('month',$month);
    $year = $this->request->getData('year');
    $this->set('year',$year);
    if($month=="" || $year=="") 
    {
        $this->Flash->success(__('Month and Year not selected'));
        return $this->redirect(['action' => 'fullrec']);
    }

    $sql='SELECT m.*,p.*,l.loan_no as loan_no FROM payment p join member m on m.mem_no=p.mem_no join loan l on p.mem_no=l.mem_no where ';
    if($division!="")
    {
     $sql.='m.division="'.$division.'" and ';   
    }
    if($section!="")
    {
     $sql.='m.section="'.$section.'" and ';   
    }
    $sql.='p.payment_month="'.$month.'" and p.payment_year="'.$year.'" order by p.rec_no ASC';
 
         $this->loadModel('Payment');
         $conn = ConnectionManager::get('default');
         $members = $conn->execute($sql);
    $this->set(compact('members'));  
}
    }

    public function smsall()
    {
        $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ));
        $this->set('divisions',$divisions);
        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
if ($this->request->is('post')) {
    $division = $this->request->getData('division');
    $this->set('division',$division);
    $section = $this->request->getData('section');
    $this->set('section',$section);
    $month = $this->request->getData('month');
    $this->set('month',$month);
    $year = $this->request->getData('year');
    $this->set('year',$year);
    if($month=="" || $year=="") 
    {
        $this->Flash->success(__('Month and Year not selected'));
        return $this->redirect(['action' => 'fullrec']);
    }

 $this->loadModel('Member');
    if($division!="" && $section=="")
    {
    $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.division'=>$division]] );
    $this->set(compact('members'));
    }
    elseif($division!="" && $section!=""){
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.division'=>$division,'Member.section'=>$section]] );
    $this->set(compact('members'));
    }
     elseif($division=="" && $section!=""){
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.section'=>$section]] );
    $this->set(compact('members'));
    }
    else{
    $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0]] );
    $this->set(compact('members'));
    }
    $result = array();
    foreach($members as $key=>$member){
        $mem_no=$member->mem_no;
        $this->loadModel('Payment');
     
            $query = $this->Payment->find('all', ['conditions' => ['Payment.is_deleted'=>'0','Payment.mem_no'=>$mem_no,'Payment.payment_month'=>$month,'Payment.payment_year'=>$year]] );
    
        
        $payment  = $query->first(); 
       // print_r($payment);die();
        $re =$payment ? $payment->mem_no :0;
        $member->thrift_amt =$payment ? $payment->thrift_amt :0;
        $member->rec_no =$payment ? $payment->rec_no :0;
        $member->share_amt =$payment ? $payment->share_amt :0;
          $member->ent_fees =$payment ? $payment->ent_fees :0;
        $member->loan_pri_amt =$payment ? $payment->loan_pri_amt :0;
        $member->loan_int_amt =$payment ? $payment->loan_int_amt :0;
        $member->last_int =$payment ? $payment->last_int :0;
        $member->last_principal =$payment ? $payment->last_principal :0;
        $member->p_int =$payment ? $payment->p_int :0;
         $member->f_pri =$payment ? $payment->f_pri :0;
        $member->f_int =$payment ? $payment->f_int :0;
        $member->f_old_pri =$payment ? $payment->f_old_pri :0;
        $member->f_old_int =$payment ? $payment->f_old_int :0;
        $member->f_p_int =$payment ? $payment->f_p_int :0;
        $member->others =$payment ? $payment->others :0;
        $member->insurance =$payment ? $payment->insurance :0;
         $member->old_ins =$payment ? $payment->old_ins :0;
          $member->old_thrift =$payment ? $payment->old_thrift :0;
        $member->due_by =$payment ? $payment->due_by :0;
        $member->ex_os =$payment ? $payment->ex_os :0;
        $member->f_ex_os =$payment ? $payment->f_ex_os :0;
        $member->os_pay =$payment ? $payment->os_pay :0;
        $member->tot_pay =$payment ? $payment->tot_pay :0;
        $member->paid_on =$payment ? $payment->paid_on :0;
        $member->payment_month =$payment ? $payment->payment_month :0;
        $this->loadModel('Loan');
        $query = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0,'Loan.mem_no'=>$mem_no]] );
        $loan  = $query->first(); 
        $member->loan_no =$loan ? $loan->loan_no :0;
        $result[$key] = $member;
            $months_t=array('January'=>'ஜனவரி','February'=>'பிப்ரவரி ','March'=>'மார்ச்','April'=>'ஏப்ரல்','May'=>'மே','June'=>'ஜுன்','July'=>'ஜுலை','August'=>'ஆகஸ்ட்','September'=>'செப்டம்பர்','October'=>'அக்டோபர்','November'=>'நவம்பர்','December'=>'டிசம்பர்' );
   $s_yr=date('Y',strtotime($member->paid_on));
   $s_month=date("m", strtotime($member->paid_on));
  $month_tam=$months_t[$member->payment_month];
 // print_r($month_tam);die();
   $pri=$member->loan_pri_amt+$member->last_principal;
    $int=$member->loan_int_amt+$member->last_int;
    $f_pri=$member->f_pri+$member->f_old_pri;
    $f_int=$member->f_int+$member->f_old_int;
   $msg=$member->mem_name."(".$member->mem_no."), ".$month_tam." ".$s_yr." மாத சம்பளத்தில் ரூ.".$member->tot_pay." செலுத்தியுள்ளீர்கள், சி. சே:".$member->thrift_amt.", அசல்:".$pri.", வட்டி:".$int.", ப. கடன்:".$f_pri.", வட்டி:".$f_int.", ரசீது எண்:".$member->rec_no.", - திருச்சிராப்பள்ளி வேளாண்மை கூட்டுறவு சங்கம்: 0431-2420540, மேலும் விவரங்களுக்கு இங்கே கிளிக் செய்யவும் http://r693.tnecs.com/demand/view?mem_no=".$member->mem_no."&month=".$payment->payment_month."&year=".$payment->payment_year."&added_on=";
    $umsg=urlencode($msg);
   $url="http://brightsms.brighttechnologies.co.in/httpapi/httpapi?token=4fc1372211bcae6ea775183d27f176e8&sender=AGRISO&number=$member->phone&route=2&type=3&sms=$umsg";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
              
}
  $this->Flash->success(__('SMS send successfully'));
                return $this->redirect(['action' => 'smsall']);
    $this->set(compact('member'));  

}
    }

}
?>