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.

fix: move pricing to settings submodule

Brad Barbin2a256a78887e
diff --git a/apps/erp/app/routes/x+/settings+/company.tsx b/apps/erp/app/routes/x+/settings+/company.tsx
index 3f02ccf692..067d1dc512 100644
--- a/apps/erp/app/routes/x+/settings+/company.tsx
+++ b/apps/erp/app/routes/x+/settings+/company.tsx
@@ -1,34 +1,20 @@
import { assertIsPost, error, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
+import { validationError, validator } from "@carbon/form";
import {
- SelectControlled,
- ValidatedForm,
- validationError,
- validator,
-} from "@carbon/form";
-import {
- Button,
Card,
CardContent,
CardDescription,
- CardFooter,
CardHeader,
CardTitle,
Heading,
ScrollArea,
- Status,
VStack,
} from "@carbon/react";
-import { useEdition } from "@carbon/remix";
-import { Edition } from "@carbon/utils";
-import { Form, useLoaderData } from "@remix-run/react";
-import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
+import type { ActionFunctionArgs } from "@vercel/remix";
import { json } from "@vercel/remix";
-import { useState } from "react";
-import { z } from "zod";
-import { Submit } from "~/components/Form";
-import { usePermissions, useRouteData, useUser } from "~/hooks";
+import { useRouteData } from "~/hooks";
import type { Company as CompanyType } from "~/modules/settings";
import {
CompanyForm,
@@ -43,115 +29,13 @@ export const handle: Handle = {
to: path.to.company,
};
-const transferOwnershipValidator = z.object({
- intent: z.literal("transfer-ownership"),
- newOwnerId: z.string().min(1, { message: "New owner is required" }),
-});
-
-export async function loader({ request }: LoaderFunctionArgs) {
- const { client, companyId } = await requirePermissions(request, {
- view: "settings",
- });
-
- // Get company plan and usage data for payment section
- const companyPlan = await client
- .from("companyPlan")
- .select(
- `
- *,
- plan:planId (
- name,
- userBasedPricing,
- tasksLimit,
- aiTokensLimit
- )
- `
- )
- .eq("id", companyId)
- .single();
-
- const companyUsage = await client
- .from("companyUsage")
- .select("*")
- .eq("companyId", companyId)
- .single();
-
- const userToCompany = await client
- .from("userToCompany")
- .select("userId")
- .eq("companyId", companyId)
- .eq("role", "employee");
-
- const userIds = userToCompany.data?.map((utc) => utc.userId) || [];
-
- const employees =
- userIds.length > 0
- ? await client
- .from("user")
- .select(
- `
- id,
- firstName,
- lastName,
- fullName,
- email
- `
- )
- .in("id", userIds)
- : { data: [], error: null };
-
- return json({
- plan: companyPlan.data,
- usage: companyUsage.data,
- employees: employees.data || [],
- });
-}
-
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
const { client, companyId, userId } = await requirePermissions(request, {
update: "settings",
});
const formData = await request.formData();
- const intent = formData.get("intent");
-
- if (intent === "transfer-ownership") {
- const validation = await validator(transferOwnershipValidator).validate(
- formData
- );
- if (validation.error) {
- return validationError(validation.error);
- }
- const { newOwnerId } = validation.data;
-
- try {
- const updateResult = await client
- .from("company")
- .update({ ownerId: newOwnerId })
- .eq("id", companyId);
-
- if (updateResult.error) {
- throw new Error(updateResult.error.message);
- }
-
- return json(
- {},
- await flash(
- request,
- success("Company ownership has been transferred successfully")
- )
- );
- } catch (err) {
- console.error("Failed to transfer ownership:", err);
- return json(
- {},
- await flash(request, error("Failed to transfer ownership"))
- );
- }
- }
-
- // Handle company form updates
const validation = await validator(companyValidator).validate(formData);
if (validation.error) {
@@ -172,12 +56,6 @@ export async function action({ request }: ActionFunctionArgs) {
}
export default function Company() {
- const { plan, usage, employees } = useLoaderData<typeof loader>();
- const { isOwner } = usePermissions();
- const { id: userId } = useUser();
- const edition = useEdition();
- const [ownerId, setOwnerId] = useState<string | null>(userId);
-
const routeData = useRouteData<{ company: CompanyType }>(
path.to.authenticatedRoot
);
@@ -219,139 +97,7 @@ export default function Company() {
<CompanyForm company={initialValues} />
</CardContent>
</Card>
-
- {edition === Edition.Cloud && isOwner() && (
- <>
- <Card>
- <CardHeader>
- <CardTitle>Manage Subscription</CardTitle>
- <CardDescription>
- Manage your subscription and billing information
- </CardDescription>
- </CardHeader>
- <CardContent>
- <VStack spacing={4}>
- <div className="grid grid-cols-2 gap-4 w-full">
- <div>
- <h4 className="font-medium">Plan</h4>
- <Status color="blue">
- {plan?.plan?.name || "No active plan"}
- </Status>
- </div>
- <div>
- <h4 className="font-medium">Status</h4>
-
- <SubscriptionStatus
- status={plan?.stripeSubscriptionStatus || "Unknown"}
- />
- </div>
- </div>
-
- {usage && (
- <div className="grid grid-cols-3 gap-4 pt-4 border-t">
- <div>
- <h4 className="font-medium">Users</h4>
- <p className="text-sm text-muted-foreground">
- {usage.users} / {plan?.usersLimit || "∞"}
- </p>
- </div>
- <div>
- <h4 className="font-medium">Tasks</h4>
- <p className="text-sm text-muted-foreground">
- {usage.tasks.toLocaleString()} /{" "}
- {plan?.tasksLimit?.toLocaleString() || "∞"}
- </p>
- </div>
- <div>
- <h4 className="font-medium">AI Tokens</h4>
- <p className="text-sm text-muted-foreground">
- {usage.aiTokens.toLocaleString()} /{" "}
- {plan?.aiTokensLimit?.toLocaleString() || "∞"}
- </p>
- </div>
- </div>
- )}
- </VStack>
- </CardContent>
- <CardFooter>
- <Form method="post" action={path.to.settingsPayment}>
- <input type="hidden" name="intent" value="billing-portal" />
- <Button type="submit">Manage Subscription</Button>
- </Form>
- </CardFooter>
- </Card>
-
- <ValidatedForm validator={transferOwnershipValidator} method="post">
- <Card>
- <CardHeader>
- <CardTitle>Manage Ownership</CardTitle>
- <CardDescription>
- Transfer ownership of this company to another user
- </CardDescription>
- </CardHeader>
- <CardContent>
- <VStack spacing={4}>
- <p className="text-sm text-muted-foreground">
- As the company owner, you can transfer ownership to
- another employee. This will give them full access to
- billing and administrative settings.
- </p>
- {employees.length > 0 ? (
- <>
- <input
- type="hidden"
- name="intent"
- value="transfer-ownership"
- />
- <div className="grid grid-cols-2 gap-4 w-full">
- <SelectControlled
- name="newOwnerId"
- label="New Owner"
- placeholder="Select a new owner"
- value={ownerId || undefined}
- onChange={(value) => {
- if (value?.value) {
- setOwnerId(value.value);
- }
- }}
- options={employees.map((employee) => ({
- label: employee.fullName || employee.email,
- value: employee.id,
- }))}
- />
- </div>
- </>
- ) : (
- <p className="text-sm text-muted-foreground">
- No other employees found. Add employees to enable
- ownership transfer.
- </p>
- )}
- </VStack>
- </CardContent>
- <CardFooter>
- <Submit withBlocker={false} isDisabled={ownerId === userId}>
- Transfer Ownership
- </Submit>
- </CardFooter>
- </Card>
- </ValidatedForm>
- </>
- )}
</VStack>
</ScrollArea>
);
}
-
-function SubscriptionStatus({ status }: { status: string }) {
- switch (status) {
- case "Active":
- return <Status color="green">Active</Status>;
- case "Inactive":
- return <Status color="orange">Inactive</Status>;
- case "Cancelled":
- return <Status color="red">Cancelled</Status>;
- default:
- return <Status color="gray">Unknown</Status>;
- }
-}
diff --git a/apps/erp/app/routes/x+/settings+/payment.tsx b/apps/erp/app/routes/x+/settings+/payment.tsx
index a8dc5b5deb..3633a51d8d 100644
--- a/apps/erp/app/routes/x+/settings+/payment.tsx
+++ b/apps/erp/app/routes/x+/settings+/payment.tsx
@@ -1,9 +1,35 @@
-import { assertIsPost, error } from "@carbon/auth";
+import { assertIsPost, error, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
+import {
+ SelectControlled,
+ Submit,
+ ValidatedForm,
+ validationError,
+ validator,
+} from "@carbon/form";
+import {
+ Button,
+ Card,
+ CardContent,
+ CardDescription,
+ CardFooter,
+ CardHeader,
+ CardTitle,
+ Heading,
+ ScrollArea,
+ Status,
+ VStack,
+} from "@carbon/react";
+import { useEdition } from "@carbon/remix";
import { getBillingPortalRedirectUrl } from "@carbon/stripe/stripe.server";
-import type { ActionFunctionArgs } from "@vercel/remix";
+import { Edition } from "@carbon/utils";
+import { Form, useLoaderData } from "@remix-run/react";
+import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
import { json, redirect } from "@vercel/remix";
+import { useState } from "react";
+import z from "zod";
+import { usePermissions, useUser } from "~/hooks";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
@@ -12,9 +38,73 @@ export const handle: Handle = {
to: path.to.settingsPayment,
};
+const transferOwnershipValidator = z.object({
+ intent: z.literal("transfer-ownership"),
+ newOwnerId: z.string().min(1, { message: "New owner is required" }),
+});
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "settings",
+ });
+
+ // Get company plan and usage data for payment section
+ const companyPlan = await client
+ .from("companyPlan")
+ .select(
+ `
+ *,
+ plan:planId (
+ name,
+ userBasedPricing,
+ tasksLimit,
+ aiTokensLimit
+ )
+ `
+ )
+ .eq("id", companyId)
+ .single();
+
+ const companyUsage = await client
+ .from("companyUsage")
+ .select("*")
+ .eq("companyId", companyId)
+ .single();
+
+ const userToCompany = await client
+ .from("userToCompany")
+ .select("userId")
+ .eq("companyId", companyId)
+ .eq("role", "employee");
+
+ const userIds = userToCompany.data?.map((utc) => utc.userId) || [];
+
+ const employees =
+ userIds.length > 0
+ ? await client
+ .from("user")
+ .select(
+ `
+ id,
+ firstName,
+ lastName,
+ fullName,
+ email
+ `
+ )
+ .in("id", userIds)
+ : { data: [], error: null };
+
+ return json({
+ plan: companyPlan.data,
+ usage: companyUsage.data,
+ employees: employees.data || [],
+ });
+}
+
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { companyId } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
update: "settings",
});
@@ -34,10 +124,192 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
+ if (intent === "transfer-ownership") {
+ const validation = await validator(transferOwnershipValidator).validate(
+ formData
+ );
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const { newOwnerId } = validation.data;
+
+ try {
+ const updateResult = await client
+ .from("company")
+ .update({ ownerId: newOwnerId })
+ .eq("id", companyId);
+
+ if (updateResult.error) {
+ throw new Error(updateResult.error.message);
+ }
+
+ return json(
+ {},
+ await flash(
+ request,
+ success("Company ownership has been transferred successfully")
+ )
+ );
+ } catch (err) {
+ console.error("Failed to transfer ownership:", err);
+ return json(
+ {},
+ await flash(request, error("Failed to transfer ownership"))
+ );
+ }
+ }
+
return json({}, await flash(request, error("Invalid intent")));
}
// This route now only handles actions - UI is in the company route
export default function PaymentSettings() {
- return null;
+ const { plan, usage, employees } = useLoaderData<typeof loader>();
+ const { isOwner } = usePermissions();
+ const { id: userId } = useUser();
+ const edition = useEdition();
+ const [ownerId, setOwnerId] = useState<string | null>(userId);
+
+ return (
+ <ScrollArea className="w-full h-[calc(100dvh-49px)]">
+ <VStack
+ spacing={4}
+ className="py-12 px-4 max-w-[60rem] h-full mx-auto gap-4"
+ >
+ <Heading size="h3">Payment</Heading>
+ {edition === Edition.Cloud && isOwner() && (
+ <>
+ <Card>
+ <CardHeader>
+ <CardTitle>Manage Subscription</CardTitle>
+ <CardDescription>
+ Manage your subscription and billing information
+ </CardDescription>
+ </CardHeader>
+ <CardContent>
+ <VStack spacing={4}>
+ <div className="grid grid-cols-2 gap-4 w-full">
+ <div>
+ <h4 className="font-medium">Plan</h4>
+ <Status color="blue">
+ {plan?.plan?.name || "No active plan"}
+ </Status>
+ </div>
+ <div>
+ <h4 className="font-medium">Status</h4>
+
+ <SubscriptionStatus
+ status={plan?.stripeSubscriptionStatus || "Unknown"}
+ />
+ </div>
+ </div>
+
+ {usage && (
+ <div className="grid grid-cols-3 gap-4 pt-4 border-t">
+ <div>
+ <h4 className="font-medium">Users</h4>
+ <p className="text-sm text-muted-foreground">
+ {usage.users} / {plan?.usersLimit || "∞"}
+ </p>
+ </div>
+ <div>
+ <h4 className="font-medium">Tasks</h4>
+ <p className="text-sm text-muted-foreground">
+ {usage.tasks.toLocaleString()} /{" "}
+ {plan?.tasksLimit?.toLocaleString() || "∞"}
+ </p>
+ </div>
+ <div>
+ <h4 className="font-medium">AI Tokens</h4>
+ <p className="text-sm text-muted-foreground">
+ {usage.aiTokens.toLocaleString()} /{" "}
+ {plan?.aiTokensLimit?.toLocaleString() || "∞"}
+ </p>
+ </div>
+ </div>
+ )}
+ </VStack>
+ </CardContent>
+ <CardFooter>
+ <Form method="post" action={path.to.settingsPayment}>
+ <input type="hidden" name="intent" value="billing-portal" />
+ <Button type="submit">Manage Subscription</Button>
+ </Form>
+ </CardFooter>
+ </Card>
+
+ <ValidatedForm validator={transferOwnershipValidator} method="post">
+ <Card>
+ <CardHeader>
+ <CardTitle>Manage Ownership</CardTitle>
+ <CardDescription>
+ Transfer ownership of this company to another user
+ </CardDescription>
+ </CardHeader>
+ <CardContent>
+ <VStack spacing={4}>
+ <p className="text-sm text-muted-foreground">
+ As the company owner, you can transfer ownership to
+ another employee. This will give them full access to
+ billing and administrative settings.
+ </p>
+ {employees.length > 0 ? (
+ <>
+ <input
+ type="hidden"
+ name="intent"
+ value="transfer-ownership"
+ />
+ <div className="grid grid-cols-2 gap-4 w-full">
+ <SelectControlled
+ name="newOwnerId"
+ label="New Owner"
+ placeholder="Select a new owner"
+ value={ownerId || undefined}
+ onChange={(value) => {
+ if (value?.value) {
+ setOwnerId(value.value);
+ }
+ }}
+ options={employees.map((employee) => ({
+ label: employee.fullName || employee.email,
+ value: employee.id,
+ }))}
+ />
+ </div>
+ </>
+ ) : (
+ <p className="text-sm text-muted-foreground">
+ No other employees found. Add employees to enable
+ ownership transfer.
+ </p>
+ )}
+ </VStack>
+ </CardContent>
+ <CardFooter>
+ <Submit withBlocker={false} isDisabled={ownerId === userId}>
+ Transfer Ownership
+ </Submit>
+ </CardFooter>
+ </Card>
+ </ValidatedForm>
+ </>
+ )}
+ </VStack>
+ </ScrollArea>
+ );
+}
+
+function SubscriptionStatus({ status }: { status: string }) {
+ switch (status) {
+ case "Active":
+ return <Status color="green">Active</Status>;
+ case "Inactive":
+ return <Status color="orange">Inactive</Status>;
+ case "Cancelled":
+ return <Status color="red">Cancelled</Status>;
+ default:
+ return <Status color="gray">Unknown</Status>;
+ }
}