From 052a11a5c01e0dcee97a756c142f8b391c018028 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= <rdoursenaud@gpcsolutions.fr>
Date: Mon, 25 Jul 2016 17:18:13 +0200
Subject: [PATCH] Fixed interventions from order.

Handling of predefined services was buggy and resulted in an SQL error
because of uninitialized duration.

Generalized services duration handling and exposed it in the module's
admin page.
---
 htdocs/admin/fichinter.php            | 31 ++++++++++-
 htdocs/fichinter/card.php             | 78 ++++++++++++++++-----------
 htdocs/langs/en_US/interventions.lang |  3 +-
 3 files changed, 78 insertions(+), 34 deletions(-)

diff --git a/htdocs/admin/fichinter.php b/htdocs/admin/fichinter.php
index fd34d01f620..a67407b818c 100644
--- a/htdocs/admin/fichinter.php
+++ b/htdocs/admin/fichinter.php
@@ -232,6 +232,20 @@ elseif ($action == 'set_FICHINTER_PRINT_PRODUCTS')
     {
         setEventMessages($langs->trans("Error"), null, 'errors');
     }
+} elseif ($action == 'set_FICHINTER_USE_SERVICE_DURATION') {
+	$val = GETPOST('FICHINTER_USE_SERVICE_DURATION', 'alpha');
+	$res = dolibarr_set_const($db, "FICHINTER_USE_SERVICE_DURATION", ($val == 'on' ? 1 : 0), 'bool', 0, '',
+		$conf->entity);
+
+	if (!$res > 0) {
+		$error++;
+	}
+
+	if (!$error) {
+		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
+	} else {
+		setEventMessages($langs->trans("Error"), null, 'errors');
+	}
 }
 
 
@@ -568,7 +582,22 @@ print '/>';
 print '</td><td align="right">';
 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
 print "</td></tr>\n";
-
+// Use services duration
+$var = !$var;
+print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">';
+print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
+print '<input type="hidden" name="action" value="set_FICHINTER_USE_SERVICE_DURATION">';
+print '<tr ' . $bc[$var] . '>';
+print '<td>';
+print $langs->trans("UseServicesDurationOnFichinter");
+print '</td>';
+print '<td align="center">';
+print '<input type="checkbox" name="FICHINTER_USE_SERVICE_DURATION"' . ($conf->global->FICHINTER_USE_SERVICE_DURATION?' checked':'') . '>';
+print '</td>';
+print '<td align="right">';
+print '<input type="submit" class="button" value="' . $langs->trans("Modify") . '">';
+print '</td>';
+print '</tr>';
 print '</form>';
 
 
