2017-06-02 17:48:29 +02:00
< ? php
2018-10-15 20:36:11 +02:00
/* Copyright ( C ) 2017 Florian HENRY < florian . henry @ atm - consulting . fr >
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2017-06-02 17:48:29 +02:00
*
* 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
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-06-02 17:48:29 +02:00
*/
/**
* \file htdocs / loan / class / loanschedule . class . php
2018-08-28 08:33:02 +02:00
* \ingroup loan
* \brief File of class to manage schedule of loans
2017-06-02 17:48:29 +02:00
*/
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2017-06-03 02:22:21 +02:00
/**
* Class to manage Schedule of loans
2017-06-02 17:48:29 +02:00
*/
class LoanSchedule extends CommonObject
{
2018-08-23 18:35:45 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-13 19:37:08 +01:00
public $element = 'loan_schedule' ;
2018-08-28 08:33:02 +02:00
2018-08-22 18:48:53 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-11-13 19:37:08 +01:00
public $table_element = 'loan_schedule' ;
2017-06-02 17:48:29 +02:00
2018-10-15 20:36:11 +02:00
/**
* @ var int Loan ID
*/
public $fk_loan ;
/**
* @ var string Create date
*/
2019-06-05 16:01:02 +02:00
public $datec ;
public $tms ;
2018-10-15 20:36:11 +02:00
/**
* @ var string Payment date
*/
2019-06-05 16:01:02 +02:00
public $datep ;
2018-10-15 20:36:11 +02:00
2020-04-10 10:59:32 +02:00
public $amounts = array (); // Array of amounts
public $amount_capital ; // Total amount of payment
2018-10-15 20:36:11 +02:00
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 ;
2020-06-02 14:42:57 +02:00
/**
* @ var int Loan Payment ID
*/
public $fk_payment_loan ;
2018-10-15 20:36:11 +02:00
/**
* @ var int Bank ID
*/
public $fk_user_creat ;
/**
* @ var int User ID
*/
public $fk_user_modif ;
2019-11-13 19:37:08 +01:00
public $lines = array ();
2017-06-02 17:48:29 +02:00
/**
* @ deprecated
2019-04-04 18:33:12 +02:00
* @ see $amount , $amounts
2017-06-02 17:48:29 +02:00
*/
2018-09-02 09:27:59 +02:00
public $total ;
2017-06-02 17:48:29 +02:00
2019-11-02 11:12:57 +01:00
public $type_code ;
public $type_label ;
2017-06-02 17:48:29 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2018-10-15 20:36:11 +02:00
public function __construct ( $db )
2017-06-02 17:48:29 +02:00
{
$this -> db = $db ;
}
/**
* Create payment of loan into database .
* Use this -> amounts to have list of lines for the payment
*
* @ param User $user User making payment
* @ return int < 0 if KO , id of payment if OK
*/
2018-10-15 20:36:11 +02:00
public function create ( $user )
2017-06-02 17:48:29 +02:00
{
global $conf , $langs ;
2019-11-13 19:37:08 +01:00
$error = 0 ;
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
$now = dol_now ();
2017-06-02 17:48:29 +02:00
// Validate parameters
2019-11-13 19:37:08 +01:00
if ( ! $this -> datep )
2017-06-02 17:48:29 +02:00
{
2019-11-13 19:37:08 +01:00
$this -> error = 'ErrorBadValueForParameter' ;
2017-06-02 17:48:29 +02:00
return - 1 ;
}
// Clean parameters
2018-10-15 20:36:11 +02:00
if ( isset ( $this -> fk_loan )) $this -> fk_loan = ( int ) $this -> fk_loan ;
2019-11-13 19:37:08 +01:00
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 );
2018-10-15 20:36:11 +02:00
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 ;
2017-06-02 17:48:29 +02:00
$totalamount = $this -> amount_capital + $this -> amount_insurance + $this -> amount_interest ;
$totalamount = price2num ( $totalamount );
// Check parameters
if ( $totalamount == 0 ) {
2019-11-13 19:37:08 +01:00
$this -> errors [] = 'step1' ;
2017-06-02 17:48:29 +02:00
return - 1 ; // Negative amounts are accepted for reject prelevement but not null
}
$this -> db -> begin ();
if ( $totalamount != 0 )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest, " ;
2019-11-13 19:37:08 +01:00
$sql .= " fk_typepayment, fk_user_creat, fk_bank) " ;
$sql .= " VALUES ( " . $this -> fk_loan . " , ' " . $this -> db -> idate ( $now ) . " ', " ;
$sql .= " ' " . $this -> db -> idate ( $this -> datep ) . " ', " ;
$sql .= " " . $this -> amount_capital . " , " ;
$sql .= " " . $this -> amount_insurance . " , " ;
$sql .= " " . $this -> amount_interest . " , " ;
$sql .= " " . $this -> fk_typepayment . " , " ;
$sql .= " " . $user -> id . " , " ;
$sql .= " " . $this -> fk_bank . " ) " ;
2017-06-02 17:48:29 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2019-11-13 19:37:08 +01:00
$resql = $this -> db -> query ( $sql );
2017-06-02 17:48:29 +02:00
if ( $resql )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " payment_loan " );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$this -> error = $this -> db -> lasterror ();
2017-06-02 17:48:29 +02:00
$error ++ ;
}
}
2019-11-13 19:37:08 +01:00
if ( $totalamount != 0 && ! $error )
2017-06-02 17:48:29 +02:00
{
2019-11-13 19:37:08 +01:00
$this -> amount_capital = $totalamount ;
2017-06-02 17:48:29 +02:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-06-02 17:48:29 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* Load object in memory from database
*
* @ param int $id Id object
* @ return int < 0 if KO , > 0 if OK
*/
2018-10-15 20:36:11 +02:00
public function fetch ( $id )
2017-06-02 17:48:29 +02:00
{
global $langs ;
$sql = " SELECT " ;
2020-06-02 14:42:57 +02:00
$sql .= " t.rowid, " ;
$sql .= " t.fk_loan, " ;
$sql .= " t.datec, " ;
$sql .= " t.tms, " ;
$sql .= " t.datep, " ;
$sql .= " t.amount_capital, " ;
$sql .= " t.amount_insurance, " ;
$sql .= " t.amount_interest, " ;
$sql .= " t.fk_typepayment, " ;
$sql .= " t.num_payment, " ;
$sql .= " t.note_private, " ;
$sql .= " t.note_public, " ;
$sql .= " t.fk_bank, " ;
$sql .= " t.fk_payment_loan, " ;
$sql .= " t.fk_user_creat, " ;
$sql .= " t.fk_user_modif, " ;
$sql .= " pt.code as type_code, pt.libelle as type_label, " ;
$sql .= ' b.fk_account' ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as t " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_paiement as pt ON t.fk_typepayment = pt.id " ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid' ;
$sql .= " WHERE t.rowid = " . $id ;
2017-06-02 17:48:29 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2018-08-31 22:11:52 +02:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> rowid ;
$this -> fk_loan = $obj -> fk_loan ;
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> tms = $this -> db -> jdate ( $obj -> tms );
$this -> datep = $this -> db -> jdate ( $obj -> datep );
$this -> amount_capital = $obj -> amount_capital ;
$this -> amount_insurance = $obj -> amount_insurance ;
$this -> amount_interest = $obj -> amount_interest ;
$this -> fk_typepayment = $obj -> fk_typepayment ;
$this -> num_payment = $obj -> num_payment ;
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
$this -> fk_bank = $obj -> fk_bank ;
2020-06-02 14:42:57 +02:00
$this -> fk_payment_loan = $obj -> fk_payment_loan ;
2018-08-31 22:11:52 +02:00
$this -> fk_user_creat = $obj -> fk_user_creat ;
$this -> fk_user_modif = $obj -> fk_user_modif ;
$this -> type_code = $obj -> type_code ;
2019-11-02 11:12:57 +01:00
$this -> type_label = $obj -> type_label ;
2018-08-31 22:11:52 +02:00
2018-10-15 20:36:11 +02:00
$this -> bank_account = $obj -> fk_account ;
$this -> bank_line = $obj -> fk_bank ;
2018-08-31 22:11:52 +02:00
}
$this -> db -> free ( $resql );
2017-06-02 17:48:29 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-06-02 17:48:29 +02:00
return - 1 ;
}
}
/**
* Update database
*
* @ param User $user User that modify
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-01-27 15:20:16 +01:00
public function update ( $user = 0 , $notrigger = 0 )
2017-06-02 17:48:29 +02:00
{
global $conf , $langs ;
2020-04-10 10:59:32 +02:00
$error = 0 ;
2017-06-02 17:48:29 +02:00
// Clean parameters
2020-06-02 14:42:57 +02:00
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 -> num_payment )) $this -> num_payment = trim ( $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_payment_loan )) $this -> fk_payment_loan = ( int ) $this -> fk_payment_loan ;
2017-06-02 17:48:29 +02:00
// Check parameters
// Put here code to add control on parameters values
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " ;
2019-11-13 19:37:08 +01:00
$sql .= " fk_loan= " . ( isset ( $this -> fk_loan ) ? $this -> fk_loan : " null " ) . " , " ;
$sql .= " datec= " . ( dol_strlen ( $this -> datec ) != 0 ? " ' " . $this -> db -> idate ( $this -> datec ) . " ' " : 'null' ) . " , " ;
$sql .= " tms= " . ( dol_strlen ( $this -> tms ) != 0 ? " ' " . $this -> db -> idate ( $this -> tms ) . " ' " : 'null' ) . " , " ;
$sql .= " datep= " . ( dol_strlen ( $this -> datep ) != 0 ? " ' " . $this -> db -> idate ( $this -> datep ) . " ' " : 'null' ) . " , " ;
$sql .= " amount_capital= " . ( isset ( $this -> amount_capital ) ? $this -> amount_capital : " null " ) . " , " ;
$sql .= " amount_insurance= " . ( isset ( $this -> amount_insurance ) ? $this -> amount_insurance : " null " ) . " , " ;
$sql .= " amount_interest= " . ( isset ( $this -> amount_interest ) ? $this -> amount_interest : " null " ) . " , " ;
$sql .= " fk_typepayment= " . ( isset ( $this -> fk_typepayment ) ? $this -> fk_typepayment : " null " ) . " , " ;
$sql .= " num_payment= " . ( isset ( $this -> num_payment ) ? " ' " . $this -> db -> escape ( $this -> num_payment ) . " ' " : " null " ) . " , " ;
$sql .= " note_private= " . ( isset ( $this -> note_private ) ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " ) . " , " ;
$sql .= " note_public= " . ( isset ( $this -> note_public ) ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " ) . " , " ;
$sql .= " fk_bank= " . ( isset ( $this -> fk_bank ) ? $this -> fk_bank : " null " ) . " , " ;
2020-06-02 14:42:57 +02:00
$sql .= " fk_payment_loan= " . ( isset ( $this -> fk_payment_loan ) ? $this -> fk_payment_loan : " null " ) . " , " ;
2019-11-13 19:37:08 +01:00
$sql .= " fk_user_creat= " . ( isset ( $this -> fk_user_creat ) ? $this -> fk_user_creat : " null " ) . " , " ;
$sql .= " fk_user_modif= " . ( isset ( $this -> fk_user_modif ) ? $this -> fk_user_modif : " null " ) . " " ;
$sql .= " WHERE rowid= " . $this -> id ;
2017-06-02 17:48:29 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2019-11-13 19:37:08 +01:00
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2017-06-02 17:48:29 +02:00
// Commit or rollback
if ( $error )
{
$this -> db -> rollback ();
2019-11-13 19:37:08 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-06-02 17:48:29 +02:00
$this -> db -> commit ();
return 1 ;
}
}
/**
* Delete object in database
*
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-03-01 23:08:57 +01:00
public function delete ( $user , $notrigger = 0 )
2017-06-02 17:48:29 +02:00
{
global $conf , $langs ;
2019-11-13 19:37:08 +01:00
$error = 0 ;
2017-06-02 17:48:29 +02:00
$this -> db -> begin ();
2019-11-13 19:37:08 +01:00
if ( ! $error ) {
2017-06-02 17:48:29 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element ;
2019-11-13 19:37:08 +01:00
$sql .= " WHERE rowid= " . $this -> id ;
2017-06-02 17:48:29 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2019-11-13 19:37:08 +01:00
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2019-03-01 23:08:57 +01:00
}
2017-06-02 17:48:29 +02:00
// Commit or rollback
if ( $error )
{
2019-11-13 19:37:08 +01:00
foreach ( $this -> errors as $errmsg )
2017-06-02 17:48:29 +02:00
{
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2019-11-13 19:37:08 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-06-02 17:48:29 +02:00
}
$this -> db -> rollback ();
2019-11-13 19:37:08 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-06-02 17:48:29 +02:00
$this -> db -> commit ();
return 1 ;
}
2019-03-01 23:08:57 +01:00
}
2017-06-02 17:48:29 +02:00
2018-09-01 15:39:11 +02:00
/**
2018-10-15 20:36:11 +02:00
* Calculate Monthly Payments
2018-09-01 15:39:11 +02:00
*
* @ param double $capital Capital
* @ param double $rate rate
* @ param int $nbterm nb term
* @ return double mensuality
*/
2018-10-15 20:36:11 +02:00
public function calcMonthlyPayments ( $capital , $rate , $nbterm )
2017-06-02 17:48:29 +02:00
{
2019-11-13 19:37:08 +01:00
$result = '' ;
2017-06-02 17:48:29 +02:00
2018-08-31 22:11:52 +02:00
if ( ! empty ( $capital ) && ! empty ( $rate ) && ! empty ( $nbterm )) {
2019-11-13 19:37:08 +01:00
$result = ( $capital * ( $rate / 12 )) / ( 1 - pow (( 1 + ( $rate / 12 )), ( $nbterm * - 1 )));
2017-06-02 17:48:29 +02:00
}
return $result ;
}
/**
* Load all object in memory from database
*
2017-06-03 02:22:21 +02:00
* @ param int $loanid Id object
2017-06-02 17:48:29 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2018-10-15 20:36:11 +02:00
public function fetchAll ( $loanid )
2017-06-02 17:48:29 +02:00
{
global $langs ;
$sql = " SELECT " ;
2019-11-13 19:37:08 +01:00
$sql .= " t.rowid, " ;
$sql .= " t.fk_loan, " ;
$sql .= " t.datec, " ;
$sql .= " t.tms, " ;
$sql .= " t.datep, " ;
$sql .= " t.amount_capital, " ;
$sql .= " t.amount_insurance, " ;
$sql .= " t.amount_interest, " ;
$sql .= " t.fk_typepayment, " ;
$sql .= " t.num_payment, " ;
$sql .= " t.note_private, " ;
$sql .= " t.note_public, " ;
$sql .= " t.fk_bank, " ;
2020-06-02 14:42:57 +02:00
$sql .= " t.fk_payment_loan, " ;
2019-11-13 19:37:08 +01:00
$sql .= " t.fk_user_creat, " ;
$sql .= " t.fk_user_modif " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as t " ;
$sql .= " WHERE t.fk_loan = " . $loanid ;
2017-06-02 17:48:29 +02:00
2018-03-13 22:43:50 +01:00
dol_syslog ( get_class ( $this ) . " ::fetchAll " , LOG_DEBUG );
2019-11-13 19:37:08 +01:00
$resql = $this -> db -> query ( $sql );
2017-06-02 17:48:29 +02:00
if ( $resql )
{
2019-11-13 19:37:08 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql ))
2017-06-02 17:48:29 +02:00
{
2018-09-08 23:13:33 +02:00
$line = new LoanSchedule ( $this -> db );
2017-06-02 17:48:29 +02:00
$line -> id = $obj -> rowid ;
$line -> ref = $obj -> rowid ;
$line -> fk_loan = $obj -> fk_loan ;
$line -> datec = $this -> db -> jdate ( $obj -> datec );
$line -> tms = $this -> db -> jdate ( $obj -> tms );
$line -> datep = $this -> db -> jdate ( $obj -> datep );
$line -> amount_capital = $obj -> amount_capital ;
$line -> amount_insurance = $obj -> amount_insurance ;
$line -> amount_interest = $obj -> amount_interest ;
$line -> fk_typepayment = $obj -> fk_typepayment ;
$line -> num_payment = $obj -> num_payment ;
$line -> note_private = $obj -> note_private ;
$line -> note_public = $obj -> note_public ;
$line -> fk_bank = $obj -> fk_bank ;
2020-06-02 14:42:57 +02:00
$line -> fk_payment_loan = $obj -> fk_payment_loan ;
2017-06-02 17:48:29 +02:00
$line -> fk_user_creat = $obj -> fk_user_creat ;
$line -> fk_user_modif = $obj -> fk_user_modif ;
$this -> lines [] = $line ;
}
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-06-02 17:48:29 +02:00
return - 1 ;
}
}
2017-06-03 02:22:21 +02:00
/**
2018-10-15 20:36:11 +02:00
* transPayment
2017-06-03 02:22:21 +02:00
*
* @ return void
*/
2018-10-15 20:36:11 +02:00
private function transPayment ()
2017-06-02 17:48:29 +02:00
{
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' ;
$toinsert = array ();
$sql = " SELECT l.rowid " ;
2019-11-13 19:37:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " loan as l " ;
$sql .= " WHERE l.paid = 0 " ;
$resql = $this -> db -> query ( $sql );
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2018-10-15 20:40:59 +02:00
$lastrecorded = $this -> lastPayment ( $obj -> rowid );
2017-06-02 17:48:29 +02:00
$toinsert = $this -> paimenttorecord ( $obj -> rowid , $lastrecorded );
2019-11-13 19:37:08 +01:00
if ( count ( $toinsert ) > 0 ) {
foreach ( $toinsert as $echid ) {
2017-06-02 17:48:29 +02:00
$this -> db -> begin ();
2019-11-13 19:37:08 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " payment_loan " ;
$sql .= " (fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif) " ;
$sql .= " SELECT fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif FROM " . MAIN_DB_PREFIX . " loan_schedule WHERE rowid = " . $echid ;
$res = $this -> db -> query ( $sql );
if ( $res ) {
2017-06-02 17:48:29 +02:00
$this -> db -> commit ();
2019-11-13 19:37:08 +01:00
} else {
2017-06-02 17:48:29 +02:00
$this -> db -> rollback ();
}
}
}
}
}
}
2017-06-03 02:22:21 +02:00
/**
2018-10-15 20:40:59 +02:00
* lastpayment
2017-06-03 02:22:21 +02:00
*
* @ param int $loanid Loan id
* @ return int < 0 if KO , Date > 0 if OK
*/
2018-10-15 20:40:59 +02:00
private function lastPayment ( $loanid )
2017-06-02 17:48:29 +02:00
{
$sql = " SELECT p.datep " ;
2019-11-13 19:37:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " payment_loan as p " ;
$sql .= " WHERE p.fk_loan = " . $loanid ;
$sql .= " ORDER BY p.datep DESC " ;
$sql .= " LIMIT 1 " ;
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
$resql = $this -> db -> query ( $sql );
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
if ( $resql ) {
2017-06-02 17:48:29 +02:00
$obj = $this -> db -> fetch_object ( $resql );
return $this -> db -> jdate ( $obj -> datep );
2019-11-13 19:37:08 +01:00
} else {
2017-06-02 17:48:29 +02:00
return - 1 ;
}
}
2017-06-03 02:22:21 +02:00
/**
* paimenttorecord
*
* @ param int $loanid Loan id
* @ param int $datemax Date max
* @ return array Array of id
*/
2018-10-15 20:36:11 +02:00
public function paimenttorecord ( $loanid , $datemax )
2017-06-02 17:48:29 +02:00
{
2019-06-05 16:03:42 +02:00
2020-04-10 10:59:32 +02:00
$result = array ();
2019-06-05 16:03:42 +02:00
2017-06-02 17:48:29 +02:00
$sql = " SELECT p.rowid " ;
2019-11-13 19:37:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as p " ;
$sql .= " WHERE p.fk_loan = " . $loanid ;
if ( ! empty ( $datemax )) { $sql .= " AND p.datep > ' " . $this -> db -> idate ( $datemax ) . " ' " ; }
$sql .= " AND p.datep <= ' " . $this -> db -> idate ( dol_now ()) . " ' " ;
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
$resql = $this -> db -> query ( $sql );
2017-06-02 17:48:29 +02:00
2019-11-13 19:37:08 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql ))
2017-06-02 17:48:29 +02:00
{
$result [] = $obj -> rowid ;
}
}
return $result ;
}
}