summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/commision.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_custom/models/commision.py')
-rw-r--r--indoteknik_custom/models/commision.py270
1 files changed, 219 insertions, 51 deletions
diff --git a/indoteknik_custom/models/commision.py b/indoteknik_custom/models/commision.py
index 6920154a..eeaa8efc 100644
--- a/indoteknik_custom/models/commision.py
+++ b/indoteknik_custom/models/commision.py
@@ -1,7 +1,10 @@
-from odoo import models, api, fields
+from odoo import models, api, fields, _
from odoo.exceptions import UserError
from datetime import datetime
+# import datetime
import logging
+from terbilang import Terbilang
+import pytz
_logger = logging.getLogger(__name__)
@@ -12,8 +15,10 @@ class CustomerRebate(models.Model):
_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")
+ 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')
@@ -35,7 +40,7 @@ class CustomerRebate(models.Model):
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)
@@ -52,18 +57,18 @@ class CustomerRebate(models.Model):
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),
+ ('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:
@@ -74,13 +79,13 @@ class CustomerRebate(models.Model):
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),
+ ('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:
@@ -91,13 +96,13 @@ class CustomerRebate(models.Model):
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),
+ ('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:
@@ -108,13 +113,13 @@ class CustomerRebate(models.Model):
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),
+ ('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:
@@ -122,6 +127,22 @@ class CustomerRebate(models.Model):
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'
@@ -134,29 +155,132 @@ class CustomerCommision(models.Model):
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)
+ commision_lines = fields.One2many('customer.commision.line', 'customer_commision_id', string='Lines',
+ auto_join=True)
status = fields.Selection([
- ('pengajuan1', 'Menunggu Approval Marketing'),
- ('pengajuan2', 'Menunggu Approval Pimpinan'),
- ('approved', 'Approved')
- ], string='Status', copy=False, readonly=True, tracking=3)
+ ('draft', 'Draft'),
+ ('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([
+ ('draft', 'Draft'),
+ ('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)
- account_name = fields.Char(string='Account Name', tracking=3)
- bank_account = fields.Char(string='Account No', tracking=3)
+ 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, default=lambda self: self.env.user,
+ domain=lambda self: [
+ ('groups_id', 'in', self.env.ref('sales_team.group_sale_salesman').id)])
+
+ 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)
+
+ # get partner ids so it can be grouped by
+ @api.model
+ def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
+ if 'partner_ids' in groupby:
+ # Get all records matching the domain
+ records = self.search(domain)
+
+ # Create groups for each partner
+ groups = {}
+ for record in records:
+ for partner in record.partner_ids:
+ if partner.id not in groups:
+ groups[partner.id] = {
+ 'partner_ids': partner,
+ 'records': self.env['customer.commision']
+ }
+ groups[partner.id]['records'] |= record
+
+ # Format the result
+ result = []
+ for partner_id, group_data in groups.items():
+ partner = group_data['partner_ids']
+ record_ids = group_data['records'].ids
+
+ # Create the domain
+ group_domain = [('id', 'in', record_ids)]
+ if domain:
+ group_domain = ['&'] + domain + group_domain
+
+ result.append({
+ 'partner_ids': (partner.id, partner.display_name),
+ 'partner_ids_count': len(record_ids),
+ '__domain': group_domain,
+ '__count': len(record_ids),
+ })
+
+ return result
+
+ return super(CustomerCommision, self).read_group(domain, fields, groupby, offset, limit, orderby, lazy)
+
+ 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?
@@ -182,22 +306,25 @@ class CustomerCommision(models.Model):
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):
+ """
+ Constrain to update commision percent from commision amount
+ """
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
@@ -219,19 +346,54 @@ class CustomerCommision(models.Model):
result = super(CustomerCommision, self).create(vals)
return result
- def action_confirm_customer_commision(self):#add 2 step approval
- if not self.status:
+ def action_confirm_customer_commision(self):
+ jakarta_tz = pytz.timezone('Asia/Jakarta')
+ now = datetime.now(jakarta_tz)
+
+ now_naive = now.replace(tzinfo=None)
+
+ if not self.status or self.status == 'draft':
self.status = 'pengajuan1'
- elif self.status == 'pengajuan1' and self.env.user.id == 19:
+ elif self.status == 'pengajuan1' and self.env.user.is_sales_manager:
self.status = 'pengajuan2'
- elif self.status == 'pengajuan2' and self.env.user.is_leader:
+ self.approved_by = (self.approved_by + ', ' if self.approved_by else '') + self.env.user.name
+ self.date_approved_sales = now_naive
+ 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_naive
+ 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_naive
+ 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_naive
+ 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')
@@ -249,7 +411,7 @@ class CustomerCommision(models.Model):
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:
@@ -324,11 +486,13 @@ class CustomerCommision(models.Model):
}])
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)
+ 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')
@@ -336,7 +500,11 @@ class CustomerCommisionLine(models.Model):
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'