From a948dd4236a18ababa1e254faff7166fd0befbd6 Mon Sep 17 00:00:00 2001 From: elforjani13 <39470382+elforjani13@users.noreply.github.com> Date: Wed, 8 Dec 2021 18:51:21 +0200 Subject: [PATCH] feat: add delete reconcile & handle error. --- .../ReconcileVendorCreditDeleteAlert.js | 83 +++++++++++++++++++ .../VendorCreditDeleteAlert.js | 6 +- .../ReconcileVendorCreditTransactionsTable.js | 47 +++++++++++ .../components.js | 49 +++++++++++ .../components.js | 4 +- .../Purchases/Bills/BillForm/utils.js | 10 +++ .../CreditNotes/CreditNotesLanding/utils.js | 28 +++++++ 7 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 src/containers/Alerts/VendorCeditNotes/ReconcileVendorCreditDeleteAlert.js create mode 100644 src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/ReconcileVendorCreditTransactionsTable.js create mode 100644 src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/components.js create mode 100644 src/containers/Purchases/CreditNotes/CreditNotesLanding/utils.js diff --git a/src/containers/Alerts/VendorCeditNotes/ReconcileVendorCreditDeleteAlert.js b/src/containers/Alerts/VendorCeditNotes/ReconcileVendorCreditDeleteAlert.js new file mode 100644 index 000000000..bf493b47d --- /dev/null +++ b/src/containers/Alerts/VendorCeditNotes/ReconcileVendorCreditDeleteAlert.js @@ -0,0 +1,83 @@ +import React from 'react'; +import intl from 'react-intl-universal'; +import { FormattedMessage as T, FormattedHTMLMessage } from 'components'; +import { Intent, Alert } from '@blueprintjs/core'; +import { AppToaster } from 'components'; + +import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect'; +import withAlertActions from 'containers/Alert/withAlertActions'; +import withDrawerActions from 'containers/Drawer/withDrawerActions'; + +import { useDeleteReconcileVendorCredit } from 'hooks/query'; +import { compose } from 'utils'; + +/** + * Reconcile vendor credit delete alert. + */ +function ReconcileVendorCreditDeleteAlert({ + name, + + // #withAlertStoreConnect + isOpen, + payload: { vendorCreditId }, + + // #withAlertActions + closeAlert, + + // #withDrawerActions + closeDrawer, +}) { + const { isLoading, mutateAsync: deleteReconcileVendorCreditMutate } = + useDeleteReconcileVendorCredit(); + + // handle cancel delete credit note alert. + const handleCancelDeleteAlert = () => { + closeAlert(name); + }; + + const handleConfirmReconcileVendorCreditDelete = () => { + deleteReconcileVendorCreditMutate(vendorCreditId) + .then(() => { + AppToaster.show({ + message: intl.get('reconcile_vendor_credit.alert.success_message'), + intent: Intent.SUCCESS, + }); + // closeDrawer('vendor-credit-detail-drawer'); + }) + .catch( + ({ + response: { + data: { errors }, + }, + }) => {}, + ) + .finally(() => { + closeAlert(name); + }); + }; + + return ( + } + confirmButtonText={} + icon="trash" + intent={Intent.DANGER} + isOpen={isOpen} + onCancel={handleCancelDeleteAlert} + onConfirm={handleConfirmReconcileVendorCreditDelete} + loading={isLoading} + > +

+ +

