/
asset.class.php
1571 lines (1396 loc) · 60.5 KB
/
asset.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) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
/**
* \file asset/class/asset.class.php
* \ingroup asset
* \brief This file is a CRUD class file for Asset (Create/Read/Update/Delete)
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/**
* Class for Asset
*/
class Asset extends CommonObject
{
/**
* @var string ID of module.
*/
public $module = 'asset';
/**
* @var string ID to identify managed object.
*/
public $element = 'asset';
/**
* @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
*/
public $table_element = 'asset';
/**
* @var string String with name of icon for asset. Must be the part after the 'object_' into object_asset.png
*/
public $picto = 'asset';
const STATUS_DRAFT = 0; // In progress
const STATUS_DISPOSED = 9; // Disposed
/**
* 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
* Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
* 'label' the translation key.
* 'picto' is code of a picto to show before value in forms
* 'enabled' is a condition when the field must be managed (Example: 1 or 'getDolGlobalString("MY_SETUP_PARAM")'
* 'position' is the sort order of field.
* 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
* 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
* 'noteditable' says if field is not editable (1 or 0)
* 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
* 'index' if we want an index in database.
* 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...).
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
* 'isameasure' must be set to 1 or 2 if field can be used for measure. Field type must be summable like integer or double(24,8). Use 1 in most cases, or 2 if you don't want to see the column total into list (for example for percentage)
* 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'css'=>'minwidth300 maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200'
* 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
* 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar'
* 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
* 'comment' is not used. You can store here any text of your choice. It is not used by application.
* 'validate' is 1 if need to validate with $this->validateField()
* 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value)
*
* Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
*/
/**
* @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}> Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
*/
public $fields = array(
'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id"),
'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 1, 'noteditable' => 0, 'index' => 1, 'searchall' => 1, 'showoncombobox' => 1, 'validate' => 1, 'comment' => "Reference of object"),
'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'searchall' => 1, 'css' => 'minwidth300', 'cssview' => 'wordbreak', 'showoncombobox' => '2', 'validate' => 1,),
'fk_asset_model' => array('type' => 'integer:AssetModel:asset/class/assetmodel.class.php:1:((status:=:1) and (entity:IN:__SHARED_ENTITIES__))', 'label' => 'AssetModel', 'enabled' => 1, 'position' => 40, 'notnull' => 0, 'visible' => 1, 'index' => 1, 'validate' => 1,),
'qty' => array('type' => 'real', 'label' => 'Qty', 'enabled' => 1, 'position' => 50, 'notnull' => 1, 'visible' => 0, 'default' => '1', 'isameasure' => 1, 'css' => 'maxwidth75imp', 'validate' => 1,),
'acquisition_type' => array('type' => 'smallint', 'label' => 'AssetAcquisitionType', 'enabled' => 1, 'position' => 60, 'notnull' => 1, 'visible' => 1, 'arrayofkeyval' => array('0' => 'AssetAcquisitionTypeNew', '1' => 'AssetAcquisitionTypeOccasion'), 'validate' => 1,),
'asset_type' => array('type' => 'smallint', 'label' => 'AssetType', 'enabled' => 1, 'position' => 70, 'notnull' => 1, 'visible' => 1, 'arrayofkeyval' => array('0' => 'AssetTypeIntangible', '1' => 'AssetTypeTangible', '2' => 'AssetTypeInProgress', '3' => 'AssetTypeFinancial'), 'validate' => 1,),
'not_depreciated' => array('type' => 'boolean', 'label' => 'AssetNotDepreciated', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'default' => '0', 'visible' => 1, 'validate' => 1,),
'date_acquisition' => array('type' => 'date', 'label' => 'AssetDateAcquisition', 'enabled' => 1, 'position' => 90, 'notnull' => 1, 'visible' => 1,),
'date_start' => array('type' => 'date', 'label' => 'AssetDateStart', 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => -1,),
'acquisition_value_ht' => array('type' => 'price', 'label' => 'AssetAcquisitionValueHT', 'enabled' => 1, 'position' => 110, 'notnull' => 1, 'visible' => 1, 'isameasure' => 1, 'validate' => 1,),
'recovered_vat' => array('type' => 'price', 'label' => 'AssetRecoveredVAT', 'enabled' => 1, 'position' => 120, 'notnull' => 0, 'visible' => 1, 'isameasure' => 1, 'validate' => 1,),
'reversal_date' => array('type' => 'date', 'label' => 'AssetReversalDate', 'enabled' => 1, 'position' => 130, 'notnull' => 0, 'visible' => 1,),
'reversal_amount_ht' => array('type' => 'price', 'label' => 'AssetReversalAmountHT', 'enabled' => 1, 'position' => 140, 'notnull' => 0, 'visible' => 1, 'isameasure' => 1, 'validate' => 1,),
'disposal_date' => array('type' => 'date', 'label' => 'AssetDisposalDate', 'enabled' => 1, 'position' => 200, 'notnull' => 0, 'visible' => -2,),
'disposal_amount_ht' => array('type' => 'price', 'label' => 'AssetDisposalAmount', 'enabled' => 1, 'position' => 210, 'notnull' => 0, 'visible' => -2, 'default' => '0', 'isameasure' => 1, 'validate' => 1,),
'fk_disposal_type' => array('type' => 'sellist:c_asset_disposal_type:label:rowid::active=1', 'label' => 'AssetDisposalType', 'enabled' => 1, 'position' => 220, 'notnull' => 0, 'visible' => -2, 'index' => 1, 'validate' => 1,),
'disposal_depreciated' => array('type' => 'boolean', 'label' => 'AssetDisposalDepreciated', 'enabled' => 1, 'position' => 230, 'notnull' => 0, 'default' => '0', 'visible' => -2, 'validate' => 1,),
'disposal_subject_to_vat' => array('type' => 'boolean', 'label' => 'AssetDisposalSubjectToVat', 'enabled' => 1, 'position' => 240, 'notnull' => 0, 'default' => '0', 'visible' => -2, 'validate' => 1,),
'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'position' => 300, 'notnull' => 0, 'visible' => 0, 'validate' => 1,),
'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'position' => 301, 'notnull' => 0, 'visible' => 0, 'validate' => 1,),
'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 500, 'notnull' => 1, 'visible' => -2,),
'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 501, 'notnull' => 0, 'visible' => -2,),
'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'position' => 510, 'notnull' => 1, 'visible' => -2, 'foreignkey' => 'user.rowid',),
'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 511, 'notnull' => -1, 'visible' => -2,),
'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'LastMainDoc', 'enabled' => 1, 'position' => 600, 'notnull' => 0, 'visible' => 0,),
'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 1000, 'notnull' => -1, 'visible' => -2,),
'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'position' => 1010, 'notnull' => -1, 'visible' => 0,),
'status' => array('type' => 'smallint', 'label' => 'Status', 'enabled' => 1, 'position' => 1000, 'notnull' => 1, 'default' => '0', 'visible' => 2, 'index' => 1, 'arrayofkeyval' => array('0' => 'Draft', '1' => 'Validated', '9' => 'Canceled'), 'validate' => 1,),
);
public $rowid;
public $ref;
public $label;
public $fk_asset_model;
public $reversal_amount_ht;
public $acquisition_value_ht;
public $recovered_vat;
public $reversal_date;
public $date_acquisition;
public $date_start;
public $qty;
public $acquisition_type;
public $asset_type;
public $not_depreciated;
public $disposal_date;
public $disposal_amount_ht;
public $fk_disposal_type;
public $disposal_depreciated;
public $disposal_subject_to_vat;
public $supplier_invoice_id;
public $note_public;
public $note_private;
public $date_creation;
public $fk_user_creat;
public $fk_user_modif;
public $last_main_doc;
public $import_key;
public $model_pdf;
public $status;
/**
* @var static object oldcopy
*/
public $oldcopy;
/**
* @var AssetDepreciationOptions Used for computed fields of depreciation options class.
*/
public $asset_depreciation_options;
public $asset_accountancy_codes;
/**
* @var array List of depreciation lines for each mode (sort by depreciation date).
*/
public $depreciation_lines = array();
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct(DoliDB $db)
{
global $conf, $langs;
$this->db = $db;
$this->ismultientitymanaged = 1;
$this->isextrafieldmanaged = 1;
if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
$this->fields['rowid']['visible'] = 0;
}
if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
$this->fields['entity']['enabled'] = 0;
}
// Unset fields that are disabled
foreach ($this->fields as $key => $val) {
if (isset($val['enabled']) && empty($val['enabled'])) {
unset($this->fields[$key]);
}
}
// Translate some data of arrayofkeyval
if (is_object($langs)) {
foreach ($this->fields as $key => $val) {
if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
foreach ($val['arrayofkeyval'] as $key2 => $val2) {
$this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
}
}
}
}
}
/**
* Create object into database
*
* @param User $user User that creates
* @param int $notrigger false=launch triggers after, true=disable triggers
* @return int Return integer <0 if KO, Id of created object if OK
*/
public function create(User $user, $notrigger = 0)
{
if (!isset($this->date_start) || $this->date_start === "") {
$this->date_start = $this->date_acquisition;
}
$this->db->begin();
$result = $result_create = $this->createCommon($user, $notrigger);
if ($result > 0 && $this->fk_asset_model > 0) {
$result = $this->setDataFromAssetModel($user, $notrigger);
}
if ($result > 0) {
if ($this->supplier_invoice_id > 0) {
$this->add_object_linked('invoice_supplier', $this->supplier_invoice_id);
}
}
if ($result < 0) {
$this->db->rollback();
} else {
$this->db->commit();
}
return $result > 0 ? $result_create : $result;
}
/**
* Clone an object into another one
*
* @param User $user User that creates
* @param int $fromid Id of object to clone
* @return mixed New object created, <0 if KO
*/
public function createFromClone(User $user, $fromid)
{
global $langs, $extrafields;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
// $object = new self($this->db);
//
// $this->db->begin();
//
// // Load source object
// $result = $object->fetchCommon($fromid);
// if ($result > 0 && !empty($object->table_element_line)) {
// $object->fetchLines();
// }
//
// // get lines so they will be clone
// //foreach($this->lines as $line)
// // $line->fetch_optionals();
//
// // Reset some properties
// unset($object->id);
// unset($object->fk_user_creat);
// unset($object->import_key);
//
// // Clear fields
// if (property_exists($object, 'ref')) {
// $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
// }
// if (property_exists($object, 'label')) {
// $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
// }
// if (property_exists($object, 'status')) {
// $object->status = self::STATUS_DRAFT;
// }
// if (property_exists($object, 'date_creation')) {
// $object->date_creation = dol_now();
// }
// if (property_exists($object, 'date_modification')) {
// $object->date_modification = null;
// }
// // ...
// // Clear extrafields that are unique
// if (is_array($object->array_options) && count($object->array_options) > 0) {
// $extrafields->fetch_name_optionals_label($this->table_element);
// foreach ($object->array_options as $key => $option) {
// $shortkey = preg_replace('/options_/', '', $key);
// if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
// //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
// unset($object->array_options[$key]);
// }
// }
// }
//
// // Create clone
// $object->context['createfromclone'] = 'createfromclone';
// $result = $object->createCommon($user);
// if ($result < 0) {
// $error++;
// $this->error = $object->error;
// $this->errors = $object->errors;
// }
//
// if (!$error) {
// // copy internal contacts
// if ($this->copy_linked_contact($object, 'internal') < 0) {
// $error++;
// }
// }
//
// if (!$error) {
// // copy external contacts if same company
// if (property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
// if ($this->copy_linked_contact($object, 'external') < 0) {
// $error++;
// }
// }
// }
//
// unset($object->context['createfromclone']);
//
// // End
// if (!$error) {
// $this->db->commit();
// return $object;
// } else {
// $this->db->rollback();
// return -1;
// }
return -1;
}
/**
* Load object in memory from the database
*
* @param int $id Id object
* @param string $ref Ref
* @return int Return integer <0 if KO, 0 if not found, >0 if OK
*/
public function fetch($id, $ref = null)
{
$result = $this->fetchCommon($id, $ref);
if ($result > 0) {
if (!empty($this->table_element_line)) {
$this->fetchLines();
}
$res = $this->hasDepreciationLinesInBookkeeping();
if ($res < 0) {
return -1;
} elseif ($res > 0) {
$this->fields['date_acquisition']['noteditable'] = '1';
$this->fields['date_start']['noteditable'] = '1';
$this->fields['acquisition_value_ht']['noteditable'] = '1';
$this->fields['recovered_vat']['noteditable'] = '1';
$this->fields['reversal_date']['noteditable'] = '1';
$this->fields['reversal_amount_ht']['noteditable'] = '1';
}
}
return $result;
}
/**
* Load object lines in memory from the database
*
* @return int Return integer <0 if KO, 0 if not found, >0 if OK
*/
public function fetchLines()
{
$this->lines = array();
return 1;
}
/**
* Load list of objects in memory from the database.
*
* @param string $sortorder Sort Order
* @param string $sortfield Sort field
* @param int $limit limit
* @param int $offset Offset
* @param string $filter Filter as an Universal Search string.
* Example: '((client:=:1) OR ((client:>=:2) AND (client:<=:3))) AND (client:!=:8) AND (nom:like:'a%')'
* @param string $filtermode No more used
* @return array|int int <0 if KO, array of pages if OK
*/
public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
{
dol_syslog(__METHOD__, LOG_DEBUG);
$records = array();
$sql = "SELECT ";
$sql .= $this->getFieldList('t');
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
$sql .= " WHERE t.entity IN (".getEntity($this->element).")";
} else {
$sql .= " WHERE 1 = 1";
}
// Manage filter
$errormessage = '';
$sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
if ($errormessage) {
$this->errors[] = $errormessage;
dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
return -1;
}
if (!empty($sortfield)) {
$sql .= $this->db->order($sortfield, $sortorder);
}
if (!empty($limit)) {
$sql .= $this->db->plimit($limit, $offset);
}
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < ($limit ? min($limit, $num) : $num)) {
$obj = $this->db->fetch_object($resql);
$record = new self($this->db);
$record->setVarsFromFetchObj($obj);
$records[$record->id] = $record;
$i++;
}
$this->db->free($resql);
return $records;
} else {
$this->errors[] = 'Error '.$this->db->lasterror();
dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
return -1;
}
}
/**
* Update object into database
*
* @param User $user User that modifies
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, >0 if OK
*/
public function update(User $user, $notrigger = 0)
{
if (!isset($this->date_start) || $this->date_start === "") {
$this->date_start = $this->date_acquisition;
}
$this->db->begin();
$result = $this->updateCommon($user, $notrigger);
if ($result > 0 && $this->fk_asset_model > 0 && $this->fk_asset_model != $this->oldcopy->fk_asset_model) {
$result = $this->setDataFromAssetModel($user, $notrigger);
}
if ($result > 0 && (
$this->date_start != $this->oldcopy->date_start ||
$this->acquisition_value_ht != $this->oldcopy->acquisition_value_ht ||
$this->reversal_date != $this->oldcopy->reversal_date ||
$this->reversal_amount_ht != $this->oldcopy->reversal_amount_ht ||
($this->fk_asset_model > 0 && $this->fk_asset_model != $this->oldcopy->fk_asset_model)
)
) {
$result = $this->calculationDepreciation();
}
if ($result < 0) {
$this->db->rollback();
} else {
$this->db->commit();
}
return $result;
}
/**
* Delete object in database
*
* @param User $user User that deletes
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, >0 if OK
*/
public function delete(User $user, $notrigger = 0)
{
return $this->deleteCommon($user, $notrigger);
//return $this->deleteCommon($user, $notrigger, 1);
}
/**
* Set asset model
*
* @param User $user User that creates
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, Id of created object if OK
*/
public function setDataFromAssetModel(User $user, $notrigger = 0)
{
global $langs;
$langs->load('assets');
// Clean parameters
$this->id = $this->id > 0 ? $this->id : 0;
$this->fk_asset_model = $this->fk_asset_model > 0 ? $this->fk_asset_model : 0;
// Check parameters
$error = 0;
if (empty($this->id)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Asset") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if (empty($this->fk_asset_model)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("AssetModel") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if ($error) {
return -1;
}
$this->db->begin();
// Get depreciation options
//---------------------------
require_once DOL_DOCUMENT_ROOT . '/asset/class/assetdepreciationoptions.class.php';
$options_model = new AssetDepreciationOptions($this->db);
$result = $options_model->fetchDeprecationOptions(0, $this->fk_asset_model);
if ($result < 0) {
$this->error = $options_model->error;
$this->errors = $options_model->errors;
$error++;
} elseif ($result > 0) {
$options = new AssetDepreciationOptions($this->db);
$result = $options->fetchDeprecationOptions($this->id);
if ($result < 0) {
$this->error = $options->error;
$this->errors = $options->errors;
$error++;
}
if (!$error) {
foreach ($options_model->deprecation_options as $mode_key => $fields) {
foreach ($fields as $field_key => $value) {
$options->deprecation_options[$mode_key][$field_key] = $value;
}
}
$result = $options->updateDeprecationOptions($user, $this->id, 0, $notrigger);
if ($result < 0) {
$this->error = $options->error;
$this->errors = $options->errors;
$error++;
}
}
}
// Get accountancy codes
//---------------------------
if (!$error) {
require_once DOL_DOCUMENT_ROOT . '/asset/class/assetaccountancycodes.class.php';
$accountancy_codes_model = new AssetAccountancyCodes($this->db);
$result = $accountancy_codes_model->fetchAccountancyCodes(0, $this->fk_asset_model);
if ($result < 0) {
$this->error = $accountancy_codes_model->error;
$this->errors = $accountancy_codes_model->errors;
$error++;
} elseif ($result > 0) {
$accountancy_codes = new AssetAccountancyCodes($this->db);
$result = $accountancy_codes->fetchAccountancyCodes($this->id);
if ($result < 0) {
$this->error = $accountancy_codes->error;
$this->errors = $accountancy_codes->errors;
$error++;
}
if (!$error) {
foreach ($accountancy_codes_model->accountancy_codes as $mode_key => $fields) {
foreach ($fields as $field_key => $value) {
$accountancy_codes->accountancy_codes[$mode_key][$field_key] = $value;
}
}
$result = $accountancy_codes->updateAccountancyCodes($user, $this->id, 0, $notrigger);
if ($result < 0) {
$this->error = $accountancy_codes->error;
$this->errors = $accountancy_codes->errors;
$error++;
}
}
}
}
if ($error) {
$this->db->rollback();
return -1;
} else {
$this->db->commit();
return 1;
}
}
/**
* Fetch depreciation lines for each mode in $this->depreciation_lines (sort by depreciation date)
*
* @return int Return integer <0 if KO, Id of created object if OK
*/
public function fetchDepreciationLines()
{
global $langs;
$langs->load('assets');
$this->depreciation_lines = array();
// Clean parameters
$this->id = $this->id > 0 ? $this->id : 0;
// Check parameters
$error = 0;
if (empty($this->id)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Asset") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if ($error) {
return -1;
}
// Old request with 'WITH'
/*
$sql = "WITH in_accounting_bookkeeping(fk_docdet) AS (";
$sql .= " SELECT DISTINCT fk_docdet";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping";
$sql .= " WHERE doc_type = 'asset'";
$sql .= ")";
$sql .= "SELECT ad.rowid, ad.depreciation_mode, ad.ref, ad.depreciation_date, ad.depreciation_ht, ad.cumulative_depreciation_ht";
$sql .= ", " . $this->db->ifsql('iab.fk_docdet IS NOT NULL', 1, 0) . " AS bookkeeping";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN in_accounting_bookkeeping as iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " ORDER BY ad.depreciation_date ASC";
*/
$sql = "SELECT ad.rowid, ad.depreciation_mode, ad.ref, ad.depreciation_date, ad.depreciation_ht, ad.cumulative_depreciation_ht";
$sql .= ", " . $this->db->ifsql('iab.fk_docdet IS NOT NULL', 1, 0) . " AS bookkeeping";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN (SELECT DISTINCT fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE doc_type = 'asset') AS iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " ORDER BY ad.depreciation_date ASC";
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorFetchDepreciationLines') . ': ' . $this->db->lasterror();
return -1;
}
while ($obj = $this->db->fetch_object($resql)) {
if (!isset($this->depreciation_lines[$obj->depreciation_mode])) {
$this->depreciation_lines[$obj->depreciation_mode] = array();
}
$this->depreciation_lines[$obj->depreciation_mode][] = array(
'id' => $obj->rowid,
'ref' => $obj->ref,
'depreciation_date' => $this->db->jdate($obj->depreciation_date),
'depreciation_ht' => $obj->depreciation_ht,
'cumulative_depreciation_ht' => $obj->cumulative_depreciation_ht,
'bookkeeping' => $obj->bookkeeping,
);
}
return 1;
}
/**
* If has depreciation lines in bookkeeping
*
* @return int Return integer <0 if KO, 0 if NO, 1 if Yes
*/
public function hasDepreciationLinesInBookkeeping()
{
global $langs;
$langs->load('assets');
// Clean parameters
$this->id = $this->id > 0 ? $this->id : 0;
// Check parameters
$error = 0;
if (empty($this->id)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Asset") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if ($error) {
return -1;
}
// Old request with 'WITH'
/*
$sql = "WITH in_accounting_bookkeeping(fk_docdet) AS (";
$sql .= " SELECT DISTINCT fk_docdet";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping";
$sql .= " WHERE doc_type = 'asset'";
$sql .= ")";
$sql .= "SELECT COUNT(*) AS has_bookkeeping";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN in_accounting_bookkeeping as iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " AND iab.fk_docdet IS NOT NULL";
*/
$sql = "SELECT COUNT(*) AS has_bookkeeping";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN (SELECT DISTINCT fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE doc_type = 'asset') AS iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " AND iab.fk_docdet IS NOT NULL";
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorFetchDepreciationLines') . ': ' . $this->db->lasterror();
return -1;
}
if ($obj = $this->db->fetch_object($resql)) {
return $obj->has_bookkeeping > 0 ? 1 : 0;
}
return 0;
}
/**
* Add depreciation line for a mode
*
* @param string $mode Depreciation mode (economic, accelerated_depreciation, ...)
* @param string $ref Ref line
* @param int $depreciation_date Depreciation date
* @param double $depreciation_ht Depreciation amount HT
* @param double $cumulative_depreciation_ht Depreciation cumulative amount HT
* @param string $accountancy_code_debit Accountancy code Debit
* @param string $accountancy_code_credit Accountancy code Credit
* @return int Return integer <0 if KO, Id of created line if OK
*/
public function addDepreciationLine($mode, $ref, $depreciation_date, $depreciation_ht, $cumulative_depreciation_ht, $accountancy_code_debit, $accountancy_code_credit)
{
global $langs;
$langs->load('assets');
// Clean parameters
$this->id = $this->id > 0 ? $this->id : 0;
$mode = strtolower(trim($mode));
$ref = trim($ref);
$accountancy_code_debit = trim($accountancy_code_debit);
$accountancy_code_credit = trim($accountancy_code_credit);
// Check parameters
$error = 0;
if (empty($this->id)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Asset") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if ($error) {
return -1;
}
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "asset_depreciation(fk_asset, depreciation_mode, ref, depreciation_date, depreciation_ht, cumulative_depreciation_ht, accountancy_code_debit, accountancy_code_credit)";
$sql .= " VALUES ( ";
$sql .= " " . (int) $this->id;
$sql .= ", '" . $this->db->escape($mode) . "'";
$sql .= ", '" . $this->db->escape($ref) . "'";
$sql .= ", '" . $this->db->idate($depreciation_date) . "'";
$sql .= ", " . (float) $depreciation_ht;
$sql .= ", " . (float) $cumulative_depreciation_ht;
$sql .= ", '" . $this->db->escape($accountancy_code_debit) . "'";
$sql .= ", '" . $this->db->escape($accountancy_code_credit) . "'";
$sql .= ")";
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorAddDepreciationLine') . ': ' . $this->db->lasterror();
return -1;
}
return 1;
}
/**
* Calculation depreciation lines (reversal and future) for each mode
*
* @return int Return integer <0 if KO, Id of created object if OK
*/
public function calculationDepreciation()
{
global $conf, $langs;
$langs->load('assets');
// Clean parameters
$this->id = $this->id > 0 ? $this->id : 0;
// Check parameters
$error = 0;
if (empty($this->id)) {
$this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Asset") . ' (' . $langs->transnoentitiesnoconv("TechnicalID") . ')');
$error++;
}
if ($error) {
return -1;
}
// Get depreciation options
//---------------------------
require_once DOL_DOCUMENT_ROOT . '/asset/class/assetdepreciationoptions.class.php';
$options = new AssetDepreciationOptions($this->db);
$result = $options->fetchDeprecationOptions($this->id);
if ($result < 0) {
$this->error = $options->error;
$this->errors = $options->errors;
return -1;
}
// Get accountancy codes
//---------------------------
require_once DOL_DOCUMENT_ROOT . '/asset/class/assetaccountancycodes.class.php';
$accountancy_codes = new AssetAccountancyCodes($this->db);
$result = $accountancy_codes->fetchAccountancyCodes($this->id);
if ($result < 0) {
$this->error = $accountancy_codes->error;
$this->errors = $accountancy_codes->errors;
return -1;
}
$this->db->begin();
// Delete old lines
$modes = array();
foreach ($options->deprecation_options as $mode_key => $fields) {
$modes[$mode_key] = $this->db->escape($mode_key);
}
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "asset_depreciation";
$sql .= " WHERE fk_asset = " . (int) $this->id;
$sql .= " AND depreciation_mode NOT IN ('" . $this->db->sanitize(implode("', '", $modes)) . "')";
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorClearDepreciationLines') . ': ' . $this->db->lasterror();
$error++;
}
if (!$error) {
// Get fiscal period
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
$dates = getDefaultDatesForTransfer();
$init_fiscal_period_start = $dates['date_start'];
$init_fiscal_period_end = $dates['date_end'];
if (empty($init_fiscal_period_start) || empty($init_fiscal_period_end)) {
$pastmonthyear = $dates['pastmonthyear'];
$pastmonth = $dates['pastmonth'];
$init_fiscal_period_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
$init_fiscal_period_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
}
foreach ($options->deprecation_options as $mode_key => $fields) {
// Get last depreciation lines save in bookkeeping
//-----------------------------------------------------
// Old request with 'WITH'
/*
$sql = "WITH in_accounting_bookkeeping(fk_docdet) AS (";
$sql .= " SELECT fk_docdet";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping";
$sql .= " WHERE doc_type = 'asset'";
$sql .= ")";
$sql .= "SELECT ad.depreciation_date, ad.cumulative_depreciation_ht";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN in_accounting_bookkeeping as iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " AND ad.depreciation_mode = '" . $this->db->escape($mode_key) . "'";
$sql .= " AND iab.fk_docdet IS NOT NULL";
$sql .= " ORDER BY ad.depreciation_date DESC";
$sql .= " LIMIT 1";
*/
$sql = "SELECT ad.depreciation_date, ad.cumulative_depreciation_ht";
$sql .= " FROM " . MAIN_DB_PREFIX . "asset_depreciation AS ad";
$sql .= " LEFT JOIN (SELECT DISTINCT fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE doc_type = 'asset') AS iab ON iab.fk_docdet = ad.rowid";
$sql .= " WHERE ad.fk_asset = " . (int) $this->id;
$sql .= " AND ad.depreciation_mode = '" . $this->db->escape($mode_key) . "'";
$sql .= " AND iab.fk_docdet IS NOT NULL";
$sql .= " ORDER BY ad.depreciation_date DESC";
$sql .= " LIMIT 1";
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorFetchMaxDepreciationDateForMode', $mode_key) . ': ' . $this->db->lasterror();
$error++;
break;
}
$last_depreciation_date = '';
$last_cumulative_depreciation_ht = $this->reversal_amount_ht;
if ($obj = $this->db->fetch_object($resql)) {
$last_depreciation_date = $this->db->jdate($obj->depreciation_date);
$last_cumulative_depreciation_ht = $obj->cumulative_depreciation_ht;
}
// Set last cumulative depreciation
$sql = "UPDATE " . MAIN_DB_PREFIX . $options->deprecation_options_fields[$mode_key]['table'];
$sql .= " SET total_amount_last_depreciation_ht = " . (empty($last_cumulative_depreciation_ht) ? 0 : $last_cumulative_depreciation_ht);
$sql .= " WHERE fk_asset = " . (int) $this->id;
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorSetLastCumulativeDepreciation') . ': ' . $this->db->lasterror();
$error++;
break;
}
// Delete old lines
$sql = "DELETE " . MAIN_DB_PREFIX . "asset_depreciation FROM " . MAIN_DB_PREFIX . "asset_depreciation";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab ON ab.doc_type = 'asset' AND ab.fk_docdet = " . MAIN_DB_PREFIX . "asset_depreciation.rowid";
$sql .= " WHERE " . MAIN_DB_PREFIX . "asset_depreciation.fk_asset = " . (int) $this->id;
$sql .= " AND " . MAIN_DB_PREFIX . "asset_depreciation.depreciation_mode = '" . $this->db->escape($mode_key) . "'";
$sql .= " AND ab.fk_docdet IS NULL";
if ($last_depreciation_date !== "") {
$sql .= " AND " . MAIN_DB_PREFIX . "asset_depreciation.ref != ''";
}
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $langs->trans('AssetErrorClearDepreciationLines') . ': ' . $this->db->lasterror();
$error++;
break;
}
// Get depreciation period
$depreciation_date_start = $this->date_start > $this->date_acquisition ? $this->date_start : $this->date_acquisition;
$depreciation_date_end = dol_time_plus_duree($depreciation_date_start, $fields['duration'], $fields['duration_type'] == 1 ? 'm' : ($fields['duration_type'] == 2 ? 'd' : 'y'));
$depreciation_amount = $fields['amount_base_depreciation_ht'];
if ($fields['duration_type'] == 2) { // Daily
$fiscal_period_start = $depreciation_date_start;
$fiscal_period_end = $depreciation_date_start;
} elseif ($fields['duration_type'] == 1) { // Monthly
$date_temp = dol_getdate($depreciation_date_start);
$fiscal_period_start = dol_get_first_day($date_temp['year'], $date_temp['mon'], false);
$fiscal_period_end = dol_get_last_day($date_temp['year'], $date_temp['mon'], false);
} else { // Annually
$fiscal_period_start = $init_fiscal_period_start;
$fiscal_period_end = $init_fiscal_period_end;
}
$cumulative_depreciation_ht = $last_cumulative_depreciation_ht;
$depreciation_period_amount = $depreciation_amount - $this->reversal_amount_ht;
$start_date = $depreciation_date_start;
$disposal_date = isset($this->disposal_date) && $this->disposal_date !== "" ? $this->disposal_date : "";
$finish_date = $disposal_date !== "" ? $disposal_date : $depreciation_date_end;
$accountancy_code_depreciation_debit_key = $accountancy_codes->accountancy_codes_fields[$mode_key]['depreciation_debit'];
$accountancy_code_depreciation_debit = $accountancy_codes->accountancy_codes[$mode_key][$accountancy_code_depreciation_debit_key];
$accountancy_code_depreciation_credit_key = $accountancy_codes->accountancy_codes_fields[$mode_key]['depreciation_credit'];
$accountancy_code_credit = $accountancy_codes->accountancy_codes[$mode_key][$accountancy_code_depreciation_credit_key];
// Reversal depreciation line
//-----------------------------------------------------
if ($last_depreciation_date === "" && ($depreciation_date_start < $fiscal_period_start || is_numeric($this->reversal_date))) {
if (is_numeric($this->reversal_date)) {
if ($this->reversal_date < $fiscal_period_start) {
$this->errors[] = $langs->trans('AssetErrorReversalDateNotGreaterThanCurrentBeginFiscalDateForMode', $mode_key);
$error++;
break;
}
if (empty($this->reversal_amount_ht)) {
$this->errors[] = $langs->trans('AssetErrorReversalAmountNotProvidedForMode', $mode_key);
$error++;
break;
}
$start_date = $this->reversal_date;
$result = $this->addDepreciationLine($mode_key, '', $start_date, $this->reversal_amount_ht, $this->reversal_amount_ht, $accountancy_code_depreciation_debit, $accountancy_code_credit);
if ($result < 0) {
$error++;
break;
}
} else {
$this->errors[] = $langs->trans('AssetErrorReversalDateNotProvidedForMode', $mode_key);
$error++;
break;
}
}
// futures depreciation lines
//-----------------------------------------------------
$nb_days_in_year = getDolGlobalInt('ASSET_DEPRECIATION_DURATION_PER_YEAR', 365);
$nb_days_in_month = getDolGlobalInt('ASSET_DEPRECIATION_DURATION_PER_MONTH', 30);
$period_amount = (float) price2num($depreciation_period_amount / $fields['duration'], 'MT');
$first_period_found = false;
// TODO fix declaration of $begin_period