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

Loan::info()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 44
rs 8.8497
1
<?php
2
/* Copyright (C) 2014-2018  Alexandre Spangaro   <[email protected]>
3
 * Copyright (C) 2015-2018  Frédéric France      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 *  \file       htdocs/loan/class/loan.class.php
21
 *  \ingroup    loan
22
 *  \brief      Class for loan module
23
 */
24
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25
26
27
/**
28
 *  Loan
29
 */
30
class Loan extends CommonObject
31
{
32
	/**
33
	 * @var string ID to identify managed object
34
	 */
35
	public $element='loan';
36
37
	public $table='loan';
38
39
	/**
40
	 * @var string Name of table without prefix where object is stored
41
	 */
42
	public $table_element='loan';
43
44
	public $picto = 'bill';
45
46
	/**
47
	 * @var int ID
48
	 */
49
	public $rowid;
50
51
	public $datestart;
52
	public $dateend;
53
54
    /**
55
     * @var string Loan label
56
     */
57
    public $label;
58
59
	public $capital;
60
	public $nbterm;
61
	public $rate;
62
	public $paid;
63
	public $account_capital;
64
	public $account_insurance;
65
	public $account_interest;
66
	public $date_creation;
67
	public $date_modification;
68
	public $date_validation;
69
70
	public $insurance_amount;
71
72
	/**
73
     * @var int Bank ID
74
     */
75
	public $fk_bank;
76
77
	/**
78
     * @var int ID
79
     */
80
	public $fk_user_creat;
81
82
	/**
83
     * @var int ID
84
     */
85
	public $fk_user_modif;
86
87
	/**
88
     * @var int ID
89
     */
90
	public $fk_project;
91
92
93
	/**
94
	 * Constructor
95
	 *
96
	 * @param	DoliDB		$db		Database handler
97
	 */
98
	public function __construct($db)
99
	{
100
		$this->db = $db;
101
	}
102
103
	/**
104
	 *  Load object in memory from database
105
	 *
106
	 *  @param	int		$id		 id object
107
	 *  @return int				 <0 error , >=0 no error
108
	 */
109
	public function fetch($id)
110
	{
111
		$sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,";
112
		$sql.= " l.paid, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
113
		$sql.= " FROM ".MAIN_DB_PREFIX."loan as l";
114
		$sql.= " WHERE l.rowid = ".$id;
115
116
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
117
		$resql=$this->db->query($sql);
118
		if ($resql)
119
		{
120
			if ($this->db->num_rows($resql))
121
			{
122
				$obj = $this->db->fetch_object($resql);
123
124
				$this->id					= $obj->rowid;
125
				$this->ref					= $obj->rowid;
126
				$this->datestart			= $this->db->jdate($obj->datestart);
127
				$this->dateend				= $this->db->jdate($obj->dateend);
128
				$this->label				= $obj->label;
129
				$this->capital				= $obj->capital;
130
				$this->nbterm				= $obj->nbterm;
131
				$this->rate					= $obj->rate;
132
				$this->note_private			= $obj->note_private;
133
				$this->note_public			= $obj->note_public;
134
				$this->insurance_amount     = $obj->insurance_amount;
135
				$this->paid					= $obj->paid;
136
137
				$this->account_capital		= $obj->accountancy_account_capital;
138
				$this->account_insurance	= $obj->accountancy_account_insurance;
139
				$this->account_interest		= $obj->accountancy_account_interest;
140
				$this->fk_project			= $obj->fk_project;
141
142
				$this->db->free($resql);
143
				return 1;
144
			}
145
			else
146
			{
147
				$this->db->free($resql);
148
				return 0;
149
			}
150
		}
151
		else
152
		{
153
			$this->error=$this->db->lasterror();
154
			return -1;
155
		}
156
	}
157
158
159
	/**
160
	 *  Create a loan into database
161
	 *
162
	 *  @param	User	$user	User making creation
163
	 *  @return int				<0 if KO, id if OK
164
	 */
165
	public function create($user)
166
	{
167
		global $conf, $langs;
168
169
		$error=0;
170
171
		$now=dol_now();
172
173
		// clean parameters
174
		$newcapital=price2num($this->capital, 'MT');
175
		if (empty($this->insurance_amount)) $this->insurance_amount = 0;
176
		$newinsuranceamount=price2num($this->insurance_amount, 'MT');
177
		if (isset($this->note_private)) $this->note_private = trim($this->note_private);
178
		if (isset($this->note_public)) $this->note_public = trim($this->note_public);
179
		if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
180
		if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
181
		if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
182
		if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
183
		if (isset($this->fk_user_creat)) $this->fk_user_creat = (int) $this->fk_user_creat;
184
		if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
185
		if (isset($this->fk_project)) $this->fk_project = (int) $this->fk_project;
186
187
		// Check parameters
188
		if (! $newcapital > 0 || empty($this->datestart) || empty($this->dateend))
189
		{
190
			$this->error="ErrorBadParameter";
191
			return -2;
192
		}
193
		if (($conf->accounting->enabled) && empty($this->account_capital))
194
		{
195
			$this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
196
			return -2;
197
		}
198
		if (($conf->accounting->enabled) && empty($this->account_insurance))
199
		{
200
			$this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
201
			return -2;
202
		}
203
		if (($conf->accounting->enabled) && empty($this->account_interest))
204
		{
205
			$this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
206
			return -2;
207
		}
208
209
		$this->db->begin();
210
211
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
212
		$sql.= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
213
		$sql.= " datec, fk_projet, fk_user_author, insurance_amount)";
214
		$sql.= " VALUES ('".$this->db->escape($this->label)."',";
215
		$sql.= " '".$this->db->escape($this->fk_bank)."',";
216
		$sql.= " '".price2num($newcapital)."',";
217
		$sql.= " '".$this->db->idate($this->datestart)."',";
218
		$sql.= " '".$this->db->idate($this->dateend)."',";
219
		$sql.= " '".$this->db->escape($this->nbterm)."',";
220
		$sql.= " '".$this->db->escape($this->rate)."',";
221
		$sql.= " '".$this->db->escape($this->note_private)."',";
222
		$sql.= " '".$this->db->escape($this->note_public)."',";
223
		$sql.= " '".$this->db->escape($this->account_capital)."',";
224
		$sql.= " '".$this->db->escape($this->account_insurance)."',";
225
		$sql.= " '".$this->db->escape($this->account_interest)."',";
226
		$sql.= " ".$conf->entity.",";
227
		$sql.= " '".$this->db->idate($now)."',";
228
		$sql.= " ".(empty($this->fk_project)?'NULL':$this->fk_project).",";
229
		$sql.= " ".$user->id.",";
230
		$sql.= " '".price2num($newinsuranceamount)."'";
231
		$sql.= ")";
232
233
		dol_syslog(get_class($this)."::create", LOG_DEBUG);
234
		$resql=$this->db->query($sql);
235
		if ($resql)
236
		{
237
			$this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."loan");
238
239
			//dol_syslog("Loans::create this->id=".$this->id);
240
			$this->db->commit();
241
			return $this->id;
242
		}
243
		else
244
		{
245
			$this->error=$this->db->error();
246
			$this->db->rollback();
247
			return -1;
248
		}
249
	}
250
251
252
	/**
253
	 *  Delete a loan
254
	 *
255
	 *  @param	User	$user	Object user making delete
256
	 *  @return int 			<0 if KO, >0 if OK
257
	 */
258
	public function delete($user)
259
	{
260
		$error=0;
261
262
		$this->db->begin();
263
264
		// Get bank transaction lines for this loan
265
		include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
266
		$account=new Account($this->db);
267
		$lines_url=$account->get_url('', $this->id, 'loan');
268
269
		// Delete bank urls
270
		foreach ($lines_url as $line_url)
271
		{
272
			if (! $error)
273
			{
274
				$accountline=new AccountLine($this->db);
275
				$accountline->fetch($line_url['fk_bank']);
276
				$result=$accountline->delete_urls($user);
277
				if ($result < 0)
278
				{
279
					$error++;
280
				}
281
			}
282
		}
283
284
		// Delete payments
285
		if (! $error)
286
		{
287
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".$this->id;
288
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
289
			$resql=$this->db->query($sql);
290
			if (! $resql)
291
			{
292
				$error++;
293
				$this->error=$this->db->lasterror();
294
			}
295
		}
296
297
		if (! $error)
298
		{
299
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".$this->id;
300
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
301
			$resql=$this->db->query($sql);
302
			if (! $resql)
303
			{
304
				$error++;
305
				$this->error=$this->db->lasterror();
306
			}
307
		}
308
309
		if (! $error)
310
		{
311
			$this->db->commit();
312
			return 1;
313
		}
314
		else
315
		{
316
			$this->db->rollback();
317
			return -1;
318
		}
319
	}
320
321
322
	/**
323
	 *  Update loan
324
	 *
325
	 *  @param	User	$user	User who modified
326
	 *  @return int				<0 if error, >0 if ok
327
	 */
328
	public function update($user)
329
	{
330
		$this->db->begin();
331
332
		if (! is_numeric($this->nbterm))
333
		{
334
			$this->error='BadValueForParameterForNbTerm';
335
			return -1;
336
		}
337
338
		$sql = "UPDATE ".MAIN_DB_PREFIX."loan";
339
		$sql.= " SET label='".$this->db->escape($this->label)."',";
340
		$sql.= " capital='".price2num($this->db->escape($this->capital))."',";
341
		$sql.= " datestart='".$this->db->idate($this->datestart)."',";
342
		$sql.= " dateend='".$this->db->idate($this->dateend)."',";
343
		$sql.= " nbterm=".$this->nbterm.",";
344
		$sql.= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
345
		$sql.= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
346
		$sql.= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
347
		$sql.= " fk_projet=".(empty($this->fk_project)?'NULL':$this->fk_project).",";
348
		$sql.= " fk_user_modif = ".$user->id.",";
349
		$sql.= " insurance_amount = '".price2num($this->db->escape($this->insurance_amount))."'";
350
		$sql.= " WHERE rowid=".$this->id;
351
352
		dol_syslog(get_class($this)."::update", LOG_DEBUG);
353
		$resql=$this->db->query($sql);
354
		if ($resql)
355
		{
356
			$this->db->commit();
357
			return 1;
358
		}
359
		else
360
		{
361
			$this->error=$this->db->error();
362
			$this->db->rollback();
363
			return -1;
364
		}
365
	}
366
367
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
368
	/**
369
	 *  Tag loan as payed completely
370
	 *
371
	 *  @param	User	$user	Object user making change
372
	 *  @return	int				<0 if KO, >0 if OK
373
	 */
374
	public function set_paid($user)
375
	{
376
        // phpcs:enable
377
		$sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
378
		$sql.= " paid = 1";
379
		$sql.= " WHERE rowid = ".$this->id;
380
		$return = $this->db->query($sql);
381
		if ($return) {
382
			return 1;
383
		} else {
384
			$this->error=$this->db->lasterror();
385
			return -1;
386
		}
387
	}
388
389
	/**
390
	 *  Return label of loan status (unpaid, paid)
391
	 *
392
	 *  @param  int		$mode			0=label, 1=short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label
393
	 *  @param  integer	$alreadypaid	0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
394
	 *  @return string					Label
395
	 */
396
	public function getLibStatut($mode = 0, $alreadypaid = -1)
397
	{
398
		return $this->LibStatut($this->paid, $mode, $alreadypaid);
399
	}
400
401
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
402
	/**
403
	 *  Return label for given status
404
	 *
405
	 *  @param  int		$statut			Id statut
406
	 *  @param  int		$mode			0=Label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label, 5=Short label + Picto
407
	 *  @param  integer	$alreadypaid	0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
408
	 *  @return string					Label
409
	 */
410
	public function LibStatut($statut, $mode = 0, $alreadypaid = -1)
411
	{
412
        // phpcs:enable
413
		global $langs;
414
		$langs->loadLangs(array("customers","bills"));
415
416
		if ($mode == 0 || $mode == 1)
417
		{
418
			if ($statut ==  0) return $langs->trans("Unpaid");
419
			elseif ($statut ==  1) return $langs->trans("Paid");
420
		}
421
		elseif ($mode == 2)
422
		{
423
			if ($statut ==  0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
424
			elseif ($statut ==  0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
425
			elseif ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
426
		}
427
		elseif ($mode == 3)
428
		{
429
			if ($statut ==  0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
430
			elseif ($statut ==  0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
431
			elseif ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6');
432
		}
433
		elseif ($mode == 4)
434
		{
435
			if ($statut ==  0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
436
			elseif ($statut ==  0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
437
			elseif ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
438
		}
439
		elseif ($mode == 5)
440
		{
441
			if ($statut ==  0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
442
			elseif ($statut ==  0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
443
			elseif ($statut ==  1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
444
		}
445
		elseif ($mode == 6)
446
		{
447
			if ($statut ==  0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
448
			elseif ($statut ==  0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
449
			elseif ($statut ==  1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
450
		}
451
452
		else return "Error, mode/status not found";
453
	}
454
455
456
	/**
457
	 *  Return clicable name (with eventually the picto)
458
	 *
459
	 *  @param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
460
	 *  @param	int		$maxlen			Label max length
461
	 *  @return	string					Chaine with URL
462
	 */
463
	public function getNomUrl($withpicto = 0, $maxlen = 0)
464
	{
465
		global $langs;
466
467
		$result='';
468
469
		$tooltip = '<u>' . $langs->trans("ShowLoan") . '</u>';
470
		if (! empty($this->ref))
471
			$tooltip .= '<br><strong>' . $langs->trans('Ref') . ':</strong> ' . $this->ref;
472
		if (! empty($this->label))
473
			$tooltip .= '<br><strong>' . $langs->trans('Label') . ':</strong> ' . $this->label;
474
475
		$linkstart = '<a href="'.DOL_URL_ROOT.'/loan/card.php?id='.$this->id.'" title="'.str_replace('\n', '', dol_escape_htmltag($tooltip, 1)).'" class="classfortooltip">';
476
		$linkend = '</a>';
477
478
		$result .= $linkstart;
479
		if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $label seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $notooltip does not exist. Did you maybe mean $tooltip?
Loading history...
480
		if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref, $maxlen):$this->ref);
481
		$result .= $linkend;
482
483
		return $result;
484
	}
485
486
	/**
487
	 *  Initialise an instance with random values.
488
	 *  Used to build previews or test instances.
489
	 * 	id must be 0 if object instance is a specimen.
490
	 *
491
	 *  @return	void
492
	 */
493
	public function initAsSpecimen()
494
	{
495
	    global $user, $langs, $conf;
496
497
	    $now=dol_now();
498
499
	    // Initialise parameters
500
	    $this->id = 0;
501
	    $this->fk_bank = 1;
502
	    $this->label = 'SPECIMEN';
503
	    $this->specimen = 1;
504
	    $this->socid = 1;
505
	    $this->account_capital = 16;
506
	    $this->account_insurance = 616;
507
	    $this->account_interest = 518;
508
	    $this->datestart = $now;
509
	    $this->dateend = $now + (3600 * 24 * 365);
510
	    $this->note_public = 'SPECIMEN';
511
	    $this->capital = 20000;
512
	    $this->nbterm = 48;
513
	    $this->rate = 4.3;
514
	}
515
516
	/**
517
	 *  Return amount of payments already done
518
	 *
519
	 *  @return		int		Amount of payment already done, <0 if KO
520
	 */
521
	public function getSumPayment()
522
	{
523
		$table='payment_loan';
524
		$field='fk_loan';
525
526
		$sql = 'SELECT sum(amount) as amount';
527
		$sql.= ' FROM '.MAIN_DB_PREFIX.$table;
528
		$sql.= ' WHERE '.$field.' = '.$this->id;
529
530
		dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
531
		$resql=$this->db->query($sql);
532
		if ($resql)
533
		{
534
			$amount=0;
535
536
			$obj = $this->db->fetch_object($resql);
537
			if ($obj) $amount=$obj->amount?$obj->amount:0;
538
539
			$this->db->free($resql);
540
			return $amount;
541
		}
542
		else
543
		{
544
			$this->error=$this->db->lasterror();
545
			return -1;
546
		}
547
	}
548
549
	/**
550
	 *  Information on record
551
	 *
552
	 *  @param	int			$id		Id of record
553
	 *  @return	integer|null
554
	 */
555
	public function info($id)
556
	{
557
		$sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
558
		$sql.= ' l.tms';
559
		$sql.= ' WHERE l.rowid = '.$id;
560
561
		dol_syslog(get_class($this).'::info', LOG_DEBUG);
562
		$result = $this->db->query($sql);
563
564
		if ($result)
565
		{
566
			if ($this->db->num_rows($result))
567
			{
568
				$obj = $this->db->fetch_object($result);
569
				$this->id = $obj->rowid;
570
				if ($obj->fk_user_author)
571
				{
572
					$cuser = new User($this->db);
573
					$cuser->fetch($obj->fk_user_author);
574
					$this->user_creation = $cuser;
575
				}
576
				if ($obj->fk_user_modif)
577
				{
578
					$muser = new User($this->db);
579
					$muser->fetch($obj->fk_user_modif);
580
					$this->user_modification = $muser;
581
				}
582
				$this->date_creation	 = $this->db->jdate($obj->datec);
583
				if (empty($obj->fk_user_modif)) $obj->tms = "";
584
				$this->date_modification = $this->db->jdate($obj->tms);
585
586
				$this->db->free($result);
587
				return 1;
588
			}
589
			else
590
			{
591
				$this->db->free($result);
592
				return 0;
593
			}
594
		}
595
		else
596
		{
597
			$this->error=$this->db->lasterror();
598
			return -1;
599
		}
600
    }
601
}
602