1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
24 class stock_move(osv.osv):
25 _inherit = 'stock.move'
27 'sale_line_id': fields.many2one('sale.order.line', 'Sales Order Line', ondelete='set null', select=True, readonly=True),
30 def _prepare_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
31 values = super(stock_move, self)._prepare_chained_picking(cr, uid, picking_name, picking, picking_type, moves_todo, context=context)
33 values['sale_id'] = picking.sale_id.id
36 class stock_picking(osv.osv):
37 _inherit = 'stock.picking'
39 'sale_id': fields.many2one('sale.order', 'Sales Order', ondelete='set null', select=True),
45 def get_currency_id(self, cursor, user, picking):
47 return picking.sale_id.pricelist_id.currency_id.id
49 return super(stock_picking, self).get_currency_id(cursor, user, picking)
51 def _get_partner_to_invoice(self, cr, uid, picking, context=None):
52 """ Inherit the original function of the 'stock' module
53 We select the partner of the sale order as the partner of the customer invoice
56 return picking.sale_id.partner_id
57 return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
59 def _get_comment_invoice(self, cursor, user, picking):
60 if picking.note or (picking.sale_id and picking.sale_id.note):
61 return picking.note or picking.sale_id.note
62 return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
64 def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
65 """ Inherit the original function of the 'stock' module in order to override some
66 values if the picking has been generated by a sale order
68 invoice_vals = super(stock_picking, self)._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
70 invoice_vals['address_contact_id'] = picking.sale_id.partner_order_id.id
71 invoice_vals['address_invoice_id'] = picking.sale_id.partner_invoice_id.id
72 invoice_vals['fiscal_position'] = picking.sale_id.fiscal_position.id
73 invoice_vals['payment_term'] = picking.sale_id.payment_term.id
74 invoice_vals['user_id'] = picking.sale_id.user_id.id
77 def _get_price_unit_invoice(self, cursor, user, move_line, type):
78 if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
79 uom_id = move_line.product_id.uom_id.id
80 uos_id = move_line.product_id.uos_id and move_line.product_id.uos_id.id or False
81 price = move_line.sale_line_id.price_unit
82 coeff = move_line.product_id.uos_coeff
83 if uom_id != uos_id and coeff != 0:
84 price_unit = price / coeff
86 return move_line.sale_line_id.price_unit
87 return super(stock_picking, self)._get_price_unit_invoice(cursor, user, move_line, type)
89 def _get_discount_invoice(self, cursor, user, move_line):
90 if move_line.sale_line_id:
91 return move_line.sale_line_id.discount
92 return super(stock_picking, self)._get_discount_invoice(cursor, user, move_line)
94 def _get_taxes_invoice(self, cursor, user, move_line, type):
95 if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
96 return [x.id for x in move_line.sale_line_id.tax_id]
97 return super(stock_picking, self)._get_taxes_invoice(cursor, user, move_line, type)
99 def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
101 return picking.sale_id.project_id.id
102 return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
104 def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
105 sale_line_obj = self.pool.get('sale.order.line')
106 invoice_line_obj = self.pool.get('account.invoice.line')
107 if move_line.sale_line_id:
108 sale_line_obj.write(cursor, user, [move_line.sale_line_id.id],
111 'invoice_lines': [(4, invoice_line_id)],
113 invoice_line_obj.write(cursor, user, [invoice_line_id], {'note': move_line.sale_line_id.notes,})
114 return super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id)
116 def _invoice_hook(self, cursor, user, picking, invoice_id):
117 sale_obj = self.pool.get('sale.order')
119 sale_obj.write(cursor, user, [picking.sale_id.id], {
120 'invoice_ids': [(4, invoice_id)],
122 return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
124 def action_invoice_create(self, cursor, user, ids, journal_id=False,
125 group=False, type='out_invoice', context=None):
128 invoice_obj = self.pool.get('account.invoice')
129 picking_obj = self.pool.get('stock.picking')
130 invoice_line_obj = self.pool.get('account.invoice.line')
131 fiscal_position_obj = self.pool.get('account.fiscal.position')
132 order_line_obj = self.pool.get('sale.order.line')
134 result = super(stock_picking, self).action_invoice_create(cursor, user,
135 ids, journal_id=journal_id, group=group, type=type,
137 picking_ids = result.keys()
138 invoice_ids = result.values()
140 for invoice in invoice_obj.browse(cursor, user, invoice_ids,
142 invoices[invoice.id] = invoice
144 for picking in picking_obj.browse(cursor, user, picking_ids,
146 if not picking.sale_id or picking.backorder_id:
148 sale_lines = picking.sale_id.order_line
149 invoice_created = invoices[result[picking.id]]
150 if picking.sale_id.user_id:
151 invoice_obj.write(cursor, user, [invoice_created.id], {'user_id': picking.sale_id.user_id.id}, context=context)
153 if picking.sale_id.fiscal_position:
154 invoice_obj.write(cursor, user, [invoice_created.id], {'fiscal_position': picking.sale_id.fiscal_position.id}, context=context)
155 if picking.sale_id.client_order_ref:
156 inv_name = picking.sale_id.client_order_ref + " : " + invoice_created.name
157 invoice_obj.write(cursor, user, [invoice_created.id], {'name': inv_name}, context=context)
158 for sale_line in sale_lines:
159 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
161 type = context.get('inv_type', False)
163 name = picking.name + '-' + sale_line.name
165 name = sale_line.name
166 if type in ('out_invoice', 'out_refund'):
167 account_id = sale_line.product_id.product_tmpl_id.\
168 property_account_income.id
170 account_id = sale_line.product_id.categ_id.\
171 property_account_income_categ.id
173 account_id = sale_line.product_id.product_tmpl_id.\
174 property_account_expense.id
176 account_id = sale_line.product_id.categ_id.\
177 property_account_expense_categ.id
179 vals = order_line_obj._prepare_order_line_invoice_line(cursor, user, ids, sale_line, account_id, context)
180 if vals: #note: in some cases we may not want to include all service lines as invoice lines
182 vals['account_analytic_id'] = self._get_account_analytic_invoice(cursor, user, picking, sale_line)
183 vals['invoice_id'] = invoices[result[picking.id]].id
184 invoice_line_id = invoice_line_obj.create(cursor, user, vals, context=context)
185 order_line_obj.write(cursor, user, [sale_line.id], {
187 'invoice_lines': [(6, 0, [invoice_line_id])],
191 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: