From e5191cad2459ae74df66bb90ceba5a1681eaf26e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 16 Sep 2020 02:50:18 +0200 Subject: [PATCH] Move code of incoterm into the Incoterm Trait. --- ChangeLog | 3 +- htdocs/comm/propal/class/propal.class.php | 3 + htdocs/commande/class/commande.class.php | 1 + htdocs/core/class/commonincoterm.class.php | 141 ++++++++++++++++++ htdocs/core/class/commoninvoice.class.php | 3 + htdocs/core/class/commonobject.class.php | 110 -------------- htdocs/core/class/commonorder.class.php | 3 +- htdocs/core/tpl/objectline_view.tpl.php | 2 +- htdocs/expedition/class/expedition.class.php | 3 + .../class/fournisseur.commande.class.php | 5 - .../fourn/class/fournisseur.facture.class.php | 8 - htdocs/reception/class/reception.class.php | 3 + htdocs/societe/class/societe.class.php | 17 +-- .../class/supplier_proposal.class.php | 3 + 14 files changed, 165 insertions(+), 140 deletions(-) create mode 100644 htdocs/core/class/commonincoterm.class.php diff --git a/ChangeLog b/ChangeLog index d1aa820e4a2..7bfd43947b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,7 +18,8 @@ Following changes may create regressions for some external modules, but were nec * The hidden option HOLIDAY_MORE_PUBLIC_HOLIDAYS has been removed. Use instead the dictionary table if you need to define custom days of holiday. * Property num_paiement has been renamed num_payment everywhere for better code consistency. - +* If you build a class that implement CommonObject to use the incoterm properties or method (->fk_incoterm, ->label_incoterm, ->location_incoterm), + you must now also include declaration of the Trait CommonIncoterm in your class. All incoterm functions were moved into this Trait. ***** ChangeLog for 12.0.2 compared to 12.0.1 ***** FIX: computation of the bottom margin of returns NaN because body is not loaded yet diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index d1936d9d17e..0712346f927 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -41,12 +41,15 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; /** * Class to manage proposals */ class Propal extends CommonObject { + use CommonIncoterm; + /** * @var string ID to identify managed object */ diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 6c6c26c135f..1bd919fd470 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -37,6 +37,7 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; + /** * Class to manage customers orders */ diff --git a/htdocs/core/class/commonincoterm.class.php b/htdocs/core/class/commonincoterm.class.php new file mode 100644 index 00000000000..b0bb5a3370c --- /dev/null +++ b/htdocs/core/class/commonincoterm.class.php @@ -0,0 +1,141 @@ + + * + * 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 + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/class/commonincoterm.class.php + * \ingroup core + * \brief File of the superclass of object classes that support incoterm (customer and supplier) + */ + + +/** + * Superclass for incoterm classes + */ +trait CommonIncoterm +{ + /** + * @var int ID incoterm. + * @see setIncoterms() + */ + public $fk_incoterms; + + /** + * @var string Label of incoterm. Used for tooltip. + * @see SetIncoterms() + */ + public $label_incoterms; + + /** + * @var string Location of incoterm. + * @see display_incoterms() + */ + public $location_incoterms; + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Return incoterms informations + * TODO Use a cache for label get + * + * @return string incoterms info + */ + public function display_incoterms() + { + // phpcs:enable + $out = ''; + + $this->label_incoterms = ''; + if (!empty($this->fk_incoterms)) + { + $sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; + $result = $this->db->query($sql); + if ($result) + { + $res = $this->db->fetch_object($result); + $out .= $res->code; + } + } + + $out .= (($out && $this->location_incoterms) ? ' - ' : '').$this->location_incoterms; + + return $out; + } + + /** + * Return incoterms informations for pdf display + * + * @return string incoterms info + */ + public function getIncotermsForPDF() + { + $sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + if ($num > 0) + { + $res = $this->db->fetch_object($resql); + return 'Incoterm : '.$res->code.' - '.$this->location_incoterms; + } else { + return ''; + } + } else { + $this->errors[] = $this->db->lasterror(); + return false; + } + } + + /** + * Define incoterms values of current object + * + * @param int $id_incoterm Id of incoterm to set or '' to remove + * @param string $location location of incoterm + * @return int <0 if KO, >0 if OK + */ + public function setIncoterms($id_incoterm, $location) + { + if ($this->id && $this->table_element) + { + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " SET fk_incoterms = ".($id_incoterm > 0 ? $id_incoterm : "null"); + $sql .= ", location_incoterms = ".($id_incoterm > 0 ? "'".$this->db->escape($location)."'" : "null"); + $sql .= " WHERE rowid = ".$this->id; + dol_syslog(get_class($this).'::setIncoterms', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $this->fk_incoterms = $id_incoterm; + $this->location_incoterms = $location; + + $sql = 'SELECT libelle FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; + $res = $this->db->query($sql); + if ($res) + { + $obj = $this->db->fetch_object($res); + $this->label_incoterms = $obj->libelle; + } + return 1; + } else { + $this->errors[] = $this->db->lasterror(); + return -1; + } + } else { + return -1; + } + } +} diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index 9e78d186172..3dabb8279e9 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -24,12 +24,15 @@ */ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; /** * Superclass for invoices classes */ abstract class CommonInvoice extends CommonObject { + use CommonIncoterm; + /** * Standard invoice */ diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 55b5628705d..a381014983b 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -393,24 +393,6 @@ abstract class CommonObject */ public $comments = array(); - /** - * @var int - * @see setIncoterms() - */ - public $fk_incoterms; - - /** - * @var string - * @see SetIncoterms() - */ - public $label_incoterms; - - /** - * @var string - * @see display_incoterms() - */ - public $location_incoterms; - /** * @var string The name */ @@ -3882,98 +3864,6 @@ abstract class CommonObject } - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Return incoterms informations - * TODO Use a cache for label get - * - * @return string incoterms info - */ - public function display_incoterms() - { - // phpcs:enable - $out = ''; - - $this->label_incoterms = ''; - if (!empty($this->fk_incoterms)) - { - $sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; - $result = $this->db->query($sql); - if ($result) - { - $res = $this->db->fetch_object($result); - $out .= $res->code; - } - } - - $out .= (($out && $this->location_incoterms) ? ' - ' : '').$this->location_incoterms; - - return $out; - } - - /** - * Return incoterms informations for pdf display - * - * @return string incoterms info - */ - public function getIncotermsForPDF() - { - $sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; - $resql = $this->db->query($sql); - if ($resql) - { - $num = $this->db->num_rows($resql); - if ($num > 0) - { - $res = $this->db->fetch_object($resql); - return 'Incoterm : '.$res->code.' - '.$this->location_incoterms; - } else { - return ''; - } - } else { - $this->errors[] = $this->db->lasterror(); - return false; - } - } - - /** - * Define incoterms values of current object - * - * @param int $id_incoterm Id of incoterm to set or '' to remove - * @param string $location location of incoterm - * @return int <0 if KO, >0 if OK - */ - public function setIncoterms($id_incoterm, $location) - { - if ($this->id && $this->table_element) - { - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " SET fk_incoterms = ".($id_incoterm > 0 ? $id_incoterm : "null"); - $sql .= ", location_incoterms = ".($id_incoterm > 0 ? "'".$this->db->escape($location)."'" : "null"); - $sql .= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this).'::setIncoterms', LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $this->fk_incoterms = $id_incoterm; - $this->location_incoterms = $location; - - $sql = 'SELECT libelle FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms; - $res = $this->db->query($sql); - if ($res) - { - $obj = $this->db->fetch_object($res); - $this->label_incoterms = $obj->libelle; - } - return 1; - } else { - $this->errors[] = $this->db->lasterror(); - return -1; - } - } else return -1; - } - - // -------------------- // TODO: All functions here must be redesigned and moved as they are not business functions but output functions // -------------------- diff --git a/htdocs/core/class/commonorder.class.php b/htdocs/core/class/commonorder.class.php index e45d08a9396..83d20e99b95 100644 --- a/htdocs/core/class/commonorder.class.php +++ b/htdocs/core/class/commonorder.class.php @@ -23,13 +23,14 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; /** * Superclass for orders classes */ abstract class CommonOrder extends CommonObject { - + use CommonIncoterm; } /** diff --git a/htdocs/core/tpl/objectline_view.tpl.php b/htdocs/core/tpl/objectline_view.tpl.php index 1b131a4b8df..99e727c48d5 100644 --- a/htdocs/core/tpl/objectline_view.tpl.php +++ b/htdocs/core/tpl/objectline_view.tpl.php @@ -64,7 +64,7 @@ $domData .= ' data-qty="'.$line->qty.'"'; $domData .= ' data-product_type="'.$line->product_type.'"'; $sign = 1; -if (!empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE_SCREEN) && $object->type == $object::TYPE_CREDIT_NOTE) { +if (!empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE_SCREEN) && in_array($object->element, array('facture', 'invoice_supplier')) && $object->type == $object::TYPE_CREDIT_NOTE) { $sign = -1; } diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index f601f483d89..8db2eac723e 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -36,6 +36,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php"; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; if (!empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; if (!empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; if (!empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; @@ -46,6 +47,8 @@ if (!empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/exped */ class Expedition extends CommonObject { + use CommonIncoterm; + /** * @var string ID to identify managed object */ diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index a2130b6135e..13c0e8fb1df 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -156,11 +156,6 @@ class CommandeFournisseur extends CommonOrder public $user_approve_id; public $user_approve_id2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set - //Incoterms - public $fk_incoterms; - public $location_incoterms; - public $label_incoterms; //Used into tooltip - public $extraparams = array(); /** diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index f94cadbca9c..5a57319b5a5 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -190,14 +190,6 @@ class FactureFournisseur extends CommonInvoice */ public $fournisseur; - /** - * @var int ID Incorterms - */ - public $fk_incoterms; - - public $location_incoterms; - public $label_incoterms; //Used into tooltip - public $extraparams = array(); // Multicurrency diff --git a/htdocs/reception/class/reception.class.php b/htdocs/reception/class/reception.class.php index 7b2f4927a22..201afb161dd 100644 --- a/htdocs/reception/class/reception.class.php +++ b/htdocs/reception/class/reception.class.php @@ -34,6 +34,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php"; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; if (!empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; if (!empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; @@ -43,6 +44,8 @@ if (!empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/ */ class Reception extends CommonObject { + use CommonIncoterm; + public $element = "reception"; public $fk_element = "fk_reception"; public $table_element = "reception"; diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index a5a489f014b..61c958ed89b 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -38,6 +38,7 @@ * \brief File for third party class */ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; /** @@ -45,6 +46,8 @@ require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; */ class Societe extends CommonObject { + use CommonIncoterm; + /** * @var string ID to identify managed object */ @@ -692,20 +695,6 @@ class Societe extends CommonObject */ public $logo_squarred_mini; - /** - * @var int ID Incoterms - */ - public $fk_incoterms; - - /** - * @var string Incoterms Location - */ - public $location_incoterms; - - /** - * @var string Incoterm label - */ - public $label_incoterms; //Used into tooltip // Multicurrency /** diff --git a/htdocs/supplier_proposal/class/supplier_proposal.class.php b/htdocs/supplier_proposal/class/supplier_proposal.class.php index 79b50071a18..eeca2c2adf3 100644 --- a/htdocs/supplier_proposal/class/supplier_proposal.class.php +++ b/htdocs/supplier_proposal/class/supplier_proposal.class.php @@ -41,12 +41,15 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php'; /** * Class to manage price ask supplier */ class SupplierProposal extends CommonObject { + use CommonIncoterm; + /** * @var string ID to identify managed object */