Created export payables screen + basic invoice exporting BOD-139

This commit is contained in:
Patrick Fic
2020-06-02 15:41:47 -07:00
parent a70933f03c
commit 73c457e972
17 changed files with 588 additions and 14 deletions

View File

@@ -0,0 +1,159 @@
import { Input, Table, Checkbox } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
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 { DateFormatter } from "../../utils/DateFormatter";
import queryString from "query-string";
export default function AccountingPayablesTableComponent({
loading,
invoices,
}) {
const { t } = useTranslation();
const [state, setState] = useState({
sortedInfo: {},
search: "",
});
const handleTableChange = (pagination, filters, sorter) => {
setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
};
const columns = [
{
title: t("invoices.fields.vendorname"),
dataIndex: "vendorname",
key: "vendorname",
sorter: (a, b) => alphaSort(a.vendor.name, b.vendor.name),
sortOrder:
state.sortedInfo.columnKey === "vendorname" && state.sortedInfo.order,
render: (text, record) => (
<Link
to={{
pathname: `/manage/shop/vendors`,
search: queryString.stringify({ selectedvendor: record.vendor.id }),
}}
>
{record.vendor.name}
</Link>
),
},
{
title: t("invoices.fields.invoice_number"),
dataIndex: "invoice_number",
key: "invoice_number",
sorter: (a, b) => alphaSort(a.invoice_number, b.invoice_number),
sortOrder:
state.sortedInfo.columnKey === "invoice_number" &&
state.sortedInfo.order,
render: (text, record) => (
<Link
to={{
pathname: `/manage/invoices`,
search: queryString.stringify({
invoiceid: record.id,
vendorid: record.vendor.id,
}),
}}
>
{record.invoice_number}
</Link>
),
},
{
title: t("invoices.fields.date"),
dataIndex: "date",
key: "date",
sorter: (a, b) => a.date - b.date,
sortOrder:
state.sortedInfo.columnKey === "date" && state.sortedInfo.order,
render: (text, record) => <DateFormatter>{record.date}</DateFormatter>,
},
{
title: t("invoices.fields.total"),
dataIndex: "total",
key: "total",
sorter: (a, b) => a.total - b.total,
sortOrder:
state.sortedInfo.columnKey === "total" && state.sortedInfo.order,
render: (text, record) => (
<CurrencyFormatter>{record.total}</CurrencyFormatter>
),
},
{
title: t("invoices.fields.is_credit_memo"),
dataIndex: "is_credit_memo",
key: "is_credit_memo",
sorter: (a, b) => a.is_credit_memo - b.is_credit_memo,
sortOrder:
state.sortedInfo.columnKey === "is_credit_memo" &&
state.sortedInfo.order,
render: (text, record) => (
<Checkbox disabled checked={record.is_credit_memo} />
),
},
{
title: t("general.labels.actions"),
dataIndex: "actions",
key: "actions",
sorter: (a, b) => a.clm_total - b.clm_total,
render: (text, record) => (
<div>
<InvoiceExportButton
invoiceId={record.id}
disabled={!!record.exported}
/>
</div>
),
},
];
const handleSearch = (e) => {
setState({ ...state, search: e.target.value });
};
const dataSource = state.search
? invoices.filter(
(v) =>
(v.vendor.name || "")
.toLowerCase()
.includes(state.search.toLowerCase()) ||
(v.invoice_number || "")
.toLowerCase()
.includes(state.search.toLowerCase())
)
: invoices;
return (
<div>
<Table
loading={loading}
title={() => {
return (
<div>
<Input
value={state.search}
onChange={handleSearch}
placeholder={t("general.labels.search")}
allowClear
/>
</div>
);
}}
dataSource={dataSource}
size="small"
pagination={{ position: "top" }}
columns={columns}
rowKey="id"
onChange={handleTableChange}
/>
</div>
);
}

View File

@@ -0,0 +1,98 @@
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_INVOICE } from "../../graphql/invoices.queries";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
export function InvoiceExportButton({ bodyshop, invoiceId, disabled }) {
const { t } = useTranslation();
const [updateInvoice] = useMutation(UPDATE_INVOICE);
const [loading, setLoading] = useState(false);
const handleQbxml = async () => {
setLoading(true);
let QbXmlResponse;
try {
QbXmlResponse = await axios.post(
"/accounting/qbxml/payables",
{ invoices: [invoiceId] },
{
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),
}),
});
setLoading(false);
return;
}
let PartnerResponse;
try {
PartnerResponse = await axios.post(
"http://e9c5a8ed9079.ngrok.io/qb/receivables",
QbXmlResponse.data
);
} catch (error) {
console.log("Error connecting to quickbooks or partner.", error);
notification["error"]({
message: t("invoices.errors.exporting-partner"),
});
setLoading(false);
return;
}
console.log("PartnerResponse", PartnerResponse);
// const invoiceUpdateResponse = await updateInvoice({
// variables: {
// invoiceId: invoiceId,
// 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),
// }),
// });
// }
setLoading(false);
};
return (
<Button
onClick={handleQbxml}
loading={loading}
disabled={disabled}
type="dashed"
>
{t("jobs.actions.export")}
</Button>
);
}
export default connect(mapStateToProps, null)(InvoiceExportButton);

View File

@@ -44,7 +44,7 @@ export function JobsCloseExportButton({ bodyshop, jobId, disabled }) {
let PartnerResponse;
try {
PartnerResponse = await axios.post(
"http://e9c5a8ed9079.ngrok.io/qb/receivables",
"http://localhost:1337/qb/receivables",
QbXmlResponse.data,
{
headers: {
@@ -60,7 +60,7 @@ export function JobsCloseExportButton({ bodyshop, jobId, disabled }) {
setLoading(false);
return;
}
console.log("PartnerResponse", PartnerResponse);
const jobUpdateResponse = await updateJob({
variables: {