Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions src/pages/invoices/common/hooks/useHandleProductChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ export function useHandleProductChange(props: Props) {
? product?.cost
: product?.price || 0;

// Additive only: only empty fields are filled, so data already on the line
// (task hours, rate, manual entries) survives selecting a service.
if (company.fill_products && !lineItem.quantity) {
// Task lines are additive only: they carry logged hours, rate and manual
// entries, so a service fills only what is still empty. Product lines take
// the selected product's values, replacing the previous product's.
const additive = props.type === 'task';

const shouldFill = (populated: boolean) => !additive || !populated;

if (company.fill_products && shouldFill(Boolean(lineItem.quantity))) {
if (!company?.enable_product_quantity) {
lineItem.quantity = 1;
} else {
Expand All @@ -66,7 +71,7 @@ export function useHandleProductChange(props: Props) {
}
}

if (company.fill_products && !lineItem.cost) {
if (company.fill_products && shouldFill(Boolean(lineItem.cost))) {
if (resource.client_id) {
// Use client from resource if available to avoid permission issues
const clientPromise =
Expand Down Expand Up @@ -106,41 +111,56 @@ export function useHandleProductChange(props: Props) {
}
}

const fillNotes = props.type !== 'product' || company.fill_products;
const fillNotes = additive || company.fill_products;

if (fillNotes && product?.notes && !lineItem.notes) {
if (fillNotes && product?.notes && shouldFill(Boolean(lineItem.notes))) {
lineItem.notes = product.notes;
}

if (!lineItem.tax_name1 && !lineItem.tax_rate1) {
if (shouldFill(Boolean(lineItem.tax_name1 || lineItem.tax_rate1))) {
lineItem.tax_name1 = product?.tax_name1 || '';
lineItem.tax_rate1 = product?.tax_rate1 || 0;
}

if (!lineItem.tax_name2 && !lineItem.tax_rate2) {
if (shouldFill(Boolean(lineItem.tax_name2 || lineItem.tax_rate2))) {
lineItem.tax_name2 = product?.tax_name2 || '';
lineItem.tax_rate2 = product?.tax_rate2 || 0;
}

if (!lineItem.tax_name3 && !lineItem.tax_rate3) {
if (shouldFill(Boolean(lineItem.tax_name3 || lineItem.tax_rate3))) {
lineItem.tax_name3 = product?.tax_name3 || '';
lineItem.tax_rate3 = product?.tax_rate3 || 0;
}

lineItem.custom_value1 ||= product?.custom_value1 || '';
lineItem.custom_value2 ||= product?.custom_value2 || '';
lineItem.custom_value3 ||= product?.custom_value3 || '';
lineItem.custom_value4 ||= product?.custom_value4 || '';
if (shouldFill(Boolean(lineItem.custom_value1))) {
lineItem.custom_value1 = product?.custom_value1 || '';
}

if (shouldFill(Boolean(lineItem.custom_value2))) {
lineItem.custom_value2 = product?.custom_value2 || '';
}

if (shouldFill(Boolean(lineItem.custom_value3))) {
lineItem.custom_value3 = product?.custom_value3 || '';
}

if (shouldFill(Boolean(lineItem.custom_value4))) {
lineItem.custom_value4 = product?.custom_value4 || '';
}

// '1' is the default tax category, so it counts as not populated.
if (!lineItem.tax_id || lineItem.tax_id === '1') {
if (shouldFill(Boolean(lineItem.tax_id && lineItem.tax_id !== '1'))) {
lineItem.tax_id = product?.tax_id || '1';
}

lineItem.product_cost ||= product?.cost;
if (shouldFill(Boolean(lineItem.product_cost))) {
lineItem.product_cost = product?.cost;
}

if (import.meta.env.VITE_DISABLE_QUICKBOOKS_INTEGRATION !== 'true') {
lineItem.income_account_id ||= product?.income_account_id || '';
if (shouldFill(Boolean(lineItem.income_account_id))) {
lineItem.income_account_id = product?.income_account_id || '';
}
}

return props.onChange(index, lineItem);
Expand Down
44 changes: 39 additions & 5 deletions tests/unit/hooks/useHandleProductChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,19 @@ describe('useHandleProductChange', () => {
);
});

test('does not overwrite taxes or custom values already on a product line', async () => {
test('replaces the previous product values when switching products on a product line', async () => {
const { onChange, handler } = handleChange(
'product',
lineItem({
product_key: 'basic_plan',
quantity: 1,
cost: 100,
notes: 'The Basic Plan',
tax_name1: 'GST',
tax_rate1: 10,
custom_value1: 'Line custom',
custom_value1: 'Basic custom',
tax_id: '2',
product_cost: 40,
})
);

Expand All @@ -138,10 +144,38 @@ describe('useHandleProductChange', () => {
expect(onChange).toHaveBeenCalledWith(
0,
expect.objectContaining({
tax_name1: 'GST',
tax_rate1: 10,
custom_value1: 'Line custom',
product_key: 'pro_plan',
quantity: 1,
cost: 12,
notes: 'The Pro Plan',
tax_name1: 'VAT',
tax_rate1: 20,
custom_value1: 'Product custom',
tax_id: '3',
product_cost: 0,
})
);
});

test('uses the product quantity on a new product line', async () => {
mocks.company.enable_product_quantity = true;
mocks.company.default_quantity = false;

try {
const { onChange, handler } = handleChange(
'product',
lineItem({ quantity: 1, cost: 0, notes: '' })
);

await handler(0, 'pro_plan', product);

expect(onChange).toHaveBeenCalledWith(
0,
expect.objectContaining({ quantity: 3, cost: 12 })
);
} finally {
mocks.company.enable_product_quantity = false;
mocks.company.default_quantity = true;
}
});
});
Loading