-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webapp): dynamic columns of sales by items sheet
- Loading branch information
Showing
8 changed files
with
276 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/server/src/services/FinancialStatements/SalesByItems/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
|
||
export enum ROW_TYPE { | ||
ITEM = 'ITEM', | ||
TOTAL = 'TOTAL', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/webapp/src/containers/FinancialStatements/SalesByItems/dynamicColumns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @ts-nocheck | ||
import { getColumnWidth } from '@/utils'; | ||
import * as R from 'ramda'; | ||
import { Align } from '@/constants'; | ||
import { useSalesByItemsContext } from './SalesByItemProvider'; | ||
|
||
const getTableCellValueAccessor = (index) => `cells[${index}].value`; | ||
|
||
const getReportColWidth = (data, accessor, headerText) => { | ||
return getColumnWidth( | ||
data, | ||
accessor, | ||
{ magicSpacing: 10, minWidth: 100 }, | ||
headerText, | ||
); | ||
}; | ||
|
||
/** | ||
* Account name column mapper. | ||
*/ | ||
const commonColumnMapper = R.curry((data, column) => { | ||
const accessor = getTableCellValueAccessor(column.cell_index); | ||
|
||
return { | ||
key: column.key, | ||
Header: column.label, | ||
accessor, | ||
className: column.key, | ||
textOverview: true, | ||
}; | ||
}); | ||
|
||
/** | ||
* Numeric columns accessor. | ||
*/ | ||
const numericColumnAccessor = R.curry((data, column) => { | ||
const accessor = getTableCellValueAccessor(column.cell_index); | ||
const width = getReportColWidth(data, accessor, column.label); | ||
|
||
return { | ||
...column, | ||
align: Align.Right, | ||
width, | ||
}; | ||
}); | ||
|
||
const dynamiColumnMapper = R.curry((data, column) => { | ||
const _numericColumnAccessor = numericColumnAccessor(data); | ||
|
||
return R.compose( | ||
R.when(R.pathEq(['key'], 'sold_quantity'), _numericColumnAccessor), | ||
R.when(R.pathEq(['key'], 'sold_amount'), _numericColumnAccessor), | ||
R.when(R.pathEq(['key'], 'average_price'), _numericColumnAccessor), | ||
commonColumnMapper(data), | ||
)(column); | ||
}); | ||
|
||
/** | ||
* Composes the dynamic columns that fetched from request to columns to table component. | ||
*/ | ||
export const dynamicColumns = R.curry((data, columns) => { | ||
return R.map(dynamiColumnMapper(data), columns); | ||
}); | ||
|
||
/** | ||
* Retrieves the G/L sheet table columns for table component. | ||
*/ | ||
export const useSalesByItemsTableColumns = () => { | ||
const { salesByItems } = useSalesByItemsContext(); | ||
|
||
if (!salesByItems) { | ||
throw new Error('Sales by items context not found'); | ||
} | ||
const { table } = salesByItems; | ||
|
||
return dynamicColumns(table.rows, table.columns); | ||
}; |
Oops, something went wrong.