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