dolibarr 24.0.0-beta
replenishment.lib.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.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
26require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
27
34function dolDispatchToDo($order_id)
35{
36 global $db, $conf;
37
38 $dispatched = array();
39 $ordered = array();
40
41 // Count nb of quantity dispatched per product
42 $sql = 'SELECT fk_product, SUM(qty) as qtydispatched FROM '.MAIN_DB_PREFIX.'receptiondet_batch';
43 $sql .= " WHERE fk_element = ".((int) $order_id)." AND element_type = 'supplier_order'";
44 $sql .= ' GROUP BY fk_product';
45 $sql .= ' ORDER by fk_product';
46 $resql = $db->query($sql);
47 if ($resql && $db->num_rows($resql)) {
48 while ($obj = $db->fetch_object($resql)) {
49 $dispatched[$obj->fk_product] = $obj->qtydispatched;
50 }
51 }
52
53 // Count nb of quantity to dispatch per product
54 $sql = 'SELECT fk_product, SUM(qty) as qtyordered FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet';
55 $sql .= ' WHERE fk_commande = '.((int) $order_id);
56 $sql .= ' AND fk_product > 0';
57 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
58 $sql .= ' AND product_type = 0';
59 }
60 $sql .= ' GROUP BY fk_product';
61 $sql .= ' ORDER by fk_product';
62 $resql = $db->query($sql);
63 if ($resql && $db->num_rows($resql)) {
64 while ($obj = $db->fetch_object($resql)) {
65 $ordered[$obj->fk_product] = $obj->qtyordered;
66 }
67 }
68
69 $todispatch = 0;
70 foreach ($ordered as $key => $val) {
71 if ((empty($ordered[$key]) ? 0 : $ordered[$key]) > (empty($dispatched[$key]) ? 0 : $dispatched[$key])) {
72 $todispatch++;
73 }
74 }
75
76 return ($todispatch ? true : false);
77 //return true;
78}
79
86{
87 global $db;
88
89 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commande_fournisseur';
90 $resql = $db->query($sql);
91 $resarray = array();
92 if ($resql && $db->num_rows($resql) > 0) {
93 while ($obj = $db->fetch_object($resql)) {
94 if (!dolDispatchToDo($obj->rowid)) {
95 $resarray[] = $obj->rowid;
96 }
97 }
98 }
99
100 if (count($resarray)) {
101 $res = '('.implode(',', $resarray).')';
102 } else {
103 //hack to make sure ordered SQL request won't syntax error
104 $res = '(0)';
105 }
106 return $res;
107}
108
115function ordered($product_id)
116{
117 global $db, $conf;
118
119 $sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) as qty FROM';
120 $sql .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as cfd ';
121 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur as cf';
122 $sql .= ' ON cfd.fk_commande = cf.rowid WHERE';
123 if (getDolGlobalInt("STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER")) {
124 $sql .= ' cf.fk_statut < 3';
125 } elseif (getDolGlobalInt("STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER")) {
126 $sql .= ' cf.fk_statut < 6 AND cf.rowid NOT IN '.dispatchedOrders();
127 } else {
128 $sql .= ' cf.fk_statut < 5';
129 }
130 $sql .= ' AND cfd.fk_product = '.((int) $product_id);
131 $sql .= ' GROUP BY cfd.fk_product';
132
133 $resql = $db->query($sql);
134 if ($resql) {
135 $exists = $db->num_rows($resql);
136 if ($exists && $obj = $db->fetch_array($resql)) {
137 return $obj['qty']; //. ' ' . img_picto('','tick');
138 } else {
139 return null; //img_picto('', 'stcomm-1');
140 }
141 } else {
143
144 return 'Error '.$db->lasterror();
145 }
146}
147
154function getProducts($order_id)
155{
156 global $db;
157
158 $order = new CommandeFournisseur($db);
159 $f = $order->fetch($order_id);
160 $products = array();
161 if ($f) {
162 foreach ($order->lines as $line) {
163 if (!in_array($line->fk_product, $products)) {
164 $products[] = $line->fk_product;
165 }
166 }
167 }
168 return $products;
169}
Class to manage predefined suppliers products.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
ordered($product_id)
ordered
dolDispatchToDo($order_id)
Check if there is still some dispatching of stock to do.
getProducts($order_id)
getProducts
dispatchedOrders()
dispatchedOrders