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.

feat: add tax percent to purchase order and purchase invoice forms

Brad Barbin1af8d0436686
diff --git a/apps/erp/app/components/Gantt/Gantt.tsx b/apps/erp/app/components/Gantt/Gantt.tsx
index f54e0df2f4..25d0df07ec 100644
--- a/apps/erp/app/components/Gantt/Gantt.tsx
+++ b/apps/erp/app/components/Gantt/Gantt.tsx
@@ -30,7 +30,7 @@ import { useEffect, useRef, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";
import {
LuChevronDown,
- LuChevronUp,
+ LuChevronRight,
LuSearch,
LuZoomIn,
LuZoomOut,
@@ -212,7 +212,7 @@ const Gantt = ({
state.expanded ? (
<LuChevronDown className="h-4 w-4 text-gray-400" />
) : (
- <LuChevronUp className="h-4 w-4 text-gray-400" />
+ <LuChevronRight className="h-4 w-4 text-gray-400" />
)
) : (
<div className="h-8 w-4" />
diff --git a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
index d4bd60341a..748e39fbc6 100644
--- a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
+++ b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
@@ -17,7 +17,7 @@ import {
import { useCarbon } from "@carbon/auth";
import { ValidatedForm } from "@carbon/form";
import { useParams } from "@remix-run/react";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import type { z } from "zod";
import {
ConversionFactor,
@@ -25,7 +25,6 @@ import {
Hidden,
Item,
Location,
- Number,
NumberControlled,
Shelf,
Submit,
@@ -38,7 +37,9 @@ import type { MethodItemType } from "~/modules/shared";
import { path } from "~/utils/path";
type PurchaseInvoiceLineFormProps = {
- initialValues: z.infer<typeof purchaseInvoiceLineValidator>;
+ initialValues: z.infer<typeof purchaseInvoiceLineValidator> & {
+ taxPercent?: number;
+ };
type?: "card" | "modal";
onClose?: () => void;
};
@@ -74,24 +75,48 @@ const PurchaseInvoiceLineForm = ({
description: string;
quantity: number;
supplierUnitPrice: number;
+ supplierShippingCost: number;
purchaseUom: string;
inventoryUom: string;
conversionFactor: number;
shelfId: string | null;
minimumOrderQuantity?: number;
+ taxAmount: number;
+ taxPercent: number;
}>({
itemId: initialValues.itemId ?? "",
itemReadableId: initialValues.itemReadableId ?? "",
description: initialValues.description ?? "",
quantity: initialValues.quantity ?? 1,
supplierUnitPrice: initialValues.supplierUnitPrice ?? 0,
+ supplierShippingCost: initialValues.supplierShippingCost ?? 0,
purchaseUom: initialValues.purchaseUnitOfMeasureCode ?? "",
inventoryUom: initialValues.inventoryUnitOfMeasureCode ?? "",
conversionFactor: initialValues.conversionFactor ?? 1,
shelfId: initialValues.shelfId ?? "",
minimumOrderQuantity: undefined,
+ taxAmount: initialValues.supplierTaxAmount ?? 0,
+ taxPercent: initialValues.taxPercent ?? 0,
});
+ // update tax amount when quantity or unit price changes
+ useEffect(() => {
+ const subtotal =
+ itemData.supplierUnitPrice * itemData.quantity +
+ itemData.supplierShippingCost;
+ if (itemData.taxPercent !== 0) {
+ setItemData((d) => ({
+ ...d,
+ taxAmount: subtotal * itemData.taxPercent,
+ }));
+ }
+ }, [
+ itemData.supplierUnitPrice,
+ itemData.quantity,
+ itemData.supplierShippingCost,
+ itemData.taxPercent,
+ ]);
+
const isEditing = initialValues.id !== undefined;
const isDisabled = !isEditable
? true
@@ -108,11 +133,14 @@ const PurchaseInvoiceLineForm = ({
description: "",
quantity: 1,
supplierUnitPrice: 0,
+ supplierShippingCost: 0,
inventoryUom: "",
purchaseUom: "",
conversionFactor: 1,
shelfId: "",
minimumOrderQuantity: undefined,
+ taxAmount: 0,
+ taxPercent: 0,
});
};
@@ -161,6 +189,7 @@ const PurchaseInvoiceLineForm = ({
supplierUnitPrice:
(supplierPart?.data?.unitPrice ?? itemCost?.unitCost ?? 0) /
(routeData?.purchaseInvoice?.exchangeRate ?? 1),
+ supplierShippingCost: 0,
purchaseUom:
supplierPart?.data?.supplierUnitOfMeasureCode ??
itemReplenishment?.purchasingUnitOfMeasureCode ??
@@ -172,6 +201,8 @@ const PurchaseInvoiceLineForm = ({
itemReplenishment?.conversionFactor ??
1,
shelfId: inventory.data?.defaultShelfId ?? null,
+ taxAmount: 0,
+ taxPercent: 0,
});
if (item.data?.type) {
@@ -257,6 +288,7 @@ const PurchaseInvoiceLineForm = ({
name="inventoryUnitOfMeasureCode"
value={itemData?.inventoryUom}
/>
+
<VStack>
<div className="grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3">
<Item
@@ -271,7 +303,7 @@ const PurchaseInvoiceLineForm = ({
onTypeChange={onTypeChange}
/>
- <FormControl>
+ <FormControl className="col-span-2">
<FormLabel>Description</FormLabel>
<Input
value={itemData.description}
@@ -344,25 +376,44 @@ const PurchaseInvoiceLineForm = ({
}))
}
/>
- <Number
+ <NumberControlled
name="supplierShippingCost"
label="Shipping"
+ value={itemData.supplierShippingCost}
formatOptions={{
style: "currency",
currency:
routeData?.purchaseInvoice?.currencyCode ??
company.baseCurrencyCode,
}}
+ onChange={(value) =>
+ setItemData((d) => ({
+ ...d,
+ supplierShippingCost: value,
+ }))
+ }
/>
- <Number
+
+ <NumberControlled
name="supplierTaxAmount"
label="Tax"
+ value={itemData.taxAmount}
formatOptions={{
style: "currency",
currency:
routeData?.purchaseInvoice?.currencyCode ??
company.baseCurrencyCode,
}}
+ onChange={(value) => {
+ const subtotal =
+ itemData.supplierUnitPrice * itemData.quantity +
+ itemData.supplierShippingCost;
+ setItemData((d) => ({
+ ...d,
+ taxAmount: value,
+ taxPercent: subtotal > 0 ? value / subtotal : 0,
+ }));
+ }}
/>
<Location
@@ -387,6 +438,29 @@ const PurchaseInvoiceLineForm = ({
/>
</>
)}
+ <NumberControlled
+ name="taxPercent"
+ label="Tax Percent"
+ value={itemData.taxPercent}
+ minValue={0}
+ maxValue={1}
+ step={0.0001}
+ formatOptions={{
+ style: "percent",
+ minimumFractionDigits: 0,
+ maximumFractionDigits: 2,
+ }}
+ onChange={(value) => {
+ const subtotal =
+ itemData.supplierUnitPrice * itemData.quantity +
+ itemData.supplierShippingCost;
+ setItemData((d) => ({
+ ...d,
+ taxPercent: value,
+ taxAmount: subtotal * value,
+ }));
+ }}
+ />
<CustomFormFields table="purchaseInvoiceLine" />
</div>
</VStack>
diff --git a/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx b/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx
index 72c494a866..1e3e98db38 100644
--- a/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx
+++ b/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx
@@ -13,7 +13,7 @@ import {
} from "@carbon/react";
import { useNavigate, useParams } from "@remix-run/react";
import { useRef, useState } from "react";
-import { LuChevronDown, LuChevronUp, LuSearch } from "react-icons/lu";
+import { LuChevronDown, LuChevronRight, LuSearch } from "react-icons/lu";
import { Copy, MethodIcon, MethodItemTypeIcon } from "~/components";
import type { FlatTreeItem } from "~/components/TreeView";
import { LevelLine, TreeView, useTree } from "~/components/TreeView";
@@ -157,7 +157,7 @@ const BoMExplorer = ({ itemType, methods, selectedId }: BoMExplorerProps) => {
state.expanded ? (
<LuChevronDown className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
) : (
- <LuChevronUp className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
+ <LuChevronRight className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
)
) : (
<div className="h-8 w-4" />
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
index 1d5131445b..319fb04c3d 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
@@ -16,7 +16,7 @@ import {
} from "@carbon/react";
import { useFetchers, useNavigate } from "@remix-run/react";
import { useRef, useState } from "react";
-import { LuChevronDown, LuChevronUp, LuSearch } from "react-icons/lu";
+import { LuChevronDown, LuChevronRight, LuSearch } from "react-icons/lu";
import { Copy, MethodIcon, MethodItemTypeIcon } from "~/components";
import type { FlatTree, FlatTreeItem } from "~/components/TreeView";
import { LevelLine, TreeView, useTree } from "~/components/TreeView";
@@ -151,7 +151,7 @@ const JobBoMExplorer = ({ method }: JobBoMExplorerProps) => {
state.expanded ? (
<LuChevronDown className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
) : (
- <LuChevronUp className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
+ <LuChevronRight className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
)
) : (
<div className="h-8 w-4" />
diff --git a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
index 958c5bd658..9a85248d4f 100644
--- a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
+++ b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
@@ -16,9 +16,9 @@ import {
} from "@carbon/react";
import { useCarbon } from "@carbon/auth";
-import { Number, ValidatedForm } from "@carbon/form";
+import { ValidatedForm } from "@carbon/form";
import { useFetcher, useParams } from "@remix-run/react";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import type { z } from "zod";
import {
ConversionFactor,
@@ -75,24 +75,57 @@ const PurchaseOrderLineForm = ({
description: string;
purchaseQuantity: number;
supplierUnitPrice: number;
+ supplierShippingCost: number;
purchaseUom: string;
inventoryUom: string;
conversionFactor: number;
shelfId: string | null;
minimumOrderQuantity?: number;
+ supplierTaxAmount: number;
+ taxPercent: number;
}>({
itemId: initialValues.itemId ?? "",
itemReadableId: initialValues.itemReadableId ?? "",
description: initialValues.description ?? "",
purchaseQuantity: initialValues.purchaseQuantity ?? 1,
supplierUnitPrice: initialValues.supplierUnitPrice ?? 0,
+ supplierShippingCost: initialValues.supplierShippingCost ?? 0,
purchaseUom: initialValues.purchaseUnitOfMeasureCode ?? "",
inventoryUom: initialValues.inventoryUnitOfMeasureCode ?? "",
conversionFactor: initialValues.conversionFactor ?? 1,
shelfId: initialValues.shelfId ?? "",
minimumOrderQuantity: undefined,
+ supplierTaxAmount: initialValues.supplierTaxAmount ?? 0,
+ taxPercent:
+ (initialValues.supplierUnitPrice ?? 0) *
+ (initialValues.purchaseQuantity ?? 1) +
+ (initialValues.supplierShippingCost ?? 0) >
+ 0
+ ? (initialValues.supplierTaxAmount ?? 0) /
+ ((initialValues.supplierUnitPrice ?? 0) *
+ (initialValues.purchaseQuantity ?? 1) +
+ (initialValues.supplierShippingCost ?? 0))
+ : 0,
});
+ // update tax amount when quantity or unit price changes
+ useEffect(() => {
+ const subtotal =
+ itemData.supplierUnitPrice * itemData.purchaseQuantity +
+ itemData.supplierShippingCost;
+ if (itemData.taxPercent !== 0) {
+ setItemData((d) => ({
+ ...d,
+ supplierTaxAmount: subtotal * itemData.taxPercent,
+ }));
+ }
+ }, [
+ itemData.supplierUnitPrice,
+ itemData.purchaseQuantity,
+ itemData.supplierShippingCost,
+ itemData.taxPercent,
+ ]);
+
const isEditing = initialValues.id !== undefined;
const isDisabled = !isEditable
? true
@@ -111,11 +144,14 @@ const PurchaseOrderLineForm = ({
description: "",
purchaseQuantity: 1,
supplierUnitPrice: 0,
+ supplierShippingCost: 0,
inventoryUom: "",
purchaseUom: "",
conversionFactor: 1,
shelfId: "",
minimumOrderQuantity: undefined,
+ supplierTaxAmount: 0,
+ taxPercent: 0,
});
};
@@ -164,6 +200,7 @@ const PurchaseOrderLineForm = ({
supplierUnitPrice:
(supplierPart?.data?.unitPrice ?? itemCost?.unitCost ?? 0) /
(routeData?.purchaseOrder?.exchangeRate ?? 1),
+ supplierShippingCost: 0,
purchaseUom:
supplierPart?.data?.supplierUnitOfMeasureCode ??
itemReplenishment?.purchasingUnitOfMeasureCode ??
@@ -175,6 +212,8 @@ const PurchaseOrderLineForm = ({
itemReplenishment?.conversionFactor ??
1,
shelfId: inventory.data?.defaultShelfId ?? null,
+ supplierTaxAmount: 0,
+ taxPercent: 0,
});
if (item.data?.type) {
@@ -182,28 +221,6 @@ const PurchaseOrderLineForm = ({
}
break;
- // case "Service":
- // const service = await carbon
- // .from("item")
- // .select("readableId, name")
- // .eq("id", itemId)
- // .eq("companyId", company.id)
- // .single();
-
- // setItemData({
- // itemId: itemId,
- // itemReadableId: service.data?.readableId ?? "",
- // description: service.data?.name ?? "",
- // purchaseQuantity: 1,
- // supplierUnitPrice: 0,
- // purchaseUom: "EA",
- // inventoryUom: "EA",
- // conversionFactor: 1,
- // shelfId: "",
- // minimumOrderQuantity: undefined,
- // });
-
- // break;
default:
throw new Error(
`Invalid purchase order line type: ${itemType} is not implemented`
@@ -298,32 +315,6 @@ const PurchaseOrderLineForm = ({
onTypeChange={onTypeChange}
/>
- {/*
- {itemType === "G/L Account" && (
- <Account
- name="accountNumber"
- label="Account"
- classes={["Expense", "Asset"]}
- onChange={(value) => {
- setItemData({
- itemId: "",
- itemReadableId: "",
- description: value?.label ?? "",
- purchaseQuantity: 1,
- supplierUnitPrice: 0,
- purchaseUom: "EA",
- inventoryUom: "EA",
- conversionFactor: 1,
- shelfId: "",
- minimumOrderQuantity: 0,
- });
- }}
- />
- )}
- {itemType === "Fixed Asset" && (
- // TODO: implement Fixed Asset
- <Select name="assetId" label="Asset" options={[]} />
- )} */}
<FormControl className="col-span-2">
<FormLabel>Description</FormLabel>
<Input
@@ -402,25 +393,44 @@ const PurchaseOrderLineForm = ({
}))
}
/>
- <Number
+ <NumberControlled
name="supplierShippingCost"
label="Shipping"
+ value={itemData.supplierShippingCost}
formatOptions={{
style: "currency",
currency:
routeData?.purchaseOrder?.currencyCode ??
company.baseCurrencyCode,
}}
+ onChange={(value) =>
+ setItemData((d) => ({
+ ...d,
+ supplierShippingCost: value,
+ }))
+ }
/>
- <Number
+ <NumberControlled
name="supplierTaxAmount"
label="Tax"
+ value={itemData.supplierTaxAmount}
formatOptions={{
style: "currency",
currency:
routeData?.purchaseOrder?.currencyCode ??
company.baseCurrencyCode,
}}
+ onChange={(value) => {
+ const subtotal =
+ itemData.supplierUnitPrice *
+ itemData.purchaseQuantity +
+ itemData.supplierShippingCost;
+ setItemData((d) => ({
+ ...d,
+ supplierTaxAmount: value,
+ taxPercent: subtotal > 0 ? value / subtotal : 0,
+ }));
+ }}
/>
{[
"Item",
@@ -462,6 +472,30 @@ const PurchaseOrderLineForm = ({
}}
/>
)}
+ <NumberControlled
+ name="taxPercent"
+ label="Tax Percent"
+ value={itemData.taxPercent}
+ minValue={0}
+ maxValue={1}
+ step={0.0001}
+ formatOptions={{
+ style: "percent",
+ minimumFractionDigits: 0,
+ maximumFractionDigits: 2,
+ }}
+ onChange={(value) => {
+ const subtotal =
+ itemData.supplierUnitPrice *
+ itemData.purchaseQuantity +
+ itemData.supplierShippingCost;
+ setItemData((d) => ({
+ ...d,
+ taxPercent: value,
+ supplierTaxAmount: subtotal * value,
+ }));
+ }}
+ />
<CustomFormFields table="purchaseOrderLine" />
</div>
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx
index 18fc062320..52ccc9b9fa 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx
@@ -16,7 +16,7 @@ import {
} from "@carbon/react";
import { useFetchers, useNavigate } from "@remix-run/react";
import { useRef, useState } from "react";
-import { LuChevronDown, LuChevronUp, LuSearch } from "react-icons/lu";
+import { LuChevronDown, LuChevronRight, LuSearch } from "react-icons/lu";
import { Copy, MethodIcon, MethodItemTypeIcon } from "~/components";
import type { FlatTree, FlatTreeItem } from "~/components/TreeView";
import { LevelLine, TreeView, useTree } from "~/components/TreeView";
@@ -161,7 +161,7 @@ const QuoteBoMExplorer = ({
state.expanded ? (
<LuChevronDown className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
) : (
- <LuChevronUp className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
+ <LuChevronRight className="h-4 w-4 text-gray-400 flex-shrink-0 ml-1" />
)
) : (
<div className="h-8 w-4" />
diff --git a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.$lineId.details.tsx b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.$lineId.details.tsx
index 05e379998b..7acd386e90 100644
--- a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.$lineId.details.tsx
@@ -127,6 +127,7 @@ export default function EditPurchaseInvoiceLineRoute() {
purchaseInvoiceLine?.inventoryUnitOfMeasureCode ?? "",
conversionFactor: purchaseInvoiceLine?.conversionFactor ?? 1,
shelfId: purchaseInvoiceLine?.shelfId ?? "",
+ taxPercent: purchaseInvoiceLine?.taxPercent ?? 0,
...getCustomFields(purchaseInvoiceLine?.customFields),
};
diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId.$lineId.details.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId.$lineId.details.tsx
index a51c6057ce..0343c1293c 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId.$lineId.details.tsx
@@ -138,6 +138,7 @@ export default function EditPurchaseOrderLineRoute() {
inventoryUnitOfMeasureCode: line?.inventoryUnitOfMeasureCode ?? "",
conversionFactor: line?.conversionFactor ?? 1,
shelfId: line?.shelfId ?? "",
+ taxPercent: line?.taxPercent ?? 0,
...getCustomFields(line?.customFields),
};
diff --git a/packages/database/supabase/migrations/20250128195311_fix-purchasing-tax-percent.sql b/packages/database/supabase/migrations/20250128195311_fix-purchasing-tax-percent.sql
new file mode 100644
index 0000000000..061e0be06c
--- /dev/null
+++ b/packages/database/supabase/migrations/20250128195311_fix-purchasing-tax-percent.sql
@@ -0,0 +1,76 @@
+DROP VIEW IF EXISTS "purchaseOrderLines";
+ALTER TABLE "purchaseOrderLine" DROP COLUMN "taxPercent";
+
+DROP VIEW IF EXISTS "purchaseInvoiceLines";
+ALTER TABLE "purchaseInvoiceLine" DROP COLUMN "taxPercent";
+
+ALTER TABLE "purchaseOrderLine" ADD COLUMN "taxPercent" NUMERIC(10,5) GENERATED ALWAYS AS (
+ CASE
+ WHEN ("supplierUnitPrice" * "purchaseQuantity" + "supplierShippingCost") = 0 THEN 0
+ ELSE "supplierTaxAmount" / ("supplierUnitPrice" * "purchaseQuantity" + "supplierShippingCost")
+ END
+) STORED;
+
+DROP VIEW IF EXISTS "purchaseOrderLines";
+CREATE OR REPLACE VIEW "purchaseOrderLines" WITH(SECURITY_INVOKER=true) AS (
+ SELECT
+ pl.*,
+ CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ WHEN i."thumbnailPath" IS NULL AND imu."thumbnailPath" IS NOT NULL THEN imu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END as "thumbnailPath",
+ i.name as "itemName",
+ i.description as "itemDescription",
+ COALESCE(mu.id, imu.id) as "modelId",
+ COALESCE(mu."autodeskUrn", imu."autodeskUrn") as "autodeskUrn",
+ COALESCE(mu."modelPath", imu."modelPath") as "modelPath",
+ COALESCE(mu."name", imu."name") as "modelName",
+ COALESCE(mu."size", imu."size") as "modelSize",
+ ic."unitCost" as "unitCost",
+ sp."supplierPartId"
+ FROM "purchaseOrderLine" pl
+ INNER JOIN "purchaseOrder" so ON so.id = pl."purchaseOrderId"
+ LEFT JOIN "modelUpload" mu ON pl."modelUploadId" = mu."id"
+ INNER JOIN "item" i ON i.id = pl."itemId"
+ LEFT JOIN "itemCost" ic ON ic."itemId" = i.id
+ LEFT JOIN "modelUpload" imu ON imu.id = i."modelUploadId"
+ LEFT JOIN "supplierPart" sp ON sp."supplierId" = so."supplierId" AND sp."itemId" = i.id
+);
+
+ALTER TABLE "purchaseInvoiceLine" ADD COLUMN "taxPercent" NUMERIC(10,5) GENERATED ALWAYS AS (
+ CASE
+ WHEN ("supplierUnitPrice" * "quantity" + "supplierShippingCost") = 0 THEN 0
+ ELSE "supplierTaxAmount" / ("supplierUnitPrice" * "quantity" + "supplierShippingCost")
+ END
+) STORED;
+
+DROP VIEW IF EXISTS "purchaseInvoiceLines";
+CREATE OR REPLACE VIEW "purchaseInvoiceLines" WITH(SECURITY_INVOKER=true) AS (
+ SELECT
+ pl.*,
+ CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ WHEN i."thumbnailPath" IS NULL AND imu."thumbnailPath" IS NOT NULL THEN imu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END as "thumbnailPath",
+ i.name as "itemName",
+ i.description as "itemDescription",
+ ic."unitCost" as "unitCost",
+ sp."supplierPartId"
+ FROM "purchaseInvoiceLine" pl
+ INNER JOIN "purchaseInvoice" pi ON pi.id = pl."invoiceId"
+ LEFT JOIN "modelUpload" mu ON pl."modelUploadId" = mu."id"
+ INNER JOIN "item" i ON i.id = pl."itemId"
+ LEFT JOIN "itemCost" ic ON ic."itemId" = i.id
+ LEFT JOIN "modelUpload" imu ON imu.id = i."modelUploadId"
+ LEFT JOIN "supplierPart" sp ON sp."supplierId" = pi."supplierId" AND sp."itemId" = i.id
+);
+
+ALTER TABLE "supplierQuoteLinePrice" DROP COLUMN "taxPercent";
+ALTER TABLE "supplierQuoteLinePrice" ADD COLUMN "taxPercent" NUMERIC(10,5) GENERATED ALWAYS AS (
+ CASE
+ WHEN ("supplierUnitPrice" * "quantity" + "supplierShippingCost") = 0 THEN 0
+ ELSE "supplierTaxAmount" / ("supplierUnitPrice" * "quantity" + "supplierShippingCost")
+ END
+) STORED;
\ No newline at end of file