modFournisseur.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2013-2015 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 <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \defgroup fournisseur suppliers Module
  23. * \brief Module to manage companies and contacts of supplier type
  24. * \file htdocs/core/modules/modFournisseur.class.php
  25. * \ingroup fournisseur
  26. * \brief Description and activation file for module Fournisseur
  27. */
  28. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  29. /**
  30. * Description and activation class for module Fournisseur
  31. */
  32. class modFournisseur extends DolibarrModules
  33. {
  34. /**
  35. * Constructor. Define names, constants, directories, boxes, permissions
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. function __construct($db)
  40. {
  41. global $conf;
  42. $this->db = $db;
  43. $this->numero = 40;
  44. $this->family = "products";
  45. $this->module_position = 10;
  46. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  47. $this->name = preg_replace('/^mod/i','',get_class($this));
  48. $this->description = "Gestion des fournisseurs";
  49. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  50. $this->version = 'dolibarr';
  51. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  52. $this->special = 0;
  53. $this->picto='company';
  54. // Data directories to create when module is enabled
  55. $this->dirs = array("/fournisseur/temp",
  56. "/fournisseur/commande",
  57. "/fournisseur/commande/temp",
  58. "/fournisseur/facture",
  59. "/fournisseur/facture/temp"
  60. );
  61. // Dependencies
  62. $this->depends = array("modSociete");
  63. $this->requiredby = array();
  64. $this->langfiles = array('bills', 'companies', 'suppliers', 'orders');
  65. // Config pages
  66. $this->config_page_url = array("supplier_order.php");
  67. // Constants
  68. $this->const = array();
  69. $r=0;
  70. $this->const[$r][0] = "COMMANDE_SUPPLIER_ADDON_PDF";
  71. $this->const[$r][1] = "chaine";
  72. $this->const[$r][2] = "muscadet";
  73. $this->const[$r][3] = 'Nom du gestionnaire de generation des bons de commande en PDF';
  74. $this->const[$r][4] = 0;
  75. $r++;
  76. $this->const[$r][0] = "COMMANDE_SUPPLIER_ADDON_NUMBER";
  77. $this->const[$r][1] = "chaine";
  78. $this->const[$r][2] = "mod_commande_fournisseur_muguet";
  79. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des commandes fournisseur';
  80. $this->const[$r][4] = 0;
  81. $r++;
  82. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_PDF";
  83. $this->const[$r][1] = "chaine";
  84. $this->const[$r][2] = "canelle";
  85. $this->const[$r][3] = 'Nom du gestionnaire de generation des factures fournisseur en PDF';
  86. $this->const[$r][4] = 0;
  87. $r++;
  88. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_NUMBER";
  89. $this->const[$r][1] = "chaine";
  90. $this->const[$r][2] = "mod_facture_fournisseur_cactus";
  91. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des factures fournisseur';
  92. $this->const[$r][4] = 0;
  93. $r++;
  94. // Boxes
  95. $this->boxes = array(
  96. 0=>array('file'=>'box_graph_invoices_supplier_permonth.php','enabledbydefaulton'=>'Home'),
  97. 1=>array('file'=>'box_graph_orders_supplier_permonth.php','enabledbydefaulton'=>'Home'),
  98. 2=>array('file'=>'box_fournisseurs.php','enabledbydefaulton'=>'Home'),
  99. 3=>array('file'=>'box_factures_fourn_imp.php','enabledbydefaulton'=>'Home'),
  100. 4=>array('file'=>'box_factures_fourn.php','enabledbydefaulton'=>'Home'),
  101. 5=>array('file'=>'box_supplier_orders.php','enabledbydefaulton'=>'Home'),
  102. );
  103. // Permissions
  104. $this->rights = array();
  105. $this->rights_class = 'fournisseur';
  106. $r=0;
  107. $r++;
  108. $this->rights[$r][0] = 1181;
  109. $this->rights[$r][1] = 'Consulter les fournisseurs';
  110. $this->rights[$r][2] = 'r';
  111. $this->rights[$r][3] = 1;
  112. $this->rights[$r][4] = 'lire';
  113. $r++;
  114. $this->rights[$r][0] = 1182;
  115. $this->rights[$r][1] = 'Consulter les commandes fournisseur';
  116. $this->rights[$r][2] = 'r';
  117. $this->rights[$r][3] = 1;
  118. $this->rights[$r][4] = 'commande';
  119. $this->rights[$r][5] = 'lire';
  120. $r++;
  121. $this->rights[$r][0] = 1183;
  122. $this->rights[$r][1] = 'Creer une commande fournisseur';
  123. $this->rights[$r][2] = 'w';
  124. $this->rights[$r][3] = 0;
  125. $this->rights[$r][4] = 'commande';
  126. $this->rights[$r][5] = 'creer';
  127. $r++;
  128. $this->rights[$r][0] = 1184;
  129. $this->rights[$r][1] = 'Valider une commande fournisseur';
  130. $this->rights[$r][2] = 'w';
  131. $this->rights[$r][3] = 0;
  132. $this->rights[$r][4] = 'supplier_order_advance';
  133. $this->rights[$r][5] = 'validate';
  134. $r++;
  135. $this->rights[$r][0] = 1185;
  136. $this->rights[$r][1] = 'Approuver une commande fournisseur';
  137. $this->rights[$r][2] = 'w';
  138. $this->rights[$r][3] = 0;
  139. $this->rights[$r][4] = 'commande';
  140. $this->rights[$r][5] = 'approuver';
  141. /*$r++;
  142. $this->rights[$r][0] = 1191;
  143. $this->rights[$r][1] = 'Approuver une commande fournisseur (si supérieur hiérarchique)';
  144. $this->rights[$r][2] = 'w';
  145. $this->rights[$r][3] = 0;
  146. $this->rights[$r][4] = 'commande';
  147. $this->rights[$r][5] = 'approve_ifsupervisor_advance';*/
  148. $r++;
  149. $this->rights[$r][0] = 1186;
  150. $this->rights[$r][1] = 'Commander une commande fournisseur';
  151. $this->rights[$r][2] = 'w';
  152. $this->rights[$r][3] = 0;
  153. $this->rights[$r][4] = 'commande';
  154. $this->rights[$r][5] = 'commander';
  155. $r++;
  156. $this->rights[$r][0] = 1187;
  157. $this->rights[$r][1] = 'Receptionner une commande fournisseur';
  158. $this->rights[$r][2] = 'd';
  159. $this->rights[$r][3] = 0;
  160. $this->rights[$r][4] = 'commande';
  161. $this->rights[$r][5] = 'receptionner';
  162. $r++;
  163. $this->rights[$r][0] = 1189;
  164. $this->rights[$r][1] = 'Check/Uncheck a supplier order reception';
  165. $this->rights[$r][2] = 'w';
  166. $this->rights[$r][3] = 0;
  167. $this->rights[$r][4] = 'commande_advance';
  168. $this->rights[$r][5] = 'check';
  169. $r++;
  170. $this->rights[$r][0] = 1188;
  171. $this->rights[$r][1] = 'Supprimer une commande fournisseur';
  172. $this->rights[$r][2] = 'd';
  173. $this->rights[$r][3] = 0;
  174. $this->rights[$r][4] = 'commande';
  175. $this->rights[$r][5] = 'supprimer';
  176. $r++;
  177. $this->rights[$r][0] = 1231;
  178. $this->rights[$r][1] = 'Consulter les factures fournisseur';
  179. $this->rights[$r][2] = 'r';
  180. $this->rights[$r][3] = 1;
  181. $this->rights[$r][4] = 'facture';
  182. $this->rights[$r][5] = 'lire';
  183. $r++;
  184. $this->rights[$r][0] = 1232;
  185. $this->rights[$r][1] = 'Creer une facture fournisseur';
  186. $this->rights[$r][2] = 'w';
  187. $this->rights[$r][3] = 0;
  188. $this->rights[$r][4] = 'facture';
  189. $this->rights[$r][5] = 'creer';
  190. $r++;
  191. $this->rights[$r][0] = 1233;
  192. $this->rights[$r][1] = 'Valider une facture fournisseur';
  193. $this->rights[$r][2] = 'w';
  194. $this->rights[$r][3] = 0;
  195. $this->rights[$r][4] = 'supplier_invoice_advance';
  196. $this->rights[$r][5] = 'validate';
  197. $r++;
  198. $this->rights[$r][0] = 1234;
  199. $this->rights[$r][1] = 'Supprimer une facture fournisseur';
  200. $this->rights[$r][2] = 'd';
  201. $this->rights[$r][3] = 0;
  202. $this->rights[$r][4] = 'facture';
  203. $this->rights[$r][5] = 'supprimer';
  204. $r++;
  205. $this->rights[$r][0] = 1235;
  206. $this->rights[$r][1] = 'Envoyer les factures par mail';
  207. $this->rights[$r][2] = 'a';
  208. $this->rights[$r][3] = 0;
  209. $this->rights[$r][4] = 'supplier_invoice_advance';
  210. $this->rights[$r][5] = 'send';
  211. $r++;
  212. $this->rights[$r][0] = 1236;
  213. $this->rights[$r][1] = 'Exporter les factures fournisseurs, attributs et reglements';
  214. $this->rights[$r][2] = 'r';
  215. $this->rights[$r][3] = 0;
  216. $this->rights[$r][4] = 'facture';
  217. $this->rights[$r][5] = 'export';
  218. $r++;
  219. $this->rights[$r][0] = 1237;
  220. $this->rights[$r][1] = 'Exporter les commande fournisseurs, attributs';
  221. $this->rights[$r][2] = 'r';
  222. $this->rights[$r][3] = 0;
  223. $this->rights[$r][4] = 'commande';
  224. $this->rights[$r][5] = 'export';
  225. if (! empty($conf->global->SUPPLIER_ORDER_DOUBLE_APPROVAL))
  226. {
  227. $r++;
  228. $this->rights[$r][0] = 1190;
  229. $this->rights[$r][1] = 'Approve supplier order (second level)'; // $langs->trans("Permission1190");
  230. $this->rights[$r][2] = 'w';
  231. $this->rights[$r][3] = 0;
  232. $this->rights[$r][4] = 'commande';
  233. $this->rights[$r][5] = 'approve2';
  234. }
  235. // Exports
  236. //--------
  237. $r=0;
  238. $r++;
  239. $this->export_code[$r]=$this->rights_class.'_'.$r;
  240. $this->export_label[$r]='Factures fournisseurs et lignes de facture';
  241. $this->export_icon[$r]='bill';
  242. $this->export_permission[$r]=array(array("fournisseur","facture","export"));
  243. $this->export_fields_array[$r]=array('s.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','c.code'=>'CountryCode','s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.idprof5'=>'ProfId5','s.idprof6'=>'ProfId6','s.tva_intra'=>'VATIntra','f.rowid'=>"InvoiceId",'f.ref'=>"InvoiceRef",'f.ref_supplier'=>"RefSupplier",'f.datec'=>"InvoiceDateCreation",'f.datef'=>"DateInvoice",'f.total_ht'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.total_tva'=>"TotalVAT",'f.paye'=>"InvoicePaid",'f.fk_statut'=>'InvoiceStatus','f.note_public'=>"InvoiceNote",'fd.rowid'=>'LineId','fd.description'=>"LineDescription",'fd.tva_tx'=>"LineVATRate",'fd.qty'=>"LineQty",'fd.remise_percent'=>"Discount",'fd.total_ht'=>"LineTotalHT",'fd.total_ttc'=>"LineTotalTTC",'fd.tva'=>"LineTotalVAT",'fd.product_type'=>'TypeOfLineServiceOrProduct','fd.fk_product'=>'ProductId',
  244. 'p.ref'=>'ProductRef','p.label'=>'ProductLabel','p.accountancy_code_buy'=>'ProductAccountancyBuyCode','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  245. //$this->export_TypeFields_array[$r]=array('s.rowid'=>"List:societe:CompanyName",'s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.tva_intra'=>'Text','f.ref'=>"Text",'f.datec'=>"Date",'f.datef'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_public'=>"Text",'fd.description'=>"Text",'fd.tva_tx'=>"Text",'fd.qty'=>"Numeric",'fd.total_ht'=>"Numeric",'fd.total_ttc'=>"Numeric",'fd.tva'=>"Numeric",'fd.product_type'=>'Numeric','fd.fk_product'=>'List:product:label','p.ref'=>'Text','p.label'=>'Text');
  246. $this->export_TypeFields_array[$r]=array('s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.tva_intra'=>'Text','f.ref'=>"Text",'f.ref_supplier'=>"Text",'f.datec'=>"Date",'f.datef'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_public'=>"Text",'fd.description'=>"Text",'fd.tva_tx'=>"Text",'fd.qty'=>"Numeric",'fd.total_ht'=>"Numeric",'fd.total_ttc'=>"Numeric",'fd.tva'=>"Numeric",'fd.product_type'=>'Numeric','fd.fk_product'=>'List:product:label',
  247. 'p.ref'=>'Text','p.label'=>'Text','project.ref'=>'Text','project.title'=>'Text');
  248. $this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','c.code'=>'company','s.phone'=>'company','s.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.idprof5'=>'company','s.idprof6'=>'company','s.tva_intra'=>'company','f.rowid'=>"invoice",'f.ref'=>"invoice",'f.ref_supplier'=>"invoice",'f.datec'=>"invoice",'f.datef'=>"invoice",'f.total_ht'=>"invoice",'f.total_ttc'=>"invoice",'f.total_tva'=>"invoice",'f.paye'=>"invoice",'f.fk_statut'=>'invoice','f.note_public'=>"invoice",'fd.rowid'=>'invoice_line','fd.description'=>"invoice_line",'fd.tva_tx'=>"invoice_line",'fd.qty'=>"invoice_line",'fd.remise_percent'=>"invoice_line",'fd.total_ht'=>"invoice_line",'fd.total_ttc'=>"invoice_line",'fd.tva'=>"invoice_line",'fd.product_type'=>'invoice_line','fd.fk_product'=>'product',
  249. 'p.ref'=>'product','p.label'=>'product','p.accountancy_code_buy'=>'product','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  250. $this->export_dependencies_array[$r]=array('invoice_line'=>'fd.rowid','product'=>'fd.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  251. // Add extra fields object
  252. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn'";
  253. $resql=$this->db->query($sql);
  254. if ($resql) // This can fail when class is used on old database (during migration for example)
  255. {
  256. while ($obj=$this->db->fetch_object($resql))
  257. {
  258. $fieldname='extra.'.$obj->name;
  259. $fieldlabel=ucfirst($obj->label);
  260. $typeFilter="Text";
  261. switch($obj->type)
  262. {
  263. case 'int':
  264. case 'double':
  265. case 'price':
  266. $typeFilter="Numeric";
  267. break;
  268. case 'date':
  269. case 'datetime':
  270. $typeFilter="Date";
  271. break;
  272. case 'boolean':
  273. $typeFilter="Boolean";
  274. break;
  275. case 'sellist':
  276. $tmp='';
  277. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  278. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  279. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  280. break;
  281. }
  282. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  283. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  284. $this->export_entities_array[$r][$fieldname]='invoice';
  285. }
  286. }
  287. // End add extra fields
  288. // Add extra fields line
  289. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn_det'";
  290. $resql=$this->db->query($sql);
  291. if ($resql) // This can fail when class is used on old database (during migration for example)
  292. {
  293. while ($obj=$this->db->fetch_object($resql))
  294. {
  295. $fieldname='extraline.'.$obj->name;
  296. $fieldlabel=ucfirst($obj->label);
  297. $typeFilter="Text";
  298. switch($obj->type)
  299. {
  300. case 'int':
  301. case 'double':
  302. case 'price':
  303. $typeFilter="Numeric";
  304. break;
  305. case 'date':
  306. case 'datetime':
  307. $typeFilter="Date";
  308. break;
  309. case 'boolean':
  310. $typeFilter="Boolean";
  311. break;
  312. case 'sellist':
  313. $tmp='';
  314. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  315. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  316. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  317. break;
  318. }
  319. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  320. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  321. $this->export_entities_array[$r][$fieldname]='invoice_line';
  322. }
  323. }
  324. // End add extra fields line
  325. $this->export_sql_start[$r]='SELECT DISTINCT ';
  326. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  327. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  328. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'facture_fourn as f';
  329. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  330. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  331. $this->export_sql_end[$r] .=' , '.MAIN_DB_PREFIX.'facture_fourn_det as fd';
  332. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_det_extrafields as extraline ON fd.rowid = extraline.fk_object';
  333. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  334. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture_fourn';
  335. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_invoice',1).')';
  336. $r++;
  337. $this->export_code[$r]=$this->rights_class.'_'.$r;
  338. $this->export_label[$r]='Factures fournisseurs et reglements';
  339. $this->export_icon[$r]='bill';
  340. $this->export_permission[$r]=array(array("fournisseur","facture","export"));
  341. $this->export_fields_array[$r]=array('s.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','c.code'=>'CountryCode','s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.idprof5'=>'ProfId5','s.idprof6'=>'ProfId6','s.tva_intra'=>'VATIntra','f.rowid'=>"InvoiceId",'f.ref'=>"InvoiceRef",'f.ref_supplier'=>"RefSupplier",'f.datec'=>"InvoiceDateCreation",'f.datef'=>"DateInvoice",'f.total_ht'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.total_tva'=>"TotalVAT",'f.paye'=>"InvoicePaid",'f.fk_statut'=>'InvoiceStatus','f.note_public'=>"InvoiceNote",'p.rowid'=>'PaymentId','pf.amount'=>'AmountPayment',
  342. 'p.datep'=>'DatePayment','p.num_paiement'=>'PaymentNumber','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  343. //$this->export_TypeFields_array[$r]=array('s.rowid'=>"List:societe:CompanyName",'s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.tva_intra'=>'Text','f.ref'=>"Text",'f.datec'=>"Date",'f.datef'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_public'=>"Text",'pf.amount'=>'Numeric','p.datep'=>'Date','p.num_paiement'=>'Numeric');
  344. $this->export_TypeFields_array[$r]=array('s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.tva_intra'=>'Text','f.ref'=>"Text",'f.ref_supplier'=>"Text",'f.datec'=>"Date",'f.datef'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_public'=>"Text",'pf.amount'=>'Numeric',
  345. 'p.datep'=>'Date','p.num_paiement'=>'Numeric','project.ref'=>'Text','project.title'=>'Text');
  346. $this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','c.code'=>'company','s.phone'=>'company','s.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.idprof5'=>'company','s.idprof6'=>'company','s.tva_intra'=>'company','f.rowid'=>"invoice",'f.ref'=>"invoice",'f.ref_supplier'=>"invoice",'f.datec'=>"invoice",'f.datef'=>"invoice",'f.total_ht'=>"invoice",'f.total_ttc'=>"invoice",'f.total_tva'=>"invoice",'f.paye'=>"invoice",'f.fk_statut'=>'invoice','f.note_public'=>"invoice",'p.rowid'=>'payment','pf.amount'=>'payment',
  347. 'p.datep'=>'payment','p.num_paiement'=>'payment','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  348. $this->export_dependencies_array[$r]=array('payment'=>'p.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  349. // Add extra fields object
  350. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn'";
  351. $resql=$this->db->query($sql);
  352. if ($resql) // This can fail when class is used on old database (during migration for example)
  353. {
  354. while ($obj=$this->db->fetch_object($resql))
  355. {
  356. $fieldname='extra.'.$obj->name;
  357. $fieldlabel=ucfirst($obj->label);
  358. $typeFilter="Text";
  359. switch($obj->type)
  360. {
  361. case 'int':
  362. case 'double':
  363. case 'price':
  364. $typeFilter="Numeric";
  365. break;
  366. case 'date':
  367. case 'datetime':
  368. $typeFilter="Date";
  369. break;
  370. case 'boolean':
  371. $typeFilter="Boolean";
  372. break;
  373. case 'sellist':
  374. $tmp='';
  375. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  376. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  377. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  378. break;
  379. }
  380. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  381. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  382. $this->export_entities_array[$r][$fieldname]='invoice';
  383. }
  384. }
  385. // End add extra fields object
  386. $this->export_sql_start[$r]='SELECT DISTINCT ';
  387. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  388. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  389. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'facture_fourn as f';
  390. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  391. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  392. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
  393. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn as p ON pf.fk_paiementfourn = p.rowid';
  394. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid';
  395. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_invoice',1).')';
  396. // Order
  397. $r++;
  398. $this->export_code[$r]=$this->rights_class.'_'.$r;
  399. $this->export_label[$r]='Commandes fournisseurs et lignes de commandes';
  400. $this->export_icon[$r]='order';
  401. $this->export_permission[$r]=array(array("fournisseur","commande","export"));
  402. $this->export_fields_array[$r]=array('s.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','c.code'=>'CountryCode','s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.idprof5'=>'ProfId5','s.idprof6'=>'ProfId6','s.tva_intra'=>'VATIntra','f.rowid'=>"OrderId",'f.ref'=>"Ref",'f.ref_supplier'=>"RefSupplier",'f.date_creation'=>"DateCreation",'f.date_commande'=>"OrderDate",'f.total_ht'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.tva'=>"TotalVAT",'f.fk_statut'=>'Status','f.date_approve'=>'DateApprove','f.date_approve2'=>'DateApprove2','f.note_public'=>"NotePublic",'f.note_private'=>"NotePrivate",'ua1.login'=>'ApprovedBy','ua2.login'=>'ApprovedBy2','fd.rowid'=>'LineId','fd.description'=>"LineDescription",'fd.tva_tx'=>"LineVATRate",'fd.qty'=>"LineQty",'fd.remise_percent'=>"Discount",'fd.total_ht'=>"LineTotalHT",'fd.total_ttc'=>"LineTotalTTC",'fd.total_tva'=>"LineTotalVAT",'fd.product_type'=>'TypeOfLineServiceOrProduct','fd.fk_product'=>'ProductId',
  403. 'p.ref'=>'ProductRef','p.label'=>'ProductLabel','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  404. if (empty($conf->global->SUPPLIER_ORDER_DOUBLE_APPROVAL))
  405. {
  406. unset($this->export_fields_array['f.date_approve2']);
  407. unset($this->export_fields_array['ua2.login']);
  408. }
  409. $this->export_TypeFields_array[$r]=array('s.rowid'=>"company",'s.nom'=>'Text','s.address'=>'Text','s.cp'=>'Text','s.ville'=>'Text','c.code'=>'Text','s.tel'=>'Text','s.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.idprof5'=>'Text','s.idprof6'=>'Text','s.tva_intra'=>'Text','f.ref'=>"Text",'f.ref_supplier'=>"Text",'f.date_creation'=>"Date",'f.date_commande'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.tva'=>"Numeric",'f.fk_statut'=>'Status','f.date_approve'=>'Date','f.date_approve2'=>'Date','f.note_public'=>"Text",'f.note_private'=>"Text",'fd.description'=>"Text",'fd.tva_tx'=>"Numeric",'fd.qty'=>"Numeric",'fd.remise_percent'=>"Numeric",'fd.total_ht'=>"Numeric",'fd.total_ttc'=>"Numeric",'fd.total_tva'=>"Numeric",'fd.product_type'=>'Numeric','fd.fk_product'=>'List:product:label',
  410. 'p.ref'=>'Text','p.label'=>'Text','project.ref'=>'Text','project.title'=>'Text');
  411. $this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','c.code'=>'company','s.phone'=>'company','s.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.idprof5'=>'company','s.idprof6'=>'company','s.tva_intra'=>'company','ua1.login'=>'user','ua2.login'=>'user','fd.rowid'=>'order_line','fd.description'=>"order_line",'fd.tva_tx'=>"order_line",'fd.qty'=>"order_line",'fd.remise_percent'=>"order_line",'fd.total_ht'=>"order_line",'fd.total_ttc'=>"order_line",'fd.total_tva'=>"order_line",'fd.product_type'=>'order_line','fd.fk_product'=>'product',
  412. 'p.ref'=>'product','p.label'=>'product','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  413. $this->export_dependencies_array[$r]=array('order_line'=>'fd.rowid','product'=>'fd.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  414. // Add extra fields object
  415. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseur'";
  416. $resql=$this->db->query($sql);
  417. if ($resql) // This can fail when class is used on old database (during migration for example)
  418. {
  419. while ($obj=$this->db->fetch_object($resql))
  420. {
  421. $fieldname='extra.'.$obj->name;
  422. $fieldlabel=ucfirst($obj->label);
  423. $typeFilter="Text";
  424. switch($obj->type)
  425. {
  426. case 'int':
  427. case 'double':
  428. case 'price':
  429. $typeFilter="Numeric";
  430. break;
  431. case 'date':
  432. case 'datetime':
  433. $typeFilter="Date";
  434. break;
  435. case 'boolean':
  436. $typeFilter="Boolean";
  437. break;
  438. case 'sellist':
  439. $tmp='';
  440. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  441. $tmpkey=array_keys($tmpparam['options']);
  442. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift($tmpkey);
  443. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  444. break;
  445. }
  446. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  447. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  448. $this->export_entities_array[$r][$fieldname]='order';
  449. }
  450. }
  451. // End add extra fields object
  452. // Add extra fields line
  453. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseurdet'";
  454. $resql=$this->db->query($sql);
  455. if ($resql) // This can fail when class is used on old database (during migration for example)
  456. {
  457. while ($obj=$this->db->fetch_object($resql))
  458. {
  459. $fieldname='extraline.'.$obj->name;
  460. $fieldlabel=ucfirst($obj->label);
  461. $typeFilter="Text";
  462. switch($obj->type)
  463. {
  464. case 'int':
  465. case 'double':
  466. case 'price':
  467. $typeFilter="Numeric";
  468. break;
  469. case 'date':
  470. case 'datetime':
  471. $typeFilter="Date";
  472. break;
  473. case 'boolean':
  474. $typeFilter="Boolean";
  475. break;
  476. case 'sellist':
  477. $tmp='';
  478. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  479. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  480. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  481. break;
  482. }
  483. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  484. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  485. $this->export_entities_array[$r][$fieldname]='order_line';
  486. }
  487. }
  488. // End add extra fields line
  489. $this->export_sql_start[$r]='SELECT DISTINCT ';
  490. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  491. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  492. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande_fournisseur as f';
  493. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  494. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua1 ON ua1.rowid = f.fk_user_approve';
  495. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua2 ON ua2.rowid = f.fk_user_approve2';
  496. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur_extrafields as extra ON f.rowid = extra.fk_object,';
  497. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande_fournisseurdet as fd';
  498. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields as extraline ON fd.rowid = extraline.fk_object';
  499. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  500. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_commande';
  501. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_order',1).')';
  502. }
  503. /**
  504. * Function called when module is enabled.
  505. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  506. * It also creates data directories
  507. *
  508. * @param string $options Options when enabling module ('', 'noboxes')
  509. * @return int 1 if OK, 0 if KO
  510. */
  511. function init($options='')
  512. {
  513. global $conf;
  514. $this->remove($options);
  515. $sql = array(
  516. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->const[0][2]."' AND entity = ".$conf->entity,
  517. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->const[0][2]."','order_supplier',".$conf->entity.")",
  518. );
  519. return $this->_init($sql,$options);
  520. }
  521. }