diff --git a/htdocs/fichinter/card.php b/htdocs/fichinter/card.php
index 2a4ec4337f2..934d19fb320 100644
--- a/htdocs/fichinter/card.php
+++ b/htdocs/fichinter/card.php
@@ -280,68 +280,82 @@ if (empty($reshook))
 
 						for ($i=0;$i<$num;$i++)
 						{
-							$product_type=($lines[$i]->product_type?$lines[$i]->product_type:0);
+							$product_type=($lines[$i]->product_type?$lines[$i]->product_type:Product::TYPE_PRODUCT);
 
-							if ($product_type == 1 || !empty($conf->global->FICHINTER_PRINT_PRODUCTS)) { //only services except if config includes products
-								// service prédéfini
+							if ($product_type == Product::TYPE_SERVICE || !empty($conf->global->FICHINTER_PRINT_PRODUCTS)) { //only services except if config includes products
+								$duration = 3600; // Default to one hour
+
+								// Predefined products & services
 								if ($lines[$i]->fk_product > 0)
 								{
+									$prod = new Product($db);
+									$prod->id = $lines[$i]->fk_product;
+
 									// Define output language
-									if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE))
-									{
-										$prod = new Product($db);
-										$prod->id=$lines[$i]->fk_product;
+									if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
 										$prod->getMultiLangs();
 										// We show if duration is present on service (so we get it)
 										$prod->fetch($lines[$i]->fk_product);
-										if ($prod->duration_value && $prod->duration_unit == 'h' && $conf->global->FICHINTER_USE_SERVICE_DURATION)
-										{
-											$durationproduct=$prod->duration_value * 3600 * $lines[$i]->qty;
-										}
-										else
-											$durationproduct=3600;
 										$outputlangs = $langs;
 										$newlang='';
 										if (empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
 										if (empty($newlang)) $newlang=$srcobject->client->default_lang;
-										if (! empty($newlang))
-										{
+										if (! empty($newlang)) {
 											$outputlangs = new Translate("",$conf);
 											$outputlangs->setDefaultLang($newlang);
 										}
-
 										$label = (! empty($prod->multilangs[$outputlangs->defaultlang]["libelle"])) ? $prod->multilangs[$outputlangs->defaultlang]["libelle"] : $lines[$i]->product_label;
+									} else {
+										$prod->fetch($lines[$i]->fk_product);
+										$label .= $lines[$i]->product_label;
 									}
-									else
-									{
-										$label = $lines[$i]->product_label;
+
+									if ($prod->duration_value && $conf->global->FICHINTER_USE_SERVICE_DURATION) {
+										switch ($prod->duration_unit) {
+											default:
+											case 'h':
+												$mult = 3600;
+												break;
+											case 'd':
+												$mult = 3600 * 24;
+												break;
+											case 'w':
+												$mult = 3600 * 24 * 7;
+												break;
+											case 'm':
+												$mult = (int) 3600 * 24 * (365 / 12); // Average month duration
+												break;
+											case 'y':
+												$mult = 3600 * 24 * 365;
+												break;
+										}
+										$duration = $prod->duration_value * $mult * $lines[$i]->qty;
 									}
 
-									$desc = $label;
-									$desc .= ' ('.$langs->trans('Quantity').': '.$lines[$i]->qty.')';
-								}
-								else {
-									$desc = dol_htmlentitiesbr($lines[$i]->desc);
-									$desc .= ' ('.$langs->trans('Quantity').': '.$lines[$i]->qty.')';
+									$desc = $lines[$i]->product_ref;
+									$desc .= ' - ';
+									$desc .= $label;
+									$desc .= '<br>';
 								}
+								// Common part (predefined or free line)
+								$desc .= dol_htmlentitiesbr($lines[$i]->desc);
+								$desc .= '<br>';
+								$desc .= ' (' . $langs->trans('Quantity') . ': ' . $lines[$i]->qty . ')';
+
 								$timearray=dol_getdate(mktime());
 								$date_intervention=dol_mktime(0,0,0,$timearray['mon'],$timearray['mday'],$timearray['year']);
-								if ($product_type == 1)
-								{ //service
-									$duration = $durationproduct;
-								}
-								else
-								{ //product
+
+								if ($product_type == Product::TYPE_PRODUCT) {
 									$duration = 0;
 								}
 
 								$predef = '';
+
 								// Extrafields
 								$extrafieldsline = new ExtraFields($db);
 								$extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
 								$array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline, $predef);
 
-
 			                    $result = $object->addline(
 									$user,
 			                        $id,
diff --git a/htdocs/langs/en_US/interventions.lang b/htdocs/langs/en_US/interventions.lang
index b0adb985dff..cad0806282e 100644
--- a/htdocs/langs/en_US/interventions.lang
+++ b/htdocs/langs/en_US/interventions.lang
@@ -45,6 +45,7 @@ TypeContact_fichinter_external_CUSTOMER=Following-up customer contact
 # Modele numérotation
 PrintProductsOnFichinter=Print also lines of type "product" (not only services) on intervention card
 PrintProductsOnFichinterDetails=interventions generated from orders
+UseServicesDurationOnFichinter=Use services duration for interventions generated from orders
 InterventionStatistics=Statistics of interventions
 NbOfinterventions=Nb of intervention cards
 NumberOfInterventionsByMonth=Nb of intervention cards by month (date of validation)
@@ -58,4 +59,4 @@ InterNote=Note intervention
 InterLineId=Line id intervention
 InterLineDate=Line date intervention
 InterLineDuration=Line duration intervention
-InterLineDesc=Line description intervention 
+InterLineDesc=Line description intervention
-- 
GitLab