Completed
Branch develop (c022b0)
by
unknown
40:00
created

CommonDocGenerator::get_substitutionarray_user()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2003-2005	Rodolphe Quiedeville	<[email protected]>
3
 * Copyright (C) 2004-2010	Laurent Destailleur	<[email protected]>
4
 * Copyright (C) 2004		Eric Seigne		<[email protected]>
5
 * Copyright (C) 2005-2012	Regis Houssin		<[email protected]>
6
 * Copyright (C) 2015       	Marcos García           <[email protected]>
7
 * Copyright (C) 2016       	Charlie Benke           <[email protected]>
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 <http://www.gnu.org/licenses/>.
21
 * or see http://www.gnu.org/
22
 */
23
24
/**
25
 *	    \file       htdocs/core/class/commondocgenerator.class.php
26
 *		\ingroup    core
27
 *		\brief      File of parent class for documents generators
28
 */
29
30
31
/**
32
 *	Parent class for documents generators
33
 */
34
abstract class CommonDocGenerator
35
{
36
	var $error='';
37
	protected $db;
38
39
40
	/**
41
	 *	Constructor
42
	 *
43
	 *  @param		DoliDB		$db      Database handler
44
	*/
45
	public function __construct($db) {
46
		$this->db = $db;
47
		return 1;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
48
	}
49
50
51
    /**
52
     * Define array with couple subtitution key => subtitution value
53
     *
54
     * @param   User		$user           User
55
     * @param   Translate	$outputlangs    Language object for output
56
     * @return	array						Array of substitution key->code
57
     */
58
    function get_substitutionarray_user($user,$outputlangs)
59
    {
60
        global $conf;
61
62
        $logotouse=$conf->user->dir_output.'/'.get_exdir($user->id, 2, 0, 1, $user, 'user').'/'.$user->photo;
63
64
        return array(
65
            'myuser_lastname'=>$user->lastname,
66
            'myuser_firstname'=>$user->firstname,
67
            'myuser_fullname'=>$user->getFullName($outputlangs,1),
68
            'myuser_login'=>$user->login,
69
            'myuser_phone'=>$user->office_phone,
70
       		'myuser_address'=>$user->address,
71
       		'myuser_zip'=>$user->zip,
72
       		'myuser_town'=>$user->town,
73
       		'myuser_country'=>$user->country,
74
        	'myuser_country_code'=>$user->country_code,
75
       		'myuser_state'=>$user->state,
76
        	'myuser_state_code'=>$user->state_code,
77
        	'myuser_fax'=>$user->office_fax,
78
            'myuser_mobile'=>$user->user_mobile,
79
            'myuser_email'=>$user->email,
80
        	'myuser_logo'=>$logotouse,
81
        	'myuser_job'=>$user->job,
82
            'myuser_web'=>''	// url not exist in $user object
83
        );
84
    }
85
86
87
    /**
88
     * Define array with couple subtitution key => subtitution value
89
     *
90
     * @param   Societe		$mysoc			Object thirdparty
91
     * @param   Translate	$outputlangs    Language object for output
92
     * @return	array						Array of substitution key->code
93
     */
94
    function get_substitutionarray_mysoc($mysoc,$outputlangs)
95
    {
96
        global $conf;
97
98
        if (empty($mysoc->forme_juridique) && ! empty($mysoc->forme_juridique_code))
99
        {
100
            $mysoc->forme_juridique=getFormeJuridiqueLabel($mysoc->forme_juridique_code);
101
        }
102
        if (empty($mysoc->country) && ! empty($mysoc->country_code))
103
        {
104
        	$mysoc->country=$outputlangs->transnoentitiesnoconv("Country".$mysoc->country_code);
105
        }
106
        if (empty($mysoc->state) && ! empty($mysoc->state_code))
107
        {
108
        	$mysoc->state=getState($mysoc->state_code,0);
109
        }
110
111
        $logotouse=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
112
113
        return array(
114
            'mycompany_logo'=>$logotouse,
115
            'mycompany_name'=>$mysoc->name,
116
            'mycompany_email'=>$mysoc->email,
117
            'mycompany_phone'=>$mysoc->phone,
118
            'mycompany_fax'=>$mysoc->fax,
119
            'mycompany_address'=>$mysoc->address,
120
            'mycompany_zip'=>$mysoc->zip,
121
            'mycompany_town'=>$mysoc->town,
122
            'mycompany_country'=>$mysoc->country,
123
            'mycompany_country_code'=>$mysoc->country_code,
124
            'mycompany_state'=>$mysoc->state,
125
            'mycompany_state_code'=>$mysoc->state_code,
126
        	'mycompany_web'=>$mysoc->url,
127
            'mycompany_juridicalstatus'=>$mysoc->forme_juridique,
128
            'mycompany_managers'=>$mysoc->managers,
129
            'mycompany_capital'=>$mysoc->capital,
130
            'mycompany_barcode'=>$mysoc->barcode,
131
            'mycompany_idprof1'=>$mysoc->idprof1,
132
            'mycompany_idprof2'=>$mysoc->idprof2,
133
            'mycompany_idprof3'=>$mysoc->idprof3,
134
            'mycompany_idprof4'=>$mysoc->idprof4,
135
            'mycompany_idprof5'=>$mysoc->idprof5,
136
            'mycompany_idprof6'=>$mysoc->idprof6,
137
        	'mycompany_vatnumber'=>$mysoc->tva_intra,
138
			'mycompany_object'=>$mysoc->object,
139
            'mycompany_note_private'=>$mysoc->note_private,
140
            //'mycompany_note_public'=>$mysoc->note_public,        // Only private not exists for "mysoc" but both for thirdparties
141
        );
142
    }
143
144
145
    /**
146
     * Define array with couple subtitution key => subtitution value
147
     *
148
     * @param	Object		$object			Object
149
     * @param   Translate	$outputlangs    Language object for output
150
     * @return	array						Array of substitution key->code
151
     */
152
    function get_substitutionarray_thirdparty($object,$outputlangs)
153
    {
154
        global $conf;
155
156
        if (empty($object->country) && ! empty($object->country_code))
157
        {
158
        	$object->country=$outputlangs->transnoentitiesnoconv("Country".$object->country_code);
159
        }
160
        if (empty($object->state) && ! empty($object->state_code))
161
        {
162
        	$object->state=getState($object->state_code,0);
163
        }
164
165
        $array_thirdparty = array(
166
            'company_name'=>$object->name,
167
	        'company_name_alias' => $object->name_alias,
168
            'company_email'=>$object->email,
169
            'company_phone'=>$object->phone,
170
            'company_fax'=>$object->fax,
171
            'company_address'=>$object->address,
172
            'company_zip'=>$object->zip,
173
            'company_town'=>$object->town,
174
            'company_country'=>$object->country,
175
        	'company_country_code'=>$object->country_code,
176
            'company_state'=>$object->state,
177
        	'company_state_code'=>$object->state_code,
178
        	'company_web'=>$object->url,
179
            'company_barcode'=>$object->barcode,
180
            'company_vatnumber'=>$object->tva_intra,
181
            'company_customercode'=>$object->code_client,
182
            'company_suppliercode'=>$object->code_fournisseur,
183
            'company_customeraccountancycode'=>$object->code_compta,
184
            'company_supplieraccountancycode'=>$object->code_compta_fournisseur,
185
            'company_juridicalstatus'=>$object->forme_juridique,
186
            'company_outstanding_limit'=>$object->outstanding_limit,
187
            'company_capital'=>$object->capital,
188
            'company_idprof1'=>$object->idprof1,
189
            'company_idprof2'=>$object->idprof2,
190
            'company_idprof3'=>$object->idprof3,
191
            'company_idprof4'=>$object->idprof4,
192
            'company_idprof5'=>$object->idprof5,
193
            'company_idprof6'=>$object->idprof6,
194
            'company_note_public'=>$object->note_public,
195
            'company_note_private'=>$object->note_private,
196
            'company_default_bank_iban'=>$object->bank_account->iban,
197
            'company_default_bank_bic'=>$object->bank_account->bic
198
        );
199
200
        // Retrieve extrafields
201
        if(is_array($object->array_options) && count($object->array_options))
202
        {
203
        	require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
204
        	$extrafields = new ExtraFields($this->db);
205
        	$extralabels = $extrafields->fetch_name_optionals_label('societe',true);
206
        	$object->fetch_optionals($object->id,$extralabels);
207
208
        	foreach($extrafields->attribute_label as $key=>$label)
209
        	{
210
        		if($extrafields->attribute_type[$key] == 'price')
211
        		{
212
        			$object->array_options['options_'.$key] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
213
        		}
214
        		else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
215
        		{
216
        			$object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
217
        		}
218
        		$array_thirdparty = array_merge($array_thirdparty, array ('company_options_'.$key => $object->array_options ['options_' . $key]));
219
			}
220
		}
221
		return $array_thirdparty;
222
	}
223
224
	/**
225
	 * Define array with couple subtitution key => subtitution value
226
	 *
227
	 * @param	Contact 		$object        	contact
228
	 * @param	Translate 	$outputlangs   	object for output
229
	 * @param   array_key	$array_key	    Name of the key for return array
230
	 * @return	array of substitution key->code
231
	 */
232
	function get_substitutionarray_contact($object, $outputlangs, $array_key = 'object') {
233
		global $conf;
234
235
		if(empty($object->country) && ! empty($object->country_code))
236
		{
237
			$object->country = $outputlangs->transnoentitiesnoconv("Country" . $object->country_code);
238
		}
239
		if(empty($object->state) && ! empty($object->state_code))
240
		{
241
			$object->state = getState($object->state_code, 0);
242
		}
243
244
		$array_contact = array (
245
		    $array_key . '_fullname' => $object->getFullName($outputlangs, 1),
246
            $array_key . '_lastname' => $object->lastname,
247
            $array_key . '_firstname' => $object->firstname,
248
            $array_key . '_address' => $object->address,
249
            $array_key . '_zip' => $object->zip,
250
            $array_key . '_town' => $object->town,
251
            $array_key . '_state_id' => $object->state_id,
252
            $array_key . '_state_code' => $object->state_code,
253
            $array_key . '_state' => $object->state,
254
            $array_key . '_country_id' => $object->country_id,
255
            $array_key . '_country_code' => $object->country_code,
256
            $array_key . '_country' => $object->country,
257
            $array_key . '_poste' => $object->poste,
258
            $array_key . '_socid' => $object->socid,
259
            $array_key . '_statut' => $object->statut,
260
            $array_key . '_code' => $object->code,
261
            $array_key . '_email' => $object->email,
262
            $array_key . '_jabberid' => $object->jabberid,
263
            $array_key . '_phone_pro' => $object->phone_pro,
264
            $array_key . '_phone_perso' => $object->phone_perso,
265
            $array_key . '_phone_mobile' => $object->phone_mobile,
266
            $array_key . '_fax' => $object->fax,
267
            $array_key . '_birthday' => $object->birthday,
268
            $array_key . '_default_lang' => $object->default_lang,
269
            $array_key . '_note_public' => $object->note_public,
270
            $array_key . '_note_private' => $object->note_private
271
		);
272
273
		// Retrieve extrafields
274
		require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
275
		$extrafields = new ExtraFields($this->db);
276
		$extralabels = $extrafields->fetch_name_optionals_label('socpeople', true);
277
		$object->fetch_optionals($object->id, $extralabels);
278
279
		foreach($extrafields->attribute_label as $key => $label)
280
		{
281
			if ($extrafields->attribute_type[$key] == 'price')
282
			{
283
				$object->array_options['options_' . $key] = price($object->array_options ['options_' . $key], 0, $outputlangs, 0, 0, - 1, $conf->currency);
284
			}
285
			elseif($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
286
			{
287
				$object->array_options['options_' . $key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_' . $key]];
288
			}
289
			$array_contact = array_merge($array_contact, array($array_key.'_options_' . $key => $object->array_options['options_'. $key]));
290
		}
291
		return $array_contact;
292
	}
293
294
295
    /**
296
     * Define array with couple subtitution key => subtitution value
297
     *
298
     * @param   Translate	$outputlangs    Language object for output
299
     * @return	array						Array of substitution key->code
300
     */
301
    function get_substitutionarray_other($outputlangs)
302
    {
303
    	global $conf;
304
305
    	$now=dol_now('gmt');	// gmt
306
    	$array_other = array(
307
    	    // Date in default language
308
    	    'current_date'=>dol_print_date($now,'day','tzuser'),
309
    	    'current_datehour'=>dol_print_date($now,'dayhour','tzuser'),
310
   			'current_server_date'=>dol_print_date($now,'day','tzserver'),
311
   			'current_server_datehour'=>dol_print_date($now,'dayhour','tzserver'),
312
    	    // Date in requested output language
313
    	    'current_date_locale'=>dol_print_date($now,'day','tzuser',$outputlangs),
314
   			'current_datehour_locale'=>dol_print_date($now,'dayhour','tzuser',$outputlangs),
315
   			'current_server_date_locale'=>dol_print_date($now,'day','tzserver',$outputlangs),
316
   			'current_server_datehour_locale'=>dol_print_date($now,'dayhour','tzserver',$outputlangs),
317
    	);
318
319
    	return $array_other;
320
    }
321
322
323
	/**
324
	 * Define array with couple substitution key => substitution value
325
	 *
326
	 * @param   Object			$object             Main object to use as data source
327
	 * @param   Translate		$outputlangs        Lang object to use for output
328
     * @param   string		    $array_key	        Name of the key for return array
329
	 * @return	array								Array of substitution
330
	 */
331
	function get_substitutionarray_object($object,$outputlangs,$array_key='object')
332
	{
333
		global $conf;
334
335
		$sumpayed=$sumdeposit=$sumcreditnote='';
336
		if ($object->element == 'facture')
337
		{
338
			$invoice_source=new Facture($this->db);
339
			if ($object->fk_facture_source > 0)
340
			{
341
				$invoice_source->fetch($object->fk_facture_source);
342
			}
343
			$sumpayed = $object->getSommePaiement();
344
			$sumdeposit = $object->getSumDepositsUsed();
345
			$sumcreditnote = $object->getSumCreditNotesUsed();
346
		}
347
348
		$resarray=array(
349
		$array_key.'_id'=>$object->id,
350
		$array_key.'_ref'=>$object->ref,
351
		$array_key.'_ref_ext'=>$object->ref_ext,
352
		$array_key.'_ref_customer'=>$object->ref_client,
353
		$array_key.'_ref_supplier'=>(! empty($object->ref_fournisseur)?$object->ref_fournisseur:''),
354
		$array_key.'_source_invoice_ref'=>$invoice_source->ref,
0 ignored issues
show
Bug introduced by
The variable $invoice_source does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
355
		// Dates
356
        $array_key.'_hour'=>dol_print_date($object->date,'hour'),
357
		$array_key.'_date'=>dol_print_date($object->date,'day'),
358
		$array_key.'_date_rfc'=>dol_print_date($object->date,'dayrfc'),
359
		$array_key.'_date_limit'=>(! empty($object->date_lim_reglement)?dol_print_date($object->date_lim_reglement,'day'):''),
360
	    $array_key.'_date_end'=>(! empty($object->fin_validite)?dol_print_date($object->fin_validite,'day'):''),
361
		$array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
362
		$array_key.'_date_modification'=>(! empty($object->date_modification)?dol_print_date($object->date_modification,'day'):''),
363
		$array_key.'_date_validation'=>(! empty($object->date_validation)?dol_print_date($object->date_validation,'dayhour'):''),
364
		$array_key.'_date_delivery_planed'=>(! empty($object->date_livraison)?dol_print_date($object->date_livraison,'day'):''),
365
		$array_key.'_date_close'=>(! empty($object->date_cloture)?dol_print_date($object->date_cloture,'dayhour'):''),
366
367
		$array_key.'_payment_mode_code'=>$object->mode_reglement_code,
368
		$array_key.'_payment_mode'=>($outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code)!='PaymentType'.$object->mode_reglement_code?$outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code):$object->mode_reglement),
369
		$array_key.'_payment_term_code'=>$object->cond_reglement_code,
370
		$array_key.'_payment_term'=>($outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code)!='PaymentCondition'.$object->cond_reglement_code?$outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code):$object->cond_reglement),
371
372
		$array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
373
		$array_key.'_total_vat_locale'=>(! empty($object->total_vat)?price($object->total_vat, 0, $outputlangs):price($object->total_tva, 0, $outputlangs)),
374
		$array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
375
		$array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
376
		$array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
377
378
		$array_key.'_total_ht'=>price2num($object->total_ht),
379
		$array_key.'_total_vat'=>(! empty($object->total_vat)?price2num($object->total_vat):price2num($object->total_tva)),
380
		$array_key.'_total_localtax1'=>price2num($object->total_localtax1),
381
		$array_key.'_total_localtax2'=>price2num($object->total_localtax2),
382
		$array_key.'_total_ttc'=>price2num($object->total_ttc),
383
384
		$array_key.'_multicurrency_code' => price2num($object->multicurrency_code),
385
		$array_key.'_multicurrency_tx' => price2num($object->multicurrency_tx),
386
	    $array_key.'_multicurrency_total_ht' => price2num($object->multicurrency_total_ht),
387
	    $array_key.'_multicurrency_total_tva' => price2num($object->multicurrency_total_tva),
388
		$array_key.'_multicurrency_total_ttc' => price2num($object->multicurrency_total_ttc),
389
		$array_key.'_multicurrency_total_ht_locale' => price($object->multicurrency_total_ht, 0, $outputlangs),
390
		$array_key.'_multicurrency_total_tva_locale' => price($object->multicurrency_total_tva, 0, $outputlangs),
391
		$array_key.'_multicurrency_total_ttc_locale' => price($object->multicurrency_total_ttc, 0, $outputlangs),
392
393
		$array_key.'_note_private'=>$object->note,
394
		$array_key.'_note_public'=>$object->note_public,
395
		$array_key.'_note'=>$object->note_public,			// For backward compatibility
396
397
		// Payments
398
		$array_key.'_already_payed_locale'=>price($sumpayed, 0, $outputlangs),
399
		$array_key.'_already_payed'=>price2num($sumpayed),
400
		$array_key.'_already_deposit_locale'=>price($sumdeposit, 0, $outputlangs),
401
		$array_key.'_already_deposit'=>price2num($sumdeposit),
402
		$array_key.'_already_creditnote_locale'=>price($sumcreditnote, 0, $outputlangs),
403
		$array_key.'_already_creditnote'=>price2num($sumcreditnote),
404
405
		$array_key.'_already_payed_all_locale'=>price(price2num($sumpayed + $sumdeposit + $sumcreditnote, 'MT'), 0, $outputlangs),
406
		$array_key.'_already_payed_all'=> price2num(($sumpayed + $sumdeposit + $sumcreditnote), 'MT'),
407
408
		// Remain to pay with all know infrmation (except open direct debit requests)
409
		$array_key.'_remain_to_pay_locale'=>price(price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT'), 0, $outputlangs),
410
		$array_key.'_remain_to_pay'=>price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT')
411
		);
412
413
		if (method_exists($object, 'getTotalDiscount')) {
414
			$resarray[$array_key.'_total_discount_ht_locale'] = price($object->getTotalDiscount(), 0, $outputlangs);
415
			$resarray[$array_key.'_total_discount_ht'] = price2num($object->getTotalDiscount());
416
		} else {
417
			$resarray[$array_key.'_total_discount_ht_locale'] = '';
418
			$resarray[$array_key.'_total_discount_ht'] = '';
419
		}
420
421
		// Fetch project information if there is a project assigned to this object
422
		if ($object->element != "project" && ! empty($object->fk_project) && $object->fk_project > 0)
423
		{
424
			if (! is_object($object->project))
425
			{
426
				$object->fetch_projet();
427
			}
428
429
			$resarray[$array_key.'_project_ref'] = $object->project->ref;
430
			$resarray[$array_key.'_project_title'] = $object->project->title;
431
			$resarray[$array_key.'_project_description'] = $object->project->description;
432
			$resarray[$array_key.'_project_date_start'] = dol_print_date($object->project->date_start, 'day');
433
			$resarray[$array_key.'_project_date_end'] = dol_print_date($object->project->date_end, 'day');
434
		}
435
436
		// Add vat by rates
437
		if (is_array($object->lines) && count($object->lines)>0)
438
		{
439
			foreach ($object->lines as $line)
440
			{
441
			    // $line->tva_tx format depends on database field accuraty, no reliable. This is kept for backward comaptibility
442
				if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) $resarray[$array_key.'_total_vat_'.$line->tva_tx]=0;
443
				$resarray[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
444
				$resarray[$array_key.'_total_vat_locale_'.$line->tva_tx]=price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
445
			    // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
446
				$vatformated=vatrate($line->tva_tx);
447
				if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) $resarray[$array_key.'_total_vat_'.$vatformated]=0;
448
				$resarray[$array_key.'_total_vat_'.$vatformated]+=$line->total_tva;
449
				$resarray[$array_key.'_total_vat_locale_'.$vatformated]=price($resarray[$array_key.'_total_vat_'.$vatformated]);
450
			}
451
		}
