From ff92572d5645c61964e1502180bf6cb9357c6b15 Mon Sep 17 00:00:00 2001 From: atm-greg Date: Thu, 18 Oct 2018 17:03:49 +0200 Subject: [PATCH] add insurance amount on loan --- .../install/mysql/migration/8.0.0-9.0.0.sql | 3 ++ htdocs/install/mysql/tables/llx_loan.sql | 1 + htdocs/loan/card.php | 18 ++++++++- htdocs/loan/class/loan.class.php | 16 +++++--- htdocs/loan/createschedule.php | 37 ++++++++++++++----- htdocs/loan/payment/payment.php | 6 +-- 6 files changed, 62 insertions(+), 19 deletions(-) diff --git a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql index 09908ac86f5..dd2b55e17ca 100644 --- a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql +++ b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql @@ -129,3 +129,6 @@ CREATE TABLE llx_takepos_floor_tables( UPDATE llx_c_payment_term SET decalage = nbjour, nbjour = 0 where decalage IS NULL AND type_cdr = 2; + +ALTER TABLE llx_loan ADD COLUMN insurance_amount double(24,8) DEFAULT 0; + diff --git a/htdocs/install/mysql/tables/llx_loan.sql b/htdocs/install/mysql/tables/llx_loan.sql index 7277fa85d47..1fca2af50ba 100644 --- a/htdocs/install/mysql/tables/llx_loan.sql +++ b/htdocs/install/mysql/tables/llx_loan.sql @@ -28,6 +28,7 @@ create table llx_loan fk_bank integer, capital double(24,8) default 0 NOT NULL, + insurance_amount double(24,8) default 0, datestart date, dateend date, nbterm real, diff --git a/htdocs/loan/card.php b/htdocs/loan/card.php index 25651ac842a..2a6708655a8 100644 --- a/htdocs/loan/card.php +++ b/htdocs/loan/card.php @@ -135,6 +135,7 @@ if (empty($reshook)) $object->note_private = GETPOST('note_private','none'); $object->note_public = GETPOST('note_public','none'); $object->fk_project = GETPOST('projectid','int'); + $object->insurance_amount = GETPOST('insurance_amount', 'int'); $accountancy_account_capital = GETPOST('accountancy_account_capital'); $accountancy_account_insurance = GETPOST('accountancy_account_insurance'); @@ -148,7 +149,7 @@ if (empty($reshook)) if ($id <= 0) { $error++; - setEventMessages($object->error, $object->errors, 'errors'); + setEventMessages($object->db->lastqueryerror, $object->errors, 'errors'); $action = 'create'; } } @@ -301,6 +302,9 @@ if ($action == 'create') // Rate print ''.$langs->trans("Rate").' %'; + // insurance amount + print ''.$langs->trans("Insurance").''; + // Project if (! empty($conf->projet->enabled)) { @@ -495,6 +499,18 @@ if ($id > 0) { print ''.$langs->trans("LoanCapital").''.price($object->capital,0,$outputlangs,1,-1,-1,$conf->currency).''; } + + // Insurance + if ($action == 'edit') + { + print ''.$langs->trans("Insurance").''; + print ''; + print ''; + } + else + { + print ''.$langs->trans("Insurance").''.price($object->insurance_amount,0,$outputlangs,1,-1,-1,$conf->currency).''; + } // Date start print ''.$langs->trans("DateStart").""; diff --git a/htdocs/loan/class/loan.class.php b/htdocs/loan/class/loan.class.php index 0e4542b1d20..3805704fbb9 100644 --- a/htdocs/loan/class/loan.class.php +++ b/htdocs/loan/class/loan.class.php @@ -66,6 +66,8 @@ class Loan extends CommonObject public $date_creation; public $date_modification; public $date_validation; + + public $insurance_amount; /** * @var int Bank ID @@ -106,7 +108,7 @@ class Loan extends CommonObject */ function fetch($id) { - $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public,"; + $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,"; $sql.= " l.paid, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project"; $sql.= " FROM ".MAIN_DB_PREFIX."loan as l"; $sql.= " WHERE l.rowid = ".$id; @@ -129,6 +131,7 @@ class Loan extends CommonObject $this->rate = $obj->rate; $this->note_private = $obj->note_private; $this->note_public = $obj->note_public; + $this->insurance_amount = $obj->insurance_amount; $this->paid = $obj->paid; $this->account_capital = $obj->accountancy_account_capital; @@ -169,6 +172,8 @@ class Loan extends CommonObject // clean parameters $newcapital=price2num($this->capital,'MT'); + if (empty($this->insurance_amount)) $this->insurance_amount = 0; + $newinsuranceamount=price2num($this->insurance_amount, 'MT'); if (isset($this->note_private)) $this->note_private = trim($this->note_private); if (isset($this->note_public)) $this->note_public = trim($this->note_public); if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital); @@ -205,7 +210,7 @@ class Loan extends CommonObject $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,"; $sql.= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,"; - $sql.= " datec, fk_projet, fk_user_author)"; + $sql.= " datec, fk_projet, fk_user_author, insurance_amount)"; $sql.= " VALUES ('".$this->db->escape($this->label)."',"; $sql.= " '".$this->db->escape($this->fk_bank)."',"; $sql.= " '".price2num($newcapital)."',"; @@ -221,7 +226,8 @@ class Loan extends CommonObject $sql.= " ".$conf->entity.","; $sql.= " '".$this->db->idate($now)."',"; $sql.= " ".(empty($this->fk_project)?'NULL':$this->fk_project).","; - $sql.= " ".$user->id; + $sql.= " ".$user->id.","; + $sql.= " '".price2num($newinsuranceamount)."'"; $sql.= ")"; dol_syslog(get_class($this)."::create", LOG_DEBUG); @@ -339,7 +345,8 @@ class Loan extends CommonObject $sql.= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',"; $sql.= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',"; $sql.= " fk_projet=".(empty($this->fk_project)?'NULL':$this->fk_project).","; - $sql.= " fk_user_modif = ".$user->id; + $sql.= " fk_user_modif = ".$user->id.","; + $sql.= " insurance_amount = '".price2num($this->db->escape($this->insurance_amount))."'"; $sql.= " WHERE rowid=".$this->id; dol_syslog(get_class($this)."::update", LOG_DEBUG); @@ -549,7 +556,6 @@ class Loan extends CommonObject { $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,'; $sql.= ' l.tms'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'loan as l'; $sql.= ' WHERE l.rowid = '.$id; dol_syslog(get_class($this).'::info', LOG_DEBUG); diff --git a/htdocs/loan/createschedule.php b/htdocs/loan/createschedule.php index 7cfd54ab998..7a68d3e38a8 100644 --- a/htdocs/loan/createschedule.php +++ b/htdocs/loan/createschedule.php @@ -35,7 +35,7 @@ $object = new Loan($db); $object->fetch($loanid); // Load translation files required by the page -$langs->loadLangs(array("loan")); +$langs->loadLangs(array("compta","bills","loan")); if ($action == 'createecheancier') { @@ -45,6 +45,7 @@ if ($action == 'createecheancier') { $date = GETPOST('hi_date'.$i,'int'); $mens = GETPOST('mens'.$i); $int = GETPOST('hi_interets'.$i); + $insurance = GETPOST('hi_insurance'.$i); $echeance = new LoanSchedule($db); @@ -53,10 +54,10 @@ if ($action == 'createecheancier') { $echeance->tms = dol_now(); $echeance->datep = $date; $echeance->amount_capital = $mens-$int; - $echeance->amount_insurance = 0; + $echeance->amount_insurance = $insurance; $echeance->amount_interest = $int; $echeance->fk_typepayment = 3; - $echeance->fk_bank = 1; + $echeance->fk_bank = 0; $echeance->fk_user_creat = $user->id; $echeance->fk_user_modif = $user->id; $result=$echeance->create($user); @@ -75,11 +76,13 @@ if ($action == 'updateecheancier') { $mens = GETPOST('mens'.$i); $int = GETPOST('hi_interets'.$i); $id = GETPOST('hi_rowid'.$i); + $insurance = GETPOST('hi_insurance'.$i); + $echeance = new LoanSchedule($db); $echeance->fetch($id); $echeance->tms = dol_now(); $echeance->amount_capital = $mens-$int; - $echeance->amount_insurance = 0; + $echeance->amount_insurance = $insurance; $echeance->amount_interest = $int; $echeance->fk_user_modif = $user->id; $result= $echeance->update($user,0); @@ -143,26 +146,33 @@ if(count($echeance->lines)>0) } print ''; print ''; -print ''; print ''; print ''; -Print ''; -Print ''; +Print ''; +Print ''; +print ''; Print ''; -Print ''; Print ''; +if (count($echeance->lines)>0) print ''; print ''."\n"; if ($object->nbterm > 0 && count($echeance->lines)==0) { $i=1; $capital = $object->capital; + $insurance = $object->insurance_amount/$object->nbterm; + $insurance = price2num($insurance, 'MT'); + $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm)); while($i <$object->nbterm+1) { $mens = price2num($echeance->calcMonthlyPayments($capital, $object->rate/100, $object->nbterm-$i+1), 'MT'); @@ -172,8 +182,9 @@ if ($object->nbterm > 0 && count($echeance->lines)==0) print ''; print ''; print ''; - print ''; + print ''; print ''; + print ''; print ''; print ''."\n"; $i++; @@ -184,6 +195,9 @@ elseif(count($echeance->lines)>0) { $i=1; $capital = $object->capital; + $insurance = $object->insurance_amount/$object->nbterm; + $insurance = price2num($insurance, 'MT'); + $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm)); foreach ($echeance->lines as $line){ $mens = $line->amount_capital+$line->amount_insurance+$line->amount_interest; $int = $line->amount_interest; @@ -191,13 +205,16 @@ elseif(count($echeance->lines)>0) print ''; print ''; print ''; + print ''; + print ''; if($line->datep > dol_now()){ print ''; }else{ print ''; } - print ''; + print ''; + print ''; print ''."\n"; $i++; $capital = $cap_rest; diff --git a/htdocs/loan/payment/payment.php b/htdocs/loan/payment/payment.php index 08937c1f389..ba6650c9c03 100644 --- a/htdocs/loan/payment/payment.php +++ b/htdocs/loan/payment/payment.php @@ -264,7 +264,7 @@ if ($action == 'create') print '
'; +$colspan = 6; +if (count($echeance->lines)>0) $colspan++; +print ''; print $langs->trans("FinancialCommitment"); print '
'.$langs->trans("Term").''.$langs->trans("Date").''.$langs->trans("Term").''.$langs->trans("Date").''.$langs->trans("Insurance"); +Print ''.$langs->trans("InterestAmount").''.$langs->trans("Amount").''.$langs->trans("InterestAmount").''.$langs->trans("CapitalRemain"); print ' ('.price2num($object->capital).')'; print ''; print ''.$langs->trans('DoPayment').'
' . $i .'' . dol_print_date(dol_time_plus_duree($object->datestart, $i-1, 'm'),'day') . ''.price($insurance+(($i == 1) ? $regulInsurance : 0),0,'',1).' €'.price($int,0,'',1).' €'.price($cap_rest).' €
' . $i .'' . dol_print_date($line->datep,'day') . ''.price($insurance+(($i == 1) ? $regulInsurance : 0),0,'',1).' €'.price($int,0,'',1).' €' . price($mens) . ' €'.price($int,0,'',1).' €'.price($cap_rest).' €'.$langs->trans('DoPayment').'
'; if ($sumpaid < $loan->capital) { - print $langs->trans("LoanCapital") .': '; + print $langs->trans("LoanCapital") .': '; } else { @@ -273,7 +273,7 @@ if ($action == 'create') print '
'; if ($sumpaid < $loan->capital) { - print $langs->trans("Insurance") .': '; + print $langs->trans("Insurance") .': '; } else { @@ -282,7 +282,7 @@ if ($action == 'create') print '
'; if ($sumpaid < $loan->capital) { - print $langs->trans("Interest") .': '; + print $langs->trans("Interest") .': '; } else {