function updateGroupPayment($data,$id){ $db = $this->getAdapter(); $db->beginTransaction(); $session_user=new Zend_Session_Namespace('auth'); $user_id = $session_user->user_id; $query = new Application_Model_DbTable_DbGlobal(); //$id= $data["id"]; try{ $arr_client_pay = array( 'co_id' => $data['co_id'], 'group_id' => $data["client_code"], 'receiver_id' => $data['reciever'], 'branch_id' => $data['branch_id'], 'date_input' => $data["date_input"], 'principal_amount' => $data["priciple_amount"], 'total_principal_permonth' => $data["os_amount"], 'total_payment' => $data["total_payment"], 'total_interest' => $data["total_interest"], 'recieve_amount' => $data["amount_receive"], 'penalize_amount' => $data['penalize_amount'], 'return_amount' => $data["amount_return"], 'service_charge' => $data["service_charge"], 'note' => $data['note'], 'user_id' => $user_id, 'is_group' => 1, 'payment_option' => 1, 'currency_type' => $data["currency_type"], 'status' => 1, 'amount_payment' => $data["amount_receive"]-$data["amount_return"], 'is_completed' => 1 ); $this->_name = "ln_client_receipt_money"; $where = $db->quoteInto("id=?", $id); $client_pay = $this->update($arr_client_pay, $where); $loan_fun = $this->getGroupPaymentDetail($id); $sql_delete = "DELETE FROM ln_client_receipt_money_detail WHERE crm_id =$id"; $db->query($sql_delete); if(!empty($loan_fun)){ foreach ($loan_fun as $rs){ $principle = $rs["principal_permonth"]; $interest = $rs["total_interest"]; $recieve_amount = $rs["total_recieve"]; $total_pay = $rs["total_payment"]; $penelize = $rs["penelize_amount"]; $service_charge = $rs["service_charge"]; $db_fun = new Loan_Model_DbTable_DbLoanILPayment(); $fun = $db_fun->getFunDetail($rs["lfd_id"]); foreach ($fun as $row_fun){ if($row_fun["is_completed"]==1){ $arr_fun = array( 'is_completed' => 0, ); $this->_name= "ln_loanmember_funddetail"; $where = $db->quoteInto("id=?", $rs["lfd_id"]); $this->update($arr_fun, $where); }else{ $arr_fun = array( 'principle_after' => $principle, 'total_interest_after' => $interest, 'total_payment_after' => $total_pay, 'penelize' => $penelize, 'service_charge' => $service_charge, ); $this->_name= "ln_loanmember_funddetail"; $where = $db->quoteInto("id=?", $rs["lfd_id"]); $this->update($arr_fun, $where); } } } } $identify = explode(',',$data['identity']); foreach($identify as $i){ $client_detail = $data["mfdid_".$i]; $sub_recieve_amount = $data["sub_recive_amount_".$i]; $sub_service_charge = $data["sub_service_charge_".$i]; $sub_peneline_amount = $data["sub_penelize_".$i]; $sub_interest_amount = $data["sub_interest_".$i]; $sub_principle= $data["sub_principal_permonth_".$i]; $sub_total_payment = $data["sub_payment_".$i]; if($client_detail!=""){ //$reciept_no=$this->getIlPaymentNumber(); if($sub_recieve_amount>=$sub_total_payment){ $arr_money_detail = array( 'crm_id' => $id, 'loan_number' => $data["loan_number_".$i], 'lfd_id' => $data["mfdid_".$i], 'client_id' => $data["client_id_".$i], 'date_payment' => $data["date_payment_".$i], 'capital' => $data["sub_total_priciple_".$i], 'remain_capital' => $data["sub_total_priciple_".$i] - $data["sub_principal_permonth_".$i], 'principal_permonth' => $data["sub_principal_permonth_".$i], 'total_interest' => $data["sub_interest_".$i], 'total_payment' => $data["sub_payment_".$i], 'total_recieve' => $data["sub_recive_amount_".$i], 'currency_id' => $data["cu_id_".$i], 'pay_after' => $data['multiplier_'.$i], 'is_completed' => 0, 'is_verify' => 0, 'verify_by' => 0, 'is_closingentry' => 0, 'status' => 1 ); $db->insert("ln_client_receipt_money_detail", $arr_money_detail); $arr_update_fun_detail = array( 'is_completed' => 1, 'payment_option' => 1 ); $this->_name="ln_loanmember_funddetail"; $where = $db->quoteInto("id=?", $data["mfdid_".$i]); $this->update($arr_update_fun_detail, $where); }else{ $new_sub_interest_amount = $data["sub_interest_".$i]; $new_sub_penelize = $data["sub_penelize_".$i]; $new_sub_service_charge = $data["sub_service_charge_".$i]; $new_sub_principle = $data["sub_principal_permonth_".$i]; if($sub_recieve_amount>0){ $new_amount_after_service = $sub_recieve_amount-$sub_service_charge; if($new_amount_after_service>=0){ $new_sub_service_charge = 0; $new_amount_after_penelize = $new_amount_after_service - $sub_peneline_amount; if($new_amount_after_penelize>=0){ $new_sub_penelize = 0; $new_amount_after_interest = $new_amount_after_penelize - $sub_interest_amount; if($new_amount_after_interest>=0){ $new_sub_interest_amount = 0; $new_sub_principle = $sub_principle - $new_amount_after_interest; }else{ $new_sub_interest_amount = abs($new_amount_after_interest); } }else{ $new_sub_penelize = abs($new_amount_after_penelize); } }else{ $new_sub_service_charge = abs($new_amount_after_service); } } $arr_money_detail = array( 'crm_id' => $id, 'loan_number' => $data["loan_number_".$i], 'lfd_id' => $data["mfdid_".$i], 'client_id' => $data["client_id_".$i], 'date_payment' => $data["date_payment_".$i], 'capital' => $data["sub_total_priciple_".$i], 'remain_capital' => $data["sub_total_priciple_".$i] - $data["sub_principal_permonth_".$i], 'principal_permonth' => $sub_principle, 'total_interest' => $sub_interest_amount, 'total_payment' => $sub_total_payment, 'total_recieve' => $data["sub_recive_amount_".$i], 'currency_id' => $data["cu_id_".$i], 'pay_after' => $data['multiplier_'.$i], 'is_completed' => 0, 'is_verify' => 0, 'verify_by' => 0, 'is_closingentry' => 0, 'status' => 1 ); $db->insert("ln_client_receipt_money_detail", $arr_money_detail); $arr_update_fun_detail = array( 'is_completed' => 0, 'total_interest_after' => $new_sub_interest_amount, 'total_payment_after' => $new_sub_principle, 'penelize' => $new_sub_penelize, 'principle_after' => $new_sub_principle, 'service_charge' => $new_sub_service_charge, 'payment_option' => 1 ); $this->_name="ln_loanmember_funddetail"; $where = $db->quoteInto("id=?", $data["mfdid_".$i]); $this->update($arr_update_fun_detail, $where); } $loan_number = $data["loan_number_".$i]; $sql_payment ="SELECT l.* FROM `ln_loanmember_funddetail` AS l, `ln_loan_member` AS m WHERE l.`member_id` = m.`member_id` AND m.`loan_number` = '$loan_number' AND l.`is_completed` = 0 "; $rs_payment = $db->fetchRow($sql_payment); $group_id = $data["client_code"]; if(empty($rs_payment)){ $sql ="UPDATE `ln_loan_group` AS l SET l.`status` = 2 WHERE l.`g_id`= (SELECT m.`group_id` FROM `ln_loan_member` AS m WHERE m.`loan_number`='$loan_number' LIMIT 1) AND l.`group_id`= $group_id AND l.`loan_type`=1"; $db->query($sql); $sql_loan_memeber ="UPDATE `ln_loan_member` AS m SET m.`is_completed`=1 WHERE m.`loan_number`= '$loan_number'"; $db->query($sql_loan_memeber); } } } $db->commit(); }catch (Exception $e){ $db->rollBack(); echo $e->getMessage(); Application_Model_DbTable_DbUserLog::writeMessageError($e->getMessage()); } }