modExpedition.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \defgroup expedition Module Shipping
  23. * \brief Module to manage product shipments
  24. *
  25. * \file htdocs/core/modules/modExpedition.class.php
  26. * \ingroup expedition
  27. * \brief Description and activation file for the module Expedition
  28. */
  29. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  30. /**
  31. * Class to describe and enable module Expedition
  32. */
  33. class modExpedition extends DolibarrModules
  34. {
  35. /**
  36. * Constructor. Define names, constants, directories, boxes, permissions
  37. *
  38. * @param DoliDB $db Database handler
  39. */
  40. public function __construct($db)
  41. {
  42. global $conf, $user;
  43. $this->db = $db;
  44. $this->numero = 80;
  45. $this->family = "crm";
  46. $this->module_position = '40';
  47. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  48. $this->name = preg_replace('/^mod/i', '', get_class($this));
  49. $this->description = "Gestion des expeditions";
  50. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  51. $this->version = 'dolibarr';
  52. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  53. $this->picto = "dolly";
  54. // Data directories to create when module is enabled
  55. $this->dirs = array("/expedition/temp",
  56. "/expedition/sending",
  57. "/expedition/sending/temp",
  58. "/expedition/receipt",
  59. "/expedition/receipt/temp",
  60. "/doctemplates/shipments",
  61. "/doctemplates/deliveries"
  62. );
  63. // Config pages
  64. $this->config_page_url = array("expedition.php");
  65. // Dependencies
  66. $this->depends = array("modCommande");
  67. $this->requiredby = array();
  68. $this->conflictwith = array();
  69. $this->langfiles = array('deliveries', 'sendings');
  70. // Constants
  71. $this->const = array();
  72. $r = 0;
  73. $this->const[$r][0] = "EXPEDITION_ADDON_PDF";
  74. $this->const[$r][1] = "chaine";
  75. $this->const[$r][2] = "espadon";
  76. $this->const[$r][3] = 'Nom du gestionnaire de generation des bons expeditions en PDF';
  77. $this->const[$r][4] = 0;
  78. $r++;
  79. $this->const[$r][0] = "EXPEDITION_ADDON_NUMBER";
  80. $this->const[$r][1] = "chaine";
  81. $this->const[$r][2] = "mod_expedition_safor";
  82. $this->const[$r][3] = 'Name for numbering manager for shipments';
  83. $this->const[$r][4] = 0;
  84. $r++;
  85. $this->const[$r][0] = "EXPEDITION_ADDON_PDF_ODT_PATH";
  86. $this->const[$r][1] = "chaine";
  87. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/shipments";
  88. $this->const[$r][3] = "";
  89. $this->const[$r][4] = 0;
  90. $r++;
  91. $this->const[$r][0] = "DELIVERY_ADDON_PDF";
  92. $this->const[$r][1] = "chaine";
  93. $this->const[$r][2] = "storm";
  94. $this->const[$r][3] = 'Nom du gestionnaire de generation des bons de reception en PDF';
  95. $this->const[$r][4] = 0;
  96. $r++;
  97. $this->const[$r][0] = "DELIVERY_ADDON_NUMBER";
  98. $this->const[$r][1] = "chaine";
  99. $this->const[$r][2] = "mod_delivery_jade";
  100. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des bons de reception';
  101. $this->const[$r][4] = 0;
  102. $r++;
  103. $this->const[$r][0] = "DELIVERY_ADDON_PDF_ODT_PATH";
  104. $this->const[$r][1] = "chaine";
  105. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/deliveries";
  106. $this->const[$r][3] = "";
  107. $this->const[$r][4] = 0;
  108. $r++;
  109. $this->const[$r][0] = "MAIN_SUBMODULE_EXPEDITION";
  110. $this->const[$r][1] = "chaine";
  111. $this->const[$r][2] = "1";
  112. $this->const[$r][3] = "Enable delivery receipts";
  113. $this->const[$r][4] = 0;
  114. $r++;
  115. // Boxes
  116. $this->boxes = array(
  117. 0=>array('file'=>'box_shipments.php', 'enabledbydefaulton'=>'Home'),
  118. );
  119. // Permissions
  120. $this->rights = array();
  121. $this->rights_class = 'expedition';
  122. $r = 0;
  123. $r++;
  124. $this->rights[$r][0] = 101;
  125. $this->rights[$r][1] = 'Lire les expeditions';
  126. $this->rights[$r][2] = 'r';
  127. $this->rights[$r][3] = 0;
  128. $this->rights[$r][4] = 'lire';
  129. $r++;
  130. $this->rights[$r][0] = 102;
  131. $this->rights[$r][1] = 'Creer modifier les expeditions';
  132. $this->rights[$r][2] = 'w';
  133. $this->rights[$r][3] = 0;
  134. $this->rights[$r][4] = 'creer';
  135. $r++;
  136. $this->rights[$r][0] = 104;
  137. $this->rights[$r][1] = 'Valider les expeditions';
  138. $this->rights[$r][2] = 'd';
  139. $this->rights[$r][3] = 0;
  140. $this->rights[$r][4] = 'shipping_advance';
  141. $this->rights[$r][5] = 'validate';
  142. $r++;
  143. $this->rights[$r][0] = 105; // id de la permission
  144. $this->rights[$r][1] = 'Envoyer les expeditions aux clients'; // libelle de la permission
  145. $this->rights[$r][2] = 'd'; // type de la permission (deprecie a ce jour)
  146. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  147. $this->rights[$r][4] = 'shipping_advance';
  148. $this->rights[$r][5] = 'send';
  149. $r++;
  150. $this->rights[$r][0] = 106;
  151. $this->rights[$r][1] = 'Exporter les expeditions';
  152. $this->rights[$r][2] = 'r';
  153. $this->rights[$r][3] = 0;
  154. $this->rights[$r][4] = 'shipment';
  155. $this->rights[$r][5] = 'export';
  156. $r++;
  157. $this->rights[$r][0] = 109;
  158. $this->rights[$r][1] = 'Supprimer les expeditions';
  159. $this->rights[$r][2] = 'd';
  160. $this->rights[$r][3] = 0;
  161. $this->rights[$r][4] = 'supprimer';
  162. $r++;
  163. $this->rights[$r][0] = 1101;
  164. $this->rights[$r][1] = 'Read delivery receipts';
  165. $this->rights[$r][2] = 'r';
  166. $this->rights[$r][3] = 0;
  167. $this->rights[$r][4] = 'delivery';
  168. $this->rights[$r][5] = 'lire';
  169. $r++;
  170. $this->rights[$r][0] = 1102;
  171. $this->rights[$r][1] = 'Create/modify delivery receipts';
  172. $this->rights[$r][2] = 'w';
  173. $this->rights[$r][3] = 0;
  174. $this->rights[$r][4] = 'delivery';
  175. $this->rights[$r][5] = 'creer';
  176. $r++;
  177. $this->rights[$r][0] = 1104;
  178. $this->rights[$r][1] = 'Validate delivery receipts';
  179. $this->rights[$r][2] = 'd';
  180. $this->rights[$r][3] = 0;
  181. $this->rights[$r][4] = 'delivery_advance';
  182. $this->rights[$r][5] = 'validate';
  183. $r++;
  184. $this->rights[$r][0] = 1109;
  185. $this->rights[$r][1] = 'Delete delivery receipts';
  186. $this->rights[$r][2] = 'd';
  187. $this->rights[$r][3] = 0;
  188. $this->rights[$r][4] = 'delivery';
  189. $this->rights[$r][5] = 'supprimer';
  190. // Menus
  191. //-------
  192. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  193. // Exports
  194. //--------
  195. $r = 0;
  196. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  197. $shipment = new Commande($this->db);
  198. $contact_arrays = $shipment->liste_type_contact('external', '', 0, 0, '');
  199. if (is_array($contact_arrays) && count($contact_arrays) > 0) {
  200. $idcontacts = join(',', array_keys($shipment->liste_type_contact('external', '', 0, 0, '')));
  201. } else {
  202. $idcontacts = 0;
  203. }
  204. $r++;
  205. $this->export_code[$r] = $this->rights_class.'_'.$r;
  206. $this->export_label[$r] = 'Shipments'; // Translation key (used only if key ExportDataset_xxx_z not found)
  207. $this->export_permission[$r] = array(array("expedition", "shipment", "export"));
  208. $this->export_fields_array[$r] = array(
  209. 's.rowid'=>"IdCompany", 's.nom'=>'ThirdParty', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'd.nom'=>'State', 'co.label'=>'Country',
  210. 'co.code'=>'CountryCode', 's.phone'=>'Phone', 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.idprof5'=>'ProfId5',
  211. '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",
  212. 'c.date_delivery'=>"DateDeliveryPlanned", 'c.tracking_number'=>"TrackingNumber", 'c.height'=>"Height", 'c.width'=>"Width", 'c.size'=>"Depth",
  213. 'c.size_units'=>'SizeUnits', 'c.weight'=>"Weight", 'c.weight_units'=>"WeightUnits", 'c.fk_statut'=>'Status', 'c.note_public'=>"NotePublic",
  214. 'ed.rowid'=>'LineId', 'cd.description'=>'Description', 'ed.qty'=>"Qty", 'p.rowid'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
  215. 'p.weight'=>'ProductWeight', 'p.weight_units'=>'WeightUnits', 'p.volume'=>'ProductVolume', 'p.volume_units'=>'VolumeUnits'
  216. );
  217. if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
  218. $this->export_fields_array[$r] += array('sp.rowid'=>'IdContact', 'sp.lastname'=>'Lastname', 'sp.firstname'=>'Firstname', 'sp.note_public'=>'NotePublic');
  219. }
  220. //$this->export_TypeFields_array[$r]=array(
  221. // 's.rowid'=>"Numeric",'s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','co.label'=>'List:c_country:label:label',
  222. // '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",
  223. // 'c.date_creation'=>"Date",'c.date_commande'=>"Date",'c.amount_ht'=>"Numeric",'c.remise_percent'=>"Numeric",'c.total_ht'=>"Numeric",
  224. // 'c.total_ttc'=>"Numeric",'c.facture'=>"Boolean",'c.fk_statut'=>'Status','c.note_public'=>"Text",'c.date_livraison'=>'Date','ed.qty'=>"Text"
  225. //);
  226. $this->export_TypeFields_array[$r] = array(
  227. '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',
  228. '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",
  229. 'c.date_delivery'=>"Date", 'c.tracking_number'=>"Numeric", 'c.height'=>"Numeric", 'c.width'=>"Numeric", 'c.weight'=>"Numeric", 'c.fk_statut'=>'Status',
  230. 'c.note_public'=>"Text", 'ed.qty'=>"Numeric", 'd.nom'=>'Text'
  231. );
  232. $this->export_entities_array[$r] = array(
  233. 's.rowid'=>"company", 's.nom'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'd.nom'=>'company', 'co.label'=>'company',
  234. 'co.code'=>'company', 's.fk_pays'=>'company', 's.phone'=>'company', 's.siren'=>'company', 's.ape'=>'company', 's.siret'=>'company', 's.idprof4'=>'company',
  235. 's.idprof5'=>'company', 's.idprof6'=>'company', 'c.rowid'=>"shipment", 'c.ref'=>"shipment", 'c.ref_customer'=>"shipment", 'c.fk_soc'=>"shipment",
  236. 'c.date_creation'=>"shipment", 'c.date_valid'=>"shipment", 'c.date_delivery'=>"shipment", 'c.tracking_number'=>'shipment', 'c.height'=>"shipment", 'c.width'=>"shipment",
  237. 'c.size'=>'shipment', 'c.size_units'=>'shipment', 'c.weight'=>"shipment", 'c.weight_units'=>'shipment', 'c.fk_statut'=>"shipment", 'c.note_public'=>"shipment",
  238. 'ed.rowid'=>'shipment_line', 'cd.description'=>'shipment_line', 'ed.qty'=>"shipment_line", 'p.rowid'=>'product', 'p.ref'=>'product', 'p.label'=>'product',
  239. 'p.weight'=>'product', 'p.weight_units'=>'product', 'p.volume'=>'product', 'p.volume_units'=>'product'
  240. );
  241. if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
  242. $this->export_entities_array[$r] += array('sp.rowid'=>'contact', 'sp.lastname'=>'contact', 'sp.firstname'=>'contact', 'sp.note_public'=>'contact');
  243. }
  244. $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
  245. if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
  246. $keyforselect = 'socpeople';
  247. $keyforelement = 'contact';
  248. $keyforaliasextra = 'extra3';
  249. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  250. }
  251. $keyforselect = 'expedition';
  252. $keyforelement = 'shipment';
  253. $keyforaliasextra = 'extra';
  254. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  255. $keyforselect = 'expeditiondet';
  256. $keyforelement = 'shipment_line';
  257. $keyforaliasextra = 'extra2';
  258. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  259. $keyforselect = 'product';
  260. $keyforelement = 'product';
  261. $keyforaliasextra = 'extraprod';
  262. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  263. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  264. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'expedition as c';
  265. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expedition_extrafields as extra ON c.rowid = extra.fk_object,';
  266. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'societe as s';
  267. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  268. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  269. }
  270. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
  271. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON s.fk_pays = co.rowid,';
  272. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'expeditiondet as ed';
  273. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expeditiondet_extrafields as extra2 ON ed.rowid = extra2.fk_object';
  274. $this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'commandedet as cd';
  275. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on cd.fk_product = p.rowid';
  276. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extraprod ON p.rowid = extraprod.fk_object';
  277. if ($idcontacts && getDolGlobalString('SHIPMENT_ADD_CONTACTS_IN_EXPORT')) {
  278. $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).')';
  279. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople as sp ON sp.rowid = ee.fk_socpeople';
  280. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople_extrafields as extra3 ON sp.rowid = extra3.fk_object';
  281. }
  282. $this->export_sql_end[$r] .= ' WHERE c.fk_soc = s.rowid AND c.rowid = ed.fk_expedition AND ed.fk_origin_line = cd.rowid';
  283. $this->export_sql_end[$r] .= ' AND c.entity IN ('.getEntity('expedition').')';
  284. if (!empty($user) && !$user->hasRight('societe', 'client', 'voir')) {
  285. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.(empty($user) ? 0 : $user->id);
  286. }
  287. }
  288. /**
  289. * Function called when module is enabled.
  290. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  291. * It also creates data directories
  292. *
  293. * @param string $options Options when enabling module ('', 'noboxes')
  294. * @return int 1 if OK, 0 if KO
  295. */
  296. public function init($options = '')
  297. {
  298. global $conf, $langs;
  299. // Permissions
  300. $this->remove($options);
  301. //ODT template
  302. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/shipments/template_shipment.odt';
  303. $dirodt = DOL_DATA_ROOT.'/doctemplates/shipments';
  304. $dest = $dirodt.'/template_shipment.odt';
  305. if (file_exists($src) && !file_exists($dest)) {
  306. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  307. dol_mkdir($dirodt);
  308. $result = dol_copy($src, $dest, 0, 0);
  309. if ($result < 0) {
  310. $langs->load("errors");
  311. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  312. return 0;
  313. }
  314. }
  315. $sql = array();
  316. $sql = array(
  317. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[0][2])."' AND type = 'shipping' AND entity = ".((int) $conf->entity),
  318. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[0][2])."', 'shipping', ".((int) $conf->entity).")",
  319. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[3][2])."' AND type = 'delivery' AND entity = ".((int) $conf->entity),
  320. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[3][2])."', 'delivery', ".((int) $conf->entity).")",
  321. //"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),
  322. //"INSERT INTO ".MAIN_DB_PREFIX."const (name, value, entity) VALUES ('STOCK_CALCULATE_ON_SHIPMENT_CLOSE', 1, ".((int) $conf->entity).")"
  323. );
  324. return $this->_init($sql, $options);
  325. }
  326. }