Skip to content
Permalink
Browse files

[IMP] website_crm_partner_assign_commissions: add commission for rese…

…llers
  • Loading branch information...
RomainLibert committed Jan 31, 2019
1 parent 49bc575 commit ba21f28420b335c50e66d3ac92050a65cab6a3c9
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import models
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

{
'name': 'Resellers Commissions',
'category': 'Website',
'summary': 'Configure resellers commissions on product sale',
'version': '1.0',
'description': """
This module allows to configure commissions for the resellers.
""",
'depends': ['website_crm_partner_assign', 'purchase', 'sale_management'],
'data': [
'data/product_data.xml',
'security/ir.model.access.csv',
'views/account_invoice_views.xml',
'views/res_partner_views.xml',
'views/sale_order_views.xml',
],
'demo': [],
'qweb': [],
'installable': True,
'auto_install': True,
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>

<record id="product_commission" model="product.product">
<field name="name">Commission</field>
<field name="purchase_ok" eval="True"/>
</record>

</data>
</odoo>
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import account_invoice
from . import res_partner
from . import sale_order
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, _
from odoo.tools import float_compare


class AccountInvoice(models.Model):
_inherit = 'account.invoice'

reseller_id = fields.Many2one('res.partner', 'Reseller', help='This field is used to track the reseller in order to generate commisions')
purchase_order_line_id = fields.Many2one('purchase.order.line', 'Reseller Purchase Order line')

def _make_commissions(self):
"""
Adds a line to a purchase order for reseller_id containing the due commissions for the invoice
The line is only added the first time we call this method (called on reconcile)
"""
self.ensure_one()

if not self.reseller_id or self.purchase_order_line_id:
return

total = 0
for commission in self.reseller_id.grade_id.commission_ids:
lines = self.invoice_line_ids.filtered(lambda line: line.product_id.product_tmpl_id.id in commission.product_ids.ids)
total += sum(line.price_subtotal * commission.percentage / 100 for line in lines)

if total:
# Find the purchase order corresponding to the current reseller or create a new one
purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.reseller_id.id), ('state', '=', 'draft')], limit=1)
if not purchase_order:
purchase_order = self.env['purchase.order'].create({
'name': _('Purchase order for %s') % self.reseller_id.display_name,
'partner_id': self.reseller_id.id,
})

purchase_line = self.env['purchase.order.line'].create({
'name': '%s %s' % (self.display_name, self.partner_id.display_name),
'product_id': self.env.ref('website_crm_partner_assign_commissions.product_commission').id,
'product_qty': 1,
'price_unit': total,
'product_uom': self.env.ref('uom.product_uom_unit').id,
'date_planned': fields.Datetime.now(),
'order_id': purchase_order.id,
})

self.purchase_order_line_id = purchase_line


class AccountMoveLine(models.Model):
_inherit = 'account.move.line'

def reconcile(self, writeoff_acc_id=False, writeoff_journal_id=False):
res = super(AccountMoveLine, self).reconcile(writeoff_acc_id=writeoff_acc_id, writeoff_journal_id=writeoff_journal_id)

if any(line.invoice_id for line in self):
# We check if any move_line is fully reconciled
account_move_ids = [l.move_id.id for l in self if float_compare(l.move_id.matched_percentage, 1, precision_digits=5) == 0]

if account_move_ids:
move_lines = self.filtered(lambda l: l.invoice_id)
is_refund = any(move_line.invoice_id.type in ['in_refund', 'out_refund'] for move_line in move_lines)

for move_line in move_lines:
if is_refund:
move_line.invoice_id.purchase_order_line_id.unlink()
else:
move_line.invoice_id._make_commissions()

return res
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


class ResPartnerCommission(models.Model):
_name = 'res.partner.commission'
_description = 'Reseller commissions'

grade_id = fields.Many2one('res.partner.grade', 'Grade', required=True)
percentage = fields.Float('Commission Percentage', required=True)
product_ids = fields.Many2many('product.template', required=True)


class ResPartnerGrade(models.Model):
_inherit = 'res.partner.grade'

commission_ids = fields.One2many('res.partner.commission', 'grade_id', string="Commissions")
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class SaleOrder(models.Model):
_inherit = 'sale.order'

reseller_id = fields.Many2one('res.partner', 'Reseller', help='This field is used to track the reseller in order to generate commisions')

@api.multi
def _prepare_invoice(self):
res = super(SaleOrder, self)._prepare_invoice()
res.update({
'reseller_id': self.reseller_id.id,
})
return res
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


class SaleSubscription(models.Model):
_inherit = "sale.subscription"

reseller_id = fields.Many2one('res.partner', 'Reseller', help='This field is used to track the reseller in order to generate commisions')

def _prepare_invoice_data(self):
res = super(SaleSubscription, self)._prepare_invoice_data()
res.update({
'reseller_id': self.reseller_id,
})
return res
@@ -0,0 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_res_partner_commissions_users,res.partner.commission,model_res_partner_commission,base.group_user,1,0,0,0
access_res_partner_commissions_partner_manager,res.partner.commission,model_res_partner_commission,base.group_partner_manager,1,1,1,1
access_res_partner_commissions_sale_manager,res.partner.commission,model_res_partner_commission,sales_team.group_sale_manager,1,1,1,1
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import test_commissions
Oops, something went wrong.

0 comments on commit ba21f28

Please sign in to comment.
You can’t perform that action at this time.