452
		// Retrieve extrafields
453
		if (is_array($object->array_options) && count($object->array_options))
454
		{
455
			$extrafieldkey=$object->element;
456
457
			require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
458
			$extrafields = new ExtraFields($this->db);
459
			$extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
460
			$object->fetch_optionals($object->id,$extralabels);
461
462
			$resarray = $this->fill_substitutionarray_with_extrafields($object,$resarray,$extrafields,$array_key,$outputlangs);
463
		}
464
		return $resarray;
465
	}
466
467
	/**
468
	 *	Define array with couple substitution key => substitution value
469
	 *
470
	 *	@param  array			$line				Array of lines
471
	 *	@param  Translate		$outputlangs        Lang object to use for output
472
	 *  @return	array								Return a substitution array
473
	 */
474
	function get_substitutionarray_lines($line,$outputlangs)
475
	{
476
		global $conf;
477
478
		$resarray= array(
479
			'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
480
			'line_product_ref'=>$line->product_ref,
481
			'line_product_label'=>$line->product_label,
482
			'line_product_type'=>$line->product_type,
483
			'line_desc'=>$line->desc,
484
			'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
485
			'line_up'=>price2num($line->subprice),
486
			'line_up_locale'=>price($line->subprice, 0, $outputlangs),
487
			'line_qty'=>$line->qty,
488
			'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
489
			'line_price_ht'=>price2num($line->total_ht),
490
			'line_price_ttc'=>price2num($line->total_ttc),
491
			'line_price_vat'=>price2num($line->total_tva),
492
			'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
493
			'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
494
			'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
495
		    // Dates
496
			'line_date_start'=>dol_print_date($line->date_start, 'day', 'tzuser'),
497
			'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzuser', $outputlangs),
498
		    'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc', 'tzuser'),
