| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models, _ |
| 4 |
from odoo.exceptions import UserError |
| 5 |
from odoo.fields import Domain |
| 6 |
from odoo.tools import float_is_zero, float_repr, float_round, float_compare |
| 7 |
from odoo.exceptions import ValidationError |
| 8 |
from collections import defaultdict |
| 9 |
from datetime import datetime |
| 10 |
|
| 11 |
|
| 12 |
class ProductTemplate(models.Model): |
| 13 |
_inherit = 'product.template' |
| 14 |
|
| 15 |
cost_method = fields.Selection( |
| 16 |
string="Cost Method", |
| 17 |
selection=[ |
| 18 |
('standard', "Standard Price"), |
| 19 |
('fifo', "First In First Out (FIFO)"), |
| 20 |
('average', "Average Cost (AVCO)"), |
| 21 |
], |
| 22 |
compute='_compute_cost_method', |
| 23 |
) |
| 24 |
valuation = fields.Selection( |
| 25 |
string="Valuation", |
| 26 |
selection=[ |
| 27 |
('periodic', 'Periodic (at closing)'), |
| 28 |
('real_time', 'Perpetual (at invoicing)'), |
| 29 |
], |
| 30 |
compute='_compute_valuation', search='_search_valuation', |
| 31 |
) |
| 32 |
lot_valuated = fields.Boolean( |
| 33 |
string="Valuation by Lot/Serial", |
| 34 |
compute='_compute_lot_valuated', store=True, readonly=False, |
| 35 |
help="If checked, the valuation will be specific by Lot/Serial number.", |
| 36 |
) |
| 37 |
|
| 38 |
property_price_difference_account_id = fields.Many2one( |
| 39 |
'account.account', 'Price Difference Account', company_dependent=True, ondelete='restrict', |
| 40 |
check_company=True, |
| 41 |
help="""With perpetual valuation, this account will hold the price difference between the standard price and the bill price.""") |
| 42 |
|
| 43 |
def _search_valuation(self, operator, value): |
| 44 |
if operator != '=': |
| 45 |
raise UserError(self.env._("You can only use the '=' operator to search on valuation field.")) |
| 46 |
if value not in ['periodic', 'real_time']: |
| 47 |
raise UserError(self.env._("Only the value 'periodic' and 'real_time' are accepted to search on valuation field.")) |
| 48 |
domain_categ = Domain([('categ_id.property_valuation', operator, value)]) |
| 49 |
domain_company = Domain(['|', ('categ_id.property_valuation', '=', False), ('categ_id', '=', False), ('company_id.inventory_valuation', operator, value)]) |
| 50 |
return domain_company | domain_categ |
| 51 |
|
| 52 |
@api.depends('tracking') |
| 53 |
def _compute_lot_valuated(self): |
| 54 |
for product in self: |
| 55 |
if product.tracking == 'none': |
| 56 |
product.lot_valuated = False |
| 57 |
|
| 58 |
@api.depends_context('company') |
| 59 |
@api.depends('categ_id.property_cost_method') |
| 60 |
def _compute_cost_method(self): |
| 61 |
for product_template in self: |
| 62 |
product_template.cost_method = ( |
| 63 |
product_template.categ_id.with_company( |
| 64 |
product_template.company_id |
| 65 |
).property_cost_method |
| 66 |
or (product_template.company_id or self.env.company).cost_method |
| 67 |
) |
| 68 |
|
| 69 |
@api.depends_context('company') |
| 70 |
@api.depends('categ_id.property_valuation') |
| 71 |
def _compute_valuation(self): |
| 72 |
for product_template in self: |
| 73 |
product_template.valuation = product_template.categ_id.with_company( |
| 74 |
product_template.company_id).property_valuation or self.env.company.inventory_valuation |
| 75 |
|
| 76 |
def write(self, vals): |
| 77 |
product_to_update = set() |
| 78 |
if 'categ_id' in vals: |
| 79 |
category = self.env['product.category'].browse(vals['categ_id']) |
| 80 |
valuation = category.property_valuation if category else self.env.company.inventory_valuation |
| 81 |
for product in self: |
| 82 |
if product.valuation != valuation: |
| 83 |
product_to_update.update(product.product_variant_ids.ids) |
| 84 |
res = super().write(vals) |
| 85 |
if 'lot_valuated' in vals: |
| 86 |
self.env['stock.lot'].search([ |
| 87 |
('product_id', 'in', self.product_variant_ids.ids), |
| 88 |
])._update_standard_price() |
| 89 |
if 'product_to_update': |
| 90 |
self.env['product.product'].browse(product_to_update)._update_standard_price() |
| 91 |
return res |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def _get_product_accounts(self): |
| 97 |
""" Add the stock accounts related to product to the result of super() |
| 98 |
@return: dictionary which contains information regarding stock accounts and super (income+expense accounts) |
| 99 |
""" |
| 100 |
accounts = super()._get_product_accounts() |
| 101 |
|
| 102 |
accounts['stock_valuation'] = ( |
| 103 |
self.categ_id.property_stock_valuation_account_id |
| 104 |
or self.categ_id._fields['property_stock_valuation_account_id'].get_company_dependent_fallback(self.categ_id) |
| 105 |
or self.env.company.account_stock_valuation_id |
| 106 |
) |
| 107 |
accounts['stock_variation'] = accounts['stock_valuation'].account_stock_variation_id |
| 108 |
return accounts |
| 109 |
|
| 110 |
def get_product_accounts(self, fiscal_pos=None): |
| 111 |
""" Add the stock journal related to product to the result of super() |
| 112 |
@return: dictionary which contains all needed information regarding stock accounts and journal and super (income+expense accounts) |
| 113 |
""" |
| 114 |
accounts = super().get_product_accounts(fiscal_pos=fiscal_pos) |
| 115 |
accounts.update({ |
| 116 |
'stock_journal': ( |
| 117 |
self.categ_id.property_stock_journal |
| 118 |
or self.categ_id._fields['property_stock_journal'].get_company_dependent_fallback(self.categ_id) |
| 119 |
or self.env.company.account_stock_journal_id |
| 120 |
) |
| 121 |
}) |
| 122 |
return accounts |
| 123 |
|
| 124 |
|
| 125 |
class ProductProduct(models.Model): |
| 126 |
_inherit = 'product.product' |
| 127 |
|
| 128 |
avg_cost = fields.Monetary( |
| 129 |
string="Average Cost", compute='_compute_value', |
| 130 |
compute_sudo=True, currency_field='company_currency_id') |
| 131 |
total_value = fields.Monetary( |
| 132 |
string="Total Value", compute='_compute_value', |
| 133 |
compute_sudo=True, currency_field='company_currency_id') |
| 134 |
company_currency_id = fields.Many2one( |
| 135 |
'res.currency', 'Valuation Currency', compute='_compute_value', compute_sudo=True, |
| 136 |
help="Technical field to correctly show the currently selected company's currency that corresponds " |
| 137 |
"to the totaled value of the product's valuation layers") |
| 138 |
|
| 139 |
@api.depends_context('to_date', 'company', 'warehouse_id') |
| 140 |
@api.depends('cost_method', 'stock_move_ids.value', 'standard_price') |
| 141 |
def _compute_value(self): |
| 142 |
"""Compute totals of multiple svl related values""" |
| 143 |
company_id = self.env.company |
| 144 |
self.company_currency_id = company_id.currency_id |
| 145 |
|
| 146 |
for product in self: |
| 147 |
at_date = fields.Datetime.to_datetime(product.env.context.get('to_date')) |
| 148 |
if at_date: |
| 149 |
at_date = at_date.replace(hour=23, minute=59, second=59) |
| 150 |
product = product.with_context(at_date=at_date) |
| 151 |
valuated_product = product.sudo(False)._with_valuation_context() |
| 152 |
qty_valued = valuated_product.qty_available |
| 153 |
qty_available = valuated_product.with_context(warehouse_id=False).qty_available if self.env.context.get('warehouse_id') else qty_valued |
| 154 |
if product.lot_valuated: |
| 155 |
product.total_value = product._get_value_from_lots() |
| 156 |
elif product.uom_id.is_zero(qty_valued): |
| 157 |
product.total_value = 0 |
| 158 |
elif product.uom_id.is_zero(qty_available): |
| 159 |
product.total_value = product.standard_price * qty_valued |
| 160 |
elif product.cost_method == 'standard': |
| 161 |
standard_price = product.standard_price |
| 162 |
if at_date: |
| 163 |
standard_price = product._get_standard_price_at_date(at_date) |
| 164 |
product.total_value = standard_price * qty_valued |
| 165 |
elif product.cost_method == 'average': |
| 166 |
product.total_value = product._run_avco(at_date=at_date)[1] * qty_valued / qty_available |
| 167 |
else: |
| 168 |
product.total_value = product.with_context(warehouse_id=False)._run_fifo(qty_available, at_date=at_date) * qty_valued / qty_available |
| 169 |
product.avg_cost = product.total_value / qty_valued if not product.uom_id.is_zero(qty_valued) else 0 |
| 170 |
|
| 171 |
@api.model_create_multi |
| 172 |
def create(self, vals_list): |
| 173 |
products = super().create(vals_list) |
| 174 |
products._change_standard_price({product: 0 for product in products if product.standard_price}) |
| 175 |
return products |
| 176 |
|
| 177 |
def write(self, vals): |
| 178 |
old_price = False |
| 179 |
if 'standard_price' in vals and not self.env.context.get('disable_auto_revaluation'): |
| 180 |
old_price = {product: product.standard_price for product in self} |
| 181 |
if 'lot_valuated' in vals: |
| 182 |
|
| 183 |
self.product_tmpl_id.write({'lot_valuated': vals.pop('lot_valuated')}) |
| 184 |
res = super().write(vals) |
| 185 |
if old_price: |
| 186 |
self._change_standard_price(old_price) |
| 187 |
return res |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
def _change_standard_price(self, old_price): |
| 194 |
product_values = [] |
| 195 |
for product in self: |
| 196 |
if product.cost_method == 'fifo' or product.standard_price == old_price.get(product): |
| 197 |
continue |
| 198 |
product_values.append({ |
| 199 |
'product_id': product.id, |
| 200 |
'value': product.standard_price, |
| 201 |
'company_id': product.company_id.id or self.env.company.id, |
| 202 |
'date': fields.Datetime.now(), |
| 203 |
'description': _('Price update from %(old_price)s to %(new_price)s by %(user)s', |
| 204 |
old_price=old_price.get(product), new_price=product.standard_price, user=self.env.user.name) |
| 205 |
}) |
| 206 |
self.env['product.value'].sudo().create(product_values) |
| 207 |
return |
| 208 |
|
| 209 |
def _get_standard_price_at_date(self, date=None): |
| 210 |
""" Get Last Price History """ |
| 211 |
self.ensure_one() |
| 212 |
if not date or date == fields.Date.today(): |
| 213 |
return self.standard_price |
| 214 |
if self.cost_method != 'standard': |
| 215 |
raise ValidationError(_("You can only get the standard price at a given date for products with 'Standard Price' as cost method.")) |
| 216 |
product_value_domain = Domain([ |
| 217 |
('product_id', '=', self.id), |
| 218 |
('move_id', '=', False), |
| 219 |
('lot_id', '=', False), |
| 220 |
]) |
| 221 |
product_value = self.env['product.value'].search(product_value_domain & Domain([('date', '<=', date)]), limit=1, order="date DESC, id DESC") |
| 222 |
if not product_value: |
| 223 |
|
| 224 |
product_value = self.env['product.value'].search(product_value_domain, limit=1, order="date, id") |
| 225 |
return product_value.value if product_value else self.standard_price |
| 226 |
|
| 227 |
def _get_last_in(self, date=None): |
| 228 |
last_in_domain = Domain([('is_in', '=', True), ('product_id', '=', self.id)]) |
| 229 |
if date: |
| 230 |
last_in_domain &= Domain([('date', '<=', date)]) |
| 231 |
last_in = self.env['stock.move'].search(last_in_domain, order='date desc, id desc', limit=1) |
| 232 |
return last_in |
| 233 |
|
| 234 |
def _get_value_from_lots(self): |
| 235 |
domain = Domain([('product_id', 'in', self.ids)]) |
| 236 |
if not self.env.context.get('warehouse_id'): |
| 237 |
domain &= Domain([('product_qty', '!=', 0)]) |
| 238 |
lots = self.env['stock.lot'].search(domain) |
| 239 |
return sum(lots.mapped('total_value')) |
| 240 |
|
| 241 |
def _with_valuation_context(self): |
| 242 |
self_with_context = self |
| 243 |
valued_locations = self.env['stock.location'].search([('is_valued_internal', '=', True)]) |
| 244 |
self_with_context = self.with_context(location=valued_locations.ids) |
| 245 |
|
| 246 |
if self.cost_method != 'fifo': |
| 247 |
self_with_context = self_with_context.with_context( |
| 248 |
owners=[False, self.env.company.partner_id.id] |
| 249 |
) |
| 250 |
return self_with_context |
| 251 |
|
| 252 |
def _get_remaining_moves(self): |
| 253 |
moves_qty_by_product = {} |
| 254 |
for product in self: |
| 255 |
moves, remaining_qty = product._run_fifo_get_stack() |
| 256 |
moves = self.env['stock.move'].concat(*moves) |
| 257 |
if not moves: |
| 258 |
continue |
| 259 |
qty_by_move = {m: m.quantity for m in moves[1:]} |
| 260 |
qty_by_move[moves[0]] = remaining_qty |
| 261 |
moves_qty_by_product[product] = qty_by_move |
| 262 |
return moves_qty_by_product |
| 263 |
|
| 264 |
def _run_avco(self, at_date=None, lot=None, method="realtime"): |
| 265 |
""" Recompute the average cost of the product base on the last closing |
| 266 |
inventory value and all the incoming moves during the period.""" |
| 267 |
|
| 268 |
self.ensure_one() |
| 269 |
|
| 270 |
quantity = 0 |
| 271 |
|
| 272 |
moves_domain = Domain([ |
| 273 |
('product_id', '=', self.id), |
| 274 |
('company_id', '=', self.env.company.id), |
| 275 |
]) |
| 276 |
if lot: |
| 277 |
moves_domain &= Domain([ |
| 278 |
('move_line_ids.lot_id', 'in', lot.id), |
| 279 |
]) |
| 280 |
if at_date: |
| 281 |
moves_domain &= Domain([ |
| 282 |
('date', '<=', at_date), |
| 283 |
]) |
| 284 |
|
| 285 |
|
| 286 |
move_fields = ['date', 'is_dropship', 'is_in', 'is_out', 'location_dest_id', 'location_id', 'move_line_ids', 'picked', 'value'] |
| 287 |
|
| 288 |
moves_in = self.env['stock.move'].search_fetch( |
| 289 |
moves_domain & Domain(['|', ('is_in', '=', True), ('is_dropship', '=', True)]), |
| 290 |
field_names=move_fields, |
| 291 |
order='date, id' |
| 292 |
) |
| 293 |
|
| 294 |
product_value_domain = Domain([('product_id', '=', self.id), ('move_id', '=', False)]) |
| 295 |
if lot: |
| 296 |
product_value_domain &= Domain(['|', ('lot_id', '=', lot.id), ('lot_id', '=', False)]) |
| 297 |
else: |
| 298 |
product_value_domain &= Domain([('lot_id', '=', False)]) |
| 299 |
if at_date: |
| 300 |
product_value_domain &= Domain([('date', '<=', at_date)]) |
| 301 |
|
| 302 |
product_values = self.env['product.value'].sudo().search(product_value_domain, order="date, id") |
| 303 |
|
| 304 |
|
| 305 |
if product_values and not moves_in: |
| 306 |
quantity = self._with_valuation_context().with_context(to_date=at_date, lot_id=lot.id if lot else None, warehouse_id=False).qty_available |
| 307 |
last_value = product_values[-1] |
| 308 |
return last_value.value, last_value.value * quantity |
| 309 |
if product_values and moves_in and product_values[-1].date > moves_in[-1].date: |
| 310 |
quantity = self._with_valuation_context().with_context(to_date=at_date, lot_id=lot.id if lot else None, warehouse_id=False).qty_available |
| 311 |
avco_value = product_values[-1].value |
| 312 |
return avco_value, avco_value * quantity |
| 313 |
|
| 314 |
avco_value = 0 |
| 315 |
avco_total_value = 0 |
| 316 |
|
| 317 |
if method == "realtime": |
| 318 |
moves_full_domain = moves_domain & Domain([ |
| 319 |
'|', |
| 320 |
'|', ('is_in', '=', True), |
| 321 |
('is_out', '=', True), |
| 322 |
('is_dropship', '=', True) |
| 323 |
]) |
| 324 |
moves = self.env['stock.move'].search_fetch(moves_full_domain, field_names=move_fields, order='date, id') |
| 325 |
else: |
| 326 |
|
| 327 |
moves = moves_in |
| 328 |
|
| 329 |
|
| 330 |
moves.move_line_ids.fetch(['company_id', 'location_id', 'location_dest_id', 'lot_id', 'owner_id', 'picked', 'quantity_product_uom']) |
| 331 |
|
| 332 |
|
| 333 |
for move in moves: |
| 334 |
while product_values and move.date >= product_values[0].date: |
| 335 |
product_value = product_values[0] |
| 336 |
product_values = product_values[1:] |
| 337 |
avco_value = product_value.value |
| 338 |
avco_total_value = avco_value * quantity |
| 339 |
if move.is_in or move.is_dropship: |
| 340 |
in_qty = move._get_valued_qty() |
| 341 |
in_value = move.value |
| 342 |
if at_date or move.is_dropship: |
| 343 |
in_value = move._get_value(at_date=at_date) |
| 344 |
if lot: |
| 345 |
lot_qty = move._get_valued_qty(lot) |
| 346 |
in_value = (in_value * lot_qty / in_qty) if in_qty else 0 |
| 347 |
in_qty = lot_qty |
| 348 |
previous_qty = quantity |
| 349 |
quantity += in_qty |
| 350 |
|
| 351 |
if previous_qty > 0: |
| 352 |
avco_total_value += in_value |
| 353 |
avco_value = avco_total_value / quantity |
| 354 |
|
| 355 |
elif previous_qty <= 0: |
| 356 |
avco_value = in_value / in_qty if in_qty else avco_value |
| 357 |
avco_total_value = avco_value * quantity |
| 358 |
if move.is_out or move.is_dropship: |
| 359 |
out_qty = move._get_valued_qty() |
| 360 |
out_value = out_qty * avco_value |
| 361 |
if lot: |
| 362 |
lot_qty = move._get_valued_qty(lot) |
| 363 |
out_value = (out_value * lot_qty / out_qty) if out_qty else 0 |
| 364 |
out_qty = lot_qty |
| 365 |
avco_total_value -= out_value |
| 366 |
quantity -= out_qty |
| 367 |
|
| 368 |
return avco_value, avco_total_value |
| 369 |
|
| 370 |
def _run_fifo(self, quantity, lot=None, at_date=None, location=None): |
| 371 |
""" Returns the value for the next outgoing product base on the qty give as argument.""" |
| 372 |
self.ensure_one() |
| 373 |
if self.uom_id.compare(quantity, 0) <= 0: |
| 374 |
if at_date: |
| 375 |
last_in = self._get_last_in(at_date) |
| 376 |
return quantity * (last_in._get_price_unit() if last_in else self.standard_price) |
| 377 |
return quantity * self.standard_price |
| 378 |
external_location = location and location.is_valued_external |
| 379 |
|
| 380 |
fifo_cost = 0 |
| 381 |
fifo_stack, qty_on_first_move = self._run_fifo_get_stack(lot=lot, at_date=at_date, location=location) |
| 382 |
last_move = False |
| 383 |
|
| 384 |
while quantity > 0 and fifo_stack: |
| 385 |
move = fifo_stack.pop(0) |
| 386 |
last_move = move |
| 387 |
move_value = move.value |
| 388 |
if at_date: |
| 389 |
move_value = move._get_value(at_date=at_date) |
| 390 |
if qty_on_first_move: |
| 391 |
valued_qty = move._get_valued_qty() |
| 392 |
in_qty = qty_on_first_move |
| 393 |
in_value = move_value * in_qty / valued_qty |
| 394 |
qty_on_first_move = 0 |
| 395 |
else: |
| 396 |
in_qty = move._get_valued_qty() |
| 397 |
in_value = move_value |
| 398 |
if in_qty > quantity: |
| 399 |
in_value = in_value * quantity / in_qty |
| 400 |
in_qty = quantity |
| 401 |
fifo_cost += in_value |
| 402 |
quantity -= in_qty |
| 403 |
|
| 404 |
if quantity > 0: |
| 405 |
if last_move and last_move.quantity: |
| 406 |
fifo_cost += quantity * (last_move.value / last_move.quantity) |
| 407 |
else: |
| 408 |
fifo_cost += quantity * self.standard_price |
| 409 |
return fifo_cost |
| 410 |
|
| 411 |
def _run_fifo_get_stack(self, lot=None, at_date=None, location=None): |
| 412 |
|
| 413 |
external_location = location and location.is_valued_external |
| 414 |
fifo_stack = [] |
| 415 |
fifo_stack_size = 0 |
| 416 |
if location: |
| 417 |
self = self.with_context(location=location.ids) |
| 418 |
if lot: |
| 419 |
fifo_stack_size = lot.product_qty |
| 420 |
else: |
| 421 |
fifo_stack_size = self._with_valuation_context().with_context(to_date=at_date).qty_available |
| 422 |
if self.env.context.get('fifo_qty_already_processed'): |
| 423 |
|
| 424 |
fifo_stack_size -= self.env.context['fifo_qty_already_processed'] |
| 425 |
if self.uom_id.compare(fifo_stack_size, 0) <= 0: |
| 426 |
return fifo_stack, 0 |
| 427 |
|
| 428 |
moves_domain = Domain([ |
| 429 |
('product_id', '=', self.id), |
| 430 |
('company_id', '=', self.env.company.id) |
| 431 |
]) |
| 432 |
if lot: |
| 433 |
moves_domain &= Domain([('move_line_ids.lot_id', 'in', lot.id)]) |
| 434 |
if at_date: |
| 435 |
moves_domain &= Domain([('date', '<=', at_date)]) |
| 436 |
if location: |
| 437 |
moves_domain &= Domain([('location_dest_id', '=', location.id)]) |
| 438 |
if external_location: |
| 439 |
moves_domain &= Domain([('is_out', '=', True)]) |
| 440 |
else: |
| 441 |
moves_domain &= Domain([('is_in', '=', True)]) |
| 442 |
|
| 443 |
|
| 444 |
initial_limit = 100 |
| 445 |
moves_in = self.env['stock.move'].search(moves_domain, order='date desc, id desc', limit=initial_limit) |
| 446 |
|
| 447 |
remaining_qty_on_first_stack_move = 0 |
| 448 |
current_offset = 0 |
| 449 |
|
| 450 |
while self.uom_id.compare(fifo_stack_size, 0) > 0 and moves_in: |
| 451 |
move = moves_in[0] |
| 452 |
moves_in = moves_in[1:] |
| 453 |
in_qty = move._get_valued_qty() |
| 454 |
fifo_stack.append(move) |
| 455 |
remaining_qty_on_first_stack_move = min(in_qty, fifo_stack_size) |
| 456 |
fifo_stack_size -= in_qty |
| 457 |
if self.uom_id.compare(fifo_stack_size, 0) > 0 and not moves_in: |
| 458 |
|
| 459 |
current_offset += 1 |
| 460 |
moves_in = self.env['stock.move'].search(moves_domain, order='date desc, id desc', offset=current_offset * initial_limit, limit=initial_limit) |
| 461 |
fifo_stack.reverse() |
| 462 |
return fifo_stack, remaining_qty_on_first_stack_move |
| 463 |
|
| 464 |
def _update_standard_price(self, extra_value=None, extra_quantity=None): |
| 465 |
|
| 466 |
for product in self: |
| 467 |
if product.cost_method == 'standard': |
| 468 |
continue |
| 469 |
if product.cost_method == 'fifo': |
| 470 |
qty_available = product._with_valuation_context().qty_available |
| 471 |
if product.uom_id.compare(qty_available, 0) > 0: |
| 472 |
product.sudo().with_context(disable_auto_revaluation=True).standard_price = product.total_value / qty_available |
| 473 |
elif last_in := product._get_last_in(): |
| 474 |
product.sudo().with_context(disable_auto_revaluation=True).standard_price = last_in._get_price_unit() |
| 475 |
continue |
| 476 |
new_standard_price = product._run_avco()[0] |
| 477 |
if new_standard_price: |
| 478 |
product.with_context(disable_auto_revaluation=True).sudo().standard_price = new_standard_price |
| 479 |
|
| 480 |
|
| 481 |
class ProductCategory(models.Model): |
| 482 |
_inherit = 'product.category' |
| 483 |
|
| 484 |
anglo_saxon_accounting = fields.Boolean( |
| 485 |
string="Use Anglo-Saxon Accounting", compute="_compute_anglo_saxon_accounting", |
| 486 |
help="If checked, the product will be valued using the Anglo-Saxon accounting method.") |
| 487 |
property_valuation = fields.Selection( |
| 488 |
string="Inventory Valuation", |
| 489 |
selection=[ |
| 490 |
('periodic', 'Periodic (at closing)'), |
| 491 |
('real_time', 'Perpetual (at invoicing)'), |
| 492 |
], |
| 493 |
company_dependent=True, copy=True, tracking=True, |
| 494 |
help="""Periodic: The accounting entries are suggested manually in the inventory valuation report. |
| 495 |
Perpetual: An accounting entry is automatically created to value the inventory when a product is billed or invoiced. |
| 496 |
""") |
| 497 |
property_cost_method = fields.Selection( |
| 498 |
string="Costing Method", |
| 499 |
selection=[ |
| 500 |
('standard', "Standard Price"), |
| 501 |
('fifo', "First In First Out (FIFO)"), |
| 502 |
('average', "Average Cost (AVCO)"), |
| 503 |
], |
| 504 |
company_dependent=True, copy=True, |
| 505 |
default=lambda self: self.env.company.cost_method, |
| 506 |
help="""Standard Price: The products are valued at their standard cost defined on the product. |
| 507 |
Average Cost (AVCO): The products are valued at weighted average cost. |
| 508 |
First In First Out (FIFO): The products are valued supposing those that enter the company first will also leave it first. |
| 509 |
""", |
| 510 |
tracking=True, |
| 511 |
) |
| 512 |
property_stock_journal = fields.Many2one( |
| 513 |
'account.journal', 'Stock Journal', company_dependent=True, |
| 514 |
help="When doing automated inventory valuation, this is the Accounting Journal in which entries will be automatically posted when stock moves are processed.") |
| 515 |
property_stock_valuation_account_id = fields.Many2one( |
| 516 |
'account.account', 'Stock Valuation Account', company_dependent=True, ondelete='restrict', |
| 517 |
check_company=True, |
| 518 |
help="""When automated inventory valuation is enabled on a product, this account will hold the current value of the products.""") |
| 519 |
property_price_difference_account_id = fields.Many2one( |
| 520 |
'account.account', 'Price Difference Account', company_dependent=True, ondelete='restrict', |
| 521 |
check_company=True, |
| 522 |
help="""With perpetual valuation, this account will hold the price difference between the standard price and the bill price.""") |
| 523 |
account_stock_variation_id = fields.Many2one( |
| 524 |
'account.account', string="Stock Variation Account", readonly=False, |
| 525 |
related="property_stock_valuation_account_id.account_stock_variation_id") |
| 526 |
|
| 527 |
@api.depends_context('company') |
| 528 |
def _compute_anglo_saxon_accounting(self): |
| 529 |
self.anglo_saxon_accounting = self.env.company.anglo_saxon_accounting |
| 530 |
|
| 531 |
def write(self, vals): |
| 532 |
products_to_update = self.env['product.product'] |
| 533 |
if 'property_cost_method' in vals: |
| 534 |
updated_categories = self.filtered(lambda c: c.property_cost_method != vals['property_cost_method']) |
| 535 |
if updated_categories: |
| 536 |
products_to_update = self.env['product.product'].search([('categ_id', 'in', updated_categories.ids)]) |
| 537 |
res = super().write(vals) |
| 538 |
if products_to_update: |
| 539 |
products_to_update._update_standard_price() |
| 540 |
return res |