mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
[MIG] hr_commission: migrate to Odoo 14.0
This commit is contained in:
@@ -3,12 +3,13 @@
|
|||||||
{
|
{
|
||||||
'name': 'Hibou Commissions',
|
'name': 'Hibou Commissions',
|
||||||
'author': 'Hibou Corp. <hello@hibou.io>',
|
'author': 'Hibou Corp. <hello@hibou.io>',
|
||||||
'version': '13.0.1.0.1',
|
'version': '14.0.1.0.0',
|
||||||
'category': 'Accounting/Commissions',
|
'category': 'Accounting/Commissions',
|
||||||
'license': 'OPL-1',
|
'license': 'OPL-1',
|
||||||
'website': 'https://hibou.io/',
|
'website': 'https://hibou.io/',
|
||||||
'depends': [
|
'depends': [
|
||||||
# 'account_invoice_margin', # optional
|
# 'account_invoice_margin', # optional
|
||||||
|
'account',
|
||||||
'hr_contract',
|
'hr_contract',
|
||||||
],
|
],
|
||||||
'data': [
|
'data': [
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class AccountMove(models.Model):
|
|||||||
def amount_for_commission(self):
|
def amount_for_commission(self):
|
||||||
# TODO Should toggle in Config Params
|
# TODO Should toggle in Config Params
|
||||||
if hasattr(self, 'margin') and self.company_id.commission_amount_type == 'on_invoice_margin':
|
if hasattr(self, 'margin') and self.company_id.commission_amount_type == 'on_invoice_margin':
|
||||||
sign = -1 if self.type in ['in_refund', 'out_refund'] else 1
|
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
|
||||||
return self.margin * sign
|
return self.margin * sign
|
||||||
return self.amount_total_signed
|
return self.amount_total_signed
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,6 @@ class Commission(models.Model):
|
|||||||
commission.amount = amount
|
commission.amount = amount
|
||||||
|
|
||||||
|
|
||||||
@api.model
|
|
||||||
def create(self, values):
|
def create(self, values):
|
||||||
res = super(Commission, self).create(values)
|
res = super(Commission, self).create(values)
|
||||||
res._compute_amount()
|
res._compute_amount()
|
||||||
@@ -102,12 +101,10 @@ class Commission(models.Model):
|
|||||||
def _filter_source_moves_for_creation(self, moves):
|
def _filter_source_moves_for_creation(self, moves):
|
||||||
return moves.filtered(lambda i: i.user_id and not i.commission_ids)
|
return moves.filtered(lambda i: i.user_id and not i.commission_ids)
|
||||||
|
|
||||||
@api.model
|
|
||||||
def _commissions_to_confirm(self, moves):
|
def _commissions_to_confirm(self, moves):
|
||||||
commissions = moves.mapped('commission_ids')
|
commissions = moves.mapped('commission_ids')
|
||||||
return commissions.filtered(lambda c: c.state != 'cancel' and not c.move_id)
|
return commissions.filtered(lambda c: c.state != 'cancel' and not c.move_id)
|
||||||
|
|
||||||
@api.model
|
|
||||||
def invoice_validated(self, moves):
|
def invoice_validated(self, moves):
|
||||||
employee_obj = self.env['hr.employee'].sudo()
|
employee_obj = self.env['hr.employee'].sudo()
|
||||||
commission_obj = self.sudo()
|
commission_obj = self.sudo()
|
||||||
@@ -155,7 +152,6 @@ class Commission(models.Model):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@api.model
|
|
||||||
def invoice_paid(self, moves):
|
def invoice_paid(self, moves):
|
||||||
commissions = self._commissions_to_confirm(moves)
|
commissions = self._commissions_to_confirm(moves)
|
||||||
commissions.sudo().action_confirm()
|
commissions.sudo().action_confirm()
|
||||||
@@ -172,7 +168,7 @@ class Commission(models.Model):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
journal = commission.company_id.commission_journal_id
|
journal = commission.company_id.commission_journal_id
|
||||||
if not journal or not journal.default_debit_account_id or not journal.default_credit_account_id:
|
if not journal or not journal.default_account_id:
|
||||||
raise UserError('Commission Journal not configured.')
|
raise UserError('Commission Journal not configured.')
|
||||||
|
|
||||||
liability_account = commission.company_id.commission_liability_id
|
liability_account = commission.company_id.commission_liability_id
|
||||||
@@ -186,7 +182,7 @@ class Commission(models.Model):
|
|||||||
# Already paid.
|
# Already paid.
|
||||||
payments = commission.source_move_id._get_reconciled_payments()
|
payments = commission.source_move_id._get_reconciled_payments()
|
||||||
if payments:
|
if payments:
|
||||||
date = max(payments.mapped('payment_date'))
|
date = max(payments.mapped('date'))
|
||||||
if commission.accounting_date:
|
if commission.accounting_date:
|
||||||
date = commission.accounting_date
|
date = commission.accounting_date
|
||||||
|
|
||||||
@@ -198,7 +194,7 @@ class Commission(models.Model):
|
|||||||
'date': date,
|
'date': date,
|
||||||
'ref': ref,
|
'ref': ref,
|
||||||
'journal_id': journal.id,
|
'journal_id': journal.id,
|
||||||
'type': 'entry',
|
'move_type': 'entry',
|
||||||
'line_ids': [
|
'line_ids': [
|
||||||
(0, 0, {
|
(0, 0, {
|
||||||
'name': ref,
|
'name': ref,
|
||||||
@@ -210,7 +206,7 @@ class Commission(models.Model):
|
|||||||
(0, 0, {
|
(0, 0, {
|
||||||
'name': ref,
|
'name': ref,
|
||||||
'partner_id': commission.employee_id.address_home_id.id,
|
'partner_id': commission.employee_id.address_home_id.id,
|
||||||
'account_id': journal.default_credit_account_id.id if commission.amount > 0.0 else journal.default_debit_account_id.id,
|
'account_id': journal.default_account_id.id,
|
||||||
'credit': 0.0 if commission.amount > 0.0 else -commission.amount,
|
'credit': 0.0 if commission.amount > 0.0 else -commission.amount,
|
||||||
'debit': commission.amount if commission.amount > 0.0 else 0.0,
|
'debit': commission.amount if commission.amount > 0.0 else 0.0,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
from odoo.tests import common
|
from odoo.tests import common
|
||||||
|
|
||||||
# TODO Tests won't pass without `sale`
|
|
||||||
# Tests should be refactored to not build sale orders
|
|
||||||
# to invoice, just create invoices directly.
|
|
||||||
|
|
||||||
|
|
||||||
class TestCommission(common.TransactionCase):
|
class TestCommission(common.TransactionCase):
|
||||||
|
|
||||||
@@ -44,34 +40,32 @@ class TestCommission(common.TransactionCase):
|
|||||||
'admin_commission_rate': admin_commission_rate,
|
'admin_commission_rate': admin_commission_rate,
|
||||||
'state': 'open', # if not "Running" then no automatic selection when Payslip is created in 11.0
|
'state': 'open', # if not "Running" then no automatic selection when Payslip is created in 11.0
|
||||||
})
|
})
|
||||||
|
|
||||||
def _createInvoiceableSaleOrder(self, user):
|
def _create_invoice(self, user):
|
||||||
product = self.env.ref('sale.advance_product_0')
|
# Create invoice
|
||||||
partner = self.env.ref('base.res_partner_12')
|
invoice = self.env['account.move'].create({
|
||||||
sale = self.env['sale.order'].create({
|
'move_type': 'out_invoice',
|
||||||
'partner_id': partner.id,
|
'partner_id': self.env.ref("base.res_partner_2").id,
|
||||||
'user_id': user.id,
|
'currency_id': self.env.ref('base.USD').id,
|
||||||
'order_line': [(0, 0, {
|
'invoice_date': '2020-12-11',
|
||||||
'name': 'test deposit',
|
'invoice_user_id': user.id,
|
||||||
'product_id': product.id,
|
'invoice_line_ids': [(0, 0, {
|
||||||
'product_uom_qty': 1.0,
|
'product_id': self.env.ref("product.product_product_4").id,
|
||||||
'product_uom': product.uom_id.id,
|
'quantity': 1,
|
||||||
'price_unit': 5.0,
|
'price_unit': 5.0,
|
||||||
})]
|
})],
|
||||||
})
|
})
|
||||||
self.assertEqual(sale.user_id, user)
|
|
||||||
sale.action_confirm()
|
self.assertEqual(invoice.invoice_user_id.id, user.id)
|
||||||
self.assertTrue(sale.state in ('sale', 'done'))
|
self.assertEqual(invoice.payment_state, 'not_paid')
|
||||||
self.assertEqual(sale.invoice_status, 'to invoice')
|
return invoice
|
||||||
return sale
|
|
||||||
|
|
||||||
def test_commission(self):
|
def test_commission(self):
|
||||||
# find and configure company commissions journal
|
# find and configure company commissions journal
|
||||||
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
||||||
self.assertTrue(commission_journal)
|
self.assertTrue(commission_journal)
|
||||||
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
||||||
commission_journal.default_debit_account_id = expense_account
|
commission_journal.default_account_id = expense_account
|
||||||
commission_journal.default_credit_account_id = expense_account
|
|
||||||
self.env.user.company_id.commission_journal_id = commission_journal
|
self.env.user.company_id.commission_journal_id = commission_journal
|
||||||
|
|
||||||
coach = self._createEmployee(self.browse_ref('base.user_root'))
|
coach = self._createEmployee(self.browse_ref('base.user_root'))
|
||||||
@@ -83,12 +77,12 @@ class TestCommission(common.TransactionCase):
|
|||||||
|
|
||||||
contract = self._createContract(emp, 5.0)
|
contract = self._createContract(emp, 5.0)
|
||||||
|
|
||||||
so = self._createInvoiceableSaleOrder(user)
|
inv = self._create_invoice(user)
|
||||||
inv = so._create_invoices()
|
|
||||||
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
||||||
inv.action_post() # validate
|
inv.action_post() # validate
|
||||||
self.assertEqual(inv.state, 'posted')
|
self.assertEqual(inv.state, 'posted')
|
||||||
self.assertEqual(inv.invoice_payment_state, 'not_paid')
|
self.assertEqual(inv.payment_state, 'not_paid')
|
||||||
self.assertTrue(inv.commission_ids, 'Commissions not created when invoice is validated.')
|
self.assertTrue(inv.commission_ids, 'Commissions not created when invoice is validated.')
|
||||||
|
|
||||||
user_commission = inv.commission_ids.filtered(lambda c: c.employee_id.id == emp.id)
|
user_commission = inv.commission_ids.filtered(lambda c: c.employee_id.id == emp.id)
|
||||||
@@ -114,11 +108,12 @@ class TestCommission(common.TransactionCase):
|
|||||||
'currency_id': inv.currency_id.id,
|
'currency_id': inv.currency_id.id,
|
||||||
'journal_id': pay_journal.id,
|
'journal_id': pay_journal.id,
|
||||||
})
|
})
|
||||||
payment.post()
|
payment.action_post()
|
||||||
|
|
||||||
|
receivable_line = payment.move_id.line_ids.filtered('credit')
|
||||||
|
|
||||||
receivable_line = payment.move_line_ids.filtered('credit')
|
|
||||||
inv.js_assign_outstanding_line(receivable_line.id)
|
inv.js_assign_outstanding_line(receivable_line.id)
|
||||||
self.assertEqual(inv.invoice_payment_state, 'paid', 'Invoice is not paid.')
|
self.assertEqual(inv.payment_state, 'in_payment', 'Invoice is not paid.')
|
||||||
|
|
||||||
user_commission = inv.commission_ids.filtered(lambda c: c.employee_id.id == emp.id)
|
user_commission = inv.commission_ids.filtered(lambda c: c.employee_id.id == emp.id)
|
||||||
self.assertEqual(user_commission.state, 'done', 'Commission is not done.')
|
self.assertEqual(user_commission.state, 'done', 'Commission is not done.')
|
||||||
@@ -150,8 +145,7 @@ class TestCommission(common.TransactionCase):
|
|||||||
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
||||||
self.assertTrue(commission_journal)
|
self.assertTrue(commission_journal)
|
||||||
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
||||||
commission_journal.default_debit_account_id = expense_account
|
commission_journal.default_account_id = expense_account
|
||||||
commission_journal.default_credit_account_id = expense_account
|
|
||||||
self.env.user.company_id.commission_journal_id = commission_journal
|
self.env.user.company_id.commission_journal_id = commission_journal
|
||||||
|
|
||||||
|
|
||||||
@@ -164,8 +158,7 @@ class TestCommission(common.TransactionCase):
|
|||||||
|
|
||||||
contract = self._createContract(emp, 5.0)
|
contract = self._createContract(emp, 5.0)
|
||||||
|
|
||||||
so = self._createInvoiceableSaleOrder(user)
|
inv = self._create_invoice(user)
|
||||||
inv = so._create_invoices()
|
|
||||||
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
||||||
inv.action_post() # validate
|
inv.action_post() # validate
|
||||||
self.assertEqual(inv.state, 'posted')
|
self.assertEqual(inv.state, 'posted')
|
||||||
@@ -188,8 +181,7 @@ class TestCommission(common.TransactionCase):
|
|||||||
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
commission_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1)
|
||||||
self.assertTrue(commission_journal)
|
self.assertTrue(commission_journal)
|
||||||
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
expense_account = self.env.ref('l10n_generic_coa.1_expense')
|
||||||
commission_journal.default_debit_account_id = expense_account
|
commission_journal.default_account_id = expense_account
|
||||||
commission_journal.default_credit_account_id = expense_account
|
|
||||||
self.env.user.company_id.commission_journal_id = commission_journal
|
self.env.user.company_id.commission_journal_id = commission_journal
|
||||||
|
|
||||||
|
|
||||||
@@ -202,8 +194,6 @@ class TestCommission(common.TransactionCase):
|
|||||||
|
|
||||||
contract = self._createContract(emp, 5.0)
|
contract = self._createContract(emp, 5.0)
|
||||||
|
|
||||||
so = self._createInvoiceableSaleOrder(user)
|
|
||||||
|
|
||||||
# Create and set commission structure
|
# Create and set commission structure
|
||||||
commission_structure = self.env['hr.commission.structure'].create({
|
commission_structure = self.env['hr.commission.structure'].create({
|
||||||
'name': 'Test Structure',
|
'name': 'Test Structure',
|
||||||
@@ -212,9 +202,9 @@ class TestCommission(common.TransactionCase):
|
|||||||
(0, 0, {'employee_id': coach.id, 'rate': 0.0}), # This means it will use the coach's contract normal rate
|
(0, 0, {'employee_id': coach.id, 'rate': 0.0}), # This means it will use the coach's contract normal rate
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
so.partner_id.commission_structure_id = commission_structure
|
|
||||||
|
|
||||||
inv = so._create_invoices()
|
inv = self._create_invoice(user)
|
||||||
|
inv.partner_id.commission_structure_id = commission_structure
|
||||||
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
||||||
inv.action_post() # validate
|
inv.action_post() # validate
|
||||||
self.assertEqual(inv.state, 'posted')
|
self.assertEqual(inv.state, 'posted')
|
||||||
|
|||||||
Reference in New Issue
Block a user