Browse Source

Merge pull request #19929 from atm-lena/develop_of_consumption_selectbatch

Of consumption select batch and warehouse
Laurent Destailleur 2 years ago
parent
commit
4ad7b69cf3

+ 90 - 0
htdocs/mrp/ajax/interface.php

@@ -0,0 +1,90 @@
+<?php
+
+/**
+ *	\file       htdocs/mrp/ajax/interface.php
+ */
+
+if (!defined('NOREQUIRESOC')) {
+	define('NOREQUIRESOC', '1');
+}
+if (!defined('NOCSRFCHECK')) {
+	define('NOCSRFCHECK', '1');
+}
+if (!defined('NOTOKENRENEWAL')) {
+	define('NOTOKENRENEWAL', '1');
+}
+if (!defined('NOREQUIREMENU')) {
+	define('NOREQUIREMENU', '1');
+}
+if (!defined('NOREQUIREHTML')) {
+	define('NOREQUIREHTML', '1');
+}
+if (!defined('NOREQUIREAJAX')) {
+	define('NOREQUIREAJAX', '1');
+}
+
+require '../../main.inc.php'; // Load $user and permissions
+
+$warehouse_id = GETPOST('warehouse_id', 'int');
+$batch = GETPOST('batch', 'alphanohtml');
+$fk_product = GETPOST('product_id', 'int');
+$action = GETPOST('action', 'alphanohtml');
+
+$result = restrictedArea($user, 'mrp');
+
+$permissiontoproduce = $user->rights->mrp->write;
+
+
+
+/*
+ * View
+ */
+
+top_httphead("application/json");
+
+if ($action == 'updateselectbatchbywarehouse' && $permissiontoproduce) {
+	$TRes = array();
+
+	$sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.ref as label, ps.fk_product";
+	$sql .= " FROM " . MAIN_DB_PREFIX . "product_batch as pb";
+	$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_stock as ps on ps.rowid = pb.fk_product_stock";
+	$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (" . getEntity('stock') . ")";
+	$sql .= " WHERE ps.fk_product = " .((int) $fk_product);
+	if ($warehouse_id > 0) $sql .= " AND fk_entrepot = '" . ((int) $warehouse_id) . "'";
+	$sql .= " ORDER BY e.ref, pb.batch";
+
+	$resql = $db->query($sql);
+
+	if ($resql) {
+		while ($obj = $db->fetch_object($resql)) {
+			if (empty($TRes[$obj->batch])) {
+				$TRes[$obj->batch]  = $obj->qty;
+			} else {
+				$TRes[$obj->batch] += $obj->qty;
+			}
+		}
+	}
+
+	echo json_encode($TRes);
+} elseif ($action == 'updateselectwarehousebybatch' && $permissiontoproduce) {
+	$res = 0;
+
+	$sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, e.ref, pb.qty";
+	$sql .= " FROM " . MAIN_DB_PREFIX . "product_batch as pb";
+	$sql .= " JOIN " . MAIN_DB_PREFIX . "product_stock as ps on ps.rowid = pb.fk_product_stock";
+	$sql .= " JOIN " . MAIN_DB_PREFIX . "entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (" . getEntity('stock') . ")";
+	$sql .= " WHERE ps.fk_product = " .((int) $fk_product);
+	if ($batch) $sql.= " AND pb.batch = '" . $db->escape($batch) . "'";
+	$sql .= " ORDER BY e.ref, pb.batch";
+
+	$resql = $db->query($sql);
+
+	if ($resql) {
+		if ($db->num_rows($resql) == 1) {
+			$obj = $db->fetch_object($resql);
+			$res = $obj->fk_entrepot;
+		}
+	}
+
+	echo json_encode($res);
+}

+ 103 - 0
htdocs/mrp/mo_production.php

@@ -1495,6 +1495,109 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
 	if (in_array($action, array('consumeorproduce', 'consumeandproduceall', 'addconsumeline'))) {
 		print "</form>\n";
 	}
