function save($data) { $this->_name = 'cs_exchange'; $session_user = new Zend_Session_Namespace('auth'); $to_type = $this->getCurrencyById("`name`, `symbol`", $data["to_amount_type"]); $from_type = $this->getCurrencyById("`name`, `symbol`", $data["from_amount_type"]); $status = "in"; if ($data["from_amount_type"] == 2 && $data["to_amount_type"] == 1 || $data["from_amount_type"] == 3) { $status = "out"; } $db = $this->getAdapter(); $db->beginTransaction(); try { $user_id = $session_user->user_id; $_data = array("changedAmount" => $data["return_money"], "recieptNo" => $data["inv_no"], "toAmount" => $data["to_amount"], "toAmountType" => $to_type["symbol"], "rate" => $data["rate"], "fromAmountType" => $from_type["symbol"], "fromAmount" => $data["from_amount"], "recievedType" => $from_type["symbol"], "recievedAmount" => $data["recieve_money"], "statusDate" => date('Y-m-d H:i:s'), "userid" => $user_id, "status" => $status, "specail_customer" => empty($data['special_cus']) ? 0 : 1, "from_to" => $from_type['name'] . " - " . $to_type["name"]); $ex_id = $this->insert($_data); $arr_cd = array('tran_id' => $ex_id, 'tran_type' => 3, 'date' => date('Y-m-d'), 'user_id' => $user_id); $db_cap = new Application_Model_DbTable_DbCapital(); //from amount $currency_type = $data["from_amount_type"]; $amount = $data["from_amount"] - $data["return_money"]; $rs = $db_cap->DetechCapitalExist($user_id, $currency_type, null); //check if add capital exist if (!empty($rs)) { $arr = array('amount' => $rs['amount'] + $amount); $db_cap->updateCurrentBalanceById($rs['id'], $arr); } else { $arr = array('amount' => $amount, 'currencyType' => $currency_type, 'userid' => $user_id, 'statusDate' => date("Y-m-d H:i:s")); $db_cap->AddCurrentBalanceById($arr); } //end from amount //insert to capital_detail $arr_cd['curr_type'] = $currency_type; $arr_cd['amount'] = $data["from_amount"]; $db_cap->addMoneyToCapitalDetail($arr_cd); //to amount $currency_type = $data["to_amount_type"]; $rs = $db_cap->DetechCapitalExist($user_id, $currency_type, null); //check if add capital exist if (!empty($rs)) { $arr = array('amount' => $rs['amount'] - $data["to_amount"]); $db_cap->updateCurrentBalanceById($rs['id'], $arr); } else { $arr = array('amount' => -$data["to_amount"], 'currencyType' => $currency_type, 'userid' => $user_id, 'statusDate' => date("Y-m-d H:i:s")); $db_cap->AddCurrentBalanceById($arr); } //end to amount //insert to capital_detail $arr_cd['curr_type'] = $currency_type; $arr_cd['amount'] = -$data["to_amount"]; $db_cap->addMoneyToCapitalDetail($arr_cd); return $db->commit(); } catch (Exception $e) { $db->rollBack(); } }
function insertMoneyTransfer($data) { $percent = empty($data['percent']) ? 0 : $data['percent']; $send_date = $data['send_date'] . ' ' . date('h:i:s'); $exp_date = $data['epx_date'] . ' ' . date('h:i:s'); $session_user = new Zend_Session_Namespace('auth'); $db = $this->getAdapter(); $db->beginTransaction(); try { $agent_id = $data['agent_id']; $_data = array('sender_name' => $data['sender'], 'reciever_name' => $data['reciever'], 'reciever_tel' => $data['reciever_tel'], 'invoice_no' => $data['invoice_no'], 'amount' => $data['amount'], 'amount_type' => $data['type_money'], 'commission' => empty($data['commission']) ? 0 : $data['commission'], 'commission_type' => $data['type_money'], 'commission_percent' => $percent, 'commission_agent' => $data['commission_agent'], 'recieved' => $data['recieve_money'], 'recieved_type' => $data['type_money'], 'status' => $data['tran_type'] == 0 ? 0 : 2, 'status_loan' => empty($data['loan']) ? 0 : $data['loan'], 'cut_service' => empty($data['minus']) ? 0 : $data['minus'], 'agent_id' => $agent_id, 'subagent_id' => empty($data['sub_agent_id']) ? 0 : $data['sub_agent_id'], 'send_date' => $send_date, 'expire_date' => $exp_date, 'transfer_money' => $data['gave'], 'total_money' => $data['total'], 'return_money' => $data['return_money'], 'tran_type' => $data['tran_type'], 'is_kbank' => empty($data['is_kbank']) ? 0 : $data['is_kbank'], 'user_id' => $session_user->user_id); $tran_no = $this->insert($_data); $db_sender = new Application_Model_DbTable_DbKbank(); $sender_id = $db_sender->getSenderIdbyName($data['sender']); // $db_rate=new Application_Model_DbTable_DbRate(); // $rate = $db_rate->getCurrentRate(); $_data['sender_name'] = $sender_id; $_data['exchange_rate_db'] = $data['BD']; $_data['exchange_rate_dr'] = $data['RD']; $_data['exchange_rate_rb'] = $data['RB']; $_data['is_orgdebt'] = 1; $db_cap = new Application_Model_DbTable_DbCapital(); //for data capital if (!empty($data['loan'])) { //IF DEBT $this->_name = "cs_transactions_owe"; $_data['money_tran_id'] = $tran_no; $_data['rate_perday'] = $data['rate_perday']; $money_type = 1; if ($data['type_money'] == 1) { $total_debt = $data['total'] + $data['debt_dollar']; } elseif ($data['type_money'] == 2) { $total_debt = $data['total'] + $data['debt_bath']; $money_type = 2; } else { $total_debt = $data['total'] + $data['debt_khmer']; $money_type = 3; } $this->updateStatustoPaid($sender_id, $money_type); //update old tran debt to paid below add new debt $_data['total_money_owe'] = $total_debt; unset($_data['return_money']); unset($_data['is_kbank']); $this->insert($_data); if ($data['tran_type'] == 0) { //debt with tran type = transfer $amount = $data['total']; } else { $amount = -$data['total']; //យើងអស់លុយប៉ុន្តែយើងជំពាក់គេ } $income = 0; // $tran_type=7;//remain debt before pls ckeck condition ; $tran_type = 2; //remain debt ; } else { //add owe if money kbank differ money transfer $_data['money_tran_id'] = $tran_no; $_data['rate_perday'] = $data['rate_perday']; $_data['total_money_owe'] = $data['total']; $_data['status_loan'] = 1; unset($_data['return_money']); // if($data['tran_type'] == 0){//with tran type = transfer // $amount = $data['total']; // }else{ // $amount = -$data['total'];//វេចូល // } // $income = 1; if ($data['tran_type'] == 0) { //with tran type = transfer $amount = $data['total']; } else { $amount = -$data['total']; //វេចូល } // for add withdraw to send if sender use kbank account if (!empty($data['is_kbank'])) { if ($data['type_money'] == 1 and empty($data['debt_dollar'])) { //if kbank no money for transfer $this->AddOweTransationBysender($_data); } elseif ($data['type_money'] == 2 and empty($data['debt_bath'])) { $this->AddOweTransationBysender($_data); } elseif ($data['type_money'] == 3 and empty($data['debt_khmer'])) { $this->AddOweTransationBysender($_data); } $w_dollar = 0; $w_bath = 0; $w_riel = 0; $b_dollar = 0; $b_bath = 0; $b_riel = 0; $invoice = Application_Model_GlobalClass::getInvoiceWithdraw(); $db_draw = new Application_Model_DbTable_DbKbank(); $sender_id = $db_draw->getSenderIdbyName($data['sender']); //covert sender name to sender id $remain_from_kbank = 0; $total_withdraw = $data['total']; if ($data['type_money'] == 1) { $w_dollar = $data['total']; $remain_from_kbank = $data['debt_dollar'] - $data['total']; if ($data['debt_dollar'] < $data['total']) { $w_dollar = $data['debt_dollar']; $total_withdraw = $w_dollar; } } elseif ($data['type_money'] == 2) { $w_bath = $data['total']; $remain_from_kbank = $data['debt_bath'] - $data['total']; if ($data['debt_bath'] < $data['total']) { $w_bath = $data['debt_bath']; $total_withdraw = $w_bath; } } else { $w_riel = $data['total']; $remain_from_kbank = $data['debt_khmer'] - $data['total']; if ($data['debt_khmer'] < $data['total']) { $w_riel = $data['debt_khmer']; $total_withdraw = $w_riel; } } ///if transfer more thand money in kbank so we add this send dept if ($remain_from_kbank < 0) { $_data['total_money_owe'] = -$remain_from_kbank; //if money tranfer larg than money in kbank $debt_id = $this->AddOweTransationBysender($_data); $arr = array('tran_id' => $tran_no, 'tran_type' => 8, 'curr_type' => $data['type_money'], 'amount' => -$remain_from_kbank, 'user_id' => $session_user->user_id, 'income' => 0); $ass = $db_cap->addMoneyToCapitalDetail($arr); } //if money have in account kbank if (!empty($data['debt_dollar'])) { $b_dollar = $data['debt_dollar']; } if (!empty($data['debt_bath'])) { $b_bath = $data['debt_bath']; } if (!empty($data['debt_khmer'])) { $b_riel = $data['debt_khmer']; } // $amount = 0; if ($b_dollar > 0 or $b_bath or $b_riel > 0) { $_arr = array('sender' => $sender_id, 'invoice_no' => $invoice, 'withdraw_dollar' => $w_dollar, 'withdraw_bath' => $w_bath, 'withdraw_riel' => $w_riel, 'debt_dollar' => $b_dollar, 'debt_bath' => $b_bath, 'debt_riel' => $b_riel, 'send_date' => $data['send_date']); $db_draw->addWithdrawBySendName($_arr); $tran_no = $db->lastInsertId(); $arr = array('tran_id' => $tran_no, 'tran_type' => 7, 'curr_type' => $data['type_money'], 'amount' => -$total_withdraw, 'user_id' => $session_user->user_id, 'income' => 0); $ass = $db_cap->addMoneyToCapitalDetail($arr); } $income = 0; // if($data['tran_type'] == 0){//with tran type = transfer // $amount = $data['total']; // }else{ // $amount = -$data['total'];//វេចូល // } } else { // if($data['tran_type'] == 0){//with tran type = transfer // $amount = $data['total']; // }else{ // $amount = -$data['total'];//វេចូល // } $income = 1; ///update date current capital if get money from sender//////////////////// $rs = $db_cap->DetechCapitalExist($session_user->user_id, $data['type_money']); if (!empty($rs)) { //update new user $arr = array('amount' => $rs['amount'] + $amount); $db_cap->updateCurrentBalanceById($rs['id'], $arr); } else { $date = date("Y-m-d H:i:s"); //change it to current edit $arr = array('amount' => $amount, 'currencyType' => $data['type_money'], 'userid' => $session_user->user_id, 'statusDate' => $date); $db_cap->AddCurrentBalanceById($arr); } } $tran_type = 2; //transfer } ////for add capital detail $date = date("Y-m-d H:i:s"); //change it to current edit $arr = array('tran_id' => $tran_no, 'tran_type' => $tran_type, 'curr_type' => $data['type_money'], 'amount' => $amount, 'user_id' => $session_user->user_id, 'income' => $income); $ass = $db_cap->addMoneyToCapitalDetail($arr); return $db->commit(); } catch (Exception $e) { $db->rollBack(); // echo 1111; // echo $e->getMessage();exit(); } }
public function culToCapital($found_id, $currency_type, $amount) { /*start insert to capital*/ $session_user = new Zend_Session_Namespace('auth'); $user_id = $session_user->user_id; $db_cap = new Application_Model_DbTable_DbCapital(); $rs = $db_cap->DetechCapitalExist($user_id, $currency_type, null); //check if add capital exist if (!empty($rs)) { $arr = array('amount' => $rs['amount'] + $amount); $db_cap->updateCurrentBalanceById($rs['id'], $arr); } else { $arr = array('amount' => $amount, 'currencyType' => $currency_type, 'userid' => $user_id, 'statusDate' => date("Y-m-d H:i:s")); $db_cap->AddCurrentBalanceById($arr); } //insert to capital_detail $arr_cd = array('tran_id' => $found_id, 'tran_type' => 6, 'curr_type' => $currency_type, 'amount' => $amount, 'date' => date('Y-m-d'), 'user_id' => $user_id); $db_cap->addMoneyToCapitalDetail($arr_cd); /*end intsert to capital*/ }
public function updateTranLoanById($data) { try { $db = $this->getAdapter(); $db->beginTransaction(); $session_user = new Zend_Session_Namespace('auth'); $user_id = $session_user->user_id; $is_allinterest = 0; if (!empty($data['all_interest'])) { $is_allinterest = $data['all_interest']; } $arr = array('sender_id' => $data['sender'], 'invoice' => $data['invoice_no'], 'loan_date' => $data['loan_date'], 'user_id' => $user_id, 'status' => 1, 'is_allinterest' => $is_allinterest); $where = $db->quoteInto('b_id=?', $data['tran_id']); $this->update($arr, $where); $db_tran = new Application_Model_DbTable_DbCustomerLoan(); $rows = $db_tran->getBorrowDetail($data['tran_id']); $dbc = new Application_Model_DbTable_DbCapital(); $this->_name = 'cs_capital_detail'; foreach ($rows as $key => $rs) { $row = $dbc->DetechCapitalExist($user_id, $rs['currency_type'], null); if (!empty($row)) { $arr = array('amount' => $row['amount'] + $rs['loan_amount']); $dbc->updateCurrentBalanceById($row['id'], $arr); $where = ' tran_id = ' . $rs['id'] . ' AND tran_type = 5 '; $this->delete($where); //for delete capital detail } } $this->_name = 'cs_borrow_detail'; $where = 'br_id = ' . $data['tran_id']; $this->delete($where); $process = 0; $amount = 0; for ($i = 1; $i < 4; $i++) { //for add capital detail and update current capital by staff if (!empty($data['borrow_dollar']) and $i == 1) { $process = 1; $curr_type = 1; $amount = $data['borrow_dollar'] + ($data['debt_dollar'] - $data['old_dollar']); if ($data['sender'] != $data['old_sender']) { $amount = $data['borrow_dollar'] + $data['debt_dollar']; } $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_dollar']; } elseif (!empty($data['borrow_bath']) and $i == 2) { $process = 1; $curr_type = 2; $amount = $data['borrow_bath'] + ($data['debt_bath'] - $data['old_bath']); if ($data['sender'] != $data['old_sender']) { $amount = $data['borrow_bath'] + $data['debt_bath']; } $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_bath']; } elseif (!empty($data['borrow_riel']) and $i == 3) { $process = 1; $curr_type = 3; $amount = $data['borrow_riel'] + ($data['debt_khmer'] - $data['old_riel']); if ($data['sender'] != $data['old_sender']) { $amount = $data['borrow_riel'] + $data['debt_khmer']; } $interest_rate = empty($data['all_interest']) == 0 ? $data['total_interest'] : $data['interest_riel']; } if ($process == 1) { //with loan tran_type = 5 $arr = array('br_id' => $data['tran_id'], 'currency_type' => $curr_type, 'loan_amount' => $amount, 'laon_rate' => $interest_rate, 'BD' => $data['BD'], 'RD' => $data['RD'], 'RB' => $data['RB'], 'status' => 1); $l_id = $this->insert($arr); $arr = array('tran_id' => $l_id, 'tran_type' => 5, 'curr_type' => $curr_type, 'amount' => -$amount, 'user_id' => $user_id); $dbc->addMoneyToCapitalDetail($arr); $rs = $dbc->DetechCapitalExist($session_user->user_id, $curr_type, null); if (!empty($rs)) { //update old user $arr = array('amount' => $rs['amount'] - $amount); $dbc->updateCurrentBalanceById($rs['id'], $arr); } else { $date = date("Y-m-d H:i:s"); $arr = array('amount' => -$amount, 'currencyType' => $curr_type, 'userid' => $user_id, 'statusDate' => $date); $dbc->AddCurrentBalanceById($arr); } } $process = 0; } $db->commit(); } catch (Exception $e) { $db->rollBack(); } }
public function AddTransactionWithDraw($data) { //after delete withdraw then after add withdraw $this->_name = 'cs_withdraw'; $session_user = new Zend_Session_Namespace('auth'); $user_id = $session_user->user_id; if (empty($data['withdraw_dollar'])) { $w_dollar = 0; } else { $w_dollar = $data['withdraw_dollar']; $rs_dollar = $this->getMoneyInAccountBySender($data['sender'], 1); $this->updateOldDeposit($w_dollar, $rs_dollar); } if (empty($data['withdraw_bath'])) { $w_bath = 0; } else { $w_bath = $data['withdraw_bath']; $rs_bath = $this->getMoneyInAccountBySender($data['sender'], 2); $this->updateOldDeposit($w_bath, $rs_bath); } if (empty($data['withdraw_riel'])) { $w_riel = 0; } else { $w_riel = $data['withdraw_riel']; $rs_riel = $this->getMoneyInAccountBySender($data['sender'], 3); $this->updateOldDeposit($w_riel, $rs_riel); } $this->_name = 'cs_withdraw'; $data = array('sender_id' => $data['sender'], 'invoice' => $data['invoice_no'], 'wd_amountdollar' => $w_dollar, 'wd_amountbath' => $w_bath, 'wd_amountriel' => $w_riel, 'user_id' => $user_id, 'create_date' => $data['send_date']); $id = $this->insert($data); $process = 0; $amount = 0; $dbc = new Application_Model_DbTable_DbCapital(); for ($i = 1; $i < 4; $i++) { //for add capital detail and update current capital by staff if (!empty($w_dollar) and $i == 1) { $process = 1; $curr_type = 1; $amount = $w_dollar; } elseif (!empty($w_bath) and $i == 2) { $process = 1; $curr_type = 2; $amount = $w_bath; } elseif (!empty($w_riel) and $i == 3) { $process = 1; $curr_type = 3; $amount = $w_riel; } if ($process == 1) { //with draw tran_type = 7 $_arr = array('tran_id' => $id, 'tran_type' => 7, 'curr_type' => $curr_type, 'amount' => -$amount, 'user_id' => $user_id); $dbc->addMoneyToCapitalDetail($_arr); $rs = $dbc->DetechCapitalExist($user_id, $curr_type, null); if (!empty($rs)) { //update old user $arr = array('amount' => $rs['amount'] - $amount); $dbc->updateCurrentBalanceById($rs['id'], $arr); } else { $date = date("Y-m-d H:i:s"); $arr = array('amount' => -$amount, 'currencyType' => $curr_type, 'userid' => $user_id, 'statusDate' => $date); $dbc->AddCurrentBalanceById($arr); } } $process = 0; } }
public function reStroreDataBeforeUpdateExchange($data){ $db = $this->getAdapter(); $db->beginTransaction(); try{ $tran_id = $data['id']; $tran_type = 3; $sql = " SELECT id, tran_id, tran_type, curr_type, amount, sign, date, income, user_id FROM `cs_capital_detail` WHERE tran_id = $tran_id AND tran_type = $tran_type "; $rows = $db->fetchAll($sql); if($rows){ $db_cap = new Application_Model_DbTable_DbCapital();//for data capital foreach($rows as $row){ $session_user=new Zend_Session_Namespace('auth'); $income = 1; $date = $row['date']; ///update date current capital if get money from sender//////////////////// $rs = $db_cap->DetechCapitalExist($session_user->user_id, $row['curr_type'],$date); if(!empty($rs)){//update new user $amount = $row['amount']; if($row['sign']==0){ $amount = -$row['amount']; } $arr = array( 'amount'=>$rs['amount']-$amount ); $db_cap->updateCurrentBalanceById($rs['id'],$arr); }else{ } } $this->_name='cs_capital_detail'; $where =" tran_type =3 AND tran_id = $tran_id "; $this->delete($where); $this->_name='cs_exchange'; $where = " id = $tran_id "; $this->delete($where); //////////for add new record $this->_name = 'cs_exchange'; $session_user=new Zend_Session_Namespace('auth'); $data["to_amount_type"] =$data['exchangeto']; $to_type = $this->getCurrencyById("`name`, `symbol`", $data["to_amount_type"]); $from_type = $this->getCurrencyById("`name`, `symbol`", $data["exchangefrom"]); $status = "in"; if(($data["exchangefrom"] == 2 && $data["to_amount_type"] == 1) || $data["exchangefrom"] == 3 ){ $status = "out"; } $date = (!empty($date))?$date:date('Y-m-d H:i:s'); $db = $this->getAdapter(); $user_id = $session_user->user_id; $_data=array( "changedAmount"=>$data["return_money"],// "recieptNo"=>$data["inv_no"],// "toAmount"=>$data["to_amount"], "toAmountType"=>$to_type["symbol"], "rate"=>$data["rate"], "fromAmountType"=>$from_type["symbol"], "fromAmount"=>$data["from_amount"], "recievedType"=>$from_type["symbol"], "recievedAmount"=>$data["recieve_money"], "statusDate"=>$date,//date('Y-m-d H:i:s'), "userid"=>$user_id, "status"=>$status, "specail_customer"=>(empty($data['special_cus']))? 0 : 1, "from_to"=>$from_type['name'] . " - " . $to_type["name"] ); $ex_id = $this->insert($_data); ////////////capital $arr_cd = array( 'tran_id'=>$ex_id, 'tran_type'=>3, 'date'=>$date, 'user_id'=>$user_id, ); $db_cap = new Application_Model_DbTable_DbCapital(); //from amount $currency_type = $data["exchangefrom"]; $amount = ($data["from_amount"] - $data["return_money"]); $rs = $db_cap->DetechCapitalExist($user_id,$currency_type,$date);//check if add capital exist if(!empty($rs)){ $arr = array( 'amount'=>$rs['amount']+$amount ); $db_cap->updateCurrentBalanceById($rs['id'],$arr); }else{ $arr =array( 'amount'=>$amount, 'currencyType'=>$currency_type, 'userid'=>$user_id, 'statusDate'=>$date//date("Y-m-d H:i:s") ); $db_cap->AddCurrentBalanceById($arr); } //end from amount //insert to capital_detail $arr_cd['curr_type']=$currency_type; $arr_cd['amount']=$data["from_amount"]; $db_cap->addMoneyToCapitalDetail($arr_cd); //to amount $currency_type = $data["to_amount_type"]; $rs = $db_cap->DetechCapitalExist($user_id,$currency_type,$date);//check if add capital exist if(!empty($rs)){ $arr = array( 'amount'=>$rs['amount']-$data["to_amount"] ); $db_cap->updateCurrentBalanceById($rs['id'],$arr); }else{ $arr =array( 'amount'=>-$data["to_amount"], 'currencyType'=>$currency_type, 'userid'=>$user_id, 'statusDate'=>$date//date("Y-m-d H:i:s") ); $db_cap->AddCurrentBalanceById($arr); } //end to amount //insert to capital_detail $arr_cd['curr_type']=$currency_type; $arr_cd['amount']=-$data["to_amount"]; $db_cap->addMoneyToCapitalDetail($arr_cd); } $db->commit(); }catch(Exception $err){ $db->rollBack(); } }