1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
# Copyright (C) 2010-2012 Camptocamp Austria (<http://www.camptocamp.at>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
import decimal_precision as dp
from tools.translate import _
import logging
class landed_cost_position(osv.osv):
_name = "landed.cost.position"
_columns = \
{ 'product_id' : fields.many2one('product.product','Landed Cost Name', required=True, domain=[('landed_cost_type','!=', False)]),
'amount' : fields.float
( 'Amount'
, required=True
, digits_compute=dp.get_precision('Purchase Price')
, help="""Landed cost for stock valuation. It will be added to the price of the supplier price."""),
'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency."),
'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Optional other currency."),
'partner_id': fields.many2one('res.partner', 'Partner', help="The supplier of this cost component.", required="True"),
'price_type': fields.selection( [('per_unit','Per Quantity'), ('value','Absolute Value')], 'Amount Type', required=True, \
help="Defines if the amount is to be calculated for each quantity or an absolute value"),
'purchase_order_line_id': fields.many2one('purchase.order.line', 'Purchase Order Line'),
'purchase_order_id': fields.many2one('purchase.order', 'Purchase Order'),
'move_line_id': fields.many2one('stock.move', 'Picking Line'),
'picking_id': fields.many2one('stock.picking', 'Picking'),
}
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
if product_id:
prod_obj=self.pool.get('product.product')
prod=prod_obj.browse(cr,uid,[product_id])[0]
v = {'price_type':prod.landed_cost_type}
return {'value': v}
return {}
landed_cost_position()
#----------------------------------------------------------
# Purchase Line INHERIT
#----------------------------------------------------------
class purchase_order_line(osv.osv):
_inherit = "purchase.order.line"
def _landing_cost(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
# landed costss for the line
for line in self.browse(cr, uid, ids):
landed_costs = 0.0
if line.landed_cost_line_ids:
for costs in line.landed_cost_line_ids:
if costs.price_type == 'value':
landed_costs += costs.amount
else:
landed_costs += costs.amount * line.product_qty
result[line.id] = landed_costs
return result
def _landing_cost_order(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
# landed costs for the line
for line in self.browse(cr, uid, ids):
landed_costs = 0.0
# distribution of landed costs of PO
if line.order_id.landed_cost_line_ids:
landed_costs += line.order_id.landed_cost_base_value / line.order_id.amount_total * line.price_subtotal + \
line.order_id.landed_cost_base_quantity / line.order_id.quantity_total * line.product_qty
result[line.id] = landed_costs
return result
def _landing_cost_factor(self, cr, uid, ids, name, args, context):
"""
Calculates the percentage of landing costs that should be put on this order line
"""
for line in self.browse(cr, uid, ids):
if line.landed_cost_line_ids:
pass
def _landed_cost(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
# landed costss for the line
for line in self.browse(cr, uid, ids):
landed_costs = 0.0
landed_costs += line.price_subtotal + line.landing_costs + line.landing_costs_order
result[line.id] = landed_costs
return result
_columns = \
{
'landed_cost_line_ids': fields.one2many('landed.cost.position', 'purchase_order_line_id', 'Landed Costs Positions'),
'landing_costs' : fields.function(_landing_cost, digits_compute=dp.get_precision('Account'), string='Landing Costs'),
'landing_costs_order' : fields.function(_landing_cost_order, digits_compute=dp.get_precision('Account'), string='Landing Costs from Order'),
'landed_costs' : fields.function(_landed_cost, digits_compute=dp.get_precision('Account'), string='Landed Costs'),
}
purchase_order_line()
class purchase_order(osv.osv):
_inherit = "purchase.order"
_logger = logging.getLogger(__name__)
def _landed_cost_base_value(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
landed_costs_base_value = 0.0
for line in self.browse(cr, uid, ids):
if line.landed_cost_line_ids:
for costs in line.landed_cost_line_ids:
if costs.price_type == 'value':
landed_costs_base_value += costs.amount
result[line.id] = landed_costs_base_value
return result
def _landed_cost_base_quantity(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
landed_costs_base_quantity = 0.0
for line in self.browse(cr, uid, ids):
if line.landed_cost_line_ids:
for costs in line.landed_cost_line_ids:
if costs.price_type == 'per_unit':
landed_costs_base_quantity += costs.amount
result[line.id] = landed_costs_base_quantity
return result
def _quantity_total(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
quantity_total = 0.0
for line in self.browse(cr, uid, ids):
if line.order_line:
for pol in line.order_line:
if pol.product_qty > 0.0:
quantity_total += pol.product_qty
result[line.id] = quantity_total
return result
def _landed_cost(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
landed_costs = 0.0
# landed costss for the line
for line in self.browse(cr, uid, ids):
landed_costs += line.landing_cost_lines + line.landed_cost_base_value + line.landed_cost_base_quantity + line.amount_untaxed
result[line.id] = landed_costs
return result
def _landing_cost_lines(self, cr, uid, ids, name, args, context):
if not ids : return {}
result = {}
landed_cost_lines = 0.0
for line in self.browse(cr, uid, ids):
if line.order_line:
for pol in line.order_line:
if pol.product_qty > 0.0:
landed_cost_lines += pol.landing_costs
result[line.id] = landed_cost_lines
return result
_columns = \
{
'landed_cost_line_ids': fields.one2many('landed.cost.position', 'purchase_order_id', 'Landed Costs'),
'landed_cost_base_value' : fields.function(_landed_cost_base_value, digits_compute=dp.get_precision('Account'), string='Landed Costs Base Value'),
'landed_cost_base_quantity' : fields.function(_landed_cost_base_quantity, digits_compute=dp.get_precision('Account'), string='Landed Costs Base Quantity'),
'landing_cost_lines' : fields.function(_landing_cost_lines, digits_compute=dp.get_precision('Account'), string='Landing Cost Lines'),
'landed_cost' : fields.function(_landed_cost, digits_compute=dp.get_precision('Account'), string='Landed Costs Total Untaxed'),
'quantity_total' : fields.function(_quantity_total, digits_compute=dp.get_precision('Product UoM'), string='Total Quantity'),
}
def _prepare_order_line_move(self, cr, uid, order, order_line, picking_id, context=None):
res = super(purchase_order,self)._prepare_order_line_move( cr, uid, order, order_line, picking_id, context)
res['price_unit_net'] = res['price_unit']
res['price_unit'] = order_line.landed_costs / order_line.product_qty
return res
def _prepare_order_picking(self, cr, uid, order, context=None):
res = super(purchase_order,self)._prepare_order_picking( cr, uid, order, context)
return res
def _get_product_account_expense_id(self, product):
"""
Returns the product's account expense id if present
or it's parent categories account expense id otherwise
"""
if product.property_account_expense.id:
return product.property_account_expense.id
return product.categ_id.property_account_expense_categ.id
def _create_pickings(self, cr, uid, order, order_lines, picking_id=False, context=None):
res = super(purchase_order,self)._create_pickings(cr, uid, order, order_lines, picking_id, context)
pick_id = int(res[0])
# landing costs Invoices from PO
#cost_obj = self.pool.get('landed.cost.position')
invoice_obj = self.pool.get('account.invoice')
invoice_line_obj = self.pool.get('account.invoice.line')
journal_obj = self.pool.get('account.journal')
journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', order.company_id.id)], limit=1)
for order_cost in order.landed_cost_line_ids:
vals_inv = {
'partner_id' : order_cost.partner_id.id
#,'amount' : order_cost.amount
#,'amount_currency' : order_cost.amount_currency
,'currency_id' : order_cost.currency_id.id or order.company_id.currency_id.id
,'account_id' : order_cost.partner_id.property_account_payable.id
,'type' : 'in_invoice'
,'origin' : order.name
,'fiscal_position': order.partner_id.property_account_position and order.partner_id.property_account_position.id or False
,'company_id': order.company_id.id
,'journal_id': len(journal_ids) and journal_ids[0] or False
}
self._logger.debug('vals inv`%s`', vals_inv)
#cost_obj.create(cr, uid, vals, context=None)
inv_id = invoice_obj.create(cr, uid, vals_inv, context=None)
vals_line = {
'product_id' : order_cost.product_id.id
,'name' : order_cost.product_id.name
#,'amount' : order_cost.amount
#,'amount_currency' : order_cost.amount_currency
#,'picking_id' : pick_id
,'account_id' : self._get_product_account_expense_id(order_cost.product_id)
,'partner_id' : order_cost.partner_id.id
,'invoice_id' : inv_id
,'price_unit' : order_cost.amount
,'invoice_line_tax_id': [(6, 0, [x.id for x in order_cost.product_id.supplier_taxes_id])],
}
self._logger.debug('vals line `%s`', vals_line)
inv_line_id = invoice_line_obj.create(cr, uid, vals_line, context=None)
#self.pool.get('landed.cost.position').create(cr, uid, cost_lines, context=None)
# landing costs for PICK Lines from PO
#pick_obj = self.pool.get('stock.picking')
#for pick in pick_obj.browse(cr, uid, [pick_id], context=None):
# self._logger.debug('pick `%s`', pick)
# for line in pick.move_lines:
# self._logger.debug('line `%s`', line)
# for order_cost in line.purchase_line_id.landed_cost_line_ids:
# vals = {}
# vals['product_id'] = order_cost.product_id.id
# vals['partner_id'] = order_cost.partner_id.id
# vals['amount'] = order_cost.amount
# vals['amount_currency'] = order_cost.amount_currency
# vals['currency_id'] = order_cost.currency_id.id
# vals['price_type'] = order_cost.price_type
# vals['move_line_id'] = line.id
# self._logger.debug('vals `%s`', vals)
# cost_obj.create(cr, uid, vals, context=None)
#self._logger.debug('cost created')
return res
purchase_order()
|