@@ -66,6 +66,7 @@ def get_default_move_ids(self):
66
66
management_type = fields .Selection (
67
67
[
68
68
("create" , "Create New" ),
69
+ ("partial_recharge" , "Partial Recharge" ),
69
70
("update" , "Update Existing" ),
70
71
("partial_dismiss" , "Partial Dismiss" ),
71
72
("dismiss" , "Dismiss Asset" ),
@@ -104,6 +105,12 @@ def get_default_move_ids(self):
104
105
105
106
used = fields .Boolean ()
106
107
108
+ recharge_date = fields .Date (
109
+ default = fields .Date .today (),
110
+ )
111
+ recharge_purchase_amount = fields .Monetary ()
112
+ recharge_fund_amount = fields .Monetary ()
113
+
107
114
# Mapping between move journal type and depreciation line type
108
115
_move_journal_type_2_dep_line_type = {
109
116
"purchase" : "in" ,
@@ -117,6 +124,7 @@ def get_default_move_ids(self):
117
124
# Every method used in here must return an asset
118
125
_management_type_2_method = {
119
126
"create" : lambda w : w .create_asset (),
127
+ "partial_recharge" : lambda w : w .partial_recharge_asset (),
120
128
"dismiss" : lambda w : w .dismiss_asset (),
121
129
"partial_dismiss" : lambda w : w .partial_dismiss_asset (),
122
130
"update" : lambda w : w .update_asset (),
@@ -735,3 +743,134 @@ def update_asset(self):
735
743
self .check_pre_update_asset ()
736
744
self .asset_id .write (self .get_update_asset_vals ())
737
745
return self .asset_id
746
+
747
+ def check_pre_partial_recharge_asset (self ):
748
+ self .ensure_one ()
749
+ asset = self .asset_id
750
+ if not asset :
751
+ raise ValidationError (_ ("Please choose an asset before continuing!" ))
752
+
753
+ move_lines = self .move_line_ids
754
+ if not move_lines :
755
+ raise ValidationError (
756
+ _ (
757
+ "At least one move line is needed"
758
+ " to partial recharge asset %(asset)s!" ,
759
+ asset = asset ,
760
+ )
761
+ )
762
+
763
+ asset_account = asset .category_id .asset_account_id
764
+ if not all (line .account_id == asset_account for line in move_lines ):
765
+ raise ValidationError (
766
+ _ (
767
+ "You need to choose move lines with account `%(ass_acc)s`"
768
+ " if you need them to partial recharge asset `%(ass_name)s`!" ,
769
+ ass_acc = asset_account .display_name ,
770
+ ass_name = asset .display_name ,
771
+ )
772
+ )
773
+
774
+ def get_partial_recharge_asset_vals (self ):
775
+ self .ensure_one ()
776
+ asset = self .asset_id
777
+ currency = self .asset_id .currency_id
778
+ recharge_date = self .recharge_date
779
+ digits = self .env ["decimal.precision" ].precision_get ("Account" )
780
+ fund_amt = self .recharge_fund_amount
781
+ purchase_amt = self .recharge_purchase_amount
782
+
783
+ move = self .move_line_ids .mapped ("move_id" )
784
+ move_nums = move .name
785
+
786
+ writeoff = 0
787
+ for line in self .move_line_ids :
788
+ writeoff += line .currency_id ._convert (
789
+ line .credit - line .debit , currency , line .company_id , line .date
790
+ )
791
+ writeoff = round (writeoff , digits )
792
+
793
+ vals = {"depreciation_ids" : []}
794
+ for dep in asset .depreciation_ids :
795
+ dep_writeoff = writeoff
796
+ if dep .pro_rata_temporis :
797
+ dep_writeoff *= dep .get_pro_rata_temporis_multiplier (
798
+ recharge_date , "std"
799
+ )
800
+
801
+ name = _ (
802
+ "Partial recharge from move(s) %(move_nums)s" ,
803
+ move_nums = move_nums ,
804
+ )
805
+
806
+ out_line_vals = {
807
+ "asset_accounting_info_ids" : [
808
+ Command .create (
809
+ {
810
+ "move_line_id" : line .id ,
811
+ "relation_type" : self .management_type ,
812
+ },
813
+ )
814
+ for line in self .move_line_ids
815
+ ],
816
+ "amount" : purchase_amt ,
817
+ "date" : recharge_date ,
818
+ "move_type" : "in" ,
819
+ "name" : name ,
820
+ }
821
+ dep_line_vals = {
822
+ "asset_accounting_info_ids" : [
823
+ Command .create (
824
+ {
825
+ "move_line_id" : line .id ,
826
+ "relation_type" : self .management_type ,
827
+ },
828
+ )
829
+ for line in self .move_line_ids
830
+ ],
831
+ "amount" : - fund_amt ,
832
+ "date" : recharge_date ,
833
+ "move_type" : "depreciated" ,
834
+ "name" : name ,
835
+ }
836
+
837
+ dep_vals = {
838
+ "line_ids" : [
839
+ Command .create (out_line_vals ),
840
+ Command .create (dep_line_vals ),
841
+ ]
842
+ }
843
+
844
+ balance = purchase_amt + dep_writeoff - fund_amt
845
+ if not float_is_zero (balance , digits ):
846
+ loss_gain_vals = {
847
+ "asset_accounting_info_ids" : [
848
+ Command .create (
849
+ {
850
+ "move_line_id" : line .id ,
851
+ "relation_type" : self .management_type ,
852
+ },
853
+ )
854
+ for line in self .move_line_ids
855
+ ],
856
+ "amount" : abs (balance ),
857
+ "date" : recharge_date ,
858
+ "move_type" : "gain" if balance > 0 else "loss" ,
859
+ "name" : name ,
860
+ }
861
+ dep_vals ["line_ids" ].append (Command .create (loss_gain_vals ))
862
+
863
+ vals ["depreciation_ids" ].append (Command .update (dep .id , dep_vals ))
864
+ return vals
865
+
866
+ def partial_recharge_asset (self ):
867
+ """Recharge asset partially and return it."""
868
+ self .ensure_one ()
869
+ self .check_pre_partial_recharge_asset ()
870
+ old_dep_lines = self .asset_id .mapped ("depreciation_ids.line_ids" )
871
+ self .asset_id .write (self .get_partial_recharge_asset_vals ())
872
+
873
+ for dep in self .asset_id .depreciation_ids :
874
+ (dep .line_ids - old_dep_lines ).post_partial_dismiss_asset ()
875
+
876
+ return self .asset_id
0 commit comments