Openbravo Issue Tracking System - Openbravo ERP
View Issue Details
0044938Openbravo ERP04. Warehouse managementpublic2020-08-31 14:492020-09-02 08:45
gorkaion 
dmiguelez 
highmajoralways
closedfixed 
5
 
PR20Q4 
Core
No
0044938: Cost Adjustment process do not generate required transaction cost records in some scenarios
It has been observed in a customer environment that some cost adjustment lines have not their related record in the m_transaction_cost table generated.

This is causing wrong costs calculation as some processes are summing the transaction cost amount to calculate the cost.

On the customer instance we have observed that:

1. The affected cost adjustment lines are always negative stock corrections.
2. They are the last cost adjustment line generated for a "parent cost adjustment line"

If a cost adjustment generates missing transaction costs creating a new manual adjustment on the same source transaction reproduces also the issue. Meaning that there must be a way to reproduce the issue in a consistent way.

Adding some log we have found out that the issue is caused by an early return [1] in the while loop of related transactions in AverageCostAdjustment class. That return is existing the getRelatedTransactionsByAlgorithm method. This causes that:
* Flush and clear at the end of each loop [2] is not executed
* Last check on current average cost [3] is not executed



[1] https://gitlab.com/openbravo/product/openbravo/-/blob/master/src/org/openbravo/costing/AverageCostAdjustment.java#L415 [^]
[2] https://gitlab.com/openbravo/product/openbravo/-/blob/master/src/org/openbravo/costing/AverageCostAdjustment.java#L460 [^]
[3] https://gitlab.com/openbravo/product/openbravo/-/blob/master/src/org/openbravo/costing/AverageCostAdjustment.java#L467 [^]
There are no clear steps to reproduce the issue. It is required to build some steps that generate a negative stock correction line and enters on the if clause that executes the early return.

A probable scenario is:

Negative stock allowed and negative correction preference enabled.
Having a product with following transactions:


Trx 1 +10 units cost 5EUR/unit
Trx 2 -15 units
Trx 3 +10 units cost 7EUR/unit
Trx 4 +10 units
Trx 5 -5 units.

Trx 3 should have a negative stock correction so the average generated by it is 7EUR

On that scenario execute a manual stock correction on Trx 1 so the new price is 6EUR/unit


On customer instance adding the flush & clear just before the return statement seems to solve the issue.

In any case it should be reviewed if that is the best solution as that fix still does not cover the last check on current average.

Alternative fixes would be:
* Consider to execute a "break" statement instead so only the while loop is exited, and execute the flush&clear just before.
* Add a new exit condition to the while clause so the flush&clear at the end of the loop is executed:
  while(trxs.next() || forceExit) {
    ... do things.
    if (new cost match)
      // return --> remove return statement
      forceExit = true;
    }
  }
* Adding a flush&clear on the finally clause [1]. But this would mean an extra flush in normal conditions.


[1] https://gitlab.com/openbravo/product/openbravo/-/blob/master/src/org/openbravo/costing/AverageCostAdjustment.java#L464 [^]
No tags attached.
Issue History
2020-08-31 14:49gorkaionNew Issue
2020-08-31 14:49gorkaionAssigned To => Triage Finance
2020-08-31 14:49gorkaionModules => Core
2020-08-31 14:49gorkaionResolution time => 1600639200
2020-08-31 14:49gorkaionTriggers an Emergency Pack => No
2020-09-01 13:17dmiguelezNote Added: 0122507
2020-09-01 13:20dmiguelezNote Edited: 0122507bug_revision_view_page.php?bugnote_id=0122507#r21586
2020-09-01 15:34dmiguelezAssigned ToTriage Finance => dmiguelez
2020-09-01 16:19hgbotNote Added: 0122527
2020-09-02 08:45hgbotNote Added: 0122537
2020-09-02 08:45hgbotResolutionopen => fixed
2020-09-02 08:45hgbotStatusnew => closed
2020-09-02 08:45hgbotFixed in Version => PR20Q4
2020-09-02 08:45hgbotNote Added: 0122538

Notes
(0122507)
dmiguelez   
2020-09-01 13:17   
(edited on: 2020-09-01 13:20)
Test Plan:

0.- Enable Negative Stock Correction Preference:
  * Log in as F&B Admin
  * Go to Preference Window and create a new record with:
    - Organization: *
    - Property: Enable Negative Stock Corrections
    - Value: Y
    - All visibility fields empty

0.- Enable Negative Stock:
  * Go to Warehouse and Storage Bins Window
    - Select US West Coast Warehouse
    - Select WC-0-0-0 Storage Bin and click on Change Status button
    - Select Undefined-OverIssue and click ok

1.- Create new Product:
  * Go to Product Window and create a new record with:
    - Organization: F&B US
    - SearchKey: Test
    - Name: Test
    - In Price Tab create a new record with:
      - Price List Version: Be Soft Drinker Price List
      - Unit Price: 10
      - List Price 10
    - In Price Tab create a new record with:
      - Price List Version: General Sales
      - Unit Price: 10
      - List Price 10

2.- Create new Purchase Order (+10 Units at 5€) + Goods Receipt:
  * Go to Purhcase Order and create a new record with:
    - Organization: F&B US
    - Business Partner: Be Soft Drinker
    - Warehouse: US West Coast
    - Move to Lines and create a new record with:
      - Product: Test
      - Ordered Quantity: 10
      - Net Unit Price: 5
      - Tax: Sales Exempt
    - Click on Complete and Ok
  * Go to Goods Receipt Window and create a new record with:
    - Organization: F&B US
    - Business Partner: Be Soft Drinker
    - Warehouse: US West Coast
    - Click on Create lines from and select the previous Purchase Order and WC-0-0-0 as the Storage Bin
    - Click on Complete and Ok

3.- Create Goods Shipment (-15 Units):
  * Go to Goods Receipt Window and create a new record with:
    - Organization: F&B US
    - Business Partner: Healthy Foods Supermarkets
    - Warehouse: US West Coast
    - Move to lines and create a new record with:
      - Product: Test
      - Movement Quantity: 15
      - Storage Bin: WC-0-0-0
    - Click on Complete and Ok

4.- Create new Purchase Order (+10 Units at 7€) + Goods Receipt:
  * Go to Purhcase Order and create a new record with:
    - Organization: F&B US
    - Business Partner: Be Soft Drinker
    - Warehouse: US West Coast
    - Move to Lines and create a new record with:
      - Product: Test
      - Ordered Quantity: 10
      - Net Unit Price: 7
      - Tax: Sales Exempt
    - Click on Complete and Ok
  * Go to Goods Receipt Window and create a new record with:
    - Organization: F&B US
    - Business Partner: Be Soft Drinker
    - Warehouse: US West Coast
    - Click on Create lines from and select the previous Purchase Order and WC-0-0-0 as the Storage Bin
    - Click on Complete and Ok

5.- Create Goods Receipt (+10 Units):
  * Go to Goods Receipt Window and create a new record with:
    - Organization: F&B US
    - Business Partner: Be Soft Drinker
    - Warehouse: US West Coast
    - Move to lines and create a new record with:
      - Product: Test
      - Movement Quantity: 10
      - Storage Bin: WC-0-0-0
    - Click on Complete and Ok

6.- Create Goods Shipment (-5 Units):
  * Go to Goods Receipt Window and create a new record with:
    - Organization: F&B US
    - Business Partner: Healthy Foods Supermarkets
    - Warehouse: US West Coast
    - Move to lines and create a new record with:
      - Product: Test
      - Movement Quantity: 5
      - Storage Bin: WC-0-0-0
    - Click on Complete and Ok

7.- Execute Manual Adjustment for a Transaction:
  * First go to Process Request Window and create a new record with:
    - Organization: *
    - Process: Costing Background Process
    - Click on Schedule Process (if there is any other process costing running at the same time, unchedule it first)
  * Go to Product window, select the previously created product and move to Transaction Tab
    - Order Transactions by Trx Process Date
    - Select the first Transaction (10 Units at 5€)
    - Click on Manual Adjustment and select Incremental + Unit Cost and set 6€
  * Schedule the costing background process again
  * Check the Transactions for the Product, it should be:
      Trx Order: 1 Movement Qty: 10 Total Cost: 56
      Trx Order: 2 Movement Qty: -15 Total Cost: 84
      Trx Order: 3 Movement Qty: 10 Total Cost: 63
      Trx Order: 4 Movement Qty: 10 Total Cost: 70
      Trx Order: 5 Movement Qty: -5 Total Cost: 35

(0122527)
hgbot   
2020-09-01 16:19   
Merge Request created: https://gitlab.com/openbravo/product/openbravo/-/merge_requests/136 [^]
(0122537)
hgbot   
2020-09-02 08:45   
Merge request merged: https://gitlab.com/openbravo/product/openbravo/-/merge_requests/136 [^]
(0122538)
hgbot   
2020-09-02 08:45   
Directly closing issue as related merge request is already approved.

Repository: https://gitlab.com/openbravo/product/openbravo [^]
Changeset: 8afed91769275f523ce8bedc16eeea1a5388d68d
Author: david.miguelez <david.miguelez@openbravo.com>
Date: 2020-09-02T06:44:57+00:00
URL: https://gitlab.com/openbravo/product/openbravo/-/commit/8afed91769275f523ce8bedc16eeea1a5388d68d [^]

FIXES ISSUE-44938. Fixes problem when persisting information in Costing
process

Before, when finding a particular condtion, a return statement was executed,
finishing the loop but also finalizing the execution for the rest of the
code of the method, which included some final validations + a flush and a clear.
Due to this fact, the changes were not persisted in the Database.

This change allows to break the execution of the loop, but keep executing the
final validations + flush and clear as it was intended.

Also, for this case, a new boolean variable named forceExit has been used
instead of the more common 'break' sentence to finish the loop.
It has been done because the code inside the loop is too big to understand
everything that is happening, so it is better to explicitly explain that
the loop can be prematurely finished, instead of doing it through a buried
return statement.

---
M src/org/openbravo/costing/AverageCostAdjustment.java
---