clean loan code

This commit is contained in:
Frédéric FRANCE 2018-10-15 20:36:11 +02:00
parent 6d4d8a0342
commit 7ea4f7ea0b
No known key found for this signature in database
GPG Key ID: 06809324E4B2ABC1
6 changed files with 172 additions and 94 deletions

View File

@ -50,7 +50,7 @@ $echance++;
$capital=$cap_rest;
while ($echance<=$nbterm) {
$mens = round($object->calc_mens($capital,$rate,$nbterm-$echance+1),2,PHP_ROUND_HALF_UP);
$mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm-$echance+1), 2, PHP_ROUND_HALF_UP);
$int = ($capital*($rate/12));
$int = round($int,2,PHP_ROUND_HALF_UP);
@ -63,4 +63,3 @@ while ($echance<=$nbterm) {
}
echo json_encode($output);

View File

@ -51,7 +51,7 @@ class Loan extends CommonObject
public $datestart;
public $dateend;
/**
/**
* @var string Loan label
*/
public $label;
@ -66,10 +66,26 @@ class Loan extends CommonObject
public $date_creation;
public $date_modification;
public $date_validation;
public $fk_bank;
public $fk_user_creat;
public $fk_user_modif;
public $fk_project;
/**
* @var int Bank ID
*/
public $fk_bank;
/**
* @var int User ID
*/
public $fk_user_creat;
/**
* @var int User ID
*/
public $fk_user_modif;
/**
* @var int Project ID
*/
public $fk_project;
/**
@ -158,10 +174,10 @@ class Loan extends CommonObject
if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
if (isset($this->fk_project)) $this->fk_project=trim($this->fk_project);
if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
if (isset($this->fk_project)) $this->fk_project = (int) $this->fk_project;
// Check parameters
if (! $newcapital > 0 || empty($this->datestart) || empty($this->dateend))
@ -445,9 +461,9 @@ class Loan extends CommonObject
$tooltip = '<u>' . $langs->trans("ShowLoan") . '</u>';
if (! empty($this->ref))
$tooltip .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
$tooltip .= '<br><strong>' . $langs->trans('Ref') . ':</strong> ' . $this->ref;
if (! empty($this->label))
$tooltip .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label;
$tooltip .= '<br><strong>' . $langs->trans('Label') . ':</strong> ' . $this->label;
$linkstart = '<a href="'.DOL_URL_ROOT.'/loan/card.php?id='.$this->id.'" title="'.str_replace('\n', '', dol_escape_htmltag($tooltip, 1)).'" class="classfortooltip">';
$linkend = '</a>';

View File

@ -1,5 +1,6 @@
<?php
/* Copyright (C) 2017 Florian HENRY <florian.henry@atm-consulting.fr>
/* Copyright (C) 2017 Florian HENRY <florian.henry@atm-consulting.fr>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -39,20 +40,53 @@ class LoanSchedule extends CommonObject
*/
public $table_element='loan_schedule';
var $fk_loan;
var $datec='';
var $tms='';
var $datep='';
var $amounts=array(); // Array of amounts
var $amount_capital; // Total amount of payment
var $amount_insurance;
var $amount_interest;
var $fk_typepayment;
var $num_payment;
var $fk_bank;
var $fk_user_creat;
var $fk_user_modif;
var $lines=array();
/**
* @var int Loan ID
*/
public $fk_loan;
/**
* @var string Create date
*/
public $datec='';
public $tms='';
/**
* @var string Payment date
*/
public $datep='';
public $amounts=array(); // Array of amounts
public $amount_capital; // Total amount of payment
public $amount_insurance;
public $amount_interest;
/**
* @var int Payment Type ID
*/
public $fk_typepayment;
/**
* @var int Payment ID
*/
public $num_payment;
/**
* @var int Bank ID
*/
public $fk_bank;
/**
* @var int Bank ID
*/
public $fk_user_creat;
/**
* @var int User ID
*/
public $fk_user_modif;
public $lines=array();
/**
* @deprecated
@ -65,7 +99,7 @@ class LoanSchedule extends CommonObject
*
* @param DoliDB $db Database handler
*/
function __construct($db)
public function __construct($db)
{
$this->db = $db;
}
@ -77,7 +111,7 @@ class LoanSchedule extends CommonObject
* @param User $user User making payment
* @return int <0 if KO, id of payment if OK
*/
function create($user)
public function create($user)
{
global $conf, $langs;
@ -86,21 +120,21 @@ class LoanSchedule extends CommonObject
$now=dol_now();
// Validate parameters
if (! $this->datepaid)
if (! $this->datep)
{
$this->error='ErrorBadValueForParameter';
return -1;
}
// Clean parameters
if (isset($this->fk_loan)) $this->fk_loan = trim($this->fk_loan);
if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
if (isset($this->amount_capital)) $this->amount_capital = trim($this->amount_capital?$this->amount_capital:0);
if (isset($this->amount_insurance)) $this->amount_insurance = trim($this->amount_insurance?$this->amount_insurance:0);
if (isset($this->amount_interest)) $this->amount_interest = trim($this->amount_interest?$this->amount_interest:0);
if (isset($this->fk_typepayment)) $this->fk_typepayment = trim($this->fk_typepayment);
if (isset($this->fk_bank)) $this->fk_bank = trim($this->fk_bank);
if (isset($this->fk_user_creat)) $this->fk_user_creat = trim($this->fk_user_creat);
if (isset($this->fk_user_modif)) $this->fk_user_modif = trim($this->fk_user_modif);
if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
$totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
$totalamount = price2num($totalamount);
@ -119,7 +153,7 @@ class LoanSchedule extends CommonObject
$sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
$sql.= " fk_typepayment, fk_user_creat, fk_bank)";
$sql.= " VALUES (".$this->fk_loan.", '".$this->db->idate($now)."',";
$sql.= " '".$this->db->idate($this->datepaid)."',";
$sql.= " '".$this->db->idate($this->datep)."',";
$sql.= " ".$this->amount_capital.",";
$sql.= " ".$this->amount_insurance.",";
$sql.= " ".$this->amount_interest.",";
@ -162,7 +196,7 @@ class LoanSchedule extends CommonObject
* @param int $id Id object
* @return int <0 if KO, >0 if OK
*/
function fetch($id)
public function fetch($id)
{
global $langs;
$sql = "SELECT";
@ -215,8 +249,8 @@ class LoanSchedule extends CommonObject
$this->type_code = $obj->type_code;
$this->type_libelle = $obj->type_libelle;
$this->bank_account = $obj->fk_account;
$this->bank_line = $obj->fk_bank;
$this->bank_account = $obj->fk_account;
$this->bank_line = $obj->fk_bank;
}
$this->db->free($resql);
@ -237,7 +271,7 @@ class LoanSchedule extends CommonObject
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, >0 if OK
*/
function update($user=0, $notrigger=0)
public function update($user=0, $notrigger=0)
{
global $conf, $langs;
$error=0;
@ -321,7 +355,7 @@ class LoanSchedule extends CommonObject
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, >0 if OK
*/
function delete($user, $notrigger=0)
public function delete($user, $notrigger=0)
{
global $conf, $langs;
$error=0;
@ -372,18 +406,16 @@ class LoanSchedule extends CommonObject
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Calculate mensuality
* Calculate Monthly Payments
*
* @param double $capital Capital
* @param double $rate rate
* @param int $nbterm nb term
* @return double mensuality
*/
function calc_mens($capital, $rate, $nbterm)
public function calcMonthlyPayments($capital, $rate, $nbterm)
{
// phpcs:enable
$result='';
if (!empty($capital) && !empty($rate) && !empty($nbterm)) {
@ -400,7 +432,7 @@ class LoanSchedule extends CommonObject
* @param int $loanid Id object
* @return int <0 if KO, >0 if OK
*/
function fetchAll($loanid)
public function fetchAll($loanid)
{
global $langs;
@ -461,15 +493,13 @@ class LoanSchedule extends CommonObject
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* trans_paiment
* transPayment
*
* @return void
*/
function trans_paiment()
private function transPayment()
{
// phpcs:enable
require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
@ -505,12 +535,12 @@ class LoanSchedule extends CommonObject
/**
* trans_paiment
* transPayment
*
* @param int $loanid Loan id
* @return int < 0 if KO, Date > 0 if OK
*/
function lastpaiment($loanid)
public function lastpaiment($loanid)
{
$sql = "SELECT p.datep";
$sql.= " FROM ".MAIN_DB_PREFIX."payment_loan as p ";
@ -535,7 +565,7 @@ class LoanSchedule extends CommonObject
* @param int $datemax Date max
* @return array Array of id
*/
function paimenttorecord($loanid, $datemax)
public function paimenttorecord($loanid, $datemax)
{
$sql = "SELECT p.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as p ";

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@zendsi.com>
* Copyright (C) 2015 Frederic France <frederic.france@free.fr>
* Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -40,32 +40,68 @@ class PaymentLoan extends CommonObject
*/
public $table_element='payment_loan';
var $fk_loan;
var $datec='';
var $tms='';
var $datep='';
var $amounts=array(); // Array of amounts
var $amount_capital; // Total amount of payment
var $amount_insurance;
var $amount_interest;
var $fk_typepayment;
var $num_payment;
var $fk_bank;
var $fk_user_creat;
var $fk_user_modif;
/**
* @var int Loan ID
*/
public $fk_loan;
/**
* @var string Create date
*/
public $datec='';
public $tms='';
/**
* @var string Payment date
*/
public $datep='';
public $amounts=array(); // Array of amounts
public $amount_capital; // Total amount of payment
public $amount_insurance;
public $amount_interest;
/**
* @var int Payment type ID
*/
public $fk_typepayment;
/**
* @var int Payment ID
*/
public $num_payment;
/**
* @var int Bank ID
*/
public $fk_bank;
/**
* @var int User ID
*/
public $fk_user_creat;
/**
* @var int user ID
*/
public $fk_user_modif;
/**
* @deprecated
* @see amount, amounts
*/
var $total;
public $total;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
public function __construct($db)
{
$this->db = $db;
}
@ -86,24 +122,24 @@ class PaymentLoan extends CommonObject
$now=dol_now();
// Validate parameters
if (! $this->datepaid)
if (! $this->datep)
{
$this->error='ErrorBadValueForParameter';
return -1;
}
// Clean parameters
if (isset($this->fk_loan)) $this->fk_loan = trim($this->fk_loan);
if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
if (isset($this->amount_capital)) $this->amount_capital = price2num($this->amount_capital?$this->amount_capital:0);
if (isset($this->amount_insurance)) $this->amount_insurance = price2num($this->amount_insurance?$this->amount_insurance:0);
if (isset($this->amount_insurance)) $this->amount_insurance = price2num($this->amount_insurance?$this->amount_insurance:0);
if (isset($this->amount_interest)) $this->amount_interest = price2num($this->amount_interest?$this->amount_interest:0);
if (isset($this->fk_typepayment)) $this->fk_typepayment = trim($this->fk_typepayment);
if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
if (isset($this->num_payment)) $this->num_payment = (int) $this->num_payment;
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->fk_bank)) $this->fk_bank = trim($this->fk_bank);
if (isset($this->fk_user_creat)) $this->fk_user_creat = trim($this->fk_user_creat);
if (isset($this->fk_user_modif)) $this->fk_user_modif = trim($this->fk_user_modif);
if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
$totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
$totalamount = price2num($totalamount);
@ -119,7 +155,7 @@ class PaymentLoan extends CommonObject
$sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
$sql.= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)";
$sql.= " VALUES (".$this->chid.", '".$this->db->idate($now)."',";
$sql.= " '".$this->db->idate($this->datepaid)."',";
$sql.= " '".$this->db->idate($this->datep)."',";
$sql.= " ".$this->amount_capital.",";
$sql.= " ".$this->amount_insurance.",";
$sql.= " ".$this->amount_interest.",";
@ -244,17 +280,17 @@ class PaymentLoan extends CommonObject
$error=0;
// Clean parameters
if (isset($this->fk_loan)) $this->fk_loan=trim($this->fk_loan);
if (isset($this->fk_loan)) $this->fk_loan = (int) $this->fk_loan;
if (isset($this->amount_capital)) $this->amount_capital=trim($this->amount_capital);
if (isset($this->amount_insurance)) $this->amount_insurance=trim($this->amount_insurance);
if (isset($this->amount_interest)) $this->amount_interest=trim($this->amount_interest);
if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
if (isset($this->num_payment)) $this->num_payment = (int) $this->num_payment;
if (isset($this->note_private)) $this->note=trim($this->note_private);
if (isset($this->note_public)) $this->note=trim($this->note_public);
if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
// Check parameters
@ -418,7 +454,7 @@ class PaymentLoan extends CommonObject
// Insert payment into llx_bank
$bank_line_id = $acc->addline(
$this->datepaid,
$this->datep,
$this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
$label,
$total,

View File

@ -51,7 +51,7 @@ if ($action == 'createecheancier') {
$echeance->fk_loan = $object->id;
$echeance->datec = dol_now();
$echeance->tms = dol_now();
$echeance->datepaid = $date;
$echeance->datep = $date;
$echeance->amount_capital = $mens-$int;
$echeance->amount_insurance = 0;
$echeance->amount_interest = $int;
@ -61,7 +61,7 @@ if ($action == 'createecheancier') {
$echeance->fk_user_modif = $user->id;
$result=$echeance->create($user);
if ($result<0) {
setEventMessages(null, $echeance->errors,'errors');
setEventMessages($echeance->error, $echeance->errors,'errors');
}
$i++;
}
@ -165,7 +165,7 @@ if ($object->nbterm > 0 && count($echeance->lines)==0)
$capital = $object->capital;
while($i <$object->nbterm+1)
{
$mens = price2num($echeance->calc_mens($capital, $object->rate/100, $object->nbterm-$i+1), 'MT');
$mens = price2num($echeance->calcMonthlyPayments($capital, $object->rate/100, $object->nbterm-$i+1), 'MT');
$int = ($capital*($object->rate/12))/100;
$int = price2num($int, 'MT');
$cap_rest = price2num($capital - ($mens-$int), 'MT');
@ -213,6 +213,3 @@ print '</form>';
// End of page
llxFooter();
$db->close();

View File

@ -93,12 +93,12 @@ if ($action == 'add_payment')
// Create a line of payments
$payment = new PaymentLoan($db);
$payment->chid = $chid;
$payment->datepaid = $datepaid;
$payment->datep = $datepaid;
$payment->label = $loan->label;
$payment->amount_capital = GETPOST('amount_capital');
$payment->amount_insurance = GETPOST('amount_insurance');
$payment->amount_interest = GETPOST('amount_interest');
$payment->paymenttype = GETPOST('paymenttype');
$payment->paymenttype = GETPOST('paymenttype', 'int');
$payment->num_payment = GETPOST('num_payment');
$payment->note_private = GETPOST('note_private','none');
$payment->note_public = GETPOST('note_public','none');