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.

refactor: services for multi-tenant - accounting

Brad Barbin4db73f82142b
diff --git a/apps/carbon/app/modules/accounting/accounting.service.ts b/apps/carbon/app/modules/accounting/accounting.service.ts
index 30384e0291..4d3b2e4aea 100644
--- a/apps/carbon/app/modules/accounting/accounting.service.ts
+++ b/apps/carbon/app/modules/accounting/accounting.service.ts
@@ -12,7 +12,6 @@ import type {
currencyValidator,
defaultAcountValidator,
fiscalYearSettingsValidator,
- journalLineValidator,
partLedgerValidator,
paymentTermValidator,
} from "./accounting.models";
@@ -70,9 +69,9 @@ export async function deleteAccount(
export async function deleteAccountCategory(
client: SupabaseClient<Database>,
- accountSubcategoryId: string
+ accountCategoryId: string
) {
- return client.from("accountCategory").delete().eq("id", accountSubcategoryId);
+ return client.from("accountCategory").delete().eq("id", accountCategoryId);
}
export async function deleteAccountSubcategory(
@@ -111,6 +110,7 @@ export async function getAccount(
export async function getAccounts(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters & {
search: string | null;
}
@@ -120,6 +120,7 @@ export async function getAccounts(
.select("*", {
count: "exact",
})
+ .eq("companyId", companyId)
.eq("active", true);
if (args.search) {
@@ -134,6 +135,7 @@ export async function getAccounts(
export async function getAccountsList(
client: SupabaseClient<Database>,
+ companyId: number,
args?: {
type?: string | null;
incomeBalance?: string | null;
@@ -143,6 +145,7 @@ export async function getAccountsList(
let query = client
.from("account")
.select("number, name, incomeBalance")
+ .eq("companyId", companyId)
.eq("active", true);
if (args?.type) {
@@ -163,13 +166,17 @@ export async function getAccountsList(
export async function getAccountCategories(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters & {
search: string | null;
}
) {
- let query = client.from("accountCategories").select("*", {
- count: "exact",
- });
+ let query = client
+ .from("accountCategories")
+ .select("*", {
+ count: "exact",
+ })
+ .eq("companyId", companyId);
if (args.search) {
query = query.ilike("category", `%${args.search}%`);
@@ -184,11 +191,13 @@ export async function getAccountCategories(
}
export async function getAccountCategoriesList(
- client: SupabaseClient<Database>
+ client: SupabaseClient<Database>,
+ companyId: number
) {
return client
.from("accountCategory")
.select("*")
+ .eq("companyId", companyId)
.order("category", { ascending: true });
}
@@ -205,6 +214,7 @@ export async function getAccountCategory(
export async function getAccountSubcategories(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters & {
search: string | null;
}
@@ -214,6 +224,7 @@ export async function getAccountSubcategories(
.select("*", {
count: "exact",
})
+ .eq("companyId", companyId)
.eq("active", true);
if (args.search) {
@@ -290,16 +301,21 @@ function getAccountTotal(
return total;
}
-export async function getBaseCurrency(client: SupabaseClient<Database>) {
+export async function getBaseCurrency(
+ client: SupabaseClient<Database>,
+ companyId: number
+) {
return client
.from("currency")
.select("*")
+ .eq("companyId", companyId)
.eq("isBaseCurrency", true)
.single();
}
export async function getChartOfAccounts(
client: SupabaseClient<Database>,
+ companyId: number,
args: Omit<GenericQueryFilters, "limit" | "offset"> & {
name: string | null;
incomeBalance: string | null;
@@ -307,7 +323,11 @@ export async function getChartOfAccounts(
endDate: string | null;
}
) {
- let accountsQuery = client.from("accounts").select("*").eq("active", true);
+ let accountsQuery = client
+ .from("accounts")
+ .select("*")
+ .eq("companyId", companyId)
+ .eq("active", true);
if (args.incomeBalance) {
accountsQuery = accountsQuery.eq("incomeBalance", args.incomeBalance);
@@ -378,6 +398,7 @@ export async function getCurrency(
export async function getCurrencies(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters & {
search: string | null;
}
@@ -387,6 +408,7 @@ export async function getCurrencies(
.select("*", {
count: "exact",
})
+ .eq("companyId", companyId)
.eq("active", true);
if (args.search) {
@@ -399,10 +421,14 @@ export async function getCurrencies(
return query;
}
-export async function getCurrenciesList(client: SupabaseClient<Database>) {
+export async function getCurrenciesList(
+ client: SupabaseClient<Database>,
+ companyId: number
+) {
return client
.from("currency")
.select("code, name")
+ .eq("companyId", companyId)
.order("name", { ascending: true });
}
@@ -420,12 +446,26 @@ export async function getCurrentAccountingPeriod(
.single();
}
-export async function getDefaultAccounts(client: SupabaseClient<Database>) {
- return client.from("accountDefault").select("*").eq("id", true).single();
+export async function getDefaultAccounts(
+ client: SupabaseClient<Database>,
+ companyId: number
+) {
+ return client
+ .from("accountDefault")
+ .select("*")
+ .eq("companyId", companyId)
+ .single();
}
-export async function getFiscalYearSettings(client: SupabaseClient<Database>) {
- return client.from("fiscalYearSettings").select("*").eq("id", true).single();
+export async function getFiscalYearSettings(
+ client: SupabaseClient<Database>,
+ companyId: number
+) {
+ return client
+ .from("fiscalYearSettings")
+ .select("*")
+ .eq("companyId", companyId)
+ .single();
}
export async function getInventoryPostingGroup(
@@ -454,11 +494,15 @@ export async function getInventoryPostingGroup(
export async function getInventoryPostingGroups(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters
) {
- let query = client.from("postingGroupInventory").select("*", {
- count: "exact",
- });
+ let query = client
+ .from("postingGroupInventory")
+ .select("*", {
+ count: "exact",
+ })
+ .eq("companyId", companyId);
query = setGenericQueryFilters(query, args, [
{ column: "partGroupId", ascending: false },
@@ -479,6 +523,7 @@ export async function getPaymentTerm(
export async function getPaymentTerms(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters & {
search: string | null;
}
@@ -488,6 +533,7 @@ export async function getPaymentTerms(
.select("*", {
count: "exact",
})
+ .eq("companyId", companyId)
.eq("active", true);
if (args.search) {
@@ -500,10 +546,14 @@ export async function getPaymentTerms(
return query;
}
-export async function getPaymentTermsList(client: SupabaseClient<Database>) {
+export async function getPaymentTermsList(
+ client: SupabaseClient<Database>,
+ companyId: number
+) {
return client
.from("paymentTerm")
.select("id, name")
+ .eq("companyId", companyId)
.eq("active", true)
.order("name", { ascending: true });
}
@@ -534,11 +584,15 @@ export async function getPurchasingPostingGroup(
export async function getPurchasingPostingGroups(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters
) {
- let query = client.from("postingGroupPurchasing").select("*", {
- count: "exact",
- });
+ let query = client
+ .from("postingGroupPurchasing")
+ .select("*", {
+ count: "exact",
+ })
+ .eq("companyId", companyId);
query = setGenericQueryFilters(query, args, [
{ column: "partGroupId", ascending: false },
@@ -572,11 +626,15 @@ export async function getPurchasingSalesGroup(
export async function getSalesPostingGroups(
client: SupabaseClient<Database>,
+ companyId: number,
args: GenericQueryFilters
) {
- let query = client.from("postingGroupSales").select("*", {
- count: "exact",
- });
+ let query = client
+ .from("postingGroupSales")
+ .select("*", {
+ count: "exact",
+ })
+ .eq("companyId", companyId);
query = setGenericQueryFilters(query, args, [
{ column: "partGroupId", ascending: false },
@@ -584,30 +642,20 @@ export async function getSalesPostingGroups(
return query;
}
-export async function insertJournalLines(
- client: SupabaseClient<Database>,
- journalLines: z.infer<typeof journalLineValidator>[]
-) {
- return client.from("journalLedger").insert(journalLines);
-}
-
-export async function insertJournalEntry(
- client: SupabaseClient<Database>,
- generalEntry: z.infer<typeof journalLineValidator>
-) {
- return client.from("generalLedger").insert([generalEntry]);
-}
-
export async function insertPartEntries(
client: SupabaseClient<Database>,
- partEntries: z.infer<typeof partLedgerValidator>[]
+ partEntries: (z.infer<typeof partLedgerValidator> & {
+ companyId: number;
+ })[]
) {
return client.from("partLedger").insert(partEntries);
}
export async function insertPartLedger(
client: SupabaseClient<Database>,
- partEntry: z.infer<typeof partLedgerValidator>
+ partEntry: z.infer<typeof partLedgerValidator> & {
+ companyId: number;
+ }
) {
return client.from("partLedger").insert([partEntry]);
}
@@ -615,28 +663,34 @@ export async function insertPartLedger(
export async function updateDefaultAccounts(
client: SupabaseClient<Database>,
defaultAccounts: z.infer<typeof defaultAcountValidator> & {
+ companyId: number;
updatedBy: string;
}
) {
- return client.from("accountDefault").update(defaultAccounts).eq("id", true);
+ return client
+ .from("accountDefault")
+ .update(defaultAccounts)
+ .eq("companyId", defaultAccounts.companyId);
}
export async function updateFiscalYearSettings(
client: SupabaseClient<Database>,
fiscalYearSettings: z.infer<typeof fiscalYearSettingsValidator> & {
+ companyId: number;
updatedBy: string;
}
) {
return client
.from("fiscalYearSettings")
.update(sanitize(fiscalYearSettings))
- .eq("id", true);
+ .eq("id", fiscalYearSettings.companyId);
}
export async function upsertAccount(
client: SupabaseClient<Database>,
account:
| (Omit<z.infer<typeof accountValidator>, "id"> & {
+ companyId: number;
createdBy: string;
customFields?: Json;
})
@@ -661,6 +715,7 @@ export async function upsertAccountCategory(
client: SupabaseClient<Database>,
accountCategory:
| (Omit<z.infer<typeof accountCategoryValidator>, "id"> & {
+ companyId: number;
createdBy: string;
customFields?: Json;
})
@@ -689,6 +744,7 @@ export async function upsertAccountSubcategory(
client: SupabaseClient<Database>,
accountSubcategory:
| (Omit<z.infer<typeof accountSubcategoryValidator>, "id"> & {
+ companyId: number;
createdBy: string;
customFields?: Json;
})
@@ -717,11 +773,13 @@ export async function upsertCurrency(
client: SupabaseClient<Database>,
currency:
| (Omit<z.infer<typeof currencyValidator>, "id"> & {
+ companyId: number;
createdBy: string;
customFields?: Json;
})
| (Omit<z.infer<typeof currencyValidator>, "id"> & {
id: string;
+ companyId: number;
updatedBy: string;
customFields?: Json;
})
@@ -730,7 +788,8 @@ export async function upsertCurrency(
await client
.from("currency")
.update({ isBaseCurrency: false })
- .eq("isBaseCurrency", true);
+ .eq("isBaseCurrency", true)
+ .eq("companyId", currency.companyId);
}
if ("createdBy" in currency) {
@@ -748,6 +807,7 @@ export async function upsertPaymentTerm(
client: SupabaseClient<Database>,
paymentTerm:
| (Omit<z.infer<typeof paymentTermValidator>, "id"> & {
+ companyId: number;
createdBy: string;
customFields?: Json;
})
diff --git a/apps/carbon/app/modules/settings/settings.service.ts b/apps/carbon/app/modules/settings/settings.service.ts
index 9e434574a4..598449b9ab 100644
--- a/apps/carbon/app/modules/settings/settings.service.ts
+++ b/apps/carbon/app/modules/settings/settings.service.ts
@@ -206,11 +206,13 @@ export async function rollbackNextSequence(
export async function seedCompany(
client: SupabaseClient<Database>,
- companyId: number
+ companyId: number,
+ userId: string
) {
return client.functions.invoke("seed-company", {
body: {
- id: companyId,
+ companyId,
+ userId,
},
});
}
diff --git a/apps/carbon/app/routes/api+/accounting.categories.ts b/apps/carbon/app/routes/api+/accounting.categories.ts
index 2d2cf9bd29..78f46122e7 100644
--- a/apps/carbon/app/routes/api+/accounting.categories.ts
+++ b/apps/carbon/app/routes/api+/accounting.categories.ts
@@ -4,7 +4,7 @@ import { getAccountCategoriesList } from "~/modules/accounting";
import { requirePermissions } from "~/services/auth/auth.server";
export async function loader({ request }: LoaderFunctionArgs) {
- const authorized = await requirePermissions(request, {});
+ const { client, companyId } = await requirePermissions(request, {});
- return json(await getAccountCategoriesList(authorized.client));
+ return json(await getAccountCategoriesList(client, companyId));
}
diff --git a/apps/carbon/app/routes/api+/accounting.currencies.ts b/apps/carbon/app/routes/api+/accounting.currencies.ts
index 2d42a9ca83..8bb9b73420 100644
--- a/apps/carbon/app/routes/api+/accounting.currencies.ts
+++ b/apps/carbon/app/routes/api+/accounting.currencies.ts
@@ -4,7 +4,7 @@ import { getCurrenciesList } from "~/modules/accounting";
import { requirePermissions } from "~/services/auth/auth.server";
export async function loader({ request }: LoaderFunctionArgs) {
- const authorized = await requirePermissions(request, {});
+ const { client, companyId } = await requirePermissions(request, {});
- return json(await getCurrenciesList(authorized.client));
+ return json(await getCurrenciesList(client, companyId));
}
diff --git a/apps/carbon/app/routes/onboarding+/company.tsx b/apps/carbon/app/routes/onboarding+/company.tsx
index 3733f0df09..0ee24fa968 100644
--- a/apps/carbon/app/routes/onboarding+/company.tsx
+++ b/apps/carbon/app/routes/onboarding+/company.tsx
@@ -113,7 +113,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Fatal: failed to get company ID");
}
- const seed = await seedCompany(supabaseClient, companyId);
+ const seed = await seedCompany(supabaseClient, companyId, userId);
if (seed.error) {
logger.error(seed.error);
throw new Error("Fatal: failed to seed company");
diff --git a/apps/carbon/app/routes/x+/accounting+/_layout.tsx b/apps/carbon/app/routes/x+/accounting+/_layout.tsx
index 45660f9d89..086db7b5ab 100644
--- a/apps/carbon/app/routes/x+/accounting+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/_layout.tsx
@@ -25,16 +25,15 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
});
- // the ABCs of accounting
const [accounts, baseCurrency] = await Promise.all([
- getAccountsList(client, {
+ getAccountsList(client, companyId, {
type: "Posting",
}),
- getBaseCurrency(client),
+ getBaseCurrency(client, companyId),
]);
if (accounts.error) {
diff --git a/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.new.tsx b/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.new.tsx
index afdf759735..cae711a95a 100644
--- a/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.new.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/categories.list.$categoryId.new.tsx
@@ -17,7 +17,7 @@ import { error } from "~/utils/result";
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
create: "accounting",
});
@@ -35,6 +35,7 @@ export async function action({ request }: ActionFunctionArgs) {
const createSubcategory = await upsertAccountSubcategory(client, {
...data,
+ companyId,
customFields: setCustomFields(formData),
createdBy: userId,
});
diff --git a/apps/carbon/app/routes/x+/accounting+/categories.new.tsx b/apps/carbon/app/routes/x+/accounting+/categories.new.tsx
index b011334d9a..b117192aab 100644
--- a/apps/carbon/app/routes/x+/accounting+/categories.new.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/categories.new.tsx
@@ -17,7 +17,7 @@ import { error } from "~/utils/result";
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
update: "accounting",
});
@@ -35,6 +35,7 @@ export async function action({ request }: ActionFunctionArgs) {
const createAccountCategory = await upsertAccountCategory(client, {
...data,
+ companyId,
customFields: setCustomFields(formData),
createdBy: userId,
});
diff --git a/apps/carbon/app/routes/x+/accounting+/categories.tsx b/apps/carbon/app/routes/x+/accounting+/categories.tsx
index ed96c53916..1fa1a2b74f 100644
--- a/apps/carbon/app/routes/x+/accounting+/categories.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/categories.tsx
@@ -19,7 +19,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
});
@@ -31,7 +31,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
const [categories] = await Promise.all([
- getAccountCategories(client, {
+ getAccountCategories(client, companyId, {
search,
limit,
diff --git a/apps/carbon/app/routes/x+/accounting+/charts.new.tsx b/apps/carbon/app/routes/x+/accounting+/charts.new.tsx
index 81d2cee3a4..a8d921d536 100644
--- a/apps/carbon/app/routes/x+/accounting+/charts.new.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/charts.new.tsx
@@ -29,7 +29,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
create: "accounting",
});
@@ -44,6 +44,7 @@ export async function action({ request }: ActionFunctionArgs) {
const insertAccount = await upsertAccount(client, {
...data,
+ companyId,
customFields: setCustomFields(formData),
createdBy: userId,
});
diff --git a/apps/carbon/app/routes/x+/accounting+/charts.tsx b/apps/carbon/app/routes/x+/accounting+/charts.tsx
index 0d5b3acf15..b0d296cd2d 100644
--- a/apps/carbon/app/routes/x+/accounting+/charts.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/charts.tsx
@@ -16,7 +16,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
});
@@ -28,7 +28,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const startDate = searchParams.get("startDate");
const endDate = searchParams.get("endDate");
- const chartOfAccounts = await getChartOfAccounts(client, {
+ const chartOfAccounts = await getChartOfAccounts(client, companyId, {
name,
incomeBalance,
startDate,
diff --git a/apps/carbon/app/routes/x+/accounting+/currencies.$currencyId.tsx b/apps/carbon/app/routes/x+/accounting+/currencies.$currencyId.tsx
index 9902efb541..3cd9fd6fb6 100644
--- a/apps/carbon/app/routes/x+/accounting+/currencies.$currencyId.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/currencies.$currencyId.tsx
@@ -34,7 +34,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
update: "accounting",
});
@@ -51,6 +51,7 @@ export async function action({ request }: ActionFunctionArgs) {
const updateCurrency = await upsertCurrency(client, {
id,
...data,
+ companyId,
customFields: setCustomFields(formData),
updatedBy: userId,
});
diff --git a/apps/carbon/app/routes/x+/accounting+/currencies.new.tsx b/apps/carbon/app/routes/x+/accounting+/currencies.new.tsx
index bb69f9687b..65cfd5b412 100644
--- a/apps/carbon/app/routes/x+/accounting+/currencies.new.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/currencies.new.tsx
@@ -23,7 +23,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
create: "accounting",
});
@@ -38,6 +38,7 @@ export async function action({ request }: ActionFunctionArgs) {
const insertCurrency = await upsertCurrency(client, {
...data,
+ companyId,
customFields: setCustomFields(formData),
createdBy: userId,
});
diff --git a/apps/carbon/app/routes/x+/accounting+/currencies.tsx b/apps/carbon/app/routes/x+/accounting+/currencies.tsx
index 61e9d94043..520051ad2b 100644
--- a/apps/carbon/app/routes/x+/accounting+/currencies.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/currencies.tsx
@@ -14,7 +14,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
});
@@ -26,7 +26,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
return json(
- await getCurrencies(client, { search, limit, offset, sorts, filters })
+ await getCurrencies(client, companyId, {
+ search,
+ limit,
+ offset,
+ sorts,
+ filters,
+ })
);
}
diff --git a/apps/carbon/app/routes/x+/accounting+/defaults.tsx b/apps/carbon/app/routes/x+/accounting+/defaults.tsx
index dc317844f2..fa3f7367a8 100644
--- a/apps/carbon/app/routes/x+/accounting+/defaults.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/defaults.tsx
@@ -24,11 +24,13 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
});
- const [defaultAccounts] = await Promise.all([getDefaultAccounts(client)]);
+ const [defaultAccounts] = await Promise.all([
+ getDefaultAccounts(client, companyId),
+ ]);
if (defaultAccounts.error || !defaultAccounts.data) {
throw redirect(
@@ -47,7 +49,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
create: "accounting",
});
@@ -61,6 +63,7 @@ export async function action({ request }: ActionFunctionArgs) {
const updateDefaults = await updateDefaultAccounts(client, {
...validation.data,
+ companyId,
updatedBy: userId,
});
if (updateDefaults.error) {
diff --git a/apps/carbon/app/routes/x+/accounting+/groups.inventory.tsx b/apps/carbon/app/routes/x+/accounting+/groups.inventory.tsx
index aea5f99163..2fd8ac8175 100644
--- a/apps/carbon/app/routes/x+/accounting+/groups.inventory.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/groups.inventory.tsx
@@ -34,7 +34,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
const [inventoryGroups, partGroups, locations] = await Promise.all([
- getInventoryPostingGroups(client, {
+ getInventoryPostingGroups(client, companyId, {
limit,
offset,
sorts,
diff --git a/apps/carbon/app/routes/x+/accounting+/groups.purchasing.tsx b/apps/carbon/app/routes/x+/accounting+/groups.purchasing.tsx
index f0e6a76a2c..f3e306c380 100644
--- a/apps/carbon/app/routes/x+/accounting+/groups.purchasing.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/groups.purchasing.tsx
@@ -33,7 +33,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
const [purchasingGroups, partGroups, supplierTypes] = await Promise.all([
- getPurchasingPostingGroups(client, {
+ getPurchasingPostingGroups(client, companyId, {
limit,
offset,
sorts,
diff --git a/apps/carbon/app/routes/x+/accounting+/groups.sales.tsx b/apps/carbon/app/routes/x+/accounting+/groups.sales.tsx
index 78e4fb01d5..96d6a4343b 100644
--- a/apps/carbon/app/routes/x+/accounting+/groups.sales.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/groups.sales.tsx
@@ -33,7 +33,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
const [salesGroups, partGroups, customerTypes] = await Promise.all([
- getSalesPostingGroups(client, {
+ getSalesPostingGroups(client, companyId, {
limit,
offset,
sorts,
diff --git a/apps/carbon/app/routes/x+/accounting+/payment-terms.new.tsx b/apps/carbon/app/routes/x+/accounting+/payment-terms.new.tsx
index 4b331fdf55..4e7cbf55d0 100644
--- a/apps/carbon/app/routes/x+/accounting+/payment-terms.new.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/payment-terms.new.tsx
@@ -24,7 +24,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
create: "accounting",
});
@@ -35,15 +35,11 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { name, daysDue, daysDiscount, discountPercentage, calculationMethod } =
- validation.data;
+ const { id, ...data } = validation.data;
const insertPaymentTerm = await upsertPaymentTerm(client, {
- name,
- daysDue,
- daysDiscount,
- discountPercentage,
- calculationMethod,
+ ...data,
+ companyId,
createdBy: userId,
customFields: setCustomFields(formData),
});
diff --git a/apps/carbon/app/routes/x+/accounting+/payment-terms.tsx b/apps/carbon/app/routes/x+/accounting+/payment-terms.tsx
index 05a58cb81b..39ae62a599 100644
--- a/apps/carbon/app/routes/x+/accounting+/payment-terms.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/payment-terms.tsx
@@ -14,7 +14,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
role: "employee",
});
@@ -26,7 +26,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
getGenericQueryFilters(searchParams);
return json(
- await getPaymentTerms(client, { search, limit, offset, sorts, filters })
+ await getPaymentTerms(client, companyId, {
+ search,
+ limit,
+ offset,
+ sorts,
+ filters,
+ })
);
}
diff --git a/apps/carbon/app/routes/x+/accounting+/years.tsx b/apps/carbon/app/routes/x+/accounting+/years.tsx
index 8c0621ba6c..7eac390e65 100644
--- a/apps/carbon/app/routes/x+/accounting+/years.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/years.tsx
@@ -22,11 +22,11 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "accounting",
});
- const settings = await getFiscalYearSettings(client);
+ const settings = await getFiscalYearSettings(client, companyId);
if (settings.error) {
throw redirect(
path.to.accounting,
@@ -42,7 +42,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
+ const { client, companyId, userId } = await requirePermissions(request, {
update: "accounting",
});
@@ -56,6 +56,7 @@ export async function action({ request }: ActionFunctionArgs) {
const update = await updateFiscalYearSettings(client, {
...validation.data,
+ companyId,
updatedBy: userId,
});
if (update.error) {
diff --git a/apps/carbon/app/routes/x+/customer+/_layout.tsx b/apps/carbon/app/routes/x+/customer+/_layout.tsx
index 574a62c6b8..04acbacabc 100644
--- a/apps/carbon/app/routes/x+/customer+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/customer+/_layout.tsx
@@ -23,7 +23,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "sales",
});
@@ -36,7 +36,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
] = await Promise.all([
getCustomerTypes(client),
getCustomerStatuses(client),
- getPaymentTermsList(client),
+ getPaymentTermsList(client, companyId),
getShippingMethodsList(client),
getShippingTermsList(client),
]);
diff --git a/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx b/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx
index 72e267aed7..01acc43fa4 100644
--- a/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx
+++ b/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx
@@ -17,7 +17,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "inventory",
role: "employee",
});
@@ -36,7 +36,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
sorts,
filters,
}),
- getAccountsList(client),
+ getAccountsList(client, companyId),
]);
if (shippingMethods.error) {
diff --git a/apps/carbon/app/routes/x+/part+/$partId.costing.tsx b/apps/carbon/app/routes/x+/part+/$partId.costing.tsx
index 0ff7020b9a..4bea631597 100644
--- a/apps/carbon/app/routes/x+/part+/$partId.costing.tsx
+++ b/apps/carbon/app/routes/x+/part+/$partId.costing.tsx
@@ -26,7 +26,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const [partCost, accounts] = await Promise.all([
getPartCost(client, partId, companyId),
- getAccountsList(client),
+ getAccountsList(client, companyId),
]);
if (partCost.error) {
diff --git a/apps/carbon/app/routes/x+/parts+/groups.tsx b/apps/carbon/app/routes/x+/parts+/groups.tsx
index 9aa0148c64..993f79e014 100644
--- a/apps/carbon/app/routes/x+/parts+/groups.tsx
+++ b/apps/carbon/app/routes/x+/parts+/groups.tsx
@@ -36,7 +36,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
search,
filters,
}),
- getAccountsList(client),
+ getAccountsList(client, companyId),
]);
if (partGroups.error) {
diff --git a/apps/carbon/app/routes/x+/purchase-invoice+/_layout.tsx b/apps/carbon/app/routes/x+/purchase-invoice+/_layout.tsx
index ea13dc941b..68cb59e793 100644
--- a/apps/carbon/app/routes/x+/purchase-invoice+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/purchase-invoice+/_layout.tsx
@@ -17,10 +17,12 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "invoicing",
});
- const [paymentTerms] = await Promise.all([getPaymentTermsList(client)]);
+ const [paymentTerms] = await Promise.all([
+ getPaymentTermsList(client, companyId),
+ ]);
return {
paymentTerms: paymentTerms.data ?? [],
diff --git a/apps/carbon/app/routes/x+/purchase-order+/$orderId.payment.tsx b/apps/carbon/app/routes/x+/purchase-order+/$orderId.payment.tsx
index a971282d36..527a33fcb0 100644
--- a/apps/carbon/app/routes/x+/purchase-order+/$orderId.payment.tsx
+++ b/apps/carbon/app/routes/x+/purchase-order+/$orderId.payment.tsx
@@ -17,7 +17,7 @@ import { path } from "~/utils/path";
import { error, success } from "~/utils/result";
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
});
@@ -26,7 +26,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const [purchaseOrderPayment, paymentTerms] = await Promise.all([
getPurchaseOrderPayment(client, orderId),
- getPaymentTermsList(client),
+ getPaymentTermsList(client, companyId),
]);
if (purchaseOrderPayment.error) {
diff --git a/apps/carbon/app/routes/x+/supplier+/_layout.tsx b/apps/carbon/app/routes/x+/supplier+/_layout.tsx
index 74434fd1e9..57cad9596d 100644
--- a/apps/carbon/app/routes/x+/supplier+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/_layout.tsx
@@ -23,7 +23,7 @@ export const handle: Handle = {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
});
@@ -36,7 +36,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
] = await Promise.all([
getSupplierTypes(client),
getSupplierStatuses(client),
- getPaymentTermsList(client),
+ getPaymentTermsList(client, companyId),
getShippingMethodsList(client),
getShippingTermsList(client),
]);
diff --git a/packages/database/src/seed.ts b/packages/database/src/seed.ts
index 867ad88d3f..815779735e 100644
--- a/packages/database/src/seed.ts
+++ b/packages/database/src/seed.ts
@@ -142,12 +142,6 @@ async function seed() {
if (insertEmployeeTypePermissions.error)
throw insertEmployeeTypePermissions.error;
- const insertEmployee = await supabaseAdmin
- .from("employee")
- .upsert([{ id, employeeTypeId: employeeTypes.Admin.id }]);
-
- if (insertEmployee.error) throw insertEmployee.error;
-
console.log(`Database has been seeded. 🌱\n`);
console.log(
`Admin user is 👇 \n🆔: ${id}\n📧: ${admin.email}\n🔑: ${admin.password}`
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index b3da6c6a93..01289fb757 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -4247,21 +4247,18 @@ export type Database = {
fiscalYearSettings: {
Row: {
companyId: number
- id: boolean
startMonth: Database["public"]["Enums"]["month"]
taxStartMonth: Database["public"]["Enums"]["month"]
updatedBy: string
}
Insert: {
companyId: number
- id?: boolean
startMonth?: Database["public"]["Enums"]["month"]
taxStartMonth?: Database["public"]["Enums"]["month"]
updatedBy: string
}
Update: {
companyId?: number
- id?: boolean
startMonth?: Database["public"]["Enums"]["month"]
taxStartMonth?: Database["public"]["Enums"]["month"]
updatedBy?: string
@@ -5120,6 +5117,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partCost_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partCost_partId_fkey"
columns: ["partId", "companyId"]
@@ -5393,6 +5397,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partInventory_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partInventory_partId_fkey"
columns: ["partId", "companyId"]
@@ -5525,6 +5536,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partLedger_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partLedger_partId_fkey"
columns: ["partId", "companyId"]
@@ -5806,6 +5824,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partPlanning_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partPlanning_partId_fkey"
columns: ["partId", "companyId"]
@@ -5947,6 +5972,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partReplenishment_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partReplenishment_partId_fkey"
columns: ["partId", "companyId"]
@@ -6121,6 +6153,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partSupplier_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partSupplier_partId_fkey"
columns: ["partId", "companyId"]
@@ -6289,6 +6328,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partUnitSalePrice_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partUnitSalePrice_partId_fkey"
columns: ["partId", "companyId"]
@@ -7601,6 +7647,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseInvoiceLines_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseInvoiceLines_partId_fkey"
columns: ["partId", "companyId"]
@@ -8491,6 +8544,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -9329,6 +9389,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteAssembly_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteAssembly_partId_fkey"
columns: ["partId", "companyId"]
@@ -9568,6 +9635,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -9868,6 +9942,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteMaterial_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteMaterial_partId_fkey"
columns: ["partId", "companyId"]
@@ -10486,6 +10567,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "receiptLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "receiptLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -10902,6 +10990,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "requestForQuoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "requestForQuoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -11711,6 +11806,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "salesOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "salesOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -16164,6 +16266,7 @@ export type Database = {
}
partQuantities: {
Row: {
+ companyId: number | null
locationId: string | null
partId: string | null
quantityAvailable: number | null
@@ -16172,7 +16275,15 @@ export type Database = {
quantityOnPurchaseOrder: number | null
quantityOnSalesOrder: number | null
}
- Relationships: []
+ Relationships: [
+ {
+ foreignKeyName: "part_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ ]
}
parts: {
Row: {
@@ -16663,6 +16774,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -17099,6 +17217,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -17795,6 +17920,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "salesOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "salesOrderLine_partId_fkey"
columns: ["partId", "companyId"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index b3da6c6a93..01289fb757 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -4247,21 +4247,18 @@ export type Database = {
fiscalYearSettings: {
Row: {
companyId: number
- id: boolean
startMonth: Database["public"]["Enums"]["month"]
taxStartMonth: Database["public"]["Enums"]["month"]
updatedBy: string
}
Insert: {
companyId: number
- id?: boolean
startMonth?: Database["public"]["Enums"]["month"]
taxStartMonth?: Database["public"]["Enums"]["month"]
updatedBy: string
}
Update: {
companyId?: number
- id?: boolean
startMonth?: Database["public"]["Enums"]["month"]
taxStartMonth?: Database["public"]["Enums"]["month"]
updatedBy?: string
@@ -5120,6 +5117,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partCost_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partCost_partId_fkey"
columns: ["partId", "companyId"]
@@ -5393,6 +5397,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partInventory_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partInventory_partId_fkey"
columns: ["partId", "companyId"]
@@ -5525,6 +5536,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partLedger_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partLedger_partId_fkey"
columns: ["partId", "companyId"]
@@ -5806,6 +5824,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partPlanning_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partPlanning_partId_fkey"
columns: ["partId", "companyId"]
@@ -5947,6 +5972,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partReplenishment_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partReplenishment_partId_fkey"
columns: ["partId", "companyId"]
@@ -6121,6 +6153,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partSupplier_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partSupplier_partId_fkey"
columns: ["partId", "companyId"]
@@ -6289,6 +6328,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "partUnitSalePrice_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "partUnitSalePrice_partId_fkey"
columns: ["partId", "companyId"]
@@ -7601,6 +7647,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseInvoiceLines_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseInvoiceLines_partId_fkey"
columns: ["partId", "companyId"]
@@ -8491,6 +8544,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -9329,6 +9389,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteAssembly_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteAssembly_partId_fkey"
columns: ["partId", "companyId"]
@@ -9568,6 +9635,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -9868,6 +9942,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteMaterial_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteMaterial_partId_fkey"
columns: ["partId", "companyId"]
@@ -10486,6 +10567,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "receiptLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "receiptLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -10902,6 +10990,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "requestForQuoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "requestForQuoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -11711,6 +11806,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "salesOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "salesOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -16164,6 +16266,7 @@ export type Database = {
}
partQuantities: {
Row: {
+ companyId: number | null
locationId: string | null
partId: string | null
quantityAvailable: number | null
@@ -16172,7 +16275,15 @@ export type Database = {
quantityOnPurchaseOrder: number | null
quantityOnSalesOrder: number | null
}
- Relationships: []
+ Relationships: [
+ {
+ foreignKeyName: "part_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ ]
}
parts: {
Row: {
@@ -16663,6 +16774,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "purchaseOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "purchaseOrderLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -17099,6 +17217,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "quoteLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "quoteLine_partId_fkey"
columns: ["partId", "companyId"]
@@ -17795,6 +17920,13 @@ export type Database = {
referencedRelation: "part"
referencedColumns: ["id", "companyId"]
},
+ {
+ foreignKeyName: "salesOrderLine_partId_fkey"
+ columns: ["partId", "companyId"]
+ isOneToOne: false
+ referencedRelation: "partQuantities"
+ referencedColumns: ["partId", "companyId"]
+ },
{
foreignKeyName: "salesOrderLine_partId_fkey"
columns: ["partId", "companyId"]
diff --git a/packages/database/supabase/functions/seed-company/index.ts b/packages/database/supabase/functions/seed-company/index.ts
index 3fbd03c34b..154dfc3b67 100644
--- a/packages/database/supabase/functions/seed-company/index.ts
+++ b/packages/database/supabase/functions/seed-company/index.ts
@@ -27,13 +27,27 @@ serve(async (req: Request) => {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}
- const { id } = await req.json();
+ const { companyId: id, userId } = await req.json();
try {
if (!id) throw new Error("Payload is missing id");
+ if (!userId) throw new Error("Payload is missing userId");
const companyId = Number(id);
await db.transaction().execute(async (trx) => {
+ // TODO: insert employee types and groups
+
+ // insert employee
+ await trx
+ .insertInto("employee")
+ .values([
+ {
+ id: String(userId),
+ companyId,
+ },
+ ])
+ .execute();
+
// supplier status
await trx
.insertInto("supplierStatus")
diff --git a/packages/database/supabase/migrations/20230705033432_ledgers.sql b/packages/database/supabase/migrations/20230705033432_ledgers.sql
index c083ae1b30..048a9190e2 100644
--- a/packages/database/supabase/migrations/20230705033432_ledgers.sql
+++ b/packages/database/supabase/migrations/20230705033432_ledgers.sql
@@ -14,13 +14,12 @@ CREATE TYPE "month" AS ENUM (
);
CREATE TABLE "fiscalYearSettings" (
- "id" BOOLEAN NOT NULL DEFAULT TRUE,
+ "companyId" INTEGER NOT NULL,
"startMonth" "month" NOT NULL DEFAULT 'January',
"taxStartMonth" "month" NOT NULL DEFAULT 'January',
- "companyId" INTEGER NOT NULL,
"updatedBy" TEXT NOT NULL,
- CONSTRAINT "fiscalYearSettings_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "fiscalYearSettings_pkey" PRIMARY KEY ("companyId"),
CONSTRAINT "fiscalYearSettings_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "fiscalYearSettings_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT,
CONSTRAINT "fiscalYear_id_unique" UNIQUE ("companyId")
diff --git a/packages/database/supabase/migrations/20230915193213_quantity-on-hand.sql b/packages/database/supabase/migrations/20230915193213_quantity-on-hand.sql
index 89ef7f540a..d46fcda3ca 100644
--- a/packages/database/supabase/migrations/20230915193213_quantity-on-hand.sql
+++ b/packages/database/supabase/migrations/20230915193213_quantity-on-hand.sql
@@ -1,6 +1,7 @@
CREATE OR REPLACE VIEW "partQuantities" AS
SELECT
p."id" AS "partId",
+ p."companyId",
loc."id" AS "locationId",
COALESCE(SUM(pl."quantity"), 0) AS "quantityOnHand",
COALESCE(pol."quantityToReceive", 0) AS "quantityOnPurchaseOrder",
@@ -27,6 +28,7 @@ CREATE OR REPLACE VIEW "partQuantities" AS
pol."locationId"
) pol ON pol."partId" = p."id" AND pol."locationId" = loc."id"
GROUP BY
- p."id",
+ p."id",
+ p."companyId",
loc."id",
pol."quantityToReceive"
\ No newline at end of file