import Dinero from "dinero.js";

export function CalculateJobRpsDollars(job, returnSumActPrice, mpi_count_quantity = false) {
  if (!job) {
    return 0;
  }
  let actPriceSum = Dinero();
  const jobRpsDollars = job.joblines
    .filter((j) => !j.ignore)
    .reduce((acc, val) => {
      actPriceSum = actPriceSum.add(
        Dinero({ amount: Math.round((val.act_price || 0) * 100) })
          .multiply(
            mpi_count_quantity ? (val.part_qty || 1) : 1
          )
      );
      // if (val.price_diff > 0) {  //
      return acc.add(
        Dinero({ amount: Math.round((val.price_diff || 0) * 100) })
          .multiply(
            mpi_count_quantity ? (val.part_qty || 1) : 1
          )
      );
      // } else {
      //   return acc;
      // }
    }, Dinero());
  return returnSumActPrice ? { actPriceSum, jobRpsDollars } : jobRpsDollars;
}

export function CalculateJobRpsPc(
  job,
  currentRpsDollars,
  returnSumDbPrice = false,
  mpi_count_quantity = false
) {
  //TODO Redo this to do total of db price - act price / db price
  if (!job) {
    return 0;
  }
  const dbPriceSum = job.joblines
    .filter((j) => !j.ignore)
    .reduce((acc, val) => {
      return acc.add(
        Dinero({ amount: Math.round((val.db_price || 0) * 100) })
          .multiply(
            mpi_count_quantity ? (val.part_qty || 1) : 1
          )
      );
    }, Dinero());

  const jobRpsPc = currentRpsDollars.getAmount() / dbPriceSum.getAmount();
  return returnSumDbPrice ? { dbPriceSum, jobRpsPc } : jobRpsPc;
}
