@adatechnology/purchase-module 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,186 @@
1
+ CREATE SCHEMA "purchase";
2
+ --> statement-breakpoint
3
+ CREATE TABLE "purchase"."order_counters" (
4
+ "company_id" uuid PRIMARY KEY NOT NULL,
5
+ "last_number" integer DEFAULT 0 NOT NULL
6
+ );
7
+ --> statement-breakpoint
8
+ CREATE TABLE "purchase"."order_lines" (
9
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
10
+ "order_id" uuid NOT NULL,
11
+ "company_id" uuid NOT NULL,
12
+ "position" integer NOT NULL,
13
+ "supplier_item_id" uuid,
14
+ "item_ref" varchar(128),
15
+ "item_name" varchar(160) NOT NULL,
16
+ "supplier_sku" varchar(100),
17
+ "purchase_unit" varchar(12) NOT NULL,
18
+ "units_per_purchase" numeric(14, 4) DEFAULT '1' NOT NULL,
19
+ "stock_unit" varchar(8),
20
+ "quantity" numeric(14, 4) NOT NULL,
21
+ "unit_price" numeric(14, 4) DEFAULT '0' NOT NULL,
22
+ "line_total" numeric(14, 2) DEFAULT '0' NOT NULL,
23
+ "received_quantity" numeric(14, 4) DEFAULT '0' NOT NULL,
24
+ "canceled_quantity" numeric(14, 4) DEFAULT '0' NOT NULL,
25
+ "notes" varchar(500),
26
+ CONSTRAINT "purchase_order_lines_quantity_positive" CHECK ("quantity" > 0),
27
+ CONSTRAINT "purchase_order_lines_units_positive" CHECK ("units_per_purchase" > 0)
28
+ );
29
+ --> statement-breakpoint
30
+ CREATE TABLE "purchase"."orders" (
31
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
32
+ "company_id" uuid NOT NULL,
33
+ "number" integer NOT NULL,
34
+ "supplier_id" uuid NOT NULL,
35
+ "status" varchar(20) DEFAULT 'draft' NOT NULL,
36
+ "expected_at" date,
37
+ "notes" text DEFAULT '' NOT NULL,
38
+ "subtotal" numeric(14, 2) DEFAULT '0' NOT NULL,
39
+ "freight" numeric(14, 2) DEFAULT '0' NOT NULL,
40
+ "discount" numeric(14, 2) DEFAULT '0' NOT NULL,
41
+ "total" numeric(14, 2) DEFAULT '0' NOT NULL,
42
+ "payment_term_days" integer[] DEFAULT '{0}'::integer[] NOT NULL,
43
+ "requires_approval" boolean DEFAULT false NOT NULL,
44
+ "created_by" varchar(128),
45
+ "submitted_by" varchar(128),
46
+ "submitted_at" timestamp with time zone,
47
+ "approved_by" varchar(128),
48
+ "approved_at" timestamp with time zone,
49
+ "rejection_reason" varchar(500),
50
+ "sent_at" timestamp with time zone,
51
+ "sent_via" varchar(12),
52
+ "canceled_at" timestamp with time zone,
53
+ "cancel_reason" varchar(500),
54
+ "closed_at" timestamp with time zone,
55
+ "last_late_alert_at" date,
56
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
57
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
58
+ );
59
+ --> statement-breakpoint
60
+ CREATE TABLE "purchase"."receipt_lines" (
61
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
62
+ "receipt_id" uuid NOT NULL,
63
+ "company_id" uuid NOT NULL,
64
+ "position" integer NOT NULL,
65
+ "order_line_id" uuid,
66
+ "supplier_item_id" uuid,
67
+ "item_ref" varchar(128),
68
+ "item_name" varchar(160) NOT NULL,
69
+ "supplier_sku" varchar(100),
70
+ "purchase_unit" varchar(12) NOT NULL,
71
+ "units_per_purchase" numeric(14, 4) DEFAULT '1' NOT NULL,
72
+ "stock_unit" varchar(8),
73
+ "quantity" numeric(14, 4) NOT NULL,
74
+ "unit_price" numeric(14, 4) NOT NULL,
75
+ "line_total" numeric(14, 2) NOT NULL,
76
+ "allocated" numeric(14, 2) DEFAULT '0' NOT NULL,
77
+ "landed_unit_cost" numeric(14, 4),
78
+ "batch_code" varchar(100),
79
+ "expires_at" date,
80
+ "divergence" varchar(10) DEFAULT 'none' NOT NULL,
81
+ "divergence_reason" varchar(500),
82
+ "stock_status" varchar(10) DEFAULT 'skipped' NOT NULL,
83
+ CONSTRAINT "purchase_receipt_lines_quantity_positive" CHECK ("quantity" > 0)
84
+ );
85
+ --> statement-breakpoint
86
+ CREATE TABLE "purchase"."receipt_payments" (
87
+ "company_id" uuid NOT NULL,
88
+ "receipt_id" uuid NOT NULL,
89
+ "installment_number" integer NOT NULL,
90
+ "paid_amount" numeric(14, 2) NOT NULL,
91
+ "paid_at" date NOT NULL,
92
+ CONSTRAINT "receipt_payments_receipt_id_installment_number_pk" PRIMARY KEY("receipt_id","installment_number")
93
+ );
94
+ --> statement-breakpoint
95
+ CREATE TABLE "purchase"."receipts" (
96
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
97
+ "company_id" uuid NOT NULL,
98
+ "order_id" uuid,
99
+ "supplier_id" uuid NOT NULL,
100
+ "received_at" timestamp with time zone NOT NULL,
101
+ "invoice_number" varchar(30),
102
+ "invoice_key" varchar(44),
103
+ "invoice_issued_at" timestamp with time zone,
104
+ "subtotal" numeric(14, 2) NOT NULL,
105
+ "freight" numeric(14, 2) DEFAULT '0' NOT NULL,
106
+ "discount" numeric(14, 2) DEFAULT '0' NOT NULL,
107
+ "total" numeric(14, 2) NOT NULL,
108
+ "notes" text DEFAULT '' NOT NULL,
109
+ "actor_id" varchar(128),
110
+ "idempotency_key" varchar(128),
111
+ "installments" text DEFAULT '[]' NOT NULL,
112
+ "stock_status" varchar(10) DEFAULT 'skipped' NOT NULL,
113
+ "payable_status" varchar(10) DEFAULT 'skipped' NOT NULL,
114
+ "payable_ref" varchar(200),
115
+ "payment_status" varchar(10) DEFAULT 'none' NOT NULL,
116
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
117
+ );
118
+ --> statement-breakpoint
119
+ CREATE TABLE "purchase"."supplier_items" (
120
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
121
+ "company_id" uuid NOT NULL,
122
+ "supplier_id" uuid NOT NULL,
123
+ "item_ref" varchar(128),
124
+ "item_name" varchar(160) NOT NULL,
125
+ "supplier_sku" varchar(100),
126
+ "purchase_unit" varchar(12) NOT NULL,
127
+ "units_per_purchase" numeric(14, 4) DEFAULT '1' NOT NULL,
128
+ "stock_unit" varchar(8),
129
+ "last_price" numeric(14, 4),
130
+ "last_price_at" timestamp with time zone,
131
+ "priority" integer DEFAULT 0 NOT NULL,
132
+ "is_active" boolean DEFAULT true NOT NULL,
133
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
134
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
135
+ CONSTRAINT "purchase_supplier_items_units_positive" CHECK ("units_per_purchase" > 0),
136
+ CONSTRAINT "purchase_supplier_items_price_not_negative" CHECK ("last_price" is null or "last_price" >= 0)
137
+ );
138
+ --> statement-breakpoint
139
+ CREATE TABLE "purchase"."suppliers" (
140
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
141
+ "company_id" uuid NOT NULL,
142
+ "name" varchar(160) NOT NULL,
143
+ "document" varchar(20),
144
+ "contact_name" varchar(120),
145
+ "email" varchar(254),
146
+ "phone" varchar(30),
147
+ "whatsapp" varchar(30),
148
+ "payment_term_days" integer[] DEFAULT '{0}'::integer[] NOT NULL,
149
+ "lead_time_days" integer,
150
+ "minimum_order_amount" numeric(14, 2),
151
+ "delivery_weekdays" integer[] DEFAULT '{}'::integer[] NOT NULL,
152
+ "notes" text DEFAULT '' NOT NULL,
153
+ "is_active" boolean DEFAULT true NOT NULL,
154
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
155
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
156
+ );
157
+ --> statement-breakpoint
158
+ ALTER TABLE "purchase"."order_lines" ADD CONSTRAINT "order_lines_order_id_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "purchase"."orders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
159
+ ALTER TABLE "purchase"."order_lines" ADD CONSTRAINT "order_lines_supplier_item_id_supplier_items_id_fk" FOREIGN KEY ("supplier_item_id") REFERENCES "purchase"."supplier_items"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
160
+ ALTER TABLE "purchase"."orders" ADD CONSTRAINT "orders_supplier_id_suppliers_id_fk" FOREIGN KEY ("supplier_id") REFERENCES "purchase"."suppliers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
161
+ ALTER TABLE "purchase"."receipt_lines" ADD CONSTRAINT "receipt_lines_receipt_id_receipts_id_fk" FOREIGN KEY ("receipt_id") REFERENCES "purchase"."receipts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
162
+ ALTER TABLE "purchase"."receipt_lines" ADD CONSTRAINT "receipt_lines_order_line_id_order_lines_id_fk" FOREIGN KEY ("order_line_id") REFERENCES "purchase"."order_lines"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
163
+ ALTER TABLE "purchase"."receipt_lines" ADD CONSTRAINT "receipt_lines_supplier_item_id_supplier_items_id_fk" FOREIGN KEY ("supplier_item_id") REFERENCES "purchase"."supplier_items"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
164
+ ALTER TABLE "purchase"."receipt_payments" ADD CONSTRAINT "receipt_payments_receipt_id_receipts_id_fk" FOREIGN KEY ("receipt_id") REFERENCES "purchase"."receipts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
165
+ ALTER TABLE "purchase"."receipts" ADD CONSTRAINT "receipts_order_id_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "purchase"."orders"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
166
+ ALTER TABLE "purchase"."receipts" ADD CONSTRAINT "receipts_supplier_id_suppliers_id_fk" FOREIGN KEY ("supplier_id") REFERENCES "purchase"."suppliers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
167
+ ALTER TABLE "purchase"."supplier_items" ADD CONSTRAINT "supplier_items_supplier_id_suppliers_id_fk" FOREIGN KEY ("supplier_id") REFERENCES "purchase"."suppliers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
168
+ CREATE INDEX "idx_purchase_order_lines_order" ON "purchase"."order_lines" USING btree ("order_id","position");--> statement-breakpoint
169
+ CREATE INDEX "idx_purchase_order_lines_item" ON "purchase"."order_lines" USING btree ("company_id","item_ref");--> statement-breakpoint
170
+ CREATE UNIQUE INDEX "uq_purchase_orders_company_number" ON "purchase"."orders" USING btree ("company_id","number");--> statement-breakpoint
171
+ CREATE INDEX "idx_purchase_orders_company_status" ON "purchase"."orders" USING btree ("company_id","status","created_at");--> statement-breakpoint
172
+ CREATE INDEX "idx_purchase_orders_supplier" ON "purchase"."orders" USING btree ("company_id","supplier_id");--> statement-breakpoint
173
+ CREATE INDEX "idx_purchase_orders_open_expected" ON "purchase"."orders" USING btree ("expected_at") WHERE "status" in ('approved', 'sent', 'partially_received');--> statement-breakpoint
174
+ CREATE INDEX "idx_purchase_receipt_lines_receipt" ON "purchase"."receipt_lines" USING btree ("receipt_id","position");--> statement-breakpoint
175
+ CREATE INDEX "idx_purchase_receipt_lines_item" ON "purchase"."receipt_lines" USING btree ("company_id","item_ref");--> statement-breakpoint
176
+ CREATE INDEX "idx_purchase_receipt_lines_supplier_item" ON "purchase"."receipt_lines" USING btree ("supplier_item_id");--> statement-breakpoint
177
+ CREATE INDEX "idx_purchase_receipts_company_received" ON "purchase"."receipts" USING btree ("company_id","received_at");--> statement-breakpoint
178
+ CREATE INDEX "idx_purchase_receipts_order" ON "purchase"."receipts" USING btree ("order_id");--> statement-breakpoint
179
+ CREATE UNIQUE INDEX "uq_purchase_receipts_invoice_key" ON "purchase"."receipts" USING btree ("company_id","invoice_key") WHERE "invoice_key" is not null;--> statement-breakpoint
180
+ CREATE UNIQUE INDEX "uq_purchase_receipts_idempotency" ON "purchase"."receipts" USING btree ("company_id","idempotency_key") WHERE "idempotency_key" is not null;--> statement-breakpoint
181
+ CREATE INDEX "idx_purchase_receipts_failed" ON "purchase"."receipts" USING btree ("company_id") WHERE "stock_status" = 'failed' or "payable_status" = 'failed';--> statement-breakpoint
182
+ CREATE INDEX "idx_purchase_supplier_items_item" ON "purchase"."supplier_items" USING btree ("company_id","item_ref");--> statement-breakpoint
183
+ CREATE UNIQUE INDEX "uq_purchase_supplier_items_sku" ON "purchase"."supplier_items" USING btree ("company_id","supplier_id","supplier_sku") WHERE "supplier_sku" is not null;--> statement-breakpoint
184
+ CREATE UNIQUE INDEX "uq_purchase_supplier_items_ref" ON "purchase"."supplier_items" USING btree ("company_id","supplier_id","item_ref") WHERE "item_ref" is not null;--> statement-breakpoint
185
+ CREATE INDEX "idx_purchase_suppliers_company_name" ON "purchase"."suppliers" USING btree ("company_id","name");--> statement-breakpoint
186
+ CREATE UNIQUE INDEX "uq_purchase_suppliers_company_document" ON "purchase"."suppliers" USING btree ("company_id","document") WHERE "document" is not null;