[RESOLU] Retours de stocks et calcul de Marges

Bonsoir à tous,
Je suis à la recherche de la méthode à mettre en oeuvre pour gérer correctement un retour en stock après livraison :
1°) un client commande 3 produits distincts par qté de 10
- la commande est validée
- la commande est livrée
- et est facturée
2°) Le client revient pour échanger un des produit contre un autre de remplacement
- je créé une nouvelle commande
- j’ajoute la ligne du produit retourné avec une Qté de -10
- j’ajoute la ligne du produit de remplacement pour une qté de 10
===> Pb le calcul de marge totale de la commande est ERRONÉ car
- Doli calcule (PrixP1 x Qté) + (PrixP2 x Qté)
- au lieu de (PrixP1 x -Qté) + (PrixP2 x Qté)

Avez-vous trouvé une solution fonctionnelle ?
Merci par avance de votre collaboration

Bertrand

Dolibar 8.0.2 - Linux - PHP 7 - MySQL

Salut bertrand,

Dolibarr n’y est pour rien … c’est toi qui veut faire un truc pas logique …

dans ta procédure :

  1. vente ok
  2. tu fais un avoir (du retour) -> valable pour remboursement ou paiement sur une prochaine facture.
  3. vente ok (avec en moyen de paiement l’avoir ou un règlement)
    etc…
1 « J'aime »

Bonsoir,
Merci pour votre réactivité.
C’est bien comme ça que je l’ai expliqué au client…mais il fait comme ça depuis 4 ans !
Pour moi, l’avoir me parait totalement logique… et légal.
Merci pour votre réponse.

ah les quantité négative…

j’ai le même problème sur une base dolibarr actuellement, le calcul de la marge est complètement a la ramasse

et donc je suis en train de voir pour faire une moulinette qui identifie les problèmes (ligne négative), qui permet de les corriger (un avoir)
et un re calcul de la mage commerciale

Bonjour à tous,

La réponse est enfin trouvée : le calcul des marges sur des qtés négatives est COMMENTÉ dans le code :

  • fichier dolibarr/htdocs/core/class/html.formmargin.class.php
  • enlever les commentaires sur les lignes :
  • lignes 106 à 110
  • lignes 119 à 121
  • lignes 137 à 142
  • ligne 144
  • lignes 152 à 154
  • lignes 170 à 172

Comment faut-il faire pour demander aux développeurs Dolibarr de décommenter de base ces lignes.
Sinon on perdra la correction à chaque nouvelle version. Dans la version 9 et la version develop elles sont toujours commentées.

Amicalement,
Bertrand

Voici le correctif complet qui fonctionne pour le calcul des marges dans :

  • devis / commandes / factures
  • commandes avec qtés négatives / factures avec qtés négatives
  • avoirs
  • onglet marge dans fiches produits
  • Module Facturation / marges / par produit, par utilisateur, par client

le patch :

le code :

--- htdocs/core/class/html.formmargin.class.php.orig	2019-03-06 16:34:48.703461647 +0100
+++ htdocs/core/class/html.formmargin.class.php	2019-03-06 16:40:52.320136602 +0100
@@ -91,9 +91,16 @@
 				$line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
 			}
 
+			//Conservation du prix de vente HT saisi pour calcul des marges sur retours et avoirs
+			$credit_note = 0; //0 pour facture / commande / devis, 1 pour avoir
+			$pv_ht = $line->subprice;
+
 			$pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
+
+			//Conservation du Prix d' achat négatif pour calcul des marges sur retours produits
+			$pa = $line->qty * $line->pa_ht;
+
 			$pa_ht = ($pv < 0 ? - $line->pa_ht : $line->pa_ht);      // We choosed to have line->pa_ht always positive in database, so we guess the correct sign
