2
# -*- coding: utf-8 -*-
3
##############################################################################
5
# OpenERP, Open Source Management Solution
6
# Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
7
# Developer: Olivier DOSSMANN
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.
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.
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/>.
22
##############################################################################
26
class stock_picking(osv.osv):
27
_name = 'stock.picking'
28
_inherit = 'stock.picking'
30
def _invoice_line_hook(self, cr, uid, move_line, invoice_line_id):
32
Create a link between invoice_line and purchase_order_line. This piece of information is available on move_line.order_line_id
34
if invoice_line_id and move_line:
36
if move_line.purchase_line_id:
37
vals.update({'order_line_id': move_line.purchase_line_id.id})
38
if move_line.sale_line_id:
39
vals.update({'sale_order_line_id': move_line.sale_line_id.id})
41
self.pool.get('account.invoice.line').write(cr, uid, [invoice_line_id], vals)
42
return super(stock_picking, self)._invoice_line_hook(cr, uid, move_line, invoice_line_id)
44
def _invoice_hook(self, cr, uid, picking, invoice_id):
46
Create a link between invoice and purchase_order.
47
Copy analytic distribution from purchase order to invoice (or from commitment voucher if exists)
49
if invoice_id and picking:
50
po_id = picking.purchase_id and picking.purchase_id.id or False
51
so_id = picking.sale_id and picking.sale_id.id or False
53
self.pool.get('purchase.order').write(cr, uid, [po_id], {'invoice_ids': [(4, invoice_id)]})
55
self.pool.get('sale.order').write(cr, uid, [so_id], {'invoice_ids': [(4, invoice_id)]})
56
# Copy analytic distribution from purchase order or commitment voucher (if exists) or sale order
57
self.pool.get('account.invoice').fetch_analytic_distribution(cr, uid, [invoice_id])
58
return super(stock_picking, self)._invoice_hook(cr, uid, picking, invoice_id)
60
# action_invoice_create method have been removed because of impossibility to retrieve DESTINATION from SO.
64
class stock_move(osv.osv):
66
_inherit = 'stock.move'
68
def action_cancel(self, cr, uid, ids, context=None):
70
Update commitment voucher line for the given moves
75
if isinstance(ids, (int, long)):
78
for move in self.browse(cr, uid, ids, context=context):
79
# Fetch all necessary elements
80
qty = move.product_uos_qty or move.product_qty or 0.0
81
picking = move.picking_id or False
83
# If no picking then no PO have generated this stock move
85
# fetch invoice type in order to retrieve price unit
86
inv_type = self.pool.get('stock.picking')._get_invoice_type(picking) or 'out_invoice'
87
price_unit = self.pool.get('stock.picking')._get_price_unit_invoice(cr, uid, move, inv_type)
89
# If no price_unit, so no impact on commitments because no price unit have been taken for commitment calculation
91
# update all commitment voucher lines
92
if not move.purchase_line_id:
94
for cl in move.purchase_line_id.commitment_line_ids:
95
new_amount = cl.amount - (qty * price_unit)
98
self.pool.get('account.commitment.line').write(cr, uid, [cl.id], {'amount': new_amount}, context=context)
99
return super(stock_move, self).action_cancel(cr, uid, ids, context=context)
102
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: