/
replenishment.lib.php
168 lines (151 loc) · 4.61 KB
/
replenishment.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/product/stock/lib/replenishment.lib.php
* \ingroup produit
* \brief Contains functions used in replenish.php and replenishorders.php
*/
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
/**
* Check if there is still some dispatching of stock to do.
*
* @param int $order_id Id of order to check
* @return boolean True = There is some dispatching to do, False = All dispatching is done (may be we receive more) or is not required
*/
function dolDispatchToDo($order_id)
{
global $db, $conf;
$dispatched = array();
$ordered = array();
// Count nb of quantity dispatched per product
$sql = 'SELECT fk_product, SUM(qty) as qtydispatched FROM '.MAIN_DB_PREFIX.'receptiondet_batch';
$sql .= ' WHERE fk_commande = '.((int) $order_id);
$sql .= ' GROUP BY fk_product';
$sql .= ' ORDER by fk_product';
$resql = $db->query($sql);
if ($resql && $db->num_rows($resql)) {
while ($obj = $db->fetch_object($resql)) {
$dispatched[$obj->fk_product] = $obj->qtydispatched;
}
}
// Count nb of quantity to dispatch per product
$sql = 'SELECT fk_product, SUM(qty) as qtyordered FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet';
$sql .= ' WHERE fk_commande = '.((int) $order_id);
$sql .= ' AND fk_product > 0';
if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
$sql .= ' AND product_type = 0';
}
$sql .= ' GROUP BY fk_product';
$sql .= ' ORDER by fk_product';
$resql = $db->query($sql);
if ($resql && $db->num_rows($resql)) {
while ($obj = $db->fetch_object($resql)) {
$ordered[$obj->fk_product] = $obj->qtyordered;
}
}
$todispatch = 0;
foreach ($ordered as $key => $val) {
if ((empty($ordered[$key]) ? 0 : $ordered[$key]) > (empty($dispatched[$key]) ? 0 : $dispatched[$key])) {
$todispatch++;
}
}
return ($todispatch ? true : false);
//return true;
}
/**
* dispatchedOrders
*
* @return string Array of id of orders with all dispatching already done or not required
*/
function dispatchedOrders()
{
global $db;
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commande_fournisseur';
$resql = $db->query($sql);
$resarray = array();
if ($resql && $db->num_rows($resql) > 0) {
while ($obj = $db->fetch_object($resql)) {
if (!dolDispatchToDo($obj->rowid)) {
$resarray[] = $obj->rowid;
}
}
}
if (count($resarray)) {
$res = '('.implode(',', $resarray).')';
} else {
//hack to make sure ordered SQL request won't syntax error
$res = '(0)';
}
return $res;
}
/**
* ordered
*
* @param int $product_id Product id
* @return string|null
*/
function ordered($product_id)
{
global $db, $conf;
$sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) as qty FROM';
$sql .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as cfd ';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur as cf';
$sql .= ' ON cfd.fk_commande = cf.rowid WHERE';
if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) {
$sql .= ' cf.fk_statut < 3';
} elseif ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) {
$sql .= ' cf.fk_statut < 6 AND cf.rowid NOT IN '.dispatchedOrders();
} else {
$sql .= ' cf.fk_statut < 5';
}
$sql .= ' AND cfd.fk_product = '.((int) $product_id);
$sql .= ' GROUP BY cfd.fk_product';
$resql = $db->query($sql);
if ($resql) {
$exists = $db->num_rows($resql);
if ($exists && $obj = $db->fetch_array($resql)) {
return $obj['qty']; //. ' ' . img_picto('','tick');
} else {
return null; //img_picto('', 'stcomm-1');
}
} else {
dol_print_error($db);
return 'Error '.$db->lasterror();
}
}
/**
* getProducts
*
* @param int $order_id Order id
* @return array|integer[]
*/
function getProducts($order_id)
{
global $db;
$order = new CommandeFournisseur($db);
$f = $order->fetch($order_id);
$products = array();
if ($f) {
foreach ($order->lines as $line) {
if (!in_array($line->fk_product, $products)) {
$products[] = $line->fk_product;
}
}
}
return $products;
}