modFournisseur.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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, $user;
  42. $this->db = $db;
  43. $this->numero = 40;
  44. // Family can be 'crm','financial','hr','projects','product','ecm','technic','other'
  45. // It is used to group modules in module setup page
  46. $this->family = "srm";
  47. $this->module_position = 10;
  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 fournisseurs";
  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='company';
  56. // Data directories to create when module is enabled
  57. $this->dirs = array("/fournisseur/temp",
  58. "/fournisseur/commande",
  59. "/fournisseur/commande/temp",
  60. "/fournisseur/facture",
  61. "/fournisseur/facture/temp"
  62. );
  63. // Dependencies
  64. $this->depends = array("modSociete");
  65. $this->requiredby = array();
  66. $this->langfiles = array('bills', 'companies', 'suppliers', 'orders');
  67. // Config pages
  68. $this->config_page_url = array("supplier_order.php");
  69. // Constants
  70. $this->const = array();
  71. $r=0;
  72. $this->const[$r][0] = "COMMANDE_SUPPLIER_ADDON_PDF";
  73. $this->const[$r][1] = "chaine";
  74. $this->const[$r][2] = "muscadet";
  75. $this->const[$r][3] = 'Nom du gestionnaire de generation des bons de commande en PDF';
  76. $this->const[$r][4] = 0;
  77. $r++;
  78. $this->const[$r][0] = "COMMANDE_SUPPLIER_ADDON_NUMBER";
  79. $this->const[$r][1] = "chaine";
  80. $this->const[$r][2] = "mod_commande_fournisseur_muguet";
  81. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des commandes fournisseur';
  82. $this->const[$r][4] = 0;
  83. $r++;
  84. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_PDF";
  85. $this->const[$r][1] = "chaine";
  86. $this->const[$r][2] = "canelle";
  87. $this->const[$r][3] = 'Nom du gestionnaire de generation des factures fournisseur en PDF';
  88. $this->const[$r][4] = 0;
  89. $r++;
  90. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_NUMBER";
  91. $this->const[$r][1] = "chaine";
  92. $this->const[$r][2] = "mod_facture_fournisseur_cactus";
  93. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des factures fournisseur';
  94. $this->const[$r][4] = 0;
  95. $r++;
  96. // Boxes
  97. $this->boxes = array(
  98. 0=>array('file'=>'box_graph_invoices_supplier_permonth.php','enabledbydefaulton'=>'Home'),
  99. 1=>array('file'=>'box_graph_orders_supplier_permonth.php','enabledbydefaulton'=>'Home'),
  100. 2=>array('file'=>'box_fournisseurs.php','enabledbydefaulton'=>'Home'),
  101. 3=>array('file'=>'box_factures_fourn_imp.php','enabledbydefaulton'=>'Home'),
  102. 4=>array('file'=>'box_factures_fourn.php','enabledbydefaulton'=>'Home'),
  103. 5=>array('file'=>'box_supplier_orders.php','enabledbydefaulton'=>'Home'),
  104. );
  105. // Permissions
  106. $this->rights = array();
  107. $this->rights_class = 'fournisseur';
  108. $r=0;
  109. $r++;
  110. $this->rights[$r][0] = 1181;
  111. $this->rights[$r][1] = 'Consulter les fournisseurs';
  112. $this->rights[$r][2] = 'r';
  113. $this->rights[$r][3] = 1;
  114. $this->rights[$r][4] = 'lire';
  115. $r++;
  116. $this->rights[$r][0] = 1182;
  117. $this->rights[$r][1] = 'Consulter les commandes fournisseur';
  118. $this->rights[$r][2] = 'r';
  119. $this->rights[$r][3] = 1;
  120. $this->rights[$r][4] = 'commande';
  121. $this->rights[$r][5] = 'lire';
  122. $r++;
  123. $this->rights[$r][0] = 1183;
  124. $this->rights[$r][1] = 'Creer une commande fournisseur';
  125. $this->rights[$r][2] = 'w';
  126. $this->rights[$r][3] = 0;
  127. $this->rights[$r][4] = 'commande';
  128. $this->rights[$r][5] = 'creer';
  129. $r++;
  130. $this->rights[$r][0] = 1184;
  131. $this->rights[$r][1] = 'Valider une commande fournisseur';
  132. $this->rights[$r][2] = 'w';
  133. $this->rights[$r][3] = 0;
  134. $this->rights[$r][4] = 'supplier_order_advance';
  135. $this->rights[$r][5] = 'validate';
  136. $r++;
  137. $this->rights[$r][0] = 1185;
  138. $this->rights[$r][1] = 'Approuver une commande fournisseur';
  139. $this->rights[$r][2] = 'w';
  140. $this->rights[$r][3] = 0;
  141. $this->rights[$r][4] = 'commande';
  142. $this->rights[$r][5] = 'approuver';
  143. /*$r++;
  144. $this->rights[$r][0] = 1191;
  145. $this->rights[$r][1] = 'Approuver une commande fournisseur (si supérieur hiérarchique)';
  146. $this->rights[$r][2] = 'w';
  147. $this->rights[$r][3] = 0;
  148. $this->rights[$r][4] = 'commande';
  149. $this->rights[$r][5] = 'approve_ifsupervisor_advance';*/
  150. $r++;
  151. $this->rights[$r][0] = 1186;
  152. $this->rights[$r][1] = 'Commander une commande fournisseur';
  153. $this->rights[$r][2] = 'w';
  154. $this->rights[$r][3] = 0;
  155. $this->rights[$r][4] = 'commande';
  156. $this->rights[$r][5] = 'commander';
  157. $r++;
  158. $this->rights[$r][0] = 1187;
  159. $this->rights[$r][1] = 'Receptionner une commande fournisseur';
  160. $this->rights[$r][2] = 'd';
  161. $this->rights[$r][3] = 0;
  162. $this->rights[$r][4] = 'commande';
  163. $this->rights[$r][5] = 'receptionner';
  164. $r++;
  165. $this->rights[$r][0] = 1189;
  166. $this->rights[$r][1] = 'Check/Uncheck a supplier order reception';
  167. $this->rights[$r][2] = 'w';
  168. $this->rights[$r][3] = 0;
  169. $this->rights[$r][4] = 'commande_advance';
  170. $this->rights[$r][5] = 'check';
  171. $r++;
  172. $this->rights[$r][0] = 1188;
  173. $this->rights[$r][1] = 'Supprimer une commande fournisseur';
  174. $this->rights[$r][2] = 'd';
  175. $this->rights[$r][3] = 0;
  176. $this->rights[$r][4] = 'commande';
  177. $this->rights[$r][5] = 'supprimer';
  178. $r++;
  179. $this->rights[$r][0] = 1231;
  180. $this->rights[$r][1] = 'Consulter les factures fournisseur';
  181. $this->rights[$r][2] = 'r';
  182. $this->rights[$r][3] = 1;
  183. $this->rights[$r][4] = 'facture';
  184. $this->rights[$r][5] = 'lire';
  185. $r++;
  186. $this->rights[$r][0] = 1232;
  187. $this->rights[$r][1] = 'Creer une facture fournisseur';
  188. $this->rights[$r][2] = 'w';
  189. $this->rights[$r][3] = 0;
  190. $this->rights[$r][4] = 'facture';
  191. $this->rights[$r][5] = 'creer';
  192. $r++;
  193. $this->rights[$r][0] = 1233;
  194. $this->rights[$r][1] = 'Valider une facture fournisseur';
  195. $this->rights[$r][2] = 'w';
  196. $this->rights[$r][3] = 0;
  197. $this->rights[$r][4] = 'supplier_invoice_advance';
  198. $this->rights[$r][5] = 'validate';
  199. $r++;
  200. $this->rights[$r][0] = 1234;
  201. $this->rights[$r][1] = 'Supprimer une facture fournisseur';
  202. $this->rights[$r][2] = 'd';
  203. $this->rights[$r][3] = 0;
  204. $this->rights[$r][4] = 'facture';
  205. $this->rights[$r][5] = 'supprimer';
  206. $r++;
  207. $this->rights[$r][0] = 1235;
  208. $this->rights[$r][1] = 'Envoyer les factures par mail';
  209. $this->rights[$r][2] = 'a';
  210. $this->rights[$r][3] = 0;
  211. $this->rights[$r][4] = 'supplier_invoice_advance';
  212. $this->rights[$r][5] = 'send';
  213. $r++;
  214. $this->rights[$r][0] = 1236;
  215. $this->rights[$r][1] = 'Exporter les factures fournisseurs, attributs et reglements';
  216. $this->rights[$r][2] = 'r';
  217. $this->rights[$r][3] = 0;
  218. $this->rights[$r][4] = 'facture';
  219. $this->rights[$r][5] = 'export';
  220. $r++;
  221. $this->rights[$r][0] = 1237;
  222. $this->rights[$r][1] = 'Exporter les commande fournisseurs, attributs';
  223. $this->rights[$r][2] = 'r';
  224. $this->rights[$r][3] = 0;
  225. $this->rights[$r][4] = 'commande';
  226. $this->rights[$r][5] = 'export';
  227. if (! empty($conf->global->SUPPLIER_ORDER_DOUBLE_APPROVAL))
  228. {
  229. $r++;
  230. $this->rights[$r][0] = 1190;
  231. $this->rights[$r][1] = 'Approve supplier order (second level)'; // $langs->trans("Permission1190");
  232. $this->rights[$r][2] = 'w';
  233. $this->rights[$r][3] = 0;
  234. $this->rights[$r][4] = 'commande';
  235. $this->rights[$r][5] = 'approve2';
  236. }
  237. // Exports
  238. //--------
  239. $r=0;
  240. $r++;
  241. $this->export_code[$r]=$this->rights_class.'_'.$r;
  242. $this->export_label[$r]='Factures fournisseurs et lignes de facture';
  243. $this->export_icon[$r]='bill';
  244. $this->export_permission[$r]=array(array("fournisseur","facture","export"));
  245. $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',
  246. 'p.ref'=>'ProductRef','p.label'=>'ProductLabel','p.accountancy_code_buy'=>'ProductAccountancyBuyCode','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  247. //$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');
  248. $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',
  249. 'p.ref'=>'Text','p.label'=>'Text','project.ref'=>'Text','project.title'=>'Text');
  250. $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',
  251. 'p.ref'=>'product','p.label'=>'product','p.accountancy_code_buy'=>'product','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  252. $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
  253. // Add extra fields object
  254. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn'";
  255. $resql=$this->db->query($sql);
  256. if ($resql) // This can fail when class is used on old database (during migration for example)
  257. {
  258. while ($obj=$this->db->fetch_object($resql))
  259. {
  260. $fieldname='extra.'.$obj->name;
  261. $fieldlabel=ucfirst($obj->label);
  262. $typeFilter="Text";
  263. switch($obj->type)
  264. {
  265. case 'int':
  266. case 'double':
  267. case 'price':
  268. $typeFilter="Numeric";
  269. break;
  270. case 'date':
  271. case 'datetime':
  272. $typeFilter="Date";
  273. break;
  274. case 'boolean':
  275. $typeFilter="Boolean";
  276. break;
  277. case 'sellist':
  278. $tmp='';
  279. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  280. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  281. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  282. break;
  283. }
  284. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  285. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  286. $this->export_entities_array[$r][$fieldname]='invoice';
  287. }
  288. }
  289. // End add extra fields
  290. // Add extra fields line
  291. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn_det'";
  292. $resql=$this->db->query($sql);
  293. if ($resql) // This can fail when class is used on old database (during migration for example)
  294. {
  295. while ($obj=$this->db->fetch_object($resql))
  296. {
  297. $fieldname='extraline.'.$obj->name;
  298. $fieldlabel=ucfirst($obj->label);
  299. $typeFilter="Text";
  300. switch($obj->type)
  301. {
  302. case 'int':
  303. case 'double':
  304. case 'price':
  305. $typeFilter="Numeric";
  306. break;
  307. case 'date':
  308. case 'datetime':
  309. $typeFilter="Date";
  310. break;
  311. case 'boolean':
  312. $typeFilter="Boolean";
  313. break;
  314. case 'sellist':
  315. $tmp='';
  316. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  317. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  318. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  319. break;
  320. }
  321. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  322. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  323. $this->export_entities_array[$r][$fieldname]='invoice_line';
  324. }
  325. }
  326. // End add extra fields line
  327. $this->export_sql_start[$r]='SELECT DISTINCT ';
  328. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  329. 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';
  330. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  331. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'facture_fourn as f';
  332. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  333. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  334. $this->export_sql_end[$r] .=' , '.MAIN_DB_PREFIX.'facture_fourn_det as fd';
  335. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_det_extrafields as extraline ON fd.rowid = extraline.fk_object';
  336. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  337. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture_fourn';
  338. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_invoice',1).')';
  339. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
  340. $r++;
  341. $this->export_code[$r]=$this->rights_class.'_'.$r;
  342. $this->export_label[$r]='Factures fournisseurs et reglements';
  343. $this->export_icon[$r]='bill';
  344. $this->export_permission[$r]=array(array("fournisseur","facture","export"));
  345. $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',
  346. 'p.datep'=>'DatePayment','p.num_paiement'=>'PaymentNumber','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  347. //$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');
  348. $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',
  349. 'p.datep'=>'Date','p.num_paiement'=>'Numeric','project.ref'=>'Text','project.title'=>'Text');
  350. $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',
  351. 'p.datep'=>'payment','p.num_paiement'=>'payment','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  352. $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
  353. // Add extra fields object
  354. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn'";
  355. $resql=$this->db->query($sql);
  356. if ($resql) // This can fail when class is used on old database (during migration for example)
  357. {
  358. while ($obj=$this->db->fetch_object($resql))
  359. {
  360. $fieldname='extra.'.$obj->name;
  361. $fieldlabel=ucfirst($obj->label);
  362. $typeFilter="Text";
  363. switch($obj->type)
  364. {
  365. case 'int':
  366. case 'double':
  367. case 'price':
  368. $typeFilter="Numeric";
  369. break;
  370. case 'date':
  371. case 'datetime':
  372. $typeFilter="Date";
  373. break;
  374. case 'boolean':
  375. $typeFilter="Boolean";
  376. break;
  377. case 'sellist':
  378. $tmp='';
  379. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  380. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  381. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  382. break;
  383. }
  384. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  385. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  386. $this->export_entities_array[$r][$fieldname]='invoice';
  387. }
  388. }
  389. // End add extra fields object
  390. $this->export_sql_start[$r]='SELECT DISTINCT ';
  391. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  392. 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';
  393. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  394. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'facture_fourn as f';
  395. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  396. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  397. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
  398. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn as p ON pf.fk_paiementfourn = p.rowid';
  399. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid';
  400. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_invoice',1).')';
  401. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
  402. // Order
  403. $r++;
  404. $this->export_code[$r]=$this->rights_class.'_'.$r;
  405. $this->export_label[$r]='Commandes fournisseurs et lignes de commandes';
  406. $this->export_icon[$r]='order';
  407. $this->export_permission[$r]=array(array("fournisseur","commande","export"));
  408. $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',
  409. 'p.ref'=>'ProductRef','p.label'=>'ProductLabel','project.rowid'=>'ProjectId','project.ref'=>'ProjectRef','project.title'=>'ProjectLabel');
  410. if (empty($conf->global->SUPPLIER_ORDER_DOUBLE_APPROVAL))
  411. {
  412. unset($this->export_fields_array['f.date_approve2']);
  413. unset($this->export_fields_array['ua2.login']);
  414. }
  415. $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',
  416. 'p.ref'=>'Text','p.label'=>'Text','project.ref'=>'Text','project.title'=>'Text');
  417. $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',
  418. 'p.ref'=>'product','p.label'=>'product','project.rowid'=>'project','project.ref'=>'project','project.title'=>'project');
  419. $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
  420. // Add extra fields object
  421. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseur'";
  422. $resql=$this->db->query($sql);
  423. if ($resql) // This can fail when class is used on old database (during migration for example)
  424. {
  425. while ($obj=$this->db->fetch_object($resql))
  426. {
  427. $fieldname='extra.'.$obj->name;
  428. $fieldlabel=ucfirst($obj->label);
  429. $typeFilter="Text";
  430. switch($obj->type)
  431. {
  432. case 'int':
  433. case 'double':
  434. case 'price':
  435. $typeFilter="Numeric";
  436. break;
  437. case 'date':
  438. case 'datetime':
  439. $typeFilter="Date";
  440. break;
  441. case 'boolean':
  442. $typeFilter="Boolean";
  443. break;
  444. case 'sellist':
  445. $tmp='';
  446. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  447. $tmpkey=array_keys($tmpparam['options']);
  448. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift($tmpkey);
  449. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  450. break;
  451. }
  452. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  453. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  454. $this->export_entities_array[$r][$fieldname]='order';
  455. }
  456. }
  457. // End add extra fields object
  458. // Add extra fields line
  459. $sql="SELECT name, label, type, param FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseurdet'";
  460. $resql=$this->db->query($sql);
  461. if ($resql) // This can fail when class is used on old database (during migration for example)
  462. {
  463. while ($obj=$this->db->fetch_object($resql))
  464. {
  465. $fieldname='extraline.'.$obj->name;
  466. $fieldlabel=ucfirst($obj->label);
  467. $typeFilter="Text";
  468. switch($obj->type)
  469. {
  470. case 'int':
  471. case 'double':
  472. case 'price':
  473. $typeFilter="Numeric";
  474. break;
  475. case 'date':
  476. case 'datetime':
  477. $typeFilter="Date";
  478. break;
  479. case 'boolean':
  480. $typeFilter="Boolean";
  481. break;
  482. case 'sellist':
  483. $tmp='';
  484. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  485. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  486. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  487. break;
  488. }
  489. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  490. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  491. $this->export_entities_array[$r][$fieldname]='order_line';
  492. }
  493. }
  494. // End add extra fields line
  495. $this->export_sql_start[$r]='SELECT DISTINCT ';
  496. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'societe as s';
  497. 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';
  498. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  499. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande_fournisseur as f';
  500. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  501. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua1 ON ua1.rowid = f.fk_user_approve';
  502. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua2 ON ua2.rowid = f.fk_user_approve2';
  503. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur_extrafields as extra ON f.rowid = extra.fk_object,';
  504. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'commande_fournisseurdet as fd';
  505. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields as extraline ON fd.rowid = extraline.fk_object';
  506. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  507. $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_commande';
  508. $this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('supplier_order',1).')';
  509. if(!$user->rights->societe->client->voir) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
  510. }
  511. /**
  512. * Function called when module is enabled.
  513. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  514. * It also creates data directories
  515. *
  516. * @param string $options Options when enabling module ('', 'noboxes')
  517. * @return int 1 if OK, 0 if KO
  518. */
  519. function init($options='')
  520. {
  521. global $conf;
  522. $this->remove($options);
  523. $sql = array(
  524. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->const[0][2]."' AND entity = ".$conf->entity,
  525. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->const[0][2]."','order_supplier',".$conf->entity.")",
  526. );
  527. return $this->_init($sql,$options);
  528. }
  529. }