Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

794 B
Raw Blame
-- Create new function to insert both terms and settings
CREATE OR REPLACE FUNCTION insert_company_related_records()
RETURNS TRIGGER AS $$
BEGIN
  -- Insert terms record
  INSERT INTO "terms" ("id")
  VALUES (NEW.id);
  
  -- Insert company settings record with metric setting based on country/currency
  INSERT INTO "companySettings" ("id", "useMetric") 
  VALUES (
    NEW.id, 
    CASE 
      WHEN NEW."countryCode" = 'US' OR NEW."baseCurrencyCode" = 'USD' THEN false
      ELSE true
    END
  );
  
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Update useMetric setting for all existing companies
UPDATE "companySettings" 
SET "useMetric" = CASE 
  WHEN c."countryCode" = 'US' OR c."baseCurrencyCode" = 'USD' THEN false
  ELSE true
END
FROM "company" c
WHERE "companySettings"."id" = c."id";