fix
This commit is contained in:
@@ -43,16 +43,20 @@ function parseLocalDate(dateValue) {
|
|||||||
return Number.isNaN(d.getTime()) ? null : d;
|
return Number.isNaN(d.getTime()) ? null : d;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLastSentDate(invoice) {
|
function getFirstSentDate(invoice) {
|
||||||
const sentDates = Array.isArray(invoice.sent_dates)
|
const sentDates = Array.isArray(invoice.sent_dates)
|
||||||
? invoice.sent_dates.filter(Boolean)
|
? invoice.sent_dates.filter(Boolean)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
if (sentDates.length > 0) {
|
if (sentDates.length === 0) {
|
||||||
return sentDates[sentDates.length - 1];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
const sortedDates = sentDates
|
||||||
|
.map(d => String(d).split('T')[0])
|
||||||
|
.sort();
|
||||||
|
|
||||||
|
return sortedDates[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTermDays(invoice) {
|
function getTermDays(invoice) {
|
||||||
@@ -79,18 +83,13 @@ function addDays(dateValue, days) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getEffectiveDueDate(invoice) {
|
function getEffectiveDueDate(invoice) {
|
||||||
const lastSentDate = getLastSentDate(invoice);
|
const firstSentDate = getFirstSentDate(invoice);
|
||||||
|
|
||||||
// Nie versendet = kein Reminder/Overdue Text
|
if (!firstSentDate) {
|
||||||
if (!lastSentDate) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (invoice.due_date) {
|
return addDays(firstSentDate, getTermDays(invoice));
|
||||||
return parseLocalDate(invoice.due_date);
|
|
||||||
}
|
|
||||||
|
|
||||||
return addDays(lastSentDate, getTermDays(invoice));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOverdueDays(invoice) {
|
function getOverdueDays(invoice) {
|
||||||
|
|||||||
@@ -87,14 +87,22 @@ function isPartiallyPaid(inv) {
|
|||||||
return !inv.paid_date && amountPaid > 0 && balance > 0;
|
return !inv.paid_date && amountPaid > 0 && balance > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLastSentDate(inv) {
|
function getFirstSentDate(inv) {
|
||||||
const sentDates = Array.isArray(inv.sent_dates) ? inv.sent_dates.filter(Boolean) : [];
|
const sentDates = Array.isArray(inv.sent_dates)
|
||||||
|
? inv.sent_dates.filter(Boolean)
|
||||||
|
: [];
|
||||||
|
|
||||||
if (sentDates.length > 0) {
|
if (sentDates.length === 0) {
|
||||||
return sentDates[sentDates.length - 1];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
// Sicherheit: nicht auf Array-Reihenfolge verlassen,
|
||||||
|
// sondern wirklich das früheste Versanddatum nehmen.
|
||||||
|
const sortedDates = sentDates
|
||||||
|
.map(d => String(d).split('T')[0])
|
||||||
|
.sort();
|
||||||
|
|
||||||
|
return sortedDates[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTermDays(inv) {
|
function getTermDays(inv) {
|
||||||
@@ -132,22 +140,18 @@ function isBeforeToday(dateObj) {
|
|||||||
|
|
||||||
return compare < today;
|
return compare < today;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEffectiveDueDate(inv) {
|
function getEffectiveDueDate(inv) {
|
||||||
const lastSentDate = getLastSentDate(inv);
|
const firstSentDate = getFirstSentDate(inv);
|
||||||
|
|
||||||
// Wichtig: Nie versendete Rechnungen können nicht overdue sein.
|
// Nie versendet = nicht overdue
|
||||||
if (!lastSentDate) {
|
if (!firstSentDate) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wenn due_date vorhanden ist, darf es genutzt werden,
|
// WICHTIG:
|
||||||
// aber nur nachdem die Rechnung tatsächlich gesendet wurde.
|
// Für Overdue zählt der erste tatsächliche Versand.
|
||||||
if (inv.due_date) {
|
// Bei Net 30 also first_sent_date + 30 Tage.
|
||||||
return parseLocalDate(inv.due_date);
|
return addDays(firstSentDate, getTermDays(inv));
|
||||||
}
|
|
||||||
|
|
||||||
return addDays(lastSentDate, getTermDays(inv));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOverdueDays(inv) {
|
function getOverdueDays(inv) {
|
||||||
@@ -166,7 +170,7 @@ function isOverdue(inv) {
|
|||||||
return !!inv.qbo_id
|
return !!inv.qbo_id
|
||||||
&& !isPaid(inv)
|
&& !isPaid(inv)
|
||||||
&& !isPartiallyPaid(inv)
|
&& !isPartiallyPaid(inv)
|
||||||
&& !!getLastSentDate(inv)
|
&& !!getFirstSentDate(inv)
|
||||||
&& isBeforeToday(getEffectiveDueDate(inv));
|
&& isBeforeToday(getEffectiveDueDate(inv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user