mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-17 18:20:36 -05:00
Blank VIN on new documents; VIN from attached estimates; read estimate VINs with newer custom fields
- New estimates and invoices start with a blank VIN; picking a customer only fills the VIN box's suggestions. - Importing an estimate whose work orders give no VIN fills the invoice's VIN box with the estimate's own VIN (open estimates now carry it). - EstimateWriter.vin_for reads an estimate's VIN with QuickBooks' newer custom fields (cached a minute). The local Estimate's details leave those out, so plugins matching the estimate's VIN to a vehicle never found one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
5 files changed
+40
-20
No files matched your search
@@ -123,7 +123,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours*
|
|||||||
* **Undo import** puts the lines back until the invoice is saved.
|
* **Undo import** puts the lines back until the invoice is saved.
|
||||||
* **An estimate is billed once**: an estimate that's on an invoice (linked to one, or closed) or was rejected can't be made into an invoice or imported, and its page shows the invoice it's billed on. This is checked against QuickBooks again when the invoice is saved, so an estimate invoiced meanwhile (in another tab, by someone else, or in QuickBooks) stops the save.
|
* **An estimate is billed once**: an estimate that's on an invoice (linked to one, or closed) or was rejected can't be made into an invoice or imported, and its page shows the invoice it's billed on. This is checked against QuickBooks again when the invoice is saved, so an estimate invoiced meanwhile (in another tab, by someone else, or in QuickBooks) stops the save.
|
||||||
* Time lines are linked to their time activities, so QuickBooks marks the time as billed. Estimates the invoice was made from, or imported into it, are linked and closed when it's saved.
|
* Time lines are linked to their time activities, so QuickBooks marks the time as billed. Estimates the invoice was made from, or imported into it, are linked and closed when it's saved.
|
||||||
* **VIN and mileage from the work orders**: the issues behind an invoice are the ones its time lines and lineitems estimates name. When they're imported, the VIN box takes their vehicle's VIN and a **Mileage** box (shown when QuickBooks has a text custom field named like "Mileage") takes the highest value of the issues' Mileage custom field, both supplied by redmine_qbo_vehicles. With redmine_qbo_vehicles, an invoice whose work orders are for more than one vehicle isn't saved.
|
* **VIN and mileage from the work orders**: the issues behind an invoice are the ones its time lines and lineitems estimates name. When they're imported, the VIN box takes their vehicle's VIN (or, for an imported estimate whose work orders give none, the estimate's own VIN) and a **Mileage** box (shown when QuickBooks has a text custom field named like "Mileage") takes the highest value of the issues' Mileage custom field, both supplied by redmine_qbo_vehicles. With redmine_qbo_vehicles, an invoice whose work orders are for more than one vehicle isn't saved.
|
||||||
* **Sales person**: an invoice credits the sales person on the estimate it was made from (the first linked estimate that names one); if none does, the user who writes the invoice.
|
* **Sales person**: an invoice credits the sales person on the estimate it was made from (the first linked estimate that names one); if none does, the user who writes the invoice.
|
||||||
* Invoices with a payment applied open read-only. The due date is left to QuickBooks (from the customer's terms) unless one is picked. The "default message shown on invoices" fills in the message.
|
* Invoices with a payment applied open read-only. The due date is left to QuickBooks (from the customer's terms) unless one is picked. The "default message shown on invoices" fills in the message.
|
||||||
|
|
||||||
@@ -224,7 +224,7 @@ Hook|:qbo_billing_issue_defaults, { estimate:, params: } | Return `start_date`,
|
|||||||
Hook|:qbo_billing_build_issue, { issue:, estimate:, defaults: } | Set your plugin's own fields on each issue created from an estimate, before it's saved (redmine_qbo_vehicles sets the vehicle: the one in `defaults`, or the customer's vehicle whose VIN is on the estimate)
|
Hook|:qbo_billing_build_issue, { issue:, estimate:, defaults: } | Set your plugin's own fields on each issue created from an estimate, before it's saved (redmine_qbo_vehicles sets the vehicle: the one in `defaults`, or the customer's vehicle whose VIN is on the estimate)
|
||||||
View|:view_qbo_estimate_issues_form, { estimate:, defaults:, project: } | Draw your plugin's fields on the Create issues from estimate page, applied to every issue. Name them `issue_plugin_fields[name]`; the posted values reach `:qbo_billing_build_issue` in `defaults` (redmine_qbo_vehicles: the Vehicle, preselected from the estimate's VIN)
|
View|:view_qbo_estimate_issues_form, { estimate:, defaults:, project: } | Draw your plugin's fields on the Create issues from estimate page, applied to every issue. Name them `issue_plugin_fields[name]`; the posted values reach `:qbo_billing_build_issue` in `defaults` (redmine_qbo_vehicles: the Vehicle, preselected from the estimate's VIN)
|
||||||
Hook|:qbo_billing_issues_created, { estimate:, issues:, params: } | Called after issues are created from an estimate
|
Hook|:qbo_billing_issues_created, { estimate:, issues:, params: } | Called after issues are created from an estimate
|
||||||
Hook|:qbo_billing_default_vin, { customer:, issues: } | Return the VIN a new estimate or invoice starts with (an invoice made from an estimate keeps the estimate's VIN; `issues` are that estimate's work orders). redmine_qbo_vehicles: the work orders' vehicle when they share one, else the customer's only vehicle
|
Hook|:qbo_billing_default_vin, { customer:, issues: } | Return the VIN for a document's work orders: used when an invoice is made from an estimate that has no VIN (`issues` are the estimate's work orders). New estimates and invoices start with a blank VIN. redmine_qbo_vehicles: the work orders' vehicle when they share one
|
||||||
Hook|:qbo_billing_vin_options, { customer: } | Return `[{ vin:, name: }, ...]` to suggest in the estimate/invoice VIN box; one suggestion fills an empty box (redmine_qbo_vehicles: the customer's vehicles)
|
Hook|:qbo_billing_vin_options, { customer: } | Return `[{ vin:, name: }, ...]` to suggest in the estimate/invoice VIN box; one suggestion fills an empty box (redmine_qbo_vehicles: the customer's vehicles)
|
||||||
View|checkboxes with `data-new-issue-param="issue[field]"` on the customer page | Ticking one adds `issue[field]=value` to the New issue link, one per field (redmine_qbo's estimates, redmine_qbo_vehicles' vehicles)
|
View|checkboxes with `data-new-issue-param="issue[field]"` on the customer page | Ticking one adds `issue[field]=value` to the New issue link, one per field (redmine_qbo's estimates, redmine_qbo_vehicles' vehicles)
|
||||||
View Hook|:view_qbo_estimate_contextual, { estimate_id:, customer_id:, vin:, doc_number: } | Adds links to an estimate page's action area (e.g. redmine_qbo_calendar's Add appointment)
|
View Hook|:view_qbo_estimate_contextual, { estimate_id:, customer_id:, vin:, doc_number: } | Adds links to an estimate page's action area (e.g. redmine_qbo_calendar's Add appointment)
|
||||||
|
|||||||
@@ -283,15 +283,12 @@ $(function () {
|
|||||||
loadedEmail = info.email || '';
|
loadedEmail = info.email || '';
|
||||||
$('#qbo-estimate-address').text(info.billing_address || '');
|
$('#qbo-estimate-address').text(info.billing_address || '');
|
||||||
|
|
||||||
// VINs plugins suggest for this customer (e.g. their vehicles); one fills an empty VIN box
|
// VINs plugins suggest for this customer (e.g. their vehicles), offered in the VIN box's list; the box
|
||||||
var options = info.vin_options || [];
|
// itself stays as it is (a new document starts blank)
|
||||||
var $list = $('#qbo-estimate-vins').empty();
|
var $list = $('#qbo-estimate-vins').empty();
|
||||||
options.forEach(function (option) {
|
(info.vin_options || []).forEach(function (option) {
|
||||||
$('<option>').attr('value', option.vin).text(option.name).appendTo($list);
|
$('<option>').attr('value', option.vin).text(option.name).appendTo($list);
|
||||||
});
|
});
|
||||||
if ($vin.length && !$vin.prop('disabled') && !$vin.val().trim() && options.length === 1) {
|
|
||||||
$vin.val(options[0].vin);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -383,17 +380,26 @@ $(function () {
|
|||||||
return ids;
|
return ids;
|
||||||
};
|
};
|
||||||
|
|
||||||
// VIN and mileage from the work orders (via plugins such as redmine_qbo_vehicles):
|
// VIN and mileage from the work orders (via plugins such as redmine_qbo_vehicles): one vehicle fills the
|
||||||
// one vehicle fills the VIN box; the highest mileage fills the mileage box
|
// VIN box; the highest mileage fills the mileage box. Imported estimates whose work orders give no VIN
|
||||||
var updateVehicleFields = function (issueIds) {
|
// add their own.
|
||||||
|
var updateVehicleFields = function (issueIds, importedEstimates) {
|
||||||
var values = (importData && importData.issue_values) || {};
|
var values = (importData && importData.issue_values) || {};
|
||||||
var vins = [], mileage = null;
|
var vins = [], mileage = null;
|
||||||
|
var addVin = function (vin) {
|
||||||
|
vin = (vin || '').trim().toUpperCase();
|
||||||
|
if (vin && vins.indexOf(vin) < 0) vins.push(vin);
|
||||||
|
};
|
||||||
issueIds.forEach(function (id) {
|
issueIds.forEach(function (id) {
|
||||||
var v = values[id] || {};
|
var v = values[id] || {};
|
||||||
if (v.vin && vins.indexOf(v.vin.toUpperCase()) < 0) vins.push(v.vin.toUpperCase());
|
addVin(v.vin);
|
||||||
var miles = parseNumber(v.mileage);
|
var miles = parseNumber(v.mileage);
|
||||||
if (miles !== null && !isNaN(miles) && (mileage === null || miles > mileage)) mileage = miles;
|
if (miles !== null && !isNaN(miles) && (mileage === null || miles > mileage)) mileage = miles;
|
||||||
});
|
});
|
||||||
|
(importedEstimates || []).forEach(function (estimate) {
|
||||||
|
var fromIssue = estimate.issue_id && (values[String(estimate.issue_id)] || {}).vin;
|
||||||
|
if (!fromIssue) addVin(estimate.vin);
|
||||||
|
});
|
||||||
var $vinBox = $('#qbo_estimate_vin');
|
var $vinBox = $('#qbo_estimate_vin');
|
||||||
if (vins.length === 1 && $vinBox.length && !$vinBox.prop('disabled')) $vinBox.val(vins[0]);
|
if (vins.length === 1 && $vinBox.length && !$vinBox.prop('disabled')) $vinBox.val(vins[0]);
|
||||||
var $mileageBox = $('#qbo_estimate_mileage');
|
var $mileageBox = $('#qbo_estimate_mileage');
|
||||||
@@ -540,7 +546,7 @@ $(function () {
|
|||||||
estimates.forEach(function (estimate) {
|
estimates.forEach(function (estimate) {
|
||||||
if (estimate.issue_id && issueIds.indexOf(String(estimate.issue_id)) < 0) issueIds.push(String(estimate.issue_id));
|
if (estimate.issue_id && issueIds.indexOf(String(estimate.issue_id)) < 0) issueIds.push(String(estimate.issue_id));
|
||||||
});
|
});
|
||||||
var vins = updateVehicleFields(issueIds);
|
var vins = updateVehicleFields(issueIds, estimates);
|
||||||
$panel.prop('hidden', true);
|
$panel.prop('hidden', true);
|
||||||
if (vins.length > 1) window.alert($panel.data('manyVehiclesText') + ' ' + vins.join(', '));
|
if (vins.length > 1) window.alert($panel.data('manyVehiclesText') + ' ' + vins.join(', '));
|
||||||
$('#qbo-import-undo').prop('hidden', false);
|
$('#qbo-import-undo').prop('hidden', false);
|
||||||
|
|||||||
@@ -43,10 +43,11 @@ module RedmineQboBilling
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
# The VIN a new estimate or invoice starts with, from plugins answering
|
# The VIN for a document's work orders, from plugins answering
|
||||||
# :qbo_billing_default_vin, { customer:, issues: } => a VIN or nil (e.g. redmine_qbo_vehicles: the work
|
# :qbo_billing_default_vin, { customer:, issues: } => a VIN or nil (e.g. redmine_qbo_vehicles: the work
|
||||||
# orders' vehicle when they share one, else the customer's only vehicle)
|
# orders' vehicle when they share one)
|
||||||
# issues are the work orders behind the document (e.g. the issues linked to the estimate it's made from).
|
# Used for an invoice made from an estimate that has no VIN (issues: the estimate's work orders). New
|
||||||
|
# estimates and invoices start with a blank VIN.
|
||||||
def self.default_vin(customer, issues: [])
|
def self.default_vin(customer, issues: [])
|
||||||
return nil unless customer
|
return nil unless customer
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ module RedmineQboBilling
|
|||||||
customer_name: customer&.name,
|
customer_name: customer&.name,
|
||||||
email: customer_email(customer),
|
email: customer_email(customer),
|
||||||
billing_address: customer_address(customer),
|
billing_address: customer_address(customer),
|
||||||
vin: RedmineQboBilling.default_vin(customer),
|
|
||||||
txn_date: user.today.iso8601,
|
txn_date: user.today.iso8601,
|
||||||
expiration_date: default_expiration_date(user.today)&.iso8601,
|
expiration_date: default_expiration_date(user.today)&.iso8601,
|
||||||
sales_person: user.name,
|
sales_person: user.name,
|
||||||
@@ -143,6 +142,20 @@ module RedmineQboBilling
|
|||||||
saved
|
saved
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The VIN on an estimate, read with QuickBooks' newer custom fields (a plain fetch, e.g. Estimate#details,
|
||||||
|
# leaves them out). Cached for a minute, since creating issues asks once per issue.
|
||||||
|
def vin_for(estimate_id)
|
||||||
|
return nil if estimate_id.blank?
|
||||||
|
|
||||||
|
Rails.cache.fetch("redmine_qbo:estimate_vin:#{estimate_id}", expires_in: 1.minute) do
|
||||||
|
remote = QboEstimateService.with_service { |service| service.fetch_by_id(estimate_id) }
|
||||||
|
remote ? custom_value(remote, Company.vin_field, Company::VIN_NAME).to_s.strip : ''
|
||||||
|
end.presence
|
||||||
|
rescue StandardError => e
|
||||||
|
Rails.logger.warn "[RedmineQboBilling] Could not read estimate #{estimate_id}'s VIN: #{e.message}"
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
# Emails the estimate; QuickBooks uses its own estimate email template
|
# Emails the estimate; QuickBooks uses its own estimate email template
|
||||||
def send_email(id, email)
|
def send_email(id, email)
|
||||||
sent = QboEstimateService.with_service { |service| service.send_estimate(id, email) }
|
sent = QboEstimateService.with_service { |service| service.send_estimate(id, email) }
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ module RedmineQboBilling
|
|||||||
customer_name: customer&.name,
|
customer_name: customer&.name,
|
||||||
email: writer.customer_email(customer),
|
email: writer.customer_email(customer),
|
||||||
billing_address: writer.customer_address(customer),
|
billing_address: writer.customer_address(customer),
|
||||||
vin: RedmineQboBilling.default_vin(customer),
|
|
||||||
txn_date: user.today.iso8601,
|
txn_date: user.today.iso8601,
|
||||||
sales_person: user.name,
|
sales_person: user.name,
|
||||||
customer_memo: Company.invoice_message,
|
customer_memo: Company.invoice_message,
|
||||||
@@ -73,8 +72,7 @@ module RedmineQboBilling
|
|||||||
form.billing_address = source.billing_address.presence || form.billing_address
|
form.billing_address = source.billing_address.presence || form.billing_address
|
||||||
# The estimate's VIN, or else a plugin's for the estimate's work orders (e.g. their vehicle)
|
# The estimate's VIN, or else a plugin's for the estimate's work orders (e.g. their vehicle)
|
||||||
form.vin = source.vin.presence ||
|
form.vin = source.vin.presence ||
|
||||||
RedmineQboBilling.default_vin(customer, issues: Issue.where(estimate_id: source.id.to_s).to_a) ||
|
RedmineQboBilling.default_vin(customer, issues: Issue.where(estimate_id: source.id.to_s).to_a)
|
||||||
form.vin
|
|
||||||
form.mileage = source.mileage
|
form.mileage = source.mileage
|
||||||
# The estimate's writer sells the job; with no one on the estimate, whoever writes the invoice
|
# The estimate's writer sells the job; with no one on the estimate, whoever writes the invoice
|
||||||
form.sales_person = source.sales_person.presence || user.name
|
form.sales_person = source.sales_person.presence || user.name
|
||||||
@@ -363,6 +361,8 @@ module RedmineQboBilling
|
|||||||
issue: linked_issue&.then { |i| "#{i.tracker} ##{i.id}: #{i.subject}" },
|
issue: linked_issue&.then { |i| "#{i.tracker} ##{i.id}: #{i.subject}" },
|
||||||
# Lines from this estimate go next to the invoice's time for the same issue
|
# Lines from this estimate go next to the invoice's time for the same issue
|
||||||
issue_id: issue_reference(lines.detect { |l| l.description.present? }&.description) || linked_issue&.id&.to_s,
|
issue_id: issue_reference(lines.detect { |l| l.description.present? }&.description) || linked_issue&.id&.to_s,
|
||||||
|
# The estimate's own VIN, used when its work orders give none
|
||||||
|
vin: writer.custom_value(e, Company.vin_field, Company::VIN_NAME).to_s.strip,
|
||||||
lines: lines.map { |l| line_hash(l) }
|
lines: lines.map { |l| line_hash(l) }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user