|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | ##############################################################################
|
3 | 3 | #
|
4 |
| -# Copyright (C) 2013 Agile Business Group sagl |
5 |
| -# (<http://www.agilebg.com>) |
| 4 | +# Copyright (C) 2013 Agile Business Group sagl (<http://www.agilebg.com>) |
| 5 | +# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>) |
6 | 6 | #
|
7 | 7 | # This program is free software: you can redistribute it and/or modify
|
8 | 8 | # it under the terms of the GNU Affero General Public License as published
|
|
19 | 19 | #
|
20 | 20 | ##############################################################################
|
21 | 21 |
|
22 |
| -from openerp.osv import fields, orm |
| 22 | +from openerp import models, fields, api |
23 | 23 | import openerp.addons.decimal_precision as dp
|
24 | 24 |
|
25 | 25 |
|
26 |
| -class purchase_order_line(orm.Model): |
| 26 | +class PurchaseOrderLine(models.Model): |
27 | 27 |
|
28 |
| - def _invoiced_qty(self, cursor, user, ids, name, arg, context=None): |
29 |
| - res = {} |
30 |
| - for line in self.browse(cursor, user, ids, context=context): |
31 |
| - invoiced_qty = 0.0 |
32 |
| - for invoice_line in line.invoice_lines: |
33 |
| - invoiced_qty += invoice_line.quantity |
34 |
| - res[line.id] = invoiced_qty |
35 |
| - return res |
| 28 | + @api.one |
| 29 | + @api.depends('invoice_lines', 'invoice_lines.invoice_id', |
| 30 | + 'invoice_lines.quantity') |
| 31 | + def _compute_invoiced_qty(self): |
| 32 | + self.invoiced_qty = sum(self.invoice_lines.mapped('quantity')) |
36 | 33 |
|
37 |
| - def _fully_invoiced(self, cursor, user, ids, name, arg, context=None): |
38 |
| - res = {} |
39 |
| - for line in self.browse(cursor, user, ids, context=context): |
40 |
| - res[line.id] = line.invoiced_qty == line.product_qty |
41 |
| - return res |
| 34 | + @api.one |
| 35 | + @api.depends('invoice_lines', 'invoice_lines.invoice_id', |
| 36 | + 'invoice_lines.quantity') |
| 37 | + def _compute_fully_invoiced(self): |
| 38 | + self.fully_invoiced = (self.invoiced_qty == self.product_qty) |
42 | 39 |
|
43 |
| - def _all_invoices_approved(self, cursor, user, ids, name, arg, |
44 |
| - context=None): |
45 |
| - res = {} |
46 |
| - for line in self.browse(cursor, user, ids, context=context): |
47 |
| - if line.invoice_lines: |
48 |
| - res[line.id] = not any(inv_line.invoice_id.state |
49 |
| - in ['draft', 'cancel'] |
50 |
| - for inv_line in line.invoice_lines) |
51 |
| - else: |
52 |
| - res[line.id] = False |
53 |
| - return res |
54 |
| - |
55 |
| - def _order_lines_from_invoice_line(self, cr, uid, ids, context=None): |
56 |
| - result = set() |
57 |
| - for invoice in self.pool['account.invoice'].browse(cr, uid, ids, |
58 |
| - context=context): |
59 |
| - for line in invoice.invoice_line: |
60 |
| - result.add(line.purchase_line_id.id) |
61 |
| - return list(result) |
| 40 | + @api.one |
| 41 | + def _compute_all_invoices_approved(self): |
| 42 | + if self.invoice_lines: |
| 43 | + self.all_invoices_approved = \ |
| 44 | + not any(inv_line.invoice_id.state in ['draft', 'cancel'] |
| 45 | + for inv_line in self.invoice_lines) |
| 46 | + else: |
| 47 | + self.all_invoices_approved = False |
62 | 48 |
|
63 | 49 | _inherit = 'purchase.order.line'
|
64 | 50 |
|
65 |
| - _columns = { |
66 |
| - 'invoiced_qty': fields.function( |
67 |
| - _invoiced_qty, |
68 |
| - string='Invoiced quantity', |
69 |
| - type='float', |
70 |
| - digits_compute=dp.get_precision('Product Unit of Measure'), |
71 |
| - copy=False, |
72 |
| - store={ |
73 |
| - 'account.invoice': (_order_lines_from_invoice_line, [], 5), |
74 |
| - 'purchase.order.line': (lambda self, cr, uid, ids, |
75 |
| - context=None: ids, |
76 |
| - ['invoice_lines'], 5)}), |
77 |
| - 'fully_invoiced': fields.function( |
78 |
| - _fully_invoiced, |
79 |
| - string='Fully invoiced', |
80 |
| - type='boolean', |
81 |
| - copy=False, |
82 |
| - store={ |
83 |
| - 'account.invoice': (_order_lines_from_invoice_line, [], 10), |
84 |
| - 'purchase.order.line': (lambda self, cr, uid, ids, |
85 |
| - context=None: ids, |
86 |
| - ['invoice_lines'], 10)}), |
87 |
| - 'all_invoices_approved': fields.function( |
88 |
| - _all_invoices_approved, |
89 |
| - string='All invoices approved', |
90 |
| - type='boolean'), |
91 |
| - } |
92 |
| - |
93 |
| - |
94 |
| -class purchase_order(orm.Model): |
| 51 | + invoiced_qty = fields.Float( |
| 52 | + compute='_compute_invoiced_qty', |
| 53 | + digits_compute=dp.get_precision('Product Unit of Measure'), |
| 54 | + copy=False, store=True) |
| 55 | + |
| 56 | + fully_invoiced = fields.Boolean( |
| 57 | + compute='_compute_fully_invoiced', copy=False, store=True) |
| 58 | + |
| 59 | + all_invoices_approved = fields.Boolean( |
| 60 | + compute='_compute_all_invoices_approved') |
| 61 | + |
| 62 | + |
| 63 | +class PurchaseOrder(models.Model): |
95 | 64 |
|
96 | 65 | _inherit = 'purchase.order'
|
97 | 66 |
|
98 |
| - def _invoiced(self, cursor, user, ids, name, arg, context=None): |
99 |
| - res = {} |
100 |
| - for purchase in self.browse(cursor, user, ids, context=context): |
101 |
| - res[purchase.id] = all(line.all_invoices_approved and |
102 |
| - line.fully_invoiced |
103 |
| - for line in purchase.order_line) |
104 |
| - return res |
| 67 | + @api.one |
| 68 | + def _compute_invoiced(self): |
| 69 | + self.invoiced = all(line.all_invoices_approved and line.fully_invoiced |
| 70 | + for line in self.order_line) |
| 71 | + |
| 72 | + invoiced = fields.Boolean(compute='_compute_invoiced') |
105 | 73 |
|
106 |
| - _columns = { |
107 |
| - 'invoiced': fields.function(_invoiced, string='Invoice Received', |
108 |
| - type='boolean', |
109 |
| - help="It indicates that an invoice has " |
110 |
| - "been validated"), |
111 |
| - } |
112 |
| - |
113 |
| - def _prepare_inv_line(self, cr, uid, account_id, order_line, context=None): |
114 |
| - if context is None: |
115 |
| - context = {} |
116 |
| - res = super(purchase_order, self).\ |
117 |
| - _prepare_inv_line(cr, uid, account_id, order_line, context=context) |
118 |
| - if context.get('partial_quantity_lines'): |
119 |
| - partial_quantity_lines = context.get('partial_quantity_lines') |
| 74 | + @api.model |
| 75 | + def _prepare_inv_line(self, account_id, order_line): |
| 76 | + res = super(PurchaseOrder, self).\ |
| 77 | + _prepare_inv_line(account_id, order_line) |
| 78 | + ctx = self.env.context.copy() |
| 79 | + if ctx.get('partial_quantity_lines'): |
| 80 | + partial_quantity_lines = ctx.get('partial_quantity_lines') |
120 | 81 | if partial_quantity_lines.get(order_line.id):
|
121 | 82 | res.update({'quantity':
|
122 | 83 | partial_quantity_lines.get(order_line.id)})
|
123 | 84 | return res
|
124 | 85 |
|
125 | 86 |
|
126 |
| -class account_invoice(orm.Model): |
| 87 | +class AccountInvoice(models.Model): |
127 | 88 | _inherit = 'account.invoice'
|
128 | 89 |
|
129 |
| - def invoice_validate(self, cr, uid, ids, context=None): |
130 |
| - res = super(account_invoice, self).invoice_validate(cr, uid, ids, |
131 |
| - context=context) |
132 |
| - purchase_order_obj = self.pool.get('purchase.order') |
133 |
| - po_ids = purchase_order_obj.search( |
134 |
| - cr, uid, [('invoice_ids', 'in', ids)], context=context) |
135 |
| - for purchase_order in purchase_order_obj.browse(cr, uid, po_ids, |
136 |
| - context=context): |
| 90 | + @api.multi |
| 91 | + def invoice_validate(self): |
| 92 | + res = super(AccountInvoice, self).invoice_validate() |
| 93 | + purchase_order_obj = self.env['purchase.order'] |
| 94 | + po_ids = purchase_order_obj.search([('invoice_ids', 'in', self.ids)]) |
| 95 | + for purchase_order in po_ids: |
137 | 96 | for po_line in purchase_order.order_line:
|
138 | 97 | if po_line.invoiced_qty != po_line.product_qty:
|
139 |
| - po_line.write({'invoiced': False}) |
| 98 | + po_line.invoiced = False |
140 | 99 | return res
|
0 commit comments