dolibarr  19.0.0-dev
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 
25 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
26 
33 function dolDispatchToDo($order_id)
34 {
35  global $db;
36 
37  $dispatched = array();
38  $ordered = array();
39 
40  // Count nb of quantity dispatched per product
41  $sql = 'SELECT fk_product, SUM(qty) FROM '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch';
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;
49  }
50  }
51 
52  // Count nb of quantity to dispatch per product
53  $sql = 'SELECT fk_product, SUM(qty) FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet';
54  $sql .= ' WHERE fk_commande = '.((int) $order_id);
55  $sql .= ' AND fk_product > 0';
56  if (empty($conf->global->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;
65  }
66  }
67 
68  $todispatch = 0;
69  foreach ($ordered as $key => $val) {
70  if ($ordered[$key] > $dispatched[$key]) {
71  $todispatch++;
72  }
73  }
74 
75  return ($todispatch ? true : false);
76  //return true;
77 }
78 
84 function dispatchedOrders()
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 
114 function ordered($product_id)
115 {
116  global $db, $langs, $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) {
136  $obj = $db->fetch_array($resql);
137  return $obj['qty']; //. ' ' . img_picto('','tick');
138  } else {
139  return null; //img_picto('', 'stcomm-1');
140  }
141  } else {
142  $error = $db->lasterror();
143  dol_print_error($db);
144 
145  return $langs->trans('error');
146  }
147 }
148 
155 function getProducts($order_id)
156 {
157  global $db;
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('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
ordered($product_id)
ordered
dolDispatchToDo($order_id)
Check if there is still some dispatching of stock to do.
getProducts($order_id)
getProducts
dispatchedOrders()
dispatchedOrders