From 9cf06ec21c8a98cdfac096386c17c6ffae1aa13d Mon Sep 17 00:00:00 2001
From: aspangaro <alexandre.spangaro@gmail.com>
Date: Thu, 18 Jun 2015 06:12:05 +0200
Subject: [PATCH] Add function set_paid

---
 htdocs/expensereport/card.php                 | 18 ++++++++++-
 .../class/expensereport.class.php             | 30 +++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/htdocs/expensereport/card.php b/htdocs/expensereport/card.php
index 54bdc279098..8e382fb4285 100644
--- a/htdocs/expensereport/card.php
+++ b/htdocs/expensereport/card.php
@@ -827,6 +827,18 @@ if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 &&
 	}
 }
 
+if ($action == 'set_paid')
+{
+	if ($object->set_paid($id) >= 0)
+	{
+		header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
+		exit;
+	}
+    else {
+	    setEventMessage($object->error, 'errors');
+    }
+}
+
 if ($action == "addline")
 {
 	$error = 0;
@@ -1988,7 +2000,11 @@ if ($action != 'create' && $action != 'edit')
 		{
 			print '<div class="inline-block divButAction"><a class="butAction" href="'.DOL_URL_ROOT.'/expensereport/payment/payment.php?id=' . $object->id . '&amp;action=create">' . $langs->trans('DoPayment') . '</a></div>';
 		}
-		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=paid&id='.$object->id.'">'.$langs->trans('TO_PAID').'</a>';
+		
+		if ($object->statut == 1 && round($remaintopay) == 0 && $object->paid == 0 && $user->rights->don->creer)
+		{
+			print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?rowid='.$object->id.'&action=set_paid">'.$langs->trans("ClassifyPaid")."</a></div>";
+		}
 
 		// Cancel
 		if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php
index 7dede6d11a9..168b4580a62 100644
--- a/htdocs/expensereport/class/expensereport.class.php
+++ b/htdocs/expensereport/class/expensereport.class.php
@@ -372,6 +372,36 @@ class ExpenseReport extends CommonObject
 		}
 	}
 
+    /**
+     *    Classify the expense report as paid
+     *
+     *    @param	int		$id           	    id of expense report
+     *    @param    int		$modepayment   	    mode of payment
+     *    @return   int      					<0 if KO, >0 if OK
+     */
+    function set_paid($id)
+    {
+        $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport SET fk_statut = 6";
+        $sql.= " WHERE rowid = $id AND fk_statut = 5";
+
+        $resql=$this->db->query($sql);
+        if ($resql)
+        {
+            if ($this->db->affected_rows($resql))
+            {
+                return 1;
+            }
+            else
+            {
+                return 0;
+            }
+        }
+        else
+        {
+            dol_print_error($this->db);
+            return -1;
+        }
+    }
 
 	/**
 	 *	Returns the label status
-- 
GitLab