Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.0 kB
7
Indexable
const assortCalculations = (
    _allSizeBreakDown,
    _plData = plData,
    packageCount = 1
  ) => {
    let { _assortBreakdown, _solidBreakdown } = _allSizeBreakDown;
    let { qty, capacity, cartonName } = _assortBreakdown;

    //Size as per ratio
    let sizes = weightPerSize.map((item) => {
      return item.product_size;
    });

    let ratioSum = _assortBreakdown.ratio.split(":").reduce((prev, next) => {
      return Number(Number(prev) + Number(next));
    });

    let perfectPackageCount = parseInt(qty / capacity);
    let sizeCountOnePkg = Number(capacity) / ratioSum;

    let ratioSize = _assortBreakdown.ratio.split(":").map((item, i) => {
      let m = sizes[i];
      return { size: m, ratio: item };
    });

    let remainingQty = qty % capacity; //30
    if (parseInt(qty) < parseInt(capacity)) {
      remainingQty = -9;
      perfectPackageCount = 1;
      sizeCountOnePkg = Number(qty) / ratioSum;
    }

    let finalNetWeight = 0;
    let finalNumOfCarton = 0;
    if (qty > 0) {
      let assort1 = _plData.map((item, index) => {
        let _item = { ...item };
        let currentRatio = _.find(ratioSize, ["size", item.item_size])?.ratio;
        _item.from_carton_no = packageCount;
        _item.carton_number = cartonName;
        _item.to_carton_no =
          perfectPackageCount === 1 ? packageCount : perfectPackageCount;

        _item.ordered_qty = parseInt(_item.ordered_qty);

        _item.remaining_qty =
          parseInt(_item.ordered_qty) -
          parseInt(perfectPackageCount) *
            parseInt(sizeCountOnePkg) *
            parseInt(currentRatio);

        _item.pcs_per_carton =
          parseInt(sizeCountOnePkg) * parseInt(currentRatio);

        if (props.packingInPacks) {
          _item.total_packs =
            parseInt(_item.pcs_per_carton) * parseInt(perfectPackageCount);
          _item.total_pcs = _item.total_packs * _item.pcs_per_pack;

          console.log();
        } else {
          _item.total_pcs =
            parseInt(_item.pcs_per_carton) * parseInt(perfectPackageCount);
        }

        // _item.net_weight = (
        //   _.find(weightPerSize, ["product_size", _item.item_size])
        //     ?.product_weight * _item.total_pcs
        // ).toFixed(2);

        finalNetWeight += parseFloat(
          (
            _.find(weightPerSize, ["product_size", _item.item_size])
              ?.product_weight * _item.total_pcs
          ).toFixed(2)
        );

        console.log();
        _item.net_weight = "";

        let numOfCarton =
          parseInt(_item.to_carton_no) - parseInt(_item.from_carton_no) + 1;
        _item.no_of_cartons = numOfCarton;

        finalNumOfCarton = numOfCarton;

        _item.gross_weight = (
          parseFloat(_item.net_weight) +
          parseInt(numOfCarton) *
            parseFloat(
              _.find(cartonDimension, ["ctn_code", cartonName])?.ctn_weight
            )
        ).toFixed(2);

        _item.net_weight_per_carton = "";
        // _item.net_weight_per_carton = (
        //   parseFloat(_item.net_weight) / parseInt(numOfCarton)
        // ).toFixed(2);

        _item.gross_weight_per_carton = (
          parseFloat(_item.gross_weight) / numOfCarton
        ).toFixed(2);

        _item.overall_cbm = "";
        // _item.overall_cbm = parseFloat(
        //   numOfCarton *
        //     _.find(cartonDimension, ["ctn_code", cartonName])?.ctn_cbm
        // ).toFixed(2);
        _item.pack_type = "assort";
        return _item;
      });

      assort1[0].net_weight = finalNetWeight;
      assort1[0].net_weight_per_carton =
        parseFloat(finalNetWeight) / parseFloat(finalNumOfCarton);
      assort1[0].gross_weight =
        parseFloat(finalNetWeight) +
        finalNumOfCarton *
          parseFloat(
            _.find(cartonDimension, ["ctn_code", cartonName])?.ctn_weight
          );
      assort1[0].gross_weight_per_carton = (
        parseFloat(assort1[0].gross_weight) / parseFloat(finalNumOfCarton)
      ).toFixed(2);
      assort1[0].overall_cbm = (
        parseFloat(_.find(cartonDimension, ["ctn_code", cartonName])?.ctn_cbm) *
        parseInt(finalNumOfCarton)
      ).toFixed(2);

      assort1[0].no_of_cartons_cal = finalNumOfCarton;

      let updatedPlData = assort1.map((item, i) => {
        let _item = { ...item };
        _item.ordered_qty = _item.remaining_qty;
        return _item;
      });
      setAssortData([...assortData, ...assort1]);
      assortCalculations(
        {
          ..._allSizeBreakDown,
          _assortBreakdown: {
            ..._assortBreakdown,
            qty: remainingQty,
          },
        },
        updatedPlData,
        perfectPackageCount + 1
      );
    } else {
      if (!_.isNil(_solidBreakdown)) {
        solidCalculations(_allSizeBreakDown, _plData, packageCount);
      }
      console.log("done");
    }
  };