-			$pa = $line->qty * $pa_ht;
 
 			// calcul des marges
 			if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) {    // remise
@@ -102,24 +109,34 @@
 					$marginInfos['pv_products'] += $pv;
 					$marginInfos['pa_total'] +=  $pa;
 					$marginInfos['pv_total'] +=  $pv;
-					// if credit note, margin = -1 * (abs(selling_price) - buying_price)
-					//if ($pv < 0)
-					//{
-					//	$marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
-					//}
-					//else
+					// if credit note, pv_ht <0
+					// so margin = -1 * (abs(selling_price) - buying_price)
+					if ($pv_ht < 0)
+					{
+						// on a un pv_ht négatif -> c'est une ligne d'avoir
+						$credit_note = 1;
+						$marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
+					}
+					else {
 						$marginInfos['margin_on_products'] += $pv - $pa;
+					}
 				}
 				elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
 					$marginInfos['pa_services'] += $pa;
 					$marginInfos['pv_services'] += $pv;
 					$marginInfos['pa_total'] +=  $pa;
 					$marginInfos['pv_total'] +=  $pv;
-					// if credit note, margin = -1 * (abs(selling_price) - buying_price)
-					//if ($pv < 0)
-					//	$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
-					//else
+					// if credit note, pv_ht <0
+					// so margin = -1 * (abs(selling_price) - buying_price)
+					if ($pv_ht < 0)
+					{
+						// on a un pv_ht négatif -> c'est une ligne d'avoir
+						$credit_note = 1;
+						$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
+					}
+					else {
 						$marginInfos['margin_on_services'] += $pv - $pa;
+					}
 				}
 				elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
 					$marginInfos['pa_total'] += $pa;
@@ -133,29 +150,39 @@
 					$marginInfos['pv_products'] += $pv;
 					$marginInfos['pa_total'] +=  $pa;
 					$marginInfos['pv_total'] +=  $pv;
-					// if credit note, margin = -1 * (abs(selling_price) - buying_price)
-					//if ($pv < 0)
-					//{
-					//    $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
-					//}
-					//else
-					//{
+					// if credit note, pv_ht <0
+					// so margin = -1 * (abs(selling_price) - buying_price)
+					if ($pv_ht < 0)
+					{
+						// on a un pv_ht négatif -> c'est une ligne d'avoir
+						$credit_note = 1;
+						$marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
+					}
+					else
+					{
 					    $marginInfos['margin_on_products'] += $pv - $pa;
-					//}
+					}
 				}
 				elseif ($type == 1) {  // service
 					$marginInfos['pa_services'] += $pa;
 					$marginInfos['pv_services'] += $pv;
 					$marginInfos['pa_total'] +=  $pa;
 					$marginInfos['pv_total'] +=  $pv;
-					// if credit note, margin = -1 * (abs(selling_price) - buying_price)
-					//if ($pv < 0)
-					//	$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
-					//else
+					// if credit note, pv_ht <0
+					// so margin = -1 * (abs(selling_price) - buying_price)
+					if ($pv_ht < 0)
+					{
+						// on a un pv_ht négatif -> c'est une ligne d'avoir
+						$credit_note = 1;
+						$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
+					}
+					else
 						$marginInfos['margin_on_services'] += $pv - $pa;
 				}
 			}
 		}
+
+
 		if ($marginInfos['pa_products'] > 0)
 			$marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
 		if ($marginInfos['pv_products'] > 0)
@@ -166,10 +193,10 @@
 		if ($marginInfos['pv_services'] > 0)
 			$marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
 
-		// if credit note, margin = -1 * (abs(selling_price) - buying_price)
-		//if ($marginInfos['pv_total'] < 0)
-		//	$marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
-		//else
+		// if credit note = 1, c'est un avoir : margin = -1 * (abs(selling_price) - buying_price)
+		if ($credit_note == 1)
+			$marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
+		else
 			$marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
 		if ($marginInfos['pa_total'] > 0)
 			$marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
@@ -278,4 +305,3 @@
 
 }
 
-

Si vous pouvez retester le patch sur une v8.0.4 ou supérieure.
Amicalement,
Bertrand

1 « J'aime »

Bonjour bertrand,

Merci pour ta correction.
Tu peux proposer sur Git Hub en ouvrant une issue ?

@+