fix(pos): prevent null branch_id errors during sales

- process_sale now falls back to main branch when g.branch_id is missing
- Accept branch_id from request body as override
- Make update_inventory_stock trigger skip operations with NULL branch_id
- Applied updated trigger to all active tenant DBs
This commit is contained in:
2026-06-11 23:11:02 +00:00
parent 203960fff3
commit 383799ff3d
2 changed files with 22 additions and 1 deletions

View File

@@ -230,6 +230,13 @@ ALTER TABLE cfdi_queue ALTER COLUMN sale_id DROP NOT NULL;
CREATE OR REPLACE FUNCTION update_inventory_stock()
RETURNS TRIGGER AS $$
BEGIN
-- Skip operations that are not tied to a specific branch.
-- Per-branch stock tracking requires a branch_id; without it we can't
-- assign the stock to any location.
IF NEW.branch_id IS NULL THEN
RETURN NEW;
END IF;
INSERT INTO inventory_stock (inventory_id, branch_id, stock)
VALUES (NEW.inventory_id, NEW.branch_id, NEW.quantity)
ON CONFLICT (inventory_id, branch_id) DO UPDATE