Merge lp:~savoirfairelinux-openerp/openerp-icm/bug1160750 into lp:~openerp-icm/openerp-icm/7.0
- bug1160750
- Merge into 7.0
Proposed by
Sandy Carter (http://www.savoirfairelinux.com)
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 12 | ||||
Proposed branch: | lp:~savoirfairelinux-openerp/openerp-icm/bug1160750 | ||||
Merge into: | lp:~openerp-icm/openerp-icm/7.0 | ||||
Diff against target: |
1633 lines (+504/-528) 12 files modified
sale_commission/__init__.py (+2/-3) sale_commission/__openerp__.py (+25/-27) sale_commission/invoice.py (+61/-56) sale_commission/partner.py (+7/-8) sale_commission/partner_agent.py (+43/-43) sale_commission/partner_view.xml (+1/-1) sale_commission/sale_agent.py (+30/-29) sale_commission/sale_order.py (+66/-64) sale_commission/settled.py (+235/-249) sale_commission/stock_picking.py (+23/-26) sale_commission/wizard/__init__.py (+1/-1) sale_commission/wizard/wizard_invoice.py (+10/-21) |
||||
To merge this branch: | bzr merge lp:~savoirfairelinux-openerp/openerp-icm/bug1160750 | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Maxime Chambreuil (http://www.savoirfairelinux.com) | test | Approve | |
Jenkins Savoir-faire Linux (community) | test | Approve | |
Guewen Baconnier @ Camptocamp | code review | Approve | |
Review via email: mp+199201@code.launchpad.net |
Commit message
Description of the change
Revision of code including pep8, optimization of pools, some comment fixes.
It would be good to translate non-English state names, strings, function names, and, especially comments.
There still is some strange code that I was unable to revise due to lack of original intent.
Revision 14 assures that context is used as per Bug #1160750
Please do test, as there may be some bugs left or some regression.
To post a comment you must log in.
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote : | # |
review:
Approve
(code review)
Revision history for this message
Jenkins Savoir-faire Linux (jenkins-a) : | # |
review:
Approve
(test)
Revision history for this message
Maxime Chambreuil (http://www.savoirfairelinux.com) (max3903) wrote : | # |
Sorry for the last review made with the wrong user.
review:
Approve
(test)
- 15. By Maxime Chambreuil (http://www.savoirfairelinux.com)
-
[FIX] onchange_
partner_ id() got an unexpected keyword argument 'context'
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'sale_commission/__init__.py' |
2 | --- sale_commission/__init__.py 2012-07-13 10:51:04 +0000 |
3 | +++ sale_commission/__init__.py 2013-12-23 18:02:44 +0000 |
4 | @@ -1,7 +1,7 @@ |
5 | # -*- coding: utf-8 -*- |
6 | ############################################################################## |
7 | # |
8 | -# OpenERP, Open Source Management Solution |
9 | +# OpenERP, Open Source Management Solution |
10 | # Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved |
11 | # $Id$ |
12 | # |
13 | @@ -20,7 +20,7 @@ |
14 | # |
15 | ############################################################################## |
16 | |
17 | -import saleagent |
18 | +import sale_agent |
19 | import invoice |
20 | import partner_agent |
21 | import partner |
22 | @@ -30,4 +30,3 @@ |
23 | import wizard |
24 | |
25 | # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
26 | - |
27 | |
28 | === modified file 'sale_commission/__openerp__.py' |
29 | --- sale_commission/__openerp__.py 2012-07-13 10:51:04 +0000 |
30 | +++ sale_commission/__openerp__.py 2013-12-23 18:02:44 +0000 |
31 | @@ -20,37 +20,35 @@ |
32 | # |
33 | ############################################################################## |
34 | { |
35 | - 'name' : 'Sales commissions', |
36 | + 'name': 'Sales commissions', |
37 | 'version': '1.0', |
38 | - 'author' : 'Pexego', |
39 | + 'author': 'Pexego', |
40 | "category": "Generic Modules/Sales & Purchases", |
41 | - 'depends' : [ |
42 | - 'base', |
43 | - 'account', |
44 | - 'product', |
45 | - 'sale', |
46 | - 'hr', |
47 | - 'stock' |
48 | - ], |
49 | + 'depends': [ |
50 | + 'base', |
51 | + 'account', |
52 | + 'product', |
53 | + 'sale', |
54 | + 'hr', |
55 | + 'stock' |
56 | + ], |
57 | 'description': 'Sales commissions', |
58 | - 'init_xml' : [], |
59 | - 'update_xml': [ |
60 | - 'security/ir.model.access.csv', |
61 | - 'saleagent_view.xml', |
62 | - 'partner_agent_view.xml', |
63 | - 'wizard/wizard_invoice.xml', |
64 | - 'partner_view.xml', |
65 | - 'settled_view.xml', |
66 | - 'invoice_view.xml', |
67 | - 'sale_order_view.xml', |
68 | - 'product_view.xml', |
69 | - 'stock_picking_view.xml', |
70 | - 'cc_commission_report.xml', |
71 | - ], |
72 | - 'demo_xml': [ |
73 | - ], |
74 | + 'data': [ |
75 | + 'security/ir.model.access.csv', |
76 | + 'sale_agent_view.xml', |
77 | + 'partner_agent_view.xml', |
78 | + 'wizard/wizard_invoice.xml', |
79 | + 'partner_view.xml', |
80 | + 'settled_view.xml', |
81 | + 'invoice_view.xml', |
82 | + 'sale_order_view.xml', |
83 | + 'product_view.xml', |
84 | + 'stock_picking_view.xml', |
85 | + 'cc_commission_report.xml', |
86 | + ], |
87 | + 'demo': [ |
88 | + ], |
89 | 'active': False, |
90 | 'installable': True |
91 | } |
92 | # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
93 | - |
94 | |
95 | === modified file 'sale_commission/invoice.py' |
96 | --- sale_commission/invoice.py 2013-05-03 16:11:58 +0000 |
97 | +++ sale_commission/invoice.py 2013-12-23 18:02:44 +0000 |
98 | @@ -22,114 +22,119 @@ |
99 | |
100 | """invoice agents""" |
101 | |
102 | -from osv import fields, osv |
103 | +from osv import fields, orm |
104 | from tools.translate import _ |
105 | |
106 | -class invoice_line_agent(osv.osv): |
107 | + |
108 | +class invoice_line_agent(orm.Model): |
109 | """invoice agents""" |
110 | |
111 | _name = "invoice.line.agent" |
112 | - |
113 | _columns = { |
114 | - 'invoice_line_id':fields.many2one('account.invoice.line', 'Invoice Line', required=True, ondelete='cascade', help=''), |
115 | - 'invoice_id':fields.related('invoice_line_id', 'invoice_id', type='many2one', relation='account.invoice', string='Invoice'), |
116 | - 'invoice_date':fields.related('invoice_id',type='date_invoice', readonly=True), |
117 | + 'invoice_line_id': fields.many2one('account.invoice.line', 'Invoice Line', required=True, ondelete='cascade', |
118 | + help=''), |
119 | + 'invoice_id': fields.related('invoice_line_id', 'invoice_id', type='many2one', relation='account.invoice', |
120 | + string='Invoice'), |
121 | + 'invoice_date': fields.related('invoice_id', type='date_invoice', readonly=True), |
122 | 'agent_id': fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''), |
123 | - 'commission_id':fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', help=''), |
124 | - 'settled':fields.boolean('Settled', readonly=True), |
125 | - 'quantity':fields.float('Settled amount') |
126 | + 'commission_id': fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', |
127 | + help=''), |
128 | + 'settled': fields.boolean('Settled', readonly=True), |
129 | + 'quantity': fields.float('Settled amount') |
130 | } |
131 | _defaults = { |
132 | - 'settled': lambda *a: False, |
133 | - 'quantity':lambda *a: 0 |
134 | - } |
135 | - |
136 | - |
137 | - def calculate_commission (self, cr, uid, ids): |
138 | - for line_agent in self.browse(cr,uid,ids): |
139 | - if line_agent.commission_id.type == 'fijo' and line_agent.commission_id.fix_qty: |
140 | + 'settled': False, |
141 | + 'quantity': 0 |
142 | + } |
143 | + |
144 | + def calculate_commission(self, cr, uid, ids, context=None): |
145 | + if context is None: |
146 | + context = {} |
147 | + for line_agent in self.browse(cr, uid, ids, context): |
148 | + if line_agent.commission_id.type == 'fijo' and line_agent.commission_id.fix_qty: |
149 | quantity = line_agent.invoice_line_id.price_subtotal * (line_agent.commission_id.fix_qty / 100.0) |
150 | - self.write(cr,uid,line_agent.id, {'quantity':quantity}) |
151 | + self.write(cr, uid, line_agent.id, {'quantity': quantity}, context=context) |
152 | |
153 | - def onchange_agent_id(self, cr, uid, ids, agent_id): |
154 | + def onchange_agent_id(self, cr, uid, ids, agent_id, context=None): |
155 | """al cambiar el agente se le carga la comisión""" |
156 | + if context is None: |
157 | + context = {} |
158 | result = {} |
159 | v = {} |
160 | if agent_id: |
161 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
162 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
163 | v['commission_id'] = agent.commission.id |
164 | - agent_line = self.browse(cr, uid, ids) |
165 | + agent_line = self.browse(cr, uid, ids, context=context) |
166 | if agent_line: |
167 | v['quantity'] = agent_line[0].invoice_line_id.price_subtotal * (agent.commission.fix_qty / 100.0) |
168 | else: |
169 | v['quantity'] = 0 |
170 | - |
171 | result['value'] = v |
172 | return result |
173 | |
174 | - def onchange_commission_id(self, cr, uid, ids, agent_id, commission_id): |
175 | + def onchange_commission_id(self, cr, uid, ids, agent_id, commission_id, context=None): |
176 | """alerta al usuario sobre la comisión elegida""" |
177 | + if context is None: |
178 | + context = {} |
179 | result = {} |
180 | v = {} |
181 | if commission_id: |
182 | - partner_commission = self.pool.get('commission').browse(cr, uid, commission_id) |
183 | - agent_line = self.browse(cr, uid, ids) |
184 | + partner_commission = self.pool.get('commission').browse(cr, uid, commission_id, context=context) |
185 | + agent_line = self.browse(cr, uid, ids, context=context) |
186 | v['quantity'] = agent_line[0].invoice_line_id.price_subtotal * (partner_commission.fix_qty / 100.0) |
187 | - result['value']=v |
188 | - if partner_commission.sections: |
189 | - if agent_id: |
190 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
191 | - |
192 | - if agent.commission.id != partner_commission.id: |
193 | - result['warning'] = {} |
194 | - result['warning']['title'] = _('Fee installments!') |
195 | - #result['warning']['message'] = 'Se ha asignado una comisión por tramos que no coincide con la definida para el agente por defecto, por lo que estos tramos se aplicarán tan solo sobre esta factura.' |
196 | - 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.') |
197 | + result['value'] = v |
198 | + if partner_commission.sections and agent_id: |
199 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
200 | + if agent.commission.id != partner_commission.id: |
201 | + result['warning'] = { |
202 | + 'title': _('Fee installments!'), |
203 | + 'message': _('A commission has been assigned by sections that does not ' |
204 | + 'match that defined for the agent by default, so that these ' |
205 | + 'sections shall apply only on this bill.') |
206 | + } |
207 | return result |
208 | |
209 | |
210 | -invoice_line_agent() |
211 | - |
212 | -class account_invoice_line(osv.osv): |
213 | +class account_invoice_line(orm.Model): |
214 | """Enlazamos las comisiones a la factura""" |
215 | |
216 | _inherit = "account.invoice.line" |
217 | - |
218 | _columns = { |
219 | - 'commission_ids': fields.one2many('invoice.line.agent', 'invoice_line_id', 'Commissions', help="Commissions asociated to invoice line."), |
220 | + 'commission_ids': fields.one2many('invoice.line.agent', 'invoice_line_id', 'Commissions', |
221 | + help="Commissions asociated to invoice line."), |
222 | } |
223 | |
224 | -account_invoice_line() |
225 | |
226 | -class account_invoice(osv.osv): |
227 | +class account_invoice(orm.Model): |
228 | """heredamos las facturas para añadirles el representante de venta""" |
229 | |
230 | _inherit = "account.invoice" |
231 | - |
232 | _columns = { |
233 | 'agent_id': fields.many2one('sale.agent', 'Agent'), |
234 | - 'agent_code':fields.related ('agent_id', 'code', readonly=True, type='char', string='Agent code' ), |
235 | - 'country':fields.related ('partner_id', 'country_id', readonly=True, type='many2one', string= 'Country', relation='res.country' ), |
236 | + 'agent_code': fields.related('agent_id', 'code', readonly=True, type='char', string='Agent code'), |
237 | + 'country': fields.related('partner_id', 'country_id', readonly=True, type='many2one', string='Country', |
238 | + relation='res.country'), |
239 | } |
240 | |
241 | - def onchange_partner_id(self, cr, uid, ids, type, part, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False): |
242 | + def onchange_partner_id(self, cr, uid, ids, type, part, date_invoice=False, payment_term=False, |
243 | + partner_bank_id=False, company_id=False, context=None): |
244 | """Al cambiar la empresa nos treamos el representante asociado a la empresa""" |
245 | - res = super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, part, date_invoice=date_invoice, payment_term=payment_term, partner_bank_id=partner_bank_id, company_id=company_id) |
246 | - |
247 | + if context is None: |
248 | + context = {} |
249 | + res = super(account_invoice, self).onchange_partner_id(cr, uid, ids, |
250 | + type, part, date_invoice, payment_term, partner_bank_id, company_id) |
251 | if part and res.get('value', False): |
252 | - partner = self.pool.get('res.partner').browse(cr, uid, part) |
253 | + partner = self.pool.get('res.partner').browse(cr, uid, part, context=context) |
254 | if partner.commission_ids: |
255 | res['value']['agent_id'] = partner.commission_ids[0].agent_id.id |
256 | - |
257 | return res |
258 | |
259 | - def _refund_cleanup_lines(self, cr, uid, lines): |
260 | + def _refund_cleanup_lines(self, cr, uid, lines, context=None): |
261 | """ugly function to map all fields of account.invoice.line when creates refund invoice""" |
262 | - res = super(account_invoice, self)._refund_cleanup_lines(cr, uid, lines) |
263 | + if context is None: |
264 | + context = {} |
265 | + res = super(account_invoice, self)._refund_cleanup_lines(cr, uid, lines, context=context) |
266 | for line in res: |
267 | if 'commission_ids' in line[2]: |
268 | - line[2]['commission_ids'] = [(6,0, line[2].get('commission_ids', [])) ] |
269 | - |
270 | + line[2]['commission_ids'] = [(6, 0, line[2].get('commission_ids', [])), ] |
271 | return res |
272 | - |
273 | -account_invoice() |
274 | |
275 | === modified file 'sale_commission/partner.py' |
276 | --- sale_commission/partner.py 2012-07-13 10:51:04 +0000 |
277 | +++ sale_commission/partner.py 2013-12-23 18:02:44 +0000 |
278 | @@ -1,7 +1,7 @@ |
279 | # -*- coding: utf-8 -*- |
280 | ############################################################################## |
281 | # |
282 | -# OpenERP, Open Source Management Solution |
283 | +# OpenERP, Open Source Management Solution |
284 | # Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved |
285 | # $Id$ |
286 | # |
287 | @@ -22,19 +22,18 @@ |
288 | |
289 | """añadimos algún campo relacionado con el intrastat""" |
290 | |
291 | -from osv import fields, osv |
292 | - |
293 | -class res_partner(osv.osv): |
294 | +from osv import fields, orm |
295 | + |
296 | + |
297 | +class res_partner(orm.Model): |
298 | """añadimos algún campo relacionado con elas comisiones""" |
299 | |
300 | _name = "res.partner" |
301 | _inherit = "res.partner" |
302 | _columns = { |
303 | 'commission_ids': fields.one2many('res.partner.agent', 'partner_id', 'Agents'), |
304 | - 'agent': fields.boolean('Creditor/Agent', help="If you check this field will be available as creditor or agent.") |
305 | + 'agent': fields.boolean('Creditor/Agent', |
306 | + help="If you check this field will be available as creditor or agent.") |
307 | } |
308 | - |
309 | -res_partner() |
310 | |
311 | # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
312 | - |
313 | |
314 | === modified file 'sale_commission/partner_agent.py' |
315 | --- sale_commission/partner_agent.py 2012-07-13 10:51:04 +0000 |
316 | +++ sale_commission/partner_agent.py 2013-12-23 18:02:44 +0000 |
317 | @@ -1,7 +1,7 @@ |
318 | # -*- coding: utf-8 -*- |
319 | ############################################################################## |
320 | # |
321 | -# OpenERP, Open Source Management Solution |
322 | +# OpenERP, Open Source Management Solution |
323 | # Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved |
324 | # $Id$ |
325 | # |
326 | @@ -22,68 +22,68 @@ |
327 | |
328 | """objeto de comportamiento many2many que relaciona agentes o comerciales con partners""" |
329 | |
330 | -from osv import fields, osv |
331 | +from osv import fields, orm |
332 | from tools.translate import _ |
333 | |
334 | -class res_partner_agent(osv.osv): |
335 | - "objeto de comportamiento many2many que relaciona agentes o comerciales con partners" |
336 | + |
337 | +class res_partner_agent(orm.Model): |
338 | + """objeto de comportamiento many2many que relaciona agentes o comerciales con partners""" |
339 | _name = "res.partner.agent" |
340 | |
341 | def name_get(self, cr, uid, ids, context=None): |
342 | """devuelve como nombre del agente del partner el nombre del agente""" |
343 | - if context is None: context = {} |
344 | - res = [] |
345 | - for obj in self.browse(cr, uid, ids): |
346 | - res.append((obj.id, obj.agent_id.name)) |
347 | - |
348 | - return res |
349 | + if context is None: |
350 | + context = {} |
351 | + return [(obj.id, obj.agent_id.name) for obj in self.browse(cr, uid, ids, context=context)] |
352 | |
353 | def _get_partner_agents_to_update_from_sale_agents(self, cr, uid, ids, context=None): |
354 | - """devuelve los ids de partner agents a actualizar desde el lanzamiento de un evento de actualización en agentes de ventas""" |
355 | - if context is None: context = {} |
356 | - result = [] |
357 | - for agent_obj_id in self.browse(cr, uid, ids): |
358 | - partner_agents_ids = self.pool.get('res.partner.agent').search(cr, uid, [('agent_id', '=', agent_obj_id.id)]) |
359 | - result.extend(partner_agents_ids) |
360 | - return result |
361 | + """ |
362 | + devuelve los ids de partner agents a actualizar desde el lanzamiento de un evento de actualización en agentes |
363 | + de ventas |
364 | + """ |
365 | + if context is None: |
366 | + context = {} |
367 | + agent_pool = self.pool.get('res.partner.agent') |
368 | + agent_obj_ids = [agent_obj_id.id for agent_obj_id in self.browse(cr, uid, ids, context=context)] |
369 | + return agent_pool.search(cr, uid, [('agent_id', 'in', agent_obj_ids)], context=context) |
370 | |
371 | _columns = { |
372 | - 'partner_id':fields.many2one('res.partner', 'Partner', required=True, ondelete='cascade', help='', select=1), |
373 | - 'agent_id':fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''), |
374 | - 'commission_id':fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', help=''), |
375 | - 'type': fields.related('agent_id', 'type', type="selection", selection=[('asesor', 'Adviser'), ('comercial', 'Commercial')], readonly=True, |
376 | - store={'sale.agent': (_get_partner_agents_to_update_from_sale_agents, ['type'], 10), |
377 | - 'res.partner.agent': (lambda self, cr, uid, ids, c={}: ids, None, 20)}) |
378 | + 'partner_id': fields.many2one('res.partner', 'Partner', required=True, ondelete='cascade', help='', select=1), |
379 | + 'agent_id': fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''), |
380 | + 'commission_id': fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', |
381 | + help=''), |
382 | + 'type': fields.related('agent_id', 'type', type="selection", |
383 | + selection=[('asesor', 'Adviser'), ('comercial', 'Commercial')], readonly=True, |
384 | + store={'sale.agent': (_get_partner_agents_to_update_from_sale_agents, ['type'], 10), |
385 | + 'res.partner.agent': (lambda self, cr, uid, ids, c={}: ids, None, 20)}) |
386 | } |
387 | - |
388 | - def onchange_agent_id(self, cr, uid, ids, agent_id): |
389 | + |
390 | + def onchange_agent_id(self, cr, uid, ids, agent_id, context=None): |
391 | """al cambiar el agente cargamos sus comisión""" |
392 | result = {} |
393 | v = {} |
394 | if agent_id: |
395 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
396 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
397 | v['commission_id'] = agent.commission.id |
398 | - |
399 | result['value'] = v |
400 | return result |
401 | |
402 | - def onchange_commission_id(self, cr, uid, ids, agent_id=False, commission_id=False): |
403 | + def onchange_commission_id(self, cr, uid, ids, agent_id=False, commission_id=False, context=None): |
404 | """al cambiar la comisión comprobamos la selección""" |
405 | + if context is None: |
406 | + context = {} |
407 | result = {} |
408 | - |
409 | if commission_id: |
410 | - partner_commission = self.pool.get('commission').browse(cr, uid, commission_id) |
411 | - if partner_commission.sections: |
412 | - if agent_id: |
413 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
414 | - if agent.commission.id != partner_commission.id: |
415 | - result['warning'] = {} |
416 | - result['warning']['title'] = _('Fee installments!') |
417 | - 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.') |
418 | - |
419 | + partner_commission = self.pool.get('commission').browse(cr, uid, commission_id, context=context) |
420 | + if partner_commission.sections and agent_id: |
421 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
422 | + if agent.commission.id != partner_commission.id: |
423 | + result['warning'] = { |
424 | + 'title': _('Fee installments!'), |
425 | + 'message': _('A commission has been assigned by sections that does not ' |
426 | + 'match that defined for the agent by default, so that these ' |
427 | + 'sections shall apply only on this bill.') |
428 | + } |
429 | return result |
430 | - |
431 | - |
432 | -res_partner_agent() |
433 | - |
434 | -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
435 | \ No newline at end of file |
436 | + |
437 | +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
438 | |
439 | === modified file 'sale_commission/partner_view.xml' |
440 | --- sale_commission/partner_view.xml 2013-02-19 14:49:01 +0000 |
441 | +++ sale_commission/partner_view.xml 2013-12-23 18:02:44 +0000 |
442 | @@ -9,7 +9,7 @@ |
443 | <field name="priority" eval="18"/> |
444 | <field name="arch" type="xml"> |
445 | <field name="supplier" position="after"> |
446 | - <field name="agent" string="Consultancy"></field> |
447 | + <field name="agent" string="Consultancy"/> |
448 | </field> |
449 | </field> |
450 | </record> |
451 | |
452 | === renamed file 'sale_commission/saleagent.py' => 'sale_commission/sale_agent.py' |
453 | --- sale_commission/saleagent.py 2012-07-13 10:51:04 +0000 |
454 | +++ sale_commission/sale_agent.py 2013-12-23 18:02:44 +0000 |
455 | @@ -1,7 +1,7 @@ |
456 | # -*- coding: utf-8 -*- |
457 | ############################################################################## |
458 | # |
459 | -# OpenERP, Open Source Management Solution |
460 | +# OpenERP, Open Source Management Solution |
461 | # Copyright (C) 2011 Pexego Sistemas Informáticos (<http://www.pexego.es>). All Rights Reserved |
462 | # $Id$ |
463 | # |
464 | @@ -20,10 +20,10 @@ |
465 | # |
466 | ############################################################################## |
467 | |
468 | -from osv import fields, osv |
469 | - |
470 | - |
471 | -class commission(osv.osv): |
472 | +from osv import fields, orm |
473 | + |
474 | + |
475 | +class commission(orm.Model): |
476 | """Objeto comisión""" |
477 | |
478 | _name = "commission" |
479 | @@ -35,11 +35,13 @@ |
480 | 'sections': fields.one2many('commission.section', 'commission_id', 'Sections') |
481 | } |
482 | _defaults = { |
483 | - 'type' : lambda *a: 'fijo', |
484 | + 'type': 'fijo', |
485 | } |
486 | |
487 | - def calcula_tramos(self, cr, uid, ids, base): |
488 | - commission = self.browse(cr, uid, ids)[0] |
489 | + def calcula_tramos(self, cr, uid, ids, base, context=None): |
490 | + if context is None: |
491 | + context = {} |
492 | + commission = self.browse(cr, uid, ids, context=context)[0] |
493 | #Cálculo de tramos |
494 | for section in commission.sections: |
495 | if base >= section.commission_from and (base < section.commission_until or section.commission_until == 0): |
496 | @@ -47,10 +49,8 @@ |
497 | return res |
498 | return 0.0 |
499 | |
500 | -commission() |
501 | - |
502 | - |
503 | -class commission_section(osv.osv): |
504 | + |
505 | +class commission_section(orm.Model): |
506 | """periodo de las comisiones""" |
507 | |
508 | _name = "commission.section" |
509 | @@ -63,39 +63,40 @@ |
510 | |
511 | } |
512 | |
513 | -commission_section() |
514 | |
515 | -class sale_agent(osv.osv): |
516 | +class sale_agent(orm.Model): |
517 | """Agente de ventas""" |
518 | |
519 | _name = "sale.agent" |
520 | _description = "Sale agent" |
521 | - |
522 | _columns = { |
523 | 'name': fields.char('Saleagent Name', size=125, required=True), |
524 | 'type': fields.selection((('asesor', 'Adviser'), ('comercial', 'Commercial')), 'Type', required=True), |
525 | - 'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade', help='Associated partner, is necessary for income invoices.'), |
526 | - 'code':fields.related ('partner_id','ref', string='Code', readonly=True, type='char', help='Se obtiene del código de la empresa relacionada'), |
527 | - 'employee_id': fields.many2one('hr.employee', 'Associated Employee', help='Employee associated to agent, is necessary for set an employee to settle commissions in wage.'), |
528 | + 'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade', |
529 | + help='Associated partner, is necessary for income invoices.'), |
530 | + 'code': fields.related('partner_id', 'ref', string='Code', readonly=True, type='char', |
531 | + help='Se obtiene del código de la empresa relacionada'), |
532 | + 'employee_id': fields.many2one('hr.employee', 'Associated Employee', |
533 | + help='Employee associated to agent, is necessary for set an employee ' |
534 | + 'to settle commissions in wage.'), |
535 | 'customer': fields.one2many('res.partner.agent', 'agent_id', 'Customer', readonly=True), |
536 | 'commission': fields.many2one('commission', 'Commission by default', required=True), |
537 | - 'settlement': fields.selection((('m', 'Monthly'),('t', 'Quarterly'),('s', 'Semiannual'),('a', 'Annual')), 'Period settlement', required=True), |
538 | + 'settlement': fields.selection((('m', 'Monthly'), ('t', 'Quarterly'), ('s', 'Semiannual'), ('a', 'Annual')), |
539 | + 'Period settlement', required=True), |
540 | 'active': fields.boolean('Active'), |
541 | - 'retention_id': fields.many2one ('account.tax', 'Applied retention'), |
542 | - 'settlement_ids': fields.one2many ('settlement.agent', 'agent_id', 'Settlements executed', readonly=True) |
543 | + 'retention_id': fields.many2one('account.tax', 'Applied retention'), |
544 | + 'settlement_ids': fields.one2many('settlement.agent', 'agent_id', 'Settlements executed', readonly=True) |
545 | } |
546 | _defaults = { |
547 | - 'active': lambda *a: True, |
548 | - 'type' : lambda *a: 'asesor', |
549 | + 'active': True, |
550 | + 'type': 'asesor', |
551 | } |
552 | |
553 | - def calcula_tramos (self, cr, uid, ids, base): |
554 | + def calcula_tramos(self, cr, uid, ids, base, context=None): |
555 | """calcula los tramos por factura""" |
556 | - agente = self.browse(cr, uid, ids)[0] |
557 | + if context is None: |
558 | + context = {} |
559 | + agente = self.browse(cr, uid, ids, context=context)[0] |
560 | return agente.commission.calcula_tramos(base) |
561 | |
562 | - |
563 | -sale_agent()# |
564 | - |
565 | # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
566 | - |
567 | |
568 | === renamed file 'sale_commission/saleagent_demo.xml' => 'sale_commission/sale_agent_demo.xml' |
569 | === renamed file 'sale_commission/saleagent_view.xml' => 'sale_commission/sale_agent_view.xml' |
570 | === modified file 'sale_commission/sale_order.py' |
571 | --- sale_commission/sale_order.py 2013-06-07 21:40:14 +0000 |
572 | +++ sale_commission/sale_order.py 2013-12-23 18:02:44 +0000 |
573 | @@ -22,150 +22,152 @@ |
574 | |
575 | """Modificamos las ventas para incluir el comportamiento de comisiones""" |
576 | |
577 | -from osv import fields, osv |
578 | +from osv import fields, orm |
579 | from tools.translate import _ |
580 | |
581 | -class sale_order_agent(osv.osv): |
582 | + |
583 | +class sale_order_agent(orm.Model): |
584 | _name = "sale.order.agent" |
585 | |
586 | def name_get(self, cr, uid, ids, context=None): |
587 | """devuelve como nombre del agente del partner el nombre del agente""" |
588 | - if context is None: context = {} |
589 | - res = [] |
590 | - for obj in self.browse(cr, uid, ids): |
591 | - res.append((obj.id, obj.agent_id.name)) |
592 | - return res |
593 | + if context is None: |
594 | + context = {} |
595 | + return [(obj.id, obj.agent_id.name) for obj in self.browse(cr, uid, ids, context=context)] |
596 | |
597 | _columns = { |
598 | - 'sale_id':fields.many2one('sale.order', 'Sale order', required=False, ondelete='cascade', help=''), |
599 | - 'agent_id':fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''), |
600 | - 'commission_id':fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', help=''), |
601 | + 'sale_id': fields.many2one('sale.order', 'Sale order', required=False, ondelete='cascade', help=''), |
602 | + 'agent_id': fields.many2one('sale.agent', 'Agent', required=True, ondelete='cascade', help=''), |
603 | + 'commission_id': fields.many2one('commission', 'Applied commission', required=True, ondelete='cascade', |
604 | + help=''), |
605 | } |
606 | |
607 | - def onchange_agent_id(self, cr, uid, ids, agent_id): |
608 | + def onchange_agent_id(self, cr, uid, ids, agent_id, context=None): |
609 | """al cambiar el agente cargamos sus comisión""" |
610 | + if context is None: |
611 | + context = {} |
612 | result = {} |
613 | v = {} |
614 | if agent_id: |
615 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
616 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
617 | v['commission_id'] = agent.commission.id |
618 | - |
619 | result['value'] = v |
620 | return result |
621 | |
622 | - def onchange_commission_id(self, cr, uid, ids, agent_id=False, commission_id=False): |
623 | + def onchange_commission_id(self, cr, uid, ids, agent_id=False, commission_id=False, context=None): |
624 | """al cambiar la comisión comprobamos la selección""" |
625 | + if context is None: |
626 | + context = {} |
627 | result = {} |
628 | - |
629 | if commission_id: |
630 | - partner_commission = self.pool.get('commission').browse(cr, uid, commission_id) |
631 | - if partner_commission.sections: |
632 | - if agent_id: |
633 | - agent = self.pool.get('sale.agent').browse(cr, uid, agent_id) |
634 | - if agent.commission.id != partner_commission.id: |
635 | - result['warning'] = {} |
636 | - result['warning']['title'] = _('Fee installments!') |
637 | - 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.') |
638 | + partner_commission = self.pool.get('commission').browse(cr, uid, commission_id, context=context) |
639 | + if partner_commission.sections and agent_id: |
640 | + agent = self.pool.get('sale.agent').browse(cr, uid, agent_id, context=context) |
641 | + if agent.commission.id != partner_commission.id: |
642 | + result['warning'] = { |
643 | + 'title': _('Fee installments!'), |
644 | + 'message': _('A commission has been assigned by sections that does not ' |
645 | + 'match that defined for the agent by default, so that these ' |
646 | + 'sections shall apply only on this bill.') |
647 | + } |
648 | return result |
649 | |
650 | -sale_order_agent() |
651 | |
652 | -class sale_order(osv.osv): |
653 | +class sale_order(orm.Model): |
654 | """Modificamos las ventas para incluir el comportamiento de comisiones""" |
655 | |
656 | _inherit = "sale.order" |
657 | - |
658 | _columns = { |
659 | - 'sale_agent_ids':fields.one2many('sale.order.agent', 'sale_id', 'Agents', states={'draft': [('readonly', False)]}) |
660 | + 'sale_agent_ids': fields.one2many('sale.order.agent', 'sale_id', 'Agents', |
661 | + states={'draft': [('readonly', False)]}) |
662 | } |
663 | |
664 | def create(self, cr, uid, values, context=None): |
665 | - """ |
666 | - """ |
667 | + if context is None: |
668 | + context = {} |
669 | + agent_pool = self.pool.get('sale.order.agent') |
670 | res = super(sale_order, self).create(cr, uid, values, context=context) |
671 | if 'sale_agent_ids' in values: |
672 | for sale_order_agent in values['sale_agent_ids']: |
673 | - self.pool.get('sale.order.agent').write(cr, uid, sale_order_agent[1], {'sale_id':res}) |
674 | + agent_pool.write(cr, uid, sale_order_agent[1], {'sale_id': res}) |
675 | return res |
676 | |
677 | def write(self, cr, uid, ids, values, context=None): |
678 | - """ |
679 | - """ |
680 | - |
681 | + if context is None: |
682 | + context = {} |
683 | + agent_pool = self.pool.get('sale.order.agent') |
684 | if 'sale_agent_ids' in values: |
685 | for sale_order_agent in values['sale_agent_ids']: |
686 | for id in ids: |
687 | if sale_order_agent[2]: |
688 | - sale_order_agent[2]['sale_id']=id |
689 | + sale_order_agent[2]['sale_id'] = id |
690 | else: |
691 | - self.pool.get('sale.order.agent').unlink(cr, uid, sale_order_agent[1]) |
692 | + agent_pool.unlink(cr, uid, sale_order_agent[1], context=context) |
693 | return super(sale_order, self).write(cr, uid, ids, values, context=context) |
694 | |
695 | def onchange_partner_id(self, cr, uid, ids, part, context=None): |
696 | """heredamos el evento de cambio del campo partner_id para actualizar el campo agent_id""" |
697 | - sale_agent_ids=[] |
698 | + if context is None: |
699 | + context = {} |
700 | + sale_agent_ids = [] |
701 | res = super(sale_order, self).onchange_partner_id(cr, uid, ids, part, context=context) |
702 | if res.get('value', False) and part: |
703 | sale_order_agent = self.pool.get('sale.order.agent') |
704 | if ids: |
705 | - sale_order_agent.unlink(cr, uid, sale_order_agent.search(cr, uid ,[('sale_id','=',ids)])) |
706 | - partner = self.pool.get('res.partner').browse(cr, uid, part) |
707 | + sale_order_agent.unlink(cr, uid, sale_order_agent.search(cr, uid, [('sale_id', '=', ids)], |
708 | + context=context)) |
709 | + partner = self.pool.get('res.partner').browse(cr, uid, part, context=context) |
710 | for partner_agent in partner.commission_ids: |
711 | - vals={ |
712 | - 'agent_id':partner_agent.agent_id.id, |
713 | - 'commission_id':partner_agent.commission_id.id, |
714 | + vals = { |
715 | + 'agent_id': partner_agent.agent_id.id, |
716 | + 'commission_id': partner_agent.commission_id.id, |
717 | #'sale_id':ids |
718 | } |
719 | + # FIXME: What is going on in this block? |
720 | if ids: |
721 | for id in ids: |
722 | - vals['sale_id']=id |
723 | - sale_agent_id=sale_order_agent.create(cr, uid, vals) |
724 | + vals['sale_id'] = id |
725 | + sale_agent_id = sale_order_agent.create(cr, uid, vals, context=context) |
726 | sale_agent_ids.append(int(sale_agent_id)) |
727 | - res['value']['sale_agent_ids'] = sale_agent_ids |
728 | + res['value']['sale_agent_ids'] = sale_agent_ids |
729 | return res |
730 | |
731 | - |
732 | def action_ship_create(self, cr, uid, ids, context=None): |
733 | """extend this method to add agent_id to picking""" |
734 | + if context is None: |
735 | + context = {} |
736 | + picking_pool = self.pool.get('stock.picking') |
737 | res = super(sale_order, self).action_ship_create(cr, uid, ids, context=context) |
738 | - |
739 | - for order in self.browse(cr, uid, ids): |
740 | + for order in self.browse(cr, uid, ids, context=context): |
741 | pickings = [x.id for x in order.picking_ids] |
742 | agents = [x.agent_id.id for x in order.sale_agent_ids] |
743 | if pickings and agents: |
744 | - self.pool.get('stock.picking').write(cr, uid, pickings, {'agent_ids': [[6, 0, agents]] }) |
745 | + picking_pool.write(cr, uid, pickings, {'agent_ids': [[6, 0, agents]], }, context=context) |
746 | return res |
747 | |
748 | -sale_order() |
749 | - |
750 | - |
751 | -class sale_order_line(osv.osv): |
752 | + |
753 | +class sale_order_line(orm.Model): |
754 | """Modificamos las lineas ventas para incluir las comisiones en las facturas creadas desde ventas""" |
755 | |
756 | _inherit = "sale.order.line" |
757 | |
758 | - |
759 | def invoice_line_create(self, cr, uid, ids, context=None): |
760 | if context is None: |
761 | context = {} |
762 | - |
763 | + invoice_line_pool = self.pool.get('account.invoice.line') |
764 | + invoice_line_agent_pool = self.pool.get('invoice.line.agent') |
765 | res = super(sale_order_line, self).invoice_line_create(cr, uid, ids, context) |
766 | - so_ref = self.browse(cr,uid,ids)[0].order_id |
767 | + so_ref = self.browse(cr, uid, ids)[0].order_id |
768 | for so_agent_id in so_ref.sale_agent_ids: |
769 | - inv_lines = self.pool.get('account.invoice.line').browse(cr, uid, res) |
770 | + inv_lines = invoice_line_pool.browse(cr, uid, res, context=context) |
771 | for inv_line in inv_lines: |
772 | - if inv_line.product_id and inv_line.product_id.commission_exent != True: |
773 | + if inv_line.product_id and inv_line.product_id.commission_exent is not True: |
774 | vals = { |
775 | 'invoice_line_id': inv_line.id, |
776 | 'agent_id': so_agent_id.agent_id.id, |
777 | 'commission_id': so_agent_id.commission_id.id, |
778 | 'settled': False |
779 | } |
780 | - line_agent_id=self.pool.get('invoice.line.agent').create(cr, uid, vals) |
781 | - self.pool.get('invoice.line.agent').calculate_commission(cr, uid, [line_agent_id]) |
782 | + line_agent_id = invoice_line_agent_pool.create(cr, uid, vals, context=context) |
783 | + invoice_line_agent_pool.calculate_commission(cr, uid, [line_agent_id], context=context) |
784 | return res |
785 | - |
786 | -sale_order_line() |
787 | - |
788 | - |
789 | - |
790 | |
791 | === modified file 'sale_commission/settled.py' |
792 | --- sale_commission/settled.py 2012-07-13 10:51:04 +0000 |
793 | +++ sale_commission/settled.py 2013-12-23 18:02:44 +0000 |
794 | @@ -22,63 +22,55 @@ |
795 | |
796 | """Objetos sobre las liquidación""" |
797 | |
798 | -from osv import fields, osv |
799 | +from osv import fields, orm, osv |
800 | from tools.translate import _ |
801 | import time |
802 | import tools |
803 | |
804 | -class settled_wizard (osv.osv_memory): |
805 | + |
806 | +class settled_wizard (orm.TransientModel): |
807 | """settled.wizard""" |
808 | - |
809 | + |
810 | _name = 'settled.wizard' |
811 | _columns = { |
812 | - #'settlement':fields.selection((('m', 'Monthly'),('t', 'Quarterly'),('s', 'Semiannual'),('a', 'Annual')), 'Settlement period', required=True), |
813 | - 'date_from':fields.date ('From',required=True), |
814 | - 'date_to':fields.date ('To',required=True), |
815 | - |
816 | + 'date_from': fields.date('From', required=True), |
817 | + 'date_to': fields.date('To', required=True), |
818 | } |
819 | _defaults = { |
820 | } |
821 | |
822 | def settlement_exec(self, cr, uid, ids, context=None): |
823 | """se ejecuta correctamente desde dos.""" |
824 | + if context is None: |
825 | + context = {} |
826 | + pool_liq = self.pool.get('settlement') |
827 | for o in self.browse(cr, uid, ids, context=context): |
828 | - |
829 | - pool_liq = self.pool.get('settlement') |
830 | - liq_id = pool_liq.search(cr, uid, [('date_to', '>=', o.date_from)]) |
831 | - |
832 | - vals={ |
833 | - 'name': o.date_from+ " // " + o.date_to, |
834 | - 'date_from':o.date_from, |
835 | - 'date_to':o.date_to |
836 | + vals = { |
837 | + 'name': o.date_from + " // " + o.date_to, |
838 | + 'date_from': o.date_from, |
839 | + 'date_to': o.date_to |
840 | } |
841 | - liq_id = pool_liq.create(cr, uid, vals) |
842 | - pool_liq.calcula(cr, uid, liq_id, context['active_ids'], o.date_from, o.date_to) |
843 | - |
844 | - |
845 | - return { |
846 | - 'type': 'ir.actions.act_window_close', |
847 | - } |
848 | - |
849 | - |
850 | - def action_cancel(self, cr, uid, ids, conect=None): |
851 | - """CANCEL LIQUIDACIÓN""" |
852 | - return { |
853 | - 'type': 'ir.actions.act_window_close', |
854 | - } |
855 | - |
856 | - |
857 | - |
858 | -settled_wizard() |
859 | - |
860 | - |
861 | -class recalculate_commision_wizard (osv.osv_memory): |
862 | + liq_id = pool_liq.create(cr, uid, vals, context=context) |
863 | + pool_liq.calcula(cr, uid, liq_id, context['active_ids'], o.date_from, o.date_to, context=context) |
864 | + |
865 | + return { |
866 | + 'type': 'ir.actions.act_window_close', |
867 | + } |
868 | + |
869 | + def action_cancel(self, cr, uid, ids, connect=None, context=None): |
870 | + """Cancel Liquidation""" |
871 | + return { |
872 | + 'type': 'ir.actions.act_window_close', |
873 | + } |
874 | + |
875 | + |
876 | +class recalculate_commision_wizard (orm.TransientModel): |
877 | """settled.wizard""" |
878 | |
879 | _name = 'recalculate.commission.wizard' |
880 | _columns = { |
881 | - 'date_from':fields.date ('From',required=True), |
882 | - 'date_to':fields.date ('To',required=True), |
883 | + 'date_from': fields.date('From', required=True), |
884 | + 'date_to': fields.date('To', required=True), |
885 | |
886 | } |
887 | _defaults = { |
888 | @@ -86,115 +78,108 @@ |
889 | |
890 | def recalculate_exec(self, cr, uid, ids, context=None): |
891 | """se ejecuta correctamente desde dos.""" |
892 | - user = self.pool.get('res.users').browse(cr, uid, uid) |
893 | - |
894 | + if context is None: |
895 | + context = {} |
896 | + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) |
897 | + agent_pool = self.pool.get('invoice.line.agent') |
898 | for o in self.browse(cr, uid, ids, context=context): |
899 | - |
900 | sql = 'SELECT invoice_line_agent.id FROM account_invoice_line ' \ |
901 | - 'INNER JOIN invoice_line_agent ON invoice_line_agent.invoice_line_id=account_invoice_line.id ' \ |
902 | - 'INNER JOIN account_invoice ON account_invoice_line.invoice_id = account_invoice.id ' \ |
903 | - 'WHERE invoice_line_agent.agent_id in (' + ",".join(map(str, context['active_ids'])) + ') AND invoice_line_agent.settled=False ' \ |
904 | - 'AND account_invoice.state<>\'draft\' AND account_invoice.type=\'out_invoice\''\ |
905 | - 'AND account_invoice.date_invoice >= \'' + o.date_from + '\' AND account_invoice.date_invoice <= \'' + o.date_to +'\''\ |
906 | - ' AND account_invoice.company_id = ' + str(user.company_id.id) |
907 | - |
908 | + 'INNER JOIN invoice_line_agent ON invoice_line_agent.invoice_line_id=account_invoice_line.id ' \ |
909 | + 'INNER JOIN account_invoice ON account_invoice_line.invoice_id = account_invoice.id ' \ |
910 | + 'WHERE invoice_line_agent.agent_id in (' + ",".join(map(str, context['active_ids'])) + ') ' \ |
911 | + 'AND invoice_line_agent.settled=False ' \ |
912 | + 'AND account_invoice.state<>\'draft\' AND account_invoice.type=\'out_invoice\'' \ |
913 | + 'AND account_invoice.date_invoice >= \'' + o.date_from + '\' ' \ |
914 | + 'AND account_invoice.date_invoice <= \'' + o.date_to + '\' ' \ |
915 | + 'AND account_invoice.company_id = ' + str(user.company_id.id) |
916 | cr.execute(sql) |
917 | res = cr.fetchall() |
918 | inv_line_agent_ids = [x[0] for x in res] |
919 | - |
920 | - self.pool.get ('invoice.line.agent').calculate_commission( cr, uid, inv_line_agent_ids) |
921 | - |
922 | - |
923 | - return { |
924 | - 'type': 'ir.actions.act_window_close', |
925 | - } |
926 | - |
927 | - |
928 | - def action_cancel(self, cr, uid, ids, conect=None): |
929 | - """CANCEL CALCULATE""" |
930 | - return { |
931 | - 'type': 'ir.actions.act_window_close', |
932 | - } |
933 | - |
934 | - |
935 | - |
936 | -recalculate_commision_wizard() |
937 | - |
938 | - |
939 | - |
940 | - |
941 | -class settlement (osv.osv): |
942 | - """Objeto Liquidación""" |
943 | + agent_pool.calculate_commission(cr, uid, inv_line_agent_ids, context=context) |
944 | + return { |
945 | + 'type': 'ir.actions.act_window_close', |
946 | + } |
947 | + |
948 | + def action_cancel(self, cr, uid, ids, connect=None, context=None): |
949 | + """Cancel Calculation""" |
950 | + return { |
951 | + 'type': 'ir.actions.act_window_close', |
952 | + } |
953 | + |
954 | + |
955 | +class settlement (orm.Model): |
956 | + """Object Liquidation""" |
957 | |
958 | _name = 'settlement' |
959 | _columns = { |
960 | 'name': fields.char('Settlement period', size=64, required=True, readonly=True), |
961 | 'total': fields.float('Total', readonly=True), |
962 | - 'date_from':fields.date('From'), |
963 | - 'date_to':fields.date('To'), |
964 | + 'date_from': fields.date('From'), |
965 | + 'date_to': fields.date('To'), |
966 | 'settlement_agent_id': fields.one2many('settlement.agent', 'settlement_id', 'Settlement agents', readonly=True), |
967 | 'date': fields.datetime('Created Date', required=True), |
968 | - 'state': fields.selection([('invoiced', 'Invoiced'),('settled', 'Settled'), ('cancel', 'Cancel')], 'State', required=True, readonly=True) |
969 | + 'state': fields.selection([('invoiced', 'Invoiced'), ('settled', 'Settled'), ('cancel', 'Cancel')], 'State', |
970 | + required=True, readonly=True) |
971 | } |
972 | _defaults = { |
973 | 'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), |
974 | - 'state': lambda *a: 'settled' |
975 | + 'state': 'settled' |
976 | } |
977 | |
978 | def action_invoice_create(self, cursor, user, ids, journal_id, product_id, context=None): |
979 | - |
980 | - agents_pool=self.pool.get('settlement.agent') |
981 | - res={} |
982 | + if context is None: |
983 | + context = {} |
984 | + agents_pool = self.pool.get('settlement.agent') |
985 | + res = {} |
986 | for settlement in self.browse(cursor, user, ids, context=context): |
987 | settlement_agent_ids = map(lambda x: x.id, settlement.settlement_agent_id) |
988 | - invoices_agent = agents_pool.action_invoice_create(cursor, user, settlement_agent_ids, journal_id, product_id) |
989 | - |
990 | + invoices_agent = agents_pool.action_invoice_create(cursor, user, settlement_agent_ids, journal_id, |
991 | + product_id, context=context) |
992 | res[settlement.id] = invoices_agent.values() |
993 | return res |
994 | |
995 | - |
996 | - |
997 | - |
998 | - def calcula(self, cr, uid, ids, agent_ids, date_from, date_to): |
999 | + def calcula(self, cr, uid, ids, agent_ids, date_from, date_to, context=None): |
1000 | """genera una entrada de liquidación por agente""" |
1001 | - |
1002 | # Busca todas las líneas de liquidación facturadas en un período |
1003 | - pool_agent = self.pool.get('sale.agent') |
1004 | - agents = pool_agent.browse(cr, uid, agent_ids) |
1005 | + if context is None: |
1006 | + context = {} |
1007 | + sale_agent_pool = self.pool.get('sale.agent') |
1008 | + settlement_agent_pool = self.pool.get('settlement.agent') |
1009 | + agents = sale_agent_pool.browse(cr, uid, agent_ids, context=context) |
1010 | total = 0 |
1011 | for agent in agents: |
1012 | # genera una entrada de liquidación por agente |
1013 | - liq_agent_id = self.pool.get('settlement.agent').create(cr, uid, {'agent_id': agent.id, 'settlement_id': ids}) |
1014 | - self.pool.get('settlement.agent').calcula(cr, uid, liq_agent_id, date_from, date_to) |
1015 | - liq_agent = self.pool.get('settlement.agent').browse(cr, uid, liq_agent_id) |
1016 | + liq_agent_id = settlement_agent_pool.create(cr, uid, {'agent_id': agent.id, 'settlement_id': ids}, |
1017 | + context=context) |
1018 | + settlement_agent_pool.calcula(cr, uid, liq_agent_id, date_from, date_to, context=context) |
1019 | + liq_agent = settlement_agent_pool.browse(cr, uid, liq_agent_id, context=context) |
1020 | total = total + liq_agent.total |
1021 | - |
1022 | - return self.write (cr, uid, ids, {'total': total}) |
1023 | + return self.write(cr, uid, ids, {'total': total}, context=context) |
1024 | |
1025 | def action_cancel(self, cr, uid, ids, context=None): |
1026 | - """Cancela la liquidación""" |
1027 | - if context is None: context={} |
1028 | - for settle in self.browse(cr, uid, ids): |
1029 | + """Cancel the liquidation""" |
1030 | + if context is None: |
1031 | + context = {} |
1032 | + agent_pool = self.pool.get('invoice.line.agent') |
1033 | + for settle in self.browse(cr, uid, ids, context=context): |
1034 | for settle_line in settle.settlement_agent_id: |
1035 | for line in settle_line.lines: |
1036 | commission_ids = line.invoice_line_id and [x.id for x in line.invoice_line_id.commission_ids] or [] |
1037 | if commission_ids: |
1038 | - self.pool.get('invoice.line.agent').write(cr, uid, commission_ids, {'settled': False, 'quantity': 0.0}) |
1039 | - |
1040 | - return self.write(cr, uid, ids, {'state': 'cancel'}) |
1041 | + agent_pool.write(cr, uid, commission_ids, {'settled': False, 'quantity': 0.0}, context=context) |
1042 | + return self.write(cr, uid, ids, {'state': 'cancel'}, context=context) |
1043 | |
1044 | def unlink(self, cr, uid, ids, context=None): |
1045 | """permite borrar liquidaciones canceladas""" |
1046 | - for settle in self.browse(cr, uid, ids): |
1047 | + if context is None: |
1048 | + context = {} |
1049 | + for settle in self.browse(cr, uid, ids, context=context): |
1050 | if settle.state != 'cancel': |
1051 | raise osv.except_osv(_('Error!'), _("You can\'t delete it, if it isn't in cancel state.")) |
1052 | - |
1053 | return super(settlement, self).unlink(cr, uid, ids, context=context) |
1054 | |
1055 | -settlement() |
1056 | - |
1057 | - |
1058 | -class settlement_agent (osv.osv): |
1059 | + |
1060 | +class settlement_agent (orm.Model): |
1061 | """Liquidaciones de Agentes""" |
1062 | |
1063 | _name = 'settlement.agent' |
1064 | @@ -203,19 +188,18 @@ |
1065 | '''Call after the creation of the invoice line''' |
1066 | return |
1067 | |
1068 | - def _get_address_invoice(self, cursor, user, settlement): |
1069 | + def _get_address_invoice(self, cr, uid, settlement, context=None): |
1070 | '''Return {'contact': address, 'invoice': address} for invoice''' |
1071 | + if context is None: |
1072 | + context = {} |
1073 | partner_obj = self.pool.get('res.partner') |
1074 | partner = settlement.agent_id.partner_id |
1075 | - |
1076 | - return partner_obj.address_get(cursor, user, [partner.id], |
1077 | - ['contact', 'invoice']) |
1078 | - |
1079 | - def _invoice_hook(self, cursor, user, picking, invoice_id): |
1080 | + return partner_obj.address_get(cr, uid, [partner.id], ['contact', 'invoice'], context=context) |
1081 | + |
1082 | + def _invoice_hook(self, cr, uid, picking, invoice_id, context=None): |
1083 | '''Call after the creation of the invoice''' |
1084 | return |
1085 | |
1086 | - |
1087 | _columns = { |
1088 | 'agent_id': fields.many2one('sale.agent', 'Agent', required=True, select=1), |
1089 | 'total_per': fields.float('Total percentages', readonly=True), |
1090 | @@ -226,74 +210,61 @@ |
1091 | 'settlement_id': fields.many2one('settlement', 'Settlement', required=True, ondelete="cascade") |
1092 | } |
1093 | |
1094 | - |
1095 | - |
1096 | - def get_currency_id(self, cursor, user, picking): |
1097 | + def get_currency_id(self, cr, uid, picking, context=None): |
1098 | return False |
1099 | |
1100 | - |
1101 | - def action_invoice_create(self, cursor, user, ids, journal_id, product_id, context=None): |
1102 | + def action_invoice_create(self, cr, uid, ids, journal_id, product_id, context=None): |
1103 | '''Return ids of created invoices for the settlements''' |
1104 | - |
1105 | + if context is None: |
1106 | + context = {} |
1107 | invoice_obj = self.pool.get('account.invoice') |
1108 | invoice_line_obj = self.pool.get('account.invoice.line') |
1109 | - invoices_group = {} |
1110 | + product_pool = self.pool.get('product.product') |
1111 | + account_fiscal_position_pool = self.pool.get('account.fiscal.position') |
1112 | res = {} |
1113 | - |
1114 | - for settlement in self.browse(cursor, user, ids, context=context): |
1115 | - |
1116 | + for settlement in self.browse(cr, uid, ids, context=context): |
1117 | payment_term_id = False |
1118 | partner = settlement.agent_id and settlement.agent_id.partner_id |
1119 | if not partner: |
1120 | raise osv.except_osv(_('Error, partner fail !'), |
1121 | - _('Agent to settle hasn\'t assigned partner.')) |
1122 | - |
1123 | + _('Agent to settle hasn\'t assigned partner.')) |
1124 | #El tipo es de facura de proveedor |
1125 | account_id = partner.property_account_payable.id |
1126 | - |
1127 | address_contact_id, address_invoice_id = \ |
1128 | - self._get_address_invoice(cursor, user, settlement).values() |
1129 | - |
1130 | + self._get_address_invoice(cr, uid, settlement, context=context).values() |
1131 | # No se agrupa |
1132 | - |
1133 | invoice_vals = { |
1134 | 'name': settlement.settlement_id.name, |
1135 | - 'origin': (settlement.settlement_id.name or '') , |
1136 | + 'origin': (settlement.settlement_id.name or ''), |
1137 | 'type': 'in_invoice', |
1138 | 'account_id': account_id, |
1139 | 'partner_id': partner.id, |
1140 | 'address_invoice_id': address_invoice_id, |
1141 | 'address_contact_id': address_contact_id, |
1142 | - #'comment': comment, |
1143 | 'payment_term': payment_term_id, |
1144 | 'fiscal_position': partner.property_account_position.id |
1145 | - } |
1146 | - cur_id = self.get_currency_id(cursor, user, settlement) |
1147 | + } |
1148 | + cur_id = self.get_currency_id(cr, uid, settlement, context=context) |
1149 | if cur_id: |
1150 | invoice_vals['currency_id'] = cur_id |
1151 | if journal_id: |
1152 | invoice_vals['journal_id'] = journal_id |
1153 | - invoice_id = invoice_obj.create(cursor, user, invoice_vals, |
1154 | - context=context) |
1155 | - |
1156 | - |
1157 | + invoice_id = invoice_obj.create(cr, uid, invoice_vals, context=context) |
1158 | res[settlement.id] = invoice_id |
1159 | # El producto se selecciona en el wizard correspondiente |
1160 | - product = self.pool.get('product.product').browse(cursor,user,product_id) |
1161 | + product = product_pool.browse(cr, uid, product_id, context=context) |
1162 | account_id = product.product_tmpl_id.property_account_expense.id |
1163 | if not account_id: |
1164 | account_id = product.categ_id.property_account_expense_categ.id |
1165 | # Cálculo de los impuestos a aplicar |
1166 | - |
1167 | taxes = product.supplier_taxes_id |
1168 | - |
1169 | # se añade la retención seleccionada de la ficha del agente |
1170 | if settlement.agent_id and settlement.agent_id.retention_id: |
1171 | taxes.append(settlement.agent_id.retention_id) |
1172 | - if settlement.agent_id and settlement.agent_id.partner_id: |
1173 | - tax_ids = self.pool.get('account.fiscal.position').map_tax( |
1174 | - cursor, |
1175 | - user, |
1176 | + if settlement.agent_id and settlement.agent_id.partner_id: |
1177 | + tax_ids = self.pool.get('account.fiscal.position').map_tax( |
1178 | + cr, |
1179 | + uid, |
1180 | settlement.agent_id.partner_id.property_account_position, |
1181 | taxes |
1182 | ) |
1183 | @@ -304,12 +275,12 @@ |
1184 | name = invoice.invoice_number |
1185 | price_unit = invoice.settled_amount |
1186 | discount = 0 |
1187 | - |
1188 | #set UoS if it's a sale and the picking doesn't have one |
1189 | - uos_id = False |
1190 | - |
1191 | - account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, partner.property_account_position, account_id) |
1192 | - invoice_line_id = invoice_line_obj.create(cursor, user, { |
1193 | + uos_id = False |
1194 | + account_id = account_fiscal_position_pool.map_account(cr, uid, |
1195 | + partner.property_account_position, account_id, |
1196 | + context=context) |
1197 | + invoice_line_id = invoice_line_obj.create(cr, uid, { |
1198 | 'name': name, |
1199 | 'origin': origin, |
1200 | 'invoice_id': invoice_id, |
1201 | @@ -320,102 +291,108 @@ |
1202 | 'discount': discount, |
1203 | 'quantity': 1, |
1204 | 'invoice_line_tax_id': [(6, 0, tax_ids)], |
1205 | - #'account_analytic_id': account_analytic_id, |
1206 | - }, context=context) |
1207 | - self._invoice_line_hook(cursor, user, invoice, invoice_line_id) |
1208 | - |
1209 | - invoice_obj.button_compute(cursor, user, [invoice_id], context=context, |
1210 | - set_total=(type in ('in_invoice', 'in_refund'))) |
1211 | - self._invoice_hook(cursor, user, settlement, invoice_id) |
1212 | + }, context=context) |
1213 | + self._invoice_line_hook(cr, uid, invoice, invoice_line_id, context=context) |
1214 | + invoice_obj.button_compute(cr, uid, [invoice_id], context=context, |
1215 | + set_total=(type in ('in_invoice', 'in_refund'))) |
1216 | + self._invoice_hook(cr, uid, settlement, invoice_id, context=context) |
1217 | return res |
1218 | |
1219 | - def calcula(self, cr, uid, ids, date_from, date_to): |
1220 | - set_agent = self.browse(cr, uid, ids) |
1221 | - user = self.pool.get('res.users').browse(cr, uid, uid) |
1222 | + def calcula(self, cr, uid, ids, date_from, date_to, context=None): |
1223 | + if context is None: |
1224 | + context = {} |
1225 | + settlement_line_pool = self.pool.get('settlement.line') |
1226 | + invoice_line_agent_pool = self.pool.get('invoice.line.agent') |
1227 | + set_agent = self.browse(cr, uid, ids, context=context) |
1228 | + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) |
1229 | # Recalculamos todas las lineas sujetas a comision |
1230 | - |
1231 | sql = 'SELECT invoice_line_agent.id FROM account_invoice_line ' \ |
1232 | 'INNER JOIN invoice_line_agent ON invoice_line_agent.invoice_line_id=account_invoice_line.id ' \ |
1233 | 'INNER JOIN account_invoice ON account_invoice_line.invoice_id = account_invoice.id ' \ |
1234 | - 'WHERE invoice_line_agent.agent_id=' + str(set_agent.agent_id.id) + ' AND invoice_line_agent.settled=True ' \ |
1235 | + 'WHERE invoice_line_agent.agent_id=' + str(set_agent.agent_id.id) + ' ' \ |
1236 | + 'AND invoice_line_agent.settled=True ' \ |
1237 | 'AND account_invoice.state<>\'draft\' AND account_invoice.type=\'out_invoice\''\ |
1238 | - 'AND account_invoice.date_invoice >= \'' + date_from + '\' AND account_invoice.date_invoice <= \'' + date_to +'\''\ |
1239 | - ' AND account_invoice.company_id = ' + str(user.company_id.id) |
1240 | - |
1241 | + 'AND account_invoice.date_invoice >= \'' + date_from + '\' ' \ |
1242 | + 'AND account_invoice.date_invoice <= \'' + date_to + '\' ' \ |
1243 | + 'AND account_invoice.company_id = ' + str(user.company_id.id) |
1244 | cr.execute(sql) |
1245 | res = cr.fetchall() |
1246 | inv_line_agent_ids = [x[0] for x in res] |
1247 | - |
1248 | - self.pool.get ('invoice.line.agent').calculate_commission( cr, uid, inv_line_agent_ids) |
1249 | - |
1250 | + invoice_line_agent_pool .calculate_commission(cr, uid, inv_line_agent_ids, context=context) |
1251 | sql = 'SELECT account_invoice_line.id FROM account_invoice_line ' \ |
1252 | 'INNER JOIN invoice_line_agent ON invoice_line_agent.invoice_line_id=account_invoice_line.id ' \ |
1253 | 'INNER JOIN account_invoice ON account_invoice_line.invoice_id = account_invoice.id ' \ |
1254 | - 'WHERE invoice_line_agent.agent_id=' + str(set_agent.agent_id.id) + ' AND invoice_line_agent.settled=False ' \ |
1255 | - 'AND account_invoice.state<>\'draft\' AND account_invoice.type=\'out_invoice\''\ |
1256 | - 'AND account_invoice.date_invoice >= \'' + date_from + '\' AND account_invoice.date_invoice <= \'' + date_to +'\''\ |
1257 | - ' AND account_invoice.company_id = ' + str(user.company_id.id) |
1258 | - |
1259 | + 'WHERE invoice_line_agent.agent_id=' + str(set_agent.agent_id.id) + ' ' \ |
1260 | + 'AND invoice_line_agent.settled=False ' \ |
1261 | + 'AND account_invoice.state<>\'draft\' AND account_invoice.type=\'out_invoice\'' \ |
1262 | + 'AND account_invoice.date_invoice >= \'' + date_from + '\' ' \ |
1263 | + 'AND account_invoice.date_invoice <= \'' + date_to + '\' ' \ |
1264 | + 'AND account_invoice.company_id = ' + str(user.company_id.id) |
1265 | cr.execute(sql) |
1266 | res = cr.fetchall() |
1267 | inv_line_ids = [x[0] for x in res] |
1268 | total_per = 0 |
1269 | total_sections = 0 |
1270 | - total = 0 |
1271 | sections = {} |
1272 | for inv_line_id in inv_line_ids: |
1273 | - linea_id = self.pool.get('settlement.line').create(cr, uid, {'invoice_line_id': inv_line_id, 'settlement_agent_id': ids}) |
1274 | - self.pool.get('settlement.line').calcula(cr, uid, linea_id) |
1275 | - |
1276 | - line = self.pool.get('settlement.line').browse(cr, uid, linea_id) |
1277 | - |
1278 | + linea_id = settlement_line_pool.create(cr, uid, |
1279 | + {'invoice_line_id': inv_line_id, 'settlement_agent_id': ids}, |
1280 | + context=context) |
1281 | + settlement_line_pool.calcula(cr, uid, linea_id, context=context) |
1282 | + line = settlement_line_pool.browse(cr, uid, linea_id, context=context) |
1283 | # Marca la comision en la factura como liquidada y establece la cantidad |
1284 | # Si es por tramos la cantidad será cero, pero se reflejará sobre el tramo del Agente |
1285 | - |
1286 | - |
1287 | if line.commission_id.type == "fijo": |
1288 | total_per = total_per + line.commission |
1289 | - inv_ag_ids = self.pool.get('invoice.line.agent').search(cr, uid, [('invoice_line_id', '=', inv_line_id), ('agent_id', '=', set_agent.agent_id.id)]) |
1290 | - self.pool.get('invoice.line.agent').write(cr, uid, inv_ag_ids, {'settled': True, 'quantity': line.commission}) |
1291 | - if line.commission_id.type == "tramos": |
1292 | - if line.invoice_line_id.product_id.commission_exent != True: |
1293 | - # Hacemos un agregado de la base de cálculo agrupándolo por las distintas comisiones en tramos que tenga el agente asignadas |
1294 | - if line.commission_id.id in sections: |
1295 | - sections[line.commission_id.id]['base'] = sections[line.commission_id.id]['base'] + line.invoice_line_id.price_subtotal |
1296 | - sections[line.commission_id.id]['lines'].append(line) # Añade la línea de la que se añade esta base para el cálculo por tramos |
1297 | - else: |
1298 | - sections[line.commission_id.id] = {'type': line.commission_id, 'base':line.invoice_line_id.price_subtotal, 'lines':[line]} |
1299 | - |
1300 | - #Tramos para cada tipo de comisión creados |
1301 | - |
1302 | + inv_ag_ids = invoice_line_agent_pool.search(cr, uid, [('invoice_line_id', '=', inv_line_id), |
1303 | + ('agent_id', '=', set_agent.agent_id.id)], |
1304 | + context=context) |
1305 | + invoice_line_agent_pool.write(cr, uid, inv_ag_ids, {'settled': True, 'quantity': line.commission}, |
1306 | + context=context) |
1307 | + if line.commission_id.type == "tramos" and not line.invoice_line_id.product_id.commission_exent: |
1308 | + # Hacemos un agregado de la base de cálculo agrupándolo por las distintas comisiones en tramos que |
1309 | + # tenga el agente asignadas |
1310 | + if line.commission_id.id in sections: |
1311 | + sections[line.commission_id.id]['base'] = (sections[line.commission_id.id]['base'] + |
1312 | + line.invoice_line_id.price_subtotal) |
1313 | + # Añade la línea de la que se añade esta base para el cálculo por tramos |
1314 | + sections[line.commission_id.id]['lines'].append(line) |
1315 | + else: |
1316 | + sections[line.commission_id.id] = {'type': line.commission_id, |
1317 | + 'base': line.invoice_line_id.price_subtotal, |
1318 | + 'lines': [line]} |
1319 | + #Tramos para cada tipo de comisión creados |
1320 | for tramo in sections: |
1321 | #Cálculo de la comisión para cada tramo |
1322 | sections[tramo].update({'commission': sections[tramo]['type'].calcula_tramos(sections[tramo]['base'])}) |
1323 | - total_sections = total_sections+sections[tramo]['commission'] |
1324 | + total_sections = total_sections + sections[tramo]['commission'] |
1325 | # reparto de la comisión para cada linea |
1326 | - |
1327 | - for linea_tramo in sections[tramo]['lines']: |
1328 | - com_por_linea = sections[tramo]['commission']* (linea_tramo.invoice_line_id.price_subtotal/sections[tramo]['base']) |
1329 | - linea_tramo.write({'commission':com_por_linea}) |
1330 | - inv_ag_ids = self.pool.get('invoice.line.agent').search(cr, uid, [('invoice_line_id', '=', linea_tramo.invoice_line_id.id), ('agent_id', '=', set_agent.agent_id.id)]) |
1331 | - self.pool.get('invoice.line.agent').write(cr, uid, inv_ag_ids, {'settled': True, 'quantity': com_por_linea}) |
1332 | - |
1333 | - |
1334 | + for linea_tramo in sections[tramo]['lines']: |
1335 | + com_por_linea = (sections[tramo]['commission'] * |
1336 | + (linea_tramo.invoice_line_id.price_subtotal / sections[tramo]['base'])) |
1337 | + linea_tramo.write({'commission': com_por_linea}) |
1338 | + inv_ag_ids = invoice_line_agent_pool.search(cr, uid, [ |
1339 | + ('invoice_line_id', '=', linea_tramo.invoice_line_id.id), |
1340 | + ('agent_id', '=', set_agent.agent_id.id) |
1341 | + ], context=context) |
1342 | + invoice_line_agent_pool.write(cr, uid, inv_ag_ids, {'settled': True, 'quantity': com_por_linea}, |
1343 | + context=context) |
1344 | total = total_per + total_sections |
1345 | - self.write (cr, uid, ids, {'total_per': total_per, 'total_sections': total_sections, 'total': total}) |
1346 | - |
1347 | -settlement_agent() |
1348 | - |
1349 | -class settlement_line (osv.osv): |
1350 | + self.write(cr, uid, ids, {'total_per': total_per, 'total_sections': total_sections, 'total': total}, |
1351 | + context=context) |
1352 | + |
1353 | + |
1354 | +class settlement_line (orm.Model): |
1355 | """Línea de las liquidaciones de los agentes |
1356 | - Una línea por línea de factura |
1357 | + Una línea por línea de factura |
1358 | """ |
1359 | - |
1360 | _name = 'settlement.line' |
1361 | _columns = { |
1362 | - 'invoice_id':fields.related('invoice_line_id', 'invoice_id', type='many2one', relation='account.invoice', string='Invoice'), |
1363 | - 'invoice_date':fields.related('invoice_id','date_invoice', type='date', readonly=True, string='Invoice Date'), |
1364 | - 'settlement_agent_id': fields.many2one('settlement.agent', 'Settlement agent', required=True, select=1, ondelete="cascade"), |
1365 | + 'invoice_id': fields.related('invoice_line_id', 'invoice_id', type='many2one', relation='account.invoice', |
1366 | + string='Invoice'), |
1367 | + 'invoice_date': fields.related('invoice_id', 'date_invoice', type='date', readonly=True, string='Invoice Date'), |
1368 | + 'settlement_agent_id': fields.many2one('settlement.agent', 'Settlement agent', required=True, select=1, |
1369 | + ondelete="cascade"), |
1370 | 'invoice_line_id': fields.many2one('account.invoice.line', 'Settled invoice line', required=True), |
1371 | 'amount': fields.float('Invoice line amount', readonly=True), |
1372 | 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), |
1373 | @@ -424,65 +401,74 @@ |
1374 | } |
1375 | |
1376 | _defaults = { |
1377 | - 'currency_id': lambda self,cr,uid,context: self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id |
1378 | + 'currency_id': (lambda self, cr, uid, context: |
1379 | + self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id) |
1380 | } |
1381 | |
1382 | - def calcula(self, cr, uid, ids): |
1383 | - line = self.browse(cr, uid, ids) |
1384 | - amount=0 |
1385 | - user = self.pool.get('res.users').browse(cr, uid, uid) |
1386 | - |
1387 | - for commission in line.invoice_line_id.commission_ids: # Recorre los agentes y condiciones asignados a la factura |
1388 | - if commission.agent_id.id == line.settlement_agent_id.agent_id.id: # selecciona el asignado al agente para el que está liquidando |
1389 | + def calcula(self, cr, uid, ids, context=None): |
1390 | + if context is None: |
1391 | + context = {} |
1392 | + currency_pool = self.pool.get('res.currency') |
1393 | + line = self.browse(cr, uid, ids, context=context) |
1394 | + amount = 0 |
1395 | + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) |
1396 | + # Recorre los agentes y condiciones asignados a la factura |
1397 | + for commission in line.invoice_line_id.commission_ids: |
1398 | + # selecciona el asignado al agente para el que está liquidando |
1399 | + if commission.agent_id.id == line.settlement_agent_id.agent_id.id: |
1400 | commission_app = commission.commission_id # Obtiene el objeto |
1401 | invoice_line_amount = line.invoice_line_id.price_subtotal |
1402 | - if commission_app.type=="fijo": |
1403 | + if commission_app.type == "fijo": |
1404 | commission_per = commission_app.fix_qty |
1405 | amount = amount + line.invoice_line_id.price_subtotal * float(commission_per) / 100 |
1406 | - |
1407 | - elif commission_app.type=="tramos": |
1408 | + elif commission_app.type == "tramos": |
1409 | invoice_line_amount = 0 |
1410 | amount = 0 |
1411 | - |
1412 | - cc_amount_subtotal = line.invoice_id.currency_id.id != user.company_id.currency_id.id and self.pool.get('res.currency').compute(cr, uid, line.invoice_id.currency_id.id, user.company_id.currency_id.id, invoice_line_amount, round = False) or invoice_line_amount |
1413 | - cc_commission_amount = line.invoice_id.currency_id.id != user.company_id.currency_id.id and self.pool.get('res.currency').compute(cr, uid, line.invoice_id.currency_id.id, user.company_id.currency_id.id, amount, round = False) or amount |
1414 | - |
1415 | - self.write(cr, uid, ids, {'amount': cc_amount_subtotal, 'commission_id': commission_app.id, 'commission': cc_commission_amount, 'currency_id': user.company_id.currency_id.id}) |
1416 | - |
1417 | -settlement_line() |
1418 | - |
1419 | - |
1420 | - |
1421 | -class settled_invoice_agent(osv.osv): |
1422 | + cc_amount_subtotal = (line.invoice_id.currency_id.id != user.company_id.currency_id.id and |
1423 | + currency_pool.compute(cr, uid, line.invoice_id.currency_id.id, |
1424 | + user.company_id.currency_id.id, invoice_line_amount, |
1425 | + round=False, context=context) or |
1426 | + invoice_line_amount) |
1427 | + cc_commission_amount = (line.invoice_id.currency_id.id != user.company_id.currency_id.id and |
1428 | + currency_pool.compute(cr, uid, line.invoice_id.currency_id.id, |
1429 | + user.company_id.currency_id.id, amount, round=False, |
1430 | + context=context) or |
1431 | + amount) |
1432 | + self.write(cr, uid, ids, {'amount': cc_amount_subtotal, |
1433 | + 'commission_id': commission_app.id, |
1434 | + 'commission': cc_commission_amount, |
1435 | + 'currency_id': user.company_id.currency_id.id}) |
1436 | + |
1437 | + |
1438 | +class settled_invoice_agent(orm.Model): |
1439 | _name = "settled.invoice.agent" |
1440 | _description = "Resumen de facturas liquidadas" |
1441 | _auto = False |
1442 | _columns = { |
1443 | - 'agent_id':fields.many2one('sale.agent', 'Agent', readonly=True, select=1), |
1444 | + 'agent_id': fields.many2one('sale.agent', 'Agent', readonly=True, select=1), |
1445 | 'invoice_id': fields.many2one('account.invoice', 'Invoice', readonly=True, select=1), |
1446 | - 'settlement_agent_id': fields.many2one('settlement.agent', 'Agent settl.', readonly=True, select=1, ondelete="cascade"), |
1447 | - 'invoice_number':fields.related('invoice_id', 'number', type='char', string='Invoice no', readonly=True ), |
1448 | - 'invoice_date':fields.related('invoice_id', 'date_invoice', string ='Invoice date', type='date', readonly=True, select=1 ), |
1449 | - 'invoice_amount':fields.float( 'Amount assigned in invoice', readonly=True), |
1450 | - 'settled_amount':fields.float('Settled amount', readonly=True), |
1451 | - #'currency_id': fields.many2one('res.currency', 'Currency', readonly=True, select="1") |
1452 | + 'settlement_agent_id': fields.many2one('settlement.agent', 'Agent settl.', readonly=True, select=1, |
1453 | + ondelete="cascade"), |
1454 | + 'invoice_number': fields.related('invoice_id', 'number', type='char', string='Invoice no', readonly=True), |
1455 | + 'invoice_date': fields.related('invoice_id', 'date_invoice', string='Invoice date', type='date', readonly=True, |
1456 | + select=1), |
1457 | + 'invoice_amount': fields.float('Amount assigned in invoice', readonly=True), |
1458 | + 'settled_amount': fields.float('Settled amount', readonly=True), |
1459 | } |
1460 | |
1461 | - |
1462 | def init(self, cr): |
1463 | - tools.sql.drop_view_if_exists(cr, "settled_invoice_agent") |
1464 | - |
1465 | + tools.sql.drop_view_if_exists(cr, "settled_invoice_agent", ) |
1466 | cr.execute(""" |
1467 | create or replace view settled_invoice_agent as ( |
1468 | - SELECT (account_invoice_line.invoice_id*10000+settlement_agent.agent_id) as id, settlement_agent.id as settlement_agent_id, |
1469 | - account_invoice_line.invoice_id as invoice_id, settlement_agent.agent_id as agent_id, |
1470 | - sum(settlement_line.amount) as invoice_amount, |
1471 | - sum(settlement_line.commission) as settled_amount |
1472 | + SELECT (account_invoice_line.invoice_id*10000+settlement_agent.agent_id) as id, |
1473 | + settlement_agent.id as settlement_agent_id, |
1474 | + account_invoice_line.invoice_id as invoice_id, |
1475 | + settlement_agent.agent_id as agent_id, |
1476 | + sum(settlement_line.amount) as invoice_amount, |
1477 | + sum(settlement_line.commission) as settled_amount |
1478 | FROM settlement_agent |
1479 | INNER JOIN settlement_line ON settlement_agent.id = settlement_line.settlement_agent_id |
1480 | INNER JOIN account_invoice_line ON account_invoice_line.id = settlement_line.invoice_line_id |
1481 | GROUP BY account_invoice_line.invoice_id, settlement_agent.agent_id, settlement_agent.id |
1482 | |
1483 | )""") |
1484 | - |
1485 | -settled_invoice_agent() |
1486 | |
1487 | === modified file 'sale_commission/stock_picking.py' |
1488 | --- sale_commission/stock_picking.py 2012-07-13 10:51:04 +0000 |
1489 | +++ sale_commission/stock_picking.py 2013-12-23 18:02:44 +0000 |
1490 | @@ -22,44 +22,41 @@ |
1491 | |
1492 | """Modificamos la creación de factura desde albarán para incluir el comportamiento de comisiones""" |
1493 | |
1494 | -from osv import osv, fields |
1495 | - |
1496 | -class product_product(osv.osv): |
1497 | +from osv import orm, fields |
1498 | + |
1499 | + |
1500 | +class product_product(orm.Model): |
1501 | _inherit = 'product.product' |
1502 | - |
1503 | _columns = { |
1504 | - 'commission_exent':fields.boolean('Commission exent') |
1505 | + 'commission_exent': fields.boolean('Commission exent') |
1506 | } |
1507 | _defaults = { |
1508 | - 'commission_exent': lambda *a: False, |
1509 | + 'commission_exent': False, |
1510 | } |
1511 | -product_product() |
1512 | - |
1513 | -class stock_picking(osv.osv): |
1514 | + |
1515 | + |
1516 | +class stock_picking(orm.Model): |
1517 | """Modificamos la creación de factura desde albarán para incluir el comportamiento de comisiones""" |
1518 | |
1519 | _inherit = 'stock.picking' |
1520 | - |
1521 | _columns = { |
1522 | - 'agent_ids':fields.many2many('sale.agent','sale_agent_clinic_rel', 'agent_id', 'clinic_id', 'Agentes' ) |
1523 | + 'agent_ids': fields.many2many('sale.agent', 'sale_agent_clinic_rel', 'agent_id', 'clinic_id', 'Agentes') |
1524 | } |
1525 | |
1526 | - def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id): |
1527 | + def _invoice_line_hook(self, cr, uid, move_line, invoice_line_id, context=None): |
1528 | '''Call after the creation of the invoice line''' |
1529 | - super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id) |
1530 | - |
1531 | - if move_line and move_line.sale_line_id and move_line.sale_line_id.product_id.commission_exent != True : |
1532 | + if context is None: |
1533 | + context = {} |
1534 | + agent_pool = self.pool.get('invoice.line.agent') |
1535 | + super(stock_picking, self)._invoice_line_hook(cr, uid, move_line, invoice_line_id, context=context) |
1536 | + if move_line and move_line.sale_line_id and not move_line.sale_line_id.product_id.commission_exent: |
1537 | so_ref = move_line.sale_line_id.order_id |
1538 | for so_agent_id in so_ref.sale_agent_ids: |
1539 | vals = { |
1540 | - 'invoice_line_id': invoice_line_id, |
1541 | - 'agent_id': so_agent_id.agent_id.id, |
1542 | - 'commission_id': so_agent_id.commission_id.id, |
1543 | - 'settled': False |
1544 | - } |
1545 | - |
1546 | - line_agent_id=self.pool.get('invoice.line.agent').create(cursor, user, vals) |
1547 | - self.pool.get('invoice.line.agent').calculate_commission(cursor, user, [line_agent_id]) |
1548 | - return |
1549 | - |
1550 | -stock_picking() |
1551 | \ No newline at end of file |
1552 | + 'invoice_line_id': invoice_line_id, |
1553 | + 'agent_id': so_agent_id.agent_id.id, |
1554 | + 'commission_id': so_agent_id.commission_id.id, |
1555 | + 'settled': False |
1556 | + } |
1557 | + line_agent_id = agent_pool.create(cr, uid, vals, context=context) |
1558 | + agent_pool.calculate_commission(cr, uid, [line_agent_id], context=context) |
1559 | |
1560 | === modified file 'sale_commission/wizard/__init__.py' |
1561 | --- sale_commission/wizard/__init__.py 2012-07-13 10:51:04 +0000 |
1562 | +++ sale_commission/wizard/__init__.py 2013-12-23 18:02:44 +0000 |
1563 | @@ -20,4 +20,4 @@ |
1564 | # |
1565 | ############################################################################## |
1566 | |
1567 | -import wizard_invoice |
1568 | \ No newline at end of file |
1569 | +import wizard_invoice |
1570 | |
1571 | === modified file 'sale_commission/wizard/wizard_invoice.py' |
1572 | --- sale_commission/wizard/wizard_invoice.py 2012-07-13 10:51:04 +0000 |
1573 | +++ sale_commission/wizard/wizard_invoice.py 2013-12-23 18:02:44 +0000 |
1574 | @@ -20,49 +20,38 @@ |
1575 | # |
1576 | ############################################################################## |
1577 | |
1578 | -from osv import fields, osv |
1579 | +from osv import fields, orm, osv |
1580 | from tools.translate import _ |
1581 | |
1582 | -class settled_invoice_wizard (osv.osv_memory): |
1583 | + |
1584 | +class settled_invoice_wizard (orm.TransientModel): |
1585 | """settled.invoice.wizard""" |
1586 | |
1587 | _name = 'settled.invoice.wizard' |
1588 | _columns = { |
1589 | - 'journal_id':fields.many2one('account.journal', 'Target journal', required=True, select=1), |
1590 | - 'product_id':fields.many2one('product.product', 'Product for account', required=True, select=1), |
1591 | + 'journal_id': fields.many2one('account.journal', 'Target journal', required=True, select=1), |
1592 | + 'product_id': fields.many2one('product.product', 'Product for account', required=True, select=1), |
1593 | } |
1594 | _defaults = { |
1595 | } |
1596 | |
1597 | - |
1598 | - |
1599 | def create_invoice(self, cr, uid, ids, context=None): |
1600 | if context is None: |
1601 | context = {} |
1602 | - |
1603 | data_pool = self.pool.get('ir.model.data') |
1604 | settlement_obj = self.pool.get('settlement') |
1605 | - |
1606 | for o in self.browse(cr, uid, ids, context=context): |
1607 | - res = settlement_obj.action_invoice_create(cr, uid, context['active_ids'], |
1608 | - journal_id=o.journal_id.id, product_id=o.product_id.id, context=context) |
1609 | - |
1610 | + res = settlement_obj.action_invoice_create( |
1611 | + cr, uid, context['active_ids'], journal_id=o.journal_id.id, product_id=o.product_id.id, context=context) |
1612 | invoice_ids = res.values() |
1613 | - |
1614 | - action_model = False |
1615 | action = {} |
1616 | if not invoice_ids[0]: |
1617 | raise osv.except_osv(_('Error'), _('No Invoices were created')) |
1618 | - |
1619 | #change state settlement |
1620 | - settlement_obj.write(cr, uid, context['active_ids'], {'state':'invoiced'}) |
1621 | - |
1622 | - action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2") |
1623 | - |
1624 | + settlement_obj.write(cr, uid, context['active_ids'], {'state': 'invoiced'}, context=context) |
1625 | + action_model, action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2") |
1626 | if action_model: |
1627 | action_pool = self.pool.get(action_model) |
1628 | action = action_pool.read(cr, uid, action_id, context=context) |
1629 | - action['domain'] = "[('id','in', ["+','.join(map(str,invoice_ids[0]))+"])]" |
1630 | + action['domain'] = "[('id','in', [" + ','.join(map(str, invoice_ids[0])) + "])]" |
1631 | return action |
1632 | - |
1633 | -settled_invoice_wizard () |
As you said, there is still things to improve as untranslated stuffs. But this is already a good step forward. Thanks.
LGTM (not tested).