modFournisseur.class.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 = '.((int) $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 = '.((int) $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. 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel', 'project.rowid'=>'ProjectId', 'project.ref'=>'ProjectRef', 'project.title'=>'ProjectLabel'
  395. );
  396. if (!empty($conf->multicurrency->enabled)) {
  397. $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  398. $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  399. $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  400. $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  401. $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  402. }
  403. if (empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED)) {
  404. unset($this->export_fields_array['f.date_approve2']);
  405. unset($this->export_fields_array['ua2.login']);
  406. }
  407. $this->export_TypeFields_array[$r] = array(
  408. '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',
  409. '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",
  410. 'f.date_creation'=>"Date", 'f.date_commande'=>"Date", 'f.date_livraison'=>"Date", 'f.total_ht'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.total_tva'=>"Numeric",
  411. 'f.fk_statut'=>'Status', 'f.date_approve'=>'Date', 'f.date_approve2'=>'Date', 'f.note_public'=>"Text", 'f.note_private'=>"Text", 'fd.description'=>"Text",
  412. 'fd.tva_tx'=>"Numeric", 'fd.qty'=>"Numeric", 'fd.remise_percent'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.total_tva'=>"Numeric",
  413. 'fd.product_type'=>'Numeric', 'fd.ref'=>'Text', 'fd.fk_product'=>'List:product:label', 'p.ref'=>'Text', 'p.label'=>'Text', 'project.ref'=>'Text', 'project.title'=>'Text'
  414. );
  415. $this->export_entities_array[$r] = array(
  416. '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',
  417. 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.idprof5'=>'company', 's.idprof6'=>'company', 's.tva_intra'=>'company', 'ua1.login'=>'user',
  418. '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",
  419. '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',
  420. 'p.ref'=>'product', 'p.label'=>'product', 'project.rowid'=>'project', 'project.ref'=>'project', 'project.title'=>'project'
  421. );
  422. $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
  423. // Add extra fields object
  424. $keyforselect = 'commande_fournisseur';
  425. $keyforelement = 'order';
  426. $keyforaliasextra = 'extra';
  427. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  428. // End add extra fields object
  429. // Add extra fields line
  430. $keyforselect = 'commande_fournisseurdet';
  431. $keyforelement = 'order_line';
  432. $keyforaliasextra = 'extraline';
  433. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  434. // End add extra fields line
  435. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  436. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'societe as s';
  437. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as ps ON ps.rowid = s.parent';
  438. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  439. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
  440. }
  441. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid,';
  442. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'commande_fournisseur as f';
  443. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as project on (f.fk_projet = project.rowid)';
  444. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua1 ON ua1.rowid = f.fk_user_approve';
  445. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as ua2 ON ua2.rowid = f.fk_user_approve2';
  446. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur_extrafields as extra ON f.rowid = extra.fk_object,';
  447. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as fd';
  448. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields as extraline ON fd.rowid = extraline.fk_object';
  449. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  450. $this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_commande';
  451. $this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('supplier_order').')';
  452. if (is_object($user) && empty($user->rights->societe->client->voir)) {
  453. $this->export_sql_end[$r] .= ' AND sc.fk_user = '.((int) $user->id);
  454. }
  455. //Import Supplier Invoice
  456. //--------
  457. $r = 0;
  458. $r++;
  459. $this->import_code[$r] = $this->rights_class.'_'.$r;
  460. $this->import_label[$r] = "SupplierInvoices"; // Translation key
  461. $this->import_icon[$r] = $this->picto;
  462. $this->import_entities_array[$r] = []; // We define here only fields that use another icon that the one defined into import_icon
  463. $this->import_tables_array[$r] = ['f' => MAIN_DB_PREFIX.'facture_fourn', 'extra' => MAIN_DB_PREFIX.'facture_fourn_extrafields'];
  464. $this->import_tables_creator_array[$r] = ['f' => 'fk_user_author']; // Fields to store import user id
  465. $this->import_fields_array[$r] = [
  466. 'f.ref' => 'InvoiceRef*',
  467. 'f.ref_supplier' => 'RefSupplier',
  468. 'f.type' => 'Type*',
  469. 'f.fk_soc' => 'Supplier/Vendor*',
  470. 'f.datec' => 'InvoiceDateCreation',
  471. 'f.datef' => 'DateInvoice',
  472. 'f.date_lim_reglement' => 'DateMaxPayment',
  473. 'f.total_ht' => 'TotalHT',
  474. 'f.total_ttc' => 'TotalTTC',
  475. 'f.total_tva' => 'TotalVAT',
  476. 'f.paye' => 'InvoicePaid',
  477. 'f.fk_statut' => 'InvoiceStatus',
  478. 'f.fk_user_modif' => 'Modifier Id',
  479. 'f.fk_user_valid' => 'Validator Id',
  480. 'f.fk_facture_source' => 'Invoice Source Id',
  481. 'f.fk_projet' => 'Project Id',
  482. 'f.fk_account' => 'Bank Account*',
  483. 'f.note_public' => 'InvoiceNote',
  484. 'f.note_private' => 'NotePrivate',
  485. 'f.fk_cond_reglement' => 'Payment Condition',
  486. 'f.fk_mode_reglement' => 'Payment Mode',
  487. 'f.model_pdf' => 'Model',
  488. 'f.date_valid' => 'Validation Date'
  489. ];
  490. if (!empty($conf->multicurrency->enabled)) {
  491. $this->import_fields_array[$r]['f.multicurrency_code'] = 'Currency';
  492. $this->import_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
  493. $this->import_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  494. $this->import_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  495. $this->import_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  496. }
  497. // Add extra fields
  498. $import_extrafield_sample = [];
  499. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn' AND entity IN (0, ".$conf->entity.")";
  500. $resql = $this->db->query($sql);
  501. if ($resql) {
  502. while ($obj = $this->db->fetch_object($resql)) {
  503. $fieldname = 'extra.'.$obj->name;
  504. $fieldlabel = ucfirst($obj->label);
  505. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  506. $import_extrafield_sample[$fieldname] = $fieldlabel;
  507. }
  508. }
  509. // End add extra fields
  510. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture_fourn'];
  511. $this->import_regex_array[$r] = ['f.ref' => '(SI\d{4}-\d{4}|PROV.{1,32}$)', 'f.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'];
  512. $import_sample = [
  513. 'f.ref' => '(PROV001)',
  514. 'f.ref_supplier' => 'Supplier1',
  515. 'f.type' => '0',
  516. 'f.fk_soc' => 'Vendor1',
  517. 'f.datec' => '2021-01-01',
  518. 'f.datef' => '',
  519. 'f.date_lim_reglement' => '2021-01-30',
  520. 'f.total_ht' => '1000',
  521. 'f.total_ttc' => '1000',
  522. 'f.total_tva' => '0',
  523. 'f.paye' => '0',
  524. 'f.fk_statut' => '0',
  525. 'f.fk_user_modif' => '',
  526. 'f.fk_user_valid' => '',
  527. 'f.fk_facture_source' => '',
  528. 'f.fk_projet' => '',
  529. 'f.fk_account' => 'BANK1',
  530. 'f.note_public' => 'Note: ',
  531. 'f.note_private' => '',
  532. 'f.fk_cond_reglement' => '1',
  533. 'f.fk_mode_reglement' => '2',
  534. 'f.model_pdf' => 'crab',
  535. 'f.date_valid' => '',
  536. 'f.multicurrency_code' => 'USD',
  537. 'f.multicurrency_tx' => '1',
  538. 'f.multicurrency_total_ht' => '1000',
  539. 'f.multicurrency_total_tva' => '0',
  540. 'f.multicurrency_total_ttc' => '1000'
  541. ];
  542. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  543. $this->import_updatekeys_array[$r] = ['f.ref' => 'Ref'];
  544. $this->import_convertvalue_array[$r] = [
  545. //'c.ref'=>array('rule'=>'getrefifauto'),
  546. 'f.fk_soc' => ['rule' => 'fetchidfromref', 'file' => '/societe/class/societe.class.php', 'class' => 'Societe', 'method' => 'fetch', 'element' => 'ThirdParty'],
  547. 'f.fk_account' => ['rule' => 'fetchidfromref', 'file' => '/compta/bank/class/account.class.php', 'class' => 'Account', 'method' => 'fetch', 'element' => 'bank_account'],
  548. ];
  549. //Import Supplier Invoice Lines
  550. $r++;
  551. $this->import_code[$r] = $this->rights_class.'_'.$r;
  552. $this->import_label[$r] = "SupplierInvoiceLines"; // Translation key
  553. $this->import_icon[$r] = $this->picto;
  554. $this->import_entities_array[$r] = []; // We define here only fields that use another icon that the one defined into import_icon
  555. $this->import_tables_array[$r] = ['fd' => MAIN_DB_PREFIX.'facture_fourn_det', 'extra' => MAIN_DB_PREFIX.'facture_fourn_det_extrafields'];
  556. $this->import_fields_array[$r] = [
  557. 'fd.fk_facture_fourn' => 'InvoiceRef*',
  558. 'fd.fk_parent_line' => 'FacParentLine',
  559. 'fd.fk_product' => 'IdProduct',
  560. 'fd.label' => 'Label',
  561. 'fd.description' => 'LineDescription',
  562. 'fd.pu_ht' => 'PriceUHT',
  563. 'fd.pu_ttc' => 'PriceUTTC',
  564. 'fd.qty' => 'LineQty',
  565. 'fd.remise_percent' => 'Reduc.',
  566. 'fd.vat_src_code' => 'Vat Source Code',
  567. 'fd.product_type' => 'TypeOfLineServiceOrProduct',
  568. 'fd.tva_tx' => 'LineVATRate',
  569. 'fd.total_ht' => 'LineTotalHT',
  570. 'fd.tva' => 'LineTotalVAT',
  571. 'fd.total_ttc' => 'LineTotalTTC',
  572. 'fd.date_start' => 'Start Date',
  573. 'fd.date_end' => 'End Date',
  574. 'fd.fk_unit' => 'Unit'
  575. ];
  576. if (!empty($conf->multicurrency->enabled)) {
  577. $this->import_fields_array[$r]['fd.multicurrency_code'] = 'Currency';
  578. $this->import_fields_array[$r]['fd.multicurrency_subprice'] = 'CurrencyRate';
  579. $this->import_fields_array[$r]['fd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  580. $this->import_fields_array[$r]['fd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  581. $this->import_fields_array[$r]['fd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  582. }
  583. // Add extra fields
  584. $import_extrafield_sample = [];
  585. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'facture_fourn_det' AND entity IN (0, ".$conf->entity.")";
  586. $resql = $this->db->query($sql);
  587. if ($resql) {
  588. while ($obj = $this->db->fetch_object($resql)) {
  589. $fieldname = 'extra.'.$obj->name;
  590. $fieldlabel = ucfirst($obj->label);
  591. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  592. $import_extrafield_sample[$fieldname] = $fieldlabel;
  593. }
  594. }
  595. // End add extra fields
  596. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'facture_fourn_det'];
  597. $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'];
  598. $import_sample = [
  599. 'fd.fk_facture_fourn' => '(PROV001)',
  600. 'fd.fk_parent_line' => '',
  601. 'fd.fk_product' => '',
  602. 'fd.label' => '',
  603. 'fd.description' => 'Test Product',
  604. 'fd.pu_ht' => '50000',
  605. 'fd.pu_ttc' => '50000',
  606. 'fd.qty' => '1',
  607. 'fd.remise_percent' => '0',
  608. 'fd.vat_src_code' => '',
  609. 'fd.product_type' => '0',
  610. 'fd.tva_tx' => '0',
  611. 'fd.total_ht' => '50000',
  612. 'fd.tva' => '0',
  613. 'fd.total_ttc' => '50000',
  614. 'fd.date_start' => '',
  615. 'fd.date_end' => '',
  616. 'fd.fk_unit' => '',
  617. 'fd.multicurrency_code' => 'USD',
  618. 'fd.multicurrency_tx' => '0',
  619. 'fd.multicurrency_total_ht' => '50000',
  620. 'fd.multicurrency_total_tva' => '0',
  621. 'fd.multicurrency_total_ttc' => '50000'
  622. ];
  623. $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
  624. $this->import_updatekeys_array[$r] = ['fd.rowid' => 'Row Id', 'fd.fk_facture_fourn' => 'Invoice Id', 'fd.fk_product' => 'Product Id'];
  625. $this->import_convertvalue_array[$r] = [
  626. 'fd.fk_facture_fourn' => ['rule' => 'fetchidfromref', 'file' => '/fourn/class/fournisseur.facture.class.php', 'class' => 'FactureFournisseur', 'method' => 'fetch'],
  627. ];
  628. //Import Purchase Orders
  629. $r++;
  630. $this->import_code[$r] = 'commande_fournisseur_'.$r;
  631. $this->import_label[$r] = 'SuppliersOrders';
  632. $this->import_icon[$r] = $this->picto;
  633. $this->import_entities_array[$r] = [];
  634. $this->import_tables_array[$r] = ['c' => MAIN_DB_PREFIX.'commande_fournisseur', 'extra' => MAIN_DB_PREFIX.'commande_fournisseur_extrafields'];
  635. $this->import_tables_creator_array[$r] = ['c' => 'fk_user_author']; // Fields to store import user id
  636. $this->import_fields_array[$r] = [
  637. 'c.ref' => 'Document Ref*',
  638. 'c.ref_supplier' => 'RefSupplier',
  639. 'c.fk_soc' => 'ThirdPartyName*',
  640. 'c.fk_projet' => 'ProjectId',
  641. 'c.date_creation' => 'DateCreation',
  642. 'c.date_valid' => 'DateValid',
  643. 'c.date_approve' => 'DateApprove',
  644. 'c.date_commande' => 'DateOrder',
  645. 'c.fk_user_modif' => 'ModifiedById',
  646. 'c.fk_user_valid' => 'ValidatedById',
  647. 'c.fk_user_approve' => 'ApprovedById',
  648. 'c.source' => 'Source',
  649. 'c.fk_statut' => 'Status*',
  650. 'c.billed' => 'Billed(0/1)',
  651. 'c.remise_percent' => 'GlobalDiscount',
  652. 'c.total_tva' => 'TotalTVA',
  653. 'c.total_ht' => 'TotalHT',
  654. 'c.total_ttc' => 'TotalTTC',
  655. 'c.note_private' => 'NotePrivate',
  656. 'c.note_public' => 'Note',
  657. 'c.date_livraison' => 'DeliveryDate',
  658. 'c.fk_cond_reglement' => 'Payment Condition',
  659. 'c.fk_mode_reglement' => 'Payment Mode',
  660. 'c.model_pdf' => 'Model'
  661. ];
  662. if (!empty($conf->multicurrency->enabled)) {
  663. $this->import_fields_array[$r]['c.multicurrency_code'] = 'Currency';
  664. $this->import_fields_array[$r]['c.multicurrency_tx'] = 'CurrencyRate';
  665. $this->import_fields_array[$r]['c.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  666. $this->import_fields_array[$r]['c.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  667. $this->import_fields_array[$r]['c.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  668. }
  669. // Add extra fields
  670. $import_extrafield_sample = [];
  671. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseur' AND entity IN (0, ".$conf->entity.")";
  672. $resql = $this->db->query($sql);
  673. if ($resql) {
  674. while ($obj = $this->db->fetch_object($resql)) {
  675. $fieldname = 'extra.'.$obj->name;
  676. $fieldlabel = ucfirst($obj->label);
  677. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  678. $import_extrafield_sample[$fieldname] = $fieldlabel;
  679. }
  680. }
  681. // End add extra fields
  682. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'commande_fournisseur'];
  683. $this->import_regex_array[$r] = [
  684. 'c.ref' => '(PO\d{4}-\d{4}|PORDER.{1,32}$|PROV.{1,32}$)',
  685. 'c.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  686. ];
  687. $this->import_updatekeys_array[$r] = ['c.ref' => 'Ref'];
  688. $this->import_convertvalue_array[$r] = [
  689. 'c.fk_soc' => [
  690. 'rule' => 'fetchidfromref',
  691. 'file' => '/societe/class/societe.class.php',
  692. 'class' => 'Societe',
  693. 'method' => 'fetch',
  694. 'element' => 'ThirdParty'
  695. ],
  696. 'c.fk_mode_reglement' => [
  697. 'rule' => 'fetchidfromcodeorlabel',
  698. 'file' => '/compta/paiement/class/cpaiement.class.php',
  699. 'class' => 'Cpaiement',
  700. 'method' => 'fetch',
  701. 'element' => 'cpayment'
  702. ],
  703. 'c.source' => ['rule' => 'zeroifnull'],
  704. ];
  705. //Import PO Lines
  706. $r++;
  707. $this->import_code[$r] = 'commande_fournisseurdet_'.$r;
  708. $this->import_label[$r] = 'PurchaseOrderLines';
  709. $this->import_icon[$r] = $this->picto;
  710. $this->import_entities_array[$r] = [];
  711. $this->import_tables_array[$r] = ['cd' => MAIN_DB_PREFIX.'commande_fournisseurdet', 'extra' => MAIN_DB_PREFIX.'commande_fournisseurdet_extrafields'];
  712. $this->import_fields_array[$r] = [
  713. 'cd.fk_commande' => 'Document Ref*',
  714. 'cd.fk_parent_line' => 'PrParentLine',
  715. 'cd.fk_product' => 'IdProduct',
  716. 'cd.label' => 'Label',
  717. 'cd.description' => 'LineDescription',
  718. 'cd.tva_tx' => 'LineVATRate',
  719. 'cd.qty' => 'LineQty',
  720. 'cd.remise_percent' => 'Reduc. Percent',
  721. 'cd.remise' => 'Reduc.',
  722. 'cd.subprice' => 'Sub Price',
  723. 'cd.total_ht' => 'LineTotalHT',
  724. 'cd.total_tva' => 'LineTotalVAT',
  725. 'cd.total_ttc' => 'LineTotalTTC',
  726. 'cd.product_type' => 'TypeOfLineServiceOrProduct',
  727. 'cd.date_start' => 'Start Date',
  728. 'cd.date_end' => 'End Date',
  729. 'cd.info_bits' => 'InfoBits',
  730. 'cd.special_code' => 'Special Code',
  731. 'cd.rang' => 'LinePosition',
  732. 'cd.fk_unit' => 'Unit'
  733. ];
  734. if (!empty($conf->multicurrency->enabled)) {
  735. $this->import_fields_array[$r]['cd.multicurrency_code'] = 'Currency';
  736. $this->import_fields_array[$r]['cd.multicurrency_subprice'] = 'CurrencyRate';
  737. $this->import_fields_array[$r]['cd.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
  738. $this->import_fields_array[$r]['cd.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
  739. $this->import_fields_array[$r]['cd.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
  740. }
  741. // Add extra fields
  742. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'commande_fournisseurdet' AND entity IN (0, ".$conf->entity.")";
  743. $resql = $this->db->query($sql);
  744. if ($resql) {
  745. while ($obj = $this->db->fetch_object($resql)) {
  746. $fieldname = 'extra.'.$obj->name;
  747. $fieldlabel = ucfirst($obj->label);
  748. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  749. }
  750. }
  751. // End add extra fields
  752. $this->import_fieldshidden_array[$r] = ['extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'commande_fournisseurdet'];
  753. $this->import_regex_array[$r] = [
  754. 'cd.product_type' => '[0|1]$',
  755. 'cd.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
  756. 'cd.multicurrency_code' => 'code@'.MAIN_DB_PREFIX.'multicurrency'
  757. ];
  758. $this->import_updatekeys_array[$r] = ['cd.fk_commande' => 'Purchase Order Id'];
  759. $this->import_convertvalue_array[$r] = [
  760. 'cd.fk_commande' => [
  761. 'rule' => 'fetchidfromref',
  762. 'file' => '/fourn/class/fournisseur.commande.class.php',
  763. 'class' => 'CommandeFournisseur',
  764. 'method' => 'fetch',
  765. 'element' => 'order_supplier'
  766. ],
  767. 'cd.info_bits' => ['rule' => 'zeroifnull'],
  768. 'cd.special_code' => ['rule' => 'zeroifnull'],
  769. ];
  770. }
  771. /**
  772. * Function called when module is enabled.
  773. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  774. * It also creates data directories
  775. *
  776. * @param string $options Options when enabling module ('', 'noboxes')
  777. * @return int 1 if OK, 0 if KO
  778. */
  779. public function init($options = '')
  780. {
  781. global $conf, $langs;
  782. $this->remove($options);
  783. //ODT template
  784. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/supplier_orders/template_supplier_order.odt';
  785. $dirodt = DOL_DATA_ROOT.'/doctemplates/supplier_orders';
  786. $dest = $dirodt.'/template_supplier_order.odt';
  787. if (file_exists($src) && !file_exists($dest)) {
  788. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  789. dol_mkdir($dirodt);
  790. $result = dol_copy($src, $dest, 0, 0);
  791. if ($result < 0) {
  792. $langs->load("errors");
  793. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  794. return 0;
  795. }
  796. }
  797. $sql = array(
  798. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[0][2])."' AND type = 'order_supplier' AND entity = ".((int) $conf->entity),
  799. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[0][2])."', 'order_supplier', ".((int) $conf->entity).")",
  800. );
  801. return $this->_init($sql, $options);
  802. }
  803. }