-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOdoo_Update_Invoice_Job_Fields.py
More file actions
82 lines (70 loc) · 2.98 KB
/
Copy pathOdoo_Update_Invoice_Job_Fields.py
File metadata and controls
82 lines (70 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Odoo Server Action: Update Invoice Job Fields from Sales Order
# Model: Journal Entry (account.move)
# Action To Do: Execute Python Code
#
# Usage:
# Select Invoices in the list view (Accounting > Invoices)
# and run this action from the Action menu.
# It updates job-related fields on invoices based on their linked sales orders.
# Configuration
BATCH_SIZE = 50
# Get selected IDs from 'records' (standard for Server Actions)
all_selected_ids = records.ids
if not all_selected_ids:
raise UserError("Please select at least one Invoice to process.")
total_invoices = len(all_selected_ids)
updated_count = 0
log(f"Starting batch job field update for {total_invoices} records...", level='info')
# Process in chunks of invoice IDs
for i in range(0, total_invoices, BATCH_SIZE):
batch_invoice_ids = all_selected_ids[i:i + BATCH_SIZE]
batch_records = env['account.move'].browse(batch_invoice_ids)
for record in batch_records:
try:
# Map sales orders via invoice lines
sale_orders = record.invoice_line_ids.mapped('sale_line_ids.order_id')
if sale_orders:
# Use the first related sales order
so = sale_orders[0]
record.write({
'x_studio_job_installation_date_1': so.x_studio_related_installation,
'x_studio_job_unit_location': so.x_studio_related_unit_location,
'x_studio_job_unit_number': so.x_studio_related_unit_number,
'x_studio_job_unit_name': so.x_studio_related_job_name,
})
updated_count += 1
else:
# Clear fields if no sales order is linked
record.write({
'x_studio_job_installation_date_1': False,
'x_studio_job_unit_location': False,
'x_studio_job_unit_number': False,
'x_studio_job_unit_name': False,
})
except Exception as e:
log(f"Error updating invoice {record.name or record.id}: {str(e)}", level='error')
# Commit to free up database locks and save progress
env.cr.commit()
# Progress notification via Bus system
progress = min(i + BATCH_SIZE, total_invoices)
env['bus.bus']._sendone(env.user.partner_id, 'simple_notification', {
'title': 'Invoice Job Update',
'message': f'Processed {progress}/{total_invoices} invoices...',
'type': 'info',
'sticky': False
})
# Final summary notification
action = {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': 'Update Complete',
'message': f"Processed {total_invoices} selected invoices.\n✅ Updated {updated_count} records.",
'type': 'success',
'sticky': True
}
}
## Optimized for Selected Records
## Real-time Batch Notifications via Bus
## Pulls fields: Installation Date, Unit Location, Unit Number, Job Name
## Powered By HSx Tech - Ali Muzafar