+
+ ); +} + +export default compose( + withAlertStoreConnect(), + withAlertActions, + withDrawerActions, +)(ReconcileVendorCreditDeleteAlert); diff --git a/src/containers/Alerts/VendorCeditNotes/VendorCreditDeleteAlert.js b/src/containers/Alerts/VendorCeditNotes/VendorCreditDeleteAlert.js index 160905c1d..44d0bcf8e 100644 --- a/src/containers/Alerts/VendorCeditNotes/VendorCreditDeleteAlert.js +++ b/src/containers/Alerts/VendorCeditNotes/VendorCreditDeleteAlert.js @@ -7,7 +7,7 @@ import { AppToaster } from 'components'; import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect'; import withAlertActions from 'containers/Alert/withAlertActions'; import withDrawerActions from 'containers/Drawer/withDrawerActions'; - +import { handleDeleteErrors } from '../../Purchases/CreditNotes/CreditNotesLanding/utils'; import { useDeleteVendorCredit } from 'hooks/query'; import { compose } from 'utils'; @@ -48,7 +48,9 @@ function VendorCreditDeleteAlert({ response: { data: { errors }, }, - }) => {}, + }) => { + handleDeleteErrors(errors); + }, ) .finally(() => { closeAlert(name); diff --git a/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/ReconcileVendorCreditTransactionsTable.js b/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/ReconcileVendorCreditTransactionsTable.js new file mode 100644 index 000000000..e6a123f92 --- /dev/null +++ b/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/ReconcileVendorCreditTransactionsTable.js @@ -0,0 +1,47 @@ +import React from 'react'; +import { DataTable, Card } from 'components'; + +import '../../../../style/pages/RefundCreditNote/List.scss'; + +import withAlertsActions from 'containers/Alert/withAlertActions'; +import { useVendorCreditDetailDrawerContext } from '../VendorCreditDetailDrawerProvider'; +import { + useReconcileVendorCreditTransactionsTableColumns, + ActionsMenu, +} from './components'; +import { compose } from 'utils'; + +/** + * Reconcile vendor credit transactions table. + */ +function ReconcileVendorCreditTransactionsTable({ + // #withAlertsActions + openAlert, +}) { + const columns = useReconcileVendorCreditTransactionsTableColumns(); + + const { reconcileVendorCredits } = useVendorCreditDetailDrawerContext(); + + // Handle delete reconile credit. + const handleDeleteReconcileVendorCredit = ({ id }) => { + openAlert('reconcile-vendor-delete', { vendorCreditId: id }); + }; + + return ( + + + + ); +} + +export default compose(withAlertsActions)( + ReconcileVendorCreditTransactionsTable, +); diff --git a/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/components.js b/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/components.js new file mode 100644 index 000000000..2ac341ce7 --- /dev/null +++ b/src/containers/Drawers/VendorCreditDetailDrawer/ReconcileVendorCreditTransactions/components.js @@ -0,0 +1,49 @@ +import React from 'react'; +import { Intent, MenuItem, Menu } from '@blueprintjs/core'; +import intl from 'react-intl-universal'; +import { FormatDateCell, Icon } from 'components'; +import { safeCallback } from 'utils'; + +/** + * Actions menu. + */ +export function ActionsMenu({ payload: { onDelete }, row: { original } }) { + return ( + + } + text={intl.get('delete_transaction')} + intent={Intent.DANGER} + onClick={safeCallback(onDelete, original)} + /> + + ); +} + +export function useReconcileVendorCreditTransactionsTableColumns() { + return React.useMemo( + () => [ + { + Header: intl.get('date'), + accessor: 'formatted_bill_date', + Cell: FormatDateCell, + width: 100, + className: 'date', + }, + { + Header: intl.get('bill_number'), + accessor: 'bill_reference_no', + width: 100, + className: 'bill_number', + }, + { + Header: intl.get('amount'), + accessor: 'formatted_amount', + width: 100, + className: 'amount', + align: 'right', + }, + ], + [], + ); +} diff --git a/src/containers/Drawers/VendorCreditDetailDrawer/RefundVendorCreditTransactions/components.js b/src/containers/Drawers/VendorCreditDetailDrawer/RefundVendorCreditTransactions/components.js index 2eb0dc9d2..1a2bcd7f7 100644 --- a/src/containers/Drawers/VendorCreditDetailDrawer/RefundVendorCreditTransactions/components.js +++ b/src/containers/Drawers/VendorCreditDetailDrawer/RefundVendorCreditTransactions/components.js @@ -39,9 +39,9 @@ export function useRefundCreditTransactionsTableColumns() { }, { Header: intl.get('refund_vendor_credit.column.withdrawal_account'), - accessor: ({ from_account }) => from_account.name, + accessor: ({ deposit_account }) => deposit_account.name, width: 100, - className: 'from_account', + className: 'deposit_account', }, { id: 'reference_no', diff --git a/src/containers/Purchases/Bills/BillForm/utils.js b/src/containers/Purchases/Bills/BillForm/utils.js index 457dc1324..d4fb277cd 100644 --- a/src/containers/Purchases/Bills/BillForm/utils.js +++ b/src/containers/Purchases/Bills/BillForm/utils.js @@ -125,6 +125,16 @@ export const handleDeleteErrors = (errors) => { intent: Intent.DANGER, }); } + if ( + errors.find((error) => error.type === 'BILL_HAS_APPLIED_TO_VENDOR_CREDIT') + ) { + AppToaster.show({ + message: intl.get( + 'bills.error.you_couldn_t_delete_bill_has_reconciled_with_vendor_credit', + ), + intent: Intent.DANGER, + }); + } }; /** diff --git a/src/containers/Purchases/CreditNotes/CreditNotesLanding/utils.js b/src/containers/Purchases/CreditNotes/CreditNotesLanding/utils.js new file mode 100644 index 000000000..dd703a459 --- /dev/null +++ b/src/containers/Purchases/CreditNotes/CreditNotesLanding/utils.js @@ -0,0 +1,28 @@ +import intl from 'react-intl-universal'; +import { Intent } from '@blueprintjs/core'; +import { AppToaster } from 'components'; + +export const handleDeleteErrors = (errors) => { + if ( + errors.find((error) => error.type === 'VENDOR_CREDIT_HAS_APPLIED_BILLS') + ) { + AppToaster.show({ + message: intl.get( + 'vendor_credit.error.you_couldn_t_delete_vendor_credit_has_reconciled', + ), + intent: Intent.DANGER, + }); + } + if ( + errors.find( + (error) => error.type === 'VENDOR_CREDIT_HAS_REFUND_TRANSACTIONS', + ) + ) { + AppToaster.show({ + message: intl.get( + 'vendor_credit.error.you_couldn_t_delete_vendor_credit_that_has_associated_refund', + ), + intent: Intent.DANGER, + }); + } +};