Odoo13 新变化:存货核算


去掉了 产品历史价格product.price.history ,增加了 stock valuation layer 对象,重写了 存货计价的逻辑



[REF] stock_account: standard cost method with valuation layers


Valuation of standard move: We introduce _create_in_svl and

_create_out_svl and call these method in _action_done only on the

standard move, the other valuation methods still use the old code. We

hijack _account_entry_move to create journal entries anyway for the

valuation layer with a minimal diff. Helpers for the valuation layers

creation are added on the product because we will need to create similar

layer without stock move later on when we'll implement changes of cost

method. We also make a first implementation of the edition in the past

to make some tests pass, it will be completed with other cost method



Change of standard price: Before this commit, the wizard to change the

standard price was only displayed in perpetual, because it was used to

create accountign entries. We also use this wizard in manual now,

because we want to create a stock valuation layer. Fortunetaly, the

accounting fields of this wizard are not required if the product has a

manual valuation. And the new method is private.


We adapt the existing tests as well as we can and include the start of a

new battery of tests: test_stockvaluationlayer.py




[REF] stock_account: average cost method with valuation layers


One of the goal of this refactoring is also to run the vacuum when the

cost method is AVCO. This will be used allow already out landed cost

on AVCO and also to have a consistent value when going into negative.


We update `_prepare_in_svl_vals` to set the remaining_qty field that

will be used when fifo is implemented. On the stock move, we create the

layer in with the real price unit of the move and adapt

product_price_update_before_done. This method will now compute the AVCO

according to the quantity valued in stock (quantity_svl) instead of all

the quantities in stock (qty_available), which was wrong (qty_available

could contain consigned quants).


We also make sure to recompute the avco when editing a done move.


We don't implement yet the negative handling as we'll need fifo for





[REF] stock_account: fifo "normal" cost method with valuation layers


We don't implement fifo negative handling in this commit.

We make sure we run fifo in average mode to get a proper remaining_qty


`_run_fifo` is now a method on product (since it could be user outside

of a stock move context). It is really similar to its ancestor on the

stock move, with a few float_is_zero and compare more and a missing



We move the valuation sanity check into a proper helper,



We complete the _create_correction_svl method with accounting entries.




[REF] stock_account: fifo "negative" cost method with valuation layers


Re-implement _run_fifo_vacuum on the product this time with

float_is_zero and without remaining_value.


Trigger the vacuum at each receipt. Before it was done in the scheduler

each night, meaning the valuation on the move was not really

understandable nor consistent (the estimated value could be fixed by

some other move than the next receipt).






[REF] stock_account: dropshipping valuation


We standardize the dropshipping by handling it as it was an receipt

directly followed by an out.


We also follow the same logic whatever the cost method, ie we don't run

fifo or update the AVCO.




[REF] mrp_account: adapt to valuation layers


A finished move is considered as an incoming move and will pass by

_create_in_svl. We thus compute the price_unit that will be set on the

valuation layer.


We also set a stat button on the MO displaying the valuation

layers, similar to the one on the picking.




[REF] stock_dropshipping: adapt to stock valuation layers


The tests of this module showed multiple issues with the anglos axon

accounting entries creation so we fix them by getting the value on the

valuation layer and not directly on the stock moves.


About the changes in


> To change a standard price, we need to open the wizard


> To change a standard price, we need to open the wizard + read the

valuation on the layers.


> Now that we create a layer in and out for a dropshipped move, the

accounting entries are similar to an in followed by an out, thus we need

to increment the number of generated AML. Not that the balance of the

accounts stay the same.


There's a slight change in fifo_perpetual_anglosaxon_ordered though.

Let's say for a start that this config doesn't make sense. Ordered means

we don't read the value on the stock move to create the accounting

entries but only the standard price. We had to change 8 by 10 in this

test because, before this task, the standard price was updated when

running fifo, even in dropshipped. As the PO line was valued at 8, the

anglos axon in stock out was at 8 because the standard price was changed

to 8 after the delivery.




[REF] stock_landed_costs: adapt to valuation layers


We need to increment the value of another layer. We don't want to write

on this layer in place as we used to do for multiple reasons:

- valuation at date: before the application of an lc, the value should

not change

- if the layer was consumed, we would need to adapt the consumers and

if one day we would have needed to understand what happened in the

fifo stack it would be hell


So we allow to create a valuation layer linked to another valuation

layer through a simple M2O O2M. At the time it is only used on the

landed costs to add value and no quantity so the code expects it would

be the case and it is not enforced. We adapt `_run_fifo` to use the full



We also add a stat button on the landed cost to access the valuation

layers, like what is done for the picking and manufacturing orders.