499
		    'line_date_end'=>dol_print_date($line->date_end, 'day', 'tzuser'),
500
		    'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzuser', $outputlangs),
501
		    'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc', 'tzuser'),
502
503
		    'line_multicurrency_code' => price2num($line->multicurrency_code),
504
		    'line_multicurrency_subprice' => price2num($line->multicurrency_subprice),
505
		    'line_multicurrency_total_ht' => price2num($line->multicurrency_total_ht),
506
		    'line_multicurrency_total_tva' => price2num($line->multicurrency_total_tva),
507
		    'line_multicurrency_total_ttc' => price2num($line->multicurrency_total_ttc),
508
		    'line_multicurrency_subprice_locale' => price($line->multicurrency_subprice, 0, $outputlangs),
509
		    'line_multicurrency_total_ht_locale' => price($line->multicurrency_total_ht, 0, $outputlangs),
510
		    'line_multicurrency_total_tva_locale' => price($line->multicurrency_total_tva, 0, $outputlangs),
511
		    'line_multicurrency_total_ttc_locale' => price($line->multicurrency_total_ttc, 0, $outputlangs),
512
		);
513
514
		    // Units
515
		if ($conf->global->PRODUCT_USE_UNITS)
516
		{
517
		      $resarray['line_unit']=$outputlangs->trans($line->getLabelOfUnit('long'));
0 ignored issues
show
Bug introduced by
The method getLabelOfUnit cannot be called on $line (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
518
		      $resarray['line_unit_short']=$outputlangs->trans($line->getLabelOfUnit('short'));
0 ignored issues
show
Bug introduced by
The method getLabelOfUnit cannot be called on $line (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
519
		}
520
521
		// Retrieve extrafields
522
		$extrafieldkey=$line->element;
523
		$array_key="line";
524
		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
525
		$extrafields = new ExtraFields($this->db);
526
		$extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
527
		$line->fetch_optionals($line->rowid,$extralabels);
0 ignored issues
show
Bug introduced by
The method fetch_optionals cannot be called on $line (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
528
529
		$resarray = $this->fill_substitutionarray_with_extrafields($line,$resarray,$extrafields,$array_key=$array_key,$outputlangs);
530
531
		// Load product data optional fields to the line -> enables to use "line_options_{extrafield}"
532
		if (isset($line->fk_product) && $line->fk_product > 0)
533
		{
534
			$tmpproduct = new Product($this->db);
535
			$result = $tmpproduct->fetch($line->fk_product);
536
			foreach($tmpproduct->array_options as $key=>$label)
537
				$resarray["line_".$key] = $label;
538
		}
539
540
		return $resarray;
541
	}
542
543
    /**
544
     * Define array with couple substitution key => substitution value
545
     *
546
     * @param   Expedition			$object             Main object to use as data source
547
     * @param   Translate		$outputlangs        Lang object to use for output
548
     * @param   array_key		$array_key	        Name of the key for return array
549
     * @return	array								Array of substitution
550
     */
551
    function get_substitutionarray_shipment($object,$outputlangs,$array_key='object')
552
    {
553
    	global $conf;
554
		dol_include_once('/core/lib/product.lib.php');
555
		$object->list_delivery_methods($object->shipping_method_id);
556
		$calculatedVolume=($object->trueWidth * $object->trueHeight * $object->trueDepth);
557
558
    	$array_shipment=array(
559
	    	$array_key.'_id'=>$object->id,
560
	    	$array_key.'_ref'=>$object->ref,
561
	    	$array_key.'_ref_ext'=>$object->ref_ext,
562
	    	$array_key.'_ref_customer'=>$object->ref_customer,
563
	    	$array_key.'_date_delivery'=>dol_print_date($object->date_delivery,'day'),
564
	    	$array_key.'_hour_delivery'=>dol_print_date($object->date_delivery,'hour'),
565
	    	$array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
566
	    	$array_key.'_total_ht'=>price($object->total_ht),
567
	    	$array_key.'_total_vat'=>price($object->total_tva),
568
	    	$array_key.'_total_ttc'=>price($object->total_ttc),
569
	    	$array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
570
	    	$array_key.'_note_private'=>$object->note_private,
571
	    	$array_key.'_note'=>$object->note_public,
572
	    	$array_key.'_tracking_number'=>$object->tracking_number,
573
	    	$array_key.'_tracking_url'=>$object->tracking_url,
574
	    	$array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
575
	    	$array_key.'_weight'=>$object->trueWeight.' '.measuring_units_string($object->weight_units, 'weight'),
576
	    	$array_key.'_width'=>$object->trueWidth.' '.measuring_units_string($object->width_units, 'size'),
577
	    	$array_key.'_height'=>$object->trueHeight.' '.measuring_units_string($object->height_units, 'size'),
578
	    	$array_key.'_depth'=>$object->trueDepth.' '.measuring_units_string($object->depth_units, 'size'),
579
	    	$array_key.'_size'=>$calculatedVolume.' '.measuring_units_string(0, 'volume'),
580
    	);
581
582
    	// Add vat by rates
583
    	foreach ($object->lines as $line)
584
    	{
585
    		if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]=0;
586
    		$array_shipment[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
587
    	}
588
589
    	// Retrieve extrafields
590
    	/*if(is_array($object->array_options) && count($object->array_options))
591
    	{
592
    		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
593
    		$extrafields = new ExtraFields($this->db);
594
    		$extralabels = $extrafields->fetch_name_optionals_label('shipment',true);
595
    		$object->fetch_optionals($object->id,$extralabels);
596
597
    		$array_shipment = $this->fill_substitutionarray_with_extrafields($object,$array_shipment,$extrafields,$array_key,$outputlangs);
598
    	}*/
599
    	return $array_shipment;
600
    }
601
602
603
    /**
604
     *	Define array with couple substitution key => substitution value
605
     *
606
     *	@param  array			$line				Array of lines
607
     *	@param  Translate		$outputlangs        Lang object to use for output
608
     *	@return	array								Substitution array
609
     */
610
    function get_substitutionarray_shipment_lines($line,$outputlangs)
611
    {
612
    	global $conf;
613
		dol_include_once('/core/lib/product.lib.php');
614
615
    	return array(
616
	    	'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
617
	    	'line_product_ref'=>$line->product_ref,
618
	    	'line_product_label'=>$line->product_label,
619
	    	'line_desc'=>$line->desc,
620
	    	'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
621
	    	'line_up'=>price($line->subprice),
622
	    	'line_qty'=>$line->qty,
623
	    	'line_qty_shipped'=>$line->qty_shipped,
624
	    	'line_qty_asked'=>$line->qty_asked,
625
	    	'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
626
	    	'line_price_ht'=>price($line->total_ht),
627
	    	'line_price_ttc'=>price($line->total_ttc),
628
	    	'line_price_vat'=>price($line->total_tva),
629
	    	'line_weight'=>empty($line->weight) ? '' : $line->weight*$line->qty_shipped.' '.measuring_units_string($line->weight_units, 'weight'),
630
	    	'line_length'=>empty($line->length) ? '' : $line->length*$line->qty_shipped.' '.measuring_units_string($line->length_units, 'size'),
631
	    	'line_surface'=>empty($line->surface) ? '' : $line->surface*$line->qty_shipped.' '.measuring_units_string($line->surface_units, 'surface'),
632
	    	'line_volume'=>empty($line->volume) ? '' : $line->volume*$line->qty_shipped.' '.measuring_units_string($line->volume_units, 'volume'),
633
    	);
634
    }
635
636
637
    /**
638
     * Define array with couple subtitution key => subtitution value
639
     *
640
     * @param   Object		$object    		Dolibarr Object
641
     * @param   Translate	$outputlangs    Language object for output
642
     * @param   boolean		$recursive    	Want to fetch child array or child object
643
     * @return	array						Array of substitution key->code
644
     */
645
    function get_substitutionarray_each_var_object(&$object,$outputlangs,$recursive=true) {
646
        $array_other = array();
647
        if(!empty($object)) {
648
            foreach($object as $key => $value) {
649
                if(!empty($value)) {
650
                    if(!is_array($value) && !is_object($value)) {
651
                        $array_other['object_'.$key] = $value;
652
                    }
653
                    if(is_array($value) && $recursive){
654
                        $array_other['object_'.$key] = $this->get_substitutionarray_each_var_object($value,$outputlangs,false);
655
                    }
656
                }
657
            }
658
        }
659
        return $array_other;
660
    }
661
662
663
    /**
664
     *	Fill array with couple extrafield key => extrafield value
665
     *
666
     *	@param  Object			$object				Object with extrafields (must have $object->array_options filled)
667
     *	@param  array			$array_to_fill      Substitution array
668
     *  @param  Extrafields		$extrafields        Extrafields object
669
     *  @param  string			$array_key	        Prefix for name of the keys into returned array
670
     *  @param  Translate		$outputlangs        Lang object to use for output
671
     *	@return	array								Substitution array
672
     */
673
	function fill_substitutionarray_with_extrafields($object,$array_to_fill,$extrafields,$array_key,$outputlangs)
674
	{
675
		global $conf;
676
		foreach($extrafields->attribute_label as $key=>$label)
677
		{
678
			if($extrafields->attribute_type[$key] == 'price')
679
			{
680
				$object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
681
				$object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
682
				//Add value to store price with currency
683
				$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
684
			}
685
			else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
686
			{
687
				$object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
688
			}
689
			else if($extrafields->attribute_type[$key] == 'date')
690
			{
691
				if (strlen($object->array_options['options_'.$key])>0)
692
				{
693
					$date = $object->array_options['options_'.$key];
694
					$object->array_options['options_'.$key] = dol_print_date($date,'day');                                       // using company output language
695
					$object->array_options['options_'.$key.'_locale'] = dol_print_date($date,'day','tzserver',$outputlangs);     // using output language format
696
					$object->array_options['options_'.$key.'_rfc'] = dol_print_date($date,'dayrfc');                             // international format
697
				}
698
				else
699
				{
700
					$object->array_options['options_'.$key] = '';
701
					$object->array_options['options_'.$key.'_locale'] = '';
702
					$object->array_options['options_'.$key.'_rfc'] = '';
703
				}
704
				$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
705
				$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
706
			}
707
			else if($extrafields->attribute_type[$key] == 'datetime')
708
			{
709
				$datetime = $object->array_options['options_'.$key];
710
				$object->array_options['options_'.$key] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhour'):'');                            // using company output language
711
				$object->array_options['options_'.$key.'_locale'] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhour','tzserver',$outputlangs):'');    // using output language format
712
				$object->array_options['options_'.$key.'_rfc'] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhourrfc'):'');                             // international format
713
				$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
714
				$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
715
			}
716
			$array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
717
		}
718
719
		return $array_to_fill;
720
721
	}
722
723
724
	/**
725
	 * Rect pdf
726
	 *
727
	 * @param	PDF		$pdf			Object PDF
728
	 * @param	float	$x				Abscissa of first point
729
	 * @param	float	$y		        Ordinate of first point
730
	 * @param	float	$l				??
731
	 * @param	float	$h				??
732
	 * @param	int		$hidetop		1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
733
	 * @param	int		$hidebottom		Hide bottom
734
	 * @return	void
735
	 */
736
    function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
737
    {
738
	    if (empty($hidetop) || $hidetop==-1) $pdf->line($x, $y, $x+$l, $y);
739
	    $pdf->line($x+$l, $y, $x+$l, $y+$h);
740
	    if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
741
	    $pdf->line($x, $y+$h, $x, $y);
742
    }
743
}
744
745