~account-core-editors/account-invoice-report/github-7.0

« back to all changes in this revision

Viewing changes to account_invoice_production_lot/invoice.py

  • Committer: Joel Grand-Guillaume
  • Author(s): Lorenzo Battistini
  • Date: 2014-01-17 15:10:59 UTC
  • mfrom: (40.4.3)
  • Revision ID: git-v1:c40f4f84682b4e7c861dd2f5409f2287e19537f7
[MRG][ADD] account_invoice_production_lot copied from https://code.launchpad.net/~agilebg/account-invoicing/adding_account_invoice_production_lot_7

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
#    Author: Lorenzo Battistini <lorenzo.battistini@agilebg.com>
 
6
#    Copyright (C) 2011 Domsense s.r.l. (<http://www.domsense.com>).
 
7
#    Copyright (C) 2013 Agile Business Group sagl (<http://www.agilebg.com>)
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as
 
11
#    published by the Free Software Foundation, either version 3 of the
 
12
#    License, or (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU Affero General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from openerp.osv import fields, orm
 
25
 
 
26
 
 
27
class account_invoice_line(orm.Model):
 
28
 
 
29
    def _get_prod_lots(self, cr, uid, ids, field_name, arg, context=None):
 
30
        result = {}
 
31
        for line in self.browse(cr, uid, ids, context=context):
 
32
            result[line.id] = []
 
33
            for order_line in line.order_lines:
 
34
                for move in order_line.move_ids:
 
35
                    if move.prodlot_id:
 
36
                        result[line.id].append(move.prodlot_id.id)
 
37
        return result
 
38
 
 
39
    _inherit = "account.invoice.line"
 
40
 
 
41
    _columns = {
 
42
        # order_lines is the reverse of invoice_lines field of sale module
 
43
        'order_lines': fields.many2many(
 
44
            'sale.order.line', 'sale_order_line_invoice_rel', 'invoice_id',
 
45
            'order_line_id', 'Order Lines', readonly=True),
 
46
        'prod_lot_ids': fields.function(
 
47
            _get_prod_lots, method=True, type='many2many',
 
48
            relation="stock.production.lot", string="Production Lots"),
 
49
        'displayed_lot_id': fields.many2one('stock.production.lot', 'Lot'),
 
50
        }
 
51
 
 
52
    def load_line_lots(self, cr, uid, ids, context=None):
 
53
        for line in self.browse(cr, uid, ids, context):
 
54
            if line.prod_lot_ids:
 
55
                note = u'<ul> '
 
56
                note += u' '.join([
 
57
                    u'<li>S/N {0}</li>'.format(lot.name)
 
58
                    for lot in line.prod_lot_ids
 
59
                ])
 
60
                note += u' </ul>'
 
61
                line.write({'formatted_note': note}, context=context)
 
62
        return True
 
63
 
 
64
    def create(self, cr, uid, vals, context=None):
 
65
        res = super(account_invoice_line, self).create(cr, uid, vals, context)
 
66
        if not vals.get('formatted_note'):
 
67
            self.load_line_lots(cr, uid, [res], context)
 
68
        return res
 
69
 
 
70
class account_invoice(orm.Model):
 
71
 
 
72
    def load_lines_lots(self, cr, uid, ids, context=None):
 
73
        invoices = self.browse(cr, uid, ids, context)
 
74
        inv_line_obj = self.pool.get('account.invoice.line')
 
75
        for invoice in invoices:
 
76
            inv_line_obj.load_line_lots(cr, uid, [l.id for l in invoice.invoice_line], context)
 
77
        return True
 
78
 
 
79
    _inherit = "account.invoice"