Example #1
0
    public function upDateLoanDisburse($data){
    	$db = $this->getAdapter();
//     	$db->beginTransaction();
//     	try{
    		$datagroup = array(
    				'group_id'=>$data['group_id'],
    				'co_id'=>$data['co_id'],
    				'zone_id'=>$data['zone'],
    				'level'=>$data['level'],
    				'date_release'=>$data['release_date'],
    				'date_line'=>$data['date_line'],
    				'create_date'=>date("Y-m-d"),
    				'branch_id'=>$data['branch_id'],
    				'total_duration'=>$data['period'],
    				'first_payment'=>$data['first_payment'],
    				'time_collect'=>$data['time'],
    				'pay_term'=>$data['pay_every'],
    				'payment_method'=>$data['repayment_method'],
    				'holiday'=>$data['every_payamount'],
    				'is_renew'=>0,
    				'loan_type'=>2,
    				'collect_typeterm'=>$data['collect_termtype']
    		);
    		$g_id = $data['id'];//add group loan
    		$where = $db->quoteInto('g_id=?', $g_id);
    		$this->update($datagroup, $where);
    		unset($datagroup);
    		
    		$this->_name='ln_loan_member';//update member 
    		$arr =array('status'=>0);
    		$where = $db->quoteInto('group_id=?', $g_id);
    		$this->update($arr, $where);
    		
    		$rows= $this->getAllMemberLoanById($g_id);
    		$s_where = array();
    		$where = '';
    		foreach ($rows as $id => $row){
					$s_where[] = "`member_id` = ".$row['member_id'];
    		}
    		$where .= implode(' OR ',$s_where);
    		
    		$this->_name='ln_loanmember_funddetail';//update schedule detail of member 
    		$this->update($arr, $where);
    		
    		$this->_name='ln_loan_member';//update member
    		$next_payment = $data['first_payment'];
    		$start_date = $data['release_date'];//loan release;
    		$curr_type = $data['currency_type'];
    		
    		$tranlist = explode(',',$data['record_row']);
    		$dbtable = new Application_Model_DbTable_DbGlobal();
    		foreach ($tranlist as $i) {
    			$this->_name='ln_loan_member';//update member
    			$loan_number = $dbtable->getLoanNumber();
    			$datamember = array(
    					'group_id'=>$g_id,
    					'loan_number'=>$loan_number,
    					'client_id'=>$data['member_id'.$i],
    					'payment_method'=>$data['repayment_method'],
    					'currency_type'=>$data['currency_type'],
    					'admin_fee'=>$data['admin_fee'.$i],
    					'total_capital'=>$data['debt_amount'.$i],//$data[''],
    					'interest_rate'=>$data['interest_rate'],
    					'branch_id'=>$data['branch_id'],
    					'pay_before'=>$data['pay_before'],
    					'pay_after'=>$data['pay_late'],
    					'graice_period'=>$data['graice_pariod'],
    					'amount_collect_principal'=>$data['amount_collect'],
    					'collect_typeterm'=>$data['collect_termtype'],
    					'loan_purpose'=>$data['note'.$i],
    					'semi'=>$data['amount_collect_pricipal']
    			);
    			 
    			$member_id = $this->insert($datamember);//add member loan
    			unset($datamember);
    			$old_remain_principal = 0;
    			$old_pri_permonth = 0;
    			$old_interest_paymonth = 0;
    			$old_amount_day = 0;
    			$amount_collect = 1;
    			$ispay_principal=2;//for payment type = 5;
    			$is_subremain = 2;
    			$remain_principal = $data['debt_amount'.$i];
    			 
    			 
    			$this->_name='ln_loanmember_funddetail';
    			 
    			$borrow_term = $dbtable->getSubDaysByPaymentTerm($data['pay_every'],null);//return amount day for payterm
    			$amount_borrow_term = $borrow_term*$data['period'];//amount of borrow
    			 
    			$fund_term = $dbtable->getSubDaysByPaymentTerm($data['collect_termtype'],null);//return amount day for payterm
    			$amount_fund_term = $fund_term*$data['amount_collect'];
    			 
    			$loop_payment = ($amount_borrow_term)/($amount_fund_term);
    			$payment_method = $data['repayment_method'];
    			
    			$this->_name='ln_loanmember_funddetail';
    			for($j=1;$j<=$loop_payment;$j++){
    				//return amount next day collection
    				$amount_collect = $data['amount_collect'];
    				$day_perterm = $dbtable->getSubDaysByPaymentTerm($data['collect_termtype'],$amount_collect);//return amount day for payterm
    
    				//$day_perterm = $dbtable->getSubDaysByPaymentTerm($data['pay_every'],$amount_collect);//return amount day for payterm
    				$str_next = $dbtable->getNextDateById($data['collect_termtype'],$data['amount_collect']);//for next,day,week,month;
    
    				if($payment_method==1){//decline//completed
    					//     					$pri_permonth = ($data['debt_amount'.$j]/($data['period']-$data['graice_pariod'])*$amount_collect);
    					$pri_permonth = $data['debt_amount'.$i]/(($amount_borrow_term-($data['graice_pariod']*$borrow_term))/$amount_fund_term);
    					$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
    						
    					if($j*$amount_collect<=$data['graice_pariod']){//check here//for graice period
    						$pri_permonth = 0;
    					}
    					if($j!=1){
    						if($data['graice_pariod']!=0){//if collect =1 not other check
    							if($j*$amount_collect>$data['graice_pariod']+$amount_collect){//not wright
    								$remain_principal = $remain_principal-$pri_permonth;
    							}else{
    
    							}
    						}else{
    							$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា}
    						}
    						if($j==$loop_payment){//check condition here//for end of record only
    							$pri_permonth = $data['debt_amount'.$i]-$pri_permonth*($j-(($data['graice_pariod']/$amount_collect)+1));//code error here
    						}
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    						$interest_paymonth = $remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
    							
    					}else{
    						$next_payment = $data['first_payment'];
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    						$interest_paymonth = ($data['debt_amount'.$i])*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
    					}
    				}elseif($payment_method==2){//baloon
    					$pri_permonth=0;
    					if(($j*$amount_fund_term)==$amount_borrow_term){//end record//if j == loop
    						$pri_permonth = $data['debt_amount'.$i];
    						$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    							
    					}
    					if($j!=1){
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    						//     						$interest_paymonth = $data['debt_amount'.$i]*($data['interest_rate']/100)*($amount_day/$day_perterm);
    					}else{
    						$next_payment = $data['first_payment'];//$dbtable->getNextPayment($str_next, $start_date, $data['amount_collect'],$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    					}
    					$interest_paymonth = $data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
    				}elseif($payment_method==3){//fixed rate
    					$pri_permonth = ($data['debt_amount'.$i]/($amount_borrow_term/$amount_fund_term));
    					$pri_permonth =$this->round_up_currency($curr_type,$pri_permonth);
    						
    					if($j!=1){
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    						if($i==$loop_payment){//for end of record only
    							$pri_permonth = $remain_principal;
    						}
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    					}else{
    						$next_payment = $data['first_payment'];
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    					}
    					$interest_paymonth = $data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm);
    
    				}elseif($payment_method==4){//fixed payment full last period yes
    					if($j!=1){
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    
    						$interest_paymonth = $data['debt_amount'.$i]*($data['interest_rate']/100)*($amount_day/$day_perterm);
    							
    					}else{
    						$next_payment = $data['first_payment'];
    					}
    					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    					$interest_paymonth = $remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
    					$interest_paymonth = $this->round_up_currency($curr_type, $interest_paymonth);
    						
    					$pri_permonth = $data['amount_collect_pricipal']-$interest_paymonth;//don't understand
    					if($j==$loop_payment){//for end of record only
    						$pri_permonth = $remain_principal;
    					}
    				}elseif($payment_method==5){//semi baloon//ok
    					if($j!=1){
    						$ispay_principal++;
    						$is_subremain++;
    						$pri_permonth = ($data['debt_amount'.$i]/$data['period'])*$data['amount_collect_pricipal'];
    						$pri_permonth = ($curr_type==1)?round($pri_permonth,-2):$pri_permonth;
    						$pri_permonth=0;
    						if(($is_subremain-1)==$data['amount_collect_pricipal']){
    							$pri_permonth = ($data['debt_amount'.$i]/$data['period'])*$data['amount_collect_pricipal'];
    							$is_subremain=1;
    						}
    						if(($ispay_principal-1)==$data['amount_collect_pricipal']+1){
    							$remain_principal = $remain_principal-($data['debt_amount'.$i]/$data['period'])*$data['amount_collect_pricipal'];
    							$ispay_principal=2;
    						}
    						if($j==($data['period']/$data['amount_collect'])){//check condition here//for end of record only
    							$pri_permonth = ($data['debt_amount'.$i]/$data['period'])*$data['amount_collect_pricipal'];
    							$pri_permonth = $data['debt_amount'.$i]-$pri_permonth*($j-(($data['graice_pariod']/$amount_collect)+1));//code error here
    						}
    							
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    						$interest_paymonth = $remain_principal*($data['interest_rate']/100)*($amount_day/$day_perterm);
    							
    					}else{
    						$pri_permonth = 0;//check if get pri first too much change;
    						$next_payment = $data['first_payment'];
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    						$interest_paymonth = $data['debt_amount'.$i]*($data['interest_rate']/100)*($amount_day/$day_perterm);
    					}
    				}else{//    fixed payment with fixed rate
    					$pri_permonth = $data['debt_amount'.$i]/$data['period']*$amount_collect;
    					$pri_permonth =$this->round_up_currency($curr_type, $pri_permonth);
    					if($j!=1){
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    						$start_date = $next_payment;
    						if($j==$loop_payment){//check condition here//for end of record only
    							$pri_permonth = $remain_principal;
    						}
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    					}else{
    						$next_payment = $data['first_payment'];
    						$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
    					}
    					$interest_paymonth = ($data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)/$data['period']*($day_perterm/$day_perterm));
    				}
    				$old_remain_principal =$old_remain_principal+$remain_principal;
    				$old_pri_permonth = $old_pri_permonth+$pri_permonth;
    
    				$old_interest_paymonth = $old_interest_paymonth+$interest_paymonth;
    				$old_amount_day =$old_amount_day+ $amount_day;
    
    				if($data['amount_collect']==$amount_collect){
    						
    					$datapayment = array(
    							'member_id'=>$member_id,
    							'total_principal'=>$remain_principal,//good
    							'principal_permonth'=> $old_pri_permonth,//good
    							'total_interest'=>$old_interest_paymonth,//good
    							'total_payment'=>$old_pri_permonth+$old_interest_paymonth,//good
    							'date_payment'=>$next_payment,//good
    							'is_completed'=>0,
    							'branch_id'=>1,
    							'status'=>1,
    							'amount_day'=>$old_amount_day,
    					);
    					$this->insert($datapayment);
    					$amount_collect=0;
    					$old_remain_principal = 0;
    					$old_pri_permonth = 0;
    					$old_interest_paymonth = 0;
    					$old_amount_day = 0;
    						
    				}else{
    						
    				}
    				$amount_collect++;
    			}
    			if(($amount_borrow_term)%($amount_fund_term)!=0){///end for record odd number only
    				$start_date = $next_payment;//$dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    				$amount_day = $amount_day = $dbtable->CountDayByDate($start_date,$data['date_line']);
    				if($payment_method==1){
    					$pri_permonth =$remain_principal-$pri_permonth; // $pri_permonth*($amount_day/$amount_fund_term);//check it if khmer currency
    					$pri_permonth = ($curr_type==1)?round($pri_permonth,-2):$pri_permonth;
    					$interest_paymonth = ($pri_permonth*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
    				}elseif($payment_method==2){
    					$pri_permonth = ($curr_type==1)?round($data['total_amount'],-2):$data['debt_amount'.$i];
    					$pri_permonth = ($curr_type==1)? round($pri_permonth,-2): round($pri_permonth);
    					$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    					$interest_paymonth = ($data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
    				}elseif($payment_method==3){
    					$pri_permonth = $remain_principal-$pri_permonth;
    					$interest_paymonth = ($data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm));
    					$pri_permonth = ($curr_type==1)? round($pri_permonth,-2): round($pri_permonth);
    						
    				}elseif($payment_method==4){
    					$interest_paymonth = ($data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
    					$pri_permonth = $remain_principal-$pri_permonth;
    				}elseif($payment_method==5){
    					$pri_permonth = $remain_principal;
    					$interest_paymonth = ($remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
    				}elseif($payment_method==6){
    					$pri_permonth = $pri_permonth*($amount_day/$amount_fund_term);
    					$interest_paymonth = ($data['debt_amount'.$i]*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
    						
    				}
    
    				$datapayment = array(
    						'member_id'=>$member_id,
    						'total_principal'=>$pri_permonth,//good
    						'principal_permonth'=> $pri_permonth,//good
    						'total_interest'=>$interest_paymonth,//good
    						'total_payment'=>$interest_paymonth+$pri_permonth,//good
    						'date_payment'=>$data['date_line'],//good
    						'is_completed'=>0,
    						'branch_id'=>1,
    						'status'=>1,
    						'amount_day'=>$amount_day,
    				);
    				$this->insert($datapayment);
    			}
    		}
// exit();
//     		$db->commit();
//     		return 1;
//     	}catch (Exception $e){
//     		$db->rollBack();
//     		echo $e->getMessage();exit();
//     		return $e->getMessage();
//     	}
    }
Example #2
0
 public function addNewLoanILTest($data){
 	$db = $this->getAdapter();
 	$db->beginTransaction();
 	try{
 		
 		$datagroup = array(
 				'group_id'=>$data['member'],
 				'co_id'=>$data['co_id'],
 				'zone_id'=>$data['zone'],
 				'level'=>$data['level'],
 				'date_release'=>$data['release_date'],
 				'date_line'=>$data['date_line'],
 				'create_date'=>date("Y-m-d"),
 				'branch_id'=>$data['branch_id'],
 				'total_duration'=>$data['period'],
 				'first_payment'=>$data['first_payment'],
 				'time_collect'=>$data['time'],
 				'pay_term'=>$data['pay_every'],
 				'payment_method'=>$data['repayment_method'],
 				'holiday'=>$data['every_payamount'],
 				'is_renew'=>0,
 				'loan_type'=>1,
 				'collect_typeterm'=>$data['collect_termtype']
 		);
 		
 		$g_id = $this->insert($datagroup);//add group loan
 		
 		unset($datagroup);
 		if(empty($data['loan_code'])){
 			$data['loan_code']=$data['get_laonnumber'];
 		}
 		$datamember = array(
 				'group_id'=>$g_id,
 				'loan_number'=>$data['loan_code'],
 				'client_id'=>$data['member'],
 				'payment_method'=>$data['repayment_method'],
 				'currency_type'=>$data['currency_type'],
 				'total_capital'=>$data['total_amount'],//$data[''],
 				'admin_fee'=>$data['loan_fee'],
 				'other_fee'=>$data['other_fee'],
 				'interest_rate'=>$data['interest_rate'],
 				'status'=>1,
 				'is_completed'=>0,
 				'branch_id'=>$data['branch_id'],
 				//'pay_before'=>$data['pay_before'],
 				'pay_after'=>$data['pay_late'],
 				'graice_period'=>$data['graice_pariod'],
 				'amount_collect_principal'=>$data['amount_collect'],
 				'collect_typeterm'=>$data['collect_termtype'],
 				'loan_number'=>$data['loan_code'],
 				'semi'=>$data['amount_collect_pricipal']
 		);
 		$this->_name='ln_test_loan_member';
 		$member_id = $this->insert($datamember);//add member loan
 		unset($datamember);
 		 
 		$remain_principal = $data['total_amount'];
 		$next_payment = $data['first_payment'];
 		$start_date = $data['release_date'];//loan release;
 		//     			$next_payment = $start_date;
 		
 		 
 		$old_remain_principal = 0;
 		$old_pri_permonth = 0;
 		$old_interest_paymonth = 0;
 		$old_amount_day = 0;
 		$amount_collect = 1;
 		$ispay_principal=2;//for payment type = 5;
 		$is_subremain = 2;
 		$curr_type = $data['currency_type'];
 		
 		//for IRR method
 		if($data['repayment_method']==6){
 		$term_install = $data['period'];
 		$loan_amount = $data['total_amount'];
 		$total_loan_amount = $loan_amount+($loan_amount*$data['interest_rate']/100*$term_install);
 		$irr_interest = $this->calCulateIRR($total_loan_amount,$loan_amount,$term_install,$curr_type);
 		//end of IRR
 		}
 		
 		$this->_name='ln_test_loanmember_funddetail';
 		$dbtable = new Application_Model_DbTable_DbGlobal();
 		$borrow_term = $dbtable->getSubDaysByPaymentTerm($data['pay_every'],null);//return amount day for payterm
 		$amount_borrow_term = $borrow_term*$data['period'];//amount of borrow
 		 
 		$fund_term = $dbtable->getSubDaysByPaymentTerm($data['collect_termtype'],null);//return amount day for payterm
 		$amount_fund_term = $fund_term*$data['amount_collect'];
 		 
 		$loop_payment = ($amount_borrow_term)/($amount_fund_term);
 		$payment_method = $data['repayment_method'];
 		//     			for($i=1;$i<=($data['period']/$data['amount_collect']);$i++){
 		for($i=1;$i<=$loop_payment;$i++){
 			$amount_collect = $data['amount_collect'];
 			$day_perterm = $dbtable->getSubDaysByPaymentTerm($data['collect_termtype'],$amount_collect);//return amount day for payterm
 			$str_next = $dbtable->getNextDateById($data['collect_termtype'],$data['amount_collect']);//for next,day,week,month;
 		
 			if($payment_method==1){//decline//completed
 				//     					$pri_permonth = ($data['total_amount']/($data['period']-$data['graice_pariod'])*$amount_collect);
 				$pri_permonth = $data['total_amount']/(($amount_borrow_term-($data['graice_pariod']*$borrow_term))/$amount_fund_term);
 				$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
 				if($i*$amount_collect<=$data['graice_pariod']){//check here//for graice period
 					$pri_permonth = 0;
 				}
 				if($i!=1){
 					if($data['graice_pariod']!=0){//if collect =1 not other check
 						if($i*$amount_collect>$data['graice_pariod']+$amount_collect){//not wright
 							$remain_principal = $remain_principal-$pri_permonth;
 						}else{
 		
 						}
 					}else{
 						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា}
 					}
 					if($i==$loop_payment){//check condition here//for end of record only
 						//     							echo $remain_principal;
 						$pri_permonth = $data['total_amount']-$pri_permonth*($i-(($data['graice_pariod']/$amount_collect)+1));//code error here
 							
 					}
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 		
 					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
 					//     						$interest_paymonth = $remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
 		
 				}else{
 					$next_payment = $data['first_payment'];
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
 					//     						$interest_paymonth = $data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
 				}
 			}elseif($payment_method==2){//baloon
 				$pri_permonth=0;
 				if(($i*$amount_fund_term)==$amount_borrow_term){//check here
 					$pri_permonth = ($curr_type==1)?round($data['total_amount'],-2):$data['total_amount'];
 					$pri_permonth =$this->round_up_currency($curr_type, $pri_permonth);
 					$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
 				}
 				if($i!=1){
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 				}else{
 					$next_payment = $data['first_payment'];
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 				}
 				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = $data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm);
 					
 			}elseif($payment_method==3){//fixed rate
 				$pri_permonth = ($data['total_amount']/($amount_borrow_term/$amount_fund_term));
 				$pri_permonth =$this->round_up_currency($curr_type,$pri_permonth);
 				if($i!=1){
 					$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
 					if($i==$loop_payment){//for end of record only
 						$pri_permonth = $remain_principal;
 					}
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 				}else{
 					$next_payment = $data['first_payment'];
 				}
 				$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					    $interest_paymonth = $data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm);
 		
 			}elseif($payment_method==4){//fixed payment full last period yes
 				if($i!=1){
 					$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 					//     						$interest_paymonth = $data['total_amount']*($data['interest_rate']/100)*($amount_day/$day_perterm);
 						
 				}else{
 					$next_payment = $data['first_payment'];
 				}
 				$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 				$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				$interest_paymonth = $this->round_up_currency($curr_type, $interest_paymonth);
 				$pri_permonth = $data['amount_collect_pricipal']-$interest_paymonth;
 				if($i==$loop_payment){//for end of record only
 					$pri_permonth = $remain_principal;
 				}
 			}elseif($payment_method==5){//semi baloon//ok
 				if($i!=1){
 					$ispay_principal++;
 					$is_subremain++;
 					$pri_permonth=0;
 					if(($is_subremain-1)==$data['amount_collect_pricipal']){
 						$pri_permonth = ($data['total_amount']/$data['period'])*$data['amount_collect_pricipal'];
 						$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
 						$is_subremain=1;
 					}
 					if(($ispay_principal-1)==$data['amount_collect_pricipal']+1){
 						$remain_principal = $remain_principal-($data['total_amount']/$data['period'])*$data['amount_collect_pricipal'];
 						$ispay_principal=2;
 					}
 					if($i==$loop_payment){//check condition here//for end of record only
 						$pri_permonth = $remain_principal;
 					}
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
 					//     							$interest_paymonth = ($remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm));
 				}else{
 					$pri_permonth = 0;//check if get pri first too much change;
 					$next_payment = $data['first_payment'];
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 					$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				}
 			}else{//    fixed payment IRR
 				if($i!=1){
 					$start_date = $next_payment;
 					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 					
 					$remain_principal = $remain_principal-$pri_permonth;
 					$interest_paymonth = $this->round_up_currency($curr_type,$remain_principal*$irr_interest);
 					$fixed_principal = intval($total_loan_amount/$term_install);
 					$fixed_principal= $this->round_up_currency($curr_type,$fixed_principal);
 					$pri_permonth = $fixed_principal-$interest_paymonth;
 					if($i==$loop_payment){//for end of record only
 						   $pri_permonth = $remain_principal;
 						   $fixed_principal = intval($total_loan_amount/$term_install);
 						   $fixed_principal= $this->round_up_currency($curr_type,$fixed_principal);
 						   $interest_paymonth = $fixed_principal-$remain_principal;
 						   	
 					}
 					
 				}else{
 					//$fixed_principal = round($total_loan_amount/$term_install,0, PHP_ROUND_HALF_DOWN);//fixed '
 					$fixed_principal = intval($total_loan_amount/$term_install);//fixed 'ex: 100.70=>100
 					$fixed_principal= $this->round_up_currency($curr_type,$fixed_principal);
 					$post_fiexed = $total_loan_amount/$term_install-$fixed_principal;
 					$total_payment_first = $this->round_up_currency($curr_type,$post_fiexed*$term_install);
 					$pri_permonth = $fixed_principal+$total_payment_first;
 					
 					$amount_day = $dbtable->CountDayByDate($start_date,$next_payment);
 					$interest_paymonth = $this->round_up_currency($curr_type,$loan_amount*($irr_interest));
 					$pri_permonth = ($fixed_principal+$total_payment_first)-$interest_paymonth;
 				}
 			}
 			$old_remain_principal =$old_remain_principal+$remain_principal;
 			$old_pri_permonth = $old_pri_permonth+$pri_permonth;
 			$old_interest_paymonth = $this->round_up_currency($curr_type,($old_interest_paymonth+$interest_paymonth));
 			$old_amount_day =$old_amount_day+ $amount_day;
 		
 		
 			if($data['amount_collect']==$amount_collect){
 					
 				$datapayment = array(
 						'member_id'=>$member_id,
 						'total_principal'=>$remain_principal,//good
 						'principal_permonth'=> $old_pri_permonth,//good
 						'total_interest'=>$old_interest_paymonth,//good
 						'total_payment'=>$old_pri_permonth+$old_interest_paymonth,//good
 						'date_payment'=>$next_payment,//good
 						'is_completed'=>0,
 						'branch_id'=>$data['branch_id'],
 						'status'=>1,
 						'amount_day'=>$old_amount_day,
 						'collect_by'=>$data['co_id']
 				);
 				$this->insert($datapayment);
 				$amount_collect=0;
 				$old_remain_principal = 0;
 				$old_pri_permonth = 0;
 				$old_interest_paymonth = 0;
 				$old_amount_day = 0;
 					
 			}else{
 					
 			}
 			$amount_collect++;
 		}
 		if(($amount_borrow_term)%($amount_fund_term)!=0){///end for record odd number only
 			$start_date = $next_payment;//$dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
 			$amount_day = $amount_day = $dbtable->CountDayByDate($start_date,$data['date_line']);
 			if($payment_method==1){
 				$pri_permonth = $remain_principal-$pri_permonth; // $pri_permonth*($amount_day/$amount_fund_term);//check it if khmer currency
 				$interest_paymonth = $pri_permonth*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = ($pri_permonth*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 			}elseif($payment_method==2){
 				//     					$pri_permonth = ($curr_type==1)?round($data['total_amount'],-2):$data['total_amount'];
 				//     					$pri_permonth = ($curr_type==1)? round($pri_permonth,-2): round($pri_permonth);
 				$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
 				$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
 				$interest_paymonth = $pri_permonth*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = ($data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 			}elseif($payment_method==3){
 				$pri_permonth = $remain_principal-$pri_permonth;
 				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = ($data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm));
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 				//     					$pri_permonth = $this->round_up_currency($curr_type,$pri_permonth);
 			}elseif($payment_method==4){
 				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = ($data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 				$pri_permonth = $remain_principal-$pri_permonth;
 			}elseif($payment_method==5){
 				$pri_permonth = $remain_principal;
 				$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				//     					$interest_paymonth = ($remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($amount_day/$day_perterm));
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 			}elseif($payment_method==6){
 				$pri_permonth = $remain_principal;
 				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
 				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
 			}
 		
 			$datapayment = array(
 					'member_id'=>$member_id,
 					'total_principal'=>$pri_permonth,//good
 					'principal_permonth'=> $pri_permonth,//good
 					'total_interest'=>$interest_paymonth,//good
 					'total_payment'=>$interest_paymonth+$pri_permonth,//good
 					'date_payment'=>$data['date_line'],//good
 					'is_completed'=>0,
 					'branch_id'=>$data['branch_id'],
 					'status'=>1,
 					'amount_day'=>$amount_day,
 					'collect_by'=>$data['co_id']
 			);
 			$this->insert($datapayment);
 		
 		}
 		$sql = "SELECT f.* , DATE_FORMAT(f.date_payment, '%d-%m-%Y') AS date_payments FROM ln_test_loanmember_funddetail AS f  WHERE member_id = ".$member_id;
 		$rows =  $db->fetchAll($sql);
 		$db->commit();
 		return $rows;
 	}catch (Exception $e){
 		$db->rollBack();
 		Application_Form_FrmMessage::message("INSERT_FAIL");
 		Application_Model_DbTable_DbUserLog::writeMessageError($e->getMessage());
 	}
 } 
Example #3
0
    function updateLoanById($data){
    	$db = $this->getAdapter();
    	$db->beginTransaction();
    	try{
    		if($data['status_using']==0){
    			$arr_update = array(
    					'status'=>0
    			);
    			$where = ' status = 1 AND g_id = '.$data['id'];
    			$this->update($arr_update, $where);
    			
    			$this->_name = 'ln_loan_member';
    			$where = ' is_completed = 0 AND status = 1 AND group_id = '.$data['id'].' AND client_id = '.$data["member"];
    			$this->update($arr_update, $where);
    			
    			
    			$rows= $this->getAllMemberLoanById($data['id']);
    			$s_where = array();
    			$where = '';
    			foreach ($rows as $id => $row){
    				$s_where[] = "`member_id` = ".$row['member_id'];
    			}
    			$where .= implode(' OR ',$s_where);
    			$where.=" AND status=1 AND is_completed=0 ";
    			
    			$arr = array(
    					'status'=>0
    			);
    			$this->_name='ln_loanmember_funddetail';
    			
    			$db->commit();
    			return 1;
    		}
    		
    		$datagroup = array(
    				'group_id'=>$data['member'],
    				'co_id'=>$data['co_id'],
    				'zone_id'=>$data['zone'],
    				'level'=>$data['level'],
    				'date_release'=>$data['release_date'],
    				'date_line'=>$data['date_line'],
    				'create_date'=>date("Y-m-d"),
    				'branch_id'=>$data['branch_id'],
    				'total_duration'=>$data['period'],
    				'first_payment'=>$data['first_payment'],
    				'time_collect'=>$data['time'],
    				'pay_term'=>$data['pay_every'],
    				'payment_method'=>$data['repayment_method'],
    				'holiday'=>$data['every_payamount'],
    				'is_renew'=>0,
    				'loan_type'=>1,
    				'collect_typeterm'=>$data['collect_termtype'],
    				'user_id'=>$this->getUserId()
    		);
    		$g_id = $data['id'];
    		$where = $db->quoteInto('g_id=?', $g_id);
    		$this->update($datagroup, $where);
    		unset($datagroup);
    		
    		$datamember = array(
    				'group_id'=>$g_id,
    				'loan_number'=>$data['loan_code'],
    				'client_id'=>$data['member'],
    				'payment_method'=>$data['repayment_method'],
    				'currency_type'=>$data['currency_type'],
    				'total_capital'=>$data['total_amount'],//$data[''],
    				'other_fee'=>$data['other_fee'],
    				'admin_fee'=>$data['loan_fee'],
    				'interest_rate'=>$data['interest_rate'],
    				'status'=>1,
    				'is_completed'=>0,
    				'branch_id'=>$data['branch_id'],
    				//'pay_before'=>$data['pay_before'],
    				'pay_after'=>$data['pay_late'],
    				'graice_period'=>$data['graice_pariod'],
    				'amount_collect_principal'=>$data['amount_collect'],
    				'collect_typeterm'=>$data['collect_termtype'],
    				'loan_number'=>$data['loan_code'],
    				'semi'=>$data['amount_collect_pricipal']
    		);
    		$this->_name='ln_loan_member';
    		
    		$where = $db->quoteInto('group_id=?', $data['id']);
    		$this->update($datamember, $where);
    		unset($datamember);
    		
    		$rows= $this->getAllMemberLoanById($data['id']);
    		$s_where = array();
    		$where = '';
    		foreach ($rows as $id => $row){
    			$s_where[] = "`member_id` = ".$row['member_id'];
    		}
    		$where .= implode(' OR ',$s_where);
    		$where.=" AND status=1 AND is_completed=0 ";
    				
    		$arr = array(
    				'status'=>0
    				);
    		$this->_name='ln_loanmember_funddetail';
//     		$where = $db->quoteInto('member_id=?', $data['id']);
    		$this->delete($where);

    		$remain_principal = $data['total_amount'];
    		$next_payment = $data['first_payment'];
    		$start_date = $data['release_date'];//loan release;
    		$from_date = $data['release_date'];
    		 
    		$old_remain_principal = 0;
    		$old_pri_permonth = 0;
    		$old_interest_paymonth = 0;
    		$old_amount_day = 0;
    		$amount_collect = 1;
    		$ispay_principal=2;//for payment type = 5;
    		$is_subremain = 2;
    		$curr_type = $data['currency_type'];
    		
    		//for IRR method
    		$term_install = $data['period'];
    		$loan_amount = $data['total_amount'];
    		$total_loan_amount = $loan_amount+($loan_amount*$data['interest_rate']/100*$term_install);
    		$irr_interest = $this->calCulateIRR($total_loan_amount,$loan_amount,$term_install,$curr_type);
    		//end of IRR
    		 
    		$this->_name='ln_loanmember_funddetail';
    		$dbtable = new Application_Model_DbTable_DbGlobal();
    		$borrow_term = $dbtable->getSubDaysByPaymentTerm($data['pay_every'],null);//return amount day for payterm
    		$amount_borrow_term = $borrow_term*$data['period'];//amount of borrow
    		 
    		$fund_term = $dbtable->getSubDaysByPaymentTerm($data['collect_termtype'],null);//return amount day for payterm
    		$amount_fund_term = $fund_term*$data['amount_collect'];
    		 
    		$loop_payment = ($amount_borrow_term)/($amount_fund_term);
    		$payment_method = $data['repayment_method'];
    		//     			for($i=1;$i<=($data['period']/$data['amount_collect']);$i++){
    		for($i=1;$i<=$loop_payment;$i++){
    			$amount_collect = $data['amount_collect'];
    			$str_next = $dbtable->getNextDateById($data['collect_termtype'],$data['amount_collect']);//for next,day,week,month;
    	
    			if($payment_method==1){//decline//completed
    				//     					$pri_permonth = ($data['total_amount']/($data['period']-$data['graice_pariod'])*$amount_collect);
    				$pri_permonth = $data['total_amount']/(($amount_borrow_term-($data['graice_pariod']*$borrow_term))/$amount_fund_term);
    				$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
    				if($i*$amount_collect<=$data['graice_pariod']){//check here//for graice period
    					$pri_permonth = 0;
    				}
    				if($i!=1){
    					if($data['graice_pariod']!=0){//if collect =1 not other check
    						if($i*$amount_collect>$data['graice_pariod']+$amount_collect){//not wright
    							$remain_principal = $remain_principal-$pri_permonth;
    						}else{
    				
    						}
    					}else{
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា}
    					}
    					if($i==$loop_payment){//check condition here//for end of record only
    						$pri_permonth = $data['total_amount']-$pri_permonth*($i-(($data['graice_pariod']/$amount_collect)+1));//code error here
    					}
    					$start_date = $next_payment;
    					$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    					$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				
    				}else{
    					$next_payment = $data['first_payment'];
    					$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    					$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				}
    			}elseif($payment_method==2){//baloon
    					$pri_permonth=0;
    					if(($i*$amount_fund_term)==$amount_borrow_term){//check here
    						$pri_permonth = ($curr_type==1)?round($data['total_amount'],-2):$data['total_amount'];
    						$pri_permonth =$this->round_up_currency($curr_type, $pri_permonth);
    						$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    					}
    					if($i!=1){
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    						$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					}else{
    						$next_payment = $data['first_payment'];
    						$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					}
    					$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
    					
    				}elseif($payment_method==3){//fixed rate
    					$pri_permonth = ($data['total_amount']/($amount_borrow_term/$amount_fund_term));
    					$pri_permonth =$this->round_up_currency($curr_type,$pri_permonth);
    					if($i!=1){
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    						if($i==$loop_payment){//for end of record only
    							$pri_permonth = $remain_principal;
    						}
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    					}else{
    						$next_payment = $data['first_payment'];
    						$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    					}
    					    $amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					    $interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
    						
    				}elseif($payment_method==4){//fixed payment full last period yes
    					if($i!=1){
    						$remain_principal = $remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    						$start_date = $next_payment;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    					}else{
    						$next_payment = $data['first_payment'];
    						$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    					}
    					$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    					$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
    					$interest_paymonth = $this->round_up_currency($curr_type, $interest_paymonth);
    					$pri_permonth = $data['amount_collect_pricipal']-$interest_paymonth;
    					if($i==$loop_payment){//for end of record only
    						$pri_permonth = $remain_principal;
    					}
    				}elseif($payment_method==5){//semi baloon//ok
    					if($i!=1){
    						$ispay_principal++;
    						$is_subremain++;
    						$pri_permonth=0;
								if(($is_subremain-1)==$data['amount_collect_pricipal']){
    								$pri_permonth = ($data['total_amount']/$data['period'])*$data['amount_collect_pricipal'];
    								$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
    								$is_subremain=1;
    							}
    							if(($ispay_principal-1)==$data['amount_collect_pricipal']+1){
    								$remain_principal = $remain_principal-($data['total_amount']/$data['period'])*$data['amount_collect_pricipal'];
    								$ispay_principal=2;
    							}
    							if($i==$loop_payment){//check condition here//for end of record only
    								$pri_permonth = $remain_principal;
    							}
    							$start_date = $next_payment;
    							$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    							$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    							$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
//     							$interest_paymonth = ($remain_principal*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm));
    					}else{
    						$pri_permonth = 0;//check if get pri first too much change;
    						$next_payment = $data['first_payment'];
    						$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    						$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
//     						$interest_paymonth = ($data['total_amount']*((($amount_fund_term*$data['interest_rate'])/$borrow_term)/100)*($day_perterm/$day_perterm));
    					}
    				}else{//    fixed payment with fixed rate
    					if($i!=1){
    						$remain_principal = $remain_principal-$pri_permonth;
    						$next_payment = $dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    						$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    						$interest_paymonth = $this->round_up_currency($curr_type,$remain_principal*$irr_interest);
    						$fixed_principal = round($total_loan_amount/$term_install,0, PHP_ROUND_HALF_DOWN);
    						$pri_permonth = $fixed_principal-$interest_paymonth;
    						if($i==$loop_payment){//for end of record only
    							$pri_permonth = $remain_principal;
    							
    							$fixed_principal = round($total_loan_amount/$term_install,0, PHP_ROUND_HALF_DOWN);
    							$fixed_principal= $this->round_up_currency($curr_type,$fixed_principal);
    							$interest_paymonth = $fixed_principal-$remain_principal;
    						}
    							
    					}else{
    						$fixed_principal = round($total_loan_amount/$term_install,0, PHP_ROUND_HALF_DOWN);//fixed
    						$fixed_principal= $this->round_up_currency($curr_type,$fixed_principal);
    						$post_fiexed = $total_loan_amount/$term_install-$fixed_principal;
    						$total_payment_first = $this->round_up_currency($curr_type,$post_fiexed*$term_install);
    						$pri_permonth = $fixed_principal+$total_payment_first;
    						
    						$next_payment = $dbtable->checkFirstHoliday($next_payment,$data['every_payamount']);
    						$amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    						$interest_paymonth = $this->round_up_currency($curr_type,$loan_amount*($irr_interest));
    						$pri_permonth = ($fixed_principal+$total_payment_first)-$interest_paymonth;
    					}	   	   
    			}
    			$old_remain_principal =$old_remain_principal+$remain_principal;
    			$old_pri_permonth = $old_pri_permonth+$pri_permonth;
    			$old_interest_paymonth = $this->round_up_currency($curr_type,($old_interest_paymonth+$interest_paymonth));
    			$old_amount_day =$old_amount_day+ $amount_day;
    	
    	
    			if($data['amount_collect']==$amount_collect){
    					
    				$datapayment = array(
    						'member_id'=>$g_id,
    						'total_principal'=>$remain_principal,//good
    						'principal_permonth'=> $old_pri_permonth,//good
    						'principle_after'=> $old_pri_permonth,//good
    						'total_interest'=>$old_interest_paymonth,//good
    						'total_interest_after'=>$old_interest_paymonth,//good
    						'total_payment'=>$old_pri_permonth+$old_interest_paymonth,//good
    						'total_payment_after'=>$old_pri_permonth+$old_interest_paymonth,//good
    						'date_payment'=>$next_payment,//good
    						'is_completed'=>0,
    						'branch_id'=>$data['branch_id'],
    						'status'=>1,
    						'amount_day'=>$old_amount_day,
    						'collect_by'=>$data['co_id']
    				);
    				
    				$this->insert($datapayment);
    				$amount_collect=0;
    				$old_remain_principal = 0;
    				$old_pri_permonth = 0;
    				$old_interest_paymonth = 0;
    				$old_amount_day = 0;
    				$from_date=$next_payment;
    				if($i!=1){
    					$next_payment = $dbtable->checkDefaultDate($str_next, $start_date, $data['amount_collect'],$data['every_payamount'],$data['first_payment']);
    				}
    					
    			}else{
    				
    			}
    			$amount_collect++;
    		}
    		if(($amount_borrow_term)%($amount_fund_term)!=0){///end for record odd number only
    			$start_date = $next_payment;//$dbtable->getNextPayment($str_next, $next_payment, $data['amount_collect'],$data['every_payamount']);
    			$next_payment = $dbtable->checkFirstHoliday($data['date_line'],$data['every_payamount']);
    			$amount_day = $amount_day = $dbtable->CountDayByDate($from_date,$next_payment);
    			if($payment_method==1){
    				$pri_permonth = $remain_principal-$pri_permonth; // $pri_permonth*($amount_day/$amount_fund_term);//check it if khmer currency
    				$interest_paymonth = $pri_permonth*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
    			}elseif($payment_method==2){
    				$pri_permonth = $this->round_up_currency($curr_type, $pri_permonth);
    				$remain_principal = $pri_permonth;//$remain_principal-$pri_permonth;//OSប្រាក់ដើមគ្រា
    				$interest_paymonth = $pri_permonth*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
    			}elseif($payment_method==3){
    				$pri_permonth = $remain_principal-$pri_permonth;
    				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
    			}elseif($payment_method==4){
    				$interest_paymonth = $data['total_amount']*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
    				$pri_permonth = $remain_principal-$pri_permonth;
    			}elseif($payment_method==5){
    				$pri_permonth = $remain_principal;
    				$interest_paymonth = $remain_principal*($data['interest_rate']/100/$borrow_term)*$amount_day;
    				$interest_paymonth = $this->round_up_currency($curr_type,$interest_paymonth);
    			}elseif($payment_method==6){
    				$interest_paymonth = $this->round_up_currency($curr_type,$remain_principal*$irr_interest);
    				$fixed_principal = round($total_loan_amount/$term_install,0, PHP_ROUND_HALF_DOWN);
    				$pri_permonth = $remain_principal;
    			}
    			 $datapayment = array(
    					'member_id'=>$g_id,
    					'total_principal'=>$pri_permonth,//good
    					'principal_permonth'=> $pri_permonth,//good
    					'principle_after'=> $pri_permonth,//good
    					'total_interest'=>$interest_paymonth,//good
    					'total_interest_after'=>$interest_paymonth,//good
    					'total_payment'=>$old_pri_permonth+$old_interest_paymonth,//good
    					'total_payment_after'=>$old_pri_permonth+$old_interest_paymonth,//good
    					'date_payment'=>$next_payment,//good
    					'is_completed'=>0,
    					'branch_id'=>$data['branch_id'],
    					'status'=>1,
    					'amount_day'=>$old_amount_day,
    					'collect_by'=>$data['co_id']
    			);
    			$this->insert($datapayment);
    		}
    		$db->commit();
    		return 1;
    	}catch (Exception $e){
    		$db->rollBack();
    		Application_Form_FrmMessage::message("INSERT_FAIL");
    		Application_Model_DbTable_DbUserLog::writeMessageError($e->getMessage());
    	}
    }