-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
intrastat_product_declaration.py
1277 lines (1173 loc) · 48.7 KB
/
intrastat_product_declaration.py
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
# Copyright 2011-2020 Akretion France (http://www.akretion.com)
# Copyright 2009-2022 Noviat (http://www.noviat.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# @author Luc de Meyer <info@noviat.com>
import logging
import warnings
from datetime import date
from dateutil.relativedelta import relativedelta
from stdnum.vatin import is_valid
from odoo import _, api, fields, models
from odoo.exceptions import RedirectWarning, UserError, ValidationError
from odoo.tools import float_is_zero
_logger = logging.getLogger(__name__)
class IntrastatProductDeclaration(models.Model):
_name = "intrastat.product.declaration"
_description = "Intrastat Product Report Base Object"
_rec_name = "year_month"
_inherit = ["mail.thread", "mail.activity.mixin"]
_order = "year_month desc, declaration_type, revision"
_sql_constraints = [
(
"date_uniq",
"unique(year_month, company_id, declaration_type, revision)",
"A declaration of the same type already exists for this month !"
"\nYou should update the existing declaration "
"or change the revision number of this one.",
)
]
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
decl_date = fields.Date.context_today(self) - relativedelta(months=1)
res.update(
{"year": str(decl_date.year), "month": str(decl_date.month).zfill(2)}
)
return res
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
required=True,
states={"done": [("readonly", True)]},
default=lambda self: self.env.company,
)
company_country_code = fields.Char(
compute="_compute_company_country_code",
string="Company Country Code",
readonly=True,
store=True,
)
state = fields.Selection(
selection=[("draft", "Draft"), ("done", "Done")],
string="State",
readonly=True,
tracking=True,
copy=False,
default="draft",
help="State of the declaration. When the state is set to 'Done', "
"the parameters become read-only.",
)
note = fields.Text(
string="Notes", help="You can add some comments here if you want."
)
year = fields.Char(
string="Year", required=True, states={"done": [("readonly", True)]}
)
month = fields.Selection(
selection=[
("01", "01"),
("02", "02"),
("03", "03"),
("04", "04"),
("05", "05"),
("06", "06"),
("07", "07"),
("08", "08"),
("09", "09"),
("10", "10"),
("11", "11"),
("12", "12"),
],
string="Month",
required=True,
states={"done": [("readonly", True)]},
)
year_month = fields.Char(
compute="_compute_year_month",
string="Period",
readonly=True,
tracking=True,
store=True,
help="Year and month of the declaration.",
)
declaration_type = fields.Selection(
selection="_get_declaration_type",
string="Type",
required=True,
states={"done": [("readonly", True)]},
tracking=True,
)
action = fields.Selection(
selection="_get_action",
string="Action",
required=True,
default="replace",
states={"done": [("readonly", True)]},
tracking=True,
)
revision = fields.Integer(
string="Revision",
default=1,
states={"done": [("readonly", True)]},
help="Used to keep track of changes",
)
computation_line_ids = fields.One2many(
comodel_name="intrastat.product.computation.line",
inverse_name="parent_id",
string="Intrastat Product Computation Lines",
states={"done": [("readonly", True)]},
)
declaration_line_ids = fields.One2many(
comodel_name="intrastat.product.declaration.line",
inverse_name="parent_id",
string="Intrastat Product Declaration Lines",
states={"done": [("readonly", True)]},
)
num_decl_lines = fields.Integer(
compute="_compute_numbers",
string="Number of Declaration Lines",
store=True,
tracking=True,
)
total_amount = fields.Integer(
compute="_compute_numbers",
string="Total Fiscal Amount",
store=True,
help="Total fiscal amount in company currency of the declaration.",
)
currency_id = fields.Many2one(
"res.currency", related="company_id.currency_id", string="Currency"
)
reporting_level = fields.Selection(
selection="_get_reporting_level",
string="Reporting Level",
states={"done": [("readonly", True)]},
)
valid = fields.Boolean(compute="_compute_check_validity", string="Valid")
xml_attachment_id = fields.Many2one("ir.attachment", string="XML Attachment")
xml_attachment_datas = fields.Binary(
related="xml_attachment_id.datas", string="XML Export"
)
xml_attachment_name = fields.Char(
related="xml_attachment_id.name", string="XML Filename"
)
@api.model
def _get_declaration_type(self):
res = []
company = self.env.company
arrivals = company.intrastat_arrivals
dispatches = company.intrastat_dispatches
if arrivals != "exempt":
res.append(("arrivals", _("Declaration for Arrivals")))
if dispatches != "exempt":
res.append(("dispatches", _("Declaration for Dispatches")))
return res
@api.model
def _get_reporting_level(self):
return [("standard", _("Standard")), ("extended", _("Extended"))]
@api.model
def _get_action(self):
return [
("replace", _("Replace")),
("append", _("Append")),
("nihil", _("Nihil")),
]
@api.depends("company_id")
def _compute_company_country_code(self):
for this in self:
if this.company_id:
if not this.company_id.country_id:
raise ValidationError(_("You must set company's country !"))
this.company_country_code = this.company_id.country_id.code.lower()
@api.depends("year", "month")
def _compute_year_month(self):
for this in self:
if this.year and this.month:
this.year_month = "-".join([this.year, this.month])
@api.depends("month")
def _compute_check_validity(self):
"""TO DO: logic based upon computation lines"""
for this in self:
this.valid = True
@api.constrains("year")
def _check_year(self):
for this in self:
if len(this.year) != 4 or this.year[0] != "2":
raise ValidationError(_("Invalid Year!"))
@api.onchange("declaration_type")
def _onchange_declaration_type(self):
if self.declaration_type == "arrivals":
self.reporting_level = (
self.company_id.intrastat_arrivals == "extended"
and "extended"
or "standard"
)
if self.declaration_type == "dispatches":
self.reporting_level = (
self.company_id.intrastat_dispatches == "extended"
and "extended"
or "standard"
)
def copy(self, default=None):
self.ensure_one()
default = default or {}
default["revision"] = self.revision + 1
return super().copy(default)
def _account_config_warning(self, msg):
action = self.env.ref("account.action_account_config")
raise RedirectWarning(
msg, action.id, _("Go to Accounting Configuration Settings screen")
)
def _attach_xml_file(self, xml_bytes, declaration_name):
"""Attach the XML file to the report_intrastat_product/service
object"""
self.ensure_one()
filename = "{}_{}.xml".format(self.year_month, declaration_name)
attach = self.env["ir.attachment"].create(
{
"name": filename,
"res_id": self.id,
"res_model": self._name,
"raw": xml_bytes,
}
)
return attach.id
def _unlink_attachments(self):
atts = self.env["ir.attachment"].search(
[("res_model", "=", self._name), ("res_id", "=", self.id)]
)
atts.unlink()
def unlink(self):
for this in self:
if this.state == "done":
raise UserError(
_("Cannot delete the declaration %s because it is in Done state.")
% this.display_name
)
return super().unlink()
def _get_partner_country(self, inv_line, notedict, eu_countries):
inv = inv_line.move_id
country = inv.src_dest_country_id or inv.partner_id.country_id
if not country:
line_notes = [
_(
"Missing country on invoice partner '%s' "
"or on the delivery address (partner '%s'). "
)
% (
inv.partner_id.display_name,
inv.partner_shipping_id
and inv.partner_shipping_id.display_name
or "-",
)
]
self._format_line_note(inv_line, notedict, line_notes)
else:
if country not in eu_countries and country.code != "GB":
line_notes = [
_(
"On invoice '%s', the source/destination country "
"is '%s' which is not part of the European Union."
)
% (inv.name, country.name)
]
self._format_line_note(inv_line, notedict, line_notes)
if country and country.code == "GB" and self.year >= "2021":
vat = inv.commercial_partner_id.vat
if not vat:
line_notes = [
_(
"On invoice '%s', the source/destination country "
"is United-Kingdom and the fiscal position is '%s'. "
"Make sure that the fiscal position is right. If "
"the origin/destination is Northern Ireland, please "
"set the VAT number of the partner '%s' in Odoo with "
"its new VAT number starting with 'XI' following Brexit."
)
% (
inv.name,
inv.fiscal_position_id.display_name,
inv.commercial_partner_id.display_name,
)
]
self._format_line_note(inv_line, notedict, line_notes)
elif not vat.startswith("XI"):
line_notes = [
_(
"On invoice '%s', the source/destination country "
"is United-Kingdom, the fiscal position is '%s' and "
"the partner's VAT number is '%s'. "
"Make sure that the fiscal position is right. If "
"the origin/destination is Northern Ireland, please "
"update the VAT number of the partner '%s' in Odoo with "
"its new VAT number starting with 'XI' following Brexit."
)
% (
inv.name,
inv.fiscal_position_id.display_name,
vat,
inv.commercial_partner_id.display_name,
)
]
self._format_line_note(inv_line, notedict, line_notes)
return country
def _get_intrastat_transaction(self, inv_line, notedict):
invoice = inv_line.move_id
if invoice.intrastat_transaction_id:
return invoice.intrastat_transaction_id
else:
company = invoice.company_id
if invoice.move_type == "out_invoice":
return company.intrastat_transaction_out_invoice
elif invoice.move_type == "out_refund":
return company.intrastat_transaction_out_refund
elif invoice.move_type == "in_invoice":
return company.intrastat_transaction_in_invoice
elif invoice.move_type == "in_refund":
return company.intrastat_transaction_in_refund
def _get_weight_and_supplunits(self, inv_line, hs_code, notedict):
line_qty = inv_line.quantity
product = inv_line.product_id
intrastat_unit_id = hs_code.intrastat_unit_id
source_uom = inv_line.product_uom_id
weight_uom_categ = self._get_uom_refs("weight_uom_categ")
kg_uom = self._get_uom_refs("kg_uom")
pce_uom_categ = self._get_uom_refs("pce_uom_categ")
pce_uom = self._get_uom_refs("pce_uom")
weight = suppl_unit_qty = 0.0
if not source_uom:
line_notes = [_("Missing unit of measure.")]
self._format_line_note(inv_line, notedict, line_notes)
return weight, suppl_unit_qty
if intrastat_unit_id:
target_uom = intrastat_unit_id.uom_id
if not target_uom:
line_notes = [
_("Intrastat Code %s:") % hs_code.display_name,
_(
"Conversion from Intrastat Supplementary Unit '%s' to "
"Unit of Measure is not implemented yet."
)
% intrastat_unit_id.name,
]
self._format_line_note(inv_line, notedict, line_notes)
return weight, suppl_unit_qty
if target_uom.category_id == source_uom.category_id:
suppl_unit_qty = source_uom._compute_quantity(line_qty, target_uom)
else:
line_notes = [
_(
"Conversion from unit of measure '%s' to '%s' "
"is not implemented yet."
)
% (source_uom.name, target_uom.name)
]
self._format_line_note(inv_line, notedict, line_notes)
return weight, suppl_unit_qty
if weight:
return weight, suppl_unit_qty
if source_uom == kg_uom:
weight = line_qty
elif source_uom.category_id == weight_uom_categ:
weight = source_uom._compute_quantity(line_qty, kg_uom)
elif source_uom.category_id == pce_uom_categ:
if not product.weight: # re-create weight_net ?
line_notes = [_("Missing weight on product %s.") % product.display_name]
self._format_line_note(inv_line, notedict, line_notes)
return weight, suppl_unit_qty
if source_uom == pce_uom:
weight = product.weight * line_qty # product.weight_net
else:
# Here, I suppose that, on the product, the
# weight is per PCE and not per uom_id
# product.weight_net
weight = product.weight * source_uom._compute_quantity(
line_qty, pce_uom
)
else:
line_notes = [
_(
"Conversion from unit of measure '%s' to 'Kg' "
"is not implemented yet. It is needed for product '%s'."
)
% (source_uom.name, product.display_name)
]
self._format_line_note(inv_line, notedict, line_notes)
return weight, suppl_unit_qty
return weight, suppl_unit_qty
def _get_region(self, inv_line, notedict):
"""
For supplier invoices/refunds: if the invoice line is linked
to a stock move, use the destination stock location ;
otherwise, get the PO (which is linked to a stock location)
and then get the warehouse.
It is important to take into account the following scenario:
I order a qty of 50 kg and my suppliers delivers and invoices 52 kg
(quite common in some industries where the order qty cannot be exact
due to some operational constraints) ; in this case, I have a qty of
2 kg which is not linked to a PO, but it is linked to a stock move.
For customer invoices/refunds: if the invoice line is linked to a
stock move, use the source stock location ;
otherwise, get the sale order, which is linked to the warehouse.
If none found, get the company's default intrastat region.
"""
region = False
move_type = inv_line.move_id.move_type
if move_type in ("in_invoice", "in_refund"):
po_line = self.env["purchase.order.line"].search(
[("invoice_lines", "in", inv_line.id)], limit=1
)
if po_line:
if po_line.move_ids:
region = po_line.move_ids[0].location_dest_id.get_intrastat_region()
elif move_type in ("out_invoice", "out_refund"):
so_line = self.env["sale.order.line"].search(
[("invoice_lines", "in", inv_line.id)], limit=1
)
if so_line:
so = so_line.order_id
region = so.warehouse_id.region_id
if not region:
if self.company_id.intrastat_region_id:
region = self.company_id.intrastat_region_id
return region
def _get_transport(self, inv_line, notedict):
transport = (
inv_line.move_id.intrastat_transport_id
or self.company_id.intrastat_transport_id
)
if not transport:
msg = _(
"The default Intrastat Transport Mode "
"of the Company is not set, "
"please configure it first."
)
self._account_config_warning(msg)
return transport
def _get_incoterm(self, inv_line, notedict):
incoterm = inv_line.move_id.invoice_incoterm_id or self.company_id.incoterm_id
if not incoterm:
msg = _(
"The default Incoterm "
"of the Company is not set, "
"please configure it first."
)
self._account_config_warning(msg)
return incoterm
def _get_product_origin_country(self, inv_line, notedict):
warnings.warn(
"Method '_get_product_origin_country' is deprecated, "
"please use '_get_product_origin_country_code'.",
DeprecationWarning,
)
return inv_line.product_id.origin_country_id
def _get_product_origin_country_code(
self, inv_line, product_origin_country, notedict
):
cc = "QU"
if product_origin_country.code:
cc = product_origin_country.code
year = self.year or str(inv_line.move_id.date.year)
if year >= "2021":
product_origin_state = getattr(
inv_line.product_id,
"origin_state_id",
self.env["res.country.state"],
)
cc = self.env["res.partner"]._get_intrastat_country_code(
product_origin_country, product_origin_state
)
return cc
def _get_vat(self, inv_line, notedict):
vat = False
inv = inv_line.move_id
if self.declaration_type == "dispatches":
vat = inv.commercial_partner_id.vat
if vat:
if vat.startswith("GB"):
line_notes = [
_(
"VAT number of partner '%s' is '%s'. If this partner "
"is from Northern Ireland, his VAT number should be "
"updated to his new VAT number starting with 'XI' "
"following Brexit. If this partner is from Great Britain, "
"maybe the fiscal position was wrong on invoice '%s' "
"(the fiscal position was '%s')."
)
% (
inv.commercial_partner_id.display_name,
vat,
inv.name,
inv.fiscal_position_id.display_name,
)
]
self._format_line_note(inv_line, notedict, line_notes)
else:
line_notes = [
_("Missing VAT Number on partner '%s'")
% inv.commercial_partner_id.display_name
]
self._format_line_note(inv_line, notedict, line_notes)
return vat
def _update_computation_line_vals(self, inv_line, line_vals, notedict):
"""placeholder for localization modules"""
def _handle_invoice_accessory_cost(
self,
invoice,
lines_current_invoice,
total_inv_accessory_costs_cc,
total_inv_product_cc,
total_inv_weight,
):
"""
Affect accessory costs pro-rata of the value
(or pro-rata of the weight if the goods of the invoice
have no value)
This method allows to implement a different logic
in the localization modules.
E.g. in Belgium accessory cost should not be added.
"""
if total_inv_accessory_costs_cc:
if total_inv_product_cc:
# pro-rata of the value
for ac_line_vals in lines_current_invoice:
ac_line_vals["amount_accessory_cost_company_currency"] = (
total_inv_accessory_costs_cc
* ac_line_vals["amount_company_currency"]
/ total_inv_product_cc
)
elif total_inv_weight:
# pro-rata of the weight
for ac_line_vals in lines_current_invoice:
ac_line_vals["amount_accessory_cost_company_currency"] = (
total_inv_accessory_costs_cc
* ac_line_vals["weight"]
/ total_inv_weight
)
else:
for ac_line_vals in lines_current_invoice:
ac_line_vals[
"amount_accessory_cost_company_currency"
] = total_inv_accessory_costs_cc / len(lines_current_invoice)
def _prepare_invoice_domain(self):
"""
Complete this method in the localization module
with the country-specific logic for arrivals and dispatches.
Cf. l10n_be_intrastat_product_declaration for an example
The dates are based on account.move,date in stead of invoice_date
to ensure consistency between intrastat and intracomm tax declaration.
"""
start_date = date(int(self.year), int(self.month), 1)
end_date = start_date + relativedelta(day=1, months=+1, days=-1)
domain = [
("date", ">=", start_date),
("date", "<=", end_date),
("state", "=", "posted"),
("intrastat_fiscal_position", "=", True),
("company_id", "=", self.company_id.id),
(
"move_type",
"in",
("out_invoice", "out_refund", "in_invoice", "in_refund"),
),
]
return domain
def _is_product(self, invoice_line):
if invoice_line.product_id and invoice_line.product_id.type in (
"product",
"consu",
):
return True
else:
return False
def _gather_invoices_init(self, notedict):
"""placeholder for localization modules"""
def _format_line_note(self, line, notedict, line_notes):
indent = 8 * " "
note = _("Invoice %s, line %s") % (line.move_id.name, notedict["line_nbr"])
note += ":\n"
for line_note in line_notes:
note += indent + line_note
note += "\n"
notedict["note"] += note
def _gather_invoices(self, notedict):
lines = []
qty_prec = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
accessory_costs = self.company_id.intrastat_accessory_costs
eu_countries = self.env.ref("base.europe").country_ids
self._gather_invoices_init(notedict)
domain = self._prepare_invoice_domain()
order = "journal_id, name"
invoices = self.env["account.move"].search(domain, order=order)
for invoice in invoices:
lines_current_invoice = []
total_inv_accessory_costs_cc = 0.0 # in company currency
total_inv_product_cc = 0.0 # in company currency
total_inv_weight = 0.0
for line_nbr, inv_line in enumerate(
invoice.invoice_line_ids.filtered(lambda x: not x.display_type), start=1
):
notedict["line_nbr"] = line_nbr
inv_intrastat_line = invoice.intrastat_line_ids.filtered(
lambda r: r.invoice_line_id == inv_line
)
if (
accessory_costs
and inv_line.product_id
and inv_line.product_id.is_accessory_cost
):
acost = invoice.currency_id._convert(
inv_line.price_subtotal,
self.company_id.currency_id,
self.company_id,
invoice.date,
)
total_inv_accessory_costs_cc += acost
continue
if float_is_zero(inv_line.quantity, precision_digits=qty_prec):
_logger.info(
"Skipping invoice line %s qty %s "
"of invoice %s. Reason: qty = 0"
% (inv_line.name, inv_line.quantity, invoice.name)
)
continue
partner_country = self._get_partner_country(
inv_line, notedict, eu_countries
)
partner_country_code = (
invoice.commercial_partner_id._get_intrastat_country_code()
)
if inv_intrastat_line:
hs_code = inv_intrastat_line.hs_code_id
elif inv_line.product_id and self._is_product(inv_line):
hs_code = inv_line.product_id.get_hs_code_recursively()
if not hs_code:
line_notes = [
_("Missing Intrastat Code on product %s. ")
% (inv_line.product_id.display_name)
]
self._format_line_note(inv_line, notedict, line_notes)
continue
else:
_logger.info(
"Skipping invoice line %s qty %s "
"of invoice %s. Reason: no product nor Intrastat Code"
% (inv_line.name, inv_line.quantity, invoice.name)
)
continue
intrastat_transaction = self._get_intrastat_transaction(
inv_line, notedict
)
if inv_intrastat_line:
weight = inv_intrastat_line.transaction_weight
suppl_unit_qty = inv_intrastat_line.transaction_suppl_unit_qty
else:
weight, suppl_unit_qty = self._get_weight_and_supplunits(
inv_line, hs_code, notedict
)
total_inv_weight += weight
sign = invoice.move_type in ("in_invoice", "out_refund") and 1 or -1
amount_company_currency = sign * inv_line.balance
total_inv_product_cc += amount_company_currency
if inv_intrastat_line:
product_origin_country = (
inv_intrastat_line.product_origin_country_id
)
product_origin_country_code = (
inv_intrastat_line.product_origin_country_code
)
else:
product_origin_country = inv_line.product_id.origin_country_id
product_origin_country_code = self._get_product_origin_country_code(
inv_line, product_origin_country, notedict
)
region = self._get_region(inv_line, notedict)
vat = self._get_vat(inv_line, notedict)
line_vals = {
"parent_id": self.id,
"invoice_line_id": inv_line.id,
"src_dest_country_id": partner_country.id,
"src_dest_country_code": partner_country_code,
"product_id": inv_line.product_id.id,
"hs_code_id": hs_code.id,
"weight": weight,
"suppl_unit_qty": suppl_unit_qty,
"amount_company_currency": amount_company_currency,
"amount_accessory_cost_company_currency": 0.0,
"transaction_id": intrastat_transaction.id,
"product_origin_country_id": product_origin_country.id or False,
"product_origin_country_code": product_origin_country_code,
"region_id": region and region.id or False,
"vat": vat,
}
# extended declaration
if self.reporting_level == "extended":
transport = self._get_transport(inv_line, notedict)
line_vals.update({"transport_id": transport.id})
self._update_computation_line_vals(inv_line, line_vals, notedict)
if line_vals:
lines_current_invoice.append(line_vals)
self._handle_invoice_accessory_cost(
invoice,
lines_current_invoice,
total_inv_accessory_costs_cc,
total_inv_product_cc,
total_inv_weight,
)
for line_vals in lines_current_invoice:
if (
not line_vals["amount_company_currency"]
and not line_vals["amount_accessory_cost_company_currency"]
):
inv_line = self.env["account.move.line"].browse(
line_vals["invoice_line_id"]
)
_logger.info(
"Skipping invoice line %s qty %s "
"of invoice %s. Reason: price_subtotal = 0 "
"and accessory costs = 0"
% (inv_line.name, inv_line.quantity, inv_line.move_id.name)
)
continue
lines.append(line_vals)
return lines
def _get_uom_refs(self, ref):
uom_refs = {
"weight_uom_categ": self.env.ref("uom.product_uom_categ_kgm"),
"kg_uom": self.env.ref("uom.product_uom_kgm"),
"pce_uom_categ": self.env.ref("uom.product_uom_categ_unit"),
"pce_uom": self.env.ref("uom.product_uom_unit"),
}
return uom_refs[ref]
def action_gather(self):
self.ensure_one()
self.message_post(body=_("Generate Lines from Invoices"))
notedict = {
"note": "",
"line_nbr": 0,
}
# TODO: implement a solution to avoid double warnings
# e.g. warning on invoice that is repeated for every line
# or warning on a product that is repeated for every invoice line
# with that product
self.computation_line_ids.unlink()
self.declaration_line_ids.unlink()
lines = self._gather_invoices(notedict)
vals = {"note": notedict["note"]}
if not lines:
vals["action"] = "nihil"
vals["note"] += (
"\n"
+ _("No records found for the selected period !")
+ "\n"
+ _("The Declaration Action has been set to 'nihil'.")
)
else:
vals["computation_line_ids"] = [(0, 0, x) for x in lines]
self.write(vals)
if vals["note"]:
result_view = self.env.ref("intrastat_product.intrastat_result_view_form")
return {
"name": _("Generate lines from invoices: results"),
"view_type": "form",
"view_mode": "form",
"res_model": "intrastat.result.view",
"view_id": result_view.id,
"target": "new",
"context": dict(self._context, note=vals["note"]),
"type": "ir.actions.act_window",
}
return True
@api.model
def _group_line_hashcode_fields(self, computation_line):
return {
"country": computation_line.src_dest_country_code,
"hs_code_id": computation_line.hs_code_id.id or False,
"intrastat_unit": computation_line.intrastat_unit_id.id or False,
"transaction": computation_line.transaction_id.id or False,
"transport": computation_line.transport_id.id or False,
"region": computation_line.region_id.id or False,
"product_origin_country": computation_line.product_origin_country_code,
"vat": computation_line.vat or False,
}
def group_line_hashcode(self, computation_line):
hc_fields = self._group_line_hashcode_fields(computation_line)
hashcode = "-".join([str(f) for f in hc_fields.values()])
return hashcode
@api.model
def _prepare_grouped_fields(self, computation_line, fields_to_sum):
vals = {
"src_dest_country_id": computation_line.src_dest_country_id.id,
"src_dest_country_code": computation_line.src_dest_country_code,
"intrastat_unit_id": computation_line.intrastat_unit_id.id,
"hs_code_id": computation_line.hs_code_id.id,
"transaction_id": computation_line.transaction_id.id,
"transport_id": computation_line.transport_id.id,
"region_id": computation_line.region_id.id,
"parent_id": computation_line.parent_id.id,
"product_origin_country_id": computation_line.product_origin_country_id.id,
"product_origin_country_code": computation_line.product_origin_country_code,
"amount_company_currency": 0.0,
"vat": computation_line.vat,
}
for field in fields_to_sum:
vals[field] = 0.0
return vals
def _fields_to_sum(self):
fields_to_sum = ["weight", "suppl_unit_qty"]
return fields_to_sum
@api.model
def _prepare_declaration_line(self, computation_lines):
fields_to_sum = self._fields_to_sum()
vals = self._prepare_grouped_fields(computation_lines[0], fields_to_sum)
for computation_line in computation_lines:
for field in fields_to_sum:
vals[field] += computation_line[field]
vals["amount_company_currency"] += (
computation_line["amount_company_currency"]
+ computation_line["amount_accessory_cost_company_currency"]
)
# round, otherwise odoo with truncate (6.7 -> 6... instead of 7 !)
for field in fields_to_sum:
vals[field] = int(round(vals[field]))
if not vals["weight"]:
vals["weight"] = 1
vals["amount_company_currency"] = int(round(vals["amount_company_currency"]))
return vals
def generate_declaration(self):
"""generate declaration lines"""
self.ensure_one()
assert self.valid, "Computation lines are not valid"
self.message_post(body=_("Generate Declaration Lines"))
# Delete existing declaration lines
self.declaration_line_ids.unlink()
# Regenerate declaration lines from computation lines
dl_group = {}
for cl in self.computation_line_ids:
hashcode = self.group_line_hashcode(cl)
if hashcode in dl_group:
dl_group[hashcode].append(cl)
else:
dl_group[hashcode] = [cl]
ipdl = self.declaration_line_ids
for cl_lines in list(dl_group.values()):
vals = self._prepare_declaration_line(cl_lines)
declaration_line = ipdl.create(vals)
for cl in cl_lines:
cl.write({"declaration_line_id": declaration_line.id})
return True
def _check_generate_xml(self):
self.ensure_one()
if not self.company_id.partner_id.vat:
raise UserError(
_("The VAT number is not set for the partner '%s'.")
% self.company_id.partner_id.display_name
)
def generate_xml(self):
"""generate the INTRASTAT Declaration XML file"""
self.ensure_one()
if self.xml_attachment_id:
raise UserError(
_(
"An XML Export already exists for %s. "
"To re-generate it, you must first delete it."
)
% self.display_name
)
self.message_post(body=_("Generate XML Declaration File"))
self._check_generate_xml()
self._unlink_attachments()
xml_bytes = self._generate_xml()
if xml_bytes:
attach_id = self._attach_xml_file(
xml_bytes, "{}_{}".format(self.declaration_type, self.revision)
)
self.write({"xml_attachment_id": attach_id})
return
else:
raise UserError(_("No XML File has been generated."))
def delete_xml(self):
self.ensure_one()
self.xml_attachment_id and self.xml_attachment_id.unlink()
def create_xls(self):
if self.env.context.get("computation_lines"):
report_file = "intrastat_transactions"
else:
report_file = "intrastat_declaration_lines"
return {
"type": "ir.actions.report",
"report_type": "xlsx",
"report_name": "intrastat_product.product_declaration_xls",
"context": dict(self.env.context, report_file=report_file),
"data": {"dynamic_report": True},
}
@api.model
def _xls_computation_line_fields(self):
"""
Update list in custom module to add/drop columns or change order
"""
return [
"product",
"product_origin_country",
"hs_code",