Skip to content

Commit

Permalink
Merge pull request #1 from SergeiPatiakin/SergeiPatiakin/download-inv…
Browse files Browse the repository at this point in the history
…oice

Allow downloading invoice PDF
  • Loading branch information
SergeiPatiakin authored Jul 8, 2023
2 parents bceb657 + bb1bdd0 commit 6021b5c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/common/ipc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ export type GetInvoices = IpcTypeDef<'get-invoices', InvoiceFilter, Array<Invoic

export type GetInvoiceDetail = IpcTypeDef<'get-invoice-detail', number, InvoiceDetail>

export type DownloadInvoiceFile = IpcTypeDef<'download-invoice-file', number, void>

export type RespondToInvoice = IpcTypeDef<'respond-to-invoice', InvoiceResponse, void>

export type IpcMethods = {
getTechnicalConf: GetTechnicalConf
updateTechnicalConf: UpdateTechnicalConf
getInvoices: GetInvoices
getInvoiceDetail: GetInvoiceDetail
downloadInvoiceFile: DownloadInvoiceFile
respondToInvoice: RespondToInvoice
}

Expand All @@ -67,6 +70,7 @@ const ipcNames: IpcNames = {
updateTechnicalConf: 'update-technical-conf',
getInvoices: 'get-invoices',
getInvoiceDetail: 'get-invoice-detail',
downloadInvoiceFile: 'download-invoice-file',
respondToInvoice: 'respond-to-invoice',
}

Expand Down
26 changes: 25 additions & 1 deletion src/main/main-ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BrowserWindow, ipcMain } from 'electron'
import fs from 'fs'
import { BrowserWindow, dialog, ipcMain } from 'electron'
import { foreachIpc, InvoiceListItem, IpcHandlerFns } from '../common/ipc-types'
import { getTechnicalConf, updateTechnicalConf } from './filesystem'
import axios from 'axios'
Expand Down Expand Up @@ -68,6 +69,29 @@ const handlers: IpcHandlerFns = {
}
return invoiceDetail
},
downloadInvoiceFile: async ({ arg: invoiceId, browserWindow }) => {
const r = await dialog.showSaveDialog(browserWindow, {
defaultPath: `${invoiceId}.pdf`
})
if (!r.canceled && r.filePath) {
const technicalConf = getTechnicalConf()
const url = new URL(`https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/xml?invoiceId=${invoiceId}`)
const response = await axios.get(
url.toString(),
{
headers: {
Accept: 'text/plain',
ApiKey: technicalConf.apiKey,
},
}
)
const parser = new XMLParser({ ignoreAttributes: false, parseTagValue: false })
const document = parser.parse(response.data)
const invoiceFileContentB64 = document['env:DocumentEnvelope']['env:DocumentHeader']['env:DocumentPdf']['#text']
const invoiceFileContent = Buffer.from(invoiceFileContentB64, 'base64')
fs.writeFileSync(r.filePath, invoiceFileContent)
}
},
respondToInvoice: async ({ arg: invoiceResponse }) => {
const technicalConf = getTechnicalConf()
const url = new URL('https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/acceptRejectPurchaseInvoice')
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/components/InvoiceDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ export const InvoiceDetailPage = (props: Props) => {
>
Reject...
</Button>
<Button
onClick={async () => {
await ipcContextApi.downloadInvoiceFile(props.invoiceId)
}}
>
Download
</Button>
{acceptDialogState.visible &&
<Dialog
open
Expand Down

0 comments on commit 6021b5c

Please sign in to comment.