+
+	?>
+
+		<script  type="text/javascript" language="javascript">
+
+			$(document).ready(function() {
+				//Consumption : When a warehouse is selected, only the lot/serial numbers that are available in it are offered
+				updateselectbatchbywarehouse();
+				//Consumption : When a lot/serial number is selected and it is only available in one warehouse, the warehouse is automatically selected
+				updateselectwarehousebybatch();
+			});
+
+			function updateselectbatchbywarehouse() {
+				var element = $("select[name*='idwarehouse']");
+
+				element.change(function () {
+
+					var selectwarehouse = $(this);
+
+					var selectbatch_name = selectwarehouse.attr('name').replace('idwarehouse', 'batch');
+					var selectbatch = $("datalist[id*='" + selectbatch_name + "']");
+					var selectedbatch = selectbatch.val();
+
+					var product_element_name = selectwarehouse.attr('name').replace('idwarehouse', 'product');
+
+					$.ajax({
+						type: "POST",
+						url: "<?php echo DOL_URL_ROOT . '/mrp/ajax/interface.php'; ?>",
+						data: {
+							action: "updateselectbatchbywarehouse",
+							permissiontoproduce: <?php echo $permissiontoproduce ?>,
+							warehouse_id: $(this).val(),
+							product_id: $("input[name='" + product_element_name + "']").val()
+						}
+					}).done(function (data) {
+
+						selectbatch.empty();
+
+						var data = JSON.parse(data);
+
+						selectbatch.append($('<option>', {
+							value: '',
+						}));
+
+						$.each(data, function (key, value) {
+
+							if(selectwarehouse.val() == -1) {
+								var label = " (<?php echo $langs->trans('Stock total') ?> : " + value + ")";
+							} else {
+								var label =  " (<?php echo $langs->trans('Stock') ?> : " + value + ")";
+							}
+
+							if(key === selectedbatch) {
+								var option ='<option value="'+key+'" selected>'+ label +'</option>';
+							} else {
+								var option ='<option value="'+key+'">'+ label +'</option>';
+							}
+
+							selectbatch.append(option);
+						});
+					});
+				});
+			}
+
+			function updateselectwarehousebybatch() {
+
+				$(document).on('change', 'input[name*=batch]', function(){
+
+					var selectbatch = $(this);
+
+					var selectwarehouse_name = selectbatch.attr('name').replace('batch', 'idwarehouse');
+					var selectwarehouse = $("select[name*='" + selectwarehouse_name + "']");
+					var selectedwarehouse = selectwarehouse.val();
+
+					if(selectedwarehouse != -1){
+						return;
+					}
+
+					var product_element_name = selectbatch.attr('name').replace('batch', 'product');
+
+					$.ajax({
+						type: "POST",
+						url: "<?php echo DOL_URL_ROOT . '/mrp/ajax/interface.php'; ?>",
+						data: {
+							action: "updateselectwarehousebybatch",
+							permissiontoproduce: <?php echo $permissiontoproduce ?>,
+							batch: $(this).val(),
+							product_id: $("input[name='" + product_element_name + "']").val()
+						}
+					}).done(function (data) {
+
+						var data = JSON.parse(data);
+
+						if(data != 0){
+							selectwarehouse.val(data).change();
+						}
+					});
+				});
+			}
+
+		</script>
+
+	<?php
 }
 
 // End of page

+ 1 - 1
htdocs/product/class/html.formproduct.class.php

@@ -825,7 +825,7 @@ class FormProduct
 					if (empty($fk_entrepot) || $fk_entrepot == $arraytypes['entrepot_id']) {
 						$label = $arraytypes['entrepot_label'] . ' - ';
 						$label .= $arraytypes['batch'];
-						$out .= '<option data-warehouse="'.dol_escape_htmltag($label).'">' . $arraytypes['batch'] . '</option>';
+						$out .= '<option data-warehouse="'.dol_escape_htmltag($label).'" value="' . $arraytypes['batch'] . '">(' . $langs->trans('Stock Total') . ': ' . $arraytypes['qty'] . ')</option>';
 					}
 				}
 			}