- Local time
- Today, 12:14
- Joined
- Sep 12, 2006
- Messages
- 15,955
Here's a semi working version.
I removed the AVCO's you had calculated in the delivery items table and set them to zero. .
So in module 1, I have broken down the calculation process to a series of steps, so that you can check that the calculation and code are working correctly.
Procedure process_order_intakes()
examines each record in the order_intakes table that hasn't already been processed.
It calls a function CalcAVCO using the productid, the current average price, and the quantity and price of this intake.
The function needs to obtain the total current stock, which involves a calculation to find all the intakes and despatches within the relevant date range which I haven't completed. You need to decide how to use the actual date. ie if your intake is 14/3/22, do you count sales up to only 13/3/22. What do you do with sales for 14/3/22? You also need to consider how to manage a starting position and opening stock for the date range. I wouldn't like to try to build this step into the query directly. (edit. I mean it's far easier to do this within code, that to try to build this into a single inline query - if indeed you even can do that)
The function then returns the new AVCO, which you need to save in the products table, and in the order intake table (or a different table) itself.
Note that you need to do this, as a further intake of the same material will need to use this AVCO for the next calculation.
I have commented out some "test" message boxes I added to check the progress. I also have commented out some code that needs to be considered.
It's a matter of taste whether you store the AVCO history at all, and where you store it. You couldn't store it IN the products table, as you need multiple history records for each product. You could store it with the product delivery records. It probably depends how you are most likely to want to use the history.
I hope this helps
[edit
I haven't included code to format, manage and check the date range used in the quantity on hand calculation.
I haven't included code to store the calculated AVCO in the relevant tables.
You could store an opening stock count and AVCO in the products table to reflect a collapsed history, if you will. That is included in the AVCO function.
I removed the AVCO's you had calculated in the delivery items table and set them to zero. .
So in module 1, I have broken down the calculation process to a series of steps, so that you can check that the calculation and code are working correctly.
Procedure process_order_intakes()
examines each record in the order_intakes table that hasn't already been processed.
It calls a function CalcAVCO using the productid, the current average price, and the quantity and price of this intake.
The function needs to obtain the total current stock, which involves a calculation to find all the intakes and despatches within the relevant date range which I haven't completed. You need to decide how to use the actual date. ie if your intake is 14/3/22, do you count sales up to only 13/3/22. What do you do with sales for 14/3/22? You also need to consider how to manage a starting position and opening stock for the date range. I wouldn't like to try to build this step into the query directly. (edit. I mean it's far easier to do this within code, that to try to build this into a single inline query - if indeed you even can do that)
The function then returns the new AVCO, which you need to save in the products table, and in the order intake table (or a different table) itself.
Note that you need to do this, as a further intake of the same material will need to use this AVCO for the next calculation.
I have commented out some "test" message boxes I added to check the progress. I also have commented out some code that needs to be considered.
It's a matter of taste whether you store the AVCO history at all, and where you store it. You couldn't store it IN the products table, as you need multiple history records for each product. You could store it with the product delivery records. It probably depends how you are most likely to want to use the history.
I hope this helps
[edit
I haven't included code to format, manage and check the date range used in the quantity on hand calculation.
I haven't included code to store the calculated AVCO in the relevant tables.
You could store an opening stock count and AVCO in the products table to reflect a collapsed history, if you will. That is included in the AVCO function.
Attachments
Last edited: