产品中的定时器和异常

时间:2018-04-10 12:31:45

标签: javascript

我们为prestashop中包含的计时器编写了一个javascript代码,它运行良好。该计时器供客户知道他的产品何时到货。目前,对于所有产品而言,它是相同的日期,但我们希望对某些需要更长时间才能到货的产品进行例外处理。我们已经尝试但我们没有得到它。任何人都可以帮助我吗?。

非常感谢!!

这是代码:



function getShippingDataFromDate(curDate) {
  // 1-31
  var day = curDate.getDate();
  var deliveryDay = day;
  var nextDay = day;
  // 24h
  var hour = curDate.getHours();
  var minute = curDate.getMinutes();
  // 0-11
  var month = curDate.getMonth();
  var year = curDate.getFullYear();

  // Today is thursday
  if (curDate.getDay() == 4) {
    if (hour >= 9) {
      // move next day to wednesday (tomorrow) 9am
      nextDay += 1;
      // move delivery to monday as it gets processed on wednesday
      deliveryDay += 4;
    } else {
      // do not move next day, shows countdown until today's 9 am
      nextDay += 0;
      // move delivery to wednesday
      deliveryDay += 1;
    }
  }
  // Today is friday
  else if (curDate.getDay() == 5) {
    if (hour >= 9) {
      // move next day to monday at 9am
      nextDay += 3;
      // move delivery to tuesday as it gets processed on monday
      deliveryDay += 4;
    } else {
      // do not move next day, shows countdown until today's 9 am
      nextDay += 0;
      // move delivery to monday
      deliveryDay += 3;
    }
  }
  // Today is saturday
  else if (curDate.getDay() == 6) {
    // move next day to monday 9am
    nextDay += 2;
    // move delivery until tuesday
    deliveryDay += 3;
  }
  // Today is sunday
  else if (curDate.getDay() == 0) {
    // move next day to monday (tomorrow) 9am
    nextDay += 1;
    // move delivery until tuesday
    deliveryDay += 2;
  }
  // Any other day 
  else {
    // past 9 am, must deliver past tomorrow
    if (hour >= 9) {
      // the next day is tomorrow 9am
      nextDay += 1;
      // deliver the day past tomorrow
      deliveryDay += 2;
    }
    // before 9am, deliver tomorrow
    else {
      // do not move next day, shows countdown until today's 9 am
      nextDay += 0;
      // move delivery to tomorrow
      deliveryDay += 1;
    }
  }

  // Date to calculate the timespan until the 9:00 am of the next labour day
  // (this is the next date from now that the hour is 9:00 am).
  var nextDate = new Date(
    year,
    month,
    nextDay,
    9,
    0,
    0);

  // Estimated delivery date
  var deliveryDate = new Date(
    year,
    month,
    deliveryDay,
    9,
    0,
    0
  );

  var result = {
    curDate: curDate,
    nextDate: nextDate,
    deliveryDate: deliveryDate,
  };

  shippingAdjustHolidays(result, year, month, day, hour, minute, deliveryDay, nextDay);

  return result;
}

function shippingAdjustHolidays(shippingData, year, month, day, hour, minute, deliveryDay, nextDay) {
  /** Exception for holidays **/

  // Fecha de entrega
  var deliveryDate = shippingData.deliveryDate;
  // Fecha para el "pídalo antes de ...."
  var nextDate = shippingData.nextDate;
  var altered = false;

  // Antes del momento límite nos aseguramos que se anuncia la entrega en el siguiente
  // día
  // Month is 0-based, 2 is march
  if (month == 11 && day == 31 && hour <= 9) {
    altered = true;
    deliveryDate = new Date(
      year,
      0, // Month is 0-based, 2 is march
      3,
      9,
      0,
      0
    );
  }
  // Tras el momento límite hay que mostrar la fecha fija hasta el último día laborable 
  // antes del primer día normal ya que sabados y domingos ya están manejados correctamente.
  // Month is 0-based, 2 is march
  else if (((month == 11 && day == 31 && hour > 9) || (month == 11 && day > 31)) && (month == 11 && day <= 25)) {
    altered = true;
    deliveryDate = new Date(
      year,
      0, // Month is 0-based, 2 is march
      3,
      9,
      0,
      0
    );

    nextDate = new Date(
      year,
      0, // Month is 0-based, 2 is march
      3,
      9,
      0,
      0);
  }

  /** Exception for poming only applies if no special date is set **/

  if (window.id_product && window.id_product == 49) {
    deliveryDate = new Date(
      year,
      month,
      deliveryDay + 5,
      9,
      0,
      0
    );

    nextDate = new Date(
      year,
      month,
      nextDay,
      9,
      0,
      0);

    /** Everything else **/
  }

  // Check if the previous correction caused the days to drift and move into non-business
  // days.
  var fixedIt = false;

  // If the next date is saturday or sunday move it to monday
  if (deliveryDate.getDay() == 6) {
    // If the next day is saturday must wait two additional days
    deliveryDay = deliveryDate.getDate() + 2;
    fixedIt = true;
  } else if (deliveryDate.getDay() == 0) {
    // If the next day is sunday must wait one additional day
    deliveryDay = deliveryDate.getDate() + 1;
    fixedIt = true;
  }

  if (fixedIt) {
    deliveryDate = new Date(
      deliveryDate.getYear(),
      deliveryDate.getMonth(),
      deliveryDay,
      9,
      0,
      0
    );
  }

  fixedIt = false;

  if (nextDate.getDay() == 6) {
    // If the next day is saturday must wait two additional days
    nextDay = nextDate.getDate() + 2;
    fixedIt = true;
  } else if (nextDate.getDay() == 0) {
    // If the next day is sunday must wait one additional day
    nextDay = nextDate.getDate() + 1;
    fixedIt = true;
  }

  if (fixedIt) {
    nextDate = new Date(
      nextDate.getYear(),
      nextDate.getMonth(),
      nextDay,
      9,
      0,
      0
    );
  }

  shippingData.deliveryDate = deliveryDate;
  shippingData.nextDate = nextDate;
}

function shippingDateToString(jDate) {
  var dayNames = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];

  var str = "" + jDate.getFullYear() +
    "/" + (jDate.getMonth() + 1) +
    "/" + jDate.getDate() +
    " (" + dayNames[jDate.getDay()] + ")" +
    " " + jDate.getHours() +
    ":" + jDate.getMinutes() +
    ":" + jDate.getSeconds();

  return str;
}

function logShippingData(data) {
  console.log(
    "shippingData {\n\tcurDate: " + shippingDateToString(data.curDate) +
    ", \n\tnextDate: " + shippingDateToString(data.nextDate) +
    ", \n\tdeliveryDate: " + shippingDateToString(data.deliveryDate) + "}");
}

function updateShippingHoursLeft(hoursTpl, strings, disabled, curDate, updateDisplay) {

  if (disabled) {
    jQuery('#estimated_delivery_date').css("display", "none");
    return;
  }

  updateDisplay = (typeof updateDisplay == "undefined") ?
    true :
    updateDisplay;

  curDate = curDate || new Date();

  var deliveryData = getShippingDataFromDate(curDate);

  var nextDate = deliveryData.nextDate;
  var deliveryDate = deliveryData.deliveryDate;
  var curDate = deliveryData.curDate;

  var timeLeftMillis = nextDate - curDate;

  var days = Math.floor(
    timeLeftMillis /
    (1000 * 3600 * 24));

  timeLeftMillis -= days * 24 * 3600 * 1000;

  var hours = Math.floor(
    timeLeftMillis /
    (1000 * 3600));

  timeLeftMillis -= hours * 3600 * 1000;

  var minutes = Math.floor(
    timeLeftMillis /
    (1000 * 60));

  timeLeftMillis -= minutes * 60 * 1000;

  var seconds = Math.floor(
    timeLeftMillis /
    (1000));

  var dateStr = hoursTpl
    .replace("[DATE_DAY]", '<span class="time-units">' + deliveryDate.getDate() + '</span>')
    .replace("[DATE_DAY_OF_WEEK]", '<span class="time-units">' + strings.weekDays[deliveryDate.getDay()] + '</span>')
    .replace("[DATE_MONTH]", '<span class="time-units">' + (deliveryDate.getMonth() + 1) + '</span>')
    .replace("[DATE_MONTH_NAME]", '<span class="time-units">' + (strings.months[deliveryDate.getMonth()]) + '</span>')
    .replace("[DATE_YEAR]", '<span class="date-units">' + deliveryDate.getFullYear() + '</span>')
    .replace("[DAYS]", days)
    .replace("[HOURS]", hours)
    .replace("[MINUTES]", minutes)
    .replace("[SECONDS]", seconds);

  if (days > 0) {
    dateStr = dateStr
      .replace("[DATE_DAYS_LEFT]", '<span class="time-units">' + days + '</span> d&iacute;as ');
  } else {
    dateStr = dateStr
      .replace("[DATE_DAYS_LEFT]", '');
  }

  if (updateDisplay) {
    jQuery('#estimated_delivery_date').html(dateStr);
    jQuery('#estimated_delivery_date').css("display", "inline");
  } else {
    return dateStr;
  }
}
&#13;
&#13;
&#13;

编辑!

好!谢谢你回答。

这段代码是由另一位程序员完成的,我是一位有点新手的人:P

有一句话说:

/** Exception for poming only applies if no special date is set **/

  if (window.id_product && window.id_product == 49) {
deliveryDate = new Date(
  year,
  month,
  deliveryDay + 5,
  9,
  0,
  0
);

nextDate = new Date(
  year,
  month,
  nextDay,
  9,
  0,
  0);

/** Everything else **/
  }

这应该参考我说的,对吧?我试图更改值,但结果没有差异。

谢谢!

0 个答案:

没有答案