2020-03-22 12:31:27 +01:00
< ? php
2015-12-18 18:00:31 +01:00
// Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
2017-09-26 12:32:50 +02:00
// Copyright (C) 2017 Francis Appels <francis.appels@z-application.com>
2015-12-18 18:00:31 +01:00
//
// 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
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// or see https://www.gnu.org/
2015-12-18 18:00:31 +01:00
2020-03-22 12:31:27 +01:00
/**
2020-03-22 17:06:59 +01:00
* \file htdocs / fourn / js / lib_dispatch . js . php
2020-03-22 12:31:27 +01:00
* \brief File that include javascript functions used for dispatching qty / stock / lot
*/
2021-02-25 23:21:30 +01:00
if ( ! defined ( 'NOREQUIRESOC' )) {
define ( 'NOREQUIRESOC' , '1' );
}
if ( ! defined ( 'NOCSRFCHECK' )) {
define ( 'NOCSRFCHECK' , 1 );
}
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , 1 );
}
if ( ! defined ( 'NOLOGIN' )) {
define ( 'NOLOGIN' , 1 );
}
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , 1 );
}
if ( ! defined ( 'NOREQUIREHTML' )) {
define ( 'NOREQUIREHTML' , 1 );
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
2015-12-18 18:00:31 +01:00
2020-03-22 12:31:27 +01:00
session_cache_limiter ( 'public' );
require_once '../../main.inc.php' ;
// Define javascript type
top_httphead ( 'text/javascript; charset=UTF-8' );
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
2021-02-25 23:21:30 +01:00
if ( empty ( $dolibarr_nocache )) {
header ( 'Cache-Control: max-age=10800, public, must-revalidate' );
} else {
header ( 'Cache-Control: no-cache' );
}
2020-03-22 12:31:27 +01:00
?>
2015-12-18 18:00:31 +01:00
/**
* addDispatchLine
2020-03-22 12:31:27 +01:00
* Adds new table row for dispatching to multiple stock locations or multiple lot / serial
2018-07-26 18:22:16 +02:00
*
2017-09-26 12:32:50 +02:00
* @ param index int index of product line . 0 = first product line
2023-05-03 04:51:15 +02:00
* @ param type string type of dispatch ( 'batch' = batch dispatch , 'dispatch' = non batch dispatch )
2017-06-27 13:57:41 +02:00
* @ param mode string 'qtymissing' will create new line with qty missing , 'lessone' will keep 1 in old line and the rest in new one
2015-12-18 18:00:31 +01:00
*/
2023-04-27 14:56:27 +02:00
function addDispatchLine ( index , type , mode ) {
2017-06-27 13:57:41 +02:00
mode = mode || 'qtymissing'
2018-07-26 18:22:16 +02:00
2023-05-03 04:51:15 +02:00
console . log ( " fourn/js/lib_dispatch.js.php addDispatchLine Split line type= " + type + " index= " + index + " mode= " + mode );
2021-07-19 14:06:56 +02:00
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
2023-05-03 04:51:15 +02:00
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
2020-03-22 17:06:59 +01:00
var qtyOrdered = parseFloat ( $ ( " #qty_ordered_0_ " + index ) . val ()); // Qty ordered is same for all rows
2023-05-03 04:51:15 +02:00
2020-03-22 17:06:59 +01:00
var qty = parseFloat ( $ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . val ());
2023-05-03 04:51:15 +02:00
if ( isNaN ( qty )) {
qty = '' ;
}
2023-04-10 16:58:38 +02:00
2023-05-03 04:51:15 +02:00
console . log ( " fourn/js/lib_dispatch.js.php addDispatchLine Split line nbrTrs= " + nbrTrs + " qtyOrdered= " + qtyOrdered + " qty= " + qty );
2023-04-10 16:58:38 +02:00
2020-03-22 17:06:59 +01:00
var qtyDispatched ;
2018-07-26 18:22:16 +02:00
2023-04-27 14:56:27 +02:00
if ( mode === 'lessone' ) {
qtyDispatched = parseFloat ( $ ( " #qty_dispatched_0_ " + index ) . val ()) + 1 ;
2015-12-18 18:00:31 +01:00
}
2023-04-27 14:56:27 +02:00
else {
qtyDispatched = parseFloat ( $ ( " #qty_dispatched_0_ " + index ) . val ()) + qty ;
2020-03-22 12:31:27 +01:00
// 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 ) {
2023-04-27 14:56:27 +02:00
qtyDispatched = parseFloat ( $ ( " #qty_dispatched_0_ " + index ) . val ()) + 1 ;
2020-03-22 12:31:27 +01:00
mode = 'lessone' ;
}
2015-12-18 18:00:31 +01:00
}
2023-05-03 04:51:15 +02:00
console . log ( " qtyDispatched= " + qtyDispatched + " qtyOrdered= " + qtyOrdered + " qty= " + qty );
2018-07-26 18:22:16 +02:00
2023-04-10 16:58:38 +02:00
if ( qty <= 1 ) {
2023-04-04 18:42:55 +02:00
window . alert ( " Remain quantity to dispatch is too low to be split " );
2023-04-10 16:58:38 +02:00
} else {
2023-06-29 13:47:28 +02:00
var oldlineqty = qtyDispatched ;
var newlineqty = qtyOrdered - qtyDispatched ;
2023-04-10 16:58:38 +02:00
if ( newlineqty <= 0 ) {
newlineqty = qty - 1 ;
oldlineqty = 1 ;
$ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . val ( oldlineqty );
}
2015-12-18 18:00:31 +01:00
//replace tr suffix nbr
2023-04-27 14:56:27 +02:00
$row . html ( $row . html () . replace ( / _0_ / g , " _ " + nbrTrs + " _ " ));
2021-07-19 14:06:56 +02:00
// jquery's deep clone is incompatible with date pickers (the clone shares data with the original)
// so we destroy and rebuild the new date pickers
setTimeout (() => {
$row . find ( '.hasDatepicker' ) . each (( i , dp ) => {
$ ( dp ) . removeData ()
. removeClass ( 'hasDatepicker' );
$ ( dp ) . next ( 'img.ui-datepicker-trigger' ) . remove ();
$ ( dp ) . datepicker ( $dpopt );
});
}, 0 );
2015-12-18 18:00:31 +01:00
//create new select2 to avoid duplicate id of cloned one
2023-04-27 14:56:27 +02:00
$row . find ( " select[name=' " + 'entrepot_' + nbrTrs + '_' + index + " '] " ) . select2 ();
2023-11-14 22:39:18 +01:00
// Copy selected option to new select
let $prevRow = $ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last " )
let $prevEntr = Number ( $prevRow . find ( " select[name=' " + 'entrepot_' + ( nbrTrs - 1 ) + '_' + index + " '] " ) . val ())
$row . find ( " select[name=' " + 'entrepot_' + nbrTrs + '_' + index + " '] " ) . val ( $prevEntr )
2015-12-18 18:00:31 +01:00
// TODO find solution to keep new tr's after page refresh
//clear value
$row . find ( " input[name^='qty'] " ) . val ( '' );
//change name of new row
2023-04-27 14:56:27 +02:00
$row . attr ( 'name' , type + '_' + nbrTrs + '_' + index );
2015-12-18 18:00:31 +01:00
//insert new row before last row
2023-04-27 14:56:27 +02:00
$ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last " ) . after ( $row );
2018-07-26 18:22:16 +02:00
2015-12-18 18:00:31 +01:00
//remove cloned select2 with duplicate id.
2023-04-27 14:56:27 +02:00
$ ( " #s2id_entrepot_ " + nbrTrs + '_' + index ) . detach (); // old way to find duplicated select2 component
$ ( " .csswarehouse_ " + nbrTrs + " _ " + index + " :first-child " ) . parent ( " span.selection " ) . parent ( " .select2 " ) . detach ();
2018-07-26 18:22:16 +02:00
2015-12-18 18:00:31 +01:00
/* Suffix of lines are: _ trs.length _ index */
$ ( " #qty_ " + nbrTrs + " _ " + index ) . focus ();
2023-04-10 16:58:38 +02:00
$ ( " #qty_dispatched_0_ " + index ) . val ( oldlineqty );
2018-07-26 18:22:16 +02:00
2017-06-27 13:57:41 +02:00
//hide all buttons then show only the last one
2023-04-27 14:56:27 +02:00
$ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " '] .splitbutton " ) . hide ();
$ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last .splitbutton " ) . show ();
2018-07-26 18:22:16 +02:00
2023-09-13 01:39:57 +02:00
$ ( " #reset_ " + ( nbrTrs ) + " _ " + index ) . click ( function ( event ) {
event . preventDefault ();
2023-04-06 15:50:34 +02:00
id = $ ( this ) . attr ( " id " );
id = id . split ( " reset_ " );
idrow = id [ 1 ];
2023-04-27 14:56:27 +02:00
idlast = $ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last .qtydispatchinput " ) . attr ( " id " );
if ( idlast == $ ( " #qty_ " + idrow ) . attr ( " id " )) {
console . log ( " Remove trigger for tr name = " + type + " _ " + idrow );
$ ( 'tr[name="' + type + '_' + idrow + '"' ) . remove ();
$ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last .splitbutton " ) . show ();
2023-04-06 15:50:34 +02:00
} else {
2023-09-13 01:39:57 +02:00
console . log ( " fourn/js/lib_dispatch.js.php Reset trigger for id = qty_ " + idrow );
2023-04-27 14:56:27 +02:00
$ ( " #qty_ " + idrow ) . val ( " " );
2023-04-06 15:50:34 +02:00
}
2023-04-27 14:56:27 +02:00
});
2023-04-06 15:50:34 +02:00
2017-06-27 13:57:41 +02:00
if ( mode === 'lessone' )
2015-12-18 18:00:31 +01:00
{
2017-09-26 15:44:40 +02:00
qty = 1 ; // keep 1 in old line
$ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . val ( qty );
2017-06-27 13:57:41 +02:00
}
2023-04-10 16:58:38 +02:00
$ ( " #qty_ " + nbrTrs + " _ " + index ) . val ( newlineqty );
2017-09-26 15:44:40 +02:00
// Store arbitrary data for dispatch qty input field change event
2023-04-27 14:56:27 +02:00
$ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . data ( 'qty' , qty );
$ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . data ( 'type' , type );
$ ( " #qty_ " + ( nbrTrs - 1 ) + " _ " + index ) . data ( 'index' , index );
2017-09-26 15:44:40 +02:00
// Update dispatched qty when value dispatch qty input field changed
2023-04-27 14:56:27 +02:00
//$("#qty_" + (nbrTrs - 1) + "_" + index).change(this.onChangeDispatchLineQty);
2017-06-27 13:57:41 +02:00
//set focus on lot of new line (if it exists)
2023-04-27 14:56:27 +02:00
$ ( " #lot_number_ " + ( nbrTrs ) + " _ " + index ) . focus ();
//Clean bad values
$ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " ']:last " ) . data ( " remove " , " remove " );
$ ( " #lot_number_ " + ( nbrTrs ) + " _ " + index ) . val ( " " )
$ ( " #idline_ " + ( nbrTrs ) + " _ " + index ) . val ( " -1 " )
$ ( " #qty_ " + ( nbrTrs ) + " _ " + index ) . data ( 'expected' , " 0 " );
$ ( " #lot_number_ " + ( nbrTrs ) + " _ " + index ) . removeAttr ( " disabled " );
2015-12-18 18:00:31 +01:00
}
2017-09-26 12:32:50 +02:00
}
/**
* onChangeDispatchLineQty
2018-07-26 18:22:16 +02:00
*
* Change event handler for dispatch qty input field ,
2017-09-26 15:44:40 +02:00
* recalculate qty dispatched when qty input has changed .
2020-03-22 17:06:59 +01:00
* If qty is more than qty ordered reset input qty to max qty to dispatch .
2018-07-26 18:22:16 +02:00
*
2017-09-26 12:32:50 +02:00
* element requires arbitrary data qty ( value before change ), type ( type of dispatch ) and index ( index of product line )
*/
2023-04-27 14:56:27 +02:00
function onChangeDispatchLineQty ( element ) {
var type = $ ( element ) . data ( 'type' ),
qty = parseFloat ( $ ( element ) . data ( 'expected' )),
2017-09-26 12:32:50 +02:00
changedQty , nbrTrs , dispatchingQty , qtyOrdered , qtyDispatched ;
2023-04-27 14:56:27 +02:00
id = $ ( element ) . attr ( " id " );
id = id . split ( " _ " );
index = id [ 2 ];
2017-09-26 12:32:50 +02:00
if ( index >= 0 && type && qty >= 0 ) {
2023-04-27 14:56:27 +02:00
nbrTrs = $ ( " tr[name^=' " + type + " _'][name $ ='_ " + index + " '] " ) . length ;
qtyChanged = parseFloat ( $ ( element ) . val ()) - qty ; // qty changed
qtyDispatching = parseFloat ( $ ( element ) . val ()); // qty currently being dispatched
qtyOrdered = parseFloat ( $ ( " #qty_ordered_0_ " + index ) . val ()); // qty ordered
qtyDispatched = parseFloat ( $ ( " #qty_dispatched_0_ " + index ) . val ()); // qty already dispatched
2017-09-26 12:32:50 +02:00
2023-04-27 14:56:27 +02:00
console . log ( " onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: " + qtyDispatched );
2017-09-26 12:32:50 +02:00
if (( qtyChanged ) <= ( qtyOrdered - ( qtyDispatched + qtyDispatching ))) {
2023-04-27 14:56:27 +02:00
$ ( " #qty_dispatched_0_ " + index ) . val ( qtyDispatched + qtyChanged );
2017-09-26 12:32:50 +02:00
} else {
2023-05-03 04:51:15 +02:00
/* console . log ( " eee " );
$ ( element ) . val ( $ ( element ) . data ( 'expected' )); */
2017-09-26 12:32:50 +02:00
}
2023-04-27 14:56:27 +02:00
$ ( element ) . data ( 'expected' , $ ( element ) . val ());
2017-09-26 12:32:50 +02:00
}
2018-07-26 18:22:16 +02:00
}