{ "version": 3, "sources": ["apps/frontend/src/app/shared/services/ocr.service.ts"], "sourcesContent": ["import { Injectable } from '@angular/core';\nimport { InputDialogComponent } from '..//components/input-dialog.component';\nimport { PurchaseEntityInterface, PurchaseTypes } from '@proman/resources/purchase';\nimport { Customer, Material, PromanFile, Supplier } from '@proman/interfaces/entity-interfaces';\nimport { mapOptionTranslate } from '@proman/utils';\nimport { SelectOption } from '@proman/interfaces/object-interfaces';\nimport { Entity } from '@proman/services/entity.service';\nimport { FilterService } from '@proman/services/filter.service';\nimport { Dialog } from './dialog.service';\nimport { PromanStateService } from './proman-state.service';\nimport { OcrEntityInterface } from '@proman/resources/ocr';\nimport { ToastService } from '@proman/services/toast.service';\n\n@Injectable({ providedIn: 'root' })\nexport class OcrService {\n typeOptions: SelectOption[];\n purchaseEntityInterface: PurchaseEntityInterface;\n ocrEntityInterface: OcrEntityInterface;\n\n constructor(\n private Entity: Entity,\n private Filter: FilterService,\n private PromanState: PromanStateService,\n private Dialog: Dialog,\n private Toast: ToastService,\n ) {\n this.ocrEntityInterface = this.Entity.get('ocr');\n this.purchaseEntityInterface = this.Entity.get('purchase');\n this.purchaseEntityInterface.getPurchaseTypes().then((response: string[]) => this.typeOptions = response.map((item: string) => mapOptionTranslate(item, this.Filter.translate)));\n }\n\n invoiceAnalyze = () => {\n return this.Dialog.open(InputDialogComponent, {\n header: 'select_purchase_type',\n mainField: {\n name: 'purchase_type',\n key: 'type',\n type: 'select',\n config: { options: this.typeOptions }\n },\n parameters: [\n {\n name: 'files',\n key: 'files',\n type: 'files',\n config: { multiple: false }\n }\n ]\n }).then((response: { type: PurchaseTypes, files: PromanFile[] }) => {\n if (response && response.type && response.files && response.files[1]) {\n (response as any).files = response.files[1].id;\n this.ocrEntityInterface.analyzeInvoice({\n id: response.files,\n type: response.type\n }).then((analysisResponse: {\n supplier: Supplier,\n customer: Customer,\n date: string,\n invoiceNumber: string,\n items: Material[],\n type: string,\n }) => {\n const parameters = [];\n if (analysisResponse.supplier) parameters.push({\n name: 'supplier',\n key: 'supplier',\n type: 'autocomplete',\n config: { entity: 'supplier', key: 'id', autofocus: false },\n value: analysisResponse.supplier\n })\n\n if (analysisResponse.customer) parameters.push({\n name: 'customer',\n key: 'customer',\n type: 'autocomplete',\n config: { entity: 'customer', key: 'id', autofocus: false },\n value: analysisResponse.customer\n },\n {\n name: 'template',\n key: 'template',\n type: 'autocomplete',\n config: { entity: 'template', entityParams: { context: 'invoice' }, key: 'id', autofocus: false }\n });\n\n this.Dialog.open(InputDialogComponent, {\n header: 'verify_supplier',\n mainField: {\n name: 'items',\n key: 'items',\n type: 'list-manager',\n config: {\n template: 'analyzed_items_row',\n silentRemove: true,\n preventHandling: true,\n noModel: true,\n key: 'items',\n mainEntity: { items: analysisResponse.items },\n searchEntity: 'material'\n }\n },\n parameters\n }).then((verifiedResponse: { supplier: number, customer: number, template: number, [key: string]: any }) => {\n const itemsResponse: any[] = [];\n Object.entries(verifiedResponse).forEach(([key, value]) => {\n if (!['supplier', 'customer', 'template'].includes(key)) itemsResponse.push(value);\n });\n\n switch (analysisResponse.type) {\n case 'invoice':\n itemsResponse.forEach((item: any) => delete item.id);\n\n this.Entity.get('invoice').create({\n customer: verifiedResponse.customer,\n template: verifiedResponse.template,\n records: itemsResponse,\n }).then((invoiceCreateResponse) => {\n if (!invoiceCreateResponse) return;\n this.PromanState.to('Invoice', invoiceCreateResponse.data);\n });\n break;\n\n default:\n this.purchaseEntityInterface.create({\n type: response.type,\n supplier: verifiedResponse.supplier\n }).then((purchaseResponse) => {\n if (response.type === 'materials' || response.type === 'inventory') {\n const entityInterface = this.Entity.get('material_quant') as any;\n const promises: Promise[] = [];\n itemsResponse.map((item: any) => ({ ...item, material: item.id, vat: item.vat.id }));\n if (response.type === 'materials') {\n itemsResponse.forEach((item: any) => {\n // eslint-disable-next-line no-prototype-builtins\n if (item.hasOwnProperty('materialType')) {\n promises.push(this.Entity.get('material').update({ id: item.material, materialType: item.materialType.id }));\n }\n delete item.materialType;\n })\n }\n\n promises.push(entityInterface.purchase({\n items: itemsResponse,\n date: analysisResponse.date,\n file: response.files,\n invoiceNumber: analysisResponse.invoiceNumber,\n purchase: purchaseResponse.data,\n quantityType: 'quantity'\n }));\n Promise.all(promises).then((response) => {\n if (!response || !response.length) return;\n this.PromanState.to('Purchase', purchaseResponse.data);\n });\n } else {\n const entityInterface = this.Entity.get('product') as any;\n const promises: Promise[] = [];\n itemsResponse.map((item: any) => ({ ...item, product: item.id, vat: item.vat.id }));\n promises.push(entityInterface.purchase_ocr({\n items: itemsResponse,\n date: analysisResponse.date,\n file: response.files,\n invoiceNumber: analysisResponse.invoiceNumber,\n purchase: purchaseResponse.data,\n quantityType: 'quantity'\n }));\n Promise.all(promises).then((response) => {\n if (!response || !response.length) return;\n this.PromanState.to('Purchase', response[0]);\n });\n }\n });\n break;\n }\n });\n }).catch((e) => this.Toast.pop('error', e.error?.error));\n }\n });\n };\n}\n"], "mappings": "uLAcA,IAAaA,GAAU,IAAA,CAAjB,IAAOA,EAAP,MAAOA,CAAU,CAKrBC,YACUC,EACAC,EACAC,EACAC,EACAC,EAAmB,CAJnB,KAAAJ,OAAAA,EACA,KAAAC,OAAAA,EACA,KAAAC,YAAAA,EACA,KAAAC,OAAAA,EACA,KAAAC,MAAAA,EAOV,KAAAC,eAAiB,IACR,KAAKF,OAAOG,KAAKC,EAAsB,CAC5CC,OAAQ,uBACRC,UAAW,CACTC,KAAM,gBACNC,IAAK,OACLC,KAAM,SACNC,OAAQ,CAAEC,QAAS,KAAKC,WAAW,GAErCC,WAAY,CACV,CACEN,KAAM,QACNC,IAAK,QACLC,KAAM,QACNC,OAAQ,CAAEI,SAAU,EAAK,EAC1B,EAEJ,EAAEC,KAAMC,GAA0D,CAC7DA,GAAYA,EAASP,MAAQO,EAASC,OAASD,EAASC,MAAM,CAAC,IAChED,EAAiBC,MAAQD,EAASC,MAAM,CAAC,EAAEC,GAC5C,KAAKC,mBAAmBC,eAAe,CACrCF,GAAIF,EAASC,M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AAS9B,KAAK6B,EAAgBmB,aAAa,CACzCpC,MAAOG,EACPyB,KAAM1C,EAAiB0C,KACvBC,KAAMhD,EAASC,MACfgD,cAAe5C,EAAiB4C,cAChCH,SAAUX,EAAiBF,KAC3BiB,aAAc,WACf,CAAC,EACFC,QAAQC,IAAIf,CAAQ,EAAEtC,KAAMC,GAAY,CAClC,CAACA,GAAY,CAACA,EAASqD,QAC3B,KAAKtE,YAAYiD,GAAG,WAAYhC,EAAS,CAAC,CAAC,CAC7C,CAAC,EAEL,CAAC,EACD,MAEN,CAAC,CACH,CAAC,EAAEwD,MAAOC,GAAM,KAAKxE,MAAMyE,IAAI,QAASD,EAAEE,OAAOA,KAAK,CAAC,EAE3D,CAAC,EAtJD,KAAKxD,mBAAqB,KAAKtB,OAAO+C,IAAI,KAAK,EAC/C,KAAKM,wBAA0B,KAAKrD,OAAO+C,IAAI,UAAU,EACzD,KAAKM,wBAAwB0B,iBAAgB,EAAG7D,KAAMC,GAAuB,KAAKJ,YAAcI,EAASsC,IAAKX,GAAiBkC,EAAmBlC,EAAM,KAAK7C,OAAOgF,SAAS,CAAC,CAAC,CACjL,yCAfWnF,GAAUoF,EAAAlF,CAAA,EAAAkF,EAAAC,CAAA,EAAAD,EAAAE,CAAA,EAAAF,EAAA/E,CAAA,EAAA+E,EAAAG,CAAA,CAAA,CAAA,wBAAVvF,EAAUwF,QAAVxF,EAAUyF,UAAAC,WADG,MAAM,CAAA,EAC1B,IAAO1F,EAAP2F,SAAO3F,CAAU,GAAA", "names": ["OcrService", "constructor", "Entity", "Filter", "PromanState", "Dialog", "Toast", "invoiceAnalyze", "open", "InputDialogComponent", "header", "mainField", "name", "key", "type", "config", "options", "typeOptions", "parameters", "multiple", "then", "response", "files", "id", "ocrEntityInterface", "analyzeInvoice", "analysisResponse", "supplier", "push", "entity", "autofocus", "value", "customer", "entityParams", "context", "template", "silentRemove", "preventHandling", "noModel", "mainEntity", "items", "searchEntity", "verifiedResponse", "itemsResponse", "Object", "entries", "forEach", "includes", "item", "get", "create", "records", "invoiceCreateResponse", "to", "data", "purchaseEntityInterface", "purchaseResponse", "entityInterface", "promises", "map", "__spreadProps", "__spreadValues", "material", "vat", "hasOwnProperty", "update", "materialType", "purchase", "date", "file", "invoiceNumber", "quantityType", "Promise", "all", "length", "product", "purchase_ocr", "catch", "e", "pop", "error", "getPurchaseTypes", "mapOptionTranslate", "translate", "\u0275\u0275inject", "FilterService", "PromanStateService", "ToastService", "factory", "\u0275fac", "providedIn", "_OcrService"] }