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 8e2f9a4 commit a848fc0938582f2411c9f9d73d4178fe9ca7a568
@@ -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,10 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>

<record id="product_commission" model="product.product">
<field name="name">Commission</field>
</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,62 @@
# -*- 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_id = fields.Many2one('purchase.order', 'Reseller Purchase Order')

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:
return

subtotals = []
for commission in self.env['res.partner.commission'].search([('grade_id', '=', self.reseller_id.grade_id.id)]):
lines = self.invoice_line_ids.filtered(lambda line: line.product_id.product_tmpl_id.id in commission.product_ids.ids)
subtotals.append(sum(line.price_subtotal * commission.percentage / 100 for line in lines))
total = sum(subtotals)

if not self.purchase_order_id and 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_order.write({
'order_line': [(0, 0, {
'name': '%s %s' % (self.display_name, self.partner_id.display_name),
'product_id': self.env.ref('website_crm_partner_assign.product_commission').id,
'product_qty': 1,
'price_unit': total,
'product_uom': self.env.ref('uom.product_uom_unit').id,
'date_planned': fields.Datetime.now(),
})]
})

self.purchase_order_id = purchase_order


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)
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:
for move_line in self.filtered(lambda l: l.invoice_id):
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')
percentage = fields.Float('Commission Percentage')
product_ids = fields.Many2many('product.template')


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,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 a848fc0

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