-
Notifications
You must be signed in to change notification settings - Fork 2
Fetch Jobs Implementation Documentation
The "Fetch>>Jobs" functionality allows users to search for and select transport jobs that can be consolidated into a Transport Consolidation document. The implementation provides a comprehensive dialog interface with filtering, sorting, and selection capabilities.
The "Fetch>>Jobs" button is added to the Transport Consolidation form in the refresh event handler:
refresh(frm) {
// Add custom button to fetch jobs (always show dialog for manual selection)
frm.add_custom_button(__("Jobs"), function() {
fetch_consolidatable_jobs(frm);
}, __("Fetch"));
}Location: Lines 6-9
Key Points:
- Button is always visible (no conditions)
- Clicking triggers
fetch_consolidatable_jobs(frm) - Button is placed in the "Fetch" button group
The fetch_consolidatable_jobs() function makes a server call to retrieve jobs that can be consolidated:
function fetch_consolidatable_jobs(frm) {
frappe.call({
method: "logistics.transport.doctype.transport_consolidation.transport_consolidation.get_consolidatable_jobs",
args: {
consolidation_type: frm.doc.consolidation_type || null,
company: frm.doc.company || null,
date: frm.doc.consolidation_date || null,
current_consolidation: frm.doc.name && !frm.doc.__islocal ? frm.doc.name : null
},
callback: function(r) {
if (r.message && r.message.status === "success") {
show_jobs_dialog(frm, r.message.jobs, r.message.consolidation_groups, r.message.debug);
}
}
});
}Location: Lines 132-158
Parameters Sent to Server:
-
consolidation_type: Type of consolidation (Route, Pick, Drop, Both) -
company: Company filter -
date: Consolidation date filter -
current_consolidation: Current consolidation name (to exclude already added jobs)
Server Response:
-
jobs: Array of consolidatable jobs -
consolidation_groups: Grouped jobs by common addresses -
debug: Debug information for diagnostics
The show_jobs_dialog() function creates a comprehensive dialog interface:
Location: Lines 518-1242
The dialog includes the following sections:
-
Summary Info (HTML Field)
- Shows count of found jobs
- Displays consolidation groups count
- Includes collapsible diagnostics section
-
Filter Section (Collapsible)
-
filter_customer: Link field to filter by customer -
filter_pick_address: Link field to filter by pick address -
filter_drop_address: Link field to filter by drop address -
filter_consolidation_type: Select field (Route/Pick/Drop/Both) -
sort_by: Select field for sorting -
sort_order: Ascending/Descending
-
-
View Toggle
- Toggle between "Jobs" view and "Transport Legs" view
- Buttons to switch views dynamically
-
Jobs Table (HTML Field)
- Dynamic table showing jobs or legs
- Includes checkboxes for selection
- Responsive with tooltips for addresses
-
Select All Checkbox
- Checkbox to select/deselect all items at once
const dialog = new frappe.ui.Dialog({
title: __("Consolidation Suggestions"),
size: 'large',
fields: [/* field definitions */],
primary_action_label: has_jobs ? __("Add Selected Jobs") : __("Close"),
primary_action: function(values) {
// Handle job selection and addition
}
});Key Features:
- Large dialog size (90% width, max 1400px)
- Dynamic primary action label
- Stores dialog reference in
frm._consolidation_dialogfor synchronization
The dialog maintains several data structures:
let all_jobs = jobs || []; // Original jobs list from server
let filtered_jobs = jobs || []; // Currently filtered list
let selected_jobs = []; // User-selected jobs
let current_view = "jobs"; // Current view: "jobs" or "legs"Dialog-level storage:
-
dialog._jobs_data.all_jobs: All consolidatable jobs -
dialog._jobs_data.filtered_jobs: Filtered jobs -
dialog._all_legs: All transport legs (when in legs view) -
dialog._filtered_legs: Filtered legs -
dialog._legs_job_names: Job names for which legs were fetched
The dialog supports two views:
-
Jobs View (Default)
- Shows transport jobs with consolidation information
- Displays: Job name, Customer, Scheduled Date, Load Type, Pick/Drop Addresses, Type
-
Legs View
- Shows individual transport legs
- Displays: Leg name, Job, Customer, Date, Vehicle Type, Load Type, Pick/Drop Addresses, Status
- Fetches ALL consolidatable legs system-wide (not just from jobs in Jobs View)
- Legs are fetched on-demand when switching to this view
- Independent view - filters apply directly to legs, not to jobs first
Switch Function: switch_view(view_type) - Lines 1044-1137
The filter_jobs() function handles filtering for both views:
Location: Lines 848-1038
Filter Logic:
For Jobs View:
- Filters by customer (exact match)
- Filters by pick address (checks if job has matching pick address in array)
- Filters by drop address (checks if job has matching drop address in array)
- Consolidation type filtering is done server-side
For Legs View:
- Filters legs directly by customer, pick address, drop address
- No job-level filtering - filters apply directly to legs
- Fetches ALL consolidatable legs if not already cached (when
fetch_all=true) - Applies filters to the legs array
- Independent of Jobs View - shows all consolidatable legs system-wide
Sorting:
- Uses
sort_jobs()for jobs view (Lines 1324-1394) - Uses
sort_legs()for legs view (Lines 1244-1322) - Supports multiple sort fields: Job, Customer, Date, Vehicle Type, Load Type, Addresses, Type/Status
- Vehicle Type sorting available in both views (mapped appropriately for legs)
Location: Lines 1142-1241
Event handlers are set up for:
- Link fields (customer, pick/drop addresses): Multiple event listeners to catch all changes
- Select fields (consolidation_type, sort_by, sort_order): Standard change events
- Consolidation type changes trigger server-side re-fetch via
reload_jobs_with_filter()
Special Handling:
- Consolidation type filter changes update the form field and trigger server re-fetch
- Prevents recursive updates with
_updating_from_dialogflag
Function: build_jobs_table_html(jobs, consolidation_groups) - Lines 1514-1744
Features:
- Responsive table with fixed layout
- Tooltips for Route consolidation type showing all addresses
- Consolidation type badges (Pick/Drop/Both/Route)
- Partial consolidation status indicator
- Checkboxes for selection with
data-job-nameattribute - Links to job documents
Address Tooltips:
- For Route consolidation or multiple addresses
- Shows numbered list of all pick/drop addresses
- Positioned dynamically on hover
Function: build_legs_table_html(legs) - Lines 1396-1512
Features:
- Similar structure to jobs table
- Shows leg name, job name, customer, date, vehicle type, load type, addresses, status
- Status badges with color coding
- Checkboxes with both
data-job-nameanddata-leg-nameattributes
Columns:
- Checkbox (40px) - Selection checkbox
- Leg (9%) - Transport Leg name (link)
- Job (9%) - Transport Job name (link)
- Customer (10%) - Customer name
- Date (9%) - Leg date (formatted)
- Vehicle Type (9%) - Vehicle type for the leg
- Load Type (9%) - Load type from parent job
- Pick Address (15%) - Pick address (with tooltip)
- Drop Address (15%) - Drop address (with tooltip)
- Status (7%) - Leg status badge
Select All Functionality:
- Header checkbox in table:
.select-all-checkbox - Dialog field checkbox:
select_allfield - Both are synchronized
Individual Checkboxes:
- Class:
.job-checkbox - Data attributes:
data-job-name(anddata-leg-namefor legs) - Change events update select-all state
Update Function: update_select_all_state() - Lines 1231-1240
When "Add Selected Jobs" is clicked:
const checkboxes = dialog.$wrapper.find('.job-checkbox:checked');
selected_jobs = [];
const selected_job_names = new Set();
checkboxes.each(function() {
const job_name = $(this).data('job-name');
if (job_name) {
selected_job_names.add(job_name);
}
});
selected_jobs = Array.from(selected_job_names);Location: Lines 757-769
Key Points:
- Collects unique job names from checked checkboxes
- Works for both jobs and legs views (legs have
data-job-name) - Validates that at least one job is selected
Function: add_jobs_to_consolidation(frm, job_names, dialog) - Lines 1746-1775
Process:
- Validates document is saved (not local)
- Makes server call to add jobs
- Shows success/error alerts
- Closes dialog and reloads form
Server Method:
method: "logistics.transport.doctype.transport_consolidation.transport_consolidation.add_jobs_to_consolidation"
args: {
consolidation_name: frm.doc.name,
job_names: job_names
}Function: fetch_legs_for_jobs(job_names, callback, company, fetch_all) - Lines 160-202
Parameters:
-
job_names: Array of job names (ignored iffetch_all=true) -
callback: Function to call with results -
company: Optional company filter -
fetch_all: Iftrue, fetches ALL consolidatable legs system-wide
Process:
- If
fetch_all=true:- Fetches ALL consolidatable legs (not tied to specific jobs)
- Filters by company if provided
- Returns all legs that meet consolidation criteria
- If
fetch_all=false(default):- Validates and cleans job names
- Fetches legs for the given job names only
- Makes server call to get consolidatable legs
- Returns legs array via callback
Server Method:
method: "logistics.transport.doctype.transport_consolidation.transport_consolidation.get_consolidatable_legs"
args: {
job_names: job_names, // Ignored if fetch_all=true
company: company, // Optional company filter
fetch_all: fetch_all // Boolean: fetch all consolidatable legs
}When fetching all consolidatable legs, the system applies these criteria:
-
Leg Requirements:
- Legs without
run_sheetassigned - Legs with both
pick_addressanddrop_address - Legs that are not cancelled (
docstatus < 2)
- Legs without
-
Job Requirements:
- Jobs must be submitted (
docstatus = 1) - Jobs must have Load Type with
can_handle_consolidation = 1 - Fallback: Jobs with
consolidate = 1flag (for backward compatibility)
- Jobs must be submitted (
-
Company Filter:
- Optional: Filter legs by company (via transport_job relationship)
When switching to legs view:
- Checks if legs are already cached
- If not, fetches ALL consolidatable legs (not just from jobs in Jobs View)
- Applies current filters directly to legs (customer, pick address, drop address)
- Renders legs table
Key Change:
- Before: Legs view showed legs only from jobs listed in Jobs View
- Now: Legs view shows ALL consolidatable legs system-wide (or filtered by company)
- This makes Legs View independent and more comprehensive
Caching Strategy:
- Legs are cached in
dialog._all_legs - Cache is cleared when jobs are reloaded
- When
fetch_all=true, legs are fetched once and cached
Python Method: get_consolidatable_legs(job_names, company, fetch_all) - Lines 1964-2065
Parameters:
-
job_names: List of Transport Job names (ignored iffetch_all=True) -
company: Optional company filter -
fetch_all: Boolean flag to fetch all consolidatable legs
When fetch_all=True:
-
Builds filters for consolidatable legs:
-
docstatus < 2(not cancelled) -
pick_address != ""(must have pick address) -
drop_address != ""(must have drop address) -
run_sheetis empty/null (not assigned to run sheet) - Optional:
transport_jobin company jobs (if company provided)
-
-
Fetches all matching legs from database
-
Validates each leg's parent job:
- Job must be submitted (
docstatus = 1) - Job's Load Type must have
can_handle_consolidation = 1 - Fallback: Job has
consolidate = 1flag
- Job must be submitted (
-
Enriches legs with:
- Job information (customer, load_type, scheduled_date, company)
- Address titles (pick_address_title, drop_address_title)
When fetch_all=False (default):
- Fetches legs for the given
job_namesonly - Applies same validation and enrichment
Return Format:
{
"status": "success",
"legs": [
{
"name": "LEG-00001",
"transport_job": "TRJ-00001",
"customer": "Customer A",
"load_type": "FTL",
"vehicle_type": "Truck",
"pick_address": "ADDR-001",
"pick_address_title": "Warehouse A",
"drop_address": "ADDR-002",
"drop_address_title": "Warehouse B",
"status": "Open",
"date": "2025-01-15",
# ... other fields
}
]
}The form and dialog stay synchronized for consolidation type:
Form → Dialog:
- When form field changes, dialog field updates (Lines 40-61)
- Triggers filter update if dialog is open
Dialog → Form:
- When dialog filter changes, form field updates (Lines 1179-1195)
- Triggers server re-fetch of jobs
Prevention of Recursion:
- Uses
_updating_from_dialogflag - Checks current values before updating
// Store reference
frm._consolidation_dialog = dialog;
// Clean up on close
const original_hide = dialog.hide;
dialog.hide = function() {
if (frm._consolidation_dialog === dialog) {
frm._consolidation_dialog = null;
}
original_hide.call(this);
};Location: Lines 823, 839-845
sort_jobs(jobs, sort_by, sort_order) - Lines 1324-1394
- Sorts jobs by various fields
- Handles date comparison
- Case-insensitive string comparison
sort_legs(legs, sort_by, sort_order) - Lines 1244-1322
- Maps job sort options to leg fields
- Handles date comparison
- Status-based sorting for "Type" option
setup_address_tooltips(dialog) - Lines 337-382
- Sets up tooltip positioning for addresses
- Handles scroll events
- Fixed positioning for tooltips
setup_filter_toggle(dialog) - Lines 274-335
- Sets up collapsible filter section
- Moves filter fields into collapsible container
- Handles toggle animation
setup_diagnostics_toggle(dialog) - Lines 254-272
- Sets up collapsible diagnostics section
- Handles toggle animation
update_dialog_jobs(dialog, jobs, consolidation_groups, debug_info) - Lines 384-516
- Updates jobs data in dialog
- Rebuilds table HTML
- Re-applies filters
- Updates summary and diagnostics
- Clears legs cache
reload_jobs_with_filter(frm, dialog, consolidation_type) - Lines 204-252
- Re-fetches jobs from server with new filter
- Updates dialog with new data
- Clears legs cache
- Switches back to jobs view if in legs view
Validation:
- Checks if document is saved before adding jobs
- Validates at least one job is selected
- Handles empty job/leg lists gracefully
Error Messages:
- Server errors shown via
frappe.show_alert() - User-friendly error messages
- Loading indicators during async operations
-
Loading Indicators:
- Spinner shown when fetching jobs/legs
- "Loading..." messages
-
Empty States:
- Friendly messages when no jobs/legs found
- Clear indication of filter results
-
Tooltips:
- Address tooltips for Route consolidation
- Status tooltips
- Field descriptions
-
Responsive Design:
- Wide dialog (90% width, max 1400px)
- Scrollable tables
- Fixed table layout for consistent columns
-
Diagnostics:
- Collapsible diagnostics section
- Shows filtering statistics
- Debug information for troubleshooting
User clicks "Fetch>>Jobs"
↓
fetch_consolidatable_jobs()
↓
Server: get_consolidatable_jobs()
↓
show_jobs_dialog() with jobs data
↓
Dialog renders with filters and table
↓
User applies filters/sorting
↓
filter_jobs() updates table
↓
User selects jobs and clicks "Add Selected Jobs"
↓
add_jobs_to_consolidation()
↓
Server: add_jobs_to_consolidation()
↓
Form reloads with new jobs
-
Always Show Dialog:
- Even when no jobs found, dialog shows for transparency
- Allows users to see why no jobs match
-
Two-View System:
- Jobs view for high-level overview
- Legs view for detailed leg-level selection
- Legs view is now independent - shows ALL consolidatable legs, not just from visible jobs
- Allows discovery of consolidatable legs across the entire system
-
Client-Side Filtering:
- Fast filtering without server calls
- Server-side filtering for consolidation_type (affects which jobs are consolidatable)
-
Caching Strategy:
- Jobs cached in dialog
- Legs cached when fetched with
fetch_all=true - Legs cache cleared when jobs are reloaded
- Prevents unnecessary server calls
- When
fetch_all=true, legs are fetched once and reused
-
Bidirectional Sync:
- Form and dialog stay in sync
- Prevents user confusion
Key Changes:
- Legs View now fetches ALL consolidatable legs system-wide
- No longer dependent on jobs shown in Jobs View
- Uses
fetch_all=trueparameter to fetch all eligible legs - Filters apply directly to legs (not to jobs first)
Benefits:
- Better discovery of consolidatable legs across the system
- Independent filtering and selection
- Company-level filtering support
- More comprehensive leg selection
- Added Vehicle Type column to legs table
- Added Vehicle Type to sort options
- Displays vehicle type information for each leg
- Helps in consolidation planning
- Validates Load Type's
can_handle_consolidationflag - Falls back to job's
consolidateflag for backward compatibility - Ensures only truly consolidatable legs are shown
The Fetch>>Jobs implementation provides a comprehensive, user-friendly interface for finding and selecting transport jobs for consolidation. It balances functionality with performance through client-side filtering, intelligent caching, and a responsive UI design.
Recent enhancements have made the Legs View independent and more powerful, allowing users to discover and select consolidatable legs across the entire system, not just from currently visible jobs. The addition of Vehicle Type information and improved Load Type validation further enhances the consolidation workflow.
Getting Started
- Getting Started
- Recent Platform Updates
- CargoNext v1 — Release Notes
- CargoNext v1 — Astraea Press Release
- Document Management
- Milestone Tracking
- Customer Portal
Setup and Settings
- Logistics Settings
- Credit Management
- Default Details and Relationships
- Sea Freight Settings
- Air Freight Settings
- Transport Settings
- Warehouse Settings
- Customs Settings
Sea Freight
- Sea Freight Module
- Sea Booking
- Sea Shipment
- Sea Consolidation
- Master Bill
- Shipper
- Consignee
- Container Type
- Container Management
Air Freight
Transport
- Transport Module
- Transport Order
- Transport Job
- Transport Consolidation
- Transport Leg
- Transport Plan
- Run Sheet
- Proof of Delivery
- Transport Template
- Load Type
- Transport Order — Inter-module Field Copy
Customs
Warehousing
- Warehousing Module
- Inbound Order
- Release Order
- Transfer Order
- VAS Order
- Stocktake Order
- Warehouse Job
- Warehouse Contract
- Gate Pass
- Periodic Billing
- Storage Location
- Handling Unit Type
Pricing Center
- Sales Quote
- Sales Quote — Separate Billings and Internal Job
- Change Request
- Sales Quote – Calculation Method
Job Management
- Job Management Module
- Revenue Recognition Policy — Accounts, Dates, and Charges
- Proforma GL Entries
- WIP and Accrual Reversal on Invoicing
Sustainability
Intercompany
Special Projects
Pages
Features
Reports
Glossary