-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.py
125 lines (106 loc) · 4.31 KB
/
invoice.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
# This file is part of the sale_invoice_line_create_wizard module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from itertools import groupby
from trytond.model import ModelView, fields
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pool import Pool
class CreateInvoicesStart(ModelView):
'Create Invoices Start'
__name__ = 'sale_invoice_line_create_wizard.create_invoices.start'
date = fields.Date('Date')
@staticmethod
def default_date():
Date = Pool().get('ir.date')
return Date.today()
class CreateInvoices(Wizard):
'Create Invoices'
__name__ = 'sale_invoice_line_create_wizard.create_invoices'
start = StateView('sale_invoice_line_create_wizard.create_invoices.start',
'sale_invoice_line_create_wizard.create_invoices_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('OK', 'create_invoices', 'tryton-ok', True),
])
create_invoices = StateAction('account_invoice.act_invoice_form')
def do_create_invoices(self, action):
pool = Pool()
SaleLine = pool.get('sale.line')
sale_lines = SaleLine.search([
('manual_delivery_date', '<=', self.start.date),
('invoice_lines', '!=', None),
('sale.state', 'in', ['processing', 'done']),
])
invoice_lines = []
for sale_line in sale_lines:
for invoice_line in sale_line.invoice_lines:
if invoice_line.invoice is None:
invoice_lines.append(invoice_line)
invoices = self._invoice(invoice_lines)
data = {'res_id': [c.id for c in invoices]}
if len(invoices) == 1:
action['views'].reverse()
return action, data
@classmethod
def _group_invoice_key(cls, invoice_line):
'''
The key to group invoice_lines by Invoice
'''
pool = Pool()
grouping = [
('party', invoice_line.party),
('company', invoice_line.company),
('agent', invoice_line.origin.sale.agent),
('currency', invoice_line.currency),
('type', invoice_line.invoice_type),
('invoice_date', invoice_line.origin.manual_delivery_date),
('account', invoice_line.party.account_receivable_used),
('reference', invoice_line.origin.sale.reference),
('description', invoice_line.origin.sale.description),
]
try:
Pos = pool.get('account.pos')
except KeyError:
Pos = None
if Pos and invoice_line.origin.sale.pos:
grouping.append(('pos', invoice_line.origin.sale.pos))
try:
Paymode = pool.get('payment.paymode')
except KeyError:
Paymode = None
if Paymode and invoice_line.origin.sale.paymode:
grouping.append(('paymode', invoice_line.origin.sale.paymode))
return grouping
@classmethod
def _get_invoice(cls, keys):
pool = Pool()
Invoice = pool.get('account.invoice')
values = dict(keys)
values['invoice_address'] = values['party'].address_get('invoice')
invoice = Invoice(**values)
invoice.on_change_type()
return invoice
@classmethod
def _invoice(cls, lines):
pool = Pool()
Invoice = pool.get('account.invoice')
try:
Pos = pool.get('account.pos')
except KeyError:
Pos = None
if not lines:
return []
lines = sorted(lines, key=cls._group_invoice_key)
invoices = []
for key, grouped_lines in groupby(lines, key=cls._group_invoice_key):
invoice = cls._get_invoice(key)
invoice.lines = (list(getattr(invoice, 'lines', [])) +
list(x for x in grouped_lines))
if Pos:
invoice.invoice_type = invoice.on_change_with_invoice_type()
invoice.set_pyafipws_concept()
if invoice.pyafipws_concept in ['2', '3']:
invoice.set_pyafipws_billing_dates()
invoices.append(invoice)
invoices = Invoice.create([x._save_values for x in invoices])
Invoice.update_taxes(invoices)
return invoices