1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
23
"""Modificamos las ventas para incluir el comportamiento de comisiones"""
25
from osv import fields, osv
26
from tools.translate import _
28
class sale_order_agent(osv.osv):
29
_name = "sale.order.agent"
30
_rec_name = "agent_id"
32
def name_get(self, cr, uid, ids, context=None):
33
"""devuelve como nombre del agente del partner el nombre del agente"""
34
if context is None: context = {}
36
for obj in self.browse(cr, uid, ids):
37
res.append((obj.id, obj.agent_id.name))
41
'sale_id':fields.many2one('sale.order', 'Sale order', required=False, ondelete='cascade', help=''),
42
'agent_id':fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''),
43
'commission_id':fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', help=''),
46
def onchange_agent_id(self, cr, uid, ids, agent_id):
47
"""al cambiar el agente cargamos sus comisión"""
51
agent = self.pool.get('sale.agent').browse(cr, uid, agent_id)
52
v['commission_id'] = agent.commission.id
57
def onchange_commission_id(self, cr, uid, ids, agent_id=False, commission_id=False):
58
"""al cambiar la comisión comprobamos la selección"""
62
partner_commission = self.pool.get('commission').browse(cr, uid, commission_id)
63
if partner_commission.sections:
65
agent = self.pool.get('sale.agent').browse(cr, uid, agent_id)
66
if agent.commission.id != partner_commission.id:
67
result['warning'] = {}
68
result['warning']['title'] = _('Fee installments!')
69
result['warning']['message'] = _('A commission has been assigned by sections that does not match that defined for the agent by default, so that these sections shall apply only on this bill.')
74
class sale_order(osv.osv):
75
"""Modificamos las ventas para incluir el comportamiento de comisiones"""
77
_inherit = "sale.order"
80
'sale_agent_ids':fields.one2many('sale.order.agent', 'sale_id', 'Agents', states={'draft': [('readonly', False)]})
83
def create(self, cr, uid, values, context=None):
86
res = super(sale_order, self).create(cr, uid, values, context=context)
87
if 'sale_agent_ids' in values:
88
for sale_order_agent in values['sale_agent_ids']:
89
self.pool.get('sale.order.agent').write(cr, uid, sale_order_agent[1], {'sale_id':res})
92
def write(self, cr, uid, ids, values, context=None):
96
if 'sale_agent_ids' in values:
97
for sale_order_agent in values['sale_agent_ids']:
99
if sale_order_agent[2]:
100
sale_order_agent[2]['sale_id']=id
102
self.pool.get('sale.order.agent').unlink(cr, uid, sale_order_agent[1])
103
return super(sale_order, self).write(cr, uid, ids, values, context=context)
105
def onchange_partner_id(self, cr, uid, ids, part):
106
"""heredamos el evento de cambio del campo partner_id para actualizar el campo agent_id"""
108
res = super(sale_order, self).onchange_partner_id(cr, uid, ids, part)
109
if res.get('value', False) and part:
110
sale_order_agent = self.pool.get('sale.order.agent')
112
sale_order_agent.unlink(cr, uid, sale_order_agent.search(cr, uid ,[('sale_id','=',ids)]))
113
partner = self.pool.get('res.partner').browse(cr, uid, part)
114
for partner_agent in partner.commission_ids:
116
'agent_id':partner_agent.agent_id.id,
117
'commission_id':partner_agent.commission_id.id,
123
sale_agent_id=sale_order_agent.create(cr, uid, vals)
124
sale_agent_ids.append(int(sale_agent_id))
125
res['value']['sale_agent_ids'] = sale_agent_ids
129
def action_ship_create(self, cr, uid, ids, context=None):
130
"""extend this method to add agent_id to picking"""
131
res = super(sale_order, self).action_ship_create(cr, uid, ids, context=context)
133
for order in self.browse(cr, uid, ids):
134
pickings = [x.id for x in order.picking_ids]
135
agents = [x.agent_id.id for x in order.sale_agent_ids]
136
if pickings and agents:
137
self.pool.get('stock.picking').write(cr, uid, pickings, {'agent_ids': [[6, 0, agents]] })
143
class sale_order_line(osv.osv):
144
"""Modificamos las lineas ventas para incluir las comisiones en las facturas creadas desde ventas"""
146
_inherit = "sale.order.line"
149
def invoice_line_create(self, cr, uid, ids, context=None):
153
res = super(sale_order_line, self).invoice_line_create(cr, uid, ids, context)
154
so_ref = self.browse(cr,uid,ids)[0].order_id
155
for so_agent_id in so_ref.sale_agent_ids:
156
inv_lines = self.pool.get('account.invoice.line').browse(cr, uid, res)
157
for inv_line in inv_lines:
158
if inv_line.product_id and inv_line.product_id.commission_exent != True:
160
'invoice_line_id': inv_line.id,
161
'agent_id': so_agent_id.agent_id.id,
162
'commission_id': so_agent_id.commission_id.id,
165
line_agent_id=self.pool.get('invoice.line.agent').create(cr, uid, vals)
166
self.pool.get('invoice.line.agent').calculate_commission(cr, uid, [line_agent_id])