dolibarr 23.0.3
modMultiCurrency.class.php
Go to the documentation of this file.
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-2016 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
6 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
29include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
30
31
36{
42 public function __construct($db)
43 {
44 global $conf, $langs;
45
46 $this->db = $db;
47
48 // Id for module (must be unique).
49 // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id).
50 $this->numero = 40000;
51 // Key text used to identify module (for permissions, menus, etc...)
52 $this->rights_class = 'multicurrency';
53
54 // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
55 // It is used to group modules in module setup page
56 $this->family = "technic";
57 // Module position in the family
58 $this->module_position = '40';
59
60 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
61 $this->name = preg_replace('/^mod/i', '', get_class($this));
62 // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
63 $this->description = "Module to manage several foreign currencies in prices and documents";
64
65 // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version
66 $this->version = 'dolibarr';
67 // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
68 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
69 // Name of image file used for this module.
70 // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
71 // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
72 $this->picto = 'multicurrency';
73
74 // Defined all module parts (triggers, login, substitutions, menus, css, etc...)
75 // for default path (eg: /multicurrency/core/xxxxx) (0=disable, 1=enable)
76 // for specific path of parts (eg: /multicurrency/core/modules/barcode)
77 // for specific css file (eg: /multicurrency/css/multicurrency.css.php)
78 $this->module_parts = array();
79
80 // Data directories to create when module is enabled.
81 // Example: this->dirs = array("/multicurrency/temp");
82 $this->dirs = array();
83
84 // Config pages. Put here list of php page, stored into multicurrency/admin directory, to use to setup module.
85 $this->config_page_url = array("multicurrency.php");
86
87 // Dependencies
88 $this->hidden = false; // A condition to hide module
89 $this->depends = array(); // List of modules id that must be enabled if this module is enabled
90 $this->requiredby = array(); // List of modules id to disable if this one is disabled
91 $this->conflictwith = array(); // List of modules id this module is in conflict with
92 $this->langfiles = array("multicurrency");
93
94 // Constants
95 // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
96 // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
97 // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
98 // );
99 $this->const = array();
100
101 // Array to add new pages in new tabs
102 // Example: $this->tabs = array('objecttype:+tabname1:Title1:mylangfile@multicurrency:$user->rights->multicurrency->read:/multicurrency/mynewtab1.php?id=__ID__', // To add a new tab identified by code tabname1
103 // 'objecttype:+tabname2:SUBSTITUTION_Title2:mylangfile@multicurrency:$user->rights->othermodule->read:/multicurrency/mynewtab2.php?id=__ID__', // To add another new tab identified by code tabname2. Label will be result of calling all substitution functions on 'Title2' key.
104 // 'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname
105 // where objecttype can be
106 // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
107 // 'contact' to add a tab in contact view
108 // 'contract' to add a tab in contract view
109 // 'group' to add a tab in group view
110 // 'intervention' to add a tab in intervention view
111 // 'invoice' to add a tab in customer invoice view
112 // 'invoice_supplier' to add a tab in supplier invoice view
113 // 'member' to add a tab in foundation member view
114 // 'opensurveypoll' to add a tab in opensurvey poll view
115 // 'order' to add a tab in sales order view
116 // 'order_supplier' to add a tab in supplier order view
117 // 'payment' to add a tab in payment view
118 // 'payment_supplier' to add a tab in supplier payment view
119 // 'product' to add a tab in product view
120 // 'propal' to add a tab in propal view
121 // 'project' to add a tab in project view
122 // 'stock' to add a tab in stock view
123 // 'thirdparty' to add a tab in third party view
124 // 'user' to add a tab in user view
125 $this->tabs = array();
126
127 // Dictionaries
128 if (!isset($conf->multicurrency->enabled)) {
129 $conf->multicurrency = new stdClass();
130 $conf->multicurrency->enabled = 0;
131 }
132 $this->dictionaries = array();
133
134
135 // Boxes
136 // Add here list of php file(s) stored in core/boxes that contains class to show a box.
137 $this->boxes = array(); // List of boxes
138
139 // Cronjobs (List of cron jobs entries to add when module is enabled)
140 // unit_frequency must be 60 for minute, 3600 for hour, 86400 for day, 604800 for week
141 $statusatinstall=1;
142 $arraydate=dol_getdate(dol_now());
143 $datestart=dol_mktime(21, 15, 0, $arraydate['mon'], $arraydate['mday'], $arraydate['year']);
144 $langs->load('multicurrency');
145
146 $this->cronjobs = array(
147 0 => array(
148 'priority' => 61,
149 'label' => $langs->trans('MutltiCurrencyAutoUpdateCurrencies'),
150 'jobtype' => 'method',
151 'class' => 'multicurrency/class/multicurrency.class.php',
152 'objectname' => 'MultiCurrency',
153 'method' => 'syncRates',
154 'parameters' => '0,0,cron',
155 'comment' => 'Update all the currencies using the currencylayer API. An API key needs to be given in the multi-currency module config page to have this job working.<br>First param is not used, Second parameter is 0 to update only already existing currency defined into the Multicurrency module or 1 to add any currency. Third parameter must be "cron".',
156 'frequency' => 1,
157 'unitfrequency' => 604800,
158 'status' => $statusatinstall,
159 'test' => 'isModEnabled("cron")',
160 'datestart' => $datestart
161 ),
162 );
163
164 // Permissions
165 $this->rights = array(); // Permission array used by this module
166 $r = 0;
167
168 // Add here list of permission defined by an id, a label, a boolean and two constant strings.
169 // Example:
170 // $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used)
171 // $this->rights[$r][1] = 'Permision label'; // Permission label
172 // $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
173 // $this->rights[$r][4] = 'level1'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
174 // $this->rights[$r][5] = 'level2'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
175 // $r++;
176
177 $this->rights[$r][0] = 40001;
178 $this->rights[$r][1] = 'Read currencies and their rates';
179 $this->rights[$r][2] = 'r';
180 $this->rights[$r][3] = 1;
181 $this->rights[$r][4] = 'currency';
182 $this->rights[$r][5] = 'read';
183 $r++;
184
185 $this->rights[$r][0] = 40002;
186 $this->rights[$r][1] = 'Create/Update currencies and their rates';
187 $this->rights[$r][2] = 'w';
188 $this->rights[$r][3] = 0;
189 $this->rights[$r][4] = 'currency';
190 $this->rights[$r][5] = 'write';
191 $r++;
192
193 $this->rights[$r][0] = 40003;
194 $this->rights[$r][1] = 'Delete currencies and their rates';
195 $this->rights[$r][2] = 'w';
196 $this->rights[$r][3] = 0;
197 $this->rights[$r][4] = 'currency';
198 $this->rights[$r][5] = 'delete';
199 $r++;
200
201 // Main menu entries
202 $this->menu = array(); // List of menus to add
203 $r = 0;
204
205 // Exports
206 $r = 1;
207
208 // Example:
209 // $this->export_code[$r]=$this->rights_class.'_'.$r;
210 // $this->export_label[$r]='MyModule'; // Translation key (used only if key ExportDataset_xxx_z not found)
211 // $this->export_enabled[$r]='1'; // Condition to show export in list (ie: '$user->id==3'). Set to 1 to always show when module is enabled.
212 // $this->export_icon[$r]='generic:MyModule';
213 // $this->export_permission[$r]=array(array("multicurrency","level1","level2"));
214 // $this->export_fields_array[$r]=array(
215 // 's.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','s.fk_pays'=>'Country','s.phone'=>'Phone',
216 // 's.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.code_compta'=>'CustomerAccountancyCode',
217 // 's.code_compta_fournisseur'=>'SupplierAccountancyCode','f.rowid'=>"InvoiceId",'f.ref'=>"InvoiceRef",'f.datec'=>"InvoiceDateCreation",
218 // 'f.datef'=>"DateInvoice",'f.total_ht'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.total_tva'=>"TotalVAT",'f.paye'=>"InvoicePaid",'f.fk_statut'=>'InvoiceStatus',
219 // 'f.note'=>"InvoiceNote",'fd.rowid'=>'LineId','fd.description'=>"LineDescription",'fd.price'=>"LineUnitPrice",'fd.tva_tx'=>"LineVATRate",
220 // 'fd.qty'=>"LineQty",'fd.total_ht'=>"LineTotalHT",'fd.total_tva'=>"LineTotalTVA",'fd.total_ttc'=>"LineTotalTTC",'fd.date_start'=>"DateStart",
221 // 'fd.date_end'=>"DateEnd",'fd.fk_product'=>'ProductId','p.ref'=>'ProductRef'
222 //);
223 // $this->export_TypeFields_array[$r]=array(
224 // 't.date'=>'Date', 't.qte'=>'Numeric', 't.poids'=>'Numeric', 't.fad'=>'Numeric', 't.paq'=>'Numeric', 't.stockage'=>'Numeric', 't.fadparliv'=>'Numeric',
225 // 't.livau100'=>'Numeric', 't.forfait'=>'Numeric', 's.nom'=>'Text','s.address'=>'Text','s.zip'=>'Text','s.town'=>'Text','c.code'=>'Text','s.phone'=>'Text',
226 // 's.siren'=>'Text','s.siret'=>'Text','s.ape'=>'Text','s.idprof4'=>'Text','s.code_compta'=>'Text','s.code_compta_fournisseur'=>'Text','s.tva_intra'=>'Text',
227 // 'f.ref'=>"Text",'f.datec'=>"Date",'f.datef'=>"Date",'f.date_lim_reglement'=>"Date",'f.total_ht'=>"Numeric",'f.total_ttc'=>"Numeric",'f.total_tva'=>"Numeric",
228 // 'f.paye'=>"Boolean",'f.fk_statut'=>'Status','f.note_private'=>"Text",'f.note_public'=>"Text",'fd.description'=>"Text",'fd.subprice'=>"Numeric",
229 // 'fd.tva_tx'=>"Numeric",'fd.qty'=>"Numeric",'fd.total_ht'=>"Numeric",'fd.total_tva'=>"Numeric",'fd.total_ttc'=>"Numeric",'fd.date_start'=>"Date",
230 // 'fd.date_end'=>"Date",'fd.special_code'=>'Numeric','fd.product_type'=>"Numeric",'fd.fk_product'=>'List:product:label','p.ref'=>'Text','p.label'=>'Text',
231 // 'p.accountancy_code_sell'=>'Text'
232 //);
233 // $this->export_entities_array[$r]=array(
234 // 's.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','s.fk_pays'=>'company','s.phone'=>'company',
235 // 's.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.code_compta'=>'company','s.code_compta_fournisseur'=>'company',
236 // 'f.rowid'=>"invoice",'f.ref'=>"invoice",'f.datec'=>"invoice",'f.datef'=>"invoice",'f.total_ht'=>"invoice",'f.total_ttc'=>"invoice",
237 // 'f.total_tva'=>"invoice",'f.paye'=>"invoice",'f.fk_statut'=>'invoice','f.note'=>"invoice",'fd.rowid'=>'invoice_line','fd.description'=>"invoice_line",
238 // 'fd.price'=>"invoice_line",'fd.total_ht'=>"invoice_line",'fd.total_tva'=>"invoice_line",'fd.total_ttc'=>"invoice_line",'fd.tva_tx'=>"invoice_line",
239 // 'fd.qty'=>"invoice_line",'fd.date_start'=>"invoice_line",'fd.date_end'=>"invoice_line",'fd.fk_product'=>'product','p.ref'=>'product'
240 //);
241 // $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
242 // $this->export_sql_start[$r]='SELECT DISTINCT ';
243 // $this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'facturedet as fd, '.MAIN_DB_PREFIX.'societe as s)';
244 // $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
245 // $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
246 // $this->export_sql_order[$r] .=' ORDER BY s.nom';
247 // $r++;
248 }
249
258 public function init($options = '')
259 {
260 $sql = array();
261
262 $res = $this->_init($sql, $options);
263
264 if ($res) {
265 $this->createFirstCurrency();
266 }
267
268 return $res;
269 }
270
279 public function remove($options = '')
280 {
281 $sql = array();
282
283 return $this->_remove($sql, $options);
284 }
285
292 private function createFirstCurrency()
293 {
294 global $user, $langs;
295
296 $multicurrency = new MultiCurrency($this->db);
297
298 if (! $multicurrency->checkCodeAlreadyExists(getDolCurrency())) {
299 $langs->loadCacheCurrencies('');
300
301 $multicurrency->code = getDolCurrency();
302 $multicurrency->name = $langs->cache_currencies[getDolCurrency()]['label'].' ('.$langs->getCurrencySymbol(getDolCurrency()).')';
303 $r = $multicurrency->create($user);
304
305 if ($r > 0) {
306 $multicurrency->addRate(1);
307 } else {
308 return 0;
309 }
310 }
311 return 1;
312 }
313}
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
_remove($array_sql, $options='')
Disable function.
Class Currency.
Description and activation class for module MyModule.
init($options='')
Function called when module is enabled.
createFirstCurrency()
Function called when module is enabled Create the currency from general setting.
__construct($db)
Constructor.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
getDolCurrency()
Return the main currency ('EUR', 'USD', ...)
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:128