Skip to content

Commit

Permalink
fix: 29673
Browse files Browse the repository at this point in the history
  • Loading branch information
FHenry committed May 24, 2024
1 parent e92fa65 commit 0b66333
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 22 deletions.
30 changes: 19 additions & 11 deletions htdocs/fourn/commande/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}

// Load translation files required by the page
$langs->loadLangs(array("bills", "orders", "sendings", "companies", "deliveries", "products", "stocks", "receptions"));
$langs->loadLangs(array("bills", "orders", "sendings", "companies", "deliveries", "products", "stocks", "receptions", "mrp"));

if (isModEnabled('productbatch')) {
$langs->load('productbatch');
Expand Down Expand Up @@ -757,6 +757,7 @@
print ' <td class="right">'.$langs->trans("QtyToDispatchShort");
print '<br><a href="#" id="autoreset">'.img_picto($langs->trans("Reset"), 'eraser', 'class="pictofixedwidth opacitymedium"').$langs->trans("Reset").'</a></td>';
print '<td width="32"></td>';
print '<td width="32"></td>';

if (getDolGlobalString('SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT')) {
if (!isModEnabled("multicurrency") && empty($conf->dynamicprices->enabled)) {
Expand Down Expand Up @@ -888,13 +889,14 @@
// Already dispatched
print '<td class="right">'.$alreadydispatched.'</td>';

$maxQty = 1;

if (isModEnabled('productbatch') && $objp->tobatch > 0) {
$type = 'batch';
print '<td class="right">';
print '</td>'; // Qty to dispatch
print '<td>';
//print img_picto($langs->trans('AddDispatchBatchLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine(' . $i . ',\'' . $type . '\')"');
print '</td>'; // Dispatch column
print '<td></td>'; // Dispatch column
print '<td></td>'; // Dispatch All column
print '<td></td>'; // Warehouse column

// Enable hooks to append additional columns
Expand All @@ -918,7 +920,7 @@

print '</tr>';

print '<tr class="oddeven" name="'.$type.$suffix.'">';
print '<tr class="oddeven" name="'.$type.$suffix.'" data-max-qty="'.$maxQty.'">';
print '<td>';
print '<input name="fk_commandefourndet'.$suffix.'" type="hidden" value="'.$objp->rowid.'">';
print '<input name="product_batch'.$suffix.'" type="hidden" value="'.$objp->fk_product.'">';
Expand Down Expand Up @@ -955,9 +957,8 @@
$colspan = (getDolGlobalString('PRODUCT_DISABLE_EATBY')) ? --$colspan : $colspan;
print '<td class="right">';
print '</td>'; // Qty to dispatch
print '<td>';
//print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine(' . $i . ',\'' . $type . '\')"');
print '</td>'; // Dispatch column
print '<td></td>'; // Dispatch column
print '<td></td>'; // Dispatch All column
print '<td></td>'; // Warehouse column

// Enable hooks to append additional columns
Expand All @@ -981,7 +982,7 @@

print '</tr>';

print '<tr class="oddeven" name="'.$type.$suffix.'">';
print '<tr class="oddeven" name="'.$type.$suffix.'" data-max-qty="'.$maxQty.'">';
print '<td colspan="'.$colspan.'">';
print '<input name="fk_commandefourndet'.$suffix.'" type="hidden" value="'.$objp->rowid.'">';
print '<input name="product'.$suffix.'" type="hidden" value="'.$objp->fk_product.'">';
Expand All @@ -1005,13 +1006,20 @@
print '<td>';
if (isModEnabled('productbatch') && $objp->tobatch > 0) {
$type = 'batch';
print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$i.', \''.$type.'\')"');
} else {
$type = 'dispatch';
print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$i.', \''.$type.'\')"');
}
print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$i.', \''.$type.'\')"');

print '</td>';

print '<td class="splitall">';
if ($remaintodispatch>1) {
print img_picto($langs->trans('SplitAllQuantity'), 'split.png', 'class="splitbutton splitallbutton field-error-icon" onClick="addDispatchLine(' . $i . ', \'' . $type . '\', \'alltodispatch\')"');
}
print '</td>';


if (getDolGlobalString('SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT')) {
if (!isModEnabled("multicurrency") && empty($conf->dynamicprices->enabled)) {
// Price
Expand Down
155 changes: 144 additions & 11 deletions htdocs/fourn/js/lib_dispatch.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@
*
* @param index int index of product line. 0 = first product line
* @param type string type of dispatch ('batch' = batch dispatch, 'dispatch' = non batch dispatch)
* @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one
* @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one, 'alltodispatch' for create all lines
*/
function addDispatchLine(index, type, mode) {
mode = mode || 'qtymissing'

console.log("fourn/js/lib_dispatch.js.php addDispatchLine Split line type="+type+" index="+index+" mode="+mode);

var $row0 = $("tr[name='"+type+'_0_'+index+"']");
var $dpopt = $row0.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
var $row = $row0.clone(true); // clone first batch line to jQuery object
var nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // count nb of tr line with attribute name that starts with 'batch_' or 'dispatch_', and end with _index
var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
let $row = $("tr[name='"+type+'_0_'+index+"']").clone(true);
let $dpopt = $row.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
let nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // count nb of tr line with attribute name that starts with 'batch_' or 'dispatch_', and end with _index
let qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows

var qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val());
if (isNaN(qty)) {
Expand All @@ -93,22 +92,85 @@ function addDispatchLine(index, type, mode) {
// If user did not reduced the qty to dispatch on old line, we keep only 1 on old line and the rest on new line
if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
mode = 'lessone';

}
if(mode == 'alltodispatch') {
var qtymax = parseFloat($($row).data('max-qty'));
if(qtymax === 'undefined') qtymax = 1;
}
}
console.log("qtyDispatched=" + qtyDispatched + " qtyOrdered=" + qtyOrdered+ " qty=" + qty);
console.log("qtyDispatched=" + qtyDispatched + " qtyOrdered=" + qtyOrdered+ " qty=" + qty + " mode=" + mode);

if(mode === "alltodispatch") {
let count = 0;
let qtyalreadyused = 0;
let error = 0;

if (qty <= 1) {
window.alert("Remain quantity to dispatch is too low to be split");
while (qtyalreadyused < qty) {
//If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;

console.log(qtyalreadyused,qty, qtymax);
//If first line, we replace value, not add line
if(count === 0){
$("#qty"+"-"+index+"-"+nbrTrs).val(qtymax);
} else {
var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, type, qtymax, mode, $row, $dpopt);
console.log("res="+res);
if(res === -1){
error = 1;
break;
}
nbrTrs++;
}
qtyalreadyused = qtyalreadyused + qtymax;
count++;
$row = $("tr[name='"+type+'_0_'+index+"']").clone(true);
}

if(error === 0) {
addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, type, '', mode, $row,$dpopt);
}
} else {
addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, type, qty, mode, $row,$dpopt)
}



}

/**
* addDispatchTR
* Adds new table row for dispatching to multiple stock locations or multiple lot/serial
*
* @param qtyOrdered double
* @param qtyDispatched double
* @param index int
* @param nbrTrs int
* @param type string
* @param qty double
* @param mode string
* @param $row object
* @param $row object
*/
function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, type, qty, mode, $row, $dpopt) {
if (qtyOrdered <= 1) {
let errormsg = '<?php echo dol_escape_js($langs->trans('QtyCantBeSplit')); ?>';
$.jnotify(errormsg, 'error', true);
return -1;
} else if (qtyDispatched >= qtyOrdered) {
let errormsg = '<?php echo dol_escape_js($langs->trans('NoRemainQtyToDispatch')); ?>';
$.jnotify(errormsg, 'error', true);
return -1;
} else if (qtyDispatched < qtyOrdered) {
console.log()
var oldlineqty = qtyDispatched;
var newlineqty = qtyOrdered - qtyDispatched;
if (newlineqty <= 0) {
newlineqty = qty - 1;
oldlineqty = 1;
$("#qty_"+(nbrTrs - 1)+"_"+index).val(oldlineqty);
}

//replace tr suffix nbr
$row.html($row.html().replace(/_0_/g, "_" + nbrTrs + "_"));

Expand Down Expand Up @@ -186,6 +248,77 @@ function addDispatchLine(index, type, mode) {
$("#qty_" + (nbrTrs) + "_" + index).data('expected', "0");
$("#lot_number_" + (nbrTrs) + "_" + index).removeAttr("disabled");
}


//
//if (qtyOrdered <= 1) {
// let errormsg = '<?php //echo dol_escape_js($langs->trans('QtyCantBeSplit')); ?>//';
// $.jnotify(errormsg, 'error', true);
// return -1;
//} else if (qtyDispatched >= qtyOrdered) {
// let errormsg = '<?php //echo dol_escape_js($langs->trans('NoRemainQtyToDispatch')); ?>//';
// $.jnotify(errormsg, 'error', true);
// return -1;
//} else if (qtyDispatched < qtyOrdered) {
// //replace tr suffix nbr
// var re1 = new RegExp('_'+index+'_1', 'g');
// var re2 = new RegExp('-'+index+'-1', 'g');
// $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
// $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
// //create new select2 to avoid duplicate id of cloned one
// $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
// // TODO find solution to copy selected option to new select
// // TODO find solution to keep new tr's after page refresh
// //clear value
// $row.find("input[id^='"+inputId+"']").val('');
// //change name of new row
// $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
// //insert new row before last row
// $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
//
// //remove cloned select2 with duplicate id.
// $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
// $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
//
// /* Suffix of lines are: index _ trs.length */
// $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
// if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
// if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
// else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
// }
// var totalonallines = 0;
// for (let i = 1; i <= nbrTrs; i++) {
// console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
// totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
// }
//
// if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
// if (totalonallines == qtyOrdered && qtyOrdered > 1) {
// var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
// $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
// $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
// }
// }
// $("#qty_dispatched_"+index).val(qtyDispatched);
//
// //hide all buttons then show only the last one
// $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
// $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
//
// if (mode === 'lessone')
// {
// qty = 1; // keep 1 in old line
// $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
// }
// // Store arbitrary data for dispatch qty input field change event
// $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
// $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
// $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
// if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
// let currentQtyDispatched = qtyDispatched+qty;
// $row.find("input[id^='"+inputId+"']").val(qty);
// }
//}
}

/**
Expand Down

0 comments on commit 0b66333

Please sign in to comment.