~argilsoft/+junk/pexego-addons-7.0

« back to all changes in this revision

Viewing changes to sale_commission/sale_order.py

  • Committer: Israel CA
  • Date: 2013-10-22 21:54:35 UTC
  • Revision ID: israel.cruz@hesatecnica.com-20131022215435-jo9l2iq7di64bqjn
Importación inicial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
"""Modificamos las ventas para incluir el comportamiento de comisiones"""
 
24
 
 
25
from osv import fields, osv
 
26
from tools.translate import _
 
27
 
 
28
class sale_order_agent(osv.osv):
 
29
    _name = "sale.order.agent"
 
30
    _rec_name = "agent_id"
 
31
 
 
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 = {}
 
35
        res = []
 
36
        for obj in self.browse(cr, uid, ids):
 
37
            res.append((obj.id, obj.agent_id.name))
 
38
        return res
 
39
 
 
40
    _columns = {
 
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=''),
 
44
    }
 
45
 
 
46
    def onchange_agent_id(self, cr, uid, ids, agent_id):
 
47
        """al cambiar el agente cargamos sus comisión"""
 
48
        result = {}
 
49
        v = {}
 
50
        if agent_id:
 
51
            agent = self.pool.get('sale.agent').browse(cr, uid, agent_id)
 
52
            v['commission_id'] = agent.commission.id
 
53
 
 
54
        result['value'] = v
 
55
        return result
 
56
 
 
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"""
 
59
        result = {}
 
60
 
 
61
        if commission_id:
 
62
            partner_commission = self.pool.get('commission').browse(cr, uid, commission_id)
 
63
            if partner_commission.sections:
 
64
                if agent_id:
 
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.')
 
70
        return result
 
71
 
 
72
sale_order_agent()
 
73
 
 
74
class sale_order(osv.osv):
 
75
    """Modificamos las ventas para incluir el comportamiento de comisiones"""
 
76
 
 
77
    _inherit = "sale.order"
 
78
 
 
79
    _columns = {
 
80
        'sale_agent_ids':fields.one2many('sale.order.agent', 'sale_id', 'Agents', states={'draft': [('readonly', False)]})
 
81
    }
 
82
 
 
83
    def create(self, cr, uid, values, context=None):
 
84
        """
 
85
        """
 
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})
 
90
        return res
 
91
 
 
92
    def write(self, cr, uid, ids, values, context=None):
 
93
        """
 
94
        """
 
95
 
 
96
        if 'sale_agent_ids' in values:
 
97
            for sale_order_agent in values['sale_agent_ids']:
 
98
                for id in ids:
 
99
                    if sale_order_agent[2]:
 
100
                        sale_order_agent[2]['sale_id']=id
 
101
                    else:
 
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)
 
104
 
 
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"""
 
107
        sale_agent_ids=[]
 
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')
 
111
            if ids:
 
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:
 
115
                vals={
 
116
                    'agent_id':partner_agent.agent_id.id,
 
117
                    'commission_id':partner_agent.commission_id.id,
 
118
                    #'sale_id':ids
 
119
                }
 
120
                if ids:
 
121
                    for id in ids:
 
122
                        vals['sale_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
 
126
        return res
 
127
 
 
128
 
 
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)
 
132
 
 
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]] })
 
138
        return res
 
139
 
 
140
sale_order()
 
141
 
 
142
 
 
143
class sale_order_line(osv.osv):
 
144
    """Modificamos las lineas ventas para incluir las comisiones en las facturas creadas desde ventas"""
 
145
 
 
146
    _inherit = "sale.order.line"
 
147
 
 
148
 
 
149
    def invoice_line_create(self, cr, uid, ids, context=None):
 
150
        if context is None:
 
151
            context = {}
 
152
 
 
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:
 
159
                    vals = {
 
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,
 
163
                        'settled': False
 
164
                    }
 
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])
 
167
        return res
 
168
 
 
169
sale_order_line()
 
170
 
 
171
 
 
172