From 3c5159f497e2e470645ee69d7ddd5dc27e9e5144 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?=
 <marcosgdf@gmail.com>
Date: Sat, 5 Sep 2015 10:56:13 +0200
Subject: [PATCH] Created Facture::hasDelay function

---
 htdocs/compta/facture.php                     |  3 ++-
 htdocs/compta/facture/class/facture.class.php | 25 ++++++++++++++++++-
 htdocs/compta/facture/list.php                |  4 ++-
 htdocs/compta/facture/mergepdftool.php        |  6 ++++-
 htdocs/compta/facture/prelevement.php         |  4 ++-
 htdocs/compta/index.php                       | 12 +++++++--
 htdocs/core/boxes/box_factures.php            |  8 +++++-
 htdocs/core/boxes/box_factures_imp.php        |  6 ++++-
 8 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index 7f5b33ee73a..973b54470c0 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -3222,8 +3222,9 @@ else if ($id > 0 || ! empty($ref))
 			$form->form_date($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->date_lim_reglement, 'paymentterm');
 		} else {
 			print dol_print_date($object->date_lim_reglement, 'daytext');
-			if ($object->date_lim_reglement < ($now - $conf->facture->client->warning_delay) && ! $object->paye && $object->statut == 1 && ! isset($object->am))
+			if ($object->hasDelay()) {
 				print img_warning($langs->trans('Late'));
+			}
 		}
 	} else {
 		print '&nbsp;';
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index dbbc1d88212..5533f70a919 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -3236,11 +3236,15 @@ class Facture extends CommonInvoice
 			$response->url=DOL_URL_ROOT.'/compta/facture/list.php?search_status=1';
 			$response->img=img_object($langs->trans("Bills"),"bill");
 
+			$generic_facture = new Facture($this->db);
+
 			while ($obj=$this->db->fetch_object($resql))
 			{
+				$generic_facture->date_lim_reglement = $this->db->jdate($obj->datefin);
+
 				$response->nbtodo++;
 
-				if ($this->db->jdate($obj->datefin) < ($now - $conf->facture->client->warning_delay)) {
+				if ($generic_facture->hasDelay()) {
 					$response->nbtodolate++;
 				}
 			}
@@ -3697,6 +3701,25 @@ class Facture extends CommonInvoice
 
 		return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
 	}
+
+	/**
+	 * Is the customer invoice delayed?
+	 *
+	 * @return bool
+	 */
+	public function hasDelay()
+	{
+		global $conf;
+
+		$now = dol_now();
+
+		//Paid invoices have status STATUS_CLOSED
+		if (!$this->statut != Facture::STATUS_VALIDATED) {
+			return false;
+		}
+
+		return $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay);
+	}
 }
 
 /**
diff --git a/htdocs/compta/facture/list.php b/htdocs/compta/facture/list.php
index faefe63d9af..3aefeeefd36 100644
--- a/htdocs/compta/facture/list.php
+++ b/htdocs/compta/facture/list.php
@@ -378,6 +378,8 @@ if ($resql)
             $facturestatic->id=$objp->facid;
             $facturestatic->ref=$objp->facnumber;
             $facturestatic->type=$objp->type;
+            $facturestatic->statut = $objp->fk_statut;
+            $facturestatic->date_lim_reglement = $db->jdate($objp->datelimite);
             $notetoshow=dol_string_nohtmltag(($user->societe_id>0?$objp->note_public:$objp->note),1);
             $paiement = $facturestatic->getSommePaiement();
 
@@ -417,7 +419,7 @@ if ($resql)
 
             // Date limit
             print '<td align="center" class="nowrap">'.dol_print_date($datelimit,'day');
-            if ($datelimit < ($now - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $paiement)
+            if ($facturestatic->hasDelay())
             {
                 print img_warning($langs->trans('Late'));
             }
diff --git a/htdocs/compta/facture/mergepdftool.php b/htdocs/compta/facture/mergepdftool.php
index d29f237c331..4c8d7c7eba8 100644
--- a/htdocs/compta/facture/mergepdftool.php
+++ b/htdocs/compta/facture/mergepdftool.php
@@ -734,6 +734,8 @@ if ($resql)
 			$facturestatic->id=$objp->facid;
 			$facturestatic->ref=$objp->facnumber;
 			$facturestatic->type=$objp->type;
+			$facturestatic->statut=$objp->fk_statut;
+			$facturestatic->date_lim_reglement= $db->jdate($objp->datelimite);
 
 			print '<table class="nobordernopadding"><tr class="nocellnopadd">';
 
@@ -744,7 +746,9 @@ if ($resql)
 
 			// Warning picto
 			print '<td width="20" class="nobordernopadding nowrap">';
-			if ($date_limit < ($now - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1) print img_warning($langs->trans("Late"));
+			if ($facturestatic->hasDelay()) {
+				print img_warning($langs->trans("Late"));
+			}
 			print '</td>';
 
 			// PDF Picto
diff --git a/htdocs/compta/facture/prelevement.php b/htdocs/compta/facture/prelevement.php
index be78b422fe6..78a93d7da0d 100644
--- a/htdocs/compta/facture/prelevement.php
+++ b/htdocs/compta/facture/prelevement.php
@@ -356,7 +356,9 @@ if ($object->id > 0)
 		else
 		{
 			print dol_print_date($object->date_lim_reglement,'daytext');
-			if ($object->date_lim_reglement < ($now - $conf->facture->client->warning_delay) && ! $object->paye && $object->statut == Facture::STATUS_VALIDATED && ! isset($object->am)) print img_warning($langs->trans('Late'));
+			if ($object->hasDelay()) {
+				print img_warning($langs->trans('Late'));
+			}
 		}
 	}
 	else
diff --git a/htdocs/compta/index.php b/htdocs/compta/index.php
index 11296bda93e..6a1f3b15028 100644
--- a/htdocs/compta/index.php
+++ b/htdocs/compta/index.php
@@ -358,11 +358,15 @@ if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
                 $facturestatic->total_ht=$obj->total_ht;
                 $facturestatic->total_tva=$obj->total_tva;
                 $facturestatic->total_ttc=$obj->total_ttc;
+				$facturestatic->statut = $obj->fk_statut;
+				$facturestatic->date_lim_reglement = $db->jdate($obj->datelimite);
 				$facturestatic->type=$obj->type;
 				print $facturestatic->getNomUrl(1,'');
 				print '</td>';
 				print '<td width="20" class="nobordernopadding nowrap">';
-				if ($obj->fk_statut == 1 && ! $obj->paye && $db->jdate($obj->datelimite) < ($now - $conf->facture->client->warning_delay)) print img_warning($langs->trans("Late"));
+				if ($facturestatic->hasDelay()) {
+					print img_warning($langs->trans("Late"));
+				}
 				print '</td>';
 				print '<td width="16" align="right" class="nobordernopadding hideonsmartphone">';
 				$filename=dol_sanitizeFileName($obj->facnumber);
@@ -800,10 +804,14 @@ if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
                 $facturestatic->total_tva=$obj->total_tva;
                 $facturestatic->total_ttc=$obj->total_ttc;
 				$facturestatic->type=$obj->type;
+				$facturestatic->statut = $obj->fk_statut;
+				$facturestatic->date_lim_reglement = $db->jdate($obj->datelimite);
 				print $facturestatic->getNomUrl(1,'');
 				print '</td>';
 				print '<td width="20" class="nobordernopadding nowrap">';
-				if ($db->jdate($obj->datelimite) < ($now - $conf->facture->client->warning_delay)) print img_warning($langs->trans("Late"));
+				if ($facturestatic->hasDelay()) {
+					print img_warning($langs->trans("Late"));
+				}
 				print '</td>';
 				print '<td width="16" align="right" class="nobordernopadding hideonsmartphone">';
 				$filename=dol_sanitizeFileName($obj->facnumber);
diff --git a/htdocs/core/boxes/box_factures.php b/htdocs/core/boxes/box_factures.php
index d04c3e48a3b..f50bff34a86 100644
--- a/htdocs/core/boxes/box_factures.php
+++ b/htdocs/core/boxes/box_factures.php
@@ -108,12 +108,18 @@ class box_factures extends ModeleBoxes
                     $facturestatic->total_ht = $objp->total_ht;
                     $facturestatic->total_tva = $objp->total_tva;
                     $facturestatic->total_ttc = $objp->total_ttc;
+                    $facturestatic->statut = $objp->fk_statut;
+                    $facturestatic->date_lim_reglement = $db->jdate($objp->datelimite);
+
                     $societestatic->id = $objp->socid;
                     $societestatic->name = $objp->name;
                     $societestatic->code_client = $objp->code_client;
 
+
 					$late = '';
-					if ($objp->paye == 0 && ($objp->fk_statut != 2 && $objp->fk_statut != 3) && $datelimite < ($now - $conf->facture->client->warning_delay)) { $late = img_warning(sprintf($l_due_date,dol_print_date($datelimite,'day')));}
+					if ($facturestatic->hasDelay()) {
+                        $late = img_warning(sprintf($l_due_date,dol_print_date($datelimite,'day')));
+                    }
 
                     $this->info_box_contents[$line][] = array(
                         'td' => 'align="left"',
diff --git a/htdocs/core/boxes/box_factures_imp.php b/htdocs/core/boxes/box_factures_imp.php
index 42e889138cc..63ce986e181 100644
--- a/htdocs/core/boxes/box_factures_imp.php
+++ b/htdocs/core/boxes/box_factures_imp.php
@@ -108,6 +108,8 @@ class box_factures_imp extends ModeleBoxes
                     $facturestatic->total_ht = $objp->total_ht;
                     $facturestatic->total_tva = $objp->total_tva;
                     $facturestatic->total_ttc = $objp->total_ttc;
+					$facturestatic->statut = $objp->fk_statut;
+					$facturestatic->date_lim_reglement = $db->jdate($objp->datelimite);
                     $societestatic->id = $objp->socid;
                     $societestatic->name = $objp->name;
                     $societestatic->client = 1;
@@ -115,7 +117,9 @@ class box_factures_imp extends ModeleBoxes
                     $societestatic->logo = $objp->logo;
 
 					$late='';
-					if ($datelimite < ($now - $conf->facture->client->warning_delay)) $late = img_warning(sprintf($l_due_date,dol_print_date($datelimite,'day')));
+					if ($facturestatic->hasDelay()) {
+						$late = img_warning(sprintf($l_due_date,dol_print_date($datelimite,'day')));
+					}
 
                     $this->info_box_contents[$line][] = array(
                         'td' => 'align="left"',
-- 
GitLab