-
Notifications
You must be signed in to change notification settings - Fork 4
/
commission.py
622 lines (546 loc) · 21.3 KB
/
commission.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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from itertools import groupby
from simpleeval import simple_eval
try:
from sql import Null
except ImportError:
Null = None
from sql.aggregate import Sum
from trytond.i18n import gettext
from trytond.model import (
MatchMixin, ModelSQL, ModelView, fields, sequence_ordered)
from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits, round_price
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, Id, If
from trytond.tools import decistmt, grouped_slice, reduce_ids
from trytond.transaction import Transaction
from trytond.wizard import Button, StateAction, StateView, Wizard
from .exceptions import FormulaError
class Agent(ModelSQL, ModelView):
'Commission Agent'
__name__ = 'commission.agent'
party = fields.Many2One('party.party', "Party", required=True,
context={
'company': Eval('company', -1),
},
depends={'company'},
help="The party for whom the commission is calculated.")
type_ = fields.Selection([
('agent', 'Agent Of'),
('principal', 'Principal Of'),
], 'Type')
company = fields.Many2One('company.company', 'Company', required=True)
plan = fields.Many2One('commission.plan', "Plan",
help="The plan used to calculate the commission.")
currency = fields.Many2One('currency.currency', "Currency", required=True)
pending_amount = fields.Function(fields.Numeric('Pending Amount',
digits=price_digits), 'get_pending_amount')
selections = fields.One2Many(
'commission.agent.selection', 'agent', "Selections")
@classmethod
def __register__(cls, module_name):
pool = Pool()
Company = pool.get('company.company')
sql_table = cls.__table__()
company = Company.__table__()
super().__register__(module_name)
transaction = Transaction()
cursor = transaction.connection.cursor()
table = cls.__table_handler__(module_name)
# Migration from 5.4: Ensure currency is set
# Don't use UPDATE FROM because SQLite does not support it
cursor.execute(*sql_table.update(
columns=[sql_table.currency],
values=[company.select(
company.currency,
where=company.id == sql_table.company)],
where=(sql_table.currency == Null)))
# Migration from 5.4: Add not null on currency
table.not_null_action('currency', 'add')
@staticmethod
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_type_():
return 'agent'
@classmethod
def default_currency(cls):
pool = Pool()
Company = pool.get('company.company')
company = cls.default_company()
if company:
return Company(company).currency.id
@fields.depends('company', 'currency')
def on_change_company(self):
if self.company and not self.currency:
self.currency = self.company.currency
def get_rec_name(self, name):
if self.plan:
return '%s - %s' % (self.party.rec_name, self.plan.rec_name)
else:
return self.party.rec_name
@classmethod
def search_rec_name(cls, name, clause):
if clause[1].startswith('!') or clause[1].startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
('party.rec_name',) + tuple(clause[1:]),
('plan.rec_name',) + tuple(clause[1:]),
]
@classmethod
def get_pending_amount(cls, agents, name):
pool = Pool()
Commission = pool.get('commission')
commission = Commission.__table__()
cursor = Transaction().connection.cursor()
ids = [a.id for a in agents]
amounts = dict.fromkeys(ids, None)
for sub_ids in grouped_slice(ids):
where = reduce_ids(commission.agent, sub_ids)
where &= commission.invoice_line == Null
query = commission.select(commission.agent, Sum(commission.amount),
where=where,
group_by=commission.agent)
cursor.execute(*query)
amounts.update(dict(cursor))
for agent_id, amount in amounts.items():
if amount:
# SQLite uses float for SUM
if not isinstance(amount, Decimal):
amount = Decimal(str(amount))
amounts[agent_id] = round_price(amount)
return amounts
@property
def account(self):
if self.type_ == 'agent':
return self.party.account_payable_used
elif self.type_ == 'principal':
return self.party.account_receivable_used
class AgentSelection(sequence_ordered(), MatchMixin, ModelSQL, ModelView):
"Agent Selection"
__name__ = 'commission.agent.selection'
agent = fields.Many2One('commission.agent', "Agent", required=True)
start_date = fields.Date(
"Start Date",
domain=[
If(Eval('start_date') & Eval('end_date'),
('start_date', '<=', Eval('end_date')),
()),
],
help="The first date that the agent will be considered for selection.")
end_date = fields.Date(
"End Date",
domain=[
If(Eval('start_date') & Eval('end_date'),
('end_date', '>=', Eval('start_date')),
()),
],
help="The last date that the agent will be considered for selection.")
party = fields.Many2One(
'party.party', "Party", ondelete='CASCADE', select=True,
context={
'company': Eval('company', -1),
},
depends={'company'})
company = fields.Function(fields.Many2One('company.company', "Company"),
'on_change_with_company', searcher='search_company')
employee = fields.Many2One(
'company.employee', "Employee", select=True,
domain=[
('company', '=', Eval('company')),
])
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('agent')
cls._order.insert(0, ('party', 'ASC NULLS LAST'))
cls._order.insert(1, ('employee', 'ASC NULLS LAST'))
@fields.depends('agent', '_parent_agent.company')
def on_change_with_company(self, name=None):
if self.agent:
return self.agent.company.id
@classmethod
def search_company(cls, name, clause):
return [('agent.' + clause[0],) + tuple(clause[1:])]
def match(self, pattern):
pool = Pool()
Date = pool.get('ir.date')
pattern = pattern.copy()
if 'company' in pattern:
pattern.pop('company')
with Transaction().set_context(company=self.company.id):
date = pattern.pop('date', None) or Date.today()
if self.start_date and self.start_date > date:
return False
if self.end_date and self.end_date < date:
return False
return super().match(pattern)
class Plan(ModelSQL, ModelView):
'Commission Plan'
__name__ = 'commission.plan'
name = fields.Char('Name', required=True, translate=True)
commission_product = fields.Many2One('product.product',
'Commission Product', required=True,
domain=[
('type', '=', 'service'),
('default_uom', '=', Id('product', 'uom_unit')),
('template.type', '=', 'service'),
('template.default_uom', '=', Id('product', 'uom_unit')),
],
help="The product that is used on the invoice lines.")
commission_method = fields.Selection([
('posting', 'On Posting'),
('payment', 'On Payment'),
], 'Commission Method',
help="When the commission is due.")
lines = fields.One2Many('commission.plan.line', 'plan', "Lines",
help="The formulas used to calculate the commission for different "
"criteria.")
@staticmethod
def default_commission_method():
return 'posting'
def get_context_formula(self, amount, product):
return {
'names': {
'amount': amount,
},
}
def compute(self, amount, product, pattern=None):
'Compute commission amount for the amount'
def parents(categories):
for category in categories:
while category:
yield category
category = category.parent
if pattern is None:
pattern = {}
if product:
pattern['categories'] = [
c.id for c in parents(product.categories_all)]
pattern['product'] = product.id
else:
pattern['categories'] = []
pattern['product'] = None
context = self.get_context_formula(amount, product)
for line in self.lines:
if line.match(pattern):
return line.get_amount(**context)
class PlanLines(sequence_ordered(), ModelSQL, ModelView, MatchMixin):
'Commission Plan Line'
__name__ = 'commission.plan.line'
plan = fields.Many2One('commission.plan', 'Plan', required=True,
ondelete='CASCADE',
help="The plan to which the line belongs.")
category = fields.Many2One(
'product.category', "Category", ondelete='CASCADE',
help="Apply only to products in the category.")
product = fields.Many2One('product.product', "Product", ondelete='CASCADE',
help="Apply only to the product.")
formula = fields.Char('Formula', required=True,
help="The python expression used to calculate the amount of "
"commission for the line.\n"
"It is evaluated with:\n"
"- amount: the original amount")
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('plan')
@staticmethod
def default_formula():
return 'amount'
@classmethod
def validate_fields(cls, lines, field_names):
super().validate_fields(lines, field_names)
cls.check_formula(lines, field_names)
@classmethod
def check_formula(cls, lines, field_names=None):
if field_names and 'formula' not in field_names:
return
for line in lines:
context = line.plan.get_context_formula(Decimal(0), None)
try:
if not isinstance(line.get_amount(**context), Decimal):
raise ValueError
except Exception as exception:
raise FormulaError(
gettext('commission.msg_plan_line_invalid_formula',
formula=line.formula,
line=line.rec_name,
exception=exception)) from exception
def get_amount(self, **context):
'Return amount (as Decimal)'
context.setdefault('functions', {})['Decimal'] = Decimal
return simple_eval(decistmt(self.formula), **context)
def match(self, pattern):
if 'categories' in pattern:
pattern = pattern.copy()
categories = pattern.pop('categories')
if (self.category is not None
and self.category.id not in categories):
return False
return super(PlanLines, self).match(pattern)
class Commission(ModelSQL, ModelView):
'Commission'
__name__ = 'commission'
_readonly_states = {
'readonly': Bool(Eval('invoice_line')),
}
origin = fields.Reference('Origin', selection='get_origin', select=True,
readonly=True,
help="The source of the commission.")
date = fields.Date('Date', select=True, states=_readonly_states,
help="When the commission is due.")
agent = fields.Many2One('commission.agent', 'Agent', required=True,
states=_readonly_states)
product = fields.Many2One('product.product', 'Product', required=True,
states=_readonly_states,
help="The product that is used on the invoice line.")
base_amount = Monetary(
"Base Amount", currency='currency', digits='currency',
states=_readonly_states)
amount = Monetary(
"Amount", currency='currency', required=True, digits=price_digits,
domain=[('amount', '!=', 0)],
states=_readonly_states)
currency = fields.Function(fields.Many2One('currency.currency',
'Currency'), 'on_change_with_currency')
type_ = fields.Function(fields.Selection([
('in', 'Incoming'),
('out', 'Outgoing'),
], 'Type'), 'on_change_with_type_')
invoice_line = fields.Many2One('account.invoice.line', 'Invoice Line',
readonly=True)
invoice_state = fields.Function(fields.Selection([
('', ''),
('invoiced', 'Invoiced'),
('paid', 'Paid'),
('cancelled', 'Cancelled'),
], "Invoice State",
help="The current state of the invoice "
"that the commission appears on."),
'get_invoice_state')
@classmethod
def __setup__(cls):
super(Commission, cls).__setup__()
cls.__access__.add('agent')
cls._buttons.update({
'invoice': {
'invisible': Bool(Eval('invoice_line')),
'depends': ['invoice_line'],
},
})
@classmethod
def _get_origin(cls):
'Return list of Model names for origin Reference'
return ['account.invoice.line']
@classmethod
def get_origin(cls):
pool = Pool()
Model = pool.get('ir.model')
get_name = Model.get_name
models = cls._get_origin()
return [(None, '')] + [(m, get_name(m)) for m in models]
@fields.depends('agent')
def on_change_with_currency(self, name=None):
if self.agent:
return self.agent.currency.id
@fields.depends('agent')
def on_change_with_type_(self, name=None):
if self.agent:
return {
'agent': 'out',
'principal': 'in',
}.get(self.agent.type_)
@fields.depends('agent', 'product')
def on_change_agent(self):
if not self.product and self.agent and self.agent.plan:
self.product = self.agent.plan.commission_product
def get_invoice_state(self, name):
state = ''
if self.invoice_line:
state = 'invoiced'
invoice = self.invoice_line.invoice
if invoice and invoice.state in {'paid', 'cancelled'}:
state = invoice.state
return state
@classmethod
def copy(cls, commissions, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('invoice_line', None)
return super(Commission, cls).copy(commissions, default=default)
@classmethod
@ModelView.button
def invoice(cls, commissions):
pool = Pool()
Invoice = pool.get('account.invoice')
InvoiceLine = pool.get('account.invoice.line')
def invoice_key(c):
return c._group_to_invoice_key()
def line_key(c):
return c._group_to_invoice_line_key()
commissions.sort(key=invoice_key)
invoices = []
invoice_lines = []
to_save = []
for key, commissions in groupby(commissions, key=invoice_key):
commissions = list(commissions)
key = dict(key)
invoice = cls._get_invoice(key)
invoices.append(invoice)
commissions.sort(key=line_key)
for key, commissions in groupby(commissions, key=line_key):
commissions = [c for c in commissions if not c.invoice_line]
key = dict(key)
invoice_line = cls._get_invoice_line(key, invoice, commissions)
invoice_lines.append(invoice_line)
for commission in commissions:
commission.invoice_line = invoice_line
to_save.append(commission)
Invoice.save(invoices)
InvoiceLine.save(invoice_lines)
Invoice.update_taxes(invoices)
cls.save(to_save)
@classmethod
def cancel(cls, commissions):
cancel_commissions = cls.copy(commissions)
for commission in cancel_commissions:
commission.update_cancel_copy()
cls.save(cancel_commissions)
return cancel_commissions
def update_cancel_copy(self):
self.amount *= -1
def _group_to_invoice_key(self):
direction = {
'in': 'out',
'out': 'in',
}.get(self.type_)
return (('agent', self.agent), ('type', direction))
@classmethod
def get_journal(cls):
pool = Pool()
Journal = pool.get('account.journal')
journals = Journal.search([
('type', '=', 'commission'),
], limit=1)
if journals:
return journals[0]
@classmethod
def _get_invoice(cls, key):
pool = Pool()
Invoice = pool.get('account.invoice')
agent = key['agent']
if key['type'] == 'out':
payment_term = agent.party.customer_payment_term
else:
payment_term = agent.party.supplier_payment_term
return Invoice(
company=agent.company,
type=key['type'],
journal=cls.get_journal(),
party=agent.party,
invoice_address=agent.party.address_get(type='invoice'),
currency=agent.currency,
account=agent.account,
payment_term=payment_term,
)
def _group_to_invoice_line_key(self):
return (('product', self.product),)
@classmethod
def _get_invoice_line(cls, key, invoice, commissions):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
def sign(commission):
if invoice.type == commission.type_:
return -1
else:
return 1
product = key['product']
amount = invoice.currency.round(
sum(c.amount * sign(c) for c in commissions))
invoice_line = InvoiceLine()
invoice_line.invoice = invoice
invoice_line.currency = invoice.currency
invoice_line.company = invoice.company
invoice_line.type = 'line'
# Use product.id to instantiate it with the correct context
invoice_line.product = product.id
invoice_line.quantity = 1
invoice_line.on_change_product()
invoice_line.unit_price = amount
return invoice_line
class CreateInvoice(Wizard):
'Create Commission Invoice'
__name__ = 'commission.create_invoice'
start_state = 'ask'
ask = StateView('commission.create_invoice.ask',
'commission.commission_create_invoice_ask_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('OK', 'create_', 'tryton-ok', default=True),
])
create_ = StateAction('account_invoice.act_invoice_form')
def get_domain(self):
domain = [('invoice_line', '=', None)]
if self.ask.from_:
domain.append(('date', '>=', self.ask.from_))
if self.ask.to:
domain.append(('date', '<=', self.ask.to))
if self.ask.type_ == 'in':
domain.append(('agent.type_', '=', 'principal'))
elif self.ask.type_ == 'out':
domain.append(('agent.type_', '=', 'agent'))
if self.ask.agents:
agents = [agent.id for agent in self.ask.agents]
domain.append(('agent', 'in', agents))
return domain
def do_create_(self, action):
pool = Pool()
Commission = pool.get('commission')
with Transaction().set_context(_check_access=True):
commissions = Commission.search(self.get_domain(),
order=[('agent', 'DESC'), ('date', 'DESC')])
commissions = Commission.browse(commissions)
Commission.invoice(commissions)
invoice_ids = list({c.invoice_line.invoice.id for c in commissions})
return action, {'res_id': invoice_ids}
class CreateInvoiceAsk(ModelView):
'Create Commission Invoice'
__name__ = 'commission.create_invoice.ask'
from_ = fields.Date('From',
domain=[
If(Eval('to') & Eval('from_'), [('from_', '<=', Eval('to'))],
[]),
],
help="Limit to commissions from this date.")
to = fields.Date('To',
domain=[
If(Eval('from_') & Eval('to'), [('to', '>=', Eval('from_'))],
[]),
],
help="Limit to commissions to this date.")
type_ = fields.Selection([
('in', 'Incoming'),
('out', 'Outgoing'),
('both', 'Both'),
], 'Type',
help="Limit to commissions of this type.")
agents = fields.Many2Many(
'commission.agent', None, None, "Agents",
domain=[
If(Eval('type_') == 'in',
('type_', '=', 'principal'), ()),
If(Eval('type_') == 'out',
('type_', '=', 'agent'), ()),
],
help="Limit to commissions for these agents.\n"
"If empty all agents of the selected type are used.")
@staticmethod
def default_type_():
return 'both'