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.

Adds async Xero sync for approved and finalized POs

Sidwebworks035e233a442f
diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId.finalize.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId.finalize.tsx
index c209396c66..ed046cb324 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId.finalize.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId.finalize.tsx
@@ -7,10 +7,8 @@ import {
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { PurchaseOrderEmail } from "@carbon/documents/email";
-import { ProviderID } from "@carbon/ee/accounting";
import { validationError, validator } from "@carbon/form";
-import type { sendEmailResendTask } from "@carbon/jobs/trigger/send-email-resend"; // Assuming you have a sendEmail task defined
-import type { syncExternalAccountingTask } from "@carbon/jobs/trigger/sync-external-accounting";
+import type { sendEmailResendTask } from "@carbon/jobs/trigger/send-email-resend";
import { NotificationEvent } from "@carbon/notifications";
import { renderAsync } from "@react-email/components";
import { FunctionRegion } from "@supabase/supabase-js";
@@ -345,26 +343,6 @@ export async function action(args: ActionFunctionArgs) {
throw new Error("Invalid notification type");
}
- // Trigger Xero sync for the finalized purchase order
- // This runs asynchronously - failures won't block the finalization
- try {
- await tasks.trigger<typeof syncExternalAccountingTask>(
- "sync-external-accounting",
- {
- companyId,
- provider: ProviderID.XERO,
- syncType: "trigger",
- syncDirection: "push-to-accounting",
- entities: [
- { entityType: "purchaseOrder", entityId: orderId, operation: "sync" }
- ]
- }
- );
- } catch (syncError) {
- // Log but don't fail - Xero sync is optional and may not be configured
- console.error("Failed to trigger Xero sync for purchase order:", syncError);
- }
-
throw redirect(
requestReferrer(request) ?? path.to.purchaseOrder(orderId),
await flash(request, success("Purchase order finalized"))
diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
index 16ceb9b5ce..3665097e94 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId.tsx
@@ -7,10 +7,8 @@ import {
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { PurchaseOrderEmail } from "@carbon/documents/email";
-import { ProviderID } from "@carbon/ee/accounting";
import { validationError, validator } from "@carbon/form";
import type { sendEmailResendTask } from "@carbon/jobs/trigger/send-email-resend";
-import type { syncExternalAccountingTask } from "@carbon/jobs/trigger/sync-external-accounting";
import { NotificationEvent } from "@carbon/notifications";
import { VStack } from "@carbon/react";
import { renderAsync } from "@react-email/components";
@@ -310,33 +308,6 @@ export async function action(args: ActionFunctionArgs) {
);
}
}
-
- // Trigger Xero sync for the approved purchase order
- // This runs asynchronously - failures won't block the approval
- try {
- await tasks.trigger<typeof syncExternalAccountingTask>(
- "sync-external-accounting",
- {
- companyId,
- provider: ProviderID.XERO,
- syncType: "trigger",
- syncDirection: "push-to-accounting",
- entities: [
- {
- entityType: "purchaseOrder",
- entityId: orderId,
- operation: "sync"
- }
- ]
- }
- );
- } catch (syncError) {
- // Log but don't fail - Xero sync is optional and may not be configured
- console.error(
- "Failed to trigger Xero sync for purchase order:",
- syncError
- );
- }
}
}
diff --git a/packages/ee/src/accounting/providers/xero/entities/purchase-order.ts b/packages/ee/src/accounting/providers/xero/entities/purchase-order.ts
index 9c6e4c1dde..86ce3cd09c 100644
--- a/packages/ee/src/accounting/providers/xero/entities/purchase-order.ts
+++ b/packages/ee/src/accounting/providers/xero/entities/purchase-order.ts
@@ -646,12 +646,15 @@ export class PurchaseOrderSyncer extends BaseEntitySyncer<
context: ShouldSyncContext<Accounting.PurchaseOrder, Xero.PurchaseOrder>
): boolean | string {
if (context.direction === "push" && context.localEntity) {
- const nonSyncableStatuses: Accounting.PurchaseOrder["status"][] = [
- "Draft",
- "Planned"
+ // Only sync POs in locked statuses (finalized and receiving/invoicing)
+ const syncableStatuses: Accounting.PurchaseOrder["status"][] = [
+ "To Receive",
+ "To Receive and Invoice",
+ "To Invoice",
+ "Completed"
];
- if (nonSyncableStatuses.includes(context.localEntity.status)) {
- return `Purchase order must be past Draft/Planned before syncing (current status: ${context.localEntity.status})`;
+ if (!syncableStatuses.includes(context.localEntity.status)) {
+ return `Purchase order must be in a locked status to sync (current: ${context.localEntity.status})`;
}
}
diff --git a/packages/ee/src/xero/config.tsx b/packages/ee/src/xero/config.tsx
index e7fd36cbaf..e41a762018 100644
--- a/packages/ee/src/xero/config.tsx
+++ b/packages/ee/src/xero/config.tsx
@@ -246,7 +246,9 @@ export const Xero = defineIntegration({
"supplier",
"item",
"salesInvoice",
- "purchaseInvoice"
+ "purchaseInvoice",
+ "purchaseOrder",
+ "salesOrder"
];
for (const table of tables) {