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
|
from odoo import models, api, fields, _
from odoo.exceptions import UserError
from datetime import datetime
import logging
from terbilang import Terbilang
_logger = logging.getLogger(__name__)
class CustomerRebate(models.Model):
_name = 'customer.rebate'
_order = 'id desc'
_inherit = ['mail.thread']
partner_id = fields.Many2one('res.partner', string='Customer', required=True)
date_from = fields.Date(string='Date From', required=True, help="Pastikan tanggal 1 januari, jika tidak, code akan break")
date_to = fields.Date(string='Date To', required=True, help="Pastikan tanggal 31 desember, jika tidak, code akan break")
description = fields.Char(string='Description')
target_1st = fields.Float(string='Target/Quarter 1st')
target_2nd = fields.Float(string='Target/Quarter 2nd')
achieve_1 = fields.Float(string='Achieve 1 %')
achieve_2 = fields.Float(string='Achieve 2 %')
dpp_q1 = fields.Float(string='DPP Q1', compute='_compute_current_dpp')
dpp_q2 = fields.Float(string='DPP Q2', compute='_compute_current_dpp')
dpp_q3 = fields.Float(string='DPP Q3', compute='_compute_current_dpp')
dpp_q4 = fields.Float(string='DPP Q4', compute='_compute_current_dpp')
status_q1 = fields.Char(string='Status Q1', compute='_compute_achievement')
status_q2 = fields.Char(string='Status Q2', compute='_compute_achievement')
status_q3 = fields.Char(string='Status Q3', compute='_compute_achievement')
status_q4 = fields.Char(string='Status Q4', compute='_compute_achievement')
# all code class CustomerRebate deprecated, cause lack of performance
def _compute_current_dpp(self):
for line in self:
line.dpp_q1 = line._get_current_dpp_q1(line)
line.dpp_q2 = line._get_current_dpp_q2(line)
line.dpp_q3 = line._get_current_dpp_q3(line)
line.dpp_q4 = line._get_current_dpp_q4(line)
def _compute_achievement(self):
for line in self:
line.status_q1 = line._check_achievement(line.target_1st, line.target_2nd, line.dpp_q1)
line.status_q2 = line._check_achievement(line.target_1st, line.target_2nd, line.dpp_q2)
line.status_q3 = line._check_achievement(line.target_1st, line.target_2nd, line.dpp_q3)
line.status_q4 = line._check_achievement(line.target_1st, line.target_2nd, line.dpp_q4)
def _check_achievement(self, target_1st, target_2nd, dpp):
status = 'not achieve'
if dpp >= target_1st:
status = '1st'
elif dpp >= target_2nd:
status = '2nd'
else:
status = 'not achieve'
return status
def _get_current_dpp_q1(self, line):
sum_dpp = 0
brand = [10, 89, 122]
where = [
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
('move_id.is_customer_commision', '=', False),
('move_id.partner_id.id', '=', line.partner_id.id),
('move_id.invoice_date', '>=', line.date_from),
('move_id.invoice_date', '<=', '2023-03-31'),
('product_id.x_manufacture', 'in', brand),
]
invoice_lines = self.env['account.move.line'].search(where, order='id')
for invoice_line in invoice_lines:
sum_dpp += invoice_line.price_subtotal
return sum_dpp
def _get_current_dpp_q2(self, line):
sum_dpp = 0
brand = [10, 89, 122]
where = [
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
('move_id.is_customer_commision', '=', False),
('move_id.partner_id.id', '=', line.partner_id.id),
('move_id.invoice_date', '>=', '2023-04-01'),
('move_id.invoice_date', '<=', '2023-06-30'),
('product_id.x_manufacture', 'in', brand),
]
invoices = self.env['account.move.line'].search(where, order='id')
for invoice in invoices:
sum_dpp += invoice.price_subtotal
return sum_dpp
def _get_current_dpp_q3(self, line):
sum_dpp = 0
brand = [10, 89, 122]
where = [
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
('move_id.is_customer_commision', '=', False),
('move_id.partner_id.id', '=', line.partner_id.id),
('move_id.invoice_date', '>=', '2023-07-01'),
('move_id.invoice_date', '<=', '2023-09-30'),
('product_id.x_manufacture', 'in', brand),
]
invoices = self.env['account.move.line'].search(where, order='id')
for invoice in invoices:
sum_dpp += invoice.price_subtotal
return sum_dpp
def _get_current_dpp_q4(self, line):
sum_dpp = 0
brand = [10, 89, 122]
where = [
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
('move_id.is_customer_commision', '=', False),
('move_id.partner_id.id', '=', line.partner_id.id),
('move_id.invoice_date', '>=', '2023-10-01'),
('move_id.invoice_date', '<=', line.date_to),
('product_id.x_manufacture', 'in', brand),
]
invoices = self.env['account.move.line'].search(where, order='id')
for invoice in invoices:
sum_dpp += invoice.price_subtotal
return sum_dpp
class RejectReasonCommision(models.TransientModel):
_name = 'reject.reason.commision'
_description = 'Wizard for Reject Reason Customer Commision'
request_id = fields.Many2one('customer.commision', string='Request')
reason_reject = fields.Text(string='Reason for Rejection', required=True, tracking=True)
def confirm_reject(self):
commision = self.request_id
if commision:
commision.last_status = commision.status
commision.write({'reason_reject': self.reason_reject})
commision.status = 'reject'
return {'type': 'ir.actions.act_window_close'}
class CustomerCommision(models.Model):
_name = 'customer.commision'
_order = 'id desc'
_inherit = ['mail.thread']
_rec_name = 'number'
number = fields.Char(string='Document No', index=True, copy=False, readonly=True)
date_from = fields.Date(string='Date From', required=True)
date_to = fields.Date(string='Date To', required=True)
partner_ids = fields.Many2many('res.partner', String='Customer', required=True)
description = fields.Char(string='Description')
notification = fields.Char(string='Notification')
commision_lines = fields.One2many('customer.commision.line', 'customer_commision_id', string='Lines', auto_join=True)
status = fields.Selection([
('pengajuan1', 'Menunggu Approval Manager Sales'),
('pengajuan2', 'Menunggu Approval Marketing'),
('pengajuan3', 'Menunggu Approval Pimpinan'),
('pengajuan4', 'Menunggu Approval Accounting'),
('approved', 'Approved'),
('reject', 'Rejected'),
], string='Status', copy=False, readonly=True, tracking=3, index=True, track_visibility='onchange',default='draft')
last_status = fields.Selection([
('pengajuan1', 'Menunggu Approval Manager Sales'),
('pengajuan2', 'Menunggu Approval Marketing'),
('pengajuan3', 'Menunggu Approval Pimpinan'),
('pengajuan4', 'Menunggu Approval Accounting'),
('approved', 'Approved'),
('reject', 'Rejected'),
], string='Status')
commision_percent = fields.Float(string='Commision %', tracking=3)
commision_amt = fields.Float(string='Commision Amount', tracking=3)
commision_amt_text = fields.Char(string='Commision Amount Text', compute='compute_delivery_amt_text')
total_dpp = fields.Float(string='Total DPP', compute='_compute_total_dpp')
commision_type = fields.Selection([
('fee', 'Fee'),
('cashback', 'Cashback'),
('rebate', 'Rebate'),
], string='Commision Type', required=True)
bank_name = fields.Char(string='Bank', tracking=3, required=True)
account_name = fields.Char(string='Account Name', tracking=3, required=True)
bank_account = fields.Char(string='Account No', tracking=3, required=True)
note_transfer = fields.Char(string='Keterangan')
brand_ids = fields.Many2many('x_manufactures', string='Brands')
payment_status = fields.Selection([
('pending', 'Pending'),
('payment', 'Payment'),
], string='Payment Status', copy=False, readonly=True, tracking=3, default='pending')
note_finnance = fields.Text('Notes Finnance')
reason_reject = fields.Char(string='Reason Reaject', tracking=True, track_visibility='onchange')
approved_by = fields.Char(string='Approved By', tracking=True, track_visibility='always')
grouped_so_number = fields.Char(string='Group SO Number', compute='_compute_grouped_numbers')
grouped_invoice_number = fields.Char(string='Group Invoice Number', compute='_compute_grouped_numbers')
sales_id = fields.Many2one('res.users', string="Sales", tracking=True)
date_approved_sales = fields.Datetime(string="Date Approved Sales", tracking=True)
date_approved_marketing = fields.Datetime(string="Date Approved Marketing", tracking=True)
date_approved_pimpinan = fields.Datetime(string="Date Approved Pimpinan", tracking=True)
date_approved_accounting = fields.Datetime(string="Date Approved Accounting", tracking=True)
position_sales = fields.Char(string="Position Sales", tracking=True)
position_marketing = fields.Char(string="Position Marketing", tracking=True)
position_pimpinan = fields.Char(string="Position Pimpinan", tracking=True)
position_accounting = fields.Char(string="Position Accounting", tracking=True)
def compute_delivery_amt_text(self):
tb = Terbilang()
for record in self:
res = ''
try:
if record.commision_amt > 0:
tb.parse(int(record.commision_amt))
res = tb.getresult().title()
record.commision_amt_text = res + ' Rupiah'
except:
record.commision_amt_text = res
def _compute_grouped_numbers(self):
for rec in self:
so_numbers = set()
invoice_numbers = set()
for line in rec.commision_lines:
if line.invoice_id:
if line.invoice_id.sale_id:
so_numbers.add(line.invoice_id.sale_id.name)
invoice_numbers.add(line.invoice_id.name)
rec.grouped_so_number = ', '.join(sorted(so_numbers))
rec.grouped_invoice_number = ', '.join(sorted(invoice_numbers))
# add status for type of commision, fee, rebate / cashback
# include child or not?
# @api.constrains('partner_ids')
def _onchange_partner_ids(self):
commision = self.env['cust.commision'].search([
('partner_id', 'in', [rec.id for rec in self.partner_ids]),
('commision_type', '=', self.commision_type)
], limit=1)
if commision:
if self.commision_type == 'fee':
max_commision = max(commision.mapped('commision_percent'))
self.commision_percent = max_commision
else:
target_1st, target_2nd = commision.target_1st, commision.target_2nd
achieve_1st, achieve_2nd = commision.achieve_1st, commision.achieve_2nd
if self.total_dpp >= target_1st:
self.commision_percent = achieve_1st
elif target_2nd <= self.total_dpp < target_1st:
self.commision_percent = achieve_2nd
else:
self.commision_percent = 0
self._onchange_commision_amt()
@api.constrains('commision_percent')
def _onchange_commision_percent(self):
if not self.env.context.get('_onchange_commision_percent', True):
return
if self.commision_amt == 0:
self.commision_amt = self.commision_percent * self.total_dpp // 100
@api.constrains('commision_amt')
def _onchange_commision_amt(self):
if not self.env.context.get('_onchange_commision_amt', True):
return
if self.total_dpp > 0 and self.commision_percent == 0:
self.commision_percent = (self.commision_amt / self.total_dpp) * 100
def _compute_total_dpp(self):
for data in self:
total_dpp = 0
for line in data.commision_lines:
total_dpp = total_dpp + line.dpp
data.total_dpp = total_dpp
@api.model
def create(self, vals):
vals['number'] = self.env['ir.sequence'].next_by_code('customer.commision') or '0'
# if vals['commision_amt'] > 0:
# commision_amt = vals['commision_amt']
# total_dpp = vals['total_dpp']
# commision_percent = commision_amt / total_dpp * 100
# vals['commision_percent'] = commision_percent
result = super(CustomerCommision, self).create(vals)
return result
def action_confirm_customer_commision(self):
now = datetime.utcnow()
if not self.status or self.status == 'draft':
self.status = 'pengajuan1'
elif self.status == 'pengajuan1' and self.env.user.is_sales_manager:
self.status = 'pengajuan2'
self.approved_by = (self.approved_by + ', ' if self.approved_by else '') + self.env.user.name
self.date_approved_sales = now
self.position_sales = 'Sales Manager'
elif self.status == 'pengajuan2' and self.env.user.id == 19:
self.status = 'pengajuan3'
self.approved_by = (self.approved_by + ', ' if self.approved_by else '') + self.env.user.name
self.date_approved_marketing = now
self.position_marketing = 'Marketing Manager'
elif self.status == 'pengajuan3' and self.env.user.is_leader:
self.status = 'pengajuan4'
self.approved_by = (self.approved_by + ', ' if self.approved_by else '') + self.env.user.name
self.date_approved_pimpinan = now
self.position_pimpinan = 'Pimpinan'
elif self.status == 'pengajuan4' and self.env.user.id == 1272:
for line in self.commision_lines:
line.invoice_id.is_customer_commision = True
self.status = 'approved'
self.approved_by = (self.approved_by + ', ' if self.approved_by else '') + self.env.user.name
self.date_approved_accounting = now
self.position_accounting = 'Accounting'
else:
raise UserError('Harus di approved oleh yang bersangkutan')
return
def action_reject(self):#add 2 step approval
return {
'type': 'ir.actions.act_window',
'name': _('Reject Reason'),
'res_model': 'reject.reason.commision',
'view_mode': 'form',
'target': 'new',
'context': {'default_request_id': self.id},
}
def button_draft(self):
for commision in self:
commision.status = commision.last_status if commision.last_status else 'draft'
def action_confirm_customer_payment(self):
if self.status != 'approved':
raise UserError('Commision harus di approve terlebih dahulu sebelum di konfirmasi pembayarannya')
if self.payment_status == 'payment':
raise UserError('Customer Commision sudah berstatus Payment')
group_id = self.env.ref('indoteknik_custom.group_role_fat').id
users_in_group = self.env['res.users'].search([('groups_id', 'in', [group_id])])
if self.env.user.id not in users_in_group.mapped('id'):
raise UserError('Hanya bisa dikonfirmasi oleh FAT')
else:
self.payment_status = 'payment'
return
def generate_customer_commision(self):
if self.commision_lines:
raise UserError('Line sudah ada, tidak bisa di generate ulang')
if self.commision_type == 'fee':
self._generate_customer_commision_fee()
else:
self._generate_customer_commision_rebate()
context = self.env.context.copy()
context.update({'_onchange_commision_amt': False})
self.env.context = context
self._onchange_partner_ids()
# self._onchange_commision_percent()
# self._onchange_commision_amt()
def _generate_customer_commision_rebate(self):
for rec in self:
# partners = rec.partner_ids.child_ids + rec.partner_ids
partners = rec.partner_ids
for partner in partners:
brand = [int(brand) for brand in rec.brand_ids]
where = [
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
('move_id.is_customer_commision', '=', False),
('move_id.amount_residual_signed', '=', 0),
('move_id.partner_id.id', '=', partner.id),
('move_id.invoice_date', '>=', self.date_from),
('move_id.invoice_date', '<=', self.date_to),
('product_id.x_manufacture', 'in', brand),
('exclude_from_invoice_tab', '=', False),
]
invoice_lines = self.env['account.move.line'].search(where, order='id')
for invoice_line in invoice_lines:
tax = invoice_line.price_total - invoice_line.price_subtotal
self.env['customer.commision.line'].create([{
'customer_commision_id': self.id,
'partner_id': invoice_line.move_id.partner_id.id,
'invoice_id': invoice_line.move_id.id,
'state': invoice_line.move_id.state,
'product_id': invoice_line.product_id.id,
'dpp': invoice_line.price_subtotal,
'tax': tax,
'total': invoice_line.price_total
}])
return
def _generate_customer_commision_fee(self):
for rec in self:
# partners = rec.partner_ids.child_ids + rec.partner_ids
partners = rec.partner_ids
for partner in partners:
where = [
('move_type', '=', 'out_invoice'),
('state', '=', 'posted'),
('is_customer_commision', '=', False),
('amount_residual_signed', '=', 0),
('partner_id.id', '=', partner.id),
('invoice_date', '>=', self.date_from),
('invoice_date', '<=', self.date_to),
]
invoices = self.env['account.move'].search(where, order='id')
for invoice in invoices:
self.env['customer.commision.line'].create([{
'customer_commision_id': self.id,
'partner_id': invoice.partner_id.id,
'invoice_id': invoice.id,
'state': invoice.state,
'dpp': invoice.amount_untaxed_signed,
'tax': invoice.amount_tax_signed,
'total': invoice.amount_total_signed
}])
return
class CustomerCommisionLine(models.Model):
_name = 'customer.commision.line'
_order = 'id'
customer_commision_id = fields.Many2one('customer.commision', string='Ref', required=True, ondelete='cascade', copy=False)
invoice_id = fields.Many2one('account.move', string='Invoice')
partner_id = fields.Many2one('res.partner', string='Customer')
state = fields.Char(string='InvStatus')
dpp = fields.Float(string='DPP')
tax = fields.Float(string='TaxAmt')
total = fields.Float(string='Total')
total_percent_margin = fields.Float('Total Margin', related='invoice_id.sale_id.total_percent_margin')
total_margin_excl_third_party = fields.Float('Before Margin', related='invoice_id.sale_id.total_margin_excl_third_party')
product_id = fields.Many2one('product.product', string='Product')
sale_order_id = fields.Many2one('sale.order', string='Sale Order', related='invoice_id.sale_id')
class AccountMove(models.Model):
_inherit = 'account.move'
is_customer_commision = fields.Boolean(string='Customer Commision Used')
|