modFournisseur.class.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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@inodbox.com>
  5. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2013-2015 Philippe Grand <philippe.grand@atoo-net.com>
  7. * Copyright (C) 2020 Ahmad Jamaly Rabib <rabib@metroworks.co.jp>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \defgroup fournisseur Module suppliers
  24. * \file htdocs/core/modules/modFournisseur.class.php
  25. * \ingroup fournisseur
  26. * \brief Description and activation file for the module Supplier
  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. public 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 = '12';
  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->picto = 'company';
  55. // Data directories to create when module is enabled
  56. $this->dirs = array(
  57. "/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("modSupplierProposal");
  66. $this->langfiles = array('bills', 'companies', 'suppliers', 'orders', 'sendings');
  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. /* For supplier invoice, we must not have default pdf template on. In most cases, we need to join PDF from supplier, not have a document generated.
  85. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_PDF";
  86. $this->const[$r][1] = "chaine";
  87. $this->const[$r][2] = "canelle";
  88. $this->const[$r][3] = 'Nom du gestionnaire de generation des factures fournisseur en PDF';
  89. $this->const[$r][4] = 0;
  90. $r++;
  91. */
  92. $this->const[$r][0] = "INVOICE_SUPPLIER_ADDON_NUMBER";
  93. $this->const[$r][1] = "chaine";
  94. $this->const[$r][2] = "mod_facture_fournisseur_cactus";
  95. $this->const[$r][3] = 'Nom du gestionnaire de numerotation des factures fournisseur';
  96. $this->const[$r][4] = 0;
  97. $r++;
  98. $this->const[$r][0] = "SUPPLIER_ORDER_ADDON_PDF_ODT_PATH";
  99. $this->const[$r][1] = "chaine";
  100. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/supplier_orders";
  101. $this->const[$r][3] = '';
  102. $this->const[$r][4] = 0;
  103. $r++;
  104. // Boxes
  105. $this->boxes = array(
  106. 0=>array('file'=>'box_graph_invoices_supplier_permonth.php', 'enabledbydefaulton'=>'Home'),
  107. 1=>array('file'=>'box_graph_orders_supplier_permonth.php', 'enabledbydefaulton'=>'Home'),
  108. 2=>array('file'=>'box_fournisseurs.php', 'enabledbydefaulton'=>'Home'),
  109. 3=>array('file'=>'box_factures_fourn_imp.php', 'enabledbydefaulton'=>'Home'),
  110. 4=>array('file'=>'box_factures_fourn.php', 'enabledbydefaulton'=>'Home'),
  111. 5=>array('file'=>'box_supplier_orders.php', 'enabledbydefaulton'=>'Home'),
  112. 6=>array('file'=>'box_supplier_orders_awaiting_reception.php', 'enabledbydefaulton'=>'Home'),
  113. );
  114. // Permissions
  115. $this->rights = array();
  116. $this->rights_class = 'fournisseur';
  117. $r = 0;
  118. $r++;
  119. $this->rights[$r][0] = 1181;
  120. $this->rights[$r][1] = 'Consulter les fournisseurs';
  121. $this->rights[$r][2] = 'r';
  122. $this->rights[$r][3] = 0;
  123. $this->rights[$r][4] = 'lire';
  124. $r++;
  125. $this->rights[$r][0] = 1182;
  126. $this->rights[$r][1] = 'Consulter les commandes fournisseur';
  127. $this->rights[$r][2] = 'r';
  128. $this->rights[$r][3] = 0;
  129. $this->rights[$r][4] = 'commande';
  130. $this->rights[$r][5] = 'lire';
  131. $r++;
  132. $this->rights[$r][0] = 1183;
  133. $this->rights[$r][1] = 'Creer une commande fournisseur';
  134. $this->rights[$r][2] = 'w';
  135. $this->rights[$r][3] = 0;
  136. $this->rights[$r][4] = 'commande';
  137. $this->rights[$r][5] = 'creer';
  138. $r++;
  139. $this->rights[$r][0] = 1184;
  140. $this->rights[$r][1] = 'Valider une commande fournisseur';
  141. $this->rights[$r][2] = 'w';
  142. $this->rights[$r][3] = 0;
  143. $this->rights[$r][4] = 'supplier_order_advance';
  144. $this->rights[$r][5] = 'validate';
  145. $r++;
  146. $this->rights[$r][0] = 1185;
  147. $this->rights[$r][1] = 'Approuver une commande fournisseur';
  148. $this->rights[$r][2] = 'w';
  149. $this->rights[$r][3] = 0;
  150. $this->rights[$r][4] = 'commande';
  151. $this->rights[$r][5] = 'approuver';
  152. $r++;
  153. $this->rights[$r][0] = 1186;
  154. $this->rights[$r][1] = 'Commander une commande fournisseur';
  155. $this->rights[$r][2] = 'w';
  156. $this->rights[$r][3] = 0;
  157. $this->rights[$r][4] = 'commande';
  158. $this->rights[$r][5] = 'commander';
  159. $r++;
  160. $this->rights[$r][0] = 1187;
  161. $this->rights[$r][1] = 'Receptionner une commande fournisseur';
  162. $this->rights[$r][2] = 'd';
  163. $this->rights[$r][3] = 0;
  164. $this->rights[$r][4] = 'commande';
  165. $this->rights[$r][5] = 'receptionner';
  166. $r++;
  167. $this->rights[$r][0] = 1189;
  168. $this->rights[$r][1] = 'Check/Uncheck a supplier order reception';
  169. $this->rights[$r][2] = 'w';
  170. $this->rights[$r][3] = 0;
  171. $this->rights[$r][4] = 'commande_advance';
  172. $this->rights[$r][5] = 'check';
  173. $r++;
  174. $this->rights[$r][0] = 1188;
  175. $this->rights[$r][1] = 'Supprimer une commande fournisseur';
  176. $this->rights[$r][2] = 'd';
  177. $this->rights[$r][3] = 0;
  178. $this->rights[$r][4] = 'commande';
  179. $this->rights[$r][5] = 'supprimer';
  180. if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED)) {
  181. $r++;
  182. $this->rights[$r][0] = 1190;
  183. $this->rights[$r][1] = 'Approve supplier order (second level)'; // $langs->trans("Permission1190");
  184. $this->rights[$r][2] = 'w';
  185. $this->rights[$r][3] = 0;
  186. $this->rights[$r][4] = 'commande';
  187. $this->rights[$r][5] = 'approve2';
  188. }
  189. $r++;
  190. $this->rights[$r][0] = 1191;
  191. $this->rights[$r][1] = 'Exporter les commande fournisseurs, attributs';
  192. $this->rights[$r][2] = 'r';
  193. $this->rights[$r][3] = 0;
  194. $this->rights[$r][4] = 'commande';
  195. $this->rights[$r][5] = 'export';
  196. $r++;
  197. $this->rights[$r][0] = 1231;
  198. $this->rights[$r][1] = 'Consulter les factures fournisseur';
  199. $this->rights[$r][2] = 'r';
  200. $this->rights[$r][3] = 0;
  201. $this->rights[$r][4] = 'facture';
  202. $this->rights[$r][5] = 'lire';
  203. $r++;
  204. $this->rights[$r][0] = 1232;
  205. $this->rights[$r][1] = 'Creer une facture fournisseur';
  206. $this->rights[$r][2] = 'w';
  207. $this->rights[$r][3] = 0;
  208. $this->rights[$r][4] = 'facture';
  209. $this->rights[$r][5] = 'creer';
  210. $r++;
  211. $this->rights[$r][0] = 1233;
  212. $this->rights[$r][1] = 'Valider une facture fournisseur';
  213. $this->rights[$r][2] = 'w';
  214. $this->rights[$r][3] = 0;
  215. $this->rights[$r][4] = 'supplier_invoice_advance';
  216. $this->rights[$r][5] = 'validate';
  217. $r++;
  218. $this->rights[$r][0] = 1234;
  219. $this->rights[$r][1] = 'Supprimer une facture fournisseur';
  220. $this->rights[$r][2] = 'd';
  221. $this->rights[$r][3] = 0;
  222. $this->rights[$r][4] = 'facture';
  223. $this->rights[$r][5] = 'supprimer';
  224. $r++;
  225. $this->rights[$r][0] = 1235;
  226. $this->rights[$r][1] = 'Envoyer les factures par mail';
  227. $this->rights[$r][2] = 'a';
  228. $this->rights[$r][3] = 0;
  229. $this->rights[$r][4] = 'supplier_invoice_advance';
  230. $this->rights[$r][5] = 'send';
  231. $r++;
  232. $this->rights[$r][0] = 1236;
  233. $this->rights[$r][1] = 'Exporter les factures fournisseurs, attributs et reglements';
  234. $this->rights[$r][2] = 'r';
  235. $this->rights[$r][3] = 0;
  236. $this->rights[$r][4] = 'facture';
  237. $this->rights[$r][5] = 'export';
  238. // Menus
  239. //-------
  240. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  241. // Exports
  242. //--------
  243. $r = 0;
  244. $r++;
  245. $this->export_code[$r] = $this->rights_class.'_'.$r;
  246. $this->export_label[$r] = 'Vendor invoices and lines of invoices';
  247. $this->export_icon[$r] = 'invoice';
  248. $this->export_permission[$r] = array(array("fournisseur", "facture", "export"));
  249. $this->export_fields_array[$r] = array(
  250. 's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 'ps.nom'=>'ParentCompany', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 's.phone'=>'Phone',
  251. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.idprof5'=>'ProfId5', 's.idprof6'=>'ProfId6',
  252. 's.code_compta'=>'CustomerAccountancyCode', 's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
  253. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_supplier'=>"RefSupplier", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>'DateMaxPayment',
  254. 'f.total_ht'=>"TotalHT", 'f.total_ttc'=>"TotalTTC", 'f.total_tva'=>"TotalVAT", 'f.paye'=>"InvoicePaid", 'f.fk_statut'=>'InvoiceStatus', 'f.note_public'=>"InvoiceNote",
  255. 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription", 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.remise_percent'=>"Discount", 'fd.total_ht'=>"LineTotalHT",
  256. 'fd.total_ttc'=>"LineTotalTTC", 'fd.tva'=>"LineTotalVAT", 'fd.product_type'=>'TypeOfLineServiceOrProduct', 'fd.fk_product'=>'ProductId',
  257. 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel', 'p.accountancy_code_buy'=>'ProductAccountancyBuyCode', 'project.rowid'=>'ProjectId',
  258. 'project.ref'=>'ProjectRef', 'project.title'=>'ProjectLabel'
  259. );
  260. if (!empty($conf->multicurrency->enabled)) {
  261. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  262. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  263. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  264. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  265. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  266. }
  267. //$this->export_TypeFields_array[$r]=array(
  268. // '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',
  269. // '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",
  270. // '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",
  271. // 'fd.tva'=>"Numeric",'fd.product_type'=>'Numeric','fd.fk_product'=>'List:product:label','p.ref'=>'Text','p.label'=>'Text'
  272. //);
  273. $this->export_TypeFields_array[$r] = array(
  274. 's.nom'=>'Text', 'ps.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.idprof5'=>'Text', 's.idprof6'=>'Text',
  275. 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text', 'f.ref'=>"Text", 'f.ref_supplier'=>"Text", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>'Date',
  276. '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",
  277. 'fd.qty'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.tva'=>"Numeric", 'fd.product_type'=>'Numeric', 'fd.fk_product'=>'List:product:label',
  278. 'p.ref'=>'Text', 'p.label'=>'Text', 'project.ref'=>'Text', 'project.title'=>'Text'
  279. );
  280. $this->export_entities_array[$r] = array(
  281. 's.rowid'=>"company", 's.nom'=>'company', 'ps.nom'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 's.phone'=>'company', 's.siren'=>'company', 's.siret'=>'company',
  282. 's.ape'=>'company', 's.idprof4'=>'company', 's.idprof5'=>'company', 's.idprof6'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company', 's.tva_intra'=>'company', 'f.rowid'=>"invoice",
  283. 'f.ref'=>"invoice", 'f.ref_supplier'=>"invoice", 'f.datec'=>"invoice", 'f.datef'=>"invoice", 'f.date_lim_reglement'=>'invoice', 'f.total_ht'=>"invoice", 'f.total_ttc'=>"invoice", 'f.total_tva'=>"invoice",
  284. '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",
  285. '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',
  286. 'p.ref'=>'product', 'p.label'=>'product', 'p.accountancy_code_buy'=>'product', 'project.rowid'=>'project', 'project.ref'=>'project', 'project.title'=>'project'
  287. );
  288. $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
  289. // Add extra fields object
  290. $keyforselect = 'facture_fourn';
  291. $keyforelement = 'invoice';
  292. $keyforaliasextra = 'extra';
  293. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  294. $keyforselect = 'facture_fourn_det';
  295. $keyforelement = 'invoice_line';
  296. $keyforaliasextra = 'extraline';
  297. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  298. // End add extra fields line
  299. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  300. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  301. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as ps ON ps.rowid = s.parent';
  302. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  303. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  304. }
  305. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  306. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture_fourn as f';
  307. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  308. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  309. $this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'facture_fourn_det as fd';
  310. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_det_extrafields as extraline ON fd.rowid = extraline.fk_object';
  311. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  312. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture_fourn';
  313. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('supplier_invoice').')';
  314. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  315. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.$user->id;
  316. }
  317. $r++;
  318. $this->export_code[$r] = $this->rights_class.'_'.$r;
  319. $this->export_label[$r] = 'Factures fournisseurs et reglements';
  320. $this->export_icon[$r] = 'invoice';
  321. $this->export_permission[$r] = array(array("fournisseur", "facture", "export"));
  322. $this->export_fields_array[$r] = array(
  323. 's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 's.phone'=>'Phone',
  324. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.idprof5'=>'ProfId5', 's.idprof6'=>'ProfId6',
  325. 's.code_compta'=>'CustomerAccountancyCode', 's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
  326. 'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_supplier'=>"RefSupplier", 'f.datec'=>"InvoiceDateCreation",
  327. 'f.datef'=>"DateInvoice", 'f.total_ht'=>"TotalHT", 'f.total_ttc'=>"TotalTTC", 'f.total_tva'=>"TotalVAT", 'f.paye'=>"InvoicePaid",
  328. 'f.fk_statut'=>'InvoiceStatus', 'f.note_public'=>"InvoiceNote", 'p.rowid'=>'PaymentId', 'pf.amount'=>'AmountPayment',
  329. 'p.datep'=>'DatePayment', 'p.num_paiement'=>'PaymentNumber', 'p.fk_bank'=>'IdTransaction', 'project.rowid'=>'ProjectId', 'project.ref'=>'ProjectRef', 'project.title'=>'ProjectLabel'
  330. );
  331. if (!empty($conf->multicurrency->enabled)) {
  332. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  333. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  334. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  335. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  336. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  337. }
  338. //$this->export_TypeFields_array[$r]=array(
  339. // 's.rowid'=>"List:societe:CompanyName",'s.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text',
  340. // '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",
  341. // 'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_public'=>"Text",
  342. // 'pf.amount'=>'Numeric','p.datep'=>'Date','p.num_paiement'=>'Numeric'
  343. //);
  344. $this->export_TypeFields_array[$r] = array(
  345. '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',
  346. 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text', 'f.ref'=>"Text", 'f.ref_supplier'=>"Text", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.total_ht'=>"Numeric",
  347. 'f.total_ttc'=>"Numeric", 'f.total_tva'=>"Numeric", 'f.paye'=>"Boolean", 'f.fk_statut'=>'Status', 'f.note_public'=>"Text", 'pf.amount'=>'Numeric',
  348. 'p.datep'=>'Date', 'p.num_paiement'=>'Numeric', 'p.fk_bank'=>'Numeric', 'project.ref'=>'Text', 'project.title'=>'Text'
  349. );
  350. $this->export_entities_array[$r] = array(
  351. 's.rowid'=>"company", 's.nom'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 's.phone'=>'company',
  352. 's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.idprof5'=>'company', 's.idprof6'=>'company',
  353. 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company', 's.tva_intra'=>'company',
  354. 'f.rowid'=>"invoice", 'f.ref'=>"invoice", 'f.ref_supplier'=>"invoice", 'f.datec'=>"invoice", 'f.datef'=>"invoice", 'f.total_ht'=>"invoice",
  355. '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',
  356. 'p.datep'=>'payment', 'p.num_paiement'=>'payment', 'p.fk_bank'=>'account', 'project.rowid'=>'project', 'project.ref'=>'project', 'project.title'=>'project');
  357. $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
  358. // Add extra fields object
  359. $keyforselect = 'facture_fourn';
  360. $keyforelement = 'invoice';
  361. $keyforaliasextra = 'extra';
  362. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  363. // End add extra fields object
  364. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  365. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  366. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  367. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  368. }
  369. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  370. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture_fourn as f';
  371. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  372. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn_extrafields as extra ON f.rowid = extra.fk_object';
  373. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
  374. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn as p ON pf.fk_paiementfourn = p.rowid';
  375. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid';
  376. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('supplier_invoice').')';
  377. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  378. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.$user->id;
  379. }
  380. // Order
  381. $r++;
  382. $this->export_code[$r] = $this->rights_class.'_'.$r;
  383. $this->export_label[$r] = 'Purchase Orders and lines of purchase orders';
  384. $this->export_icon[$r] = 'order';
  385. $this->export_permission[$r] = array(array("fournisseur", "commande", "export"));
  386. $this->export_fields_array[$r] = array(
  387. 's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 'ps.nom'=>'ParentCompany', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 's.phone'=>'Phone',
  388. 's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.idprof5'=>'ProfId5', 's.idprof6'=>'ProfId6', 's.tva_intra'=>'VATIntra',
  389. 'f.rowid'=>"OrderId", 'f.ref'=>"Ref", 'f.ref_supplier'=>"RefSupplier", 'f.date_creation'=>"DateCreation", 'f.date_commande'=>"OrderDate", 'f.date_livraison'=>"DateDeliveryPlanned",
  390. 'f.total_ht'=>"TotalHT", 'f.total_ttc'=>"TotalTTC", 'f.total_tva'=>"TotalVAT", 'f.fk_statut'=>'Status', 'f.date_approve'=>'DateApprove', 'f.date_approve2'=>'DateApprove2',
  391. 'f.note_public'=>"NotePublic", 'f.note_private'=>"NotePrivate", 'ua1.login'=>'ApprovedBy', 'ua2.login'=>'ApprovedBy2', 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription",
  392. 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.remise_percent'=>"Discount", 'fd.total_ht'=>"LineTotalHT", 'fd.total_ttc'=>"LineTotalTTC",
  393. 'fd.total_tva'=>"LineTotalVAT", 'fd.product_type'=>'TypeOfLineServiceOrProduct', 'fd.ref'=>'RefSupplier', 'fd.fk_product'=>'ProductId',
  394. // Added by MMI Mathieu Moulin iProspective : add BarCodeValue
  395. 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel', 'p.barcode'=>'BarcodeValue', 'project.rowid'=>'ProjectId', 'project.ref'=>'ProjectRef', 'project.title'=>'ProjectLabel'
  396. );
  397. if (!empty($conf->multicurrency->enabled)) {
  398. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  399. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  400. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  401. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  402. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  403. }
  404. if (empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED)) {
  405. unset($this->export_fields_array['f.date_approve2']);
  406. unset($this->export_fields_array['ua2.login']);
  407. }
  408. $this->export_TypeFields_array[$r] = array(
  409. 's.rowid'=>"company", 's.nom'=>'Text', 'ps.nom'=>'Text', 's.address'=>'Text', 's.cp'=>'Text', 's.ville'=>'Text', 'c.code'=>'Text', 's.tel'=>'Text', 's.siren'=>'Text',
  410. '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",
  411. 'f.date_creation'=>"Date", 'f.date_commande'=>"Date", 'f.date_livraison'=>"Date", 'f.total_ht'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.total_tva'=>"Numeric",
  412. 'f.fk_statut'=>'Status', 'f.date_approve'=>'Date', 'f.date_approve2'=>'Date', 'f.note_public'=>"Text", 'f.note_private'=>"Text", 'fd.description'=>"Text",
  413. 'fd.tva_tx'=>"Numeric", 'fd.qty'=>"Numeric", 'fd.remise_percent'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.total_tva'=>"Numeric",
  414. // Added by MMI Mathieu Moulin iProspective : add BarCodeValue
  415. 'fd.product_type'=>'Numeric', 'fd.ref'=>'Text', 'fd.fk_product'=>'List:product:label', 'p.ref'=>'Text', 'p.label'=>'Text', 'p.barcode'=>'Text', 'project.ref'=>'Text', 'project.title'=>'Text'
  416. );
  417. $this->export_entities_array[$r] = array(
  418. 's.rowid'=>"company", 's.nom'=>'company', 'ps.nom'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 's.phone'=>'company', 's.siren'=>'company',
  419. 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.idprof5'=>'company', 's.idprof6'=>'company', 's.tva_intra'=>'company', 'ua1.login'=>'user',
  420. '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",
  421. 'fd.total_ht'=>"order_line", 'fd.total_ttc'=>"order_line", 'fd.total_tva'=>"order_line", 'fd.product_type'=>'order_line', 'fd.ref'=>'order_line', 'fd.fk_product'=>'product',
  422. // Added by MMI Mathieu Moulin iProspective : add BarCodeValue
  423. 'p.ref'=>'product', 'p.label'=>'product', 'p.barcode'=>'product', 'project.rowid'=>'project', 'project.ref'=>'project', 'project.title'=>'project'
  424. );
  425. $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
  426. // Add extra fields object
  427. $keyforselect = 'commande_fournisseur';
  428. $keyforelement = 'order';
  429. $keyforaliasextra = 'extra';
  430. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  431. // End add extra fields object
  432. // Add extra fields line
  433. $keyforselect = 'commande_fournisseurdet';
  434. $keyforelement = 'order_line';
  435. $keyforaliasextra = 'extraline';
  436. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  437. // End add extra fields line
  438. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  439. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  440. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as ps ON ps.rowid = s.parent';
  441. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  442. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  443. }
  444. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  445. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'commande_fournisseur as f';
  446. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  447. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua1 ON ua1.rowid = f.fk_user_approve';
  448. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua2 ON ua2.rowid = f.fk_user_approve2';
  449. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur_extrafields as extra ON f.rowid = extra.fk_object,';
  450. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as fd';
  451. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields as extraline ON fd.rowid = extraline.fk_object';
  452. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  453. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_commande';
  454. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('supplier_order').')';
  455. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  456. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.$user->id;
  457. }
  458. //Import Supplier Invoice
  459. //--------
  460. $r = 0;
  461. $r++;
  462. $this->import_code[$r] = $this->rights_class.'_'.$r;
  463. $this->import_label[$r] = "SupplierInvoices"; // Translation key
  464. $this->import_icon[$r] = $this->picto;
  465. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  466. $this->import_tables_array[$r] = ['f' => MAIN_DB_PREFIX.'facture_fourn', 'extra' => MAIN_DB_PREFIX.'facture_fourn_extrafields'];
  467. $this->import_tables_creator_array[$r] = ['f' => 'fk_user_author']; // Fields to store import user id
  468. $this->import_fields_array[$r] = [
  469. 'f.ref' => 'InvoiceRef*',
  470. 'f.ref_supplier' => 'RefSupplier',
  471. 'f.type' => 'Type*',
  472. 'f.fk_soc' => 'Supplier/Vendor*',
  473. 'f.datec' => 'InvoiceDateCreation',
  474. 'f.datef' => 'DateInvoice',
  475. 'f.date_lim_reglement' => 'DateMaxPayment',
  476. 'f.total_ht' => 'TotalHT',
  477. 'f.total_ttc' => 'TotalTTC',
  478. 'f.total_tva' => 'TotalVAT',
  479. 'f.paye' => 'InvoicePaid',
  480. 'f.fk_statut' => 'InvoiceStatus',
  481. 'f.fk_user_modif' => 'Modifier Id',
  482. 'f.fk_user_valid' => 'Validator Id',
  483. 'f.fk_facture_source' => 'Invoice Source Id',
  484. 'f.fk_projet' => 'Project Id',
  485. 'f.fk_account' => 'Bank Account*',
  486. 'f.note_public' => 'InvoiceNote',
  487. 'f.note_private' => 'NotePrivate',
  488. 'f.fk_cond_reglement' => 'Payment Condition',
  489. 'f.fk_mode_reglement' => 'Payment Mode',
  490. 'f.model_pdf' => 'Model',
  491. 'f.date_valid' => 'Validation Date'
  492. ];
  493. if (!empty($conf->multicurrency->enabled)) {
  494. $this->import_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  495. $this->import_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  496. $this->import_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  497. $this->import_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  498. $this->import_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  499. }
  500. // Add extra fields
  501. $import_extrafield_sample = array();
  502. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'facture_fourn' AND entity IN (0, ".$conf->entity.")";
  503. $resql = $this->db->query($sql);
  504. if ($resql) {
  505. while ($obj = $this->db->fetch_object($resql)) {
  506. $fieldname = 'extra.'.$obj->name;
  507. $fieldlabel = ucfirst($obj->label);
  508. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  509. $import_extrafield_sample[$fieldname] = $fieldlabel;
  510. }
  511. }
  512. // End add extra fields
  513. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture_fourn'];
  514. $this->import_regex_array[$r] = ['f.ref' => '(SI\d{4}-\d{4}|PROV.{1,32}$)', 'f.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'];
  515. $import_sample = [
  516. 'f.ref' => '(PROV001)',
  517. 'f.ref_supplier' => 'Supplier1',
  518. 'f.type' => '0',
  519. 'f.fk_soc' => 'Vendor1',
  520. 'f.datec' => '2021-01-01',
  521. 'f.datef' => '',
  522. 'f.date_lim_reglement' => '2021-01-30',
  523. 'f.total_ht' => '1000',
  524. 'f.total_ttc' => '1000',
  525. 'f.total_tva' => '0',
  526. 'f.paye' => '0',
  527. 'f.fk_statut' => '0',
  528. 'f.fk_user_modif' => '',
  529. 'f.fk_user_valid' => '',
  530. 'f.fk_facture_source' => '',
  531. 'f.fk_projet' => '',
  532. 'f.fk_account' => 'BANK1',
  533. 'f.note_public' => 'Note: ',
  534. 'f.note_private' => '',
  535. 'f.fk_cond_reglement' => '1',
  536. 'f.fk_mode_reglement' => '2',
  537. 'f.model_pdf' => 'crab',
  538. 'f.date_valid' => '',
  539. 'f.multicurrency_code' => 'USD',
  540. 'f.multicurrency_tx' => '1',
  541. 'f.multicurrency_total_ht' => '1000',
  542. 'f.multicurrency_total_tva' => '0',
  543. 'f.multicurrency_total_ttc' => '1000'
  544. ];
  545. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  546. $this->import_updatekeys_array[$r] = ['f.ref' => 'Ref'];
  547. $this->import_convertvalue_array[$r] = [
  548. //'c.ref'=>array('rule'=>'getrefifauto'),
  549. 'f.fk_soc' => ['rule' => 'fetchidfromref', 'file' => '/societe/class/societe.class.php', 'class' => 'Societe', 'method' => 'fetch', 'element' => 'ThirdParty'],
  550. 'f.fk_account' => ['rule' => 'fetchidfromref', 'file' => '/compta/bank/class/account.class.php', 'class' => 'Account', 'method' => 'fetch', 'element' => 'bank_account'],
  551. ];
  552. //Import Supplier Invoice Lines
  553. $r++;
  554. $this->import_code[$r] = $this->rights_class.'_'.$r;
  555. $this->import_label[$r] = "SupplierInvoiceLines"; // Translation key
  556. $this->import_icon[$r] = $this->picto;
  557. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  558. $this->import_tables_array[$r] = ['fd' => MAIN_DB_PREFIX.'facture_fourn_det', 'extra' => MAIN_DB_PREFIX.'facture_fourn_det_extrafields'];
  559. $this->import_fields_array[$r] = [
  560. 'fd.fk_facture_fourn' => 'InvoiceRef*',
  561. 'fd.fk_parent_line' => 'FacParentLine',
  562. 'fd.fk_product' => 'IdProduct',
  563. 'fd.label' => 'Label',
  564. 'fd.description' => 'LineDescription',
  565. 'fd.pu_ht' => 'PriceUHT',
  566. 'fd.pu_ttc' => 'PriceUTTC',
  567. 'fd.qty' => 'LineQty',
  568. 'fd.remise_percent' => 'Reduc.',
  569. 'fd.vat_src_code' => 'Vat Source Code',
  570. 'fd.product_type' => 'TypeOfLineServiceOrProduct',
  571. 'fd.tva_tx' => 'LineVATRate',
  572. 'fd.total_ht' => 'LineTotalHT',
  573. 'fd.tva' => 'LineTotalVAT',
  574. 'fd.total_ttc' => 'LineTotalTTC',
  575. 'fd.date_start' => 'Start Date',
  576. 'fd.date_end' => 'End Date',
  577. 'fd.fk_unit' => 'Unit'
  578. ];
  579. if (!empty($conf->multicurrency->enabled)) {
  580. $this->import_fields_array[$r]['fd.multicurrency_code'] = 'Currency';
  581. $this->import_fields_array[$r]['fd.multicurrency_subprice'] = 'CurrencyRate';
  582. $this->import_fields_array[$r]['fd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  583. $this->import_fields_array[$r]['fd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  584. $this->import_fields_array[$r]['fd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  585. }
  586. // Add extra fields
  587. $import_extrafield_sample = array();
  588. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'facture_fourn_det' AND entity IN (0, ".$conf->entity.")";
  589. $resql = $this->db->query($sql);
  590. if ($resql) {
  591. while ($obj = $this->db->fetch_object($resql)) {
  592. $fieldname = 'extra.'.$obj->name;
  593. $fieldlabel = ucfirst($obj->label);
  594. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  595. $import_extrafield_sample[$fieldname] = $fieldlabel;
  596. }
  597. }
  598. // End add extra fields
  599. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture_fourn_det'];
  600. $this->import_regex_array[$r] = ['fd.product_type' => '[0|1]$', 'fd.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product', 'fd.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'];
  601. $import_sample = [
  602. 'fd.fk_facture_fourn' => '(PROV001)',
  603. 'fd.fk_parent_line' => '',
  604. 'fd.fk_product' => '',
  605. 'fd.label' => '',
  606. 'fd.description' => 'Test Product',
  607. 'fd.pu_ht' => '50000',
  608. 'fd.pu_ttc' => '50000',
  609. 'fd.qty' => '1',
  610. 'fd.remise_percent' => '0',
  611. 'fd.vat_src_code' => '',
  612. 'fd.product_type' => '0',
  613. 'fd.tva_tx' => '0',
  614. 'fd.total_ht' => '50000',
  615. 'fd.tva' => '0',
  616. 'fd.total_ttc' => '50000',
  617. 'fd.date_start' => '',
  618. 'fd.date_end' => '',
  619. 'fd.fk_unit' => '',
  620. 'fd.multicurrency_code' => 'USD',
  621. 'fd.multicurrency_tx' => '0',
  622. 'fd.multicurrency_total_ht' => '50000',
  623. 'fd.multicurrency_total_tva' => '0',
  624. 'fd.multicurrency_total_ttc' => '50000'
  625. ];
  626. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  627. $this->import_updatekeys_array[$r] = ['fd.rowid' => 'Row Id', 'fd.fk_facture_fourn' => 'Invoice Id', 'fd.fk_product' => 'Product Id'];
  628. $this->import_convertvalue_array[$r] = [
  629. 'fd.fk_facture_fourn' => ['rule' => 'fetchidfromref', 'file' => '/fourn/class/fournisseur.facture.class.php', 'class' => 'FactureFournisseur', 'method' => 'fetch'],
  630. ];
  631. //Import Purchase Orders
  632. $r++;
  633. $this->import_code[$r] = 'commande_fournisseur_'.$r;
  634. $this->import_label[$r] = 'SuppliersOrders';
  635. $this->import_icon[$r] = $this->picto;
  636. $this->import_entities_array[$r] = [];
  637. $this->import_tables_array[$r] = ['c' => MAIN_DB_PREFIX.'commande_fournisseur', 'extra' => MAIN_DB_PREFIX.'commande_fournisseur_extrafields'];
  638. $this->import_tables_creator_array[$r] = ['c' => 'fk_user_author']; // Fields to store import user id
  639. $this->import_fields_array[$r] = [
  640. 'c.ref' => 'Document Ref*',
  641. 'c.ref_supplier' => 'RefSupplier',
  642. 'c.fk_soc' => 'ThirdPartyName*',
  643. 'c.fk_projet' => 'ProjectId',
  644. 'c.date_creation' => 'DateCreation',
  645. 'c.date_valid' => 'DateValid',
  646. 'c.date_approve' => 'DateApprove',
  647. 'c.date_commande' => 'DateOrder',
  648. 'c.fk_user_modif' => 'ModifiedById',
  649. 'c.fk_user_valid' => 'ValidatedById',
  650. 'c.fk_user_approve' => 'ApprovedById',
  651. 'c.source' => 'Source',
  652. 'c.fk_statut' => 'Status*',
  653. 'c.billed' => 'Billed(0/1)',
  654. 'c.remise_percent' => 'GlobalDiscount',
  655. 'c.total_tva' => 'TotalTVA',
  656. 'c.total_ht' => 'TotalHT',
  657. 'c.total_ttc' => 'TotalTTC',
  658. 'c.note_private' => 'NotePrivate',
  659. 'c.note_public' => 'Note',
  660. 'c.date_livraison' => 'DeliveryDate',
  661. 'c.fk_cond_reglement' => 'Payment Condition',
  662. 'c.fk_mode_reglement' => 'Payment Mode',
  663. 'c.model_pdf' => 'Model'
  664. ];
  665. if (!empty($conf->multicurrency->enabled)) {
  666. $this->import_fields_array[$r]['c.multicurrency_code'] = 'Currency';
  667. $this->import_fields_array[$r]['c.multicurrency_tx'] = 'CurrencyRate';
  668. $this->import_fields_array[$r]['c.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  669. $this->import_fields_array[$r]['c.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  670. $this->import_fields_array[$r]['c.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  671. }
  672. // Add extra fields
  673. $import_extrafield_sample = array();
  674. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'commande_fournisseur' AND entity IN (0, ".$conf->entity.")";
  675. $resql = $this->db->query($sql);
  676. if ($resql) {
  677. while ($obj = $this->db->fetch_object($resql)) {
  678. $fieldname = 'extra.'.$obj->name;
  679. $fieldlabel = ucfirst($obj->label);
  680. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  681. $import_extrafield_sample[$fieldname] = $fieldlabel;
  682. }
  683. }
  684. // End add extra fields
  685. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'commande_fournisseur'];
  686. $this->import_regex_array[$r] = [
  687. 'c.ref' => '(PO\d{4}-\d{4}|PORDER.{1,32}$|PROV.{1,32}$)',
  688. 'c.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  689. ];
  690. $this->import_updatekeys_array[$r] = ['c.ref' => 'Ref'];
  691. $this->import_convertvalue_array[$r] = [
  692. 'c.fk_soc' => [
  693. 'rule' => 'fetchidfromref',
  694. 'file' => '/societe/class/societe.class.php',
  695. 'class' => 'Societe',
  696. 'method' => 'fetch',
  697. 'element' => 'ThirdParty'
  698. ],
  699. 'c.fk_mode_reglement' => [
  700. 'rule' => 'fetchidfromcodeorlabel',
  701. 'file' => '/compta/paiement/class/cpaiement.class.php',
  702. 'class' => 'Cpaiement',
  703. 'method' => 'fetch',
  704. 'element' => 'cpayment'
  705. ],
  706. 'c.source' => ['rule' => 'zeroifnull'],
  707. ];
  708. //Import PO Lines
  709. $r++;
  710. $this->import_code[$r] = 'commande_fournisseurdet_'.$r;
  711. $this->import_label[$r] = 'PurchaseOrderLines';
  712. $this->import_icon[$r] = $this->picto;
  713. $this->import_entities_array[$r] = array();
  714. $this->import_tables_array[$r] = ['cd' => MAIN_DB_PREFIX.'commande_fournisseurdet', 'extra' => MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields'];
  715. $this->import_fields_array[$r] = [
  716. 'cd.fk_commande' => 'Document Ref*',
  717. 'cd.fk_parent_line' => 'PrParentLine',
  718. 'cd.fk_product' => 'IdProduct',
  719. 'cd.label' => 'Label',
  720. 'cd.description' => 'LineDescription',
  721. 'cd.tva_tx' => 'LineVATRate',
  722. 'cd.qty' => 'LineQty',
  723. 'cd.remise_percent' => 'Reduc. Percent',
  724. 'cd.remise' => 'Reduc.',
  725. 'cd.subprice' => 'Sub Price',
  726. 'cd.total_ht' => 'LineTotalHT',
  727. 'cd.total_tva' => 'LineTotalVAT',
  728. 'cd.total_ttc' => 'LineTotalTTC',
  729. 'cd.product_type' => 'TypeOfLineServiceOrProduct',
  730. 'cd.date_start' => 'Start Date',
  731. 'cd.date_end' => 'End Date',
  732. 'cd.info_bits' => 'InfoBits',
  733. 'cd.special_code' => 'Special Code',
  734. 'cd.rang' => 'LinePosition',
  735. 'cd.fk_unit' => 'Unit'
  736. ];
  737. if (!empty($conf->multicurrency->enabled)) {
  738. $this->import_fields_array[$r]['cd.multicurrency_code'] = 'Currency';
  739. $this->import_fields_array[$r]['cd.multicurrency_subprice'] = 'CurrencyRate';
  740. $this->import_fields_array[$r]['cd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  741. $this->import_fields_array[$r]['cd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  742. $this->import_fields_array[$r]['cd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  743. }
  744. // Add extra fields
  745. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'commande_fournisseurdet' AND entity IN (0, ".$conf->entity.")";
  746. $resql = $this->db->query($sql);
  747. if ($resql) {
  748. while ($obj = $this->db->fetch_object($resql)) {
  749. $fieldname = 'extra.'.$obj->name;
  750. $fieldlabel = ucfirst($obj->label);
  751. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  752. }
  753. }
  754. // End add extra fields
  755. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'commande_fournisseurdet'];
  756. $this->import_regex_array[$r] = [
  757. 'cd.product_type' => '[0|1]$',
  758. 'cd.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
  759. 'cd.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  760. ];
  761. $this->import_updatekeys_array[$r] = ['cd.fk_commande' => 'Purchase Order Id'];
  762. $this->import_convertvalue_array[$r] = [
  763. 'cd.fk_commande' => [
  764. 'rule' => 'fetchidfromref',
  765. 'file' => '/fourn/class/fournisseur.commande.class.php',
  766. 'class' => 'CommandeFournisseur',
  767. 'method' => 'fetch',
  768. 'element' => 'order_supplier'
  769. ],
  770. 'cd.info_bits' => ['rule' => 'zeroifnull'],
  771. 'cd.special_code' => ['rule' => 'zeroifnull'],
  772. ];
  773. }
  774. /**
  775. * Function called when module is enabled.
  776. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  777. * It also creates data directories
  778. *
  779. * @param string $options Options when enabling module ('', 'noboxes')
  780. * @return int 1 if OK, 0 if KO
  781. */
  782. public function init($options = '')
  783. {
  784. global $conf, $langs;
  785. $this->remove($options);
  786. //ODT template
  787. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/supplier_orders/template_supplier_order.odt';
  788. $dirodt = DOL_DATA_ROOT.'/doctemplates/supplier_orders';
  789. $dest = $dirodt.'/template_supplier_order.odt';
  790. if (file_exists($src) && !file_exists($dest)) {
  791. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  792. dol_mkdir($dirodt);
  793. $result = dol_copy($src, $dest, 0, 0);
  794. if ($result < 0) {
  795. $langs->load("errors");
  796. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  797. return 0;
  798. }
  799. }
  800. $sql = array(
  801. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[0][2])."' AND type = 'order_supplier' AND entity = ".$conf->entity,
  802. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[0][2])."','order_supplier',".$conf->entity.")",
  803. );
  804. return $this->_init($sql, $options);
  805. }
  806. }