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.

12.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  HStack,
  Table,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  Tbody,
  Td,
  Th,
  Thead,
  Tr
} from "@carbon/react";
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious
} from "@carbon/react/Carousel";
import { Trans } from "@lingui/react/macro";
import { Link } from "react-router";
import { DateTime, Empty } from "~/components";
import { useCurrencyFormatter } from "~/hooks";
import { useCustomers } from "~/stores/customers";
import { path } from "~/utils/path";
import type { HistoricalQuotationPrice, SalesOrderLine } from "../../types";

const QuoteLinePricingHistory = ({
  baseCurrency,
  relatedSalesOrderLines,
  historicalQuoteLinePrices
}: {
  baseCurrency: string;
  relatedSalesOrderLines: SalesOrderLine[];
  historicalQuoteLinePrices: HistoricalQuotationPrice[];
}) => {
  const historicalQuoteLines = historicalQuoteLinePrices.reduce<
    Record<
      string,
      HistoricalQuotationPrice & { quantities: Record<number, number> }
    >
  >((acc, linePrice) => {
    if (!acc[linePrice.id!]) {
      acc[linePrice.id!] = { ...linePrice, quantities: {} };
    }
    if (linePrice.qty && linePrice.unitPrice) {
      acc[linePrice.id!].quantities[linePrice.qty] = linePrice.unitPrice;
    }
    return acc;
  }, {});

  const orderLineCount = relatedSalesOrderLines.length;
  const quoteLineCount = Object.keys(historicalQuoteLines).length;

  const hasOrderLines = orderLineCount > 0;
  const hasQuoteLines = quoteLineCount > 0;
  const hasBothTypes = hasOrderLines && hasQuoteLines;

  // Default to the tab that has items
  const defaultTab = hasOrderLines ? "salesOrderLines" : "quoteLines";
  const [customers] = useCustomers();
  const priceFormatter = useCurrencyFormatter({
    rate: true,
    currency: baseCurrency
  });

  return (
    <Card>
      <CardHeader>
        <CardTitle>
          <Trans>History</Trans>
        </CardTitle>
        <CardDescription>
          {orderLineCount > 0 || quoteLineCount > 0 ? (
            <span className="text-sm text-muted-foreground">
              {orderLineCount > 0 &&
                `${orderLineCount} order${orderLineCount !== 1 ? "s" : ""}`}
              {orderLineCount > 0 && quoteLineCount > 0 && " and "}
              {quoteLineCount > 0 &&
                `${quoteLineCount} quote${quoteLineCount !== 1 ? "s" : ""}`}
            </span>
          ) : (
            <span className="text-sm text-muted-foreground">
              No pricing history available
            </span>
          )}
        </CardDescription>
      </CardHeader>
      <CardContent>
        <div className="w-full">
          <Tabs defaultValue={defaultTab} className="w-full">
            {hasBothTypes && (
              <TabsList className="mb-4">
                <TabsTrigger value="salesOrderLines">
                  <Trans>Orders</Trans>
                </TabsTrigger>
                <TabsTrigger value="quoteLines">
                  <Trans>Quotes</Trans>
                </TabsTrigger>
              </TabsList>
            )}
            <TabsContent value="salesOrderLines">
              <div className="flex overflow-x-auto space-x-4 pb-4 w-full">
                {!hasOrderLines && <Empty className="py-6" />}
                {hasOrderLines && (
                  <Carousel className="w-full">
                    <CarouselContent className="-ml-4">
                      {relatedSalesOrderLines.map((line) => (
                        <CarouselItem
                          key={line.id}
                          className="pl-4 basis-full lg:basis-1/2"
                        >
                          <Card className="w-full p-0 bg-gradient-to-b from-card to-card via-card dark:from-card dark:to-card dark:via-card">
                            <CardContent className="p-4">
                              <HStack className="flex justify-between">
                                <div className="flex flex-col gap-1">
                                  <Link
                                    to={path.to.salesOrderLine(
                                      line.salesOrderId!,
                                      line.id!
                                    )}
                                    className="text-sm font-medium hover:underline"
                                  >
                                    {line.salesOrderReadableId}
                                  </Link>
                                  <span className="text-sm text-muted-foreground">
                                    {
                                      customers.find(
                                        (customer) =>
                                          customer.id === line.customerId
                                      )?.name
                                    }
                                  </span>
                                </div>
                                <div className="flex flex-col gap-1 items-end">
                                  <span className="text-xs text-muted-foreground">
                                    <DateTime
                                      value={line.orderDate!}
                                      variant="date"
                                    />
                                  </span>
                                  <span className="text-xs text-muted-foreground">
                                    {line.itemReadableId}
                                  </span>
                                </div>
                              </HStack>
                              <div className="my-4">
                                <Table>
                                  <Thead>
                                    <Tr className="border-b border-border">
                                      <Th>
                                        <span className="font-medium">
                                          Quantity
                                        </span>
                                      </Th>
                                      <Th>
                                        <span className="font-medium">
                                          Price
                                        </span>
                                      </Th>
                                    </Tr>
                                  </Thead>
                                  <Tbody>
                                    <Tr>
                                      <Td>{line.saleQuantity}</Td>
                                      <Td>
                                        {priceFormatter.format(
                                          line.unitPrice ?? 0
                                        )}
                                      </Td>
                                    </Tr>
                                  </Tbody>
                                </Table>
                              </div>
                            </CardContent>
                          </Card>
                        </CarouselItem>
                      ))}
                    </CarouselContent>
                    {orderLineCount > 1 && (
                      <div className="flex justify-between mt-4">
                        <CarouselPrevious />
                        <CarouselNext />
                      </div>
                    )}
                  </Carousel>
                )}
              </div>
            </TabsContent>
            <TabsContent value="quoteLines">
              <div className="flex overflow-x-auto space-x-4 pb-4 w-full">
                {!hasQuoteLines && <Empty className="py-6" />}
                {hasQuoteLines && (
                  <Carousel className="w-full">
                    <CarouselContent className="-ml-4">
                      {Object.values(historicalQuoteLines).map((line) => (
                        <CarouselItem
                          key={line.id}
                          className="pl-4 basis-full lg:basis-1/2"
                        >
                          <Card className="w-full p-0 bg-gradient-to-b from-card to-card via-card dark:from-card dark:to-card dark:via-card">
                            <CardContent className="p-4">
                              <div className="flex flex-col gap-4">
                                <HStack className="flex justify-between">
                                  <div className="flex flex-col gap-1">
                                    <Link
                                      to={path.to.quoteLine(
                                        line.quoteId!,
                                        line.id!
                                      )}
                                      className="text-sm font-medium hover:underline"
                                    >
                                      {line.quoteReadableId}
                                    </Link>

                                    <span className="text-sm text-muted-foreground">
                                      {
                                        customers.find(
                                          (customer) =>
                                            customer.id === line.customerId
                                        )?.name
                                      }
                                    </span>
                                  </div>
                                  <div className="flex flex-col gap-1 items-end">
                                    <span className="text-xs text-muted-foreground">
                                      <DateTime
                                        value={line.quoteCreatedAt!}
                                        variant="date"
                                      />
                                    </span>
                                    <span className="text-xs text-muted-foreground">
                                      {line.itemReadableId}
                                    </span>
                                  </div>
                                </HStack>
                              </div>

                              <div className="my-4">
                                <Table>
                                  <Thead>
                                    <Tr>
                                      <Th>
                                        <span className="font-medium">
                                          Quantity
                                        </span>
                                      </Th>
                                      <Th>
                                        <span className="font-medium">
                                          Price
                                        </span>
                                      </Th>
                                    </Tr>
                                  </Thead>
                                  <Tbody>
                                    {Object.entries(line.quantities).map(
                                      ([quantity, price]) => (
                                        <Tr key={quantity}>
                                          <Td>{quantity}</Td>
                                          <Td>
                                            {priceFormatter.format(
                                              price as number
                                            )}
                                          </Td>
                                        </Tr>
                                      )
                                    )}
                                  </Tbody>
                                </Table>
                              </div>
                            </CardContent>
                          </Card>
                        </CarouselItem>
                      ))}
                    </CarouselContent>
                    {quoteLineCount > 1 && (
                      <div className="flex justify-between mt-4">
                        <CarouselPrevious />
                        <CarouselNext />
                      </div>
                    )}
                  </Carousel>
                )}
              </div>
            </TabsContent>
          </Tabs>
        </div>
      </CardContent>
    </Card>
  );
};

export default QuoteLinePricingHistory;