From 9ff4b31843835b8e1cd18ecc7c572106ff86bff8 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 14 Nov 2018 15:24:22 +0100 Subject: [PATCH] FIX compatibility with invoice sharing and invoice number sharing --- .../core/modules/facture/mod_facture_mars.php | 18 ++-- .../modules/facture/mod_facture_mercure.php | 90 +++++++++++++------ .../modules/facture/mod_facture_terre.php | 14 +-- 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/htdocs/core/modules/facture/mod_facture_mars.php b/htdocs/core/modules/facture/mod_facture_mars.php index 2bdad538ec9..19161ae1325 100644 --- a/htdocs/core/modules/facture/mod_facture_mars.php +++ b/htdocs/core/modules/facture/mod_facture_mars.php @@ -1,6 +1,6 @@ - * Copyright (C) 2005-2015 Regis Houssin + * Copyright (C) 2005-2018 Regis Houssin * Copyright (C) 2013 Juanjo Menent * * This program is free software; you can redistribute it and/or modify @@ -145,19 +145,19 @@ class mod_facture_mars extends ModeleNumRefFactures * Return next value not used or last value used * * @param Societe $objsoc Object third party - * @param Facture $facture Object invoice + * @param Facture $invoice Object invoice * @param string $mode 'next' for next value or 'last' for last value * @return string Value */ - function getNextValue($objsoc,$facture,$mode='next') + function getNextValue($objsoc, $invoice, $mode='next') { global $db; $prefix=$this->prefixinvoice; - if ($facture->type == 1) $prefix=$this->prefixreplacement; - else if ($facture->type == 2) $prefix=$this->prefixcreditnote; - else if ($facture->type == 3) $prefix=$this->prefixdeposit; + if ($invoice->type == 1) $prefix=$this->prefixreplacement; + else if ($invoice->type == 2) $prefix=$this->prefixcreditnote; + else if ($invoice->type == 3) $prefix=$this->prefixdeposit; else $prefix=$this->prefixinvoice; // D'abord on recupere la valeur max @@ -165,7 +165,7 @@ class mod_facture_mars extends ModeleNumRefFactures $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL $sql.= " FROM ".MAIN_DB_PREFIX."facture"; $sql.= " WHERE facnumber LIKE '".$prefix."____-%'"; - $sql.= " AND entity IN (".getEntity('invoicenumber').")"; + $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")"; $resql=$db->query($sql); dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG); @@ -189,7 +189,7 @@ class mod_facture_mars extends ModeleNumRefFactures $sql = "SELECT facnumber as ref"; $sql.= " FROM ".MAIN_DB_PREFIX."facture"; $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'"; - $sql.= " AND entity IN (".getEntity('invoicenumber').")"; + $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")"; dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG); $resql=$db->query($sql); @@ -204,7 +204,7 @@ class mod_facture_mars extends ModeleNumRefFactures } else if ($mode == 'next') { - $date=$facture->date; // This is invoice date (not creation date) + $date=$invoice->date; // This is invoice date (not creation date) $yymm = strftime("%y%m",$date); if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is diff --git a/htdocs/core/modules/facture/mod_facture_mercure.php b/htdocs/core/modules/facture/mod_facture_mercure.php index 95e60c78398..e2e64988b4a 100644 --- a/htdocs/core/modules/facture/mod_facture_mercure.php +++ b/htdocs/core/modules/facture/mod_facture_mercure.php @@ -131,43 +131,77 @@ class mod_facture_mercure extends ModeleNumRefFactures * Return next value * * @param Societe $objsoc Object third party - * @param Facture $facture Object invoice + * @param Facture $invoice Object invoice * @param string $mode 'next' for next value or 'last' for last value * @return string Value if OK, 0 if KO */ - function getNextValue($objsoc,$facture,$mode='next') + function getNextValue($objsoc, $invoice, $mode='next') { - global $db,$conf; + global $db,$conf; - require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php'; + require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php'; - // Get Mask value - $mask = ''; - if (is_object($facture) && $facture->type == 1) - { - $mask=$conf->global->FACTURE_MERCURE_MASK_REPLACEMENT; - if (! $mask) - { - $mask=$conf->global->FACTURE_MERCURE_MASK_INVOICE; - } - } - else if (is_object($facture) && $facture->type == 2) $mask=$conf->global->FACTURE_MERCURE_MASK_CREDIT; - else if (is_object($facture) && $facture->type == 3) $mask=$conf->global->FACTURE_MERCURE_MASK_DEPOSIT; - else $mask=$conf->global->FACTURE_MERCURE_MASK_INVOICE; - if (! $mask) - { - $this->error='NotConfigured'; - return 0; - } + // Get Mask value + $mask = ''; + if (is_object($invoice) && $invoice->type == 1) + { + $constant = 'FACTURE_MERCURE_MASK_REPLACEMENT_'.$invoice->entity; + if (! empty($conf->global->$constant)) { + $mask = $conf->global->$constant; // for multicompany proposal sharing + } else { + $mask=$conf->global->FACTURE_MERCURE_MASK_REPLACEMENT; + } + if (! $mask) + { + $constant = 'FACTURE_MERCURE_MASK_INVOICE_'.$invoice->entity; + if (! empty($conf->global->$constant)) { + $mask = $conf->global->$constant; // for multicompany proposal sharing + } else { + $mask=$conf->global->FACTURE_MERCURE_MASK_INVOICE; + } + } + } + else if (is_object($invoice) && $invoice->type == 2){ + $constant = 'FACTURE_MERCURE_MASK_CREDIT_'.$invoice->entity; + if (! empty($conf->global->$constant)) { + $mask = $conf->global->$constant; // for multicompany proposal sharing + } else { + $mask=$conf->global->FACTURE_MERCURE_MASK_CREDIT; + } + } + else if (is_object($invoice) && $invoice->type == 3){ + $constant = 'FACTURE_MERCURE_MASK_DEPOSIT_'.$invoice->entity; + if (! empty($conf->global->$constant)) { + $mask = $conf->global->$constant; // for multicompany proposal sharing + } else { + $mask=$conf->global->FACTURE_MERCURE_MASK_DEPOSIT; + } + } + else { + $constant = 'FACTURE_MERCURE_MASK_INVOICE_'.$invoice->entity; + if (! empty($conf->global->$constant)) { + $mask = $conf->global->$constant; // for multicompany proposal sharing + } else { + $mask=$conf->global->FACTURE_MERCURE_MASK_INVOICE; + } + } + if (! $mask) + { + $this->error='NotConfigured'; + return 0; + } - $where=''; - //if ($facture->type == 2) $where.= " AND type = 2"; - //else $where.=" AND type != 2"; + $where=''; + //if ($facture->type == 2) $where.= " AND type = 2"; + //else $where.=" AND type != 2"; - $numFinal=get_next_value($db,$mask,'facture','facnumber',$where,$objsoc,$facture->date,$mode); - if (! preg_match('/([0-9])+/',$numFinal)) $this->error = $numFinal; + // Get entities + $entity = getEntity('invoicenumber', 1, $invoice); - return $numFinal; + $numFinal=get_next_value($db,$mask,'facture','facnumber',$where,$objsoc,$facture->date,$mode,false,null,$entity); + if (! preg_match('/([0-9])+/',$numFinal)) $this->error = $numFinal; + + return $numFinal; } diff --git a/htdocs/core/modules/facture/mod_facture_terre.php b/htdocs/core/modules/facture/mod_facture_terre.php index 4442451984f..1c19d564486 100644 --- a/htdocs/core/modules/facture/mod_facture_terre.php +++ b/htdocs/core/modules/facture/mod_facture_terre.php @@ -163,16 +163,16 @@ class mod_facture_terre extends ModeleNumRefFactures * Return next value not used or last value used * * @param Societe $objsoc Object third party - * @param Facture $facture Object invoice + * @param Facture $invoice Object invoice * @param string $mode 'next' for next value or 'last' for last value * @return string Value */ - function getNextValue($objsoc,$facture,$mode='next') + function getNextValue($objsoc, $invoice, $mode='next') { global $db; - if ($facture->type == 2) $prefix=$this->prefixcreditnote; - else if ($facture->type == 3) $prefix=$this->prefixdeposit; + if ($invoice->type == 2) $prefix=$this->prefixcreditnote; + else if ($invoice->type == 3) $prefix=$this->prefixdeposit; else $prefix=$this->prefixinvoice; // D'abord on recupere la valeur max @@ -180,7 +180,7 @@ class mod_facture_terre extends ModeleNumRefFactures $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL $sql.= " FROM ".MAIN_DB_PREFIX."facture"; $sql.= " WHERE facnumber LIKE '".$prefix."____-%'"; - $sql.= " AND entity IN (".getEntity('invoicenumber').")"; + $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")"; $resql=$db->query($sql); dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG); @@ -204,7 +204,7 @@ class mod_facture_terre extends ModeleNumRefFactures $sql = "SELECT facnumber as ref"; $sql.= " FROM ".MAIN_DB_PREFIX."facture"; $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'"; - $sql.= " AND entity IN (".getEntity('invoicenumber').")"; + $sql.= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")"; dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG); $resql=$db->query($sql); @@ -219,7 +219,7 @@ class mod_facture_terre extends ModeleNumRefFactures } else if ($mode == 'next') { - $date=$facture->date; // This is invoice date (not creation date) + $date=$invoice->date; // This is invoice date (not creation date) $yymm = strftime("%y%m",$date); if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is