~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to analytic_distribution_supply/stock.py

UF-385 [ADD] Added consumption_calculation module

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
##############################################################################
4
 
#
5
 
#    OpenERP, Open Source Management Solution
6
 
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
7
 
#    Developer: Olivier DOSSMANN
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 osv import osv
25
 
 
26
 
class stock_picking(osv.osv):
27
 
    _name = 'stock.picking'
28
 
    _inherit = 'stock.picking'
29
 
 
30
 
    def _invoice_line_hook(self, cr, uid, move_line, invoice_line_id):
31
 
        """
32
 
        Create a link between invoice_line and purchase_order_line. This piece of information is available on move_line.order_line_id
33
 
        """
34
 
        if invoice_line_id and move_line:
35
 
            vals = {}
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})
40
 
            if vals:
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)
43
 
 
44
 
    def _invoice_hook(self, cr, uid, picking, invoice_id):
45
 
        """
46
 
        Create a link between invoice and purchase_order.
47
 
        Copy analytic distribution from purchase order to invoice (or from commitment voucher if exists)
48
 
        """
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
52
 
            if po_id:
53
 
                self.pool.get('purchase.order').write(cr, uid, [po_id], {'invoice_ids': [(4, invoice_id)]})
54
 
            if so_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)
59
 
 
60
 
# action_invoice_create method have been removed because of impossibility to retrieve DESTINATION from SO.
61
 
 
62
 
stock_picking()
63
 
 
64
 
class stock_move(osv.osv):
65
 
    _name = 'stock.move'
66
 
    _inherit = 'stock.move'
67
 
 
68
 
    def action_cancel(self, cr, uid, ids, context=None):
69
 
        """
70
 
        Update commitment voucher line for the given moves
71
 
        """
72
 
        # Some verifications
73
 
        if not context:
74
 
            context = {}
75
 
        if isinstance(ids, (int, long)):
76
 
            ids = [ids]
77
 
        # Browse all elements
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
82
 
            if not picking:
83
 
                # If no picking then no PO have generated this stock move
84
 
                continue
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)
88
 
            if not price_unit:
89
 
                # If no price_unit, so no impact on commitments because no price unit have been taken for commitment calculation
90
 
                continue
91
 
            # update all commitment voucher lines
92
 
            if not move.purchase_line_id:
93
 
                continue
94
 
            for cl in move.purchase_line_id.commitment_line_ids:
95
 
                new_amount = cl.amount - (qty * price_unit)
96
 
                if new_amount < 0.0:
97
 
                    new_amount = 0.0
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)
100
 
 
101
 
stock_move()
102
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: