dolibarr/htdocs/compta/tva/class/tva.class.php

910 lines
24 KiB
PHP
Raw Normal View History

2008-04-03 21:35:46 +02:00
<?php
2018-10-15 18:47:12 +02:00
/* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
2019-01-28 21:39:22 +01:00
* Copyright (C) 2011-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
2018-10-15 18:47:12 +02:00
* Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
2021-01-18 17:08:17 +01:00
* Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
2008-04-03 21:35:46 +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
2008-04-03 21:35:46 +02:00
* (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/>.
2008-04-03 21:35:46 +02:00
*/
/**
* \file htdocs/compta/tva/class/tva.class.php
* \ingroup tax
*/
2008-04-03 21:35:46 +02:00
// Put here all includes required by your class file
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
2008-04-03 21:35:46 +02:00
/**
* Put here description of your class
*/
2008-04-03 21:35:46 +02:00
class Tva extends CommonObject
{
2018-08-23 17:07:27 +02:00
/**
* @var string ID to identify managed object
*/
public $element = 'tva';
2018-09-02 21:08:57 +02:00
2018-08-22 18:34:50 +02:00
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'tva';
2018-09-02 21:08:57 +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
*/
public $picto = 'payment';
2010-05-08 21:31:43 +02:00
/**
* @deprecated
* @see $amount
*/
public $total;
2018-09-05 10:31:12 +02:00
public $tms;
public $datep;
public $datev;
public $amount;
public $type_payment;
public $num_payment;
2018-09-02 21:08:57 +02:00
/**
2022-05-22 17:11:41 +02:00
* @var integer|string totalpaid
*/
2022-05-22 17:11:41 +02:00
public $totalpaid;
2018-08-22 11:39:37 +02:00
/**
2021-02-23 21:09:01 +01:00
* @var string label
*/
public $label;
2018-09-02 21:08:57 +02:00
2021-02-23 21:09:01 +01:00
/**
* @var int ID
*/
2018-09-05 10:31:12 +02:00
public $fk_bank;
2018-10-09 15:46:10 +02:00
2020-11-14 22:25:25 +01:00
/**
* @var int accountid
*/
public $accountid;
2018-10-09 15:46:10 +02:00
/**
2021-02-23 21:09:01 +01:00
* @var int ID
*/
2018-09-05 10:31:12 +02:00
public $fk_user_creat;
2018-10-09 15:46:10 +02:00
/**
2021-02-23 21:09:01 +01:00
* @var int ID
*/
2018-09-05 10:31:12 +02:00
public $fk_user_modif;
2008-04-03 21:35:46 +02:00
/**
* @var integer|string paiementtype
*/
public $paiementtype;
2021-01-18 17:08:17 +01:00
const STATUS_UNPAID = 0;
const STATUS_PAID = 1;
2021-02-23 21:09:01 +01:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
*
* @param DoliDB $db Database handler
2021-02-23 21:09:01 +01:00
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Create in database
*
* @param User $user User that create
* @return int <0 if KO, >0 if OK
*/
public function create($user)
{
global $conf, $langs;
2010-05-08 21:31:43 +02:00
$error = 0;
$now = dol_now();
2012-03-18 19:23:01 +01:00
2008-04-03 21:35:46 +02:00
// Clean parameters
$this->amount = trim($this->amount);
$this->label = trim($this->label);
2021-01-18 17:08:17 +01:00
$this->type_payment = (int) $this->type_payment;
$this->note = trim($this->note);
2021-01-18 17:08:17 +01:00
$this->fk_account = (int) $this->fk_account;
2018-10-15 18:47:12 +02:00
$this->fk_user_creat = (int) $this->fk_user_creat;
$this->fk_user_modif = (int) $this->fk_user_modif;
2008-04-03 21:35:46 +02:00
// Check parameters
// Put here code to add control on parameters values
2010-05-08 21:31:43 +02:00
$this->db->begin();
// Insert request
2008-04-03 21:35:46 +02:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."tva(";
2022-04-12 14:45:05 +02:00
$sql .= "entity,";
$sql .= "datec,";
$sql .= "datep,";
$sql .= "datev,";
$sql .= "amount,";
$sql .= "label,";
$sql .= "note,";
2021-01-18 17:08:17 +01:00
$sql .= "fk_account,";
$sql .= "fk_typepayment,";
$sql .= "fk_user_creat,";
$sql .= "fk_user_modif";
2021-02-23 21:09:01 +01:00
$sql .= ") VALUES (";
2022-04-12 22:02:46 +02:00
$sql .= " ".((int) $conf->entity).", ";
$sql .= " '".$this->db->idate($now)."',";
$sql .= " '".$this->db->idate($this->datep)."',";
$sql .= " '".$this->db->idate($this->datev)."',";
$sql .= " '".$this->db->escape($this->amount)."',";
$sql .= " '".$this->db->escape($this->label)."',";
$sql .= " '".$this->db->escape($this->note)."',";
2021-01-18 17:08:17 +01:00
$sql .= " '".$this->db->escape($this->fk_account)."',";
$sql .= " '".$this->db->escape($this->type_payment)."',";
2022-04-12 16:29:25 +02:00
$sql .= " ".($this->fk_user_creat > 0 ? (int) $this->fk_user_creat : (int) $user->id).",";
$sql .= " ".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
$sql .= ")";
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");
// Call trigger
$result = $this->call_trigger('TVA_CREATE', $user);
if ($result < 0) {
$error++;
}
// End call triggers
if (!$error) {
$this->db->commit();
return $this->id;
} else {
$this->db->rollback();
return -1;
2021-02-23 21:09:01 +01:00
}
} else {
$this->error = "Error ".$this->db->lasterror();
$this->db->rollback();
return -1;
2021-02-23 21:09:01 +01:00
}
}
/**
* Update database
*
* @param User $user User that modify
* @param int $notrigger 0=no, 1=yes (no update trigger)
* @return int <0 if KO, >0 if OK
*/
public function update($user, $notrigger = 0)
{
global $conf, $langs;
2010-05-08 21:31:43 +02:00
$error = 0;
2012-03-18 19:23:01 +01:00
2008-04-03 21:35:46 +02:00
// Clean parameters
$this->amount = trim($this->amount);
$this->label = trim($this->label);
$this->note = trim($this->note);
2018-10-15 18:47:12 +02:00
$this->fk_user_creat = (int) $this->fk_user_creat;
$this->fk_user_modif = (int) $this->fk_user_modif;
2008-04-03 21:35:46 +02:00
// Check parameters
// Put here code to add control on parameters values
$this->db->begin();
// Update request
2021-02-23 21:09:01 +01:00
$sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
$sql .= " tms='".$this->db->idate($this->tms)."',";
$sql .= " datep='".$this->db->idate($this->datep)."',";
$sql .= " datev='".$this->db->idate($this->datev)."',";
$sql .= " amount=".price2num($this->amount).",";
$sql .= " label='".$this->db->escape($this->label)."',";
$sql .= " note='".$this->db->escape($this->note)."',";
2021-06-09 15:36:47 +02:00
$sql .= " fk_user_creat=".((int) $this->fk_user_creat).",";
$sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? $this->fk_user_modif : $user->id)."";
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid=".((int) $this->id);
2021-02-23 21:09:01 +01:00
dol_syslog(get_class($this)."::update", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
$this->error = "Error ".$this->db->lasterror();
$error++;
}
if (!$error && !$notrigger) {
// Call trigger
$result = $this->call_trigger('TVA_MODIFY', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (!$error) {
$this->db->commit();
return 1;
} else {
$this->db->rollback();
return -1;
}
}
2021-01-18 17:08:17 +01:00
/**
* Tag TVA as payed completely
*
* @param User $user Object user making change
* @return int <0 if KO, >0 if OK
*/
2021-02-09 09:03:29 +01:00
public function setPaid($user)
2021-01-18 17:08:17 +01:00
{
// phpcs:enable
$sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
$sql .= " paye = 1";
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = ".((int) $this->id);
2021-02-09 09:03:29 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
return 1;
} else {
return -1;
}
}
/**
2021-01-18 17:08:17 +01:00
* Remove tag payed on TVA
*
2021-01-18 17:08:17 +01:00
* @param User $user Object user making change
* @return int <0 if KO, >0 if OK
*/
2021-02-09 09:03:29 +01:00
public function setUnpaid($user)
{
2021-01-18 17:08:17 +01:00
// phpcs:enable
$sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
$sql .= " paye = 0";
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = ".((int) $this->id);
2021-02-09 09:03:29 +01:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
return 1;
} else {
return -1;
}
2021-01-18 17:08:17 +01:00
}
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
/**
* 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
*/
public function fetch($id, $user = null)
{
global $langs;
$sql = "SELECT";
$sql .= " t.rowid,";
$sql .= " t.tms,";
$sql .= " t.datep,";
$sql .= " t.datev,";
$sql .= " t.amount,";
$sql .= " t.fk_typepayment,";
$sql .= " t.num_payment,";
$sql .= " t.label,";
$sql .= " t.note,";
2021-01-18 17:08:17 +01:00
$sql .= " t.paye,";
$sql .= " t.fk_user_creat,";
$sql .= " t.fk_user_modif,";
2021-01-18 17:08:17 +01:00
$sql .= " t.fk_account";
2021-02-23 21:09:01 +01:00
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
2021-01-18 17:08:17 +01:00
//$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
2021-03-14 11:48:39 +01:00
$sql .= " WHERE t.rowid = ".((int) $id);
2021-02-23 21:09:01 +01:00
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
if ($this->db->num_rows($resql)) {
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
2010-05-08 21:31:43 +02:00
$this->tms = $this->db->jdate($obj->tms);
$this->datep = $this->db->jdate($obj->datep);
$this->datev = $this->db->jdate($obj->datev);
2008-04-03 21:35:46 +02:00
$this->amount = $obj->amount;
$this->type_payment = $obj->fk_typepayment;
$this->num_payment = $obj->num_payment;
2008-04-03 21:35:46 +02:00
$this->label = $obj->label;
2021-01-18 17:08:17 +01:00
$this->paye = $obj->paye;
2010-05-08 21:31:43 +02:00
$this->note = $obj->note;
2008-04-03 21:35:46 +02:00
$this->fk_user_creat = $obj->fk_user_creat;
$this->fk_user_modif = $obj->fk_user_modif;
$this->fk_account = $obj->fk_account;
$this->fk_type = $obj->fk_type;
2010-05-08 21:31:43 +02:00
$this->rappro = $obj->rappro;
2021-02-23 21:09:01 +01:00
}
$this->db->free($resql);
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
return 1;
} else {
$this->error = "Error ".$this->db->lasterror();
return -1;
}
}
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
/**
* Delete object in database
*
2021-02-23 21:09:01 +01:00
* @param User $user User that delete
* @return int <0 if KO, >0 if OK
*/
2019-02-28 23:19:58 +01:00
public function delete($user)
2008-04-03 21:35:46 +02:00
{
global $conf, $langs;
2010-05-08 21:31:43 +02:00
$error = 0;
2014-07-11 15:01:51 +02:00
// Call trigger
$result = $this->call_trigger('TVA_DELETE', $user);
2021-02-23 21:09:01 +01:00
if ($result < 0) {
return -1;
}
2014-07-11 15:01:51 +02:00
// End call triggers
2008-04-03 21:35:46 +02:00
$sql = "DELETE FROM ".MAIN_DB_PREFIX."tva";
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid=".((int) $this->id);
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2008-04-03 21:35:46 +02:00
$resql = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if (!$resql) {
$this->error = "Error ".$this->db->lasterror();
2008-04-03 21:35:46 +02:00
return -1;
}
2010-05-08 21:31:43 +02:00
2008-04-03 21:35:46 +02:00
return 1;
}
2010-05-08 21:31:43 +02:00
2008-04-03 21:35:46 +02:00
/**
2021-02-23 21:09:01 +01:00
* 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
2008-04-03 21:35:46 +02:00
*/
2019-02-28 23:19:58 +01:00
public function initAsSpecimen()
2008-04-03 21:35:46 +02:00
{
$this->id = 0;
$this->tms = '';
$this->datep = '';
$this->datev = '';
$this->amount = '';
$this->label = '';
$this->note = '';
$this->fk_bank = '';
$this->fk_user_creat = '';
$this->fk_user_modif = '';
2008-04-03 21:35:46 +02:00
}
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
/**
* Balance of VAT
*
* @param int $year Year
* @return double Amount
*/
public function solde($year = 0)
{
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
$reglee = $this->tva_sum_reglee($year);
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
$payee = $this->tva_sum_payee($year);
$collectee = $this->tva_sum_collectee($year);
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
$solde = $reglee - ($collectee - $payee);
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
return $solde;
}
2008-04-03 21:35:46 +02:00
2019-03-01 18:28:51 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2021-02-23 21:09:01 +01:00
/**
* Total of the VAT from invoices emitted by the thirdparty.
*
* @param int $year Year
* @return double Amount
*/
public function tva_sum_collectee($year = 0)
{
2018-09-02 21:08:57 +02:00
// phpcs:enable
2008-04-03 21:35:46 +02:00
2021-03-01 04:07:31 +01:00
$sql = "SELECT sum(f.total_tva) as amount";
2021-02-23 21:09:01 +01:00
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
if ($year) {
$sql .= " AND f.datef >= '".$this->db->escape($year)."-01-01' AND f.datef <= '".$this->db->escape($year)."-12-31' ";
2021-02-23 21:09:01 +01:00
}
$result = $this->db->query($sql);
if ($result) {
if ($this->db->num_rows($result)) {
$obj = $this->db->fetch_object($result);
$ret = $obj->amount;
2021-02-23 21:09:01 +01:00
$this->db->free($result);
return $ret;
} else {
$this->db->free($result);
return 0;
2021-02-23 21:09:01 +01:00
}
} else {
print $this->db->lasterror();
return -1;
}
}
2008-04-03 21:35:46 +02:00
2019-02-28 23:19:58 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2021-02-23 21:09:01 +01:00
/**
* VAT payed
*
* @param int $year Year
* @return double Amount
*/
public function tva_sum_payee($year = 0)
{
2018-09-02 21:08:57 +02:00
// phpcs:enable
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
$sql = "SELECT sum(f.total_tva) as total_tva";
$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
if ($year) {
$sql .= " WHERE f.datef >= '".$this->db->escape($year)."-01-01' AND f.datef <= '".$this->db->escape($year)."-12-31' ";
}
$result = $this->db->query($sql);
if ($result) {
if ($this->db->num_rows($result)) {
$obj = $this->db->fetch_object($result);
$ret = $obj->total_tva;
$this->db->free($result);
return $ret;
} else {
$this->db->free($result);
return 0;
2021-02-23 21:09:01 +01:00
}
} else {
print $this->db->lasterror();
return -1;
}
}
2008-04-03 21:35:46 +02:00
2019-02-28 23:19:58 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2021-02-23 21:09:01 +01:00
/**
* Total of the VAT payed
*
* @param int $year Year
* @return double Amount
*/
public function tva_sum_reglee($year = 0)
{
2018-09-02 21:08:57 +02:00
// phpcs:enable
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
$sql = "SELECT sum(f.amount) as amount";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
2008-04-03 21:35:46 +02:00
2021-02-23 21:09:01 +01:00
if ($year) {
$sql .= " WHERE f.datev >= '".$this->db->escape($year)."-01-01' AND f.datev <= '".$this->db->escape($year)."-12-31' ";
2021-02-23 21:09:01 +01:00
}
$result = $this->db->query($sql);
if ($result) {
if ($this->db->num_rows($result)) {
$obj = $this->db->fetch_object($result);
$ret = $obj->amount;
$this->db->free($result);
return $ret;
} else {
$this->db->free($result);
return 0;
2021-02-23 21:09:01 +01:00
}
} else {
print $this->db->lasterror();
return -1;
}
}
/**
* Create in database
*
2012-03-18 19:23:01 +01:00
* @param User $user Object user that insert
* @return int <0 if KO, rowid in tva table if OK
2021-02-23 21:09:01 +01:00
*/
public function addPayment($user)
{
global $conf, $langs;
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
$this->db->begin();
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
// Clean parameters
$this->amount = price2num(trim($this->amount));
$this->label = trim($this->label);
$this->note = trim($this->note);
2020-09-10 18:25:38 +02:00
$this->num_payment = trim($this->num_payment);
2018-10-15 18:47:12 +02:00
$this->fk_bank = (int) $this->fk_bank;
$this->fk_user_creat = (int) $this->fk_user_creat;
$this->fk_user_modif = (int) $this->fk_user_modif;
2021-02-23 21:09:01 +01:00
if (empty($this->datec)) {
$this->datec = dol_now();
}
2021-02-23 21:09:01 +01:00
// Check parameters
if (!$this->label) {
$this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
2010-05-08 21:31:43 +02:00
return -3;
2008-04-03 21:35:46 +02:00
}
2021-02-23 21:09:01 +01:00
if ($this->amount == '') {
$this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
return -4;
}
2022-08-29 11:35:04 +02:00
if (isModEnabled('banque') && (empty($this->accountid) || $this->accountid <= 0)) {
2021-02-23 21:09:01 +01:00
$this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
return -5;
}
2022-08-29 11:35:04 +02:00
if (isModEnabled('banque') && (empty($this->type_payment) || $this->type_payment <= 0)) {
2021-02-23 21:09:01 +01:00
$this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
return -5;
}
// Insert into llx_tva
$sql = "INSERT INTO ".MAIN_DB_PREFIX."tva (";
$sql .= "datec";
$sql .= ", datep";
$sql .= ", datev";
$sql .= ", amount";
$sql .= ", fk_typepayment";
$sql .= ", num_payment";
2021-02-23 21:09:01 +01:00
if ($this->note) {
$sql .= ", note";
}
if ($this->label) {
$sql .= ", label";
}
$sql .= ", fk_user_creat";
$sql .= ", fk_bank";
$sql .= ", entity";
$sql .= ") ";
2021-02-23 21:09:01 +01:00
$sql .= " VALUES (";
$sql .= " '".$this->db->idate($this->datec)."'";
$sql .= ", '".$this->db->idate($this->datep)."'";
$sql .= ", '".$this->db->idate($this->datev)."'";
2021-04-24 21:15:09 +02:00
$sql .= ", ".((float) $this->amount);
2021-02-23 21:09:01 +01:00
$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)."'";
}
if ($this->label) {
$sql .= ", '".$this->db->escape($this->label)."'";
}
$sql .= ", '".$this->db->escape($user->id)."'";
$sql .= ", NULL";
2021-09-03 21:25:17 +02:00
$sql .= ", ".((int) $conf->entity);
2021-02-23 21:09:01 +01:00
$sql .= ")";
2008-04-03 21:35:46 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
2021-02-23 21:09:01 +01:00
$result = $this->db->query($sql);
if ($result) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); // TODO should be called 'payment_vat'
// Call trigger
//XXX: Should be done just befor commit no ?
$result = $this->call_trigger('TVA_ADDPAYMENT', $user);
if ($result < 0) {
$this->id = 0;
$ok = 0;
}
// End call triggers
if ($this->id > 0) {
$ok = 1;
2022-08-29 11:35:04 +02:00
if (isModEnabled('banque') && !empty($this->amount)) {
2021-02-23 21:09:01 +01:00
// Insert into llx_bank
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
$acc = new Account($this->db);
$result = $acc->fetch($this->accountid);
2021-02-23 21:09:01 +01:00
if ($result <= 0) {
dol_print_error($this->db);
}
2010-05-08 21:31:43 +02:00
2015-09-09 21:35:41 +02:00
if ($this->amount > 0) {
2020-09-10 18:25:38 +02:00
$bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs($this->amount), $this->num_payment, '', $user);
2015-09-09 21:35:41 +02:00
} else {
2020-09-10 18:25:38 +02:00
$bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs($this->amount), $this->num_payment, '', $user);
2015-09-09 21:35:41 +02:00
}
2017-07-07 19:23:19 +02:00
2021-02-23 21:09:01 +01:00
// Update fk_bank into llx_tva. So we know vat line used to generate bank transaction
if ($bank_line_id > 0) {
$this->update_fk_bank($bank_line_id);
} else {
$this->error = $acc->error;
$ok = 0;
}
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
// Update links
$result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', "(VATPayment)", "payment_vat");
if ($result < 0) {
$this->error = $acc->error;
$ok = 0;
}
}
if ($ok) {
2008-04-03 22:43:12 +02:00
$this->db->commit();
return $this->id;
2021-02-23 21:09:01 +01:00
} else {
2008-04-03 22:43:12 +02:00
$this->db->rollback();
return -3;
}
2021-02-23 21:09:01 +01:00
} else {
$this->db->rollback();
return -2;
}
} else {
$this->error = $this->db->error();
$this->db->rollback();
return -1;
}
}
2010-05-08 21:31:43 +02:00
2019-02-28 23:19:58 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2021-02-23 21:09:01 +01:00
/**
2014-05-01 08:19:55 +02:00
* Update link between payment tva and line generate into llx_bank
2021-02-23 21:09:01 +01:00
*
* @param int $id_bank Id bank account
2012-03-18 19:23:01 +01:00
* @return int <0 if KO, >0 if OK
2021-02-23 21:09:01 +01:00
*/
2019-02-28 23:19:58 +01:00
public function update_fk_bank($id_bank)
2008-04-03 21:35:46 +02:00
{
2018-09-02 21:08:57 +02:00
// phpcs:enable
2018-10-15 18:47:12 +02:00
$sql = 'UPDATE '.MAIN_DB_PREFIX.'tva SET fk_bank = '.(int) $id_bank;
$sql .= ' WHERE rowid = '.(int) $this->id;
2008-04-03 21:35:46 +02:00
$result = $this->db->query($sql);
2021-02-23 21:09:01 +01:00
if ($result) {
2008-04-03 21:35:46 +02:00
return 1;
2021-02-23 21:09:01 +01:00
} else {
dol_print_error($this->db);
2008-04-03 21:35:46 +02:00
return -1;
}
}
/**
2014-05-01 08:19:55 +02:00
* Send name clicable (with possibly the picto)
2012-03-18 19:23:01 +01:00
*
2014-05-01 08:19:55 +02:00
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
* @param string $option link option
2021-02-23 21:09:01 +01:00
* @param int $notooltip 1=Disable tooltip
* @param string $morecss More CSS
2021-02-08 19:52:29 +01:00
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
2014-05-01 08:19:55 +02:00
* @return string Chaine with URL
2012-03-18 19:23:01 +01:00
*/
2021-02-08 19:52:29 +01:00
public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
2008-04-03 21:35:46 +02:00
{
global $langs, $conf;
2021-02-23 21:09:01 +01:00
if (!empty($conf->dol_no_mouse_hover)) {
$notooltip = 1; // Force disable tooltips
}
2010-05-08 21:31:43 +02:00
$result = '';
$label = '<u>'.$langs->trans("ShowVatPayment").'</u>';
$label .= '<br>';
$label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
2021-02-23 21:09:01 +01:00
if (!empty($this->label)) {
2021-01-19 13:32:08 +01:00
$label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
2021-02-23 21:09:01 +01:00
}
2010-05-08 21:31:43 +02:00
2021-02-23 21:09:01 +01:00
$url = DOL_URL_ROOT.'/compta/tva/card.php?id='.$this->id;
if ($option != 'nolink') {
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
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';
}
}
$linkclose = '';
if (empty($notooltip)) {
if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
$label = $langs->trans("ShowMyObject");
$linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
}
$linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
$linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
} else {
$linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
}
$linkstart = '<a href="'.$url.'"';
$linkstart .= $linkclose.'>';
$linkend = '</a>';
2010-05-08 21:31:43 +02:00
$picto = 'payment';
2010-05-08 21:31:43 +02:00
2018-01-29 21:27:46 +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;
}
2018-01-29 21:27:46 +01:00
$result .= $linkend;
2008-04-03 21:35:46 +02:00
return $result;
2010-05-08 21:31:43 +02:00
}
2018-01-16 16:22:32 +01:00
/**
2021-02-23 21:09:01 +01:00
* Return amount of payments already done
*
* @return int Amount of payment already done, <0 if KO
*/
public function getSommePaiement()
{
$table = 'payment_vat';
$field = 'fk_tva';
$sql = 'SELECT sum(amount) as amount';
$sql .= ' FROM '.MAIN_DB_PREFIX.$table;
2021-08-28 00:55:51 +02:00
$sql .= " WHERE ".$field." = ".((int) $this->id);
2021-02-23 21:09:01 +01:00
dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$amount = 0;
$obj = $this->db->fetch_object($resql);
if ($obj) {
$amount = $obj->amount ? $obj->amount : 0;
}
$this->db->free($resql);
return $amount;
} else {
return -1;
}
}
2008-04-03 21:35:46 +02:00
/**
* Informations of vat payment object
*
* @param int $id Id of vat payment
* @return int <0 if KO, >0 if OK
*/
2019-02-28 23:19:58 +01:00
public function info($id)
2017-06-05 15:41:33 +02:00
{
2018-01-16 20:47:33 +01:00
$sql = "SELECT t.rowid, t.tms, t.fk_user_modif, t.datec, t.fk_user_creat";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql .= " WHERE t.rowid = ".(int) $id;
2017-06-05 15:41:33 +02: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-06-05 15:41:33 +02:00
$obj = $this->db->fetch_object($result);
2017-06-05 15:41:33 +02:00
$this->id = $obj->rowid;
2017-06-05 15:41:33 +02:00
if ($obj->fk_user_creat) {
$cuser = new User($this->db);
$cuser->fetch($obj->fk_user_creat);
$this->user_creation = $cuser;
}
2017-06-05 15:41:33 +02:00
if ($obj->fk_user_modif) {
$muser = new User($this->db);
$muser->fetch($obj->fk_user_modif);
$this->user_modification = $muser;
}
2017-06-05 15:41:33 +02:00
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->tms);
$this->import_key = $obj->import_key;
}
2017-06-05 15:41:33 +02:00
$this->db->free($result);
2021-02-23 21:09:01 +01:00
} else {
2017-06-05 15:41:33 +02:00
dol_print_error($this->db);
}
}
/**
2021-01-18 17:08:17 +01:00
* Retourne le libelle du statut d'une TVA (impaye, payee)
2017-06-05 15:41:33 +02:00
*
2021-01-18 17:08:17 +01:00
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto
* @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
* @return string Label
2017-06-05 15:41:33 +02:00
*/
2021-01-18 17:08:17 +01:00
public function getLibStatut($mode = 0, $alreadypaid = -1)
2017-06-05 15:41:33 +02:00
{
2021-01-18 17:08:17 +01:00
return $this->LibStatut($this->paye, $mode, $alreadypaid);
2017-06-05 15:41:33 +02:00
}
2017-07-07 19:23:19 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-06-05 15:41:33 +02:00
/**
2021-01-18 17:08:17 +01:00
* Renvoi le libelle d'un statut donne
2017-06-05 15:41:33 +02:00
*
2021-01-18 17:08:17 +01:00
* @param int $status Id status
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto
* @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
* @return string Label
2017-06-05 15:41:33 +02:00
*/
2021-01-18 17:08:17 +01:00
public function LibStatut($status, $mode = 0, $alreadypaid = -1)
{
// phpcs:enable
2021-01-18 17:08:17 +01:00
global $langs;
2021-01-18 17:08:17 +01:00
// Load translation files required by the page
$langs->loadLangs(array("customers", "bills"));
// We reinit status array to force to redefine them because label may change according to properties values.
$this->labelStatus = array();
$this->labelStatusShort = array();
2021-02-23 21:09:01 +01:00
if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
2021-01-18 17:08:17 +01:00
global $langs;
//$langs->load("mymodule");
2021-10-16 19:37:57 +02:00
$this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
$this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
2021-02-23 21:09:01 +01:00
if ($status == self::STATUS_UNPAID && $alreadypaid <> 0) {
2021-10-16 19:37:57 +02:00
$this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
2021-02-23 21:09:01 +01:00
}
2021-10-16 19:37:57 +02:00
$this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
$this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
2021-02-23 21:09:01 +01:00
if ($status == self::STATUS_UNPAID && $alreadypaid <> 0) {
2021-10-16 19:37:57 +02:00
$this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
2021-02-23 21:09:01 +01:00
}
2021-01-18 17:08:17 +01:00
}
$statusType = 'status1';
2021-02-23 21:09:01 +01:00
if ($status == 0 && $alreadypaid <> 0) {
$statusType = 'status3';
}
if ($status == 1) {
$statusType = 'status6';
}
2021-01-18 17:08:17 +01:00
return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
}
2008-04-03 21:35:46 +02:00
}