dolibarr  13.0.2
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 = '.$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  {
48  while ($obj = $db->fetch_object($resql))
49  $dispatched[$obj->fk_product] = $obj;
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 = '.$order_id;
55  $sql .= ' AND fk_product > 0';
56  if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) $sql .= ' AND product_type = 0';
57  $sql .= ' GROUP BY fk_product';
58  $sql .= ' ORDER by fk_product';
59  $resql = $db->query($sql);
60  if ($resql && $db->num_rows($resql))
61  {
62  while ($obj = $db->fetch_object($resql))
63  $ordered[$obj->fk_product] = $obj;
64  }
65 
66  $todispatch = 0;
67  foreach ($ordered as $key => $val)
68  {
69  if ($ordered[$key] > $dispatched[$key]) $todispatch++;
70  }
71 
72  return ($todispatch ? true : false);
73  //return true;
74 }
75 
81 function dispatchedOrders()
82 {
83  global $db;
84 
85  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commande_fournisseur';
86  $resql = $db->query($sql);
87  $resarray = array();
88  if ($resql && $db->num_rows($resql) > 0)
89  {
90  while ($obj = $db->fetch_object($resql))
91  {
92  if (!dolDispatchToDo($obj->rowid))
93  {
94  $resarray[] = $obj->rowid;
95  }
96  }
97  }
98 
99  if (count($resarray))
100  {
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 
115 function ordered($product_id)
116 {
117  global $db, $langs, $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 ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) {
124  $sql .= ' cf.fk_statut < 3';
125  } elseif ($conf->global->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 = '.$product_id;
131  $sql .= ' GROUP BY cfd.fk_product';
132 
133  $resql = $db->query($sql);
134  if ($resql)
135  {
136  $exists = $db->num_rows($resql);
137  if ($exists)
138  {
139  $obj = $db->fetch_array($resql);
140  return $obj['qty']; //. ' ' . img_picto('','tick');
141  } else {
142  return null; //img_picto('', 'stcomm-1');
143  }
144  } else {
145  $error = $db->lasterror();
146  dol_print_error($db);
147 
148  return $langs->trans('error');
149  }
150 }
151 
158 function getProducts($order_id)
159 {
160  global $db;
161  $order = new CommandeFournisseur($db);
162  $f = $order->fetch($order_id);
163  $products = array();
164  if ($f) {
165  foreach ($order->lines as $line) {
166  if (!in_array($line->fk_product, $products)) {
167  $products[] = $line->fk_product;
168  }
169  }
170  }
171  return $products;
172 }
ordered($product_id)
ordered
dolDispatchToDo($order_id)
Check if there is still some dispatching of stock to do.
dispatchedOrders()
dispatchedOrders
Class to manage predefined suppliers products.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getProducts($order_id)
getProducts