@things-factory/operato-pms 3.8.13 → 3.8.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/client/pages/dispatchment/daily-dispatch-detail.js +1 -1
  2. package/client/pages/dispatchment/dispatchment-create-record.js +15 -40
  3. package/client/pages/harvesting/daily-harvesting-detail.js +2 -2
  4. package/client/pages/harvesting/edit-harvesting-record.js +2 -2
  5. package/client/pages/harvesting/harvesting-create-record.js +2 -2
  6. package/client/pages/inventory/inventory-ramp-tonnage.js +23 -31
  7. package/client/pages/report/report-daily-ffb-dispatch-and-production.js +167 -124
  8. package/client/pages/report/report-ffb-tonnage-between-individual-block.js +12 -7
  9. package/dist-server/constants/transaction.js +2 -1
  10. package/dist-server/constants/transaction.js.map +1 -1
  11. package/dist-server/entities/daily-dispatch.js +1 -2
  12. package/dist-server/entities/daily-dispatch.js.map +1 -1
  13. package/dist-server/graphql/resolvers/daily-dispatch/update-daily-dispatch.js +1 -1
  14. package/dist-server/graphql/resolvers/daily-dispatch/update-daily-dispatch.js.map +1 -1
  15. package/dist-server/graphql/resolvers/daily-harvest/generate-daily-harvest.js +2 -2
  16. package/dist-server/graphql/resolvers/daily-harvest/generate-daily-harvest.js.map +1 -1
  17. package/dist-server/graphql/resolvers/daily-loading/update-daily-loading.js +19 -6
  18. package/dist-server/graphql/resolvers/daily-loading/update-daily-loading.js.map +1 -1
  19. package/dist-server/graphql/resolvers/dashboard/dashboard-query.js +165 -43
  20. package/dist-server/graphql/resolvers/dashboard/dashboard-query.js.map +1 -1
  21. package/dist-server/graphql/resolvers/report/daily-ffb-dispatch-production-reports.js +111 -0
  22. package/dist-server/graphql/resolvers/report/daily-ffb-dispatch-production-reports.js.map +1 -0
  23. package/dist-server/graphql/resolvers/report/daily-production-reports.js +6 -6
  24. package/dist-server/graphql/resolvers/report/index.js +2 -1
  25. package/dist-server/graphql/resolvers/report/index.js.map +1 -1
  26. package/dist-server/graphql/resolvers/report/monthly-block-dispatch-reports.js +10 -8
  27. package/dist-server/graphql/resolvers/report/monthly-block-dispatch-reports.js.map +1 -1
  28. package/dist-server/graphql/resolvers/report/monthly-ffb-sale-reports.js +1 -1
  29. package/dist-server/graphql/resolvers/report/monthly-production-reports.js +1 -1
  30. package/dist-server/graphql/types/report/daily-ffb-dispatch-production-report-list.js +14 -0
  31. package/dist-server/graphql/types/report/daily-ffb-dispatch-production-report-list.js.map +1 -0
  32. package/dist-server/graphql/types/report/daily-ffb-dispatch-production-report.js +15 -0
  33. package/dist-server/graphql/types/report/daily-ffb-dispatch-production-report.js.map +1 -0
  34. package/dist-server/graphql/types/report/index.js +13 -0
  35. package/dist-server/graphql/types/report/index.js.map +1 -1
  36. package/dist-server/utils/core-values.js +2 -2
  37. package/dist-server/utils/transaction-util.js +2 -1
  38. package/dist-server/utils/transaction-util.js.map +1 -1
  39. package/package.json +2 -2
  40. package/server/constants/transaction.ts +2 -1
  41. package/server/entities/daily-dispatch.ts +1 -4
  42. package/server/graphql/resolvers/daily-dispatch/update-daily-dispatch.ts +2 -2
  43. package/server/graphql/resolvers/daily-harvest/generate-daily-harvest.ts +2 -2
  44. package/server/graphql/resolvers/daily-loading/update-daily-loading.ts +39 -11
  45. package/server/graphql/resolvers/dashboard/dashboard-query.ts +208 -52
  46. package/server/graphql/resolvers/report/daily-ffb-dispatch-production-reports.ts +138 -0
  47. package/server/graphql/resolvers/report/daily-production-reports.ts +6 -6
  48. package/server/graphql/resolvers/report/index.ts +3 -1
  49. package/server/graphql/resolvers/report/monthly-block-dispatch-reports.ts +11 -9
  50. package/server/graphql/resolvers/report/monthly-ffb-sale-reports.ts +1 -1
  51. package/server/graphql/resolvers/report/monthly-production-reports.ts +1 -1
  52. package/server/graphql/types/report/daily-ffb-dispatch-production-report-list.ts +8 -0
  53. package/server/graphql/types/report/daily-ffb-dispatch-production-report.ts +9 -0
  54. package/server/graphql/types/report/index.ts +13 -0
  55. package/server/utils/core-values.ts +2 -2
  56. package/server/utils/transaction-util.ts +2 -1
  57. package/translations/en.json +7 -6
  58. package/translations/ko.json +7 -6
  59. package/translations/ms.json +10 -9
  60. package/translations/zh.json +7 -6
@@ -1,4 +1,8 @@
1
- // import { EntityManager } from 'typeorm'
1
+ import { EntityManager } from 'typeorm'
2
+
3
+ import { ListParam } from '@things-factory/shell'
4
+
5
+ import { Block } from '../../../entities'
2
6
 
3
7
  // import { buildQuery, ListParam } from '@things-factory/shell'
4
8
 
@@ -9,57 +13,55 @@ export const Query = {
9
13
  * @returns tonnage data with its sorted date
10
14
  */
11
15
  async monthlyTonnageProductions(_: any, { blockId, yearAndMonth }, context: any) {
12
- if (blockId == 'ALL_BLOCKS') {
13
- // should remove block filter
14
- // 'ALL_BLOCKS' was hardcoded from dashboard
15
- }
16
-
17
- const year: string = yearAndMonth.substring(0, 4)
18
- const month: string = yearAndMonth.substring(4)
19
-
20
- let data = []
21
-
22
- for (let i = 1, l = new Date(Number(year), Number(month), 0).getDate(); i <= l; i++) {
23
- let theDate = new Date(`${year}-${month}-${i}`)
24
- theDate.setHours(8, 0, 0, 0)
25
-
26
- data.push({
27
- blockId,
28
- tonnage: Math.floor(Math.random() * 1000) + 200, // generate random value within 200-1000
29
- year,
30
- month,
31
- date: theDate.toLocaleString('default', { day: '2-digit', month: 'short' })
16
+ try {
17
+ const { tx }: { tx: EntityManager } = context.state
18
+
19
+ const year = parseInt(yearAndMonth.substring(0, 4))
20
+ const month = parseInt(yearAndMonth.substring(4))
21
+
22
+ let data: any[] = await getDailyProductionData(blockId, year, month, tx);
23
+ let result = []
24
+
25
+ result = data.map(itm => {
26
+ return {
27
+ ...itm,
28
+ blockId,
29
+ tonnage: itm.todayWeightCollected,
30
+ year: itm.dateDay.getFullYear(),
31
+ month: itm.dateDay.getMonth(),
32
+ date: itm.dateDay.toLocaleString('default', { day: '2-digit', month: 'short' })
33
+ }
32
34
  })
33
- }
34
35
 
35
- return data.sort((a, b) => (a.date > b.date ? 1 : -1))
36
+ return result
37
+ } catch (ex) {
38
+ }
36
39
  },
37
40
 
38
41
  async monthlyYields(_: any, { blockId, yearAndMonth }, context: any) {
39
- if (blockId == 'ALL_BLOCKS') {
40
- // should remove block filter
41
- // 'ALL_BLOCKS' was hardcoded from dashboard
42
- }
43
-
44
- const year: string = yearAndMonth.substring(0, 4)
45
- const month: string = yearAndMonth.substring(4)
46
-
47
- let data = []
48
-
49
- for (let i = 1, l = new Date(Number(year), Number(month), 0).getDate(); i <= l; i++) {
50
- let theDate = new Date(`${year}-${month}-${i}`)
51
- theDate.setHours(8, 0, 0, 0)
52
-
53
- data.push({
54
- blockId,
55
- yield: Math.floor(Math.random() * 1000) + 200, // generate random value within 200-1000
56
- year,
57
- month,
58
- date: theDate.toLocaleString('default', { day: '2-digit', month: 'short' })
42
+ try {
43
+ const { tx }: { tx: EntityManager } = context.state
44
+
45
+ const year = parseInt(yearAndMonth.substring(0, 4))
46
+ const month = parseInt(yearAndMonth.substring(4))
47
+
48
+ let data: any[] = await getDailyProductionData(blockId, year, month, tx);
49
+ let result = []
50
+
51
+ result = data.map(itm => {
52
+ return {
53
+ ...itm,
54
+ blockId,
55
+ yield: (itm.todayHectareCover != 0 ? itm.todayWeightCollected / itm.todayHectareCover : 0).toFixed(4),
56
+ year: itm.dateDay.getFullYear(),
57
+ month: itm.dateDay.getMonth(),
58
+ date: itm.dateDay.toLocaleString('default', { day: '2-digit', month: 'short' })
59
+ }
59
60
  })
60
- }
61
61
 
62
- return data.sort((a, b) => (a.date > b.date ? 1 : -1))
62
+ return result
63
+ } catch (ex) {
64
+ }
63
65
  },
64
66
 
65
67
  async weeklyBunchesCount(_: any, { targetDate }, context: any) {
@@ -70,8 +72,10 @@ export const Query = {
70
72
  for (let i = 0; i < 7; i++) {
71
73
  bunchesCount.push({
72
74
  date: new Date(targetDate).toLocaleString('default', { day: '2-digit', month: 'short' }),
73
- harvestedCount: Math.floor(Math.random() * 1000) + 500,
74
- collectedCount: Math.floor(Math.random() * 500) + 50
75
+ harvestedCount: 0,
76
+ collectedCount: 0
77
+ // harvestedCount: Math.floor(Math.random() * 1000) + 500,
78
+ // collectedCount: Math.floor(Math.random() * 500) + 50
75
79
  })
76
80
  targetDate.setDate(targetDate.getDate() - 1)
77
81
  }
@@ -81,16 +85,168 @@ export const Query = {
81
85
 
82
86
  async tonnageAndBunchesInventories(_: any, { something }, context: any) {
83
87
  return {
84
- bunches: Math.floor(Math.random() * 10000) + 500,
85
- tonnage: Math.floor(Math.random() * 10000) + 500
88
+ bunches: 0,
89
+ tonnage: 0
90
+ // bunches: Math.floor(Math.random() * 10000) + 500,
91
+ // tonnage: Math.floor(Math.random() * 10000) + 500
86
92
  }
87
93
  },
88
94
 
89
95
  async productionsOverview(_: any, { something }, context: any) {
90
96
  return {
91
- averageBunchWeight: Math.floor(Math.random() * 150) + 70,
92
- bunchesCount: Math.floor(Math.random() * 10000) + 500,
93
- tonnageWeighed: Math.floor(Math.random() * 10000) + 500
97
+ averageBunchWeight: 0,
98
+ bunchesCount: 0,
99
+ tonnageWeighed: 0,
100
+ // averageBunchWeight: Math.floor(Math.random() * 150) + 70,
101
+ // bunchesCount: Math.floor(Math.random() * 10000) + 500,
102
+ // tonnageWeighed: Math.floor(Math.random() * 10000) + 500
94
103
  }
95
104
  }
96
105
  }
106
+
107
+
108
+ export async function getDailyProductionData(
109
+ blockId: String,
110
+ year: number,
111
+ month: number,
112
+ tx: EntityManager
113
+ ): Promise<any> {
114
+ await tx.query(`
115
+ CREATE TEMP TABLE temp_blocks on commit drop AS (
116
+ WITH RECURSIVE b AS (
117
+ SELECT b.id AS block_id, * FROM blocks b
118
+ WHERE b.deleted_at IS null
119
+ UNION ALL
120
+ SELECT b.id AS block_id, cb.* FROM blocks cb
121
+ INNER JOIN b ON b.id = cb.parent_block_id
122
+ WHERE cb.deleted_at IS null
123
+ )
124
+ SELECT block_id, id, name FROM b
125
+ ${blockId != 'all-blocks' && blockId != '' ? `WHERE block_id = '${blockId}' or id = '${blockId}'` : ``}
126
+ GROUP BY block_id, id, name
127
+ ORDER BY block_id, id, name
128
+ );
129
+ `)
130
+
131
+ let fromDate = new Date(year, month - 1, 1)
132
+ let toDate = new Date(year, month, 1)
133
+ toDate.setDate(toDate.getDate() - 1)
134
+
135
+ let fromDateString = [
136
+ fromDate.getFullYear().toString(),
137
+ String(fromDate.getMonth() + 1).padStart(2, '0'),
138
+ '01'
139
+ ].join('-')
140
+
141
+ let toDateString = [
142
+ toDate.getFullYear().toString(),
143
+ String(toDate.getMonth() + 1).padStart(2, '0'),
144
+ String(toDate.getDate()).padStart(2, '0')
145
+ ].join('-')
146
+
147
+ await tx.query(`
148
+ CREATE TEMP TABLE temp_records on commit drop AS (
149
+ select blk.block_id, coalesce(sum(field_bunches),0) AS today_field_bunches,
150
+ round(coalesce(sum(ramp_weight),0)::numeric,4) AS today_ramp_weight,
151
+ sum(case when transaction_type = 'HARVEST' OR transaction_type = 'BUNCHES_ADJUSTMENT' then field_bunches else 0 end) as today_bunch_harvest,
152
+ sum(case when transaction_type = 'LOADING' OR transaction_type = 'LOADING_ADJUSTMENT' then -field_bunches else 0 end) as today_bunch_collected,
153
+ round(sum(case when transaction_type = 'LOADING' OR transaction_type = 'LOADING_ADJUSTMENT' then ramp_weight else 0 end)::numeric,4) as today_ramp_weight_collected,
154
+ round(sum(case when transaction_type = 'DISPATCHMENT' OR transaction_type = 'DISPATCHMENT_ADJUSTMENT' then -ramp_weight else 0 end)::numeric,4) as today_weight_dispatch,
155
+ transaction_date
156
+ from record_transactions t2
157
+ inner join temp_blocks blk on blk.id = t2.block_id
158
+ where transaction_date <= $1
159
+ group by blk.block_id, transaction_date
160
+ )
161
+ `,
162
+ [toDateString]
163
+ )
164
+
165
+ await tx.query(
166
+ `
167
+ create temp table temp_daily_production_data on commit drop as (
168
+ select *,
169
+ prev_bunch_bal_in_field + today_bunch_harvest as today_bunch_in_field,
170
+ prev_bunch_bal_in_field + today_bunch_harvest - today_bunch_collected as today_bunch_bal_in_field,
171
+ prev_weight_bal_in_ramp + today_ramp_weight_collected - today_weight_dispatch as today_weight_bal_in_ramp FROM (
172
+ select md.dateDay as date_day,
173
+ coalesce(
174
+ (
175
+ select sum(today_field_bunches) from temp_records t2
176
+ where transaction_date::timestamp < md.dateDay
177
+ )
178
+ , 0) as prev_bunch_bal_in_field,
179
+ coalesce(
180
+ (
181
+ select sum(today_ramp_weight) from temp_records t2
182
+ where transaction_date::timestamp < md.dateDay
183
+ )
184
+ , 0) as prev_weight_bal_in_ramp,
185
+ coalesce(today_bunch_harvest, 0) as today_bunch_harvest,
186
+ coalesce(sum(today_bunch_harvest) over (order by dateDay asc rows between unbounded preceding and current row), 0) as to_date_bunch_harvest,
187
+ coalesce(today_bunch_collected, 0) as today_bunch_collected,
188
+ coalesce(sum(today_bunch_collected) over (order by dateDay asc rows between unbounded preceding and current row), 0) as to_date_bunch_collected,
189
+ coalesce(today_ramp_weight_collected, 0) as today_ramp_weight_collected,
190
+ coalesce(sum(today_ramp_weight_collected) over (order by dateDay asc rows between unbounded preceding and current row), 0) as to_date_ramp_weight_collected,
191
+ coalesce(today_weight_dispatch, 0) as today_weight_dispatch,
192
+ coalesce(sum(today_weight_dispatch) over (order by dateDay asc rows between unbounded preceding and current row), 0) as to_date_weight_dispatch,
193
+ coalesce(tot.today_out_turn, 0) as today_out_turn,
194
+ coalesce(sum(tot.today_out_turn) over (order by dateDay asc rows between unbounded preceding and current row), 0) as to_date_out_turn,
195
+ ${blockId != 'all-blocks' && blockId != '' ? `(select name from blocks b2 WHERE id = '${blockId}')` : `''`} as block,
196
+ coalesce(thr.today_harvesting_round, 0) as today_harvesting_round,
197
+ coalesce(thr.today_hectare_cover, 0) as today_hectare_cover
198
+ from
199
+ (select generate_series((date '${fromDateString}')::timestamp, (date '${toDateString}')::timestamp, interval '1 day')::date as dateDay) as md
200
+ left join (
201
+ select * from temp_records trx
202
+ where transaction_date between $1 and $2
203
+ order by transaction_date
204
+ ) as dt on dt.transaction_date::date = md.dateDay
205
+ LEFT JOIN (
206
+ select sum(harvesting_round) AS today_harvesting_round, sum(coverage) AS today_hectare_cover, harvest_date
207
+ from daily_harvests dh
208
+ inner join temp_blocks blk on blk.id = dh.block_id
209
+ where harvest_date between $1 and $2
210
+ group by blk.block_id, harvest_date
211
+ ) as thr ON thr.harvest_date::date = md.dateDay
212
+ LEFT JOIN (
213
+ select count(*) AS today_out_turn, dh.harvest_date
214
+ from daily_harvest_details dhd
215
+ inner join daily_harvests dh on dh.id = dhd.daily_harvest_id
216
+ inner join temp_blocks blk on blk.id = dh.block_id
217
+ where dh.harvest_date between $1 and $2
218
+ GROUP BY blk.block_id, dh.harvest_date
219
+ ) as tot ON tot.harvest_date::date = md.dateDay
220
+ ) dt
221
+ )`,
222
+ [fromDateString, toDateString]
223
+ )
224
+
225
+ const result: any = await tx.query(`
226
+ select * from temp_daily_production_data dt
227
+ `)
228
+
229
+ return result.map(itm => {
230
+ return {
231
+ block: itm.block,
232
+ dateDay: itm.date_day,
233
+ diffInBunches: parseFloat(itm.diff_in_bunches).toFixed(1),
234
+ prevBunchBalInField: parseFloat(itm.prev_bunch_bal_in_field).toFixed(1),
235
+ prevWeightBalInRamp: parseFloat(itm.prev_weight_bal_in_ramp).toFixed(4),
236
+ toDateBunchHarvest: parseFloat(itm.to_date_bunch_harvest).toFixed(1),
237
+ toDateOutTurn: itm.to_date_out_turn,
238
+ toDateWeightDispatch: parseFloat(itm.to_date_weight_dispatch).toFixed(4),
239
+ todayBunchBalInField: parseFloat(itm.today_bunch_bal_in_field).toFixed(1),
240
+ todayBunchCollected: parseFloat(itm.today_bunch_collected).toFixed(1),
241
+ todayBunchHarvest: parseFloat(itm.today_bunch_harvest).toFixed(1),
242
+ todayBunchInField: parseFloat(itm.today_bunch_in_field).toFixed(1),
243
+ todayHarvestingRound: itm.today_harvesting_round,
244
+ todayHectareCover: parseFloat(itm.today_hectare_cover).toFixed(4),
245
+ todayOutTurn: itm.today_out_turn,
246
+ todayWeightCollected: parseFloat(itm.today_ramp_weight_collected).toFixed(4),
247
+ todayWeightBalInRamp: parseFloat(itm.today_weight_bal_in_ramp).toFixed(4),
248
+ todayWeightDispatch: parseFloat(itm.today_weight_dispatch).toFixed(4),
249
+ }
250
+ })
251
+
252
+ }
@@ -0,0 +1,138 @@
1
+ import { EntityManager, getRepository, Not, IsNull } from 'typeorm'
2
+
3
+ import { ListParam, Domain } from '@things-factory/shell'
4
+ import { Block, Organization, Ramp } from '../../../entities'
5
+
6
+ export const dailyFfbDispatchProductionReports = {
7
+ async dailyFfbDispatchProductionReports(_: any, params: ListParam, context: any) {
8
+ try {
9
+ const { domain, tx }: { domain: Domain, tx: EntityManager } = context.state
10
+
11
+ let year = parseInt(params.filters.filter(x => x.name == 'month')[0].value.split('-')[0])
12
+ let month = parseInt(params.filters.filter(x => x.name == 'month')[0].value.split('-')[1])
13
+
14
+ let fromDate = new Date(year, month - 1, 1)
15
+ let toDate = new Date(year, month, 1)
16
+ toDate.setDate(toDate.getDate() - 1)
17
+
18
+ let fromDateString = [
19
+ fromDate.getFullYear().toString(),
20
+ String(fromDate.getMonth() + 1).padStart(2, '0'),
21
+ '01'
22
+ ].join('-')
23
+
24
+ let toDateString = [
25
+ toDate.getFullYear().toString(),
26
+ String(toDate.getMonth() + 1).padStart(2, '0'),
27
+ String(toDate.getDate()).padStart(2, '0')
28
+ ].join('-')
29
+
30
+ let ramps = await getRepository(Ramp).find({
31
+ where: { domain: domain.id, deletedAt: IsNull() },
32
+ relations: ['block']
33
+ })
34
+
35
+ let organizations = await getRepository(Organization).find({
36
+ where: { domain: domain.id, deletedAt: IsNull() }
37
+ })
38
+
39
+ let blocks = await tx.getRepository(Block).query(`
40
+ SELECT b.id, b.name FROM ramps r
41
+ INNER JOIN blocks b ON b.id = r.block_id
42
+ WHERE b.deleted_at IS NULL
43
+ AND r.domain_id = $1
44
+ GROUP BY b.id, b.name
45
+ `, [domain.id])
46
+
47
+
48
+ // -- WHERE dd.dispatch_at::timestamp between $1::timestamp and $2::timestamp
49
+ // [fromDateString, toDateString]
50
+ await tx.query(`create extension if not exists tablefunc`)
51
+
52
+ await tx.query(
53
+ `
54
+ CREATE TEMP TABLE raw_block_dispatch ON COMMIT DROP AS (
55
+ SELECT "name", "dispatch_at", sum(weight) AS weight FROM(
56
+ SELECT * FROM (
57
+ SELECT dd.id,COALESCE(b.name, 'unknown') AS name, 1 AS "rank", dd.dispatch_at::date AS dispatch_at,
58
+ COALESCE(ddd.adjusted_weight, ddd.collected_ramp_weight, 0) AS weight
59
+ FROM daily_dispatches dd
60
+ INNER JOIN daily_dispatch_details ddd ON ddd.daily_dispatch_id = dd.id
61
+ INNER JOIN ramps r ON r.id = ddd.ramp_id
62
+ INNER JOIN blocks b ON b.id = r.block_id
63
+ WHERE dd.dispatch_at::date between $1 and $2
64
+ UNION
65
+ SELECT dd.id, COALESCE(c.name, 'unknown') AS name, CASE WHEN c.name IS NULL THEN 3 ELSE 2 END AS "rank", dd.dispatch_at::date AS dispatch_at,
66
+ COALESCE((SELECT SUM(COALESCE(ddd.adjusted_weight, ddd.collected_ramp_weight, 0)) FROM daily_dispatch_details ddd WHERE ddd.daily_dispatch_id = dd.id),0) AS weight
67
+ FROM daily_dispatches dd
68
+ LEFT JOIN organizations c ON c.id = dd.dispatch_to_id
69
+ WHERE dd.dispatch_at::date between $1 and $2
70
+ ) foo
71
+ ORDER BY "rank", "name", "dispatch_at"
72
+ ) foo
73
+ GROUP BY "name", "dispatch_at"
74
+ ORDER BY name, "dispatch_at"
75
+ )
76
+ `,
77
+ [fromDateString, toDateString]
78
+ )
79
+
80
+ await tx.query(
81
+ `
82
+ CREATE TEMP TABLE pivot_block_dispatch ON COMMIT DROP AS (
83
+ SELECT * FROM crosstab(
84
+ $$
85
+ SELECT rbd."dispatch_at", rbd."name",
86
+ CONCAT('today: ', round(rbd.weight,2)::varchar, ' | todate: ', round(coalesce(sum(rbd.weight) over (PARTITION BY "name" ORDER BY name, dispatch_at asc rows between unbounded preceding and current row), 0),2)::varchar) AS weight
87
+ FROM raw_block_dispatch rbd
88
+ ORDER BY rbd."dispatch_at", rbd."name"
89
+ $$,
90
+ $$
91
+ SELECT b.name FROM ramps r INNER JOIN blocks b ON b.id = r.block_id WHERE r.deleted_at IS NULL AND r.domain_id = '${domain.id}'
92
+ GROUP BY b.id, b.name
93
+ UNION
94
+ SELECT c.name FROM organizations c
95
+ $$
96
+ ) AS (dispatch_at date, ${[...blocks.map(block => `"${block.name}" varchar`), ...organizations.map(organization => `"${organization.name}" varchar`)].join(',')},"unknown" varchar)
97
+ );
98
+ `
99
+ )
100
+
101
+ await tx.query(
102
+ `
103
+ CREATE TEMP TABLE temp_monthly_production_data ON COMMIT DROP AS (
104
+ SELECT TO_CHAR(md.dateDay:: DATE, 'dd/mm/yyyy') as "date"
105
+ , row_to_json((SELECT d FROM (SELECT ${[...blocks.map(block => `pbd."${block.name}"`), ...organizations.map(organization => `pbd."${organization.name}"`)].join(',')}, pbd."unknown") d))::varchar as "blockData"
106
+ , (SELECT sum(weight) FROM raw_block_dispatch rbd WHERE rbd.dispatch_at = pbd.dispatch_at) AS "today"
107
+ FROM (select generate_series((date '${fromDateString}')::timestamp, (date '${toDateString}')::timestamp, interval '1 day')::date as dateDay) as md
108
+ LEFT JOIN pivot_block_dispatch pbd on pbd.dispatch_at::date = md.dateDay
109
+ );
110
+ `
111
+ )
112
+
113
+ const result: any = await tx.query(
114
+ `
115
+ select "date", "blockData", "today"
116
+ from temp_monthly_production_data dt
117
+ `
118
+ )
119
+
120
+ const total: any = await tx.query(
121
+ `
122
+ select count(*) from temp_monthly_production_data
123
+ `
124
+ )
125
+
126
+
127
+ return {
128
+ items: result.map(itm => {
129
+ return {
130
+ ...itm
131
+ }
132
+ }), total: total[0].count
133
+ }
134
+ } catch (error) {
135
+ throw error
136
+ }
137
+ }
138
+ }
@@ -52,10 +52,10 @@ export const dailyProductionReports = {
52
52
  CREATE TEMP TABLE temp_records on commit drop AS (
53
53
  select blk.block_id, coalesce(sum(field_bunches),0) AS today_field_bunches,
54
54
  round(coalesce(sum(ramp_weight),0)::numeric,4) AS today_ramp_weight,
55
- sum(case when transaction_type = 'HARVEST' then field_bunches else 0 end) as today_bunch_harvest,
56
- sum(case when transaction_type = 'LOADING' then -field_bunches else 0 end) as today_bunch_collected,
57
- round(sum(case when transaction_type = 'LOADING' then ramp_weight else 0 end)::numeric,4) as today_ramp_weight_collected,
58
- round(sum(case when transaction_type = 'DISPATCHMENT' then -ramp_weight else 0 end)::numeric,4) as today_weight_dispatch,
55
+ sum(case when transaction_type = 'HARVEST' OR transaction_type = 'BUNCHES_ADJUSTMENT' then field_bunches else 0 end) as today_bunch_harvest,
56
+ sum(case when transaction_type = 'LOADING' OR transaction_type = 'LOADING_ADJUSTMENT' then -field_bunches else 0 end) as today_bunch_collected,
57
+ round(sum(case when transaction_type = 'LOADING' OR transaction_type = 'LOADING_ADJUSTMENT' then ramp_weight else 0 end)::numeric,4) as today_ramp_weight_collected,
58
+ round(sum(case when transaction_type = 'DISPATCHMENT' OR transaction_type = 'DISPATCHMENT_ADJUSTMENT' then -ramp_weight else 0 end)::numeric,4) as today_weight_dispatch,
59
59
  transaction_date
60
60
  from record_transactions t2
61
61
  inner join temp_blocks blk on blk.id = t2.block_id
@@ -127,11 +127,11 @@ export const dailyProductionReports = {
127
127
  )
128
128
 
129
129
  const result: any = await tx.query(`
130
- select * from temp_daily_production_data dt
130
+ select * from temp_daily_production_data dt
131
131
  `)
132
132
 
133
133
  const total: any = await tx.query(`
134
- select count(*) from temp_daily_production_data
134
+ select count(*) from temp_daily_production_data
135
135
  `)
136
136
 
137
137
  let items = result.map(itm => {
@@ -5,6 +5,7 @@ import { yearlyProductionReports } from './yearly-production-reports'
5
5
  import { dailyStaffHarvestReports } from './daily-staff-harvest-reports'
6
6
  import { monthlyBlockDispatchReports } from './monthly-block-dispatch-reports'
7
7
  import { monthlyFfbSaleReports } from './monthly-ffb-sale-reports'
8
+ import { dailyFfbDispatchProductionReports } from './daily-ffb-dispatch-production-reports'
8
9
 
9
10
  export const ReportResolver = {
10
11
  Query: {
@@ -14,7 +15,8 @@ export const ReportResolver = {
14
15
  ...yearlyProductionReports,
15
16
  ...dailyStaffHarvestReports,
16
17
  ...monthlyBlockDispatchReports,
17
- ...monthlyFfbSaleReports
18
+ ...monthlyFfbSaleReports,
19
+ ...dailyFfbDispatchProductionReports
18
20
  },
19
21
  Mutation: {}
20
22
  }
@@ -1,7 +1,7 @@
1
1
  import { EntityManager, getRepository, Not, IsNull } from 'typeorm'
2
2
 
3
3
  import { ListParam, Domain } from '@things-factory/shell'
4
- import { Ramp } from '../../../entities'
4
+ import { Block, Ramp } from '../../../entities'
5
5
 
6
6
  export const monthlyBlockDispatchReports = {
7
7
  async monthlyBlockDispatchReports(_: any, params: ListParam, context: any) {
@@ -27,11 +27,13 @@ export const monthlyBlockDispatchReports = {
27
27
  String(toDate.getDate()).padStart(2, '0')
28
28
  ].join('-')
29
29
 
30
- let ramps = await getRepository(Ramp).find({
31
- where: { domain: domain.id, deletedAt: IsNull() },
32
- relations: ['block']
33
- })
34
-
30
+ let blocks = await tx.getRepository(Block).query(`
31
+ SELECT b.id, b.name FROM ramps r
32
+ INNER JOIN blocks b ON b.id = r.block_id
33
+ WHERE b.deleted_at IS NULL
34
+ AND r.domain_id = $1
35
+ GROUP BY b.id, b.name
36
+ `, [domain.id])
35
37
 
36
38
  // -- WHERE dd.dispatch_at::timestamp between $1::timestamp and $2::timestamp
37
39
  // [fromDateString, toDateString]
@@ -56,8 +58,8 @@ export const monthlyBlockDispatchReports = {
56
58
  CREATE TEMP TABLE pivot_block_dispatch ON COMMIT DROP AS (
57
59
  SELECT * FROM crosstab(
58
60
  $$SELECT rbd.id, rbd.block_name, round(rbd.weight, 2 )::varchar FROM raw_block_dispatch rbd$$,
59
- $$SELECT b.name FROM ramps r INNER JOIN blocks b ON b.id = r.block_id WHERE r.deleted_at IS NULL$$
60
- ) AS ("id" uuid, ${ramps.map(ramp => `"${ramp.block.name}" varchar`).join(',')})
61
+ $$SELECT b.name FROM ramps r INNER JOIN blocks b ON b.id = r.block_id WHERE r.deleted_at IS NULL AND r.domain_id = '${domain.id}' GROUP BY b.id, b.name$$
62
+ ) AS ("id" uuid, ${blocks.map(block => `"${block.name}" varchar`).join(',')})
61
63
  );
62
64
  `)
63
65
 
@@ -73,7 +75,7 @@ export const monthlyBlockDispatchReports = {
73
75
  , dd.transport_no as "lorryNo"
74
76
  , dd.driver_identification as "driverIdentification"
75
77
  , TO_CHAR(dd.dispatch_at:: DATE, 'dd/mm/yyyy') as "date"
76
- , row_to_json((SELECT d FROM (SELECT ${ramps.map(ramp => `pbd."${ramp.block.name}"`).join(',')}) d))::varchar as "blockData"
78
+ , row_to_json((SELECT d FROM (SELECT ${blocks.map(block => `pbd."${block.name}"`).join(',')}) d))::varchar as "blockData"
77
79
  , (SELECT sum(weight) FROM raw_block_dispatch rbd WHERE rbd.id = dd.id) AS "today"
78
80
  FROM daily_dispatches dd
79
81
  INNER JOIN pivot_block_dispatch pbd ON pbd.id = dd.id
@@ -46,7 +46,7 @@ export const monthlyFfbSaleReports = {
46
46
  SELECT "id", dispatch_at, transport_no AS "lorryNo",
47
47
  driver_identification AS "driverIdentification", chit_no AS "chitNo", round(COALESCE(percentage,0)::numeric, 2)::varchar AS "percentage",
48
48
  COALESCE(diapatch_to_name, 'UNKNOWN') AS "dispatchTo", round(COALESCE(tonnage,0), 2)::varchar AS "tonnage",
49
- CASE WHEN forward_rn = 1 THEN dispatch_at::date::varchar ELSE '' END AS "dispatchAt",
49
+ CASE WHEN forward_rn = 1 THEN TO_CHAR(dispatch_at:: DATE, 'dd/mm/yyyy') ELSE '' END AS "dispatchAt",
50
50
  CASE WHEN reverse_rn = 1 THEN round((coalesce(sum(percentage) over (PARTITION BY dispatch_to_id, dispatch_at::date ORDER BY dispatch_at asc rows between unbounded preceding and current row), 0)/forward_rn)::numeric,2)::varchar ELSE '' END AS "todayAvg",
51
51
  CASE WHEN reverse_rn = 1 THEN round((coalesce(sum(percentage) over (PARTITION BY dispatch_to_id ORDER BY dispatch_at asc rows between unbounded preceding and current row), 0)/forward_rn)::numeric,2)::varchar ELSE '' END AS "todateAvg",
52
52
  CASE WHEN reverse_rn = 1 THEN round(coalesce(sum(tonnage) over (PARTITION BY dispatch_to_id, dispatch_at::date ORDER BY dispatch_at asc rows between unbounded preceding and current row), 0),2)::varchar ELSE '' END AS "today",
@@ -78,8 +78,8 @@ export const monthlyProductionReports = {
78
78
  left join(
79
79
  select blk.block_id, sum(total_weight) as total_weight_loaded, sum(total_bunch) as total_bunch_loaded
80
80
  from daily_loadings dl
81
- inner join temp_blocks blk on blk.id = dl.block_id
82
81
  inner join daily_loading_details dld on dld.daily_loading_id = dl.id
82
+ inner join temp_blocks blk on blk.id = dld.block_id
83
83
  where dl.loading_date::timestamp between $1::timestamp and $2::timestamp
84
84
  group by blk.block_id
85
85
  ) as loading on loading.block_id = b.id
@@ -0,0 +1,8 @@
1
+ import gql from 'graphql-tag'
2
+
3
+ export const DailyFfbDispatchProductionReportList = gql`
4
+ type DailyFfbDispatchProductionReportList {
5
+ items: [DailyFfbDispatchProductionReport]
6
+ total: Int
7
+ }
8
+ `
@@ -0,0 +1,9 @@
1
+ import gql from 'graphql-tag'
2
+
3
+ export const DailyFfbDispatchProductionReport = gql`
4
+ type DailyFfbDispatchProductionReport {
5
+ date: String
6
+ blockData: String
7
+ today: String
8
+ }
9
+ `
@@ -12,16 +12,20 @@ export * from './monthly-production-report-list'
12
12
  export * from './monthly-production-report'
13
13
  export * from './yearly-production-report-list'
14
14
  export * from './yearly-production-report'
15
+ export * from './daily-ffb-dispatch-production-report'
16
+ export * from './daily-ffb-dispatch-production-report-list'
15
17
 
16
18
  export const ReportResolver = gql`
17
19
  extend type Query {
18
20
  dailyProductionReports(filters: [Filter], pagination: Pagination, sortings: [Sorting]): DailyProductionReportList
19
21
  @transaction
22
+
20
23
  monthlyProductionReports(
21
24
  filters: [Filter]
22
25
  pagination: Pagination
23
26
  sortings: [Sorting]
24
27
  ): MonthlyProductionReportList @transaction
28
+
25
29
  yearlyProductionReports(filters: [Filter], pagination: Pagination, sortings: [Sorting]): YearlyProductionReportList
26
30
  @transaction
27
31
  dailyStaffHarvestReports(
@@ -29,21 +33,30 @@ export const ReportResolver = gql`
29
33
  pagination: Pagination
30
34
  sortings: [Sorting]
31
35
  ): DailyStaffHarvestReportList @transaction
36
+
32
37
  monthlyDispatchSummaryReports(
33
38
  filters: [Filter],
34
39
  pagination: Pagination,
35
40
  sortings: [Sorting]
36
41
  ): MonthlyDispatchSummaryReportList @transaction
42
+
37
43
  monthlyBlockDispatchReports(
38
44
  filters: [Filter],
39
45
  pagination: Pagination,
40
46
  sortings: [Sorting]
41
47
  ): MonthlyBlockDispatchReportList @transaction
48
+
42
49
  monthlyFfbSaleReports(
43
50
  filters: [Filter],
44
51
  pagination: Pagination,
45
52
  sortings: [Sorting]
46
53
  ): MonthlyDispatchSummaryReportList @transaction
54
+
55
+ dailyFfbDispatchProductionReports(
56
+ filters: [Filter],
57
+ pagination: Pagination,
58
+ sortings: [Sorting]
59
+ ): DailyFfbDispatchProductionReportList @transaction
47
60
  }
48
61
 
49
62
  `
@@ -43,8 +43,8 @@ export async function getDailyBlockCalculatedValues(
43
43
  select block_id, transaction_date,
44
44
  (select opening_field_bunches from transactions dt where dt.block_id = trx.block_id and dt.seq = min(trx.seq)) as prev_bunch_bal_in_field,
45
45
  0 as prev_weight_bal_in_ramp,
46
- sum(case when transaction_type = 'HARVEST' then field_bunches else 0 end) as today_bunch_harvest,
47
- sum(case when transaction_type = 'LOADING' then -field_bunches else 0 end) as today_bunch_collected,
46
+ sum(case when transaction_type = 'HARVEST' OR transaction_type = 'BUNCHES_ADJUSTMENT' then field_bunches else 0 end) as today_bunch_harvest,
47
+ sum(case when transaction_type = 'LOADING' OR transaction_type = 'LOADING_ADJUSTMENT' then -field_bunches else 0 end) as today_bunch_collected,
48
48
  0 as today_weight_dispatch,
49
49
  0 as today_weight_bal_in_ramp,
50
50
  (
@@ -47,7 +47,8 @@ export async function generateTransactionHistory(
47
47
  if (
48
48
  transactionType === TRANSACTION_TYPE.LOADING ||
49
49
  transactionType === TRANSACTION_TYPE.DISPATCHMENT ||
50
- transactionType === TRANSACTION_TYPE.TONNAGE_ADJUSTMENT
50
+ transactionType === TRANSACTION_TYPE.LOADING_ADJUSTMENT ||
51
+ transactionType === TRANSACTION_TYPE.DISPATCHMENT_ADJUSTMENT
51
52
  ) {
52
53
  await switchRampStatus(domain, ramp, user, trxMgr)
53
54
  }