561 lines
26 KiB
JavaScript
561 lines
26 KiB
JavaScript
frappe.pages['intake'].on_page_load = function(wrapper) {
|
|
try {
|
|
var page = frappe.ui.make_app_page({
|
|
parent: wrapper,
|
|
title: 'Customer Management',
|
|
single_column: true
|
|
});
|
|
|
|
page.set_primary_action('New Customer', function() {
|
|
show_intake_form();
|
|
}, 'add');
|
|
|
|
page.add_inner_button('Refresh', function() {
|
|
load_recent_pallets();
|
|
});
|
|
|
|
$(wrapper).find('.layout-main-section').html(`
|
|
<div class="intake-station" style="padding: 20px;">
|
|
<div id="intake-form-container" style="display:none;">
|
|
<div class="card" style="margin-bottom: 20px;">
|
|
<div class="card-header" style="background: linear-gradient(135deg, #6f42c1, #28a745); color: white; padding: 15px;">
|
|
<h4 style="margin:0; color: white;">New Customer</h4>
|
|
</div>
|
|
<div class="card-body" style="padding: 20px;">
|
|
<form id="intake-form">
|
|
<div class="row">
|
|
<div class="col-md-4">
|
|
<h5 style="color:#6f42c1;">Customer</h5>
|
|
<div class="form-group">
|
|
<label>Customer <span class="text-danger">*</span></label>
|
|
<div id="customer-number-control"></div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Company Name</label>
|
|
<input type="text" id="company_name" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Driver</label>
|
|
<div id="driver-control"></div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Contact Name</label>
|
|
<input type="text" id="contact_name" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Contact #</label>
|
|
<input type="tel" id="contact_number" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Contact Email</label>
|
|
<input type="email" id="contact_email" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Address</label>
|
|
<input type="text" id="address_line" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h5 style="color:#6f42c1;">Dates & Source</h5>
|
|
<div class="form-group">
|
|
<label>Received Date <span class="text-danger">*</span></label>
|
|
<input type="date" id="received_date" class="form-control" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Weekday</label>
|
|
<input type="text" id="weekday" class="form-control" readonly style="background:#f8f9fa;">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Pickup / Drop-off</label>
|
|
<select id="pickup" class="form-control">
|
|
<option value="">—</option>
|
|
<option value="Pickup">Pickup</option>
|
|
<option value="Drop-off">Drop-off</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Hours of Operation</label>
|
|
<input type="text" id="hours_of_operation" class="form-control" readonly style="background:#f8f9fa;" placeholder="Auto-filled from Customer">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Data Status</label>
|
|
<select id="data_status" class="form-control">
|
|
<option value="">—</option>
|
|
<option value="D0">D0 (Unknown)</option>
|
|
<option value="D1">D1 (Contains Data)</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>RED / R2</label>
|
|
<select id="red_r2" class="form-control">
|
|
<option value="">—</option>
|
|
<option value="RED">RED</option>
|
|
<option value="R2">R2</option>
|
|
<option value="Both">Both</option>
|
|
<option value="Clear">Clear</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Notes</label>
|
|
<textarea id="notes" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Legacy Notes</label>
|
|
<textarea id="legacy_notes" class="form-control" rows="2" readonly style="background:#fafafa;" title="Auto-filled from Customer record"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h5 style="color:#6f42c1;">Items & Weight</h5>
|
|
<div class="form-group">
|
|
<label>Barcode</label>
|
|
<input type="text" id="barcode" class="form-control" placeholder="Scan barcode...">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Total Items</label>
|
|
<input type="number" id="total_items" class="form-control" value="0" min="0">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Number of Labels</label>
|
|
<input type="number" id="num_labels" class="form-control" value="1" min="1" max="20">
|
|
</div>
|
|
<hr>
|
|
<div class="form-group">
|
|
<label>Weight <span class="text-danger">*</span></label>
|
|
<input type="text" id="weights" class="form-control" placeholder="e.g. 340 lbs" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Invoice / Check Request</label>
|
|
<input type="text" id="invoice_check_request" class="form-control">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Amount</label>
|
|
<input type="number" id="amount" class="form-control" step="0.01" value="0">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Paid / Received</label>
|
|
<select id="paid_received" class="form-control">
|
|
<option value="">—</option>
|
|
<option value="Paid">Paid</option>
|
|
<option value="Received">Received</option>
|
|
<option value="Pending">Pending</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row" style="margin-top: 20px;">
|
|
<div class="col-md-12">
|
|
<button type="submit" class="btn btn-primary btn-lg" style="background: linear-gradient(135deg, #6f42c1, #28a745); border: none;">
|
|
Save Customer
|
|
</button>
|
|
<button type="button" class="btn btn-default btn-lg" id="btn-print-labels" disabled>
|
|
Print Labels
|
|
</button>
|
|
<button type="button" class="btn btn-default btn-lg" id="btn-generate-cor" disabled>
|
|
CoR/AoR
|
|
</button>
|
|
<button type="button" class="btn btn-default btn-lg" id="btn-cancel">
|
|
Cancel
|
|
</button>
|
|
<span id="save-status" class="ml-3" style="font-size: 1.2em;"></span>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="recent-pallets">
|
|
<div class="card">
|
|
<div class="card-header" style="background: #f8f9fa;">
|
|
<h5 style="margin:0;">Recent Pallets</h5>
|
|
</div>
|
|
<div class="card-body" style="padding: 0;">
|
|
<table class="table table-striped table-hover" id="pallet-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Status</th>
|
|
<th>Customer</th>
|
|
<th>Driver</th>
|
|
<th>Received</th>
|
|
<th>RED/R2</th>
|
|
<th>Items</th>
|
|
<th>Weight</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="pallet-tbody">
|
|
<tr><td colspan="8" class="text-center">Loading...</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`);
|
|
|
|
setup_link_controls();
|
|
set_today_date();
|
|
|
|
// Trigger weekday calculation after date field is set
|
|
setTimeout(function() {
|
|
var dateVal = $('#received_date').val();
|
|
if (dateVal) {
|
|
var d = new Date(dateVal + 'T12:00:00');
|
|
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
|
|
$('#weekday').val(days[d.getDay()] || '');
|
|
}
|
|
}, 200);
|
|
|
|
load_recent_pallets();
|
|
|
|
$('#received_date').on('change', function() {
|
|
var d = new Date($(this).val() + 'T12:00:00');
|
|
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
|
|
$('#weekday').val(days[d.getDay()] || '');
|
|
});
|
|
|
|
$('#intake-form').on('submit', function(e) {
|
|
e.preventDefault();
|
|
save_pallet();
|
|
});
|
|
|
|
$('#btn-cancel').on('click', function() {
|
|
$('#intake-form-container').hide();
|
|
$('#recent-pallets').show();
|
|
clear_form();
|
|
});
|
|
|
|
$('#btn-print-labels').on('click', function() {
|
|
frappe.msgprint('Label printing coming soon.');
|
|
});
|
|
|
|
$('#btn-generate-cor').on('click', function() {
|
|
generate_cor_report();
|
|
});
|
|
|
|
} catch(e) {
|
|
console.error('[INTAKE] FATAL:', e.message, e.stack);
|
|
$(wrapper).find('.layout-main-section').html(
|
|
'<div style="padding:20px;"><h3>Error Loading Page</h3><pre>' + e.message + '</pre></div>'
|
|
);
|
|
}
|
|
};
|
|
|
|
// ── Link Controls ──────────────────────────────────────────────
|
|
|
|
var customer_number_control = null;
|
|
var driver_control = null;
|
|
var selected_customer_name = null;
|
|
|
|
function setup_link_controls() {
|
|
customer_number_control = frappe.ui.form.make_control({
|
|
parent: $('#customer-number-control'),
|
|
df: {
|
|
fieldtype: 'Link',
|
|
fieldname: 'customer_number',
|
|
options: 'Customer',
|
|
label: 'Customer',
|
|
reqd: 1,
|
|
placeholder: 'Search customer...',
|
|
onchange: function() {
|
|
var val = customer_number_control.get_value();
|
|
if (val) {
|
|
selected_customer_name = val;
|
|
fetch_customer_details(val);
|
|
} else {
|
|
selected_customer_name = null;
|
|
clear_customer_fields();
|
|
}
|
|
}
|
|
},
|
|
only_input: true,
|
|
});
|
|
customer_number_control.refresh();
|
|
$('#customer-number-control .control-input').css('margin', '0');
|
|
$('#customer-number-control .help-box').remove();
|
|
|
|
driver_control = frappe.ui.form.make_control({
|
|
parent: $('#driver-control'),
|
|
df: {
|
|
fieldtype: 'Link',
|
|
fieldname: 'driver',
|
|
options: 'Customer',
|
|
label: 'Driver',
|
|
placeholder: 'Search driver...',
|
|
onchange: function() {}
|
|
},
|
|
only_input: true,
|
|
});
|
|
driver_control.refresh();
|
|
$('#driver-control .control-input').css('margin', '0');
|
|
$('#driver-control .help-box').remove();
|
|
}
|
|
|
|
function fetch_customer_details(customer_name) {
|
|
frappe.call({
|
|
method: 'frappe.client.get',
|
|
args: {doctype: 'Customer', name: customer_name},
|
|
callback: function(r) {
|
|
if (r.message) {
|
|
var c = r.message;
|
|
$('#company_name').val(c.customer_name || '');
|
|
$('#contact_name').val(c.contact_persons || '');
|
|
$('#hours_of_operation').val(c.hours_of_operation || '');
|
|
$('#legacy_notes').val(c.legacy_notes || '');
|
|
|
|
// Parse phone and email from contact_persons if not in dedicated fields
|
|
var phone = c.mobile_no || '';
|
|
var email = c.email_id || '';
|
|
var addressLine = '';
|
|
|
|
// Extract phone from contact_persons text blob
|
|
if (!phone && c.contact_persons) {
|
|
var phoneMatch = c.contact_persons.match(/\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/);
|
|
if (phoneMatch) phone = phoneMatch[0];
|
|
}
|
|
// Extract email from contact_persons text blob
|
|
if (!email && c.contact_persons) {
|
|
var emailMatch = c.contact_persons.match(/[\w.+-]+@[\w.-]+\.\w+/);
|
|
if (emailMatch) email = emailMatch[0];
|
|
}
|
|
|
|
$('#contact_number').val(phone);
|
|
$('#contact_email').val(email);
|
|
|
|
// Get address — always fetch from Address record for full street+city+state+zip
|
|
if (c.customer_primary_address) {
|
|
frappe.call({
|
|
method: 'frappe.client.get',
|
|
args: {doctype: 'Address', name: c.customer_primary_address},
|
|
callback: function(r3) {
|
|
if (r3.message) {
|
|
var addr = r3.message;
|
|
var line = (addr.address_line1 || '') + (addr.address_line2 ? ', ' + addr.address_line2 : '');
|
|
var full = line + (addr.city ? ', ' + addr.city : '') + (addr.state ? ', ' + addr.state : '') + (addr.pincode ? ' ' + addr.pincode : '');
|
|
$('#address_line').val(full);
|
|
}
|
|
}
|
|
});
|
|
} else if (c.primary_address) {
|
|
// Fallback: parse text blob (less reliable)
|
|
var lines = c.primary_address.split('\n');
|
|
addressLine = lines.join(', ');
|
|
$('#address_line').val(addressLine);
|
|
}
|
|
|
|
// If still missing phone/email, try linked Contact
|
|
if ((!phone || !email) && c.customer_primary_contact) {
|
|
frappe.call({
|
|
method: 'frappe.client.get',
|
|
args: {doctype: 'Contact', name: c.customer_primary_contact},
|
|
callback: function(r2) {
|
|
if (r2.message) {
|
|
var ct = r2.message;
|
|
if (!phone) $('#contact_number').val(ct.phone || ct.mobile_no || '');
|
|
if (!email) $('#contact_email').val(ct.email_id || '');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function clear_customer_fields() {
|
|
$('#company_name').val('');
|
|
$('#contact_name').val('');
|
|
$('#contact_number').val('');
|
|
$('#contact_email').val('');
|
|
$('#address_line').val('');
|
|
$('#hours_of_operation').val('');
|
|
}
|
|
|
|
function clear_form() {
|
|
if (customer_number_control) customer_number_control.set_value('');
|
|
if (driver_control) driver_control.set_value('');
|
|
$('#company_name').val('');
|
|
$('#received_date').val('');
|
|
$('#weekday').val('');
|
|
$('#pickup').val('');
|
|
$('#data_status').val('');
|
|
$('#red_r2').val('');
|
|
$('#barcode').val('');
|
|
$('#total_items').val(0);
|
|
$('#num_labels').val(1);
|
|
$('#contact_name').val('');
|
|
$('#contact_number').val('');
|
|
$('#contact_email').val('');
|
|
$('#address_line').val('');
|
|
$('#hours_of_operation').val('');
|
|
$('#legacy_notes').val('');
|
|
$('#weights').val('');
|
|
$('#invoice_check_request').val('');
|
|
$('#amount').val(0);
|
|
$('#paid_received').val('');
|
|
$('#notes').val('');
|
|
$('#btn-print-labels').prop('disabled', true);
|
|
$('#btn-generate-cor').prop('disabled', true);
|
|
$('#save-status').html('');
|
|
}
|
|
|
|
function set_today_date() {
|
|
var today = new Date().toISOString().split('T')[0];
|
|
$('#received_date').val(today);
|
|
$('#received_date').trigger('change');
|
|
}
|
|
|
|
function show_intake_form() {
|
|
$('#intake-form-container').show();
|
|
$('#recent-pallets').hide();
|
|
}
|
|
|
|
function load_recent_pallets() {
|
|
frappe.call({
|
|
method: 'frappe.client.get_list',
|
|
args: {
|
|
doctype: 'Pallet',
|
|
fields: ['name', 'pallet_number', 'status', 'customer_number', 'company_name', 'driver', 'received_date', 'red_r2', 'total_items', 'weights', 'notes'],
|
|
limit_page_length: 20,
|
|
order_by: 'creation desc'
|
|
},
|
|
callback: function(r) {
|
|
var tbody = $('#pallet-tbody');
|
|
tbody.empty();
|
|
if (!r.message || r.message.length === 0) {
|
|
tbody.append('<tr><td colspan="8" class="text-center">No pallets yet. Click "New Customer" to create one.</td></tr>');
|
|
return;
|
|
}
|
|
r.message.forEach(function(p) {
|
|
var status_class = {
|
|
'Received': 'info',
|
|
'Sorting': 'warning',
|
|
'Processing': 'primary',
|
|
'Complete': 'success',
|
|
'Shipped': 'default'
|
|
}[p.status] || 'default';
|
|
|
|
tbody.append(
|
|
'<tr>' +
|
|
'<td><span class="label label-' + status_class + '">' + (p.status || 'Received') + '</span></td>' +
|
|
'<td><strong>' + (p.customer_number || '') + '</strong></td>' +
|
|
'<td>' + (p.driver || '') + '</td>' +
|
|
'<td>' + (p.received_date || '') + '</td>' +
|
|
'<td>' + (p.red_r2 || '') + '</td>' +
|
|
'<td>' + (p.total_items || 0) + '</td>' +
|
|
'<td>' + (p.weights || '') + '</td>' +
|
|
'<td>' +
|
|
'<button class="btn btn-xs btn-default" onclick="edit_pallet(\'' + p.name + '\')"><i class="fa fa-edit"></i></button> ' +
|
|
'<button class="btn btn-xs btn-default" onclick="window.open(\'https://eim.diagalon.com/report/data-tracking?pallet=' + p.name + '\', \'_blank\')"><i class="fa fa-file-pdf-o"></i></button>' +
|
|
'</td>' +
|
|
'</tr>'
|
|
);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
function edit_pallet(name) {
|
|
frappe.call({
|
|
method: 'frappe.client.get',
|
|
args: {doctype: 'Pallet', name: name},
|
|
callback: function(r) {
|
|
if (r.message) {
|
|
show_intake_form();
|
|
var d = r.message;
|
|
$('#intake-form-container').data('pallet-name', name);
|
|
$('#received_date').val(d.received_date || '');
|
|
if (customer_number_control) customer_number_control.set_value(d.customer_number || '');
|
|
if (driver_control) driver_control.set_value(d.driver || '');
|
|
$('#company_name').val(d.company_name || '');
|
|
$('#pickup').val(d.pickup || '');
|
|
$('#data_status').val(d.data_status || '');
|
|
$('#red_r2').val(d.red_r2 || '');
|
|
$('#barcode').val(d.barcode || '');
|
|
$('#total_items').val(d.total_items || 0);
|
|
$('#num_labels').val(d.num_labels || 1);
|
|
$('#contact_name').val(d.contact_name || '');
|
|
$('#contact_number').val(d.contact_number || '');
|
|
$('#contact_email').val(d.contact_email || '');
|
|
$('#address_line').val(d.address_line || '');
|
|
$('#hours_of_operation').val(d.hours_of_operation || '');
|
|
$('#legacy_notes').val(d.legacy_notes || '');
|
|
$('#weights').val(d.weights || '');
|
|
$('#invoice_check_request').val(d.invoice_check_request || '');
|
|
$('#amount').val(d.amount || 0);
|
|
$('#paid_received').val(d.paid_received || '');
|
|
$('#notes').val(d.notes || '');
|
|
$('#received_date').trigger('change');
|
|
$('#btn-print-labels').prop('disabled', false);
|
|
$('#btn-generate-cor').prop('disabled', false);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function save_pallet() {
|
|
var pallet_name = $('#intake-form-container').data('pallet-name');
|
|
var doc = {
|
|
doctype: 'Pallet',
|
|
received_date: $('#received_date').val(),
|
|
customer_number: customer_number_control ? customer_number_control.get_value() : '',
|
|
driver: driver_control ? driver_control.get_value() : '',
|
|
company_name: $('#company_name').val(),
|
|
pickup: $('#pickup').val(),
|
|
data_status: $('#data_status').val(),
|
|
red_r2: $('#red_r2').val(),
|
|
barcode: $('#barcode').val(),
|
|
total_items: parseInt($('#total_items').val()) || 0,
|
|
num_labels: parseInt($('#num_labels').val()) || 1,
|
|
contact_name: $('#contact_name').val(),
|
|
contact_number: $('#contact_number').val(),
|
|
contact_email: $('#contact_email').val(),
|
|
address_line: $('#address_line').val(),
|
|
weights: $('#weights').val(),
|
|
invoice_check_request: $('#invoice_check_request').val(),
|
|
amount: parseFloat($('#amount').val()) || 0,
|
|
paid_received: $('#paid_received').val(),
|
|
notes: $('#notes').val(),
|
|
legacy_notes: $('#legacy_notes').val(),
|
|
hours_of_operation: $('#hours_of_operation').val(),
|
|
};
|
|
|
|
if (pallet_name) {
|
|
doc.name = pallet_name;
|
|
frappe.call({
|
|
method: 'frappe.client.update',
|
|
args: {doc: doc},
|
|
callback: function(r) {
|
|
if (r.message) {
|
|
frappe.msgprint('Pallet updated successfully!');
|
|
$('#save-status').html('<span style="color:green;">Saved!</span>');
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
frappe.call({
|
|
method: 'frappe.client.insert',
|
|
args: {doc: doc},
|
|
callback: function(r) {
|
|
if (r.message) {
|
|
$('#intake-form-container').data('pallet-name', r.message.name);
|
|
frappe.msgprint('Pallet created: ' + r.message.name);
|
|
$('#save-status').html('<span style="color:green;">Created!</span>');
|
|
$('#btn-print-labels').prop('disabled', false);
|
|
$('#btn-generate-cor').prop('disabled', false);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function generate_cor_report() {
|
|
var pallet_name = $('#intake-form-container').data('pallet-name');
|
|
if (!pallet_name) {
|
|
frappe.msgprint('Please save the intake first before generating CoR/AoR report.');
|
|
return;
|
|
}
|
|
window.open('https://eim.diagalon.com/report/data-tracking?pallet=' + pallet_name, '_blank');
|
|
}
|
|
|
|
window.edit_pallet = edit_pallet; |