Passed
Branch develop (5cbde9)
by
unknown
26:38
created

modFacture::__construct()   F

Complexity

Conditions 21
Paths 1024

Size

Total Lines 323
Code Lines 249

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 249
c 0
b 0
f 0
nc 1024
nop 1
dl 0
loc 323
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2003-2004	Rodolphe Quiedeville	<[email protected]>
3
 * Copyright (C) 2004-2018	Laurent Destailleur		<[email protected]>
4
 * Copyright (C) 2004		Sebastien Di Cintio		<[email protected]>
5
 * Copyright (C) 2004		Benoit Mortier			<[email protected]>
6
 * Copyright (C) 2005-2012	Regis Houssin			<[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 * 		\defgroup   facture     Module invoices
24
 *      \brief      Module pour gerer les factures clients et/ou fournisseurs
25
 *      \file       htdocs/core/modules/modFacture.class.php
26
 *		\ingroup    facture
27
 *		\brief      Fichier de la classe de description et activation du module Facture
28
 */
29
include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
30
31
32
/**
33
 *  Class to describe module customer invoices
34
 */
35
class modFacture extends DolibarrModules
36
{
37
38
	/**
39
	 *   Constructor. Define names, constants, directories, boxes, permissions
40
	 *
41
	 *   @param      DoliDB		$db      Database handler
42
	 */
43
	public function __construct($db)
44
	{
45
		global $conf, $user;
46
47
		$this->db = $db;
48
		$this->numero = 30;
49
50
		$this->family = "financial";
51
		$this->module_position = '10';
52
		// Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
53
		$this->name = preg_replace('/^mod/i', '', get_class($this));
54
		$this->description = "Gestion des factures";
55
56
		// Possible values for version are: 'development', 'experimental', 'dolibarr' or version
57
		$this->version = 'dolibarr';
58
59
		$this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
60
		$this->picto = 'bill';
61
62
		// Data directories to create when module is enabled
63
		$this->dirs = array("/facture/temp");
64
65
		// Dependencies
66
		$this->depends = array('always'=>"modSociete");
67
		$this->requiredby = array("modComptabilite", "modAccounting");
0 ignored issues
show
Documentation Bug introduced by
It seems like array('modComptabilite', 'modAccounting') of type array<integer,string> is incompatible with the declared type integer[] of property $requiredby.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
		$this->conflictwith = array();
69
		$this->langfiles = array("bills", "companies", "compta", "products");
70
		$this->warnings_activation = array(); // Warning to show when we activate module. array('always'='text') or array('FR'='text')
71
		$this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='text')
72
73
		// Config pages
74
		$this->config_page_url = array("facture.php");
75
76
		// Constants
77
		$this->const = array();
78
		$r = 0;
79
80
		$this->const[$r][0] = "FACTURE_ADDON";
81
		$this->const[$r][1] = "chaine";
82
		$this->const[$r][2] = "mod_facture_terre";
83
		$this->const[$r][3] = 'Name of numbering numerotation rules of invoice';
84
		$this->const[$r][4] = 0;
85
		$r++;
86
87
		$this->const[$r][0] = "FACTURE_ADDON_PDF";
88
		$this->const[$r][1] = "chaine";
89
		$this->const[$r][2] = "crabe";
90
		$this->const[$r][3] = 'Name of PDF model of invoice';
91
		$this->const[$r][4] = 0;
92
		$r++;
93
94
		$this->const[$r][0] = "FACTURE_ADDON_PDF_ODT_PATH";
95
		$this->const[$r][1] = "chaine";
96
		$this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/invoices";
97
		$this->const[$r][3] = "";
98
		$this->const[$r][4] = 0;
99
		$r++;
100
101
		/*$this->const[$r][0] = "FACTURE_DRAFT_WATERMARK";
102
		$this->const[$r][1] = "chaine";
103
		$this->const[$r][2] = "__(Draft)__";
104
		$this->const[$r][3] = 'Watermark to show on draft invoices';
105
		$this->const[$r][4] = 0;
106
		$r++;*/
107
108
109
		// Boxes
110
		//$this->boxes = array(0=>array(1=>'box_factures_imp.php'),1=>array(1=>'box_factures.php'));
111
		$this->boxes = array(
112
				0=>array('file'=>'box_factures_imp.php', 'enabledbydefaulton'=>'Home'),
113
				1=>array('file'=>'box_factures.php', 'enabledbydefaulton'=>'Home'),
114
				2=>array('file'=>'box_graph_invoices_permonth.php', 'enabledbydefaulton'=>'Home')
115
		);
116
117
		// Cronjobs
118
		$arraydate=dol_getdate(dol_now());
119
		$datestart=dol_mktime(23, 0, 0, $arraydate['mon'], $arraydate['mday'], $arraydate['year']);
120
		$this->cronjobs = array(
121
			0=>array('label'=>'RecurringInvoices', 'jobtype'=>'method', 'class'=>'compta/facture/class/facture-rec.class.php', 'objectname'=>'FactureRec', 'method'=>'createRecurringInvoices', 'parameters'=>'', 'comment'=>'Generate recurring invoices', 'frequency'=>1, 'unitfrequency'=>3600 * 24, 'priority'=>50, 'status'=>1, 'test'=>'$conf->facture->enabled', 'datestart'=>$datestart),
122
		);
123
124
		// Permissions
125
		$this->rights = array();
126
		$this->rights_class = 'facture';
127
		$r = 0;
128
129
		$r++;
130
		$this->rights[$r][0] = 11;
131
		$this->rights[$r][1] = 'Read invoices';
132
		$this->rights[$r][2] = 'a';
133
		$this->rights[$r][3] = 0;
134
		$this->rights[$r][4] = 'lire';
135
136
		$r++;
137
		$this->rights[$r][0] = 12;
138
		$this->rights[$r][1] = 'Create and update invoices';
139
		$this->rights[$r][2] = 'a';
140
		$this->rights[$r][3] = 0;
141
		$this->rights[$r][4] = 'creer';
142
143
		// There is a particular permission for unvalidate because this may be not forbidden by some laws
144
		$r++;
145
		$this->rights[$r][0] = 13;
146
		$this->rights[$r][1] = 'Devalidate invoices';
147
		$this->rights[$r][2] = 'a';
148
		$this->rights[$r][3] = 0;
149
        $this->rights[$r][4] = 'invoice_advance';
150
		$this->rights[$r][5] = 'unvalidate';
151
152
		$r++;
153
		$this->rights[$r][0] = 14;
154
		$this->rights[$r][1] = 'Validate invoices';
155
		$this->rights[$r][2] = 'a';
156
		$this->rights[$r][3] = 0;
157
		$this->rights[$r][4] = 'invoice_advance';
158
		$this->rights[$r][5] = 'validate';
159
160
		$r++;
161
		$this->rights[$r][0] = 15;
162
		$this->rights[$r][1] = 'Send invoices by email';
163
		$this->rights[$r][2] = 'a';
164
		$this->rights[$r][3] = 0;
165
		$this->rights[$r][4] = 'invoice_advance';
166
        $this->rights[$r][5] = 'send';
167
168
		$r++;
169
		$this->rights[$r][0] = 16;
170
		$this->rights[$r][1] = 'Issue payments on invoices';
171
		$this->rights[$r][2] = 'a';
172
		$this->rights[$r][3] = 0;
173
		$this->rights[$r][4] = 'paiement';
174
175
		$r++;
176
		$this->rights[$r][0] = 19;
177
		$this->rights[$r][1] = 'Delete invoices';
178
		$this->rights[$r][2] = 'a';
179
		$this->rights[$r][3] = 0;
180
		$this->rights[$r][4] = 'supprimer';
181
182
		$r++;
183
		$this->rights[$r][0] = 1321;
184
		$this->rights[$r][1] = 'Export customer invoices, attributes and payments';
185
		$this->rights[$r][2] = 'r';
186
		$this->rights[$r][3] = 0;
187
		$this->rights[$r][4] = 'facture';
188
		$this->rights[$r][5] = 'export';
189
190
		$r++;
191
		$this->rights[$r][0] = 1322;
192
		$this->rights[$r][1] = 'Re-open a fully paid invoice';
193
		$this->rights[$r][2] = 'r';
194
		$this->rights[$r][3] = 0;
195
		$this->rights[$r][4] = 'invoice_advance';
196
		$this->rights[$r][5] = 'reopen';
197
198
199
		// Menus
200
		//-------
201
		$this->menu = 1; // This module add menu entries. They are coded into menu manager.
0 ignored issues
show
Documentation Bug introduced by
It seems like 1 of type integer is incompatible with the declared type array of property $menu.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
202
203
204
		// Exports
205
		//--------
206
		$r = 1;
207
208
		$this->export_code[$r] = $this->rights_class.'_'.$r;
209
		$this->export_label[$r] = 'CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
210
		$this->export_icon[$r] = 'invoice';
211
		$this->export_permission[$r] = array(array("facture", "facture", "export", "other"));
212
		$this->export_fields_array[$r] = array(
213
		    's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 's.code_client'=>'CustomerCode', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 'cd.nom'=>'State',
214
		    's.phone'=>'Phone',
215
			's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.code_compta'=>'CustomerAccountancyCode',
216
			's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
217
			'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef", 'f.ref_client'=>'RefCustomer',
218
			'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total'=>"TotalHT",
219
			'f.total_ttc'=>"TotalTTC", 'f.tva'=>"TotalVAT", 'f.localtax1'=>'LocalTax1', 'f.localtax2'=>'LocalTax2', 'none.rest'=>'Rest', 'f.paye'=>"InvoicePaid", 'f.fk_statut'=>'InvoiceStatus',
220
			'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
221
			'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'fd.rowid'=>'LineId', 'fd.description'=>"LineDescription",
222
			'fd.subprice'=>"LineUnitPrice", 'fd.tva_tx'=>"LineVATRate", 'fd.qty'=>"LineQty", 'fd.total_ht'=>"LineTotalHT", 'fd.total_tva'=>"LineTotalVAT",
223
			'fd.total_ttc'=>"LineTotalTTC", 'fd.date_start'=>"DateStart", 'fd.date_end'=>"DateEnd", 'fd.special_code'=>'SpecialCode',
224
			'fd.product_type'=>"TypeOfLineServiceOrProduct", 'fd.fk_product'=>'ProductId', 'p.ref'=>'ProductRef', 'p.label'=>'ProductLabel',
225
			'p.accountancy_code_sell'=>'ProductAccountancySellCode'
226
		);
227
		if (! empty($conf->multicurrency->enabled))
228
		{
229
		    $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
230
		    $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
231
		    $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
232
		    $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
233
		    $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
234
		}
235
		if (! empty($conf->cashdesk->enabled) || ! empty($conf->takepos->enabled) || ! empty($conf->global->INVOICE_SHOW_POS_IN_EXPORT))
236
		{
237
			$this->export_fields_array[$r]['f.module_source']='POSModule';
238
			$this->export_fields_array[$r]['f.pos_source']='POSTerminal';
239
		}
240
		$this->export_TypeFields_array[$r] = array(
241
			's.rowid'=>'Numeric', 's.nom'=>'Text', 's.code_client'=>'Text', 's.address'=>'Text', 's.zip'=>'Text', 's.town'=>'Text', 'c.code'=>'Text', 'cd.nom'=>'Text', 's.phone'=>'Text', 's.siren'=>'Text',
242
			's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
243
			'f.rowid'=>'Numeric', 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
244
			'f.total'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.tva'=>"Numeric", 'f.localtax1'=>'Numeric', 'f.localtax2'=>'Numeric', 'none.rest'=>"NumericCompute", 'f.paye'=>"Boolean", 'f.fk_statut'=>'Numeric',
245
			'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
246
			'pj.ref'=>'Text', 'pj.title'=>'Text', 'fd.rowid'=>'Numeric', 'fd.label'=>'Text', 'fd.description'=>"Text", 'fd.subprice'=>"Numeric", 'fd.tva_tx'=>"Numeric",
247
			'fd.qty'=>"Numeric", 'fd.total_ht'=>"Numeric", 'fd.total_tva'=>"Numeric", 'fd.total_ttc'=>"Numeric", 'fd.date_start'=>"Date", 'fd.date_end'=>"Date",
248
			'fd.special_code'=>'Numeric', 'fd.product_type'=>"Numeric", 'fd.fk_product'=>'List:product:label', 'p.ref'=>'Text', 'p.label'=>'Text',
249
			'p.accountancy_code_sell'=>'Text'
250
		);
251
		if (! empty($conf->cashdesk->enabled) || ! empty($conf->takepos->enabled) || ! empty($conf->global->INVOICE_SHOW_POS_IN_EXPORT))
252
		{
253
			$this->export_TypeFields_array[$r]['f.module_source']='Text';
254
			$this->export_TypeFields_array[$r]['f.pos_source']='Text';
255
		}
256
		$this->export_entities_array[$r] = array(
257
			's.rowid'=>"company", 's.nom'=>'company', 's.code_client'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 'cd.nom'=>'company', 's.phone'=>'company',
258
			's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
259
			's.tva_intra'=>'company', 'pj.ref'=>'project', 'pj.title'=>'project', 'fd.rowid'=>'invoice_line', 'fd.label'=>"invoice_line", 'fd.description'=>"invoice_line",
260
			'fd.subprice'=>"invoice_line", 'fd.total_ht'=>"invoice_line", 'fd.total_tva'=>"invoice_line", 'fd.total_ttc'=>"invoice_line", 'fd.tva_tx'=>"invoice_line",
261
			'fd.qty'=>"invoice_line", 'fd.date_start'=>"invoice_line", 'fd.date_end'=>"invoice_line", 'fd.special_code'=>'invoice_line',
262
			'fd.product_type'=>'invoice_line', 'fd.fk_product'=>'product', 'p.ref'=>'product', 'p.label'=>'product', 'p.accountancy_code_sell'=>'product',
263
			'f.fk_user_author'=>'user', 'uc.login'=>'user', 'f.fk_user_valid'=>'user', 'uv.login'=>'user'
264
		);
265
		$this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
266
		$this->export_dependencies_array[$r] = array('invoice_line'=>'fd.rowid', 'product'=>'fd.rowid', 'none.rest'=>array('f.rowid', 'f.total_ttc')); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
267
		$keyforselect = 'facture'; $keyforelement = 'invoice'; $keyforaliasextra = 'extra';
268
		include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
269
		$keyforselect = 'facturedet'; $keyforelement = 'invoice_line'; $keyforaliasextra = 'extra2';
270
		include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
271
		$keyforselect = 'product'; $keyforelement = 'product'; $keyforaliasextra = 'extra3';
272
		include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
273
		$this->export_sql_start[$r] = 'SELECT DISTINCT ';
274
		$this->export_sql_end[$r]  = ' FROM '.MAIN_DB_PREFIX.'societe as s';
275
		if (empty($user->rights->societe->client->voir)) $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
276
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
277
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
278
		$this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'facture as f';
279
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
280
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
281
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
282
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
283
		$this->export_sql_end[$r] .= ' , '.MAIN_DB_PREFIX.'facturedet as fd';
284
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet_extrafields as extra2 on fd.rowid = extra2.fk_object';
285
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
286
		$this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra3 on p.rowid = extra3.fk_object';
287
		$this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
288
		$this->export_sql_end[$r] .= ' AND f.entity IN ('.getEntity('invoice').')';
289
		if (isset($user) && empty($user->rights->societe->client->voir)) $this->export_sql_end[$r] .= ' AND sc.fk_user = '.$user->id;
290
		$r++;
291
292
		$this->export_code[$r] = $this->rights_class.'_'.$r;
293
		$this->export_label[$r] = 'CustomersInvoicesAndPayments'; // Translation key (used only if key ExportDataset_xxx_z not found)
294
		$this->export_icon[$r] = 'invoice';
295
		$this->export_permission[$r] = array(array("facture", "facture", "export"));
296
		$this->export_fields_array[$r] = array(
297
		    's.rowid'=>"IdCompany", 's.nom'=>'CompanyName', 's.code_client'=>'CustomerCode', 's.address'=>'Address', 's.zip'=>'Zip', 's.town'=>'Town', 'c.code'=>'CountryCode', 'cd.nom'=>'State',
298
		    's.phone'=>'Phone',
299
			's.siren'=>'ProfId1', 's.siret'=>'ProfId2', 's.ape'=>'ProfId3', 's.idprof4'=>'ProfId4', 's.code_compta'=>'CustomerAccountancyCode',
300
			's.code_compta_fournisseur'=>'SupplierAccountancyCode', 's.tva_intra'=>'VATIntra',
301
			'f.rowid'=>"InvoiceId", 'f.ref'=>"InvoiceRef",  'f.ref_client'=>'RefCustomer',
302
			'f.type'=>"Type", 'f.datec'=>"InvoiceDateCreation", 'f.datef'=>"DateInvoice", 'f.date_lim_reglement'=>"DateDue", 'f.total'=>"TotalHT",
303
			'f.total_ttc'=>"TotalTTC", 'f.tva'=>"TotalVAT", 'f.localtax1'=>'LocalTax1', 'f.localtax2'=>'LocalTax2', 'none.rest'=>'Rest', 'f.paye'=>"InvoicePaid", 'f.fk_statut'=>'InvoiceStatus',
304
			'f.note_private'=>"NotePrivate", 'f.note_public'=>"NotePublic", 'f.fk_user_author'=>'CreatedById', 'uc.login'=>'CreatedByLogin',
305
			'f.fk_user_valid'=>'ValidatedById', 'uv.login'=>'ValidatedByLogin', 'pj.ref'=>'ProjectRef', 'pj.title'=>'ProjectLabel', 'p.rowid'=>'PaymentId', 'p.ref'=>'PaymentRef',
306
			'p.amount'=>'AmountPayment', 'pf.amount'=>'AmountPaymentDistributedOnInvoice', 'p.datep'=>'DatePayment', 'p.num_paiement'=>'PaymentNumber',
307
			'pt.code'=>'CodePaymentMode', 'pt.libelle'=>'LabelPaymentMode', 'p.note'=>'PaymentNote', 'p.fk_bank'=>'IdTransaction', 'ba.ref'=>'AccountRef'
308
		);
309
		if (! empty($conf->multicurrency->enabled))
310
		{
311
		    $this->export_fields_array[$r]['f.multicurrency_code'] = 'Currency';
312
		    $this->export_fields_array[$r]['f.multicurrency_tx'] = 'CurrencyRate';
313
		    $this->export_fields_array[$r]['f.multicurrency_total_ht'] = 'MulticurrencyAmountHT';
314
		    $this->export_fields_array[$r]['f.multicurrency_total_tva'] = 'MulticurrencyAmountVAT';
315
		    $this->export_fields_array[$r]['f.multicurrency_total_ttc'] = 'MulticurrencyAmountTTC';
316
		}
317
		if (! empty($conf->cashdesk->enabled) || ! empty($conf->takepos->enabled) || ! empty($conf->global->INVOICE_SHOW_POS_IN_EXPORT))
318
		{
319
			$this->export_fields_array[$r]['f.module_source']='POSModule';
320
			$this->export_fields_array[$r]['f.pos_source']='POSTerminal';
321
		}
322
		$this->export_TypeFields_array[$r] = array(
323
		    's.rowid'=>'Numeric', 's.nom'=>'Text', 's.code_client'=>'Text', 's.address'=>'Text', 's.zip'=>'Text', 's.town'=>'Text', 'c.code'=>'Text', 'cd.nom'=>'Text', 's.phone'=>'Text', 's.siren'=>'Text',
324
			's.siret'=>'Text', 's.ape'=>'Text', 's.idprof4'=>'Text', 's.code_compta'=>'Text', 's.code_compta_fournisseur'=>'Text', 's.tva_intra'=>'Text',
325
			'f.rowid'=>"Numeric", 'f.ref'=>"Text", 'f.ref_client'=>'Text', 'f.type'=>"Numeric", 'f.datec'=>"Date", 'f.datef'=>"Date", 'f.date_lim_reglement'=>"Date",
326
			'f.total'=>"Numeric", 'f.total_ttc'=>"Numeric", 'f.tva'=>"Numeric", 'f.localtax1'=>'Numeric', 'f.localtax2'=>'Numeric', 'none.rest'=>'NumericCompute', 'f.paye'=>"Boolean", 'f.fk_statut'=>'Status',
327
			'f.note_private'=>"Text", 'f.note_public'=>"Text", 'f.fk_user_author'=>'Numeric', 'uc.login'=>'Text', 'f.fk_user_valid'=>'Numeric', 'uv.login'=>'Text',
328
			'pj.ref'=>'Text', 'p.amount'=>'Numeric', 'pf.amount'=>'Numeric', 'p.rowid'=>'Numeric', 'p.ref'=>'Text', 'p.title'=>'Text', 'p.datep'=>'Date', 'p.num_paiement'=>'Numeric',
329
			'p.fk_bank'=>'Numeric', 'p.note'=>'Text', 'pt.code'=>'Text', 'pt.libelle'=>'text', 'ba.ref'=>'Text'
330
		);
331
		if (! empty($conf->cashdesk->enabled) || ! empty($conf->takepos->enabled) || ! empty($conf->global->INVOICE_SHOW_POS_IN_EXPORT))
332
		{
333
			$this->export_fields_array[$r]['f.module_source']='POSModule';
334
			$this->export_fields_array[$r]['f.pos_source']='POSTerminal';
335
		}
336
		$this->export_entities_array[$r] = array(
337
			's.rowid'=>"company", 's.nom'=>'company', 's.code_client'=>'company', 's.address'=>'company', 's.zip'=>'company', 's.town'=>'company', 'c.code'=>'company', 'cd.nom'=>'company', 's.phone'=>'company',
338
			's.siren'=>'company', 's.siret'=>'company', 's.ape'=>'company', 's.idprof4'=>'company', 's.code_compta'=>'company', 's.code_compta_fournisseur'=>'company',
339
			's.tva_intra'=>'company', 'pj.ref'=>'project', 'p.title'=>'project', 'p.rowid'=>'payment', 'p.ref'=>'payment', 'p.amount'=>'payment', 'pf.amount'=>'payment', 'p.datep'=>'payment',
340
			'p.num_paiement'=>'payment', 'pt.code'=>'payment', 'pt.libelle'=>'payment', 'p.note'=>'payment', 'f.fk_user_author'=>'user', 'uc.login'=>'user',
341
			'f.fk_user_valid'=>'user', 'uv.login'=>'user', 'p.fk_bank'=>'account', 'ba.ref'=>'account'
342
		);
343
		$this->export_special_array[$r] = array('none.rest'=>'getRemainToPay');
344
		$this->export_dependencies_array[$r] = array('payment'=>'p.rowid', 'none.rest'=>array('f.rowid', 'f.total_ttc')); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
345
		$keyforselect = 'facture'; $keyforelement = 'invoice'; $keyforaliasextra = 'extra';
346
		include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
347
		$this->export_sql_start[$r]='SELECT DISTINCT ';
348
		$this->export_sql_end[$r]  =' FROM '.MAIN_DB_PREFIX.'societe as s';
349
		if (empty($user->rights->societe->client->voir)) $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'societe_commerciaux as sc ON sc.fk_soc = s.rowid';
350
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c on s.fk_pays = c.rowid';
351
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as cd on s.fk_departement = cd.rowid,';
352
		$this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'facture as f';
353
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pj ON f.fk_projet = pj.rowid';
354
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as uc ON f.fk_user_author = uc.rowid';
355
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'user as uv ON f.fk_user_valid = uv.rowid';
356
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'facture_extrafields as extra ON f.rowid = extra.fk_object';
357
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid';
358
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'paiement as p ON pf.fk_paiement = p.rowid';
359
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as pt ON pt.id = p.fk_paiement';
360
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON b.rowid = p.fk_bank';
361
		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON ba.rowid = b.fk_account';
362
		$this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid';
363
		$this->export_sql_end[$r] .=' AND f.entity IN ('.getEntity('invoice').')';
364
		if (isset($user) && empty($user->rights->societe->client->voir)) $this->export_sql_end[$r] .=' AND sc.fk_user = '.$user->id;
365
		$r++;
366
	}
367
368
369
	/**
370
	 *  Function called when module is enabled.
371
	 *  The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
372
	 *  It also creates data directories
373
	 *
374
     *  @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
375
	 *  @return     int             	1 if OK, 0 if KO
376
	 */
377
	public function init($options = '')
378
	{
379
		global $conf, $langs;
380
381
		// Remove permissions and default values
382
		$this->remove($options);
383
384
		//ODT template
385
		$src = DOL_DOCUMENT_ROOT.'/install/doctemplates/invoices/template_invoice.odt';
386
		$dirodt = DOL_DATA_ROOT.'/doctemplates/invoices';
387
		$dest = $dirodt.'/template_invoice.odt';
388
389
		if (file_exists($src) && !file_exists($dest))
390
		{
391
			require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
392
			dol_mkdir($dirodt);
393
			$result = dol_copy($src, $dest, 0, 0);
394
			if ($result < 0)
395
			{
396
				$langs->load("errors");
397
				$this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
398
				return 0;
399
			}
400
		}
401
402
		$sql = array(
403
				"DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'invoice' AND entity = ".$conf->entity,
404
				"INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','invoice',".$conf->entity.")"
405
		);
406
407
		return $this->_init($sql, $options);
408
	}
409
}
410