~savoirfairelinux-openerp/purchase-wkfl/pallet-delivery-optimized

« back to all changes in this revision

Viewing changes to purchase_crate_pallet/purchase.py

  • Committer: Alexandre Boily
  • Date: 2013-08-12 19:28:55 UTC
  • Revision ID: alexandre.boily@savoirfairelinux.com-20130812192855-16et2uxzb9sju7lm
Rename purchase_crate_pallet to a more evocative name

* Now called landed_cost_per_pallet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
6
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from openerp.osv import orm, fields
23
 
import openerp.addons.decimal_precision as dp
24
 
 
25
 
class purchase_order(orm.Model):
26
 
 
27
 
    _inherit = 'purchase.order'
28
 
 
29
 
    def _landed_cost_base_pallet(self, cr, uid, ids, name, args, context):
30
 
        if not ids:
31
 
            return {}
32
 
 
33
 
        result = {}
34
 
        landed_costs_base_pallet = 0.0
35
 
 
36
 
        for line in self.browse(cr, uid, ids):
37
 
            if line.landed_cost_line_ids:
38
 
                for costs in line.landed_cost_line_ids:
39
 
                    if costs.price_type == 'per_pallet':
40
 
                        landed_costs_base_pallet += costs.amount
41
 
            result[line.id] = landed_costs_base_pallet
42
 
 
43
 
        return result
44
 
 
45
 
    _columns = {
46
 
        'landed_cost_base_pallet': fields.function(_landed_cost_base_pallet, digits_compute=dp.get_precision('Account'), string='Landed Costs Base Pallet'),
47
 
    }
48
 
 
49
 
 
50
 
class purchase_order_line(orm.Model):
51
 
 
52
 
    _inherit = 'purchase.order.line'
53
 
 
54
 
    def _product_quantity(self, cursor, user, ids, name, arg, context=None):
55
 
        res = {}
56
 
        for line in self.browse(cursor, user, ids, context=context):
57
 
            if not line.nb_crates_per_pallet or not line.nb_pallets:
58
 
                res[line.id] = 0
59
 
            else:
60
 
                res[line.id] = line.nb_crates_per_pallet * line.nb_pallets
61
 
        return res
62
 
 
63
 
    def _landing_cost_order(self, cr, uid, ids, name, args, context):
64
 
        if not ids:
65
 
            return {}
66
 
 
67
 
        result = {}
68
 
 
69
 
        lines = self.browse(cr, uid, ids)
70
 
 
71
 
        # Pre-compute total number of pallets
72
 
        pallets_total = 0.0
73
 
        for line in lines:
74
 
            if line.order_id.landed_cost_line_ids:
75
 
                pallets_total += line.nb_pallets
76
 
 
77
 
        # Landed costs line by line
78
 
        for line in lines:
79
 
            landed_costs = 0.0
80
 
            # distribution of landed costs of PO
81
 
            if line.order_id.landed_cost_line_ids:
82
 
                # Base value (Absolute Value)
83
 
                landed_costs += line.order_id.landed_cost_base_value / line.order_id.amount_total * line.price_subtotal
84
 
 
85
 
                # Base quantity (Per Quantity)
86
 
                landed_costs += line.order_id.landed_cost_base_quantity / line.order_id.quantity_total * line.product_qty
87
 
 
88
 
                # Base pallet (Per Pallet)
89
 
                landed_costs += line.order_id.landed_cost_base_pallet / pallets_total * line.nb_pallets
90
 
            result[line.id] = landed_costs
91
 
 
92
 
        return result
93
 
 
94
 
    _columns = {
95
 
        'nb_pallets': fields.integer('Pallets'),
96
 
        'nb_crates_per_pallet': fields.integer('Crates per pallet'),
97
 
        'product_qty': fields.function(_product_quantity, string="Quantity", type='float'),
98
 
        'landing_costs_order' : fields.function(_landing_cost_order, digits_compute=dp.get_precision('Account'), string='Landing Costs from Order'),
99
 
    }
100
 
 
101
 
 
102
 
class landed_cost_position(orm.Model):
103
 
 
104
 
    _inherit = 'landed.cost.position'
105
 
 
106
 
    _columns = {
107
 
        'price_type': fields.selection([('per_unit','Per Quantity'), ('value','Absolute Value'), ('per_pallet', 'Per Pallet')], 'Amount Type', required=True, help="Defines if the amount is to be calculated for each quantity or an absolute value"),
108
 
    }