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 _create_chained_picking(self, cr, uid, pick_name, picking, ptype, move, context=None):
31 res = super(stock_move, self)._create_chained_picking(cr, uid, pick_name, picking, ptype, move, context=context)
33 self.pool.get('stock.picking').write(cr, uid, [res], {'sale_id': picking.sale_id.id})
37 class stock_picking(osv.osv):
38 _inherit = 'stock.picking'
40 'sale_id': fields.many2one('sale.order', 'Sales Order', ondelete='set null', select=True),
46 def get_currency_id(self, cursor, user, picking):
48 return picking.sale_id.pricelist_id.currency_id.id
50 return super(stock_picking, self).get_currency_id(cursor, user, picking)
52 def _get_payment_term(self, cursor, user, picking):
53 if picking.sale_id and picking.sale_id.payment_term:
54 return picking.sale_id.payment_term.id
55 return super(stock_picking, self)._get_payment_term(cursor, user, picking)
57 def _get_address_invoice(self, cursor, user, picking):
60 res['contact'] = picking.sale_id.partner_order_id.id
61 res['invoice'] = picking.sale_id.partner_invoice_id.id
63 return super(stock_picking, self)._get_address_invoice(cursor, user, picking)
65 def _get_comment_invoice(self, cursor, user, picking):
66 if picking.note or (picking.sale_id and picking.sale_id.note):
67 return picking.note or picking.sale_id.note
68 return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
70 def _get_price_unit_invoice(self, cursor, user, move_line, type):
71 if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
72 uom_id = move_line.product_id.uom_id.id
73 uos_id = move_line.product_id.uos_id and move_line.product_id.uos_id.id or False
74 price = move_line.sale_line_id.price_unit
75 coeff = move_line.product_id.uos_coeff
76 if uom_id != uos_id and coeff != 0:
77 price_unit = price / coeff
79 return move_line.sale_line_id.price_unit
80 return super(stock_picking, self)._get_price_unit_invoice(cursor, user, move_line, type)
82 def _get_discount_invoice(self, cursor, user, move_line):
83 if move_line.sale_line_id:
84 return move_line.sale_line_id.discount
85 return super(stock_picking, self)._get_discount_invoice(cursor, user, move_line)
87 def _get_taxes_invoice(self, cursor, user, move_line, type):
88 if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
89 return [x.id for x in move_line.sale_line_id.tax_id]
90 return super(stock_picking, self)._get_taxes_invoice(cursor, user, move_line, type)
92 def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
94 return picking.sale_id.project_id.id
95 return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
97 def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
98 sale_line_obj = self.pool.get('sale.order.line')
99 invoice_line_obj = self.pool.get('account.invoice.line')
100 if move_line.sale_line_id:
101 sale_line_obj.write(cursor, user, [move_line.sale_line_id.id],
104 'invoice_lines': [(4, invoice_line_id)],
106 invoice_line_obj.write(cursor, user, [invoice_line_id], {'note': move_line.sale_line_id.notes,})
107 return super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id)
109 def _invoice_hook(self, cursor, user, picking, invoice_id):
110 sale_obj = self.pool.get('sale.order')
112 sale_obj.write(cursor, user, [picking.sale_id.id], {
113 'invoice_ids': [(4, invoice_id)],
115 return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
117 def action_invoice_create(self, cursor, user, ids, journal_id=False,
118 group=False, type='out_invoice', context=None):
121 invoice_obj = self.pool.get('account.invoice')
122 picking_obj = self.pool.get('stock.picking')
123 invoice_line_obj = self.pool.get('account.invoice.line')
124 fiscal_position_obj = self.pool.get('account.fiscal.position')
125 order_line_obj = self.pool.get('sale.order.line')
127 result = super(stock_picking, self).action_invoice_create(cursor, user,
128 ids, journal_id=journal_id, group=group, type=type,
130 picking_ids = result.keys()
131 invoice_ids = result.values()
133 for invoice in invoice_obj.browse(cursor, user, invoice_ids,
135 invoices[invoice.id] = invoice
137 for picking in picking_obj.browse(cursor, user, picking_ids,
139 if not picking.sale_id:
141 sale_lines = picking.sale_id.order_line
142 invoice_created = invoices[result[picking.id]]
143 if picking.sale_id.user_id:
144 invoice_obj.write(cursor, user, [invoice_created.id], {'user_id': picking.sale_id.user_id.id}, context=context)
146 if picking.sale_id.fiscal_position:
147 invoice_obj.write(cursor, user, [invoice_created.id], {'fiscal_position': picking.sale_id.fiscal_position.id}, context=context)
148 if picking.sale_id.client_order_ref:
149 inv_name = picking.sale_id.client_order_ref + " : " + invoice_created.name
150 invoice_obj.write(cursor, user, [invoice_created.id], {'name': inv_name}, context=context)
151 for sale_line in sale_lines:
152 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
154 type = context.get('inv_type', False)
156 name = picking.name + '-' + sale_line.name
158 name = sale_line.name
159 if type in ('out_invoice', 'out_refund'):
160 account_id = sale_line.product_id.product_tmpl_id.\
161 property_account_income.id
163 account_id = sale_line.product_id.categ_id.\
164 property_account_income_categ.id
166 account_id = sale_line.product_id.product_tmpl_id.\
167 property_account_expense.id
169 account_id = sale_line.product_id.categ_id.\
170 property_account_expense_categ.id
171 price_unit = sale_line.price_unit
172 discount = sale_line.discount
173 tax_ids = sale_line.tax_id
174 tax_ids = map(lambda x: x.id, tax_ids)
176 account_analytic_id = self._get_account_analytic_invoice(cursor,
177 user, picking, sale_line)
179 account_id = fiscal_position_obj.map_account(cursor, user, picking.sale_id.partner_id.property_account_position, account_id)
180 invoice = invoices[result[picking.id]]
181 invoice_line_id = invoice_line_obj.create(cursor, user, {
183 'invoice_id': invoice.id,
184 'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
185 'product_id': sale_line.product_id.id,
186 'account_id': account_id,
187 'price_unit': price_unit,
188 'discount': discount,
189 'quantity': sale_line.product_uos_qty,
190 'invoice_line_tax_id': [(6, 0, tax_ids)],
191 'account_analytic_id': account_analytic_id,
192 'notes':sale_line.notes
194 order_line_obj.write(cursor, user, [sale_line.id], {'invoiced': True,
195 'invoice_lines': [(6, 0, [invoice_line_id])],
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: