From 6260c1ae9e259f25d14cfc109ad4e6eed3184e37 Mon Sep 17 00:00:00 2001
From: fmarcet <fmarcet@2byte.es>
Date: Wed, 3 Feb 2016 11:33:13 +0100
Subject: [PATCH] FIX: Check stock of batch on shippment

---
 htdocs/expedition/card.php                   |  3 ++-
 htdocs/expedition/class/expedition.class.php | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php
index 3bf17f76a4f..c7d91a28ce5 100644
--- a/htdocs/expedition/card.php
+++ b/htdocs/expedition/card.php
@@ -8,6 +8,7 @@
  * Copyright (C) 2013       Marcos García           <marcosgdf@gmail.com>
  * Copyright (C) 2014		Cedric GROSS			<c.gross@kreiz-it.fr>
  * Copyright (C) 2014		Francis Appels			<francis.appels@yahoo.com>
+ * Copyright (C) 2016		Ferran Marcet			<fmarcet@2byte.es>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -237,7 +238,7 @@ if (empty($reshook))
 						$ret=$object->addline_batch($batch_line[$i]);
 						if ($ret < 0)
 						{
-							$mesg='<div class="error">'.$object->error.'</div>';
+							$mesg='<div class="error">'.$object->errorsToString().'</div>';
 							$error++;
 						}
 					}
diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php
index cb77607261b..7dcf66d28ec 100644
--- a/htdocs/expedition/class/expedition.class.php
+++ b/htdocs/expedition/class/expedition.class.php
@@ -8,6 +8,7 @@
  * Copyright (C) 2014		Cedric GROSS			<c.gross@kreiz-it.fr>
  * Copyright (C) 2014-2015  Marcos García           <marcosgdf@gmail.com>
  * Copyright (C) 2014-2015  Francis Appels          <francis.appels@yahoo.com>
+ * Copyright (C) 2016		Ferran Marcet			<fmarcet@2byte.es>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -857,6 +858,7 @@ class Expedition extends CommonObject
 	 */
 	function addline_batch($dbatch)
 	{
+		global $conf,$langs;
 		$num = count($this->lines);
 		if ($dbatch['qty']>0)
 		{
@@ -882,7 +884,17 @@ class Expedition extends CommonObject
 
 					if ($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT)
 					{
-						// TODO
+						require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
+						$prod_batch = new Productbatch($this->db);
+						$prod_batch->fetch($value['id_batch']);
+
+						if ($prod_batch->qty < $linebatch->dluo_qty)
+						{
+							$this->errors[] = $langs->trans('ErrorStockIsNotEnough');
+							dol_syslog(get_class($this)."::addline_batch error=Product ".$prod_batch->batch.": ".$this->errorsToString(), LOG_ERR);
+							$this->db->rollback();
+							return -1;
+						}
 					}
 					
 					//var_dump($linebatch);
@@ -895,6 +907,7 @@ class Expedition extends CommonObject
 
 			//var_dump($line);
 			$this->lines[$num] = $line;
+			return 1;
 		}
 	}
 
-- 
GitLab