1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
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 openerp.osv import orm, fields
23
import openerp.addons.decimal_precision as dp
25
class purchase_order(orm.Model):
27
_inherit = 'purchase.order'
29
def _landed_cost_base_pallet(self, cr, uid, ids, name, args, context):
34
landed_costs_base_pallet = 0.0
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
46
'landed_cost_base_pallet': fields.function(_landed_cost_base_pallet, digits_compute=dp.get_precision('Account'), string='Landed Costs Base Pallet'),
50
class purchase_order_line(orm.Model):
52
_inherit = 'purchase.order.line'
54
def _product_quantity(self, cursor, user, ids, name, arg, context=None):
56
for line in self.browse(cursor, user, ids, context=context):
57
if not line.nb_crates_per_pallet or not line.nb_pallets:
60
res[line.id] = line.nb_crates_per_pallet * line.nb_pallets
63
def _landing_cost_order(self, cr, uid, ids, name, args, context):
69
lines = self.browse(cr, uid, ids)
71
# Pre-compute total number of pallets
74
if line.order_id.landed_cost_line_ids:
75
pallets_total += line.nb_pallets
77
# Landed costs line by line
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
85
# Base quantity (Per Quantity)
86
landed_costs += line.order_id.landed_cost_base_quantity / line.order_id.quantity_total * line.product_qty
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
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'),
102
class landed_cost_position(orm.Model):
104
_inherit = 'landed.cost.position'
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"),