diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 20be732da17..456a18d8e2c 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -3126,8 +3126,8 @@ class Facture extends CommonInvoice $this->line->tva_tx = $txtva; $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0); $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0); - $this->line->localtax1_type = $localtaxes_type[0]; - $this->line->localtax2_type = $localtaxes_type[2]; + $this->line->localtax1_type = isset($localtaxes_type[0]) ? $localtaxes_type[0] : ''; + $this->line->localtax2_type = isset($localtaxes_type[2]) ? $localtaxes_type[2] : ''; $this->line->total_ht = (($this->type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht); // For credit note and if qty is negative, total is negative $this->line->total_ttc = (($this->type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc); // For credit note and if qty is negative, total is negative diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 2886b140539..d38cdc69104 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5150,17 +5150,17 @@ function getTaxesFromId($vatrate, $buyer = null, $seller = null, $firstparamisid /** * Get type and rate of localtaxes for a particular vat rate/country of a thirdparty. * This does not take into account the seller setup if subject to vat or not, only country. - * TODO - * This function is ALSO called to retrieve type for building PDF. Such call of function must be removed. - * Instead this function must be called when adding a line to get the array of localtax and type, and then - * provide it to the function calcul_price_total. + * + * TODO This function is ALSO called to retrieve type for building PDF. Such call of function must be removed. + * Instead this function must be called when adding a line to get the array of possible values for localtax and type, and then + * provide the selected value to the function calcul_price_total. * * @param int|string $vatrate VAT ID or Rate+Code. Value can be value or the string with code into parenthesis or rowid if $firstparamisid is 1. Example: '8.5' or '8.5 (8.5NPR)' or 123. * @param int $local Number of localtax (1 or 2, or 0 to return 1 & 2) * @param Societe $buyer Company object * @param Societe $seller Company object * @param int $firstparamisid 1 if first param is ID into table instead of Rate+code (use this if you can) - * @return array array(localtax_type1(1-6/0 if not found), rate localtax1, localtax_type2, rate localtax2, accountancycodecust, accountancycodesupp) + * @return array array(localtax_type1(1-6 or 0 if not found), rate localtax1, localtax_type2, rate localtax2, accountancycodecust, accountancycodesupp) * @see getTaxesFromId() */ function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid = 0) @@ -5172,13 +5172,13 @@ function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisi // Search local taxes $sql = "SELECT t.taux as rate, t.code, t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type, t.accountancy_code_sell, t.accountancy_code_buy"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t"; - if ($firstparamisid) $sql .= " WHERE t.rowid = ".(int) $vatrate; - else { + if ($firstparamisid) { + $sql .= " WHERE t.rowid = ".(int) $vatrate; + } else { $vatratecleaned = $vatrate; $vatratecode = ''; $reg = array(); - if (preg_match('/^(.*)\s*\((.*)\)$/', $vatrate, $reg)) // If vat is "x.x (yy)" - { + if (preg_match('/^(.*)\s*\((.*)\)$/', $vatrate, $reg)) { // If vat is "x.x (yy)" $vatratecleaned = $reg[1]; $vatratecode = $reg[2]; } @@ -5191,20 +5191,19 @@ function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisi } $resql = $db->query($sql); - if ($resql) - { + if ($resql) { $obj = $db->fetch_object($resql); - $vateratestring = $obj->rate.($obj->code ? ' ('.$obj->code.')' : ''); + if ($obj) { + $vateratestring = $obj->rate.($obj->code ? ' ('.$obj->code.')' : ''); - if ($local == 1) - { - return array($obj->localtax1_type, get_localtax($vateratestring, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); - } elseif ($local == 2) - { - return array($obj->localtax2_type, get_localtax($vateratestring, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); - } else { - return array($obj->localtax1_type, get_localtax($vateratestring, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vateratestring, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); + if ($local == 1) { + return array($obj->localtax1_type, get_localtax($vateratestring, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); + } elseif ($local == 2) { + return array($obj->localtax2_type, get_localtax($vateratestring, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); + } else { + return array($obj->localtax1_type, get_localtax($vateratestring, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vateratestring, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy); + } } } diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 89ca1bbc1bd..ce14295b4b2 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -129,14 +129,14 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt $localtax1_type = 0; $localtax2_type = 0; - if (is_array($localtaxes_array)) - { + if (is_array($localtaxes_array)) { $localtax1_type = $localtaxes_array[0]; $localtax1_rate = $localtaxes_array[1]; $localtax2_type = $localtaxes_array[2]; $localtax2_rate = $localtaxes_array[3]; - } else // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate using the full vat rate (including text code) - { + } else { + // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate using the full vat rate (including text code) + // also, with this method, we may get several possible values (for example with localtax2 in spain), so we take the first one. dol_syslog("Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING); $sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type"; @@ -149,11 +149,11 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt $obj = $db->fetch_object($resql); if ($obj) { - $localtax1_rate = $obj->localtax1; - $localtax2_rate = $obj->localtax2; + $localtax1_rate = (float) $obj->localtax1; // Use float to force to get first numeric value when value is x:y:z + $localtax2_rate = (float) $obj->localtax2; // Use float to force to get first numeric value when value is -19:-15:-9 $localtax1_type = $obj->localtax1_type; $localtax2_type = $obj->localtax2_type; - //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);exit; + //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type); } } else dol_print_error($db); } @@ -417,7 +417,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt // initialize result array //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i]; - dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.$conf->global->MAIN_ROUNDING_RULE_TOT.' pu='.$pu.' qty='.$qty.' price_base_type='.$price_base_type.' total_ht='.$result[0].'-total_vat='.$result[1].'-total_ttc='.$result[2]); + dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.(empty($conf->global->MAIN_ROUNDING_RULE_TOT)?'':$conf->global->MAIN_ROUNDING_RULE_TOT).' pu='.$pu.' qty='.$qty.' price_base_type='.$price_base_type.' total_ht='.$result[0].'-total_vat='.$result[1].'-total_ttc='.$result[2]); return $result; }