Added bulk payables exporting with better error handling. BOD-152

This commit is contained in:
Patrick Fic
2020-06-04 09:53:08 -07:00
parent 4af463d39c
commit 910183d8e7
6 changed files with 160 additions and 3 deletions

View File

@@ -7521,6 +7521,27 @@
</translation>
</translations>
</concept_node>
<concept_node>
<name>exportselected</name>
<definition_loaded>false</definition_loaded>
<description></description>
<comment></comment>
<default_text></default_text>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-MX</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-CA</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>gotojob</name>
<definition_loaded>false</definition_loaded>

View File

@@ -5,6 +5,7 @@ import { Link } from "react-router-dom";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { alphaSort } from "../../utils/sorters";
import InvoiceExportButton from "../invoice-export-button/invoice-export-button.component";
import InvoiceExportAllButton from "../invoice-export-all-button/invoice-export-all-button.component";
import { DateFormatter } from "../../utils/DateFormatter";
import queryString from "query-string";
@@ -119,8 +120,7 @@ export default function AccountingPayablesTableComponent({
render: (text, record) => (
<div>
<InvoiceExportButton
invoiceId={record.id}
invoiceId={record.id}
disabled={transInProgress || !!record.exported}
loadingCallback={setTransInProgress}
/>
@@ -128,7 +128,7 @@ export default function AccountingPayablesTableComponent({
),
},
];
console.log('transInProgress', transInProgress)
console.log("transInProgress", transInProgress);
const handleSearch = (e) => {
setState({ ...state, search: e.target.value });
};
@@ -158,6 +158,12 @@ console.log('transInProgress', transInProgress)
placeholder={t("general.labels.search")}
allowClear
/>
<InvoiceExportAllButton
invoiceIds={selectedInvoices}
disabled={transInProgress}
loadingCallback={setTransInProgress}
completedCallback={setSelectedInvoices}
/>
</div>
);
}}

View File

@@ -0,0 +1,127 @@
import { useMutation } from "@apollo/react-hooks";
import { Button, notification } from "antd";
import axios from "axios";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { auth } from "../../firebase/firebase.utils";
import { UPDATE_INVOICES } from "../../graphql/invoices.queries";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
export function InvoiceExportAllButton({
bodyshop,
invoiceIds,
disabled,
loadingCallback,
completedCallback,
}) {
const { t } = useTranslation();
const [updateInvoice] = useMutation(UPDATE_INVOICES);
const [loading, setLoading] = useState(false);
const handleQbxml = async () => {
setLoading(true);
if (!!loadingCallback) loadingCallback(true);
let QbXmlResponse;
try {
QbXmlResponse = await axios.post(
"/accounting/qbxml/payables",
{ invoices: invoiceIds },
{
headers: {
Authorization: `Bearer ${await auth.currentUser.getIdToken(true)}`,
},
}
);
console.log("handle -> XML", QbXmlResponse);
} catch (error) {
console.log("Error getting QBXML from Server.", error);
notification["error"]({
message: t("invoices.errors.exporting", {
error: "Unable to retrieve QBXML. " + JSON.stringify(error.message),
}),
});
if (loadingCallback) loadingCallback(false);
setLoading(false);
return;
}
let PartnerResponse;
try {
PartnerResponse = await axios.post(
"http://48f9a09ccdec.ngrok.io/qb/",
QbXmlResponse.data
);
} catch (error) {
console.log("Error connecting to quickbooks or partner.", error);
notification["error"]({
message: t("invoices.errors.exporting-partner"),
});
if (!!loadingCallback) loadingCallback(false);
setLoading(false);
return;
}
console.log("handleQbxml -> PartnerResponse", PartnerResponse);
const failedTransactions = PartnerResponse.data.filter((r) => !r.success);
const successfulTransactions = PartnerResponse.data.filter(
(r) => r.success
);
if (failedTransactions.length > 0) {
//Uh oh. At least one was no good.
failedTransactions.map((ft) =>
notification["error"]({
message: t("invoices.errors.exporting", {
error: ft.errorMessage || "",
}),
})
);
}
if (successfulTransactions.length > 0) {
const invoiceUpdateResponse = await updateInvoice({
variables: {
invoiceIdList: successfulTransactions.map((st) => st.id),
invoice: {
exported: true,
exported_at: new Date(),
},
},
});
if (!!!invoiceUpdateResponse.errors) {
notification["success"]({
message: t("jobs.successes.exported"),
});
} else {
notification["error"]({
message: t("jobs.errors.exporting", {
error: JSON.stringify(invoiceUpdateResponse.error),
}),
});
}
}
//Set the list of selected invoices to be nothing.
if (!!completedCallback) completedCallback([]);
if (!!loadingCallback) loadingCallback(false);
setLoading(false);
};
return (
<Button
onClick={handleQbxml}
loading={loading}
disabled={disabled}
type="dashed"
>
{t("jobs.actions.exportselected")}
</Button>
);
}
export default connect(mapStateToProps, null)(InvoiceExportAllButton);

View File

@@ -507,6 +507,7 @@
"changestatus": "Change Status",
"convert": "Convert",
"export": "Export",
"exportselected": "Export Selected",
"gotojob": "Go to Job",
"manualnew": "Create New Job Manually",
"mark": "Mark",

View File

@@ -507,6 +507,7 @@
"changestatus": "Cambiar Estado",
"convert": "Convertir",
"export": "",
"exportselected": "",
"gotojob": "",
"manualnew": "",
"mark": "",

View File

@@ -507,6 +507,7 @@
"changestatus": "Changer le statut",
"convert": "Convertir",
"export": "",
"exportselected": "",
"gotojob": "",
"manualnew": "",
"mark": "",