2017-03-19 07:26:37 +01:00
< ? php
2021-03-01 04:49:06 +01:00
/* Copyright ( C ) 2017 - 2021 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2020-11-03 20:43:42 +01:00
* Copyright ( C ) 2018 - 2020 Frédéric France < frederic . france @ netlogic . fr >
2017-03-19 07:26:37 +01: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-03-19 07:26:37 +01:00
*/
/**
2017-06-17 07:54:35 +02:00
* \file htdocs / compta / bank / class / paymentvarious . class . php
* \ingroup bank
* \brief Class for various payment
2017-03-19 07:26:37 +01:00
*/
// Put here all includes required by your class file
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2017-03-19 07:26:37 +01:00
/**
* Class to manage various payments
*/
class PaymentVarious extends CommonObject
{
2018-08-23 17:07:27 +02:00
/**
* @ var string ID to identify managed object
*/
2020-04-10 10:59:32 +02:00
public $element = 'variouspayment' ;
2018-09-02 13:38:11 +02:00
2018-08-22 18:34:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2020-04-10 10:59:32 +02:00
public $table_element = 'payment_various' ;
2018-09-02 13:38:11 +02:00
2018-09-05 10:31:12 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2019-05-29 12:56:11 +02:00
public $picto = 'payment' ;
2017-03-19 07:26:37 +01:00
2018-08-22 11:24:31 +02:00
/**
* @ var int ID
*/
public $id ;
2018-09-02 13:38:11 +02:00
2018-08-31 18:36:15 +02:00
/**
* @ var string Ref
*/
public $ref ;
2020-11-05 20:13:13 +01:00
/**
* @ var int timestamp
*/
2018-09-02 10:42:31 +02:00
public $tms ;
public $datep ;
public $datev ;
2020-11-05 20:13:13 +01:00
/**
* @ var int sens of operation
*/
2018-09-02 10:42:31 +02:00
public $sens ;
public $amount ;
public $type_payment ;
public $num_payment ;
2021-03-01 04:49:06 +01:00
public $chqemetteur ;
public $chqbank ;
2020-10-31 14:32:18 +01:00
public $category_transaction ;
2018-09-02 13:38:11 +02:00
2018-08-22 11:39:37 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var string various payments label
*/
public $label ;
2018-09-02 13:38:11 +02:00
2020-11-03 20:43:42 +01:00
/**
* @ var string accountancy code
*/
2018-09-02 10:42:31 +02:00
public $accountancy_code ;
2018-10-09 15:38:42 +02:00
2020-11-03 20:43:42 +01:00
/**
* @ var string subledger account
*/
2020-10-31 14:32:18 +01:00
public $subledger_account ;
2019-02-01 15:50:12 +01:00
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_project ;
2018-10-09 15:38:42 +02:00
/**
2020-12-23 22:17:05 +01:00
* @ var int Bank account ID
2020-10-31 14:32:18 +01:00
*/
2020-12-23 22:17:05 +01:00
public $fk_account ;
2018-10-09 15:38:42 +02:00
2020-11-05 20:13:13 +01:00
/**
2020-12-23 22:17:05 +01:00
* @ var int Bank account ID
* @ deprecated See fk_account
2020-11-05 20:13:13 +01:00
*/
2020-12-23 22:17:05 +01:00
public $accountid ;
2020-11-05 20:13:13 +01:00
/**
2020-12-23 22:17:05 +01:00
* @ var int ID record into llx_bank
2020-11-05 20:13:13 +01:00
*/
2020-12-23 22:17:05 +01:00
public $fk_bank ;
/**
* @ var int transaction category
*/
public $categorie_transaction ;
2020-11-05 20:13:13 +01:00
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_author ;
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_modif ;
2017-03-19 07:26:37 +01:00
2023-03-13 19:12:44 +01:00
/**
* @ var int Type of bank account if the payment is on a bank account
*/
public $fk_type ;
/**
* @ var int 1 if the payment is on a bank account line that is conciliated
*/
public $rappro ;
/**
* @ var string ID of bank receipt
*/
public $bank_num_releve ;
2020-09-11 09:55:34 +02:00
/**
* 'type' if the field format ( 'integer' , 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]' , 'varchar(x)' , 'double(24,8)' , 'real' , 'price' , 'text' , 'html' , 'date' , 'datetime' , 'timestamp' , 'duration' , 'mail' , 'phone' , 'url' , 'password' )
* Note : Filter can be a string like " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL) "
* 'label' the translation key .
* 'enabled' is a condition when the field must be managed ( Example : 1 or ' $conf -> global -> MY_SETUP_PARAM )
* 'position' is the sort order of field .
* 'notnull' is set to 1 if not null in database . Set to - 1 if we must set data to null if empty ( '' or 0 ) .
* 'visible' says if field is visible in list ( Examples : 0 = Not visible , 1 = Visible on list and create / update / view forms , 2 = Visible on list only , 3 = Visible on create / update / view form only ( not list ), 4 = Visible on list and update / view form only ( not create ) . 5 = Visible on list and view only ( not create / not update ) . Using a negative value means field is not shown by default on list but can be selected for viewing )
* 'noteditable' says if field is not editable ( 1 or 0 )
* 'default' is a default value for creation ( can still be overwrote by the Setup of Default Values if field is editable in creation form ) . Note : If default is set to '(PROV)' and field is 'ref' , the default value will be set to '(PROVid)' where id is rowid when a new record is created .
* 'index' if we want an index in database .
* 'foreignkey' => 'tablename.field' if the field is a foreign key ( it is recommanded to name the field fk_ ... ) .
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button .
* 'isameasure' must be set to 1 if you want to have a total on list for this field . Field type must be summable like integer or double ( 24 , 8 ) .
* 'css' is the CSS style to use on field . For example : 'maxwidth200'
* 'help' is a string visible as a tooltip on field
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute . In most cases , this is never set into the definition of $fields into class , but is set dynamically by some part of code .
2021-04-29 12:10:55 +02:00
* 'arrayofkeyval' to set list of value if type is a list of predefined values . For example : array ( " 0 " => " Draft " , " 1 " => " Active " , " -1 " => " Cancel " )
2020-09-11 09:55:34 +02:00
* 'autofocusoncreate' to have field having the focus on a create form . Only 1 field should have this property set to 1.
* 'comment' is not used . You can store here any text of your choice . It is not used by application .
*
* Note : To have value dynamic , you can set value to 0 in definition and edit the value on the fly into the constructor .
*/
// BEGIN MODULEBUILDER PROPERTIES
2020-11-03 20:43:42 +01:00
/**
* @ var array fields definition
*/
2020-09-11 09:55:34 +02:00
public $fields = array (
// TODO: fill this array
);
// END MODULEBUILDER PROPERTIES
2017-03-19 07:26:37 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2022-05-18 11:00:43 +02:00
public function __construct ( DoliDB $db )
2017-03-19 07:26:37 +01:00
{
$this -> db = $db ;
$this -> element = 'payment_various' ;
$this -> table_element = 'payment_various' ;
}
/**
* Update database
*
* @ param User $user User that modify
2018-09-04 20:48:35 +02:00
* @ param int $notrigger 0 = no , 1 = yes ( no update trigger )
2017-03-19 07:26:37 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function update ( $user = null , $notrigger = 0 )
2017-03-19 07:26:37 +01:00
{
global $conf , $langs ;
2020-04-10 10:59:32 +02:00
$error = 0 ;
2017-03-19 07:26:37 +01:00
// Clean parameters
2020-04-10 10:59:32 +02:00
$this -> amount = trim ( $this -> amount );
$this -> label = trim ( $this -> label );
$this -> note = trim ( $this -> note );
2018-10-09 20:31:14 +02:00
$this -> fk_bank = ( int ) $this -> fk_bank ;
$this -> fk_user_author = ( int ) $this -> fk_user_author ;
$this -> fk_user_modif = ( int ) $this -> fk_user_modif ;
2017-03-19 07:26:37 +01:00
$this -> db -> begin ();
// Update request
2017-06-17 07:54:35 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " payment_various SET " ;
2021-02-23 21:09:01 +01:00
if ( $this -> tms ) {
$sql .= " tms=' " . $this -> db -> idate ( $this -> tms ) . " ', " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " datep=' " . $this -> db -> idate ( $this -> datep ) . " ', " ;
$sql .= " datev=' " . $this -> db -> idate ( $this -> datev ) . " ', " ;
$sql .= " sens= " . ( int ) $this -> sens . " , " ;
$sql .= " amount= " . price2num ( $this -> amount ) . " , " ;
2020-12-30 08:11:20 +01:00
$sql .= " fk_typepayment= " . ( int ) $this -> type_payment . " , " ;
2020-04-10 10:59:32 +02:00
$sql .= " num_payment=' " . $this -> db -> escape ( $this -> num_payment ) . " ', " ;
$sql .= " label=' " . $this -> db -> escape ( $this -> label ) . " ', " ;
$sql .= " note=' " . $this -> db -> escape ( $this -> note ) . " ', " ;
$sql .= " accountancy_code=' " . $this -> db -> escape ( $this -> accountancy_code ) . " ', " ;
2020-10-31 14:32:18 +01:00
$sql .= " subledger_account=' " . $this -> db -> escape ( $this -> subledger_account ) . " ', " ;
2020-04-10 10:59:32 +02:00
$sql .= " fk_projet=' " . $this -> db -> escape ( $this -> fk_project ) . " ', " ;
$sql .= " fk_bank= " . ( $this -> fk_bank > 0 ? $this -> fk_bank : " null " ) . " , " ;
$sql .= " fk_user_author= " . ( int ) $this -> fk_user_author . " , " ;
$sql .= " fk_user_modif= " . ( int ) $this -> fk_user_modif ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2017-03-19 07:26:37 +01:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2020-11-03 20:43:42 +01:00
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-03-19 07:26:37 +01:00
return - 1 ;
}
2020-11-03 20:43:42 +01:00
if ( ! $notrigger ) {
2017-06-17 07:54:35 +02:00
// Call trigger
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'PAYMENT_VARIOUS_MODIFY' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-06-17 07:54:35 +02:00
// End call triggers
2017-03-19 07:26:37 +01:00
}
2020-11-03 20:43:42 +01:00
if ( ! $error ) {
2017-03-19 07:26:37 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-03-19 07:26:37 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* Load object in memory from database
*
* @ param int $id id object
* @ param User $user User that load
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id , $user = null )
2017-03-19 07:26:37 +01:00
{
$sql = " SELECT " ;
2020-04-10 10:59:32 +02:00
$sql .= " v.rowid, " ;
$sql .= " v.tms, " ;
$sql .= " v.datep, " ;
$sql .= " v.datev, " ;
$sql .= " v.sens, " ;
$sql .= " v.amount, " ;
$sql .= " v.fk_typepayment, " ;
$sql .= " v.num_payment, " ;
$sql .= " v.label, " ;
2023-03-13 19:12:44 +01:00
$sql .= " v.note as note_private, " ;
2020-04-10 10:59:32 +02:00
$sql .= " v.accountancy_code, " ;
$sql .= " v.subledger_account, " ;
$sql .= " v.fk_projet as fk_project, " ;
$sql .= " v.fk_bank, " ;
$sql .= " v.fk_user_author, " ;
$sql .= " v.fk_user_modif, " ;
$sql .= " b.fk_account, " ;
$sql .= " b.fk_type, " ;
2023-03-13 19:12:44 +01:00
$sql .= " b.rappro, " ;
$sql .= " b.num_releve as bank_num_releve " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " payment_various as v " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " bank as b ON v.fk_bank = b.rowid " ;
2021-03-14 11:48:39 +01:00
$sql .= " WHERE v.rowid = " . (( int ) $id );
2017-03-19 07:26:37 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2017-03-19 07:26:37 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2019-02-01 15:50:12 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> rowid ;
$this -> tms = $this -> db -> jdate ( $obj -> tms );
$this -> datep = $this -> db -> jdate ( $obj -> datep );
$this -> datev = $this -> db -> jdate ( $obj -> datev );
$this -> sens = $obj -> sens ;
$this -> amount = $obj -> amount ;
$this -> type_payment = $obj -> fk_typepayment ;
$this -> num_payment = $obj -> num_payment ;
$this -> label = $obj -> label ;
2023-03-13 19:12:44 +01:00
$this -> note = $obj -> note_private ; // For backward compatibility
$this -> note_private = $obj -> note_private ;
2019-02-01 15:50:12 +01:00
$this -> subledger_account = $obj -> subledger_account ;
$this -> accountancy_code = $obj -> accountancy_code ;
$this -> fk_project = $obj -> fk_project ;
$this -> fk_bank = $obj -> fk_bank ;
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> fk_user_modif = $obj -> fk_user_modif ;
$this -> fk_account = $obj -> fk_account ;
$this -> fk_type = $obj -> fk_type ;
$this -> rappro = $obj -> rappro ;
2023-03-13 19:12:44 +01:00
$this -> bank_num_releve = $obj -> bank_num_releve ;
2017-03-19 07:26:37 +01:00
}
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-03-19 07:26:37 +01:00
return - 1 ;
}
}
/**
* Delete object in database
*
* @ param User $user User that delete
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function delete ( $user )
2017-03-19 07:26:37 +01:00
{
global $conf , $langs ;
2020-04-10 10:59:32 +02:00
$error = 0 ;
2017-03-19 07:26:37 +01:00
// Call trigger
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'PAYMENT_VARIOUS_DELETE' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
return - 1 ;
}
2017-03-19 07:26:37 +01:00
// End call triggers
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " payment_various " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2017-03-19 07:26:37 +01:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2020-11-03 20:43:42 +01:00
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-03-19 07:26:37 +01:00
return - 1 ;
}
return 1 ;
}
/**
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
*/
2020-10-31 14:32:18 +01:00
public function initAsSpecimen ()
2017-03-19 07:26:37 +01:00
{
2020-04-10 10:59:32 +02:00
$this -> id = 0 ;
$this -> tms = '' ;
$this -> datep = '' ;
$this -> datev = '' ;
$this -> sens = '' ;
$this -> amount = '' ;
$this -> label = '' ;
$this -> accountancy_code = '' ;
2020-10-31 14:32:18 +01:00
$this -> subledger_account = '' ;
2020-04-10 10:59:32 +02:00
$this -> note = '' ;
$this -> fk_bank = '' ;
$this -> fk_user_author = '' ;
$this -> fk_user_modif = '' ;
2017-03-19 07:26:37 +01:00
}
2020-10-06 10:04:15 +02:00
/**
* Check if a miscellaneous payment can be created into database
*
* @ return boolean True or false
*/
public function check ()
{
$newamount = price2num ( $this -> amount , 'MT' );
2020-10-07 09:36:29 +02:00
// Validation of parameters
2020-11-03 20:43:42 +01:00
if ( ! ( $newamount ) > 0 || empty ( $this -> datep )) {
2020-10-06 10:04:15 +02:00
return false ;
}
return true ;
}
2017-12-10 14:05:36 +01:00
/**
* Create in database
*
* @ param User $user User that create
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function create ( $user )
2017-03-19 07:26:37 +01:00
{
2020-04-10 10:59:32 +02:00
global $conf , $langs ;
2017-03-19 07:26:37 +01:00
2020-04-10 10:59:32 +02:00
$error = 0 ;
$now = dol_now ();
2017-03-19 07:26:37 +01:00
// Clean parameters
2020-04-10 10:59:32 +02:00
$this -> amount = price2num ( trim ( $this -> amount ));
$this -> label = trim ( $this -> label );
$this -> note = trim ( $this -> note );
2018-10-09 20:31:14 +02:00
$this -> fk_bank = ( int ) $this -> fk_bank ;
$this -> fk_user_author = ( int ) $this -> fk_user_author ;
$this -> fk_user_modif = ( int ) $this -> fk_user_modif ;
2020-12-23 22:17:05 +01:00
$this -> fk_account = ( int ) $this -> fk_account ;
if ( empty ( $this -> fk_account ) && isset ( $this -> accountid )) { // For compatibility
$this -> fk_account = $this -> accountid ;
}
2017-03-19 07:26:37 +01:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( ! $this -> label ) {
2020-04-10 10:59:32 +02:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " Label " ));
2017-03-19 07:26:37 +01:00
return - 3 ;
}
2021-02-23 21:09:01 +01:00
if ( $this -> amount < 0 || $this -> amount == '' ) {
2020-04-10 10:59:32 +02:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " Amount " ));
2017-03-19 07:26:37 +01:00
return - 5 ;
}
2022-08-29 11:21:34 +02:00
if ( isModEnabled ( 'banque' ) && ( empty ( $this -> fk_account ) || $this -> fk_account <= 0 )) {
2020-12-23 22:17:05 +01:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " BankAccount " ));
2017-03-19 07:26:37 +01:00
return - 6 ;
}
2022-08-29 11:21:34 +02:00
if ( isModEnabled ( 'banque' ) && ( empty ( $this -> type_payment ) || $this -> type_payment <= 0 )) {
2020-04-10 10:59:32 +02:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " PaymentMode " ));
2017-03-19 07:26:37 +01:00
return - 7 ;
}
$this -> db -> begin ();
// Insert into llx_payment_various
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " payment_various ( " ;
2020-04-10 10:59:32 +02:00
$sql .= " datep " ;
$sql .= " , datev " ;
$sql .= " , sens " ;
$sql .= " , amount " ;
$sql .= " , fk_typepayment " ;
$sql .= " , num_payment " ;
2021-02-23 21:09:01 +01:00
if ( $this -> note ) {
$sql .= " , note " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " , label " ;
$sql .= " , accountancy_code " ;
$sql .= " , subledger_account " ;
$sql .= " , fk_projet " ;
$sql .= " , fk_user_author " ;
$sql .= " , datec " ;
$sql .= " , fk_bank " ;
$sql .= " , entity " ;
$sql .= " ) " ;
$sql .= " VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $this -> datep ) . " ' " ;
$sql .= " , ' " . $this -> db -> idate ( $this -> datev ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> sens ) . " ' " ;
$sql .= " , " . price2num ( $this -> amount );
$sql .= " , ' " . $this -> db -> escape ( $this -> type_payment ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> num_payment ) . " ' " ;
2021-02-23 21:09:01 +01:00
if ( $this -> note ) {
$sql .= " , ' " . $this -> db -> escape ( $this -> note ) . " ' " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " , ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> accountancy_code ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> subledger_account ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . ( $this -> fk_project > 0 ? (( int ) $this -> fk_project ) : 0 );
$sql .= " , " . (( int ) $user -> id );
2020-04-10 10:59:32 +02:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
2020-12-23 22:17:05 +01:00
$sql .= " , NULL " ; // Filled later
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $conf -> entity );
2020-04-10 10:59:32 +02:00
$sql .= " ) " ;
2017-03-19 07:26:37 +01:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-03-19 07:26:37 +01:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " payment_various " );
2018-01-29 18:22:26 +01:00
$this -> ref = $this -> id ;
2017-03-19 07:26:37 +01:00
2021-02-23 21:09:01 +01:00
if ( $this -> id > 0 ) {
2022-08-29 11:21:34 +02:00
if ( isModEnabled ( 'banque' ) && ! empty ( $this -> amount )) {
2017-03-19 07:26:37 +01:00
// Insert into llx_bank
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php' ;
$acc = new Account ( $this -> db );
2020-12-23 22:17:05 +01:00
$result = $acc -> fetch ( $this -> fk_account );
2021-02-23 21:09:01 +01:00
if ( $result <= 0 ) {
dol_print_error ( $this -> db );
}
2017-03-19 07:26:37 +01:00
// Insert payment into llx_bank
2017-03-19 15:23:35 +01:00
// Add link 'payment_various' in bank_url between payment and bank transaction
2020-04-10 10:59:32 +02:00
$sign = 1 ;
2021-02-23 21:09:01 +01:00
if ( $this -> sens == '0' ) {
$sign = - 1 ;
}
2017-03-19 15:23:35 +01:00
2020-10-31 14:32:18 +01:00
$bank_line_id = $acc -> addline (
2017-03-19 07:26:37 +01:00
$this -> datep ,
$this -> type_payment ,
$this -> label ,
2018-09-08 18:38:24 +02:00
$sign * abs ( $this -> amount ),
2017-03-19 07:26:37 +01:00
$this -> num_payment ,
2020-10-31 14:32:18 +01:00
( $this -> category_transaction > 0 ? $this -> category_transaction : 0 ),
2020-06-04 11:29:51 +02:00
$user ,
2021-03-01 04:49:06 +01:00
$this -> chqemetteur ,
$this -> chqbank ,
2020-06-04 11:29:51 +02:00
'' ,
$this -> datev
2017-03-19 07:26:37 +01:00
);
2020-12-23 22:17:05 +01:00
// Update fk_bank into llx_payment_various
2017-03-19 07:26:37 +01:00
// So we know the payment which has generate the banking ecriture
2020-11-03 20:43:42 +01:00
if ( $bank_line_id > 0 ) {
2017-03-19 07:26:37 +01:00
$this -> update_fk_bank ( $bank_line_id );
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $acc -> error ;
2017-03-19 07:26:37 +01:00
$error ++ ;
}
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-03-19 07:26:37 +01:00
// Add link 'payment_various' in bank_url between payment and bank transaction
2020-04-10 10:59:32 +02:00
$url = DOL_URL_ROOT . '/compta/bank/various_payment/card.php?id=' ;
2017-03-19 07:26:37 +01:00
2020-04-10 10:59:32 +02:00
$result = $acc -> add_url_line ( $bank_line_id , $this -> id , $url , " (VariousPayment) " , " payment_various " );
2021-02-23 21:09:01 +01:00
if ( $result <= 0 ) {
2020-04-10 10:59:32 +02:00
$this -> error = $acc -> error ;
2017-03-19 07:26:37 +01:00
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( $result <= 0 ) {
2020-04-10 10:59:32 +02:00
$this -> error = $acc -> error ;
2017-03-19 07:26:37 +01:00
$error ++ ;
}
}
2017-12-10 14:05:36 +01:00
// Call trigger
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'PAYMENT_VARIOUS_CREATE' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-12-10 14:05:36 +01:00
// End call triggers
2021-02-23 21:09:01 +01:00
} else {
$error ++ ;
}
2017-03-19 07:26:37 +01:00
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-03-19 07:26:37 +01:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2017-03-19 07:26:37 +01:00
$this -> db -> rollback ();
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> error ();
2017-03-19 07:26:37 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-03-19 07:26:37 +01:00
/**
* Update link between payment various and line generate into llx_bank
*
2017-12-10 14:05:36 +01:00
* @ param int $id_bank Id bank account
* @ return int < 0 if KO , > 0 if OK
2017-03-19 07:26:37 +01:00
*/
2020-10-31 14:32:18 +01:00
public function update_fk_bank ( $id_bank )
2017-03-19 07:26:37 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'payment_various SET fk_bank = ' . (( int ) $id_bank );
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-03-19 07:26:37 +01:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-03-19 07:26:37 +01:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-03-19 07:26:37 +01:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2017-09-01 07:21:27 +02:00
/**
* Retourne le libelle du statut
*
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Libelle
*/
2020-10-31 14:32:18 +01:00
public function getLibStatut ( $mode = 0 )
2017-09-01 07:21:27 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> LibStatut ( $this -> statut , $mode );
2017-09-01 07:21:27 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-09-01 07:21:27 +02:00
/**
* Renvoi le libelle d ' un statut donne
*
2019-11-01 23:58:14 +01:00
* @ param int $status Id status
2017-09-01 07:21:27 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Libelle
*/
2020-10-31 14:32:18 +01:00
public function LibStatut ( $status , $mode = 0 )
2017-09-01 07:21:27 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2022-07-26 11:53:00 +02:00
if ( empty ( $this -> labelStatus ) || empty ( $this -> labelStatusShort )) {
global $langs ;
//$langs->load("mymodule@mymodule");
/* $this -> labelStatus [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> labelStatus [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
$this -> labelStatus [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'Disabled' );
$this -> labelStatusShort [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> labelStatusShort [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
$this -> labelStatusShort [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'Disabled' ); */
2017-09-01 07:21:27 +02:00
}
2022-07-26 11:53:00 +02:00
$statusType = 'status' . $status ;
return dolGetStatus ( $this -> labelStatus [ $status ], $this -> labelStatusShort [ $status ], '' , $statusType , $mode );
2017-09-01 07:21:27 +02:00
}
2017-03-19 07:26:37 +01:00
/**
* Send name clicable ( with possibly the picto )
*
2018-09-08 18:38:24 +02:00
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param string $option link option
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2020-10-31 14:32:18 +01:00
* @ param int $notooltip 1 = Disable tooltip
2020-11-03 20:43:42 +01:00
* @ param string $morecss morecss string
2018-09-08 18:38:24 +02:00
* @ return string String with URL
2017-03-19 07:26:37 +01:00
*/
2020-11-03 20:43:42 +01:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $save_lastsearch_value = - 1 , $notooltip = 0 , $morecss = '' )
2017-03-19 07:26:37 +01:00
{
2018-10-04 18:27:49 +02:00
global $db , $conf , $langs , $hookmanager ;
2017-03-19 07:26:37 +01:00
global $langs ;
2021-02-23 21:09:01 +01:00
if ( ! empty ( $conf -> dol_no_mouse_hover )) {
$notooltip = 1 ; // Force disable tooltips
}
2018-10-04 18:27:49 +02:00
2020-04-10 10:59:32 +02:00
$result = '' ;
2017-03-19 07:26:37 +01:00
2020-04-10 10:59:32 +02:00
$label = '<u>' . $langs -> trans ( " ShowVariousPayment " ) . '</u>' ;
$label .= '<br>' ;
$label .= '<b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
2018-10-04 18:27:49 +02:00
$url = DOL_URL_ROOT . '/compta/bank/various_payment/card.php?id=' . $this -> id ;
2021-02-23 21:09:01 +01:00
if ( $option != 'nolink' ) {
2018-10-04 18:27:49 +02:00
// Add param to save lastsearch_values or not
2020-04-10 10:59:32 +02:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-23 21:09:01 +01:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2018-10-04 18:27:49 +02:00
}
2020-04-10 10:59:32 +02:00
$linkclose = '' ;
2021-02-23 21:09:01 +01:00
if ( empty ( $notooltip )) {
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER )) {
2020-04-10 10:59:32 +02:00
$label = $langs -> trans ( " ShowMyObject " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
2018-10-04 18:27:49 +02:00
}
2020-04-10 10:59:32 +02:00
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip' . ( $morecss ? ' ' . $morecss : '' ) . '"' ;
2021-02-23 21:09:01 +01:00
} else {
$linkclose = ( $morecss ? ' class="' . $morecss . '"' : '' );
}
2018-10-04 18:27:49 +02:00
$linkstart = '<a href="' . $url . '"' ;
2020-04-10 10:59:32 +02:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2017-03-19 07:26:37 +01:00
2017-11-17 14:57:14 +01:00
$result .= $linkstart ;
2021-02-23 21:09:01 +01:00
if ( $withpicto ) {
$result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
}
if ( $withpicto != 2 ) {
$result .= $this -> ref ;
}
2017-11-17 14:57:14 +01:00
$result .= $linkend ;
2018-10-04 18:27:49 +02:00
//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
global $action ;
$hookmanager -> initHooks ( array ( 'variouspayment' ));
2021-11-22 19:57:26 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2020-04-10 10:59:32 +02:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 21:09:01 +01:00
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2017-03-19 07:26:37 +01:00
return $result ;
}
/**
* Information on record
*
2017-12-10 14:05:36 +01:00
* @ param int $id Id of record
* @ return void
2017-03-19 07:26:37 +01:00
*/
2020-10-31 14:32:18 +01:00
public function info ( $id )
{
2017-03-19 07:26:37 +01:00
$sql = 'SELECT v.rowid, v.datec, v.fk_user_author' ;
2020-04-10 10:59:32 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'payment_various as v' ;
2021-03-14 11:48:39 +01:00
$sql .= ' WHERE v.rowid = ' . (( int ) $id );
2017-03-19 07:26:37 +01:00
dol_syslog ( get_class ( $this ) . '::info' , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2017-03-19 07:26:37 +01:00
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
2021-02-23 21:09:01 +01:00
if ( $obj -> fk_user_author ) {
2017-03-19 07:26:37 +01:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
$this -> user_creation = $cuser ;
}
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
2021-02-23 21:09:01 +01:00
if ( $obj -> fk_user_modif ) {
2017-03-19 07:26:37 +01:00
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modif );
$this -> user_modif = $muser ;
}
$this -> date_modif = $this -> db -> jdate ( $obj -> tms );
}
$this -> db -> free ( $result );
2020-05-21 15:05:19 +02:00
} else {
2017-03-19 07:26:37 +01:00
dol_print_error ( $this -> db );
}
}
2020-01-10 02:43:24 +01:00
/**
* Return if a various payment linked to a bank line id was dispatched into bookkeeping
*
* @ return int < 0 if KO , 0 = no , 1 = yes
*/
public function getVentilExportCompta ()
{
$banklineid = $this -> fk_bank ;
$alreadydispatched = 0 ;
$type = 'bank' ;
2021-03-30 17:53:25 +02:00
$sql = " SELECT COUNT(ab.rowid) as nb FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping as ab WHERE ab.doc_type=' " . $this -> db -> escape ( $type ) . " ' AND ab.fk_doc = " . (( int ) $banklineid );
2020-01-10 02:43:24 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2020-01-10 02:43:24 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 21:09:01 +01:00
if ( $obj ) {
2020-01-10 02:43:24 +01:00
$alreadydispatched = $obj -> nb ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-01-10 02:43:24 +01:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( $alreadydispatched ) {
2020-01-10 02:43:24 +01:00
return 1 ;
}
return 0 ;
}
2017-03-19 07:26:37 +01:00
}