Skip to content

Commit

Permalink
feat: allow quantity of entries accept decimal value (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia authored Nov 13, 2024
1 parent 908bbb9 commit 5d6f901
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/api/controllers/Purchases/Bills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class BillsController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/controllers/Purchases/VendorCredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class VendorCreditController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down Expand Up @@ -209,7 +209,7 @@ export default class VendorCreditController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/api/controllers/Sales/CreditNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default class PaymentReceivesController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down Expand Up @@ -755,9 +755,8 @@ export default class PaymentReceivesController extends BaseController {
const { tenantId } = req;

try {
const data = await this.getCreditNoteStateService.getCreditNoteState(
tenantId
);
const data =
await this.getCreditNoteStateService.getCreditNoteState(tenantId);
return res.status(200).send({ data });
} catch (error) {
next(error);
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/api/controllers/Sales/SalesEstimates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class SalesEstimatesController extends BaseController {
check('entries').exists().isArray({ min: 1 }),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.description').optional({ nullable: true }).trim(),
check('entries.*.discount')
Expand Down Expand Up @@ -562,9 +562,8 @@ export default class SalesEstimatesController extends BaseController {
const { tenantId } = req;

try {
const data = await this.saleEstimatesApplication.getSaleEstimateState(
tenantId
);
const data =
await this.saleEstimatesApplication.getSaleEstimateState(tenantId);
return res.status(200).send({ data });
} catch (error) {
next(error);
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/api/controllers/Sales/SalesReceipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class SalesReceiptsController extends BaseController {
check('entries.*.id').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
Expand Down Expand Up @@ -392,9 +392,8 @@ export default class SalesReceiptsController extends BaseController {

// Retrieves receipt in pdf format.
try {
const data = await this.saleReceiptsApplication.getSaleReceiptState(
tenantId
);
const data =
await this.saleReceiptsApplication.getSaleReceiptState(tenantId);
return res.status(200).send({ data });
} catch (error) {
next(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema
.table('items_entries', (table) => {
table.decimal('quantity', 13, 3).alter();
})
.table('inventory_transactions', (table) => {
table.decimal('quantity', 13, 3).alter();
})
.table('inventory_cost_lot_tracker', (table) => {
table.decimal('quantity', 13, 3).alter();
})
.table('items', (table) => {
table.decimal('quantityOnHand', 13, 3).alter();
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema
.table('items_entries', (table) => {
table.integer('quantity').alter();
})
.table('inventory_transactions', (table) => {
table.integer('quantity').alter();
})
.table('inventory_cost_lot_tracker', (table) => {
table.integer('quantity').alter();
})
.table('items', (table) => {
table.integer('quantityOnHand').alter();
});
};
41 changes: 23 additions & 18 deletions packages/webapp/src/components/DataTableCells/NumericInputCell.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-nocheck
import React, { useState, useEffect } from 'react';
import { FormGroup, NumericInput, Intent } from '@blueprintjs/core';
import classNames from 'classnames';

import { CellType } from '@/constants';
import { CLASSES } from '@/constants/classes';

Expand All @@ -12,37 +10,44 @@ import { CLASSES } from '@/constants/classes';
export default function NumericInputCell({
row: { index },
column: { id },
cell: { value: initialValue },
cell: { value: controlledInputValue },
payload,
}) {
const [value, setValue] = useState(initialValue);

const handleValueChange = (newValue) => {
setValue(newValue);
}: any) {
const [valueAsNumber, setValueAsNumber] = useState<number | null>(
controlledInputValue || null,
);
const handleInputValueChange = (
valueAsNumber: number,
valueAsString: string,
) => {
setValueAsNumber(valueAsNumber);
};
const onBlur = () => {
payload.updateData(index, id, value);
const handleInputBlur = () => {
payload.updateData(index, id, valueAsNumber);
};

useEffect(() => {
setValue(initialValue);
}, [initialValue]);
setValueAsNumber(controlledInputValue);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [controlledInputValue]);

const error = payload.errors?.[index]?.[id];

return (
<FormGroup
intent={error ? Intent.DANGER : null}
intent={error ? Intent.DANGER : undefined}
className={classNames(CLASSES.FILL)}
>
<NumericInput
value={value}
onValueChange={handleValueChange}
onBlur={onBlur}
fill={true}
asyncControl
value={controlledInputValue}
onValueChange={handleInputValueChange}
onBlur={handleInputBlur}
buttonPosition={'none'}
fill
/>
</FormGroup>
);
}

NumericInputCell.cellType = CellType.Field;
NumericInputCell.cellType = CellType.Field;
6 changes: 2 additions & 4 deletions packages/webapp/src/containers/Drawers/BillDrawer/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
Tag,
} from '@blueprintjs/core';
import {
FormatNumberCell,
TextOverviewTooltipCell,
FormattedMessage as T,
Choose,
Expand Down Expand Up @@ -51,9 +50,8 @@ export const useBillReadonlyEntriesTableColumns = () => {
},
{
Header: intl.get('quantity'),
accessor: 'quantity',
Cell: FormatNumberCell,
width: getColumnWidth(entries, 'quantity', {
accessor: 'quantity_formatted',
width: getColumnWidth(entries, 'quantity_formatted', {
minWidth: 60,
magicSpacing: 5,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ export const useCreditNoteReadOnlyEntriesColumns = () => {
},
{
Header: intl.get('quantity'),
accessor: 'quantity',
Cell: FormatNumberCell,
width: getColumnWidth(entries, 'quantity', {
accessor: 'quantity_formatted',
width: getColumnWidth(entries, 'quantity_formatted', {
minWidth: 60,
magicSpacing: 5,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ export const useInvoiceReadonlyEntriesColumns = () => {
{
Header: intl.get('quantity'),
accessor: 'quantity',
Cell: FormatNumberCell,
align: 'right',
disableSortBy: true,
textOverview: true,
width: getColumnWidth(entries, 'quantity', {
width: getColumnWidth(entries, 'quantity_formatted', {
minWidth: 60,
magicSpacing: 5,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const useEstimateTransactionsColumns = () => {
{
id: 'qunatity',
Header: intl.get('item.drawer_quantity_sold'),
accessor: 'quantity',
accessor: 'quantity_formatted',
align: 'right',
width: 100,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ export const useReceiptReadonlyEntriesTableColumns = () => {
},
{
Header: intl.get('quantity'),
accessor: 'quantity',
Cell: FormatNumberCell,
width: getColumnWidth(entries, 'quantity', {
accessor: 'quantity_formatted',
width: getColumnWidth(entries, 'quantity_formatted', {
minWidth: 60,
magicSpacing: 5,
}),
Expand Down

0 comments on commit 5d6f901

Please sign in to comment.