Manually calculating and entering dates in iVendNext documents is error-prone and time-consuming. With custom scripts, you can automate date fields to:
✔ Auto-set deadlines (e.g., Production Due = Delivery Date - 2 days).
✔ Enforce business rules (e.g., prevent invalid date ranges).
✔ Sync dates across linked documents (e.g., PO → Invoice due dates).
Automation is ideal for:
Project timelines (Task Start/End Dates).
Order processing (Ship By → Delivery Date).
Financial workflows (Invoice Due = Issue Date + 30 days).
Set a Production Due Date to always be 2 days before the Delivery Date.
Ensure Fields Exist:
Add delivery_date (Date) and production_due_date (Date) to your DocType.
Add a Client Script:
// Triggered when Delivery Date changes
cur_frm.cscript.delivery_date = function(doc) {
if (doc.delivery_date) {
// Calculate 2 days before
var dueDate = ivendnext.datetime.add_days(doc.delivery_date, -2);
// Update Production Due Date
cur_frm.set_value("production_due_date", dueDate);
}
};
Test:
Enter a Delivery Date → Production Due Date auto-updates.
Set Payment Due Date to:
30 days after invoice date for "Net 30" terms.
Immediate for "Cash on Delivery".
cur_frm.cscript.payment_terms = function(doc) {
if (doc.payment_terms === "Net 30") {
var dueDate = ivendnext.datetime.add_days(doc.posting_date, 30);
cur_frm.set_value("due_date", dueDate);
} else if (doc.payment_terms === "COD") {
cur_frm.set_value("due_date", doc.posting_date); // Same day
}
};
Key Notes:
Uses ivendnext.datetime.add_days() for safe date math.
Always validate fields (if (doc.fieldname)).
When creating a Delivery Note from a Sales Order, auto-set:
delivery_date = Sales Order’s ship_by_date.
Ensure field names match in both DocTypes (ship_by_date).
Or use add_fetch for non-matching names:
// In Delivery Note script
cur_frm.add_fetch("sales_order", "ship_by_date", "delivery_date");
Test:
Create Delivery Note from Sales Order → delivery_date copies automatically.
Prevent users from selecting weekends for deadlines:
cur_frm.cscript.delivery_date = function(doc) {
var day = ivendnext.datetime.get_day(doc.delivery_date);
if (day === 0 || day === 6) { // Sunday (0) or Saturday (6)
ivendnext.msgprint("Delivery dates cannot be on weekends!");
cur_frm.set_value("delivery_date", ""); // Clear field
}
};
Auto-adjust end_date if start_date changes (e.g., fixed 7-day duration):
cur_frm.cscript.start_date = function(doc) {
if (doc.start_date) {
cur_frm.set_value("end_date", ivendnext.datetime.add_days(doc.start_date, 7));
}
};
Document Dependencies: Note which fields trigger date changes.
User Feedback: Use ivendnext.msgprint() to explain auto-updates (e.g., "Due date set to 30 days after invoice").
Test Edge Cases: Validate with blank dates, holidays, and timezone changes.
Automating date fields in iVendNext reduces manual work and ensures consistency. Whether you’re calculating deadlines, enforcing rules, or syncing documents, scripts provide flexible solutions.
✅ Use ivendnext.datetime.add_days() for safe date math.
✅ Match field names or use add_fetch to sync dates across docs.
✅ Add validation logic to block invalid entries.