FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

130 lines
3.4 KiB

  1. import dayjs, { ConfigType, Dayjs } from "dayjs";
  2. import { Uom } from "../api/settings/uom";
  3. import {
  4. ListIterateeCustom,
  5. every,
  6. isArray,
  7. isNaN,
  8. isNull,
  9. isUndefined,
  10. take,
  11. } from "lodash";
  12. import { Box, BoxProps } from "@mui/material";
  13. export const manhourFormatter = new Intl.NumberFormat("en-HK", {
  14. minimumFractionDigits: 2,
  15. maximumFractionDigits: 2,
  16. });
  17. export const moneyFormatter = new Intl.NumberFormat("en-HK", {
  18. style: "currency",
  19. currency: "HKD",
  20. });
  21. export const decimalFormatter = new Intl.NumberFormat("en-HK", {
  22. minimumFractionDigits: 2,
  23. maximumFractionDigits: 5,
  24. });
  25. export const integerFormatter = new Intl.NumberFormat("en-HK", {});
  26. export const INPUT_DATE_FORMAT = "YYYY-MM-DD";
  27. export const OUTPUT_DATE_FORMAT = "YYYY/MM/DD";
  28. export const INPUT_TIME_FORMAT = "HH:mm:ss";
  29. export const OUTPUT_TIME_FORMAT = "HH:mm:ss";
  30. export const arrayToDayjs = (arr: ConfigType | (number | undefined)[]) => {
  31. const isValidNumber = (
  32. item: ListIterateeCustom<number | undefined, boolean>,
  33. ): boolean => typeof item === "number" && !isNaN(item) && isFinite(item);
  34. let tempArr = arr;
  35. if (isArray(arr) && every(arr, isValidNumber) && arr.length >= 3) {
  36. // [year, month, day]
  37. // tempArr = take(arr, 3);
  38. tempArr = `${arr[0]?.toString().padStart(4, "0")}-${arr[1]?.toString().padStart(2, "0")}-${arr[2]?.toString().padStart(2, "0")}`;
  39. }
  40. return dayjs(tempArr as ConfigType);
  41. };
  42. export const arrayToDateString = (arr: ConfigType | (number | undefined)[]) => {
  43. return arrayToDayjs(arr).format(OUTPUT_DATE_FORMAT);
  44. };
  45. export const arrayToInputDateString = (arr: ConfigType | (number | undefined)[]) => {
  46. return arrayToDayjs(arr).format(INPUT_DATE_FORMAT);
  47. };
  48. export const dateStringToDayjs = (date: string) => {
  49. // Format: YYYY/MM/DD
  50. return dayjs(date, OUTPUT_DATE_FORMAT);
  51. };
  52. export const dateTimeStringToDayjs = (dateTime: string) => {
  53. // Format: YYYY/MM/DD HH:mm:ss
  54. return dayjs(dateTime, `${OUTPUT_DATE_FORMAT} ${OUTPUT_TIME_FORMAT}`);
  55. };
  56. export const dayjsToDateString = (date: Dayjs) => {
  57. return date.format(OUTPUT_DATE_FORMAT);
  58. };
  59. export const dayjsToInputDateString = (date: Dayjs) => {
  60. return date.format(INPUT_DATE_FORMAT + "T" + INPUT_TIME_FORMAT);
  61. };
  62. export const dayjsToInputDatetimeString = (date: Dayjs) => {
  63. return date.format(INPUT_DATE_FORMAT);
  64. };
  65. export const dayjsToInputDateTimeString = (date: Dayjs) => {
  66. return date.format(`${INPUT_DATE_FORMAT}T${OUTPUT_TIME_FORMAT}`);
  67. };
  68. export const outputDateStringToInputDateString = (date: string) => {
  69. return dayjsToInputDateString(dateStringToDayjs(date))
  70. }
  71. export const stockInLineStatusMap: { [status: string]: number } = {
  72. draft: 0,
  73. pending: 1,
  74. qc: 2,
  75. determine1: 3,
  76. determine2: 4,
  77. determine3: 5,
  78. receiving: 6,
  79. received: 7,
  80. completed: 8,
  81. rejected: 9,
  82. };
  83. export const stockOutLineStatusMap: { [status: string]: number } = {
  84. draft: 0,
  85. pending: 1, // waiting for qc
  86. determine1: 2, // waiting for qc
  87. "lot-change": 3, // waiting for qc
  88. // after qc = completed
  89. completed: 4,
  90. rejected: 5,
  91. };
  92. export const pickOrderStatusMap: { [status: string]: number } = {
  93. pending: 1,
  94. consolidated: 2,
  95. released: 3,
  96. completed: 4,
  97. };
  98. export const calculateWeight = (qty: number, uom: Uom) => {
  99. return qty * (uom.unit2Qty || 1) * (uom.unit3Qty || 1) * (uom.unit4Qty || 1);
  100. };
  101. export const returnWeightUnit = (uom: Uom) => {
  102. return uom.unit4 || uom.unit3 || uom.unit2 || uom.unit1;
  103. };