Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/100 improve performance qoh calculation #166

Closed
wants to merge 144 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
20f6d50
#100 introduced SQL script and quartz job that will be used to popula…
jmiranda Nov 14, 2016
b4f10f5
#100 improved usability of global search (now that we can retrieve qu…
jmiranda Nov 16, 2016
54d7b8f
Allow admins to configure whether quartz jobs are enabled or disabled…
jmiranda Nov 16, 2016
be427d0
#100 modified inventory item summary SQL script to handle some edge c…
jmiranda Nov 16, 2016
c7c838d
#100 major performance improvements for the dashboard
jmiranda Nov 16, 2016
ac56b12
#100 Added domain class so we can use GORM/HQL to query and manage th…
jmiranda Nov 16, 2016
49c1ed3
minor change to improve the tag summary widget on the dashboard
jmiranda Nov 16, 2016
0725f4a
#100 added data migration changelog for inventory item summary table
jmiranda Nov 16, 2016
b505284
#100 fixed issue when inventory item summary data is not available yet
jmiranda Nov 16, 2016
9a061e6
#100 fixed requisition status list to show only valid statuses
jmiranda Nov 16, 2016
e3e330c
#100 dashboard / megamenu caches should be flushed when user logs out
jmiranda Nov 16, 2016
7549468
added missing messages for some requisition statuses
jmiranda Nov 16, 2016
c3bd7f9
enabled build summary table quartz job by default
jmiranda Nov 16, 2016
24df688
#100 more improvements to the dashboard widgets as well as some of th…
jmiranda Nov 17, 2016
c34f893
#100 minor improvement to the look-and-feel for the purchase order fi…
jmiranda Nov 17, 2016
63f7b41
fixed minor issue with categories that are too long for the sidebar
jmiranda Nov 17, 2016
3f123ad
fixed issue with filtering by created date and other minor cleanups
jmiranda Nov 17, 2016
b402d72
#100 requisition workflow and consumption report are now using QoH su…
jmiranda Nov 17, 2016
13e3590
#100 deprecated method that was being used to calculate QoH of all pr…
jmiranda Nov 17, 2016
75fd70e
#100 more dashboard cleanup
jmiranda Nov 17, 2016
57268d6
#100 removed subtotals from product and expiration summary widgets to…
jmiranda Nov 17, 2016
5b7fd48
#100 Changed cron expression for the build summary tables job but it'…
jmiranda Nov 18, 2016
b3fab2c
#100 dramatically improved performance of the Edit Transaction page
jmiranda Nov 18, 2016
b12600d
#100 fixed bug with cron expression used by build summary tables job …
jmiranda Nov 18, 2016
ab3f3b7
#100 fixed multiple bugs with the summary table build script and quer…
jmiranda Nov 18, 2016
a40e03a
renamed "debug mode" to "translation mode"
jmiranda Nov 18, 2016
54ddbd0
minor improvements to dashboard widgets
jmiranda Nov 18, 2016
36a0e4d
allow users to see stock card for products that are marked as not sup…
jmiranda Nov 18, 2016
78ab79d
fixed minor UI bugs with the product and inventory item summary
jmiranda Nov 18, 2016
5fa1e40
Added a liquibase changeset for the unique index on the inventory ite…
jmiranda Nov 21, 2016
18aa098
enabled read-only cache for inventory item summary
jmiranda Nov 21, 2016
ffc68e4
changed delay for global search autocomplete to allow previous reques…
jmiranda Nov 21, 2016
f476dec
changed how QoH is compiled on consumption report (also improved UI a…
jmiranda Nov 21, 2016
ff20ee0
#100 changed how QoH values are queried within several components (e.…
jmiranda Nov 21, 2016
64f4834
removed unnecessary import
jmiranda Nov 21, 2016
e004fb7
added links to requisiton list page
jmiranda Nov 21, 2016
94557d6
fixed bug which caused all jobs to run at the same time (trigger name…
jmiranda Nov 21, 2016
7ab023e
minor changes to message properties
jmiranda Nov 21, 2016
a755bd0
UI tweaks for the transaction report
jmiranda Nov 21, 2016
9075c64
fixed bug with accessing QoH value associated with a requisition item
jmiranda Nov 21, 2016
fecc9f8
bumped app version to 0.7.14
jmiranda Nov 21, 2016
554ab23
removed unnecessary drop table statement
jmiranda Nov 21, 2016
abd159f
removed RECEIVED from valid requisition states
jmiranda Nov 22, 2016
cea9736
fixed syntax error in MySQL query
jmiranda Nov 22, 2016
6338373
#100 cached all dashboard data responses by default and changed defau…
jmiranda Nov 22, 2016
dde371b
changed uuid to an auto increment primary key and tweaked the summary…
jmiranda Nov 22, 2016
57ca506
removed unnecessary columns from summary tables to improve performanc…
jmiranda Nov 23, 2016
9509a12
fixed bug that occurs when saving inventory item snapshot records, tw…
jmiranda Nov 23, 2016
a6b4aac
added documentation to explain how to configure -javaagent command li…
jmiranda Nov 25, 2016
3a3270e
removed second-level cache to see if it helps with performance
jmiranda Nov 25, 2016
296dc5e
fixed bug related to caching fast movers; separated rebuild summary /…
jmiranda Nov 25, 2016
ac48895
added ability to forcibly execute the rebuild snapshot table job
jmiranda Nov 25, 2016
21b90dc
#100 dramatically improved the rebuilding of snapshot tables by batch…
jmiranda Nov 25, 2016
a4106db
added force param to all actions that trigger the rebuild snapshot ta…
jmiranda Nov 25, 2016
170473c
removed hard-coded references to /openboxes
jmiranda Nov 25, 2016
d4e5ea8
#100 introduced SQL script and quartz job that will be used to popula…
jmiranda Nov 14, 2016
b69c2b8
#100 improved usability of global search (now that we can retrieve qu…
jmiranda Nov 16, 2016
18122b0
Allow admins to configure whether quartz jobs are enabled or disabled…
jmiranda Nov 16, 2016
bfebd30
#100 modified inventory item summary SQL script to handle some edge c…
jmiranda Nov 16, 2016
909b597
#100 major performance improvements for the dashboard
jmiranda Nov 16, 2016
02acfd9
#100 Added domain class so we can use GORM/HQL to query and manage th…
jmiranda Nov 16, 2016
960e175
minor change to improve the tag summary widget on the dashboard
jmiranda Nov 16, 2016
a9dfe4c
#100 added data migration changelog for inventory item summary table
jmiranda Nov 16, 2016
c8160e3
#100 fixed issue when inventory item summary data is not available yet
jmiranda Nov 16, 2016
789264f
#100 fixed requisition status list to show only valid statuses
jmiranda Nov 16, 2016
02c5f54
#100 dashboard / megamenu caches should be flushed when user logs out
jmiranda Nov 16, 2016
838eb9e
added missing messages for some requisition statuses
jmiranda Nov 16, 2016
be2f572
enabled build summary table quartz job by default
jmiranda Nov 16, 2016
5ddffe2
#100 more improvements to the dashboard widgets as well as some of th…
jmiranda Nov 17, 2016
cc25f21
#100 minor improvement to the look-and-feel for the purchase order fi…
jmiranda Nov 17, 2016
96552a0
fixed minor issue with categories that are too long for the sidebar
jmiranda Nov 17, 2016
2b9c1b0
fixed issue with filtering by created date and other minor cleanups
jmiranda Nov 17, 2016
e6491ae
#100 requisition workflow and consumption report are now using QoH su…
jmiranda Nov 17, 2016
16279d5
#100 deprecated method that was being used to calculate QoH of all pr…
jmiranda Nov 17, 2016
8222420
#100 more dashboard cleanup
jmiranda Nov 17, 2016
fc28e06
#100 removed subtotals from product and expiration summary widgets to…
jmiranda Nov 17, 2016
b14feae
#100 Changed cron expression for the build summary tables job but it'…
jmiranda Nov 18, 2016
2160a64
#100 dramatically improved performance of the Edit Transaction page
jmiranda Nov 18, 2016
a3824df
#100 fixed bug with cron expression used by build summary tables job …
jmiranda Nov 18, 2016
4022402
#100 fixed multiple bugs with the summary table build script and quer…
jmiranda Nov 18, 2016
2ca9594
renamed "debug mode" to "translation mode"
jmiranda Nov 18, 2016
4419942
minor improvements to dashboard widgets
jmiranda Nov 18, 2016
3280ca2
allow users to see stock card for products that are marked as not sup…
jmiranda Nov 18, 2016
ada0590
fixed minor UI bugs with the product and inventory item summary
jmiranda Nov 18, 2016
0ec4d87
Added a liquibase changeset for the unique index on the inventory ite…
jmiranda Nov 21, 2016
5e3bd3d
enabled read-only cache for inventory item summary
jmiranda Nov 21, 2016
e2edbb9
changed delay for global search autocomplete to allow previous reques…
jmiranda Nov 21, 2016
22950ee
changed how QoH is compiled on consumption report (also improved UI a…
jmiranda Nov 21, 2016
6a4a000
#100 changed how QoH values are queried within several components (e.…
jmiranda Nov 21, 2016
2959727
removed unnecessary import
jmiranda Nov 21, 2016
a70cf31
added links to requisiton list page
jmiranda Nov 21, 2016
8a66a66
fixed bug which caused all jobs to run at the same time (trigger name…
jmiranda Nov 21, 2016
23b689f
minor changes to message properties
jmiranda Nov 21, 2016
5415c0b
UI tweaks for the transaction report
jmiranda Nov 21, 2016
7699139
fixed bug with accessing QoH value associated with a requisition item
jmiranda Nov 21, 2016
c27c203
bumped app version to 0.7.14
jmiranda Nov 21, 2016
20ea397
removed unnecessary drop table statement
jmiranda Nov 21, 2016
2986d94
removed RECEIVED from valid requisition states
jmiranda Nov 22, 2016
5c25782
fixed syntax error in MySQL query
jmiranda Nov 22, 2016
c043909
#100 cached all dashboard data responses by default and changed defau…
jmiranda Nov 22, 2016
99d564d
changed uuid to an auto increment primary key and tweaked the summary…
jmiranda Nov 22, 2016
1fd5ec9
removed unnecessary columns from summary tables to improve performanc…
jmiranda Nov 23, 2016
81f8318
fixed bug that occurs when saving inventory item snapshot records, tw…
jmiranda Nov 23, 2016
df57e09
added documentation to explain how to configure -javaagent command li…
jmiranda Nov 25, 2016
3bd7afe
removed second-level cache to see if it helps with performance
jmiranda Nov 25, 2016
8c46f5b
fixed bug related to caching fast movers; separated rebuild summary /…
jmiranda Nov 25, 2016
98d4192
added ability to forcibly execute the rebuild snapshot table job
jmiranda Nov 25, 2016
af64b3f
#100 dramatically improved the rebuilding of snapshot tables by batch…
jmiranda Nov 25, 2016
57d5329
added force param to all actions that trigger the rebuild snapshot ta…
jmiranda Nov 25, 2016
9a877da
removed hard-coded references to /openboxes
jmiranda Nov 25, 2016
0e6c32e
Merge branch 'hotfix/100-improve-performance-qoh-calculation' of http…
jmiranda Nov 25, 2016
547db83
improve UI consistency by stretching all text fields to max width; dr…
jmiranda Feb 11, 2017
a0ce500
fixed some issues with requisition enums and expected location types
jmiranda Feb 11, 2017
b84b789
added comment to transfer stock dialog
jmiranda Feb 11, 2017
49266be
removed some unnecessary inventory statuses (related to obsolete stock)
jmiranda Feb 11, 2017
0e65348
UI improvements for the dashboard page
jmiranda Feb 11, 2017
f954eee
minor bug fixes and improvements for the dashboard page
jmiranda Feb 11, 2017
fdb1f18
fixed several bugs with Purchase Order feature (e.g. ability to impor…
jmiranda Feb 11, 2017
9db8efd
UI improvements for the new inventory reports accessible from dashboard
jmiranda Feb 11, 2017
1c9e35c
minor UI improvements
jmiranda Feb 11, 2017
6397a17
fixed bug when receiving against PO where datetime was set to midnigh…
jmiranda Feb 11, 2017
6183306
removed calendar image trigger due to display issues
jmiranda Feb 11, 2017
30ffaaf
updated message properties
jmiranda Feb 11, 2017
f042a7f
minor config changes
jmiranda Feb 11, 2017
28ee2f5
added unique constraint on inventory level (product and inventory mus…
jmiranda Feb 11, 2017
ac7778f
disabled saving state on dashboard since the feature is in-progress
jmiranda Feb 11, 2017
08dbfa9
added last updated timestamp to new inventory summary dashboard widgets
jmiranda Feb 11, 2017
0874e58
fixed order summary on dashboard
jmiranda Feb 11, 2017
4cd7f0f
minor cosmetic change for order summary on dashboard
jmiranda Feb 11, 2017
fea66d4
removed temporary Generate PO feature - needs a separate feature branch
jmiranda Feb 17, 2017
b2fdb25
added item count to order summary widget
jmiranda Feb 17, 2017
127d0de
minor changes to messages.properties
jmiranda Feb 17, 2017
e20e326
Merge branch 'master' into hotfix/100-improve-performance-qoh-calcula…
jmiranda Jul 18, 2017
a24cf98
Merge branch 'master' into hotfix/100-improve-performance-qoh-calcula…
jmiranda Aug 17, 2017
abdd752
Merge branch 'master' into hotfix/100-improve-performance-qoh-calcula…
jmiranda Aug 24, 2017
513368d
Fixed some issues from the merge
jmiranda Aug 24, 2017
ab38137
Significantly improved performance of the meganmenu
jmiranda Aug 30, 2017
429a1ad
fixed some minor issues with the dashboard related to the latest merge
jmiranda Aug 30, 2017
8dfd8ba
Minor code cleanup
jmiranda Aug 30, 2017
852f362
Added gpars library and dependencies
jmiranda Aug 30, 2017
03cd681
WIP: Implemented quantity on hand calculation using gpars (reduced ti…
jmiranda Aug 30, 2017
ddef80c
Minor UI improvements for the report details page associated with the…
jmiranda Aug 30, 2017
35da606
Merge branch 'master' into hotfix/100-improve-performance-qoh-calcula…
jmiranda Dec 8, 2017
ee00cbc
Merge branch 'master' into hotfix/100-improve-performance-qoh-calcula…
jmiranda Mar 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.settings
*.iml
*.iws
*.sql
*Db.properties
*Db.script
/plugins
Expand Down
14 changes: 9 additions & 5 deletions grails-app/conf/BuildConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ grails.project.test.reports.dir = "target/test-reports"
grails.plugin.location.spock='spock/'
grails.plugin.location.liquibase='liquibase/'

// Development configuration property used to enable xrebel features
// Development configuration property used to enable xrebel and newrelic agents
// Unfortunately, these do not work so I had to explicitly export a variable before running grails run-app
// export GRAILS_OPTS="$GRAILS_OPTS -javaagent:/home/jmiranda/Desktop/newrelic/newrelic.jar"
//grails.tomcat.jvmArgs = ["-javaagent:/home/jmiranda/Desktop/xrebel/xrebel.jar"]
//grails.tomcat.jvmArgs = ["-javaagent:/home/jmiranda/Desktop/newrelic/newrelic.jar"]

grails.project.dependency.resolution = {
// inherit Grails' default dependencies
Expand Down Expand Up @@ -58,9 +61,6 @@ grails.project.dependency.resolution = {
compile "org.jadira.usertype:usertype.jodatime:1.9"

compile "com.unboundid:unboundid-ldapsdk:2.3.6"
//runtime ":resources:1.2.RC2"
//runtime ":cached-resources:1.0"

runtime 'org.springframework:spring-test:3.0.5.RELEASE'

test ("org.codehaus.geb:geb-spock:0.6.3") {
Expand All @@ -75,7 +75,9 @@ grails.project.dependency.resolution = {
test 'dumbster:dumbster:1.6'
test "org.spockframework:spock-grails-support:0.6-groovy-1.7"


compile "org.codehaus.gpars:gpars:0.12"
compile "org.codehaus.jsr166-mirror:jsr166y:1.7.0"
compile "org.codehaus.jsr166-mirror:extra166y:1.7.0"
}
plugins {
compile ":rendering:0.4.4"
Expand All @@ -101,6 +103,8 @@ grails.project.dependency.resolution = {
runtime(':external-config-reload:1.4.0') { exclude 'spock-grails-support' }
runtime(':quartz2:2.1.6.2')
runtime(":resources:1.1.6")
//runtime ":resources:1.2.RC2"
//runtime ":cached-resources:1.0"
runtime(":zipped-resources:1.0") { excludes 'resources' }
runtime(":cached-resources:1.0") { excludes 'resources' }
runtime(":cache-headers:1.1.5")
Expand Down
10 changes: 9 additions & 1 deletion grails-app/conf/Config.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mail.error.prefix = grails.mail.prefix
// set per-environment serverURL stem for creating absolute links
environments {
development {
grails.resources.debug = true
grails.serverURL = "http:https://localhost:8080/${appName}";
uiperformance.enabled = false
grails.mail.enabled = false
Expand Down Expand Up @@ -516,10 +517,17 @@ openboxes.mail.errors.recipients = ["[email protected]"]
// Barcode scanner (disabled by default)
openboxes.scannerDetection.enabled = false

// Background jobs
// Job that calculates quantity on hand every day. Occurs at midnight every day.
// NOTE: This job should only run once a day for now (that is fixed in the development branch).
openboxes.jobs.calculateQuantityJob.enabled = true
openboxes.jobs.calculateQuantityJob.cronExpression = "0 0 0 * * ?" // daily at midnight
openboxes.jobs.dataCleaningJob.cronExpression = "0 * * * * ?" // every minute


// Job that builds summary table (i.e. QoH, consumption, shipping). Occurs every ten minutes
openboxes.jobs.buildSummaryTablesJob.enabled = true
openboxes.jobs.buildSummaryTablesJob.cronExpression = "0 0/5 * * * ?" // every five minutes

// LDAP configuration
openboxes.ldap.enabled = false
openboxes.ldap.context.managerDn = "cn=read-only-admin,dc=example,dc=com"
Expand Down
3 changes: 2 additions & 1 deletion grails-app/conf/DataSource.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ dataSource {
// Hibernate caching properties
hibernate {
generate_statistics=false
cache.use_second_level_cache=true
cache.use_second_level_cache=false
cache.use_query_cache=false
cache.provider_class='org.hibernate.cache.EhCacheProvider'
jdbc.batch_size=50
//default_batch_fetch_size = 16
//jdbc.batch_size = 10
//order_inserts = true
Expand Down
231 changes: 231 additions & 0 deletions grails-app/conf/data/build-inventory-item-summary-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# ---------------------------------------------------------------------------------------------
drop table if exists stock_take;
drop table if exists latest_stock_take;
drop table if exists latest_adjustments;
drop table if exists latest_credits;
drop table if exists latest_debits;
# ---------------------------------------------------------------------------------------------
CREATE TABLE stock_take AS
select
transaction_date as transaction_date,
transaction.date_created as date_created,
location.id as location_id,
inventory_item.id as inventory_item_id,
product.id as product_id,
sum(quantity) as quantity
from transaction
join transaction_entry on transaction.id = transaction_entry.transaction_id
join transaction_type on transaction.transaction_type_id = transaction_type.id
join inventory on inventory.id = transaction.inventory_id
join location on location.inventory_id = inventory.id
join inventory_item on transaction_entry.inventory_item_id = inventory_item.id
join product on inventory_item.product_id = product.id
where transaction_type.transaction_code = 'PRODUCT_INVENTORY'
group by transaction.date_created, location.id, inventory_item.id;
#ALTER TABLE stock_take ADD FOREIGN KEY (location_id) REFERENCES location(id);
#ALTER TABLE stock_take ADD FOREIGN KEY (inventory_item_id) REFERENCES inventory_item(id);
#ALTER TABLE stock_take ADD FOREIGN KEY (product_id) REFERENCES product(id);
ALTER TABLE stock_take ADD INDEX (date_created);
ALTER TABLE stock_take ADD INDEX (transaction_date);
# ---------------------------------------------------------------------------------------------
create table latest_stock_take AS (
SELECT
null as transaction_date,
location.id as location_id,
product.id as product_id,
inventory_item.id as inventory_item_id,
0 as quantity
from transaction
join transaction_entry on transaction.id = transaction_entry.transaction_id
join inventory on inventory.id = transaction.inventory_id
join location on location.inventory_id = inventory.id
join inventory_item on transaction_entry.inventory_item_id = inventory_item.id
join product on inventory_item.product_id = product.id
group by location.id, inventory_item.id
);
ALTER TABLE latest_stock_take MODIFY COLUMN transaction_date DATETIME;
#ALTER TABLE latest_stock_take ADD FOREIGN KEY (location_id) REFERENCES location(id);
#ALTER TABLE latest_stock_take ADD FOREIGN KEY (inventory_item_id) REFERENCES inventory_item(id);
#ALTER TABLE latest_stock_take ADD FOREIGN KEY (product_id) REFERENCES product(id);
ALTER TABLE latest_stock_take ADD INDEX (transaction_date);
#ALTER TABLE latest_stock_take ADD INDEX (product_id);
#ALTER TABLE latest_stock_take ADD UNIQUE INDEX (location_id, product_id, inventory_item_id);
ALTER TABLE latest_stock_take ADD UNIQUE INDEX (location_id, inventory_item_id);
# ---------------------------------------------------------------------------------------------
INSERT INTO latest_stock_take
(transaction_date, location_id, product_id, inventory_item_id, quantity)
select
latest_stock_take.transaction_date,
stock_take.location_id,
stock_take.product_id,
stock_take.inventory_item_id,
# this needs to be sum because some stock count transactions have multiple line items per inventory item
sum(quantity) as quantity
from stock_take
inner join (
SELECT max(transaction_date) as transaction_date, location_id, product_id
FROM stock_take
GROUP BY location_id, product_id
) as latest_stock_take ON (
stock_take.location_id = latest_stock_take.location_id
AND stock_take.product_id = latest_stock_take.product_id
AND stock_take.transaction_date = latest_stock_take.transaction_date)
GROUP BY stock_take.location_id, stock_take.inventory_item_id
ON DUPLICATE KEY UPDATE quantity = values(quantity), transaction_date = values(transaction_date);
# ---------------------------------------------------------------------------------------------
UPDATE latest_stock_take t1
JOIN (
SELECT product_id, location_id, max(transaction_date) as transaction_date
FROM latest_stock_take
GROUP BY product_id, location_id
) as t2 ON t1.product_id = t2.product_id AND t1.location_id = t2.location_id
SET t1.transaction_date = t2.transaction_date;
# ---------------------------------------------------------------------------------------------
create table latest_adjustments AS
SELECT
transaction.transaction_date as transaction_date,
location.id as location_id,
inventory_item.id as inventory_item_id,
product.id as product_id,
transaction_entry.quantity
FROM transaction
JOIN transaction_type ON transaction.transaction_type_id = transaction_type.id
JOIN transaction_entry ON transaction.id = transaction_entry.transaction_id
join inventory_item on transaction_entry.inventory_item_id = inventory_item.id
join inventory on transaction.inventory_id = inventory.id
join location on location.inventory_id = inventory.id
join product on inventory_item.product_id = product.id
join (
SELECT
location.id as location_id,
transaction_entry.inventory_item_id as inventory_item_id,
transaction_type.transaction_code as transaction_code,
max(transaction.transaction_date) as transaction_date
FROM transaction
JOIN transaction_type ON transaction.transaction_type_id = transaction_type.id
JOIN transaction_entry ON transaction.id = transaction_entry.transaction_id
join inventory on transaction.inventory_id = inventory.id
join location on location.inventory_id = inventory.id
left outer join latest_stock_take on (
latest_stock_take.inventory_item_id = transaction_entry.inventory_item_id
AND latest_stock_take.location_id = location.id
)
WHERE transaction_type.transaction_code = "INVENTORY"
AND (
transaction.transaction_date >= latest_stock_take.transaction_date
OR latest_stock_take.transaction_date is null
)
GROUP BY location.id, transaction_entry.inventory_item_id
) as latest_adjustments on (
latest_adjustments.location_id = location.id
AND latest_adjustments.inventory_item_id = inventory_item.id
AND latest_adjustments.transaction_date = transaction.transaction_date
AND latest_adjustments.transaction_code = transaction_type.transaction_code
)
GROUP BY location.id, inventory_item.id;
#ALTER TABLE latest_stock_take_partial ADD FOREIGN KEY (location_id) REFERENCES location(id);
#ALTER TABLE latest_stock_take_partial ADD FOREIGN KEY (inventory_item_id) REFERENCES inventory_item(id);
#ALTER TABLE latest_stock_take_partial ADD FOREIGN KEY (product_id) REFERENCES product(id);
ALTER TABLE latest_adjustments ADD UNIQUE INDEX (location_id, inventory_item_id);
ALTER TABLE latest_adjustments ADD INDEX (transaction_date);
# ---------------------------------------------------------------------------------------------
UPDATE latest_stock_take t1
JOIN (
SELECT inventory_item_id, location_id, quantity, max(transaction_date) as transaction_date
FROM latest_adjustments
GROUP BY inventory_item_id, location_id
ORDER BY transaction_date DESC
) as t2 ON t1.inventory_item_id = t2.inventory_item_id AND t1.location_id = t2.location_id
SET t1.quantity = t2.quantity, t1.transaction_date = t2.transaction_date;
# ---------------------------------------------------------------------------------------------
# step 4a create latest credits
create table latest_credits AS
select
max(transaction.transaction_date) as transaction_date,
transaction_type.transaction_code,
location.id as location_id,
inventory_item.id as inventory_item_id,
product.id as product_id,
sum(transaction_entry.quantity) as quantity
from transaction
join transaction_entry on transaction.id = transaction_entry.transaction_id
join transaction_type on transaction.transaction_type_id = transaction_type.id
join inventory on inventory.id = transaction.inventory_id
join location on location.inventory_id = inventory.id
join inventory_item on transaction_entry.inventory_item_id = inventory_item.id
join product on inventory_item.product_id = product.id
join latest_stock_take on (latest_stock_take.location_id = location.id
AND latest_stock_take.inventory_item_id = inventory_item.id )
where transaction_type.transaction_code = 'CREDIT'
and (transaction.transaction_date >= latest_stock_take.transaction_date
OR latest_stock_take.transaction_date is null)
group by location.id, inventory_item.id;
ALTER TABLE latest_credits ADD INDEX (location_id, inventory_item_id);
ALTER TABLE latest_credits ADD INDEX (transaction_date);
# ---------------------------------------------------------------------------------------------
# step 4b create latest debits
create table latest_debits AS
select
max(transaction.transaction_date) as transaction_date,
transaction_type.transaction_code,
location.id as location_id,
inventory_item.id as inventory_item_id,
product.id as product_id,
sum(transaction_entry.quantity) as quantity
from transaction
join transaction_entry on transaction.id = transaction_entry.transaction_id
join transaction_type on transaction.transaction_type_id = transaction_type.id
join inventory on inventory.id = transaction.inventory_id
join location on location.inventory_id = inventory.id
join inventory_item on transaction_entry.inventory_item_id = inventory_item.id
join product on inventory_item.product_id = product.id
join latest_stock_take on (latest_stock_take.inventory_item_id = inventory_item.id
AND latest_stock_take.location_id = location.id)
where transaction_type.transaction_code = 'DEBIT'
and (transaction.transaction_date >= latest_stock_take.transaction_date
OR latest_stock_take.transaction_date is null)
group by location.id, inventory_item.id;
#ALTER TABLE latest_debits ADD FOREIGN KEY (location_id) REFERENCES location(id);
#ALTER TABLE latest_debits ADD FOREIGN KEY (inventory_item_id) REFERENCES inventory_item(id);
#ALTER TABLE latest_debits ADD FOREIGN KEY (product_id) REFERENCES product(id);
ALTER TABLE latest_debits ADD INDEX (location_id, inventory_item_id);
ALTER TABLE latest_debits ADD INDEX (transaction_date);
# ---------------------------------------------------------------------------------------------
# step 6 populate inventory item summary
LOCK TABLES
inventory_item_summary WRITE,
latest_stock_take READ,
latest_adjustments READ,
latest_credits READ,
latest_debits READ;

TRUNCATE inventory_item_summary;

INSERT INTO inventory_item_summary (version, location_id, inventory_item_id, product_id,
quantity0, adjustments, credits, debits, quantity, date_created, last_updated)
select
0,
latest_stock_take.location_id as location_id,
latest_stock_take.inventory_item_id as inventory_item_id,
latest_stock_take.product_id as product_id,
latest_stock_take.quantity as quantity0,
latest_adjustments.quantity as adjustments,
latest_credits.quantity as credits,
latest_debits.quantity as debits,
ifnull(latest_adjustments.quantity, latest_stock_take.quantity) +
ifnull(latest_credits.quantity,0) - ifnull(latest_debits.quantity,0) as quantity,
now(),
now()
from latest_stock_take
left outer join latest_adjustments on (
latest_adjustments.location_id = latest_stock_take.location_id
and latest_adjustments.inventory_item_id = latest_stock_take.inventory_item_id)
left outer join latest_debits on (
latest_debits.location_id = latest_stock_take.location_id
and latest_debits.inventory_item_id = latest_stock_take.inventory_item_id )
left outer join latest_credits on (
latest_credits.location_id = latest_stock_take.location_id
and latest_credits.inventory_item_id = latest_stock_take.inventory_item_id)
GROUP BY location_id, inventory_item_id;

UNLOCK TABLES;
Loading