Laurent Destailleur 1 ano atrás
pai
commit
03c05b4fd8

+ 43 - 14
htdocs/compta/prelevement/class/bonprelevement.class.php

@@ -502,10 +502,9 @@ class BonPrelevement extends CommonObject
 				if ($this->error) {
 					$error++;
 				}
-				//var_dump($facs);exit;
 
 				// Loop on each invoice or salary.
-				// $facs=array(0=>id, 1=>amount requested)
+				// $facs should be array(0=>id, 1=>amount requested)
 				$num = count($facs);
 				for ($i = 0; $i < $num; $i++) {
 					if ($this->type == 'bank-transfer') {
@@ -521,7 +520,15 @@ class BonPrelevement extends CommonObject
 					$result = $fac->fetch($facs[$i][0]);
 
 					$amounts[$fac->id] = $facs[$i][1];
-					$amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
+					if ($this->type == 'bank-transfer') {
+						if ($type == 'salary') {
+							$amountsperthirdparty[$fac->fk_user][$fac->id] = $facs[$i][1];
+						} else {
+							$amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
+						}
+					} else {
+						$amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
+					}
 
 					$totalpaid = $fac->getSommePaiement();
 					$totalcreditnotes = 0;
@@ -534,8 +541,13 @@ class BonPrelevement extends CommonObject
 					}
 					$alreadypayed = $totalpaid + $totalcreditnotes + $totaldeposits;
 
-					// @TODO Move this after creation of payment
-					if (price2num($alreadypayed + $facs[$i][1], 'MT') == $fac->total_ttc) {
+					// Set the main document to pay with status Paid.
+					// @TODO Move this after creation of payments done after
+					$amountofdocument = $fac->total_ttc;
+					if ($type == 'salary') {
+						$amountofdocument = $fac->amount;
+					}
+					if (price2num($alreadypayed + $facs[$i][1], 'MT') == price2num($amountofdocument, 'MT')) {
 						$result = $fac->setPaid($user);
 						if ($result < 0) {
 							$this->error = $fac->error;
@@ -543,7 +555,6 @@ class BonPrelevement extends CommonObject
 						}
 					}
 				}
-				//var_dump($amountsperthirdparty);exit;
 
 				// Make one payment per customer or employee
 				foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts) {
@@ -560,8 +571,16 @@ class BonPrelevement extends CommonObject
 					$paiement->amounts = $cursoramounts; // Array with detail of dispatching of payments for each invoice
 
 					if ($this->type == 'bank-transfer') {
-						$paiement->paiementid = 2;
-						$paiement->paiementcode = 'VIR';
+						if ($type == 'salary') {
+							$paiement->datep = $date;
+
+							$paiement->paiementid = 2;
+							$paiement->fk_typepayment = 2;
+							$paiement->paiementcode = 'VIR';
+						} else {
+							$paiement->paiementid = 2;
+							$paiement->paiementcode = 'VIR';
+						}
 					} else {
 						$paiement->paiementid = 3;
 						$paiement->paiementcode = 'PRE';
@@ -570,8 +589,9 @@ class BonPrelevement extends CommonObject
 					$paiement->num_payment = $this->ref; // Set ref of direct debit note
 					$paiement->id_prelevement = $this->id;
 
-					$paiement_id = $paiement->create($user); // This use ->paiementid, that is ID of payment mode
-					if ($paiement_id < 0) {
+					$result = $paiement->create($user); // This use ->paiementid, that is ID of payment mode
+
+					if ($result < 0) {
 						$error++;
 						$this->error = $paiement->error;
 						$this->errors = $paiement->errors;
@@ -594,6 +614,7 @@ class BonPrelevement extends CommonObject
 						}
 
 						$result = $paiement->addPaymentToBank($user, $modeforaddpayment, $labelforaddpayment, $fk_bank_account, '', '', 0, '', $addbankurl);
+
 						if ($result < 0) {
 							$error++;
 							$this->error = $paiement->error;
@@ -700,9 +721,9 @@ class BonPrelevement extends CommonObject
 	}
 
 	/**
-	 *	Get invoice or salary list
+	 *	Get invoice or salary list (with amount or not)
 	 *
-	 *  @param 	int		$amounts 	If you want to get the amount of the order for each invoice
+	 *  @param 	int		$amounts 	If you want to get the amount of the order for each invoice or salary
 	 *  @param  string  $type       'salary' for type=salary
 	 *	@return	array 				Array(Id of invoices/salary, Amount to pay)
 	 */
@@ -717,7 +738,11 @@ class BonPrelevement extends CommonObject
 		// Returns all invoices presented within same order
 		$sql = "SELECT ";
 		if ($this->type == 'bank-transfer') {
-			$sql .= " p.fk_facture_fourn";
+			if ($type == 'salary') {
+				$sql .= " p.fk_salary";
+			} else {
+				$sql .= " p.fk_facture_fourn";
+			}
 		} else {
 			$sql .= " p.fk_facture";
 		}
@@ -733,7 +758,11 @@ class BonPrelevement extends CommonObject
 		$sql .= " AND pb.entity = ".((int) $conf->entity);
 		if ($amounts) {
 			if ($this->type == 'bank-transfer') {
-				$sql .= " GROUP BY p.fk_facture_fourn";
+				if ($type == 'salary') {
+					$sql .= " GROUP BY p.fk_salary";
+				} else {
+					$sql .= " GROUP BY p.fk_facture_fourn";
+				}
 			} else {
 				$sql .= " GROUP BY p.fk_facture";
 			}

+ 3 - 3
htdocs/core/class/commonobject.class.php

@@ -675,12 +675,12 @@ abstract class CommonObject
 	public $sendtoid;
 
 	/**
-	 * @var	float		Amount already paid (used to show correct status)
+	 * @var	float		Amount already paid from getSommePaiement() (used to show correct status)
+	 * @deprecated		Duplicate of $totalpaid
 	 */
 	public $alreadypaid;
 	/**
-	 * @var	float		Amount already paid (used to show correct status)
-	 * @deprecated		Duplicate of $alreadypaid
+	 * @var	float		Amount already paid from getSommePaiement() (used to show correct status)
 	 */
 	public $totalpaid;
 

+ 34 - 36
htdocs/salaries/class/paymentsalary.class.php

@@ -233,45 +233,43 @@ class PaymentSalary extends CommonObject
 		$this->db->begin();
 
 		if ($totalamount != 0) {
-			$sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (entity, fk_salary, datec, datep, amount,";
-			$sql .= " fk_typepayment, num_payment, note, fk_user_author, fk_bank)";
-			$sql .= " VALUES (".((int) $conf->entity).", ".((int) $this->fk_salary).", '".$this->db->idate($now)."',";
-			$sql .= " '".$this->db->idate($this->datep)."',";
-			$sql .= " ".price2num($totalamount).",";
-			$sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".((int) $user->id).",";
-			$sql .= " 0)";
+			// Insert array of amounts
+			foreach ($this->amounts as $key => $amount) {
+				$salary_id = $key;
+				$amount = price2num($amount);
+				if (is_numeric($amount) && !empty($amount)) {
+					// We can have n payments for 1 salary but we can't have 1 payments for n salaries (for invoices link is n-n, not for salaries).
+					$sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (entity, fk_salary, datec, datep, amount,";
+					$sql .= " fk_typepayment, num_payment, note, fk_user_author, fk_bank)";
+					$sql .= " VALUES (".((int) $conf->entity).", ".((int) $salary_id).", '".$this->db->idate($now)."',";
+					$sql .= " '".$this->db->idate($this->datep)."',";
+					$sql .= " ".price2num($amount).",";
+					$sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".((int) $user->id).",";
+					$sql .= " 0)";
+
+					$resql = $this->db->query($sql);
+					if ($resql) {
+						$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
+					}
 
-			$resql = $this->db->query($sql);
-			if ($resql) {
-				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
-
-				// Insere tableau des montants / factures
-				foreach ($this->amounts as $key => $amount) {
-					$contribid = $key;
-					if (is_numeric($amount) && $amount <> 0) {
-						$amount = price2num($amount);
-
-						// If we want to closed payed invoices
-						if ($closepaidcontrib) {
-							$tmpsalary = new Salary($this->db);
-							$tmpsalary->fetch($contribid);
-							$paiement = $tmpsalary->getSommePaiement();
-							//$creditnotes=$tmpsalary->getSumCreditNotesUsed();
-							$creditnotes = 0;
-							//$deposits=$tmpsalary->getSumDepositsUsed();
-							$deposits = 0;
-							$alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
-							$remaintopay = price2num($tmpsalary->amount - $paiement - $creditnotes - $deposits, 'MT');
-							if ($remaintopay == 0) {
-								$result = $tmpsalary->setPaid($user);
-							} else {
-								dol_syslog("Remain to pay for salary id=".$contribid." not null. We do nothing.");
-							}
+					// If we want to closed payed invoices
+					if ($closepaidcontrib) {
+						$tmpsalary = new Salary($this->db);
+						$tmpsalary->fetch($salary_id);
+						$paiement = $tmpsalary->getSommePaiement();
+						//$creditnotes=$tmpsalary->getSumCreditNotesUsed();
+						$creditnotes = 0;
+						//$deposits=$tmpsalary->getSumDepositsUsed();
+						$deposits = 0;
+						$alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
+						$remaintopay = price2num($tmpsalary->amount - $paiement - $creditnotes - $deposits, 'MT');
+						if ($remaintopay == 0) {
+							$result = $tmpsalary->setPaid($user);
+						} else {
+							dol_syslog("Remain to pay for salary id=".$salary_id." not null. We do nothing.");
 						}
 					}
 				}
-			} else {
-				$error++;
 			}
 		}
 
@@ -284,7 +282,7 @@ class PaymentSalary extends CommonObject
 			$this->amount = $totalamount;
 			$this->total = $totalamount; // deprecated
 			$this->db->commit();
-			return $this->id;
+			return $this->id;	// id of the last payment inserted
 		} else {
 			$this->error = $this->db->error();
 			$this->db->rollback();