-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
paiement.class.php
1209 lines (1075 loc) · 39.3 KB
/
paiement.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
* Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
* Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/compta/paiement/class/paiement.class.php
* \ingroup facture
* \brief File of class to manage payments of customers invoices
*/
require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
require_once DOL_DOCUMENT_ROOT .'/multicurrency/class/multicurrency.class.php';
/**
* Class to manage payments of customer invoices
*/
class Paiement extends CommonObject
{
/**
* @var string ID to identify managed object
*/
public $element='payment';
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element='paiement';
/**
* @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
*/
public $picto = 'payment';
public $facid;
public $datepaye;
/**
* @deprecated
* @see amount, amounts
*/
public $total;
/**
* @deprecated
* @see amount, amounts
*/
public $montant;
public $amount; // Total amount of payment
public $amounts=array(); // Array of amounts
public $multicurrency_amounts=array(); // Array of amounts
public $author;
public $paiementid; // Type de paiement. Stocke dans fk_paiement
// de llx_paiement qui est lie aux types de
//paiement de llx_c_paiement
public $num_paiement; // Numero du CHQ, VIR, etc...
public $num_payment; // Numero du CHQ, VIR, etc...
public $bank_account; // Id compte bancaire du paiement
public $bank_line; // Id de la ligne d'ecriture bancaire
// fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
// fk_paiement dans llx_paiement_facture est le rowid du paiement
public $fk_paiement; // Type of paiment
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
}
/**
* Load payment from database
*
* @param int $id Id of payment to get
* @param string $ref Ref of payment to get (currently ref = id but this may change in future)
* @param int $fk_bank Id of bank line associated to payment
* @return int <0 if KO, 0 if not found, >0 if OK
*/
function fetch($id, $ref='', $fk_bank='')
{
$sql = 'SELECT p.rowid, p.ref, p.datep as dp, p.amount, p.statut, p.fk_bank,';
$sql.= ' c.code as type_code, c.libelle as type_libelle,';
$sql.= ' p.num_paiement as num_payment, p.note,';
$sql.= ' b.fk_account';
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
$sql.= ' WHERE p.entity IN (' . getEntity('facture').')';
if ($id > 0)
$sql.= ' AND p.rowid = '.$id;
else if ($ref)
$sql.= " AND p.ref = '".$ref."'";
else if ($fk_bank)
$sql.= ' AND p.fk_bank = '.$fk_bank;
$resql = $this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->ref = $obj->ref?$obj->ref:$obj->rowid;
$this->date = $this->db->jdate($obj->dp);
$this->datepaye = $this->db->jdate($obj->dp);
$this->numero = $obj->num_payment; // deprecated
$this->num_paiement = $obj->num_payment; // deprecated
$this->num_payment = $obj->num_payment;
$this->montant = $obj->amount; // deprecated
$this->amount = $obj->amount;
$this->note = $obj->note;
$this->type_libelle = $obj->type_libelle;
$this->type_code = $obj->type_code;
$this->statut = $obj->statut;
$this->bank_account = $obj->fk_account; // deprecated
$this->fk_account = $obj->fk_account;
$this->bank_line = $obj->fk_bank;
$this->db->free($resql);
return 1;
}
else
{
$this->db->free($resql);
return 0;
}
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Create payment of invoices into database.
* Use this->amounts to have list of invoices for the payment.
* For payment of a customer invoice, amounts are positive, for payment of credit note, amounts are negative
*
* @param User $user Object user
* @param int $closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more
* @return int id of created payment, < 0 if error
*/
function create($user, $closepaidinvoices=0)
{
global $conf, $langs;
$error = 0;
$way = $this->getWay();
$now=dol_now();
// Clean parameters
$totalamount = 0;
$totalamount_converted = 0;
$atleastonepaymentnotnull = 0;
if ($way == 'dolibarr')
{
$amounts = &$this->amounts;
$amounts_to_update = &$this->multicurrency_amounts;
}
else
{
$amounts = &$this->multicurrency_amounts;
$amounts_to_update = &$this->amounts;
}
foreach ($amounts as $key => $value) // How payment is dispatch
{
$value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value, $way);
$totalamount_converted += $value_converted;
$amounts_to_update[$key] = price2num($value_converted, 'MT');
$newvalue = price2num($value,'MT');
$amounts[$key] = $newvalue;
$totalamount += $newvalue;
if (! empty($newvalue)) $atleastonepaymentnotnull++;
}
$totalamount = price2num($totalamount);
$totalamount_converted = price2num($totalamount_converted);
// Check parameters
if (empty($totalamount) && empty($atleastonepaymentnotnull)) // We accept negative amounts for withdraw reject but not empty arrays
{
$this->errors[]='TotalAmountEmpty';
$this->error='TotalAmountEmpty';
return -1;
}
$this->db->begin();
$this->ref = $this->getNextNumRef('');
if ($way == 'dolibarr')
{
$total = $totalamount;
$mtotal = $totalamount_converted; // Maybe use price2num with MT for the converted value
}
else
{
$total = $totalamount_converted; // Maybe use price2num with MT for the converted value
$mtotal = $totalamount;
}
$note = ($this->note_public?$this->note_public:$this->note);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (entity, ref, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_creat)";
$sql.= " VALUES (".$conf->entity.", '".$this->ref."', '". $this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$total."', '".$mtotal."', ".$this->paiementid.", '".$this->num_paiement."', '".$this->db->escape($note)."', ".$user->id.")";
dol_syslog(get_class($this)."::Create insert paiement", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
// Insert links amount / invoices
foreach ($this->amounts as $key => $amount)
{
$facid = $key;
if (is_numeric($amount) && $amount <> 0)
{
$amount = price2num($amount);
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount, multicurrency_amount)';
$sql .= ' VALUES ('.$facid.', '. $this->id.', \''.$amount.'\', \''.$this->multicurrency_amounts[$key].'\')';
dol_syslog(get_class($this).'::Create Amount line '.$key.' insert paiement_facture', LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$invoice=new Facture($this->db);
$invoice->fetch($facid);
// If we want to closed payed invoices
if ($closepaidinvoices)
{
$paiement = $invoice->getSommePaiement();
$creditnotes=$invoice->getSumCreditNotesUsed();
$deposits=$invoice->getSumDepositsUsed();
$alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
$remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
//var_dump($invoice->total_ttc.' - '.$paiement.' -'.$creditnotes.' - '.$deposits.' - '.$remaintopay);exit;
/* Why this ? We can remove i think.
// If there is withdrawals request to do and not done yet on the invoice the payment is on, we wait before closing.
$mustwait=0;
$sqlrequest ="SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX."prelevement_facture_demande";
$sqlrequest.="WHERE fk_facture = ".$invoice->id." AND traite = 0";
...
$listofpayments=$invoice->getListOfPayments();
foreach($listofpayments as $paym)
{
// This payment on invoice $invoice might be the one we record or another one
if ($paym['type']=='PRE')
{
if (! empty($conf->prelevement->enabled))
{
// if not, $mustwait++; // This will disable automatic close on invoice to allow to process
}
}
}
*/
//Invoice types that are eligible for changing status to paid
$affected_types = array(
Facture::TYPE_STANDARD,
Facture::TYPE_REPLACEMENT,
Facture::TYPE_CREDIT_NOTE,
Facture::TYPE_DEPOSIT,
Facture::TYPE_SITUATION
);
if (!in_array($invoice->type, $affected_types)) dol_syslog("Invoice ".$facid." is not a standard, nor replacement invoice, nor credit note, nor deposit invoice, nor situation invoice. We do nothing more.");
else if ($remaintopay) dol_syslog("Remain to pay for invoice ".$facid." not null. We do nothing more.");
//else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more.");
else
{
// If invoice is a down payment, we also convert down payment to discount
if ($invoice->type == Facture::TYPE_DEPOSIT)
{
$amount_ht = $amount_tva = $amount_ttc = array();
// Insert one discount by VAT rate category
$discount = new DiscountAbsolute($this->db);
$discount->fetch('',$invoice->id);
if (empty($discount->id)) { // If the invoice was not yet converted into a discount (this may have been done manually before we come here)
$discount->description = '(DEPOSIT)';
$discount->fk_soc = $invoice->socid;
$discount->fk_facture_source = $invoice->id;
// Loop on each vat rate
$i = 0;
foreach ($invoice->lines as $line) {
if ($line->total_ht != 0) { // no need to create discount if amount is null
$amount_ht[$line->tva_tx] += $line->total_ht;
$amount_tva[$line->tva_tx] += $line->total_tva;
$amount_ttc[$line->tva_tx] += $line->total_ttc;
$i++;
}
}
foreach ($amount_ht as $tva_tx => $xxx) {
$discount->amount_ht = abs($amount_ht[$tva_tx]);
$discount->amount_tva = abs($amount_tva[$tva_tx]);
$discount->amount_ttc = abs($amount_ttc[$tva_tx]);
$discount->tva_tx = abs($tva_tx);
$result = $discount->create($user);
if ($result < 0) {
$error++;
break;
}
}
}
if ($error)
{
setEventMessages($discount->error, $discount->errors, 'errors');
$error++;
}
}
// Set invoice to paid
if (! $error)
{
$result=$invoice->set_paid($user,'','');
if ($result<0)
{
$this->error=$invoice->error;
$error++;
}
}
}
}
// Regenerate documents of invoices
if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
{
$outputlangs = $langs;
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $invoice->thirdparty->default_lang;
if (! empty($newlang)) {
$outputlangs = new Translate("", $conf);
$outputlangs->setDefaultLang($newlang);
}
$ret = $invoice->fetch($facid); // Reload to get new records
$result = $invoice->generateDocument($invoice->modelpdf, $outputlangs);
if ($result < 0) {
setEventMessages($invoice->error, $invoice->errors, 'errors');
$error++;
}
}
}
else
{
$this->error=$this->db->lasterror();
$error++;
}
}
else
{
dol_syslog(get_class($this).'::Create Amount line '.$key.' not a number. We discard it.');
}
}
if (! $error)
{
// Appel des triggers
$result=$this->call_trigger('PAYMENT_CUSTOMER_CREATE', $user);
if ($result < 0) { $error++; }
// Fin appel triggers
}
}
else
{
$this->error=$this->db->lasterror();
$error++;
}
if (! $error)
{
$this->amount=$total;
$this->total=$total; // deprecated
$this->multicurrency_amount=$mtotal;
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Delete a payment and generated links into account
* - Si le paiement porte sur un ecriture compte qui est rapprochee, on refuse
* - Si le paiement porte sur au moins une facture a "payee", on refuse
*
* @param int $notrigger No trigger
* @return int <0 si ko, >0 si ok
*/
function delete($notrigger=0)
{
global $conf, $user, $langs;
$error=0;
$bank_line_id = $this->bank_line;
$this->db->begin();
// Verifier si paiement porte pas sur une facture classee
// Si c'est le cas, on refuse la suppression
$billsarray=$this->getBillsArray('fk_statut > 1');
if (is_array($billsarray))
{
if (count($billsarray))
{
$this->error="ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";
$this->db->rollback();
return -1;
}
}
else
{
$this->db->rollback();
return -2;
}
// Delete bank urls. If payment is on a conciliated line, return error.
if ($bank_line_id > 0)
{
$accline = new AccountLine($this->db);
$result=$accline->fetch($bank_line_id);
if ($result == 0) $accline->rowid=$bank_line_id; // If not found, we set artificially rowid to allow delete of llx_bank_url
// Delete bank account url lines linked to payment
$result=$accline->delete_urls($user);
if ($result < 0)
{
$this->error=$accline->error;
$this->db->rollback();
return -3;
}
// Delete bank account lines linked to payment
$result=$accline->delete($user);
if ($result < 0)
{
$this->error=$accline->error;
$this->db->rollback();
return -4;
}
}
if (! $notrigger)
{
// Call triggers
$result=$this->call_trigger('PAYMENT_CUSTOMER_DELETE', $user);
if ($result < 0)
{
$this->db->rollback();
return -1;
}
// End call triggers
}
// Delete payment (into paiement_facture and paiement)
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
$sql.= ' WHERE fk_paiement = '.$this->id;
dol_syslog($sql);
$result = $this->db->query($sql);
if ($result)
{
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
$sql.= ' WHERE rowid = '.$this->id;
dol_syslog($sql);
$result = $this->db->query($sql);
if (! $result)
{
$this->error=$this->db->lasterror();
$this->db->rollback();
return -3;
}
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error;
$this->db->rollback();
return -5;
}
}
/**
* Add a record into bank for payment + links between this bank record and sources of payment.
* All payment properties (this->amount, this->amounts, ...) must have been set first like after a call to create().
*
* @param User $user Object of user making payment
* @param string $mode 'payment', 'payment_supplier'
* @param string $label Label to use in bank record. Note: If label is '(WithdrawalPayment)', a third entry 'widthdraw' is added into bank_url.
* @param int $accountid Id of bank account to do link with
* @param string $emetteur_nom Name of transmitter
* @param string $emetteur_banque Name of bank
* @param int $notrigger No trigger
* @return int <0 if KO, bank_line_id if OK
*/
function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque,$notrigger=0)
{
global $conf,$langs,$user;
$error=0;
$bank_line_id=0;
if (! empty($conf->banque->enabled))
{
if ($accountid <= 0)
{
$this->error='Bad value for parameter accountid='.$accountid;
dol_syslog(get_class($this).'::addPaymentToBank '.$this->error, LOG_ERR);
return -1;
}
$this->db->begin();
$this->fk_account=$accountid;
include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
dol_syslog("$user->id,$mode,$label,$this->fk_account,$emetteur_nom,$emetteur_banque");
$acc = new Account($this->db);
$result=$acc->fetch($this->fk_account);
$totalamount=$this->amount;
if (empty($totalamount)) $totalamount=$this->total; // For backward compatibility
// if dolibarr currency != bank currency then we received an amount in customer currency (currently I don't manage the case : my currency is USD, the customer currency is EUR and he paid me in GBP. Seems no sense for me)
if (!empty($conf->multicurrency->enabled) && $conf->currency != $acc->currency_code) $totalamount=$this->multicurrency_amount;
if ($mode == 'payment_supplier') $totalamount=-$totalamount;
// Insert payment into llx_bank
$bank_line_id = $acc->addline(
$this->datepaye,
$this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label,
$totalamount, // Sign must be positive when we receive money (customer payment), negative when you give money (supplier invoice or credit note)
$this->num_paiement,
'',
$user,
$emetteur_nom,
$emetteur_banque
);
// Mise a jour fk_bank dans llx_paiement
// On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0)
{
$result=$this->update_fk_bank($bank_line_id);
if ($result <= 0)
{
$error++;
dol_print_error($this->db);
}
// Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction
if ( ! $error)
{
$url='';
if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/card.php?id=';
if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/card.php?id=';
if ($url)
{
$result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
if ($result <= 0)
{
$error++;
dol_print_error($this->db);
}
}
}
// Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
//if (! $error && $label != '(WithdrawalPayment)')
if (! $error)
{
$linkaddedforthirdparty=array();
foreach ($this->amounts as $key => $value) // We should have invoices always for same third party but we loop in case of.
{
if ($mode == 'payment')
{
$fac = new Facture($this->db);
$fac->fetch($key);
$fac->fetch_thirdparty();
if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{
$result=$acc->add_url_line(
$bank_line_id,
$fac->thirdparty->id,
DOL_URL_ROOT.'/comm/card.php?socid=',
$fac->thirdparty->name,
'company'
);
if ($result <= 0) dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
$linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
}
}
if ($mode == 'payment_supplier')
{
$fac = new FactureFournisseur($this->db);
$fac->fetch($key);
$fac->fetch_thirdparty();
if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{
$result=$acc->add_url_line(
$bank_line_id,
$fac->thirdparty->id,
DOL_URL_ROOT.'/fourn/card.php?socid=',
$fac->thirdparty->name,
'company'
);
if ($result <= 0) dol_syslog(get_class($this).'::addPaymentToBank '.$this->db->lasterror());
$linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
}
}
}
}
// Add link 'WithdrawalPayment' in bank_url
if (! $error && $label == '(WithdrawalPayment)')
{
$result=$acc->add_url_line(
$bank_line_id,
$this->id_prelevement,
DOL_URL_ROOT.'/compta/prelevement/card.php?id=',
$this->num_paiement,
'withdraw'
);
}
if (! $error && ! $notrigger)
{
// Appel des triggers
$result=$this->call_trigger('PAYMENT_ADD_TO_BANK', $user);
if ($result < 0) { $error++; }
// Fin appel triggers
}
}
else
{
$this->error=$acc->error;
$error++;
}
if (! $error)
{
$this->db->commit();
}
else
{
$this->db->rollback();
}
}
if (! $error)
{
return $bank_line_id;
}
else
{
return -1;
}
}
/**
* Mise a jour du lien entre le paiement et la ligne generee dans llx_bank
*
* @param int $id_bank Id compte bancaire
* @return int <0 if KO, >0 if OK
*/
// phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
function update_fk_bank($id_bank)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' set fk_bank = '.$id_bank;
$sql.= ' WHERE rowid = '.$this->id;
dol_syslog(get_class($this).'::update_fk_bank', LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this).'::update_fk_bank '.$this->error);
return -1;
}
}
/**
* Updates the payment date
*
* @param int $date New date
* @return int <0 if KO, 0 if OK
*/
// phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
function update_date($date)
{
if (!empty($date) && $this->statut!=1)
{
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET datep = '".$this->db->idate($date)."'";
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update_date", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
$this->datepaye = $date;
$this->date = $date;
return 0;
}
else
{
$this->error='Error -1 '.$this->db->error();
return -2;
}
}
return -1; //no date given or already validated
}
/**
* Updates the payment number
*
* @param string $num New num
* @return int <0 if KO, 0 if OK
*/
// phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
function update_num($num)
{
if(!empty($num) && $this->statut!=1)
{
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET num_paiement = '".$this->db->escape($num)."'";
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update_num", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
$this->numero = $this->db->escape($num);
return 0;
}
else
{
$this->error='Error -1 '.$this->db->error();
return -2;
}
}
return -1; //no num given or already validated
}
/**
* Validate payment
*
* @return int <0 if KO, >0 if OK
*/
function valide()
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 1 WHERE rowid = '.$this->id;
dol_syslog(get_class($this).'::valide', LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this).'::valide '.$this->error);
return -1;
}
}
/**
* Reject payment
*
* @return int <0 if KO, >0 if OK
*/
function reject()
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 2 WHERE rowid = '.$this->id;
dol_syslog(get_class($this).'::reject', LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this).'::reject '.$this->error);
return -1;
}
}
/**
* Information sur l'objet
*
* @param int $id id du paiement dont il faut afficher les infos
* @return void
*/
function info($id)
{
$sql = 'SELECT p.rowid, p.datec, p.fk_user_creat, p.fk_user_modif, p.tms';
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p';
$sql.= ' WHERE p.rowid = '.$id;
dol_syslog(get_class($this).'::info', LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
if ($this->db->num_rows($result))
{
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
if ($obj->fk_user_creat)
{
$cuser = new User($this->db);
$cuser->fetch($obj->fk_user_creat);
$this->user_creation = $cuser;
}
if ($obj->fk_user_modif)
{
$muser = new User($this->db);
$muser->fetch($obj->fk_user_modif);
$this->user_modification = $muser;
}
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->tms);
}
$this->db->free($result);
}
else
{
dol_print_error($this->db);
}
}
/**
* Retourne la liste des factures sur lesquels porte le paiement
*
* @param string $filter Critere de filtre
* @return array Tableau des id de factures
*/
function getBillsArray($filter='')
{
$sql = 'SELECT fk_facture';
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f';
$sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id;
if ($filter) $sql.= ' AND '.$filter;
$resql = $this->db->query($sql);
if ($resql)
{
$i=0;
$num=$this->db->num_rows($resql);
$billsarray=array();
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$billsarray[$i]=$obj->fk_facture;
$i++;
}
return $billsarray;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this).'::getBillsArray Error '.$this->error.' -', LOG_DEBUG);
return -1;
}
}
/**
* Return next reference of customer invoice not already used (or last reference)
* according to numbering module defined into constant FACTURE_ADDON
*
* @param Societe $soc object company
* @param string $mode 'next' for next value or 'last' for last value
* @return string free ref or last ref
*/
function getNextNumRef($soc,$mode='next')
{
global $conf, $db, $langs;
$langs->load("bills");
// Clean parameters (if not defined or using deprecated value)
if (empty($conf->global->PAYMENT_ADDON)) $conf->global->PAYMENT_ADDON='mod_payment_cicada';
else if ($conf->global->PAYMENT_ADDON=='ant') $conf->global->PAYMENT_ADDON='mod_payment_ant';
else if ($conf->global->PAYMENT_ADDON=='cicada') $conf->global->PAYMENT_ADDON='mod_payment_cicada';
if (! empty($conf->global->PAYMENT_ADDON))
{
$mybool=false;
$file = $conf->global->PAYMENT_ADDON.".php";
$classname = $conf->global->PAYMENT_ADDON;
// Include file with class
$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
foreach ($dirmodels as $reldir) {
$dir = dol_buildpath($reldir."core/modules/payment/");
// Load file with numbering class (if found)
if (is_file($dir.$file) && is_readable($dir.$file))
{
$mybool |= include_once $dir . $file;
}
}
// For compatibility
if (! $mybool)
{
$file = $conf->global->PAYMENT_ADDON.".php";
$classname = "mod_payment_".$conf->global->PAYMENT_ADDON;
$classname = preg_replace('/\-.*$/','',$classname);
// Include file with class
foreach ($conf->file->dol_document_root as $dirroot)
{
$dir = $dirroot."/core/modules/payment/";
// Load file with numbering class (if found)
if (is_file($dir.$file) && is_readable($dir.$file)) {
$mybool |= include_once $dir . $file;
}
}
}
if (! $mybool)
{
dol_print_error('',"Failed to include file ".$file);
return '';
}
$obj = new $classname();
$numref = "";
$numref = $obj->getNextValue($soc,$this);
/**
* $numref can be empty in case we ask for the last value because if there is no invoice created with the
* set up mask.
*/
if ($mode != 'last' && !$numref) {
dol_print_error($db,"Payment::getNextNumRef ".$obj->error);
return "";
}