| | 1 | == Generating Store Reports |
| | 2 | |
| | 3 | === Actors: **Registered Boss** |
| | 4 | |
| | 5 | **1.** One of the bosses of the store request data for monthly profit for the past month from {{{orders}}} and {{{refund}}}. |
| | 6 | |
| | 7 | **2.** The system gets information from {{{orders}}} and {{{refunds}}} for the month and calculates the overall profit for the month. |
| | 8 | |
| | 9 | |
| | 10 | {{{#!sql |
| | 11 | SELECT SUM(order.total) AS sales FROM order |
| | 12 | WHERE order.placement_date >= DATE_TRUNC('month', CURRENT_DATE) AND |
| | 13 | order.placement_date < (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month') AND |
| | 14 | order.store_ID = '001'; |
| | 15 | |
| | 16 | SELECT SUM(refund.amount) AS damages FROM refund |
| | 17 | WHERE refund.approval_date >= DATE_TRUNC('month', CURRENT_DATE) AND |
| | 18 | order.approval_date < (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month') AND |
| | 19 | order.store_ID = '001'; |
| | 20 | |
| | 21 | |
| | 22 | **3.** One of the boss of the store sends data for the store through {{{exchanges_data}}} and {{{worked}}} for the current month in the last days of the month. |
| | 23 | |
| | 24 | {{{#!sql |
| | 25 | INSERT INTO exchanges_data(report_date, store_ID, monthly_profit, date, sales, damages) VALUES |
| | 26 | ('2025-12-31 23:59:59', '001', 60000.00 , '2025-12-30 15:28:46', 62000.00, 2000.00); |
| | 27 | |
| | 28 | }}} |
| | 29 | |
| | 30 | {{{#!sql |
| | 31 | INSERT INTO worked (personal_ID, report_date, store_ID, wage, pay_method, total_hours, week) VALUES |
| | 32 | ('0010001', '2025-12-31 23:59:59', '001', 15000, 'monthly', 52, '2025-12-29 - 2025-12-31'), |
| | 33 | ('0010002', '2025-12-31 23:59:59', '001', 10000, 'monthly', 34, '2025-12-29 - 2025-12-31'); |
| | 34 | |
| | 35 | }}} |
| | 36 | |
| | 37 | **4.** The system automatically generates a monthly report for the store on the last day of each month. The data is noted in {{{report}}}. |
| | 38 | |
| | 39 | {{{#!sql |
| | 40 | INSERT INTO report (date, store_ID, overall_profit, sales_trend, marketing_growth, owner_signature) VALUES |
| | 41 | ('2025-12-31 23:59:59', '001', 0.00, '', '', ''), |
| | 42 | |
| | 43 | }}} |
| | 44 | |
| | 45 | |
| | 46 | **5.** The system collects ''hours worked, wages'', and ''pay_type'' from the {{{worked}}} table: |
| | 47 | |
| | 48 | {{{#!sql |
| | 49 | SELECT personal_ID, SUM(total_hours) AS total_hours, wage, pay_method FROM worked |
| | 50 | WHERE store_ID='001' |
| | 51 | AND week.first_date >= DATE_TRUNC('month', CURRENT_DATE) |
| | 52 | AND week.last_date < (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month') |
| | 53 | GROUP BY personal_ID; |
| | 54 | |
| | 55 | }}} |
| | 56 | |
| | 57 | **6.** The system collects the ''monthly_profit, sales'' and ''damages'' from {{{exchanges_data}}}: |
| | 58 | |
| | 59 | {{{#!sql |
| | 60 | SELECT monthly_profit AS total_monthly_profit, sales AS total_sales, damages AS total_damages |
| | 61 | FROM exchanges_data |
| | 62 | WHERE store_ID='001' |
| | 63 | AND date >= DATE_TRUNC('month', CURRENT_DATE) |
| | 64 | AND date < (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month'); |
| | 65 | |
| | 66 | }}} |
| | 67 | |
| | 68 | **7.** The report is in the {{{report}}} table is updated with the data: |
| | 69 | |
| | 70 | {{{#!sql |
| | 71 | UPDATE report |
| | 72 | SET overall_profit='35000', |
| | 73 | sales_trend='Growing', |
| | 74 | markething_growth='Stable' |
| | 75 | WHERE report.date >= DATE_TRUNC('month', CURRENT_DATE) AND |
| | 76 | report.date < (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month') AND |
| | 77 | report.store_ID = '001'; |
| | 78 | |
| | 79 | }}} |
| | 80 | |
| | 81 | **8.** The system sends the report to all bosses of the store for approval. |
| | 82 | |
| | 83 | {{{#!sql |
| | 84 | SELECT b.boss_ID, p.first_name, p.last_name, p.email |
| | 85 | FROM boss b |
| | 86 | JOIN personal p ON b.boss_ID = p.id |
| | 87 | JOIN works_in_store w ON p.id = w.personal_ID |
| | 88 | WHERE w.store_ID='001'; |
| | 89 | }}} |
| | 90 | |
| | 91 | **9.** Each boss reviews and signs the report. Once at least one boss signs, the report is considered approved. |
| | 92 | |
| | 93 | {{{#!sql |
| | 94 | UPDATE report |
| | 95 | SET owner_signature = boss.signature |
| | 96 | WHERE store_ID='001' AND date = '2025-12-30 00:00:00' AND boss.boss_ID = works_in.personal_ID and works_id.store_ID = '001'; |
| | 97 | }}} |