modCommande.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  7. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  8. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \defgroup commande Module orders
  25. * \brief Module pour gerer le suivi des commandes
  26. * \file htdocs/core/modules/modCommande.class.php
  27. * \ingroup commande
  28. * \brief Fichier de description et activation du module Commande
  29. */
  30. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  31. /**
  32. * Class to describe module customer orders
  33. */
  34. class modCommande extends DolibarrModules
  35. {
  36. /**
  37. * Constructor. Define names, constants, directories, boxes, permissions
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. function __construct($db)
  42. {
  43. global $conf, $user;
  44. $this->db = $db;
  45. $this->numero = 25;
  46. $this->family = "crm";
  47. $this->module_position = 30;
  48. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  49. $this->name = preg_replace('/^mod/i','',get_class($this));
  50. $this->description = "Gestion des commandes clients";
  51. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  52. $this->version = 'dolibarr';
  53. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  54. $this->special = 0;
  55. $this->picto='order';
  56. // Data directories to create when module is enabled
  57. $this->dirs = array("/commande/temp");
  58. // Config pages
  59. $this->config_page_url = array("commande.php");
  60. // Dependancies
  61. $this->depends = array("modSociete");
  62. $this->requiredby = array("modExpedition");
  63. $this->conflictwith = array();
  64. $this->langfiles = array('orders', 'bills', 'companies','products', 'deliveries');
  65. // Constants
  66. $this->const = array();
  67. $r=0;
  68. $this->const[$r][0] = "COMMANDE_ADDON_PDF";
  69. $this->const[$r][1] = "chaine";
  70. $this->const[$r][2] = "einstein";
  71. $this->const[$r][3] = 'Name of PDF model of order';
  72. $this->const[$r][4] = 0;
  73. $r++;
  74. $this->const[$r][0] = "COMMANDE_ADDON";
  75. $this->const[$r][1] = "chaine";
  76. $this->const[$r][2] = "mod_commande_marbre";
  77. $this->const[$r][3] = 'Name of numbering numerotation rules of order';
  78. $this->const[$r][4] = 0;
  79. $r++;
  80. $this->const[$r][0] = "COMMANDE_ADDON_PDF_ODT_PATH";
  81. $this->const[$r][1] = "chaine";
  82. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/orders";
  83. $this->const[$r][3] = "";
  84. $this->const[$r][4] = 0;
  85. // Boxes
  86. $this->boxes = array(
  87. 0=>array('file'=>'box_commandes.php','enabledbydefaulton'=>'Home'),
  88. 2=>array('file'=>'box_graph_orders_permonth.php','enabledbydefaulton'=>'Home')
  89. );
  90. // Permissions
  91. $this->rights = array();
  92. $this->rights_class = 'commande';
  93. $r=0;
  94. $r++;
  95. $this->rights[$r][0] = 81;
  96. $this->rights[$r][1] = 'Lire les commandes clients';
  97. $this->rights[$r][2] = 'r';
  98. $this->rights[$r][3] = 1;
  99. $this->rights[$r][4] = 'lire';
  100. $r++;
  101. $this->rights[$r][0] = 82;
  102. $this->rights[$r][1] = 'Creer/modifier les commandes clients';
  103. $this->rights[$r][2] = 'w';
  104. $this->rights[$r][3] = 0;
  105. $this->rights[$r][4] = 'creer';
  106. $r++;
  107. $this->rights[$r][0] = 84;
  108. $this->rights[$r][1] = 'Valider les commandes clients';
  109. $this->rights[$r][2] = 'd';
  110. $this->rights[$r][3] = 0;
  111. $this->rights[$r][4] = 'order_advance';
  112. $this->rights[$r][5] = 'validate';
  113. $r++;
  114. $this->rights[$r][0] = 86;
  115. $this->rights[$r][1] = 'Envoyer les commandes clients';
  116. $this->rights[$r][2] = 'd';
  117. $this->rights[$r][3] = 0;
  118. $this->rights[$r][4] = 'order_advance';
  119. $this->rights[$r][5] = 'send';
  120. $r++;
  121. $this->rights[$r][0] = 87;
  122. $this->rights[$r][1] = 'Cloturer les commandes clients';
  123. $this->rights[$r][2] = 'd';
  124. $this->rights[$r][3] = 0;
  125. $this->rights[$r][4] = 'cloturer';
  126. $r++;
  127. $this->rights[$r][0] = 88;
  128. $this->rights[$r][1] = 'Annuler les commandes clients';
  129. $this->rights[$r][2] = 'd';
  130. $this->rights[$r][3] = 0;
  131. $this->rights[$r][4] = 'order_advance';
  132. $this->rights[$r][5] = 'annuler';
  133. $r++;
  134. $this->rights[$r][0] = 89;
  135. $this->rights[$r][1] = 'Supprimer les commandes clients';
  136. $this->rights[$r][2] = 'd';
  137. $this->rights[$r][3] = 0;
  138. $this->rights[$r][4] = 'supprimer';
  139. $r++;
  140. $this->rights[$r][0] = 1421;
  141. $this->rights[$r][1] = 'Exporter les commandes clients et attributs';
  142. $this->rights[$r][2] = 'r';
  143. $this->rights[$r][3] = 0;
  144. $this->rights[$r][4] = 'commande';
  145. $this->rights[$r][5] = 'export';
  146. // Exports
  147. //--------
  148. $r=0;
  149. $r++;
  150. $this->export_code[$r]=$this->rights_class.'_'.$r;
  151. $this->export_label[$r]='CustomersOrdersAndOrdersLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  152. $this->export_permission[$r]=array(array("commande","commande","export"));
  153. $this->export_fields_array[$r]=array('s.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','d.nom'=>'State','co.label'=>'Country','co.code'=>"CountryCode",'s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','c.rowid'=>"Id",'c.ref'=>"Ref",'c.ref_client'=>"RefCustomer",'c.fk_soc'=>"IdCompany",'c.date_creation'=>"DateCreation",'c.date_commande'=>"OrderDate",'c.amount_ht'=>"Amount",'c.remise_percent'=>"GlobalDiscount",'c.total_ht'=>"TotalHT",'c.total_ttc'=>"TotalTTC",'c.facture'=>"Billed",'c.fk_statut'=>'Status','c.note_public'=>"Note",'c.date_livraison'=>'DeliveryDate','c.fk_user_author'=>'CreatedById','uc.login'=>'CreatedByLogin','c.fk_user_valid'=>'ValidatedById','uv.login'=>'ValidatedByLogin','pj.ref'=>'ProjectRef','cd.rowid'=>'LineId','cd.label'=>"Label",'cd.description'=>"LineDescription",'cd.product_type'=>'TypeOfLineServiceOrProduct','cd.tva_tx'=>"LineVATRate",'cd.qty'=>"LineQty",'cd.total_ht'=>"LineTotalHT",'cd.total_tva'=>"LineTotalVAT",'cd.total_ttc'=>"LineTotalTTC",'p.rowid'=>'ProductId','p.ref'=>'ProductRef','p.label'=>'ProductLabel');
  154. //$this->export_TypeFields_array[$r]=array('s.rowid'=>"List:societe:nom",'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','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','c.ref'=>"Text",'c.ref_client'=>"Text",'c.date_creation'=>"Date",'c.date_commande'=>"Date",'c.amount_ht'=>"Numeric",'c.remise_percent'=>"Numeric",'c.total_ht'=>"Numeric",'c.total_ttc'=>"Numeric",'c.facture'=>"Boolean",'c.fk_statut'=>'Status','c.note_public'=>"Text",'c.date_livraison'=>'Date','cd.description'=>"Text",'cd.product_type'=>'Boolean','cd.tva_tx'=>"Numeric",'cd.qty'=>"Numeric",'cd.total_ht'=>"Numeric",'cd.total_tva'=>"Numeric",'cd.total_ttc'=>"Numeric",'p.rowid'=>'List:product:ref','p.ref'=>'Text','p.label'=>'Text');
  155. $this->export_TypeFields_array[$r]=array('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','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','c.ref'=>"Text",'c.ref_client'=>"Text",'c.date_creation'=>"Date",'c.date_commande'=>"Date",'c.amount_ht'=>"Numeric",'c.remise_percent'=>"Numeric",'c.total_ht'=>"Numeric",'c.total_ttc'=>"Numeric",'c.facture'=>"Boolean",'c.fk_statut'=>'Status','c.note_public'=>"Text",'c.date_livraison'=>'Date','pj.ref'=>'Text','cd.description'=>"Text",'cd.product_type'=>'Boolean','cd.tva_tx'=>"Numeric",'cd.qty'=>"Numeric",'cd.total_ht'=>"Numeric",'cd.total_tva'=>"Numeric",'cd.total_ttc'=>"Numeric",'p.rowid'=>'List:product:ref','p.ref'=>'Text','p.label'=>'Text','d.nom'=>'Text');
  156. $this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','d.nom'=>'company','co.label'=>'company','co.code'=>'company','s.phone'=>'company','s.siren'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.siret'=>'company','c.rowid'=>"order",'c.ref'=>"order",'c.ref_client'=>"order",'c.fk_soc'=>"order",'c.date_creation'=>"order",'c.date_commande'=>"order",'c.amount_ht'=>"order",'c.remise_percent'=>"order",'c.total_ht'=>"order",'c.total_ttc'=>"order",'c.facture'=>"order",'c.fk_statut'=>"order",'c.note'=>"order",'c.date_livraison'=>"order",'pj.ref'=>'project','cd.rowid'=>'order_line','cd.label'=>"order_line",'cd.description'=>"order_line",'cd.product_type'=>'order_line','cd.tva_tx'=>"order_line",'cd.qty'=>"order_line",'cd.total_ht'=>"order_line",'cd.total_tva'=>"order_line",'cd.total_ttc'=>"order_line",'p.rowid'=>'product','p.ref'=>'product','p.label'=>'product');
  157. $this->export_dependencies_array[$r]=array('order_line'=>'cd.rowid','product'=>'cd.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  158. $keyforselect='commande'; $keyforelement='order'; $keyforaliasextra='extra';
  159. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  160. $keyforselect='commandedet'; $keyforelement='order_line'; $keyforaliasextra='extra2';
  161. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  162. $keyforselect='product'; $keyforelement='product'; $keyforaliasextra='extra3';
  163. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  164. $this->export_sql_start[$r]='SELECT DISTINCT ';
  165. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  166. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  167. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
  168. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON s.fk_pays = co.rowid,';
  169. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande as c';
  170. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON c.fk_projet = pj.rowid';
  171. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON c.fk_user_author = uc.rowid';
  172. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON c.fk_user_valid = uv.rowid';
  173. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_extrafields as extra ON c.rowid = extra.fk_object';
  174. $this->export_sql_end[$r] .=' , '.MAIN_DB_PREFIX.'commandedet as cd';
  175. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commandedet_extrafields as extra2 on cd.rowid = extra2.fk_object';
  176. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on cd.fk_product = p.rowid';
  177. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra3 on p.rowid = extra3.fk_object';
  178. $this->export_sql_end[$r] .=' WHERE c.fk_soc = s.rowid AND c.rowid = cd.fk_commande';
  179. $this->export_sql_end[$r] .=' AND c.entity IN ('.getEntity('commande',1).')';
  180. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
  181. }
  182. /**
  183. * Function called when module is enabled.
  184. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  185. * It also creates data directories
  186. *
  187. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  188. * @return int 1 if OK, 0 if KO
  189. */
  190. function init($options='')
  191. {
  192. global $conf,$langs;
  193. // Permissions
  194. $this->remove($options);
  195. //ODT template
  196. $src=DOL_DOCUMENT_ROOT.'/install/doctemplates/orders/template_order.odt';
  197. $dirodt=DOL_DATA_ROOT.'/doctemplates/orders';
  198. $dest=$dirodt.'/template_order.odt';
  199. if (file_exists($src) && ! file_exists($dest))
  200. {
  201. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  202. dol_mkdir($dirodt);
  203. $result=dol_copy($src,$dest,0,0);
  204. if ($result < 0)
  205. {
  206. $langs->load("errors");
  207. $this->error=$langs->trans('ErrorFailToCopyFile',$src,$dest);
  208. return 0;
  209. }
  210. }
  211. $sql = array(
  212. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->const[0][2]."' AND entity = ".$conf->entity,
  213. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->const[0][2]."','order',".$conf->entity.")"
  214. );
  215. return $this->_init($sql,$options);
  216. }
  217. }