Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / sale_journal / sale_journal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23
24 class sale_journal_invoice_type(osv.osv):
25     _name = 'sale_journal.invoice.type'
26     _description = 'Invoice Types'
27     _columns = {
28         'name': fields.char('Invoice Type', required=True),
29         'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the invoice type without removing it."),
30         'note': fields.text('Note'),
31         'invoicing_method': fields.selection([('simple', 'Non grouped'), ('grouped', 'Grouped')], 'Invoicing method', required=True),
32     }
33     _defaults = {
34         'active': True,
35         'invoicing_method': 'simple'
36     }
37
38 #==============================================
39 # sale journal inherit
40 #==============================================
41
42 class res_partner(osv.osv):
43     _inherit = 'res.partner'
44     _columns = {
45         'property_invoice_type': fields.property(
46             type = 'many2one',
47             relation = 'sale_journal.invoice.type',
48             string = "Invoicing Type",
49             group_name = "Accounting Properties",
50             help = "This invoicing type will be used, by default, to invoice the current partner."),
51     }
52
53     def _commercial_fields(self, cr, uid, context=None):
54         return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['property_invoice_type']
55
56
57 class picking(osv.osv):
58     _inherit = "stock.picking"
59     _columns = {
60         'invoice_type_id': fields.many2one('sale_journal.invoice.type', 'Invoice Type', readonly=True)
61     }
62
63
64 class stock_move(osv.osv):
65     _inherit = "stock.move"
66
67     def action_confirm(self, cr, uid, ids, context=None):
68         """
69             Pass the invoice type to the picking from the sales order
70             (Should also work in case of Phantom BoMs when on explosion the original move is deleted, similar to carrier_id on delivery)
71         """
72         procs_to_check = []
73         for move in self.browse(cr, uid, ids, context=context):
74             if move.procurement_id and move.procurement_id.sale_line_id and move.procurement_id.sale_line_id.order_id.invoice_type_id:
75                 procs_to_check += [move.procurement_id]
76         res = super(stock_move, self).action_confirm(cr, uid, ids, context=context)
77         pick_obj = self.pool.get("stock.picking")
78         for proc in procs_to_check:
79             pickings = list(set([x.picking_id.id for x in proc.move_ids if x.picking_id and not x.picking_id.invoice_type_id]))
80             if pickings:
81                 pick_obj.write(cr, uid, pickings, {'invoice_type_id': proc.sale_line_id.order_id.invoice_type_id.id}, context=context)
82         return res
83
84
85 class sale(osv.osv):
86     _inherit = "sale.order"
87     _columns = {
88         'invoice_type_id': fields.many2one('sale_journal.invoice.type', 'Invoice Type', help="Generate invoice based on the selected option.")
89     }
90
91     def onchange_partner_id(self, cr, uid, ids, part, context=None):
92         result = super(sale, self).onchange_partner_id(cr, uid, ids, part, context=context)
93         if part:
94             itype = self.pool.get('res.partner').browse(cr, uid, part, context=context).property_invoice_type
95             if itype:
96                 result['value']['invoice_type_id'] = itype.id
97         return result
98
99
100 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: