| | 1 | = Automatic update of product availability after an order |
| | 2 | |
| | 3 | === Description |
| | 4 | This trigger automatically decreases the available quantity of a product when the product is included in a customer order. |
| | 5 | |
| | 6 | The sells.quantity attribute represents the quantity of a product currently available in a store. When an order is placed, the quantity ordered must be deducted from the available quantity. |
| | 7 | |
| | 8 | This ensures that product availability is automatically synchronized with customer orders. |
| | 9 | |
| | 10 | ==== Tables involved |
| | 11 | - Product |
| | 12 | - Order |
| | 13 | - includes |
| | 14 | - sells |
| | 15 | |
| | 16 | ==== Type of trigger |
| | 17 | - AFTER INSERT |
| | 18 | - AFTER UPDATE |
| | 19 | Activated on the `includes` table. |
| | 20 | |
| | 21 | ==== SQL Code |
| | 22 | {{{#!sql |
| | 23 | CREATE OR REPLACE FUNCTION update_product_availability() |
| | 24 | RETURNS TRIGGER |
| | 25 | LANGUAGE plpgsql |
| | 26 | AS $$ |
| | 27 | BEGIN |
| | 28 | -- When a product is newly added to an order, |
| | 29 | -- decrease the available quantity. |
| | 30 | IF TG_OP = 'INSERT' THEN |
| | 31 | |
| | 32 | UPDATE sells |
| | 33 | SET quantity = quantity - ( |
| | 34 | SELECT o.quantity |
| | 35 | FROM "order" o |
| | 36 | WHERE o.order_num = NEW.order_num |
| | 37 | ) |
| | 38 | WHERE code = NEW.code; |
| | 39 | |
| | 40 | -- If the product/order entry is modified, |
| | 41 | -- restore the old quantity and subtract the new quantity. |
| | 42 | ELSIF TG_OP = 'UPDATE' THEN |
| | 43 | |
| | 44 | UPDATE sells |
| | 45 | SET quantity = quantity |
| | 46 | + ( |
| | 47 | SELECT o.quantity |
| | 48 | FROM "order" o |
| | 49 | WHERE o.order_num = OLD.order_num |
| | 50 | ) |
| | 51 | - ( |
| | 52 | SELECT o.quantity |
| | 53 | FROM "order" o |
| | 54 | WHERE o.order_num = NEW.order_num |
| | 55 | ) |
| | 56 | WHERE code = NEW.code; |
| | 57 | |
| | 58 | END IF; |
| | 59 | |
| | 60 | RETURN NULL; |
| | 61 | END; |
| | 62 | $$; |
| | 63 | |
| | 64 | |
| | 65 | CREATE TRIGGER trg_update_product_availability |
| | 66 | AFTER INSERT OR UPDATE |
| | 67 | ON includes |
| | 68 | FOR EACH ROW |
| | 69 | EXECUTE FUNCTION update_product_availability(); |
| | 70 | |
| | 71 | |
| | 72 | }}} |
| | 73 | |
| | 74 | === Logic explanation |
| | 75 | |
| | 76 | When a product is added to an order, the trigger: |
| | 77 | |
| | 78 | **1.** Identifies the ordered product using NEW.code. |
| | 79 | **2.** Identifies the order using NEW.order_num. |
| | 80 | **3.** Retrieves the ordered quantity from ORDER. |
| | 81 | **4.** Finds the corresponding product in sells. |
| | 82 | **5.** Decreases the available quantity by the ordered amount. |
| | 83 | |
| | 84 | For an update, the trigger compensates for the old quantity before applying the new quantity. |
| | 85 | |
| | 86 | For example: |
| | 87 | |
| | 88 | {{{#!TXT |
| | 89 | |
| | 90 | Initial product quantity: 20 |
| | 91 | Customer orders: 3 |
| | 92 | |
| | 93 | New available quantity: 20 - 3 = 17 |
| | 94 | |
| | 95 | }}} |
| | 96 | |
| | 97 | === Reason for trigger |
| | 98 | |
| | 99 | This trigger is useful because: |
| | 100 | |
| | 101 | - Product availability must automatically reflect customer orders. |
| | 102 | - It prevents the application from having to manually update inventory. |
| | 103 | - It reduces the possibility of inconsistent inventory data. |
| | 104 | - It centralizes inventory-related business logic inside the database. |
| | 105 | - It ensures that every order affects product availability consistently. |