dolibarr 22.0.5
interface.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Laurent Destailleur (eldy) <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Lionel Vessiller <lvessiller@open-dsi.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24if (!defined('NOREQUIRESOC')) {
25 define('NOREQUIRESOC', '1');
26}
27if (!defined('NOCSRFCHECK')) {
28 define('NOCSRFCHECK', '1');
29}
30if (!defined('NOTOKENRENEWAL')) {
31 define('NOTOKENRENEWAL', '1');
32}
33if (!defined('NOREQUIREMENU')) {
34 define('NOREQUIREMENU', '1');
35}
36if (!defined('NOREQUIREHTML')) {
37 define('NOREQUIREHTML', '1');
38}
39if (!defined('NOREQUIREAJAX')) {
40 define('NOREQUIREAJAX', '1');
41}
42
43require '../../main.inc.php'; // Load $user and permissions
50$warehouse_id = GETPOSTINT('warehouse_id');
51$batch = GETPOST('batch', 'alphanohtml');
52$product_id = GETPOSTINT('product_id');
53$action = GETPOST('action', 'alphanohtml');
54
55$result = restrictedArea($user, 'expedition');
56
57$permissiontowrite = $user->hasRight('expedition', 'write');
58
59$is_eat_by_enabled = !getDolGlobalInt('PRODUCT_DISABLE_EATBY');
60$is_sell_by_enabled = !getDolGlobalInt('PRODUCT_DISABLE_SELLBY');
61
62
63/*
64 * View
65 */
66
67top_httphead("application/json");
68
69if ($action == 'updateselectbatchbywarehouse' && $permissiontowrite) {
70 $resArr = array();
71
72 $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.ref as label, ps.fk_product";
73 if ($is_eat_by_enabled) {
74 $sql .= ", pl.eatby";
75 }
76 if ($is_sell_by_enabled) {
77 $sql .= ", pl.sellby";
78 }
79 $sql .= " FROM ".$db->prefix()."product_batch as pb";
80 $sql .= " LEFT JOIN ".$db->prefix()."product_stock as ps on ps.rowid = pb.fk_product_stock";
81 $sql .= " LEFT JOIN ".$db->prefix()."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")";
82 if ($is_eat_by_enabled || $is_sell_by_enabled) {
83 $sql .= " LEFT JOIN ".$db->prefix()."product_lot as pl on ps.fk_product = pl.fk_product AND pb.batch = pl.batch";
84 }
85 $sql .= " WHERE ps.fk_product = ".((int) $product_id);
86 if ($warehouse_id > 0) {
87 $sql .= " AND fk_entrepot = '".((int) $warehouse_id)."'";
88 }
89 $sql .= " ORDER BY e.ref, pb.batch";
90
91 $resql = $db->query($sql);
92
93 if ($resql) {
94 while ($obj = $db->fetch_object($resql)) {
95 $eat_by_date_formatted = '';
96 if ($is_eat_by_enabled && !empty($obj->eatby)) {
97 $eat_by_date_formatted = dol_print_date($db->jdate($obj->eatby), 'day');
98 }
99 $sell_by_date_formatted = '';
100 if ($is_sell_by_enabled && !empty($obj->sellby)) {
101 $sell_by_date_formatted = dol_print_date($db->jdate($obj->sellby), 'day');
102 }
103
104 // set qty
105 if (!isset($resArr[$obj->batch])) {
106 $resArr[$obj->batch] = array(
107 'qty' => (float) $obj->qty,
108 );
109 } else {
110 $resArr[$obj->batch]['qty'] += $obj->qty;
111 }
112
113 // set eat-by date
114 if (!isset($resArr[$obj->batch]['eatbydate'])) {
115 $resArr[$obj->batch]['eatbydate'] = $eat_by_date_formatted;
116 }
117
118 // set sell-by date
119 if (!isset($resArr[$obj->batch]['sellbydate'])) {
120 $resArr[$obj->batch]['sellbydate'] = $sell_by_date_formatted;
121 }
122 }
123 }
124
125 echo json_encode($resArr);
126} elseif ($action == 'updateselectwarehousebybatch' && $permissiontowrite) {
127 $res = 0;
128
129 $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, e.ref, pb.qty";
130 $sql .= " FROM ".$db->prefix()."product_batch as pb";
131 $sql .= " JOIN ".$db->prefix()."product_stock as ps on ps.rowid = pb.fk_product_stock";
132 $sql .= " JOIN ".$db->prefix()."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")";
133 $sql .= " WHERE ps.fk_product = ".((int) $product_id);
134 if ($batch) {
135 $sql .= " AND pb.batch = '".$db->escape($batch)."'";
136 }
137 $sql .= " ORDER BY e.ref, pb.batch";
138
139 $resql = $db->query($sql);
140
141 if ($resql) {
142 if ($db->num_rows($resql) == 1) {
143 $obj = $db->fetch_object($resql);
144 $res = $obj->fk_entrepot;
145 }
146 }
147
148 echo json_encode($res);
149}
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.