modReception.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* Copyright (C) 2018 Quentin Vial-Gouteyron <quentin.vial-gouteyron@atm-consulting.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \defgroup reception Module reception
  19. * \brief Module pour gerer les réceptions de produits
  20. * \file htdocs/core/modules/modReception.class.php
  21. * \ingroup reception
  22. * \brief Fichier de description et activation du module Reception
  23. */
  24. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  25. /**
  26. * Class to describe and enable module Reception
  27. */
  28. class modReception extends DolibarrModules
  29. {
  30. /**
  31. * Constructor. Define names, constants, directories, boxes, permissions
  32. *
  33. * @param DoliDB $db Database handler
  34. */
  35. public function __construct($db)
  36. {
  37. global $conf, $user;
  38. $this->db = $db;
  39. $this->numero = 94160;
  40. $this->family = "srm";
  41. $this->module_position = '40';
  42. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  43. $this->name = preg_replace('/^mod/i', '', get_class($this));
  44. $this->description = "Gestion des réceptions fournisseurs";
  45. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  46. $this->version = 'experimental';
  47. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  48. $this->picto = "sending";
  49. // Data directories to create when module is enabled
  50. $this->dirs = array("/reception/receipt",
  51. "/reception/receipt/temp",
  52. "/doctemplates/receptions"
  53. );
  54. // Config pages
  55. $this->config_page_url = array("reception_setup.php");
  56. // Dependencies
  57. $this->depends = array("modFournisseur");
  58. $this->requiredby = array();
  59. $this->conflictwith = array();
  60. $this->langfiles = array('receptions');
  61. // Constants
  62. $this->const = array();
  63. $r=0;
  64. $this->const[$r][0] = "RECEPTION_ADDON_PDF";
  65. $this->const[$r][1] = "chaine";
  66. $this->const[$r][2] = "squille";
  67. $this->const[$r][3] = 'Nom du gestionnaire de generation des bons receptions en PDF';
  68. $this->const[$r][4] = 0;
  69. $r++;
  70. $this->const[$r][0] = "RECEPTION_ADDON_NUMBER";
  71. $this->const[$r][1] = "chaine";
  72. $this->const[$r][2] = "mod_reception_beryl";
  73. $this->const[$r][3] = 'Name for numbering manager for receptions';
  74. $this->const[$r][4] = 0;
  75. $r++;
  76. $this->const[$r][0] = "RECEPTION_ADDON_PDF_ODT_PATH";
  77. $this->const[$r][1] = "chaine";
  78. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/receptions";
  79. $this->const[$r][3] = "";
  80. $this->const[$r][4] = 0;
  81. $r++;
  82. $this->const[$r][0] = "MAIN_SUBMODULE_RECEPTION";
  83. $this->const[$r][1] = "chaine";
  84. $this->const[$r][2] = "1";
  85. $this->const[$r][3] = "Enable receptions";
  86. $this->const[$r][4] = 0;
  87. $r++;
  88. // Boxes
  89. $this->boxes = array();
  90. // Permissions
  91. $this->rights = array();
  92. $this->rights_class = 'reception';
  93. $r=0;
  94. $r++;
  95. $this->rights[$r][0] = $this->numero.$r;
  96. $this->rights[$r][1] = 'Lire les receptions';
  97. $this->rights[$r][2] = 'r';
  98. $this->rights[$r][3] = 0;
  99. $this->rights[$r][4] = 'lire';
  100. $r++;
  101. $this->rights[$r][0] = $this->numero.$r;
  102. $this->rights[$r][1] = 'Creer modifier les receptions';
  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] = $this->numero.$r;
  108. $this->rights[$r][1] = 'Valider les receptions';
  109. $this->rights[$r][2] = 'd';
  110. $this->rights[$r][3] = 0;
  111. $this->rights[$r][4] = 'reception_advance';
  112. $this->rights[$r][5] = 'validate';
  113. $r++;
  114. $this->rights[$r][0] = $this->numero.$r; // id de la permission
  115. $this->rights[$r][1] = 'Envoyer les receptions aux clients'; // libelle de la permission
  116. $this->rights[$r][2] = 'd'; // type de la permission (deprecie a ce jour)
  117. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  118. $this->rights[$r][4] = 'reception_advance';
  119. $this->rights[$r][5] = 'send';
  120. $r++;
  121. $this->rights[$r][0] = $this->numero.$r;
  122. $this->rights[$r][1] = 'Exporter les receptions';
  123. $this->rights[$r][2] = 'r';
  124. $this->rights[$r][3] = 0;
  125. $this->rights[$r][4] = 'reception';
  126. $this->rights[$r][5] = 'export';
  127. $r++;
  128. $this->rights[$r][0] = $this->numero.$r;
  129. $this->rights[$r][1] = 'Supprimer les receptions';
  130. $this->rights[$r][2] = 'd';
  131. $this->rights[$r][3] = 0;
  132. $this->rights[$r][4] = 'supprimer';
  133. // Menus
  134. //-------
  135. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  136. // Exports
  137. //--------
  138. $r=0;
  139. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  140. $shipment=new CommandeFournisseur($this->db);
  141. $contact_arrays=$shipment->liste_type_contact('external', '', 0, 0, '');
  142. if (is_array($contact_arrays) && count($contact_arrays)>0){
  143. $idcontacts=join(',', array_keys($shipment->liste_type_contact('external', '', 0, 0, '')));
  144. } else {
  145. $idcontacts=0;
  146. }
  147. $r++;
  148. $this->export_code[$r]=$this->rights_class.'_'.$r;
  149. $this->export_label[$r]='Receptions'; // Translation key (used only if key ExportDataset_xxx_z not found)
  150. $this->export_permission[$r]=array(array("reception","reception","export"));
  151. $this->export_fields_array[$r]=array(
  152. 's.rowid'=>"IdCompany",'s.nom'=>'ThirdParty','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town',
  153. 'd.nom'=>'State','co.label'=>'Country','co.code'=>'CountryCode','s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.idprof5'=>'ProfId5','s.idprof6'=>'ProfId6',
  154. 'c.rowid'=>"Id",'c.ref'=>"Ref",'c.ref_supplier'=>"RefSupplier",'c.fk_soc'=>"IdCompany",'c.date_creation'=>"DateCreation",'c.date_delivery'=>"DateDeliveryPlanned",'c.tracking_number'=>"TrackingNumber",'c.height'=>"Height",'c.width'=>"Width",'c.size'=>"Depth",'c.size_units'=>'SizeUnits','c.weight'=>"Weight",'c.weight_units'=>"WeightUnits",'c.fk_statut'=>'Status','c.note_public'=>"NotePublic",'ed.rowid'=>'LineId',
  155. 'ed.comment'=>'Description','ed.qty'=>"Qty",
  156. 'p.rowid'=>'ProductId','p.ref'=>'ProductRef','p.label'=>'ProductLabel','p.weight'=>'ProductWeight','p.weight_units'=>'WeightUnits','p.volume'=>'ProductVolume','p.volume_units'=>'VolumeUnits'
  157. );
  158. if ($idcontacts && ! empty($conf->global->RECEPTION_ADD_CONTACTS_IN_EXPORT)) $this->export_fields_array[$r]+=array('sp.rowid'=>'IdContact','sp.lastname'=>'Lastname','sp.firstname'=>'Firstname','sp.note_public'=>'NotePublic');
  159. //$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','ed.qty'=>"Text");
  160. $this->export_TypeFields_array[$r]=array(
  161. 's.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text',
  162. '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',
  163. 'c.ref'=>"Text",'c.ref_supplier'=>"Text",'c.date_creation'=>"Date",'c.date_delivery'=>"Date",'c.tracking_number'=>"Numeric",'c.height'=>"Numeric",'c.width'=>"Numeric",'c.weight'=>"Numeric",'c.fk_statut'=>'Status','c.note_public'=>"Text",
  164. 'ed.qty'=>"Numeric",'d.nom'=>'Text'
  165. );
  166. $this->export_entities_array[$r]=array(
  167. 's.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company',
  168. 'd.nom'=>'company','co.label'=>'company','co.code'=>'company','s.fk_pays'=>'company','s.phone'=>'company','s.siren'=>'company','s.ape'=>'company','s.siret'=>'company','s.idprof4'=>'company','s.idprof5'=>'company','s.idprof6'=>'company',
  169. 'c.rowid'=>"reception",'c.ref'=>"reception",'c.ref_supplier'=>"reception",'c.fk_soc'=>"reception",'c.date_creation'=>"reception",'c.date_delivery'=>"reception",'c.tracking_number'=>'reception','c.height'=>"reception",'c.width'=>"reception",'c.size'=>'reception','c.size_units'=>'reception','c.weight'=>"reception",'c.weight_units'=>'reception','c.fk_statut'=>"reception",'c.note_public'=>"reception",'ed.rowid'=>'reception_line','ed.comment'=>'reception_line','ed.qty'=>"reception_line",
  170. 'p.rowid'=>'product','p.ref'=>'product','p.label'=>'product','p.weight'=>'product','p.weight_units'=>'product','p.volume'=>'product','p.volume_units'=>'product'
  171. );
  172. if ($idcontacts && ! empty($conf->global->RECEPTION_ADD_CONTACTS_IN_EXPORT)) $this->export_entities_array[$r]+=array('sp.rowid'=>'contact','sp.lastname'=>'contact','sp.firstname'=>'contact','sp.note_public'=>'contact');
  173. $this->export_dependencies_array[$r]=array('reception_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
  174. if ($idcontacts && ! empty($conf->global->RECEPTION_ADD_CONTACTS_IN_EXPORT))
  175. {
  176. $keyforselect='socpeople'; $keyforelement='contact'; $keyforaliasextra='extra3';
  177. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  178. }
  179. $keyforselect='reception'; $keyforelement='reception'; $keyforaliasextra='extra';
  180. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  181. $keyforselect='commande_fournisseur_dispatch'; $keyforelement='reception_line'; $keyforaliasextra='extra2';
  182. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  183. $this->export_sql_start[$r]='SELECT DISTINCT ';
  184. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'reception as c';
  185. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'reception_extrafields as extra ON c.rowid = extra.fk_object,';
  186. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'societe as s';
  187. 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';
  188. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
  189. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON s.fk_pays = co.rowid,';
  190. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch as ed';
  191. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch_extrafields as extra2 ON ed.rowid = extra2.fk_object';
  192. $this->export_sql_end[$r] .=' , '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
  193. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on cd.fk_product = p.rowid';
  194. if ($idcontacts && ! empty($conf->global->RECEPTION_ADD_CONTACTS_IN_EXPORT)) {
  195. $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 ('.$idcontacts.')';
  196. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople as sp ON sp.rowid = ee.fk_socpeople';
  197. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'socpeople_extrafields as extra3 ON sp.rowid = extra3.fk_object';
  198. }
  199. $this->export_sql_end[$r] .=' WHERE c.fk_soc = s.rowid AND c.rowid = ed.fk_reception AND ed.fk_commandefourndet = cd.rowid';
  200. $this->export_sql_end[$r] .=' AND c.entity IN ('.getEntity('reception').')';
  201. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
  202. }
  203. /**
  204. * Function called when module is enabled.
  205. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  206. * It also creates data directories
  207. *
  208. * @param string $options Options when enabling module ('', 'noboxes')
  209. * @return int 1 if OK, 0 if KO
  210. */
  211. public function init($options = '')
  212. {
  213. global $conf,$langs;
  214. // Permissions
  215. $this->remove($options);
  216. //ODT template
  217. $src=DOL_DOCUMENT_ROOT.'/install/doctemplates/reception/template_reception.odt';
  218. $dirodt=DOL_DATA_ROOT.'/doctemplates/reception';
  219. $dest=$dirodt.'/template_reception.odt';
  220. if (file_exists($src) && ! file_exists($dest))
  221. {
  222. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  223. dol_mkdir($dirodt);
  224. $result=dol_copy($src, $dest, 0, 0);
  225. if ($result < 0)
  226. {
  227. $langs->load("errors");
  228. $this->error=$langs->trans('ErrorFailToCopyFile', $src, $dest);
  229. return 0;
  230. }
  231. }
  232. $sql = array();
  233. $sql = array(
  234. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[0][2])."' AND type = 'reception' AND entity = ".$conf->entity,
  235. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[0][2])."','reception',".$conf->entity.")",
  236. );
  237. return $this->_init($sql, $options);
  238. }
  239. }