-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
account.class.php
1464 lines (1279 loc) · 46 KB
/
account.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) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
* Copyright (C) 2005-2010 Regis Houssin <regis.houssin@capnetworks.com>
* Copytight (C) 2013 Florian Henry <florian.henry@open-concept.pro>
*
* 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/bank/class/account.class.php
* \ingroup banque
* \brief File of class to manage bank accounts
*/
require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
/**
* Class to manage bank accounts
*/
class Account extends CommonObject
{
public $element='bank_account';
public $table_element='bank_account';
var $rowid; // deprecated
var $id;
var $ref;
var $label;
//! 1=Compte courant/check/carte, 2=Compte liquide, 0=Compte épargne
var $courant;
var $type; // same as courant
//! Name
var $bank;
var $clos;
var $rappro=1; // If bank need to be conciliated
var $url;
//! BBAN field for French Code banque
var $code_banque;
//! BBAN field for French Code guichet
var $code_guichet;
//! BBAN main account number
var $number;
//! BBAN field for French Cle de controle
var $cle_rib;
//! BIC/SWIFT number
var $bic;
//! IBAN number (International Bank Account Number)
var $iban_prefix;
var $proprio;
var $owner_address;
var $state_id;
var $state_code;
var $state;
var $country_id;
var $country_code;
var $country;
var $type_lib=array();
var $account_number;
var $accountancy_journal;
var $currency_code;
var $min_allowed;
var $min_desired;
var $comment;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
global $langs;
$this->db = $db;
$this->clos = 0;
$this->solde = 0;
$this->type_lib[0]=$langs->trans("BankType0");
$this->type_lib[1]=$langs->trans("BankType1");
$this->type_lib[2]=$langs->trans("BankType2");
$this->status[0]=$langs->trans("StatusAccountOpened");
$this->status[1]=$langs->trans("StatusAccountClosed");
return 1;
}
/**
* Return if a bank account need to be conciliated
*
* @return int 1 if need to be concialiated, < 0 otherwise.
*/
function canBeConciliated()
{
if (empty($this->rappro)) return -1;
if ($this->courant == 2) return -2;
if ($this->clos) return -3;
return 1;
}
/**
* Add a link between bank line record and its source
*
* @param int $line_id Id ecriture bancaire
* @param int $url_id Id parametre url
* @param string $url Url
* @param string $label Link label
* @param string $type Type of link ('payment', 'company', 'member', ...)
* @return int <0 if KO, id line if OK
*/
function add_url_line($line_id, $url_id, $url, $label, $type)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
$sql.= "fk_bank";
$sql.= ", url_id";
$sql.= ", url";
$sql.= ", label";
$sql.= ", type";
$sql.= ") VALUES (";
$sql.= "'".$line_id."'";
$sql.= ", '".$url_id."'";
$sql.= ", '".$url."'";
$sql.= ", '".$this->db->escape($label)."'";
$sql.= ", '".$type."'";
$sql.= ")";
dol_syslog(get_class($this)."::add_url_line sql=".$sql);
if ($this->db->query($sql))
{
$rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
return $rowid;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::add_url_line ".$this->error, LOG_ERR);
return -1;
}
}
/**
* TODO Move this into AccountLine
* Return array with links from llx_bank_url
*
* @param int $fk_bank To search using bank transaction id
* @param int $url_id To search using link to
* @param string $type To search using type
* @return array Array of links
*/
function get_url($fk_bank='', $url_id='', $type='')
{
$lines = array();
// Check parameters
if (! empty($fk_bank) && (! empty($url_id) || ! empty($type)))
{
$this->error="ErrorBadParameter";
return -1;
}
$sql = "SELECT fk_bank, url_id, url, label, type";
$sql.= " FROM ".MAIN_DB_PREFIX."bank_url";
if ($fk_bank > 0) {
$sql.= " WHERE fk_bank = ".$fk_bank;
}
else { $sql.= " WHERE url_id = ".$url_id." AND type = '".$type."'";
}
$sql.= " ORDER BY type, label";
dol_syslog(get_class($this)."::get_url sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$i = 0;
$num = $this->db->num_rows($result);
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
// Anciens liens (pour compatibilite)
$lines[$i][0] = $obj->url;
$lines[$i][1] = $obj->url_id;
$lines[$i][2] = $obj->label;
$lines[$i][3] = $obj->type;
// Nouveaux liens
$lines[$i]['url'] = $obj->url;
$lines[$i]['url_id'] = $obj->url_id;
$lines[$i]['label'] = $obj->label;
$lines[$i]['type'] = $obj->type;
$lines[$i]['fk_bank'] = $obj->fk_bank;
$i++;
}
}
else dol_print_error($this->db);
return $lines;
}
/**
* Add an entry into table ".MAIN_DB_PREFIX."bank
*
* @param timsestmap $date Date operation
* @param string $oper 1,2,3,4... (deprecated) or TYP,VIR,PRE,LIQ,VAD,CB,CHQ...
* @param string $label Descripton
* @param float $amount Amount
* @param string $num_chq Numero cheque ou virement
* @param string $categorie Categorie optionnelle
* @param User $user User that create
* @param string $emetteur Name of cheque writer
* @param string $banque Bank of cheque writer
* @return int Rowid of added entry, <0 if KO
*/
function addline($date, $oper, $label, $amount, $num_chq, $categorie, $user, $emetteur='',$banque='')
{
// Clean parameters
$emetteur=trim($emetteur);
$banque=trim($banque);
$now=dol_now();
if (is_numeric($oper)) // Clean oper to have a code instead of a rowid
{
$sql ="SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
$sql.=" WHERE id=".$oper;
$resql=$this->db->query($sql);
if ($resql)
{
$obj=$this->db->fetch_object($resql);
$oper=$obj->code;
}
else
{
dol_print_error($this->db,'Failed to get payment type code');
return -1;
}
}
// Check parameters
if (! $oper)
{
$this->error="oper not defined";
return -1;
}
if (! $this->rowid)
{
$this->error="this->rowid not defined";
return -2;
}
if ($this->courant == 2 && $oper != 'LIQ')
{
$this->error="ErrorCashAccountAcceptsOnlyCashMoney";
return -3;
}
$this->db->begin();
$datev = $date;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
$sql.= "datec";
$sql.= ", dateo";
$sql.= ", datev";
$sql.= ", label";
$sql.= ", amount";
$sql.= ", fk_user_author";
$sql.= ", num_chq";
$sql.= ", fk_account";
$sql.= ", fk_type";
$sql.= ",emetteur,banque";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."'";
$sql.= ", '".$this->db->idate($date)."'";
$sql.= ", '".$this->db->idate($datev)."'";
$sql.= ", '".$this->db->escape($label)."'";
$sql.= ", ".price2num($amount);
$sql.= ", '".$user->id."'";
$sql.= ", ".($num_chq?"'".$num_chq."'":"null");
$sql.= ", '".$this->rowid."'";
$sql.= ", '".$oper."'";
$sql.= ", ".($emetteur?"'".$this->db->escape($emetteur)."'":"null");
$sql.= ", ".($banque?"'".$this->db->escape($banque)."'":"null");
$sql.= ")";
dol_syslog(get_class($this)."::addline sql=".$sql);
if ($this->db->query($sql))
{
$rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank");
if ($categorie)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
$sql.= "lineid";
$sql.= ", fk_categ";
$sql.= ") VALUES (";
$sql.= "'".$rowid."'";
$sql.= ", '".$categorie."'";
$sql.= ")";
$result = $this->db->query($sql);
if (! $result)
{
$this->db->rollback();
$this->error=$this->db->error();
return -3;
}
}
$this->db->commit();
return $rowid;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::addline ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
/**
* Create bank account into database
*
* @return int < 0 if KO, > 0 if OK
*/
function create()
{
global $langs,$conf;
// Clean parameters
if (! $this->min_allowed) $this->min_allowed=0;
if (! $this->min_desired) $this->min_desired=0;
$this->state_id = ($this->state_id?$this->state_id:$this->state_id);
$this->country_id = ($this->country_id?$this->country_id:$this->country_id);
// Check parameters
if (empty($this->country_id))
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -1;
}
if (empty($this->ref))
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -1;
}
if (empty($this->date_solde))
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateInitialBalance"));
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -1;
}
// Chargement librairie pour acces fonction controle RIB
require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
$now=dol_now();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
$sql.= "datec";
$sql.= ", ref";
$sql.= ", label";
$sql.= ", entity";
$sql.= ", account_number";
$sql.= ", accountancy_journal";
$sql.= ", currency_code";
$sql.= ", rappro";
$sql.= ", min_allowed";
$sql.= ", min_desired";
$sql.= ", comment";
$sql.= ", state_id";
$sql.= ", fk_pays";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."'";
$sql.= ", '".$this->db->escape($this->ref)."'";
$sql.= ", '".$this->db->escape($this->label)."'";
$sql.= ", ".$conf->entity;
$sql.= ", '".$this->db->escape($this->account_number)."'";
$sql.= ", '".$this->db->escape($this->accountancy_journal)."'";
$sql.= ", '".$this->currency_code."'";
$sql.= ", ".$this->rappro;
$sql.= ", ".price2num($this->min_allowed);
$sql.= ", ".price2num($this->min_desired);
$sql.= ", '".$this->db->escape($this->comment)."'";
$sql.= ", ".($this->state_id>0?"'".$this->state_id."'":"null");
$sql.= ", ".$this->country_id;
$sql.= ")";
dol_syslog(get_class($this)."::create sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
$result=$this->update();
if ($result > 0)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
$sql.= "datec";
$sql.= ", label";
$sql.= ", amount";
$sql.= ", fk_account";
$sql.= ", datev";
$sql.= ", dateo";
$sql.= ", fk_type";
$sql.= ", rappro";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."'";
$sql.= ", '(".$langs->trans("InitialBankBalance").")'";
$sql.= ", ".price2num($this->solde);
$sql.= ", '".$this->id."'";
$sql.= ", '".$this->db->idate($this->date_solde)."'";
$sql.= ", '".$this->db->idate($this->date_solde)."'";
$sql.= ", 'SOLD'";
$sql.= ", 0"; // Not conciliated by default
$sql.= ")";
$resql=$this->db->query($sql);
if (! $resql)
{
$this->error=$this->db->lasterror();
dol_syslog($this->error, LOG_ERR);
return -3;
}
}
return $this->id;
}
else
{
if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
$this->error=$langs->trans("ErrorBankLabelAlreadyExists");
dol_syslog($this->error, LOG_ERR);
return -1;
}
else {
$this->error=$this->db->error()." sql=".$sql;
dol_syslog($this->error, LOG_ERR);
return -2;
}
}
}
/**
* Update bank account card
*
* @param User $user Object user making action
* @return int <0 si ko, >0 si ok
*/
function update($user='')
{
global $langs,$conf;
// Clean parameters
if (! $this->min_allowed) $this->min_allowed=0;
if (! $this->min_desired) $this->min_desired=0;
$this->state_id = ($this->state_id?$this->state_id:$this->state_id);
$this->country_id = ($this->country_id?$this->country_id:$this->country_id);
// Check parameters
if (empty($this->country_id))
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
return -1;
}
if (empty($this->ref))
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
return -1;
}
if (! $this->label) $this->label = "???";
$sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
$sql.= " ref = '".$this->db->escape($this->ref)."'";
$sql.= ",label = '".$this->db->escape($this->label)."'";
$sql.= ",courant = ".$this->courant;
$sql.= ",clos = ".$this->clos;
$sql.= ",rappro = ".$this->rappro;
$sql.= ",url = ".($this->url?"'".$this->url."'":"null");
$sql.= ",account_number = '".$this->account_number."'";
$sql.= ",accountancy_journal = '".$this->accountancy_journal."'";
$sql.= ",currency_code = '".$this->currency_code."'";
$sql.= ",min_allowed = '".price2num($this->min_allowed)."'";
$sql.= ",min_desired = '".price2num($this->min_desired)."'";
$sql.= ",comment = '".$this->db->escape($this->comment)."'";
$sql.= ",state_id = ".($this->state_id>0?"'".$this->state_id."'":"null");
$sql.= ",fk_pays = ".$this->country_id;
$sql.= " WHERE rowid = ".$this->id;
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::update sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_print_error($this->db);
return -1;
}
}
/**
* Update BBAN (RIB) account fields
*
* @param User $user Object user making update
* @return int <0 if KO, >0 if OK
*/
function update_bban($user='')
{
global $conf,$langs;
// Clean parameters
$this->state_id = ($this->state_id?$this->state_id:$this->state_id);
$this->country_id = ($this->country_id?$this->country_id:$this->country_id);
// Chargement librairie pour acces fonction controle RIB
require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
// Check parameters
if (! $this->ref)
{
$this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->trans("Ref"));
return -2;
}
$sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
$sql.= " bank = '".$this->db->escape($this->bank)."'";
$sql.= ",code_banque='".$this->code_banque."'";
$sql.= ",code_guichet='".$this->code_guichet."'";
$sql.= ",number='".$this->number."'";
$sql.= ",cle_rib='".$this->cle_rib."'";
$sql.= ",bic='".$this->bic."'";
$sql.= ",iban_prefix = '".$this->iban."'";
$sql.= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
$sql.= ",proprio = '".$this->db->escape($this->proprio)."'";
$sql.= ",owner_address = '".$this->db->escape($this->owner_address)."'";
$sql.= ",state_id = ".($this->state_id>0?"'".$this->state_id."'":"null");
$sql.= ",fk_pays = ".$this->country_id;
$sql.= " WHERE rowid = ".$this->id;
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::update_bban sql=$sql");
$result = $this->db->query($sql);
if ($result)
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_print_error($this->db);
return -1;
}
}
/**
* Load a bank account into memory from database
*
* @param int $id Id of bank account to get
* @param string $ref Ref of bank account to get
* @return int <0 if KO, >0 if OK
*/
function fetch($id,$ref='')
{
global $conf;
if (empty($id) && empty($ref))
{
$this->error="ErrorBadParameters";
return -1;
}
$sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
$sql.= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
$sql.= " ba.domiciliation, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
$sql.= " ba.account_number, ba.accountancy_journal, ba.currency_code,";
$sql.= " ba.min_allowed, ba.min_desired, ba.comment,";
$sql.= ' p.code as country_code, p.libelle as country,';
$sql.= ' d.code_departement as state_code, d.nom as state';
$sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_pays as p ON ba.fk_pays = p.rowid';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
$sql.= " WHERE entity IN (".getEntity($this->element, 1).")";
if ($id) $sql.= " AND ba.rowid = ".$id;
if ($ref) $sql.= " AND ba.ref = '".$this->db->escape($ref)."'";
dol_syslog(get_class($this)."::fetch sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
if ($this->db->num_rows($result))
{
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
$this->rowid = $obj->rowid; // deprecated
$this->ref = $obj->ref;
$this->label = $obj->label;
$this->type = $obj->courant;
$this->courant = $obj->courant;
$this->bank = $obj->bank;
$this->clos = $obj->clos;
$this->rappro = $obj->rappro;
$this->url = $obj->url;
$this->code_banque = $obj->code_banque;
$this->code_guichet = $obj->code_guichet;
$this->number = $obj->number;
$this->cle_rib = $obj->cle_rib;
$this->bic = $obj->bic;
$this->iban = $obj->iban;
$this->iban_prefix = $obj->iban; // deprecated
$this->domiciliation = $obj->domiciliation;
$this->proprio = $obj->proprio;
$this->owner_address = $obj->owner_address;
$this->state_id = $obj->state_id;
$this->state_code = $obj->state_code;
$this->state = $obj->state;
$this->country_id = $obj->country_id;
$this->country_code = $obj->country_code;
$this->country = $obj->country;
$this->account_number = $obj->account_number;
$this->accountancy_journal = $obj->accountancy_journal;
$this->currency_code = $obj->currency_code;
$this->account_currency_code = $obj->currency_code;
$this->min_allowed = $obj->min_allowed;
$this->min_desired = $obj->min_desired;
$this->comment = $obj->comment;
return 1;
}
else
{
return 0;
}
$this->db->free($result);
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Delete bank account from database
*
* @return int <0 if KO, >0 if OK
*/
function delete()
{
global $conf;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
$sql.= " WHERE rowid = ".$this->rowid;
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::delete sql=".$sql);
$result = $this->db->query($sql);
if ($result) {
return 1;
}
else {
dol_print_error($this->db);
return -1;
}
}
/**
* Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
* @return string Libelle
*/
function getLibStatut($mode=0)
{
return $this->LibStatut($this->clos,$mode);
}
/**
* Renvoi le libelle d'un statut donne
*
* @param int $statut Id statut
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Libelle du statut
*/
function LibStatut($statut,$mode=0)
{
global $langs;
$langs->load('banks');
if ($mode == 0)
{
if ($statut==0) return $langs->trans("StatusAccountOpened");
if ($statut==1) return $langs->trans("StatusAccountClosed");
}
if ($mode == 1)
{
if ($statut==0) return $langs->trans("StatusAccountOpened");
if ($statut==1) return $langs->trans("StatusAccountClosed");
}
if ($mode == 2)
{
if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4').' '.$langs->trans("StatusAccountOpened");
if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5').' '.$langs->trans("StatusAccountClosed");
}
if ($mode == 3)
{
if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4');
if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5');
}
if ($mode == 4)
{
if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4').' '.$langs->trans("StatusAccountOpened");
if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5').' '.$langs->trans("StatusAccountClosed");
}
if ($mode == 5)
{
if ($statut==0) return $langs->trans("StatusAccountOpened").' '.img_picto($langs->trans("StatusAccountOpened"),'statut4');
if ($statut==1) return $langs->trans("StatusAccountClosed").' '.img_picto($langs->trans("StatusAccountClosed"),'statut5');
}
}
/**
* Renvoi si un compte peut etre supprimer ou non (sans mouvements)
*
* @return boolean vrai si peut etre supprime, faux sinon
*/
function can_be_deleted()
{
$can_be_deleted=false;
$sql = "SELECT COUNT(rowid) as nb";
$sql.= " FROM ".MAIN_DB_PREFIX."bank";
$sql.= " WHERE fk_account=".$this->id;
$resql = $this->db->query($sql);
if ($resql) {
$obj=$this->db->fetch_object($resql);
if ($obj->nb <= 1) $can_be_deleted=true; // Juste le solde
}
else {
dol_print_error($this->db);
}
return $can_be_deleted;
}
/**
* Return error
*
* @return string Error string
*/
function error()
{
return $this->error;
}
/**
* Return current sold
*
* @param int $option 1=Exclude future operation date (this is to exclude input made in advance and have real account sold)
* @return int Current sold (value date <= today)
*/
function solde($option=0)
{
$sql = "SELECT sum(amount) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."bank";
$sql.= " WHERE fk_account = ".$this->id;
if ($option == 1) $sql.= " AND dateo <= '".$this->db->idate(dol_now())."'";
$resql = $this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj=$this->db->fetch_object($resql);
$solde = $obj->amount;
}
$this->db->free($resql);
return $solde;
}
}
/**
* Load indicators for dashboard (this->nbtodo and this->nbtodolate)
*
* @param User $user Objet user
* @param int $filteraccountid To get info for a particular account id
* @return int <0 if KO, 0=Nothing to show, >0 if OK
*/
function load_board($user,$filteraccountid=0)
{
global $conf;
if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
$now=dol_now();
$this->nbtodo=$this->nbtodolate=0;
$sql = "SELECT b.rowid, b.datev as datefin";
$sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
$sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
$sql.= " WHERE b.rappro=0";
$sql.= " AND b.fk_account = ba.rowid";
$sql.= " AND ba.entity = ".$conf->entity;
$sql.= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
if ($filteraccountid) $sql.=" AND ba.rowid = ".$filteraccountid;
//print $sql;
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
while ($obj=$this->db->fetch_object($resql))
{
$this->nbtodo++;
if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) $this->nbtodolate++;
}
return $num;
}
else
{
dol_print_error($this->db);
$this->error=$this->db->error();
return -1;
}
}
/**
* Renvoie nom clicable (avec eventuellement le picto)
*
* @param int $withpicto Inclut le picto dans le lien
* @param string $mode ''=Link to card, 'transactions'=Link to transactions card
* @return string Chaine avec URL
*/
function getNomUrl($withpicto=0, $mode='')
{
global $langs;
$result='';
if (empty($mode))
{
$lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/fiche.php?id='.$this->id.'">';
$lienfin='</a>';
}
else if ($mode == 'transactions')
{
$lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/account.php?account='.$this->id.'">';
$lienfin='</a>';
}
if ($withpicto) $result.=($lien.img_object($langs->trans("ShowAccount"),'account').$lienfin.' ');
$result.=$lien.$this->label.$lienfin;
return $result;
}
// Method after here are common to Account and CompanyBankAccount
/**
* Return if an account has valid information
*
* @return int 1 if correct, <=0 if wrong
*/
function verif()
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php';
// Call function to check BAN
if (! checkBanForAccount($this))
{
$this->error_number = 12;
$this->error_message = 'RIBControlError';
}
if ($this->error_number == 0)
{
return 1;
}
else
{
return 0;
}
}
/**
* Return account country code
*
* @return string country code
*/
function getCountryCode()
{
global $mysoc;
// We return country code of bank account
if (! empty($this->country_code)) return $this->country_code;
// For backward compatibility, we try to guess country from other information
if (! empty($this->iban))
{
if ($mysoc->country_code === 'IN') return $mysoc->country_code; // Test to know if we can trust IBAN
// If IBAN defined, we can know country of account from it
if (preg_match("/^([a-zA-Z][a-zA-Z])/i",$this->iban,$reg)) return $reg[1];
}
// If this class is linked to a third party
if (! empty($this->socid))
{
require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
$company=new Societe($this->db);
$result=$company->fetch($this->socid);
if (! empty($company->country_code)) return $company->country_code;
}
// We return country code of managed company
if (! empty($mysoc->country_code)) return $mysoc->country_code;
return '';
}
/**
* Return if a bank account is defined with detailed information (bank code, desk code, number and key).
* More information on codes used by countries on page http://en.wikipedia.org/wiki/Bank_code
*
* @return int 0=No bank code need + Account number is enough
* 1=Need 2 fields for bank code: Bank, Desk (France, Spain, ...) + Account number and key
* 2=Neek 1 field for bank code: Bank only (Sort code for Great Britain, BSB for Australia) + Account number
*/
function useDetailedBBAN()
{
$country_code=$this->getCountryCode();
if (in_array($country_code,array('CH','DE','FR','ES','GA','IT'))) return 1; // France, Spain, Gabon
if (in_array($country_code,array('AU','BE','CA','DK','GR','GB','ID','IE','IR','KR','NL','NZ','UK','US'))) return 2; // Australia, Great Britain...
return 0;
}
/**
* Load miscellaneous information for tab "Info"
*
* @param int $id Id of object to load
* @return void
*/
function info($id)
{
}
/**
* Initialise an instance with random values.