IO-2920 Refactor fee discounting to use API to check.
This commit is contained in:
@@ -7,6 +7,7 @@ import { connect } from "react-redux";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||
import InstanceRenderManager from "../../utils/instanceRenderMgr";
|
||||
import JobTotalsCashDiscount from "./jobs-totals.cash-discount-display.component";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
//currentUser: selectCurrentUser
|
||||
@@ -169,13 +170,7 @@ export function JobTotalsTableTotals({ bodyshop, job }) {
|
||||
},
|
||||
{
|
||||
key: t("jobs.labels.total_cust_payable"),
|
||||
total: Dinero(job.job_totals.totals.custPayable.total)
|
||||
.add(
|
||||
Dinero(job.job_totals.totals.custPayable.total).percentage(
|
||||
bodyshop.intellipay_config?.cash_discount_percentage || 0
|
||||
)
|
||||
)
|
||||
.toJSON(),
|
||||
render: <JobTotalsCashDiscount amountDinero={job.job_totals.totals.custPayable.total} />,
|
||||
bold: true
|
||||
}
|
||||
]
|
||||
@@ -211,7 +206,7 @@ export function JobTotalsTableTotals({ bodyshop, job }) {
|
||||
dataIndex: "total",
|
||||
key: "total",
|
||||
align: "right",
|
||||
render: (text, record) => Dinero(record.total).toFormat(),
|
||||
render: (text, record) => (record.render ? record.render : Dinero(record.total).toFormat()),
|
||||
width: "20%",
|
||||
onCell: (record, rowIndex) => {
|
||||
return { style: { fontWeight: record.bold && "bold" } };
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { notification, Spin } from "antd";
|
||||
import axios from "axios";
|
||||
import Dinero from "dinero.js";
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { connect } from "react-redux";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
bodyshop: selectBodyshop
|
||||
});
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
});
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(JobTotalsCashDiscount);
|
||||
|
||||
export function JobTotalsCashDiscount({ bodyshop, amountDinero }) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [fee, setFee] = useState(0);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
if (amountDinero && bodyshop) {
|
||||
setLoading(true);
|
||||
let response;
|
||||
try {
|
||||
response = await axios.post("/intellipay/checkfee", {
|
||||
bodyshop: { id: bodyshop.id, imexshopid: bodyshop.imexshopid },
|
||||
amount: Dinero(amountDinero).toFormat("0.00")
|
||||
});
|
||||
|
||||
if (response?.data?.error) {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: response.data?.error || "Error encountered contacting IntelliPay service."
|
||||
});
|
||||
} else {
|
||||
setFee(response.data?.fee || 0);
|
||||
}
|
||||
} catch (error) {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: error.response?.data?.error || "Error encountered contacting IntelliPay service."
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
}, [amountDinero, bodyshop]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData, bodyshop, amountDinero]);
|
||||
|
||||
if (loading) return <Spin size="small" />;
|
||||
return Dinero(amountDinero)
|
||||
.add(Dinero({ amount: Math.round(fee * 100) }))
|
||||
.toFormat();
|
||||
}
|
||||
Reference in New Issue
Block a user