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

namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
class LoanController extends AppController
{
    var $helpers = array( 'Html' );
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        $loans = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0]] );
               $result = array();
            foreach($loans as $key=>$member){
        $mem_no= $member->mem_no;
        $this->loadModel('Member');
        $query = $this->Member->find('all', ['conditions' => ['Member.mem_no'=>$mem_no]] );
        $mem  = $query->first(); 
        $member->mem_name =$mem ? $mem->mem_name :"";
         $result[$key] = $member;
    }
        $this->set(compact('loans'));
    }

    public function view($mem_no)
    {
        $loan = $this->Loan->findByMem_no($mem_no)->firstOrFail();
        $this->set(compact('loan'));
    }

    public function add()
    {
        $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'valueField' => function ($row) {
          return $row['mem_no'] . '-' . $row['mem_name'];
      }, 'keyField' => 'mem_no'  ));
        $this->set('member',$member);
        $loan = $this->Loan->newEntity();
        if ($this->request->is('post')) {
           
            $loan = $this->Loan->patchEntity($loan, $this->request->getData());
            if ($this->Loan->save($loan)) {
                $mem_no = $this->request->getData('mem_no');
                $this->set('mem_no',$mem_no);
                $loan_os_amt = $this->request->getData('loan_amount');
                $this->set('loan_os_amt',$loan_os_amt);
                $this->loadModel('Loan');
                $os = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0,'Loan.mem_no'=>$mem_no]] );
                $os = $os->first();
                $loan_os = $os->loan_os_amt;
                $os_amt = $loan_os + $loan_os_amt;
                $loan = TableRegistry::get("Loan");
                $query = $loan->query();
                            $result = $query->update()
                                    ->set(['loan_os_amt' => $os_amt,'no_of_days' =>$this->request->getData('no_of_days') ,'fst_int' => '1'])
                                    ->where(['mem_no' => $this->request->getData('mem_no')])
                                    ->execute();
                $this->Flash->success(__(' loan has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to add loan.'));
        }
       
        $this->set('loan', $loan);
    }
  

public function edit($mem_no)
{
    $this->loadModel('Member');
    $member = $this->Member->find('list', array( 'valueField' => function ($row) {
      return $row['mem_no'] . '-' . $row['mem_name'];
  }, 'keyField' => 'mem_no'  ));
    $this->set('member',$member);
    $loan = $this->Loan->findByMem_no($mem_no)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
        $this->Loan->patchEntity($loan, $this->request->getData());
        if ($this->Loan->save($loan)) {
            $this->Flash->success(__('loan has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update loan.'));
    }

    $this->set('loan', $loan);
}
public function os()
{
    $this->loadModel('Member');
    $member = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
    $this->set('member',$member);
    if ($this->request->is('post')) {
    $mem_no = $this->request->getData('mem_no');
    $this->set('mem_no',$mem_no);
    $loan_os_amt = $this->request->getData('loan_os_amt');
    $this->set('loan_os_amt',$loan_os_amt);
    $temp=$loan_os_amt;
    $this->loadModel('Loan');
    $os = $this->Loan->find('all', ['conditions' => ['Loan.is_deleted' => 0,'Loan.mem_no'=>$mem_no]] );
    $os = $os->first();
    $loan_os = $os->loan_os_amt;
    $loan_pri = $os->loan_principle_amt;
    $f_loan_os = $os->f_os;
    $f_loan_pri = $os->f_pri;
    $this->loadModel('Member');
    $mem = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.mem_no'=>$mem_no]] );
    $mem = $mem->first();
    $od_int = $mem->old_due_int;
    $od_pri = $mem->old_due_pri;
    $f_od_int = $mem->f_old_int;
    $f_od_pri = $mem->f_old_pri;

    if($temp>0)
    {
     $temp=$temp-$od_int;
     if($temp>=0)
     {
      $old_due_int = $od_int;  
      $_old_due_int = 0;
     }
     else{
      $old_due_int = $od_int + $temp;
      $_old_due_int = $od_int - $old_due_int;
     }
    }
    else{
      $old_due_int = 0;
      $_old_due_int = $od_int;
    }

    if($temp>0)
    {
     $temp=$temp-$od_pri;
     if($temp>=0)
     {
      $old_due_pri = $od_pri;  
      $_old_due_pri = 0;
      $loan_os = $loan_os - $old_due_pri; 
     }
     else{
      $old_due_pri = $od_pri + $temp;
      $_old_due_pri = $od_pri - $old_due_pri;
      $loan_os = $loan_os - $old_due_pri;
     }
    }
    else{
      $old_due_pri = 0;
      $_old_due_pri = $od_pri;
      $loan_os = $loan_os - $old_due_pri;
    }
    $ex = $loan_os;

    if($temp>0)
    {
      $temp=$temp-$loan_os;
     if($temp>=0)
     {
       $loan_os = 0;
       $os_pay = $ex - $loan_os;
     }
     else{
      $os_pay = $ex + $temp;
      $loan_os = $ex - $os_pay;
     
     }
    }
    else{
      $os_pay = 0;
      
    }
    if($temp>0)
    {
      $this->loadModel('Member');
      $query = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.mem_no'=>$mem_no]] );
      $d  = $query->first(); 
      $due_by =  $d['due_by'];
$due_by = $due_by + $temp;
$_due_by=$temp;
    }
    else{
      $this->loadModel('Member');
      $query = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0,'Member.mem_no'=>$mem_no]] );
      $d  = $query->first(); 
      $due_by =  $d['due_by'];
      $_due_by=0;
    }   

    if($loan_os<=0){
        $loan_principle_amt=0;
        $loan_os=0;
        
      }
      else{
        $loan_principle_amt=$loan_pri;
        
      }
      if($f_loan_os<=0){
        $f_pri=0;
        $f_loan_os=0;
        
      }
      else{
        $f_pri=$f_loan_pri;
        
      }
    $loan = TableRegistry::get("Loan");
    $query = $loan->query();
                $result = $query->update()
                        ->set(['loan_os_amt' => $loan_os,'loan_principle_amt'=>$loan_principle_amt])
                        ->where(['mem_no' => $this->request->getData('mem_no')])
                        ->execute();
                        
                        $member = TableRegistry::get("Member");
                        $query = $member->query();
                                    $result = $query->update()
                                            ->set(['old_due_int' => $_old_due_int,'old_due_pri'=>$_old_due_pri,'due_by'=>$due_by])
                                            ->where(['mem_no' => $this->request->getData('mem_no')])
                                            ->execute();

                            $this->loadModel('Payment');
    $os_rno = $this->Payment->find()
->select(['os_rno' => 'MAX(Payment.os_rno)']);
$os_rno = $os_rno->first();
$os_rno= $os_rno->os_rno;
$this->loadModel('Payment');
                                    $rec_no = $this->Payment->find()
                                ->select(['rec_no' => 'MAX(Payment.rec_no)']);
                                $rec_no = $rec_no->first();
                                $re = $rec_no->rec_no; 
                           $this->loadModel('Payment');
                $pay = TableRegistry::get("Payment");
                $query = $pay->query();
                            $result = $query->insert(['mem_no', 'os_pay','last_int','last_principal','tot_pay','paid_on','os_rno','due_by','rec_no'])
                            ->values([
                                'mem_no' => $this->request->getData('mem_no') ,
                                'os_pay' => $os_pay,
                                'last_int' => $old_due_int,
                                'last_principal' => $old_due_pri,
                                'tot_pay' => $loan_os_amt,
                                'paid_on'=>date("Y-m-d"),
                                'os_rno' => $os_rno+1,
                                'due_by' => $_due_by,
                                'rec_no' => $re+1
                            ])
                                    ->execute();
                                  
    return $this->redirect(['action' => 'index']);
    }
}
public function delete($mem_no)
{
    $this->request->allowMethod(['post', 'delete']);
    $loan = $this->Loan->findByMem_no($mem_no)->firstOrFail();
    $this->Loan->updateAll(
        array('is_deleted' => "1"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'index']);
}
}
?>