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

« back to all changes in this revision

Viewing changes to analytic_distribution_supply/stock.py

  • Committer: jf
  • Date: 2012-01-10 20:57:37 UTC
  • mto: (559.2.21 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 562.
  • Revision ID: jf@ubuntu-20120110205737-gr6hukgf5ggimfup
Data supply

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
        Create a link between invoice_line and purchase_order_line. This piece of information is available on move_line.order_line_id
33
33
        """
34
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)
 
35
            self.pool.get('account.invoice.line').write(cr, uid, [invoice_line_id], 
 
36
                {'order_line_id': move_line.purchase_line_id and move_line.purchase_line_id.id or False})
42
37
        return super(stock_picking, self)._invoice_line_hook(cr, uid, move_line, invoice_line_id)
43
38
 
44
39
    def _invoice_hook(self, cr, uid, picking, invoice_id):
48
43
        """
49
44
        if invoice_id and picking:
50
45
            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
46
            if po_id:
53
47
                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
 
48
            # Copy analytic distribution from purchase order or commitment voucher (if exists)
57
49
            self.pool.get('account.invoice').fetch_analytic_distribution(cr, uid, [invoice_id])
58
50
        return super(stock_picking, self)._invoice_hook(cr, uid, picking, invoice_id)
59
51
 
60
 
# action_invoice_create method have been removed because of impossibility to retrieve DESTINATION from SO.
61
 
 
62
52
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
53
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: