dolibarr 24.0.0-beta
modExpedition.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2013 Philippe Grand <philippe.grand@atoo-net.com>
7 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
32include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
33
34
39{
45 public function __construct($db)
46 {
47 global $conf, $user; // $conf is required by /core/extrafieldsinexport.inc.php
48
49 $this->db = $db;
50 $this->numero = 80;
51
52 $this->family = "crm";
53 $this->module_position = '40';
54 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
55 $this->name = preg_replace('/^mod/i', '', get_class($this));
56 $this->description = "Gestion des expeditions";
57
58 // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
59 $this->version = 'dolibarr';
60
61 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
62 $this->picto = "dolly";
63
64 // Data directories to create when module is enabled
65 $this->dirs = array(
66 "/expedition/temp",
67 "/expedition/sending",
68 "/expedition/sending/temp",
69 "/expedition/receipt",
70 "/expedition/receipt/temp",
71 "/doctemplates/shipments",
72 "/doctemplates/deliveries",
73 );
74
75 // Config pages
76 $this->config_page_url = array("expedition.php");
77
78 // Dependencies
79 $this->depends = array("modCommande");
80 $this->requiredby = array();
81 $this->conflictwith = array();
82 $this->langfiles = array('sendings');
83
84 // Constants
85 $this->const = [
86 [
87 "EXPEDITION_ADDON_PDF",
88 "chaine",
89 "espadon",
90 'Nom du gestionnaire de generation des bons expeditions en PDF',
91 0,
92 ],
93 [
94 "EXPEDITION_ADDON_NUMBER",
95 "chaine",
96 "mod_expedition_safor",
97 'Name for numbering manager for shipments',
98 0,
99 ],
100 [
101 "EXPEDITION_ADDON_PDF_ODT_PATH",
102 "chaine",
103 "DOL_DATA_ROOT".($conf->entity > 1 ? '/'.$conf->entity : '')."/doctemplates/shipments",
104 "",
105 0,
106 ],
107 [
108 "DELIVERY_ADDON_PDF",
109 "chaine",
110 "storm",
111 'Nom du gestionnaire de generation des bons de reception en PDF',
112 0,
113 ],
114 [
115 "DELIVERY_ADDON_NUMBER",
116 "chaine",
117 "mod_delivery_jade",
118 'Nom du gestionnaire de numerotation des bons de reception',
119 0,
120 ],
121 [
122 "DELIVERY_ADDON_PDF_ODT_PATH",
123 "chaine",
124 "DOL_DATA_ROOT".($conf->entity > 1 ? '/'.$conf->entity : '')."/doctemplates/deliveries",
125 "",
126 0,
127 ],
128 [
129 "MAIN_SUBMODULE_EXPEDITION",
130 "chaine",
131 "1",
132 "Enable delivery receipts",
133 0,
134 ],
135 ];
136
137 // Boxes
138 $this->boxes = array(
139 0 => array('file'=>'box_shipments.php', 'enabledbydefaulton'=>'Home'),
140 );
141
142 // Permissions
143 $this->rights_class = 'expedition';
144 $this->rights = array();
145 $r = 0;
146
147 $r++;
148 $this->rights[$r][0] = 101;
149 $this->rights[$r][1] = 'Read shipments';
150 $this->rights[$r][2] = 'r';
151 $this->rights[$r][3] = 0;
152 $this->rights[$r][4] = 'lire';
153
154 $r++;
155 $this->rights[$r][0] = 102;
156 $this->rights[$r][1] = 'Create/modify shipments';
157 $this->rights[$r][2] = 'w';
158 $this->rights[$r][3] = 0;
159 $this->rights[$r][4] = 'creer';
160
161 $r++;
162 $this->rights[$r][0] = 104;
163 $this->rights[$r][1] = 'Validate shipments';
164 $this->rights[$r][2] = 'd';
165 $this->rights[$r][3] = 0;
166 $this->rights[$r][4] = 'shipping_advance';
167 $this->rights[$r][5] = 'validate';
168
169 $r++;
170 $this->rights[$r][0] = 105; // id de la permission
171 $this->rights[$r][1] = 'Send shipments by email to customers'; // libelle de la permission
172 $this->rights[$r][2] = 'd'; // type de la permission (deprecated)
173 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
174 $this->rights[$r][4] = 'shipping_advance';
175 $this->rights[$r][5] = 'send';
176
177 $r++;
178 $this->rights[$r][0] = 106;
179 $this->rights[$r][1] = 'Export shipments';
180 $this->rights[$r][2] = 'r';
181 $this->rights[$r][3] = 0;
182 $this->rights[$r][4] = 'shipment';
183 $this->rights[$r][5] = 'export';
184
185 $r++;
186 $this->rights[$r][0] = 109;
187 $this->rights[$r][1] = 'Delete shipments';
188 $this->rights[$r][2] = 'd';
189 $this->rights[$r][3] = 0;
190 $this->rights[$r][4] = 'supprimer';
191
192 $r++;
193 $this->rights[$r][0] = 1101;
194 $this->rights[$r][1] = 'Read delivery receipts';
195 $this->rights[$r][2] = 'r';
196 $this->rights[$r][3] = 0;
197 $this->rights[$r][4] = 'delivery';
198 $this->rights[$r][5] = 'lire';
199
200 $r++;
201 $this->rights[$r][0] = 1102;
202 $this->rights[$r][1] = 'Create/modify delivery receipts';
203 $this->rights[$r][2] = 'w';
204 $this->rights[$r][3] = 0;
205 $this->rights[$r][4] = 'delivery';
206 $this->rights[$r][5] = 'creer';
207
208 $r++;
209 $this->rights[$r][0] = 1104;
210 $this->rights[$r][1] = 'Validate delivery receipts';
211 $this->rights[$r][2] = 'd';
212 $this->rights[$r][3] = 0;
213 $this->rights[$r][4] = 'delivery_advance';
214 $this->rights[$r][5] = 'validate';
215
216 $r++;
217 $this->rights[$r][0] = 1109;
218 $this->rights[$r][1] = 'Delete delivery receipts';
219 $this->rights[$r][2] = 'd';
220 $this->rights[$r][3] = 0;
221 $this->rights[$r][4] = 'delivery';
222 $this->rights[$r][5] = 'supprimer';
223
224
225 // Menus
226 //-------
227 $this->menu = 1; // This module add menu entries. They are coded into menu manager.
228
229
230 // Exports
231 //--------
232 $r = 0;
233
234 include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
235 $shipment = new Commande($this->db);
236 $contact_arrays = $shipment->liste_type_contact('external', '', 0, 0, '');
237 if (is_array($contact_arrays) && count($contact_arrays) > 0) {
238 $idcontacts = implode(',', array_keys($shipment->liste_type_contact('external', '', 0, 0, '')));
239 } else {
240 $idcontacts = 0;
241 }
242
243
244 $r++;
245 $this->export_code[$r] = $this->rights_class.'_'.$r;
246 $this->export_label[$r] = 'Shipments'; // Translation key (used only if key ExportDataset_xxx_z not found)
247 $this->export_permission[$r] = array(array("expedition", "shipment", "export"));
248 $this->export_fields_array[$r] = array(
249 's.rowid'=>"IdCompany", 's.nom'=>'ThirdParty', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'd.nom'=>'State', 'co.label'=>'Country',
250 'co.code'=>'CountryCode', 's.phone'=>'Phone', 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.idprof5'=>'ProfId5',
251 's.idprof6'=>'ProfId6', 'c.rowid'=>"Id", 'c.ref'=>"Ref", 'c.ref_customer'=>"RefCustomer", 'c.fk_soc'=>"IdCompany", 'c.date_creation'=>"DateCreation", 'c.date_valid'=>"DateValidation",
252 'c.date_delivery'=>"DateDeliveryPlanned", 'c.tracking_number'=>"TrackingNumber", 'c.height'=>"Height", 'c.width'=>"Width", 'c.size'=>"Depth",
253 'c.size_units'=>'SizeUnits', 'c.weight'=>"Weight", 'c.weight_units'=>"WeightUnits", 'c.fk_statut'=>'Status', 'c.note_public'=>"NotePublic",
254 'ed.rowid'=>'LineId', 'cd.description'=>'Description', 'ed.qty'=>"Qty", 'p.rowid'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
255 'p.weight'=>'ProductWeight', 'p.weight_units'=>'WeightUnits', 'p.volume'=>'ProductVolume', 'p.volume_units'=>'VolumeUnits'
256 );
257 if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
258 $this->export_fields_array[$r] += array('sp.rowid'=>'IdContact', 'sp.lastname'=>'Lastname', 'sp.firstname'=>'Firstname', 'sp.note_public'=>'NotePublic');
259 }
260 //$this->export_TypeFields_array[$r]=array(
261 // 's.rowid'=>"Numeric",'s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','co.label'=>'List:c_country:label:label',
262 // 'co.code'=>'Text','s.phone'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','c.ref'=>"Text",'c.ref_client'=>"Text",
263 // 'c.date_creation'=>"Date",'c.date_commande'=>"Date",'c.amount_ht'=>"Numeric",'c.remise_percent'=>"Numeric",'c.total_ht'=>"Numeric",
264 // 'c.total_ttc'=>"Numeric",'c.facture'=>"Boolean",'c.fk_statut'=>'Status','c.note_public'=>"Text",'c.date_livraison'=>'Date','ed.qty'=>"Text"
265 //);
266 $this->export_TypeFields_array[$r] = array(
267 's.nom'=>'Text', 's.address'=>'Text', 's.zip'=>'Text', 's.town'=>'Text', 'co.label'=>'List:c_country:label:label', 'co.code'=>'Text', 's.phone'=>'Text',
268 's.siren'=>'Text', 's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 'c.ref'=>"Text", 'c.ref_customer'=>"Text", 'c.date_creation'=>"Date", 'c.date_valid'=>"Date",
269 'c.date_delivery'=>"Date", 'c.tracking_number'=>"Numeric", 'c.height'=>"Numeric", 'c.width'=>"Numeric", 'c.weight'=>"Numeric", 'c.fk_statut'=>'Status',
270 'c.note_public'=>"Text", 'ed.qty'=>"Numeric", 'd.nom'=>'Text'
271 );
272 $this->export_entities_array[$r] = array(
273 's.rowid'=>"company", 's.nom'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'd.nom'=>'company', 'co.label'=>'company',
274 'co.code'=>'company', 's.fk_pays'=>'company', 's.phone'=>'company', 's.siren'=>'company', 's.ape'=>'company', 's.siret'=>'company', 's.idprof4'=>'company',
275 's.idprof5'=>'company', 's.idprof6'=>'company', 'c.rowid'=>"shipment", 'c.ref'=>"shipment", 'c.ref_customer'=>"shipment", 'c.fk_soc'=>"shipment",
276 'c.date_creation'=>"shipment", 'c.date_valid'=>"shipment", 'c.date_delivery'=>"shipment", 'c.tracking_number'=>'shipment', 'c.height'=>"shipment", 'c.width'=>"shipment",
277 'c.size'=>'shipment', 'c.size_units'=>'shipment', 'c.weight'=>"shipment", 'c.weight_units'=>'shipment', 'c.fk_statut'=>"shipment", 'c.note_public'=>"shipment",
278 'ed.rowid'=>'shipment_line', 'cd.description'=>'shipment_line', 'ed.qty'=>"shipment_line", 'p.rowid'=>'product', 'p.ref'=>'product', 'p.label'=>'product',
279 'p.weight'=>'product', 'p.weight_units'=>'product', 'p.volume'=>'product', 'p.volume_units'=>'product'
280 );
281 if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
282 $this->export_entities_array[$r] += array('sp.rowid'=>'contact', 'sp.lastname'=>'contact', 'sp.firstname'=>'contact', 'sp.note_public'=>'contact');
283 }
284 $this->export_dependencies_array[$r] = array('shipment_line'=>'ed.rowid', 'product'=>'ed.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
285 if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
286 $keyforselect = 'socpeople';
287 $keyforelement = 'contact';
288 $keyforaliasextra = 'extra3';
289 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
290 }
291 $keyforselect = 'expedition';
292 $keyforelement = 'shipment';
293 $keyforaliasextra = 'extra';
294 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
295 $keyforselect = 'expeditiondet';
296 $keyforelement = 'shipment_line';
297 $keyforaliasextra = 'extra2';
298 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
299 $keyforselect = 'product';
300 $keyforelement = 'product';
301 $keyforaliasextra = 'extraprod';
302 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
303
304 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
305 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'expedition as c';
306 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expedition_extrafields as extra ON c.rowid = extra.fk_object,';
307 $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'societe as s';
308 if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
309 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
310 }
311 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
312 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON s.fk_pays = co.rowid,';
313 $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'expeditiondet as ed';
314 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expeditiondet_extrafields as extra2 ON ed.rowid = extra2.fk_object';
315 $this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'commandedet as cd';
316 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on cd.fk_product = p.rowid';
317 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extraprod ON p.rowid = extraprod.fk_object';
318 if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
319 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'element_contact as ee ON ee.element_id = cd.fk_commande AND ee.fk_c_type_contact IN ('.$this->db->sanitize($idcontacts).')';
320 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople as sp ON sp.rowid = ee.fk_socpeople';
321 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople_extrafields as extra3 ON sp.rowid = extra3.fk_object';
322 }
323 $this->export_sql_end[$r] .= ' WHERE c.fk_soc = s.rowid AND c.rowid = ed.fk_expedition AND ed.fk_elementdet = cd.rowid';
324 $this->export_sql_end[$r] .= ' AND c.entity IN ('.getEntity('expedition').')';
325 if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
326 $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
327 }
328 }
329
330
339 public function init($options = '')
340 {
341 global $conf, $langs;
342
343 // Permissions
344 $this->remove($options);
345
346 //ODT template
347 $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/shipments/template_shipment.odt';
348 $dirodt = DOL_DATA_ROOT.($conf->entity > 1 ? '/'.$conf->entity : '').'/doctemplates/shipments';
349 $dest = $dirodt.'/template_shipment.odt';
350
351 if (file_exists($src) && !file_exists($dest)) {
352 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
353 dol_mkdir($dirodt);
354 $result = dol_copy($src, $dest, '0', 0);
355 if ($result < 0) {
356 $langs->load("errors");
357 $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
358 return 0;
359 }
360 }
361
362 $sql = array(
363 "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[0][2])."' AND type = 'shipping' AND entity = ".((int) $conf->entity),
364 "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[0][2])."', 'shipping', ".((int) $conf->entity).")",
365 "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[3][2])."' AND type = 'delivery' AND entity = ".((int) $conf->entity),
366 "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[3][2])."', 'delivery', ".((int) $conf->entity).")",
367 //"DELETE FROM ".MAIN_DB_PREFIX."const WHERE name IN ('STOCK_CALCULATE_ON_BILL', 'STOCK_CALCULATE_ON_VALIDATE_ORDER', 'STOCK_CALCULATE_ON_SHIPMENT', 'STOCK_CALCULATE_ON_SHIPMENT_CLOSE') AND entity = ".((int) $conf->entity),
368 //"INSERT INTO ".MAIN_DB_PREFIX."const (name, value, entity) VALUES ('STOCK_CALCULATE_ON_SHIPMENT_CLOSE', 1, ".((int) $conf->entity).")"
369 );
370
371 return $this->_init($sql, $options);
372 }
373}
Class to manage customers orders.
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
Class to describe and enable module Expedition.
init($options='')
Function called when module is enabled.
__construct($db)
Constructor.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133