' +
'| ' + esc(p.pickup_date || "") + ' | ' +
'' + dn + ' | ' +
'' + esc(p.pickup_type || "") + ' | ' +
'' + esc(p.company_name || p.customer_number || "") + ' | ' +
'' + esc(p.contact_name || "") + ' | ' +
'' + esc((p.address_line || "") + (p.city ? ", " + p.city : "")) + ' | ' +
'' + (p.estimated_items || "—") + ' | ' +
'' + esc(p.data_status || "—") + ' | ' +
'' + esc(p.red_r2 || "—") + ' | ' +
'' + esc(p.status || "") + ' | ' +
'' + esc(p.notes || "") + ' | ' +
'' + esc(p.truck || "") + ' | ' +
'' + (p.needs_aor ? "✓" : "") + ' | ' +
'' + (p.needs_cod ? "✓" : "") + ' |
';
}).join(""));
}
function renderCalendar(calendar) {
var el = $("#pickup-calendar");
if (!calendar.length) { el.html('';
if (order) h += '
Stop #' + order + '
';
h += '
' + esc(p.company_name || p.customer_number || "Unknown") + '
';
h += '
' + esc((p.address_line || "") + (p.city ? ", " + p.city : "")) + '
';
h += '
';
if (p.estimated_items) h += '' + p.estimated_items + ' items';
if (p.data_status) h += '' + esc(p.data_status) + '';
if (p.red_r2) h += '' + esc(p.red_r2) + '';
if (p.needs_aor) h += 'AoR';
if (p.needs_cod) h += 'CoD';
h += '
';
return h;
}
$("#btn-load-routes").on("click", loadRoutes);
$("#btn-auto-route").on("click", function() {
var date = $("#route-date").val();
if (!date) { frappe.msgprint("Select a date first"); return; }
frappe.call({
method: "westech_r2.api.receiving_api.auto_route",
args: { date: date },
callback: function(r) {
if (r.message && r.message.success) {
frappe.show_alert({ message: "Routes optimized", indicator: "green" });
loadRoutes();
}
}
});
});
$("#btn-route-sheet").on("click", function() {
var date = $("#route-date").val() || frappe.datetime.nowdate();
window.open("/api/method/westech_r2.api.receiving_api.print_route_sheet?date=" + date, "_blank");
});
$("#btn-green-sheet").on("click", function() {
var date = $("#route-date").val() || frappe.datetime.nowdate();
window.open("/api/method/westech_r2.api.receiving_api.print_green_sheet?date=" + date, "_blank");
});
$("#btn-labels").on("click", function() {
var date = $("#route-date").val() || frappe.datetime.nowdate();
window.open("/api/method/westech_r2.api.receiving_api.print_labels?date=" + date, "_blank");
});
// ── Stage C: Check-in ──
var checkin_pickup_control = null;
var current_pickup_details = null;
function loadCheckins() {
frappe.call({
method: "westech_r2.api.receiving_api.get_checkins",
callback: function(r) {
if (r.message) renderCheckinTable(r.message.checkins || []);
}
});
}
function renderCheckinTable(checkins) {
var tbody = $("#checkin-tbody");
if (!checkins.length) {
tbody.html('' +
'| ' + esc(c.incoming_date || "") + ' | ' +
'' + esc(c.customer_name || c.customer || "") + ' | ' +
'' + esc(c.name || "") + ' | ' +
'' + (c.pallet_count || 0) + ' | ' +
'' + (c.total_weight || "—") + ' | ' +
'' + esc(c.data_status || "") + ' | ' +
'' + esc(c.red_r2 || "—") + ' | ' +
'' + palletInfo + ' |
';
}).join(""));
}
$("#btn-new-checkin").on("click", function() {
$("#checkin-form").show();
$("#ci-received_date").val(frappe.datetime.nowdate());
$("#ci-pickup-details").hide();
current_pickup_details = null;
checkin_pickup_control = frappe.ui.form.make_control({
parent: $("#ci-pickup-control"),
df: {
fieldtype: "Link",
fieldname: "pickup_ref",
options: "Scheduled Pickup",
label: "Scheduled Pickup",
reqd: 1,
placeholder: "Search pickup...",
get_query: function() {
return {
filters: [
["Scheduled Pickup", "status", "in", ["Scheduled", "Routed", "In Progress"]]
]
};
},
onchange: function() {
var val = checkin_pickup_control ? checkin_pickup_control.get_value() : "";
if (val) loadPickupDetails(val);
else {
$("#ci-pickup-details").hide();
current_pickup_details = null;
}
}
},
only_input: true,
});
checkin_pickup_control.refresh();
$("#ci-pickup-control .control-input").css("margin", "0");
$("#ci-pickup-control .help-box").remove();
});
function loadPickupDetails(pickup_name) {
frappe.call({
method: "westech_r2.api.receiving_api.get_pickup_details",
args: { pickup_name: pickup_name },
callback: function(r) {
if (!r.message) return;
current_pickup_details = r.message;
var d = r.message;
$("#ci-pickup-details").show();
$("#ci-customer-info").text((d.company_name || d.customer_number || "Unknown") + (d.red_r2 ? " — " + d.red_r2 : ""));
$("#ci-address-info").text((d.address_line || "") + (d.city ? ", " + d.city : "") + (d.state ? ", " + d.state : "") + (d.zip_code ? " " + d.zip_code : ""));
$("#ci-contact-info").text((d.contact_name || "") + (d.contact_phone ? " • " + d.contact_phone : "") + (d.contact_email ? " • " + d.contact_email : ""));
// Special handling for RED/NIST
if (d.red_r2 && d.red_r2 !== "Neither" && d.red_r2 !== "") {
$("#ci-special-handling").show().html("