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