This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedocument.py
211 lines (182 loc) · 6.75 KB
/
edocument.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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import functools
import os
import genshi
import genshi.template
# XXX fix: https://genshi.edgewall.org/ticket/582
from genshi.template.astutil import ASTCodeGenerator, ASTTransformer
from trytond.model import Model
from trytond.pool import Pool
from trytond.rpc import RPC
from trytond.tools import cached_property
from trytond.transaction import Transaction
if not hasattr(ASTCodeGenerator, 'visit_NameConstant'):
def visit_NameConstant(self, node):
if node.value is None:
self._write('None')
elif node.value is True:
self._write('True')
elif node.value is False:
self._write('False')
else:
raise Exception("Unknown NameConstant %r" % (node.value,))
ASTCodeGenerator.visit_NameConstant = visit_NameConstant
if not hasattr(ASTTransformer, 'visit_NameConstant'):
# Re-use visit_Name because _clone is deleted
ASTTransformer.visit_NameConstant = ASTTransformer.visit_Name
loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'template'),
auto_reload=True)
def has_goods_assets(func):
@functools.wraps(func)
def wrapper(self):
if any(l.product.type in {'goods', 'assets'}
for l in self.invoice.lines if l.product):
return func(self)
return wrapper
def remove_comment(stream):
for kind, data, pos in stream:
if kind is genshi.core.COMMENT:
continue
yield kind, data, pos
class Invoice(Model):
"EDocument UN/CEFACT Invoice"
__name__ = 'edocument.uncefact.invoice'
__no_slots__ = True # to work with cached_property
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls.__rpc__.update({
'render': RPC(instantiate=0),
})
def __init__(self, invoice):
pool = Pool()
Invoice = pool.get('account.invoice')
if int(invoice) >= 0:
invoice = Invoice(int(invoice))
with Transaction().set_context(language=invoice.party_lang):
self.invoice = invoice.__class__(int(invoice))
else:
self.invoice = invoice
def render(self, template):
if self.invoice.state not in {'posted', 'paid'}:
raise ValueError("Invoice must be posted")
tmpl = self._get_template(template)
if not tmpl:
raise NotImplementedError
return (tmpl.generate(this=self)
.filter(remove_comment)
.render()
.encode('utf-8'))
def _get_template(self, version):
return loader.load(os.path.join(version, 'CrossIndustryInvoice.xml'))
@cached_property
def type_code(self):
if self.invoice.type == 'out':
if all(l.amount < 0 for l in self.lines if l.product):
return '381'
else:
return '380'
else:
if all(l.amount < 0 for l in self.lines if l.product):
return '261'
else:
return '389'
@cached_property
def type_sign(self):
"The sign of the quantity depending of the type code"
if self.type_code in {'381', '261'}:
return -1
return 1
@cached_property
def lines(self):
return [l for l in self.invoice.lines if l.type == 'line']
@cached_property
def seller_trade_party(self):
if self.invoice.type == 'out':
return self.invoice.company.party
else:
return self.invoice.party
@cached_property
def seller_trade_address(self):
if self.invoice.type == 'out':
return self.invoice.company.party.address_get('invoice')
else:
return self.invoice.invoice_address
@cached_property
def seller_trade_tax_identifier(self):
if self.invoice.type == 'out':
return self.invoice.tax_identifier
else:
return self.invoice.party_tax_identifier
@cached_property
def buyer_trade_party(self):
if self.invoice.type == 'out':
return self.invoice.party
else:
return self.invoice.company.party
@cached_property
def buyer_trade_address(self):
if self.invoice.type == 'out':
return self.invoice.invoice_address
else:
return None
@cached_property
def buyer_trade_tax_identifier(self):
if self.invoice.type == 'out':
return self.invoice.party_tax_identifier
else:
return self.invoice.tax_identifier
@cached_property
@has_goods_assets
def ship_to_trade_party(self):
if self.invoice.type == 'out':
if getattr(self.invoice, 'sales'):
sale = self.invoice.sales[0] # XXX
if sale.shipment_party != self.buyer_trade_party:
return sale.shipment_party
else:
if self.invoice.purchases:
purchase = self.invoice.purchases[0] # XXX
address = purchase.warehouse.address
if (address and address.party != self.buyer_trade_party):
return address.party
@cached_property
@has_goods_assets
def ship_to_trade_address(self):
if self.invoice.type == 'out':
if getattr(self.invoice, 'sales'):
sale = self.invoice.sales[0] # XXX
if sale.shipment_party != self.buyer_trade_party:
return sale.shipment_address
else:
if getattr(self.invoice, 'purchases'):
purchase = self.invoice.purchases[0] # XXX
address = purchase.warehouse.address
if (address and address.party != self.buyer_trade_party):
return address
@cached_property
@has_goods_assets
def ship_from_trade_party(self):
if self.invoice.type == 'out':
if getattr(self.invoice, 'sales'):
sale = self.invoice.sales[0] # XXX
address = sale.warehouse.address
if address and address.party != self.seller_trade_party:
return address.shipment_party
@cached_property
@has_goods_assets
def ship_from_trade_address(self):
if self.invoice.type == 'out':
if getattr(self.invoice, 'sales'):
sale = self.invoice.sales[0] # XXX
address = sale.warehouse.address
if address and address.party != self.seller_trade_party:
return address
@cached_property
def payment_reference(self):
return self.invoice.number
@classmethod
def party_legal_ids(cls, party, address):
return []