{"version":3,"sources":["../lib/index.ts","../lib/InvoiceXMLError.ts"],"names":["randomUUID","XMLBuilder","XMLParser","InvoiceXMLError","errors","xmlopts","Invoice","data","parsed","validate","xml","builderOptions","name","value"],"mappings":"AAAA,OAAS,cAAAA,MAAkB,SAC3B,OAAS,cAAAC,EAAY,aAAAC,MAAiB,kBCDtC,IAAqBC,EAArB,cAA6C,KAAM,CAEjD,YAAYC,EAAmB,CAC7B,MAAM,mBAAqBA,GAAQ,OAAS,KAAKA,EAAO,CAAC,EAAE,OAAO,GAAK,GAAG,EACtEA,IACF,KAAK,OAASA,EAClB,CACF,EDSA,IAAMC,EAAU,CACd,oBAAqB,KACrB,iBAAkB,GAClB,OAAQ,EACV,EAEqBC,EAArB,KAA6B,CA8C3B,YAAYC,EAAsC,CA7ClD,KAAO,aAAuB,EAQ9B,KAAO,KAAgBP,EAAW,EAUlC,KAAO,wCAAkD,GAEzD,KAAO,kBAA4B,MAEnC,KAAO,SAA4B,IACnC,KAAO,YAA+B,IAmBtC,KAAQ,QAAU,iCAClB,KAAQ,UAAY,QAGlB,GAAI,OAAOO,GAAS,UAAYA,aAAgB,OAAQ,CAKtD,IAAMC,EAAS,IAAIN,EAAUG,CAAO,EAAE,MAAME,CAAI,EAChD,GAAIC,EAAO,QAAS,KAAK,OAAOA,EAAO,QAAS,EAAK,MAChD,OAAM,IAAIL,CACjB,MAAW,OAAOI,GAAS,UAAU,KAAK,OAAOA,CAAI,CACvD,CAEA,OAAOA,EAAmBE,EAAoB,GAAM,CAC9C,OAAOF,EAAK,cAAiB,WAC/BA,EAAK,aAAe,CAClB,QAAS,EACT,cAAe,EACf,aAAc,EACd,mBAAoB,EACpB,kBAAmB,EACnB,sBAAuB,EACvB,WAAY,CACd,EAAEA,EAAK,YAAY,GAErB,OAAO,OAAO,KAAMA,CAAI,EACpBE,GAAU,KAAK,SAAS,CAC9B,CAEA,SAASC,EAAoB,CAI3B,MAAO,EACT,CAEA,MAAMD,EAAoB,GAAc,CACtC,I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["import { randomUUID } from \"crypto\";\nimport { XMLBuilder, XMLParser } from \"fast-xml-parser\";\nimport InvoiceXMLError from \"./InvoiceXMLError\";\nimport InvoiceType, {\n  AccountingCustomerParty,\n  AccountingSupplierParty,\n  BuyerCustomerParty,\n  Delivery,\n  DeliveryNoteReferences,\n  InvoiceLines,\n  LegalMonetaryTotal,\n  PaymentMeans,\n  SellerSupplierParty,\n  TaxTotal,\n} from \"./interface\";\n\nconst xmlopts = {\n  attributeNamePrefix: \"$_\",\n  ignoreAttributes: false,\n  format: true,\n};\n\nexport default class Invoice {\n  public DocumentType: number = 1;\n  public SubDocumentType?: string;\n  public SubDocumentTypeOrigin?: string;\n  public TargetConsolidator?: string;\n  public ClientOnTargetConsolidator?: string;\n  public ClientBankAccount?: string;\n  public ID?: string;\n  public ID36?: string;\n  public UUID?: string = randomUUID();\n  public EgovFlag?: boolean;\n  public ISDS_ID?: string;\n  public FileReference?: string;\n  public ReferenceNumber?: string;\n  public EgovClassifiers?: any;\n  public IssuingSystem?: string;\n  public IssueDate?: string | Date;\n  public TaxPointDate?: string | Date;\n  public VATApplicable?: string;\n  public ElectronicPossibilityAgreementReference: string = \"\";\n  public Note?: string;\n  public LocalCurrencyCode: string = \"CZK\";\n  public ForeignCurrencyCode?: string;\n  public CurrRate: number | string = \"1\";\n  public RefCurrRate: number | string = \"1\";\n  public Extensions?: any;\n  public AccountingSupplierParty?: AccountingSupplierParty;\n  public SellerSupplierParty?: SellerSupplierParty;\n  public AnonymousCustomerParty?: any;\n  public AccountingCustomerParty?: AccountingCustomerParty;\n  public BuyerCustomerParty?: BuyerCustomerParty;\n  public OrderReferences?: any;\n  public OriginalDocumentReferences?: any;\n  public ContractReferences?: any;\n  public Delivery?: Delivery;\n  public DeliveryNoteReferences?: DeliveryNoteReferences;\n  public InvoiceLines?: InvoiceLines;\n  public NonTaxedDeposits?: any;\n  public TaxedDeposits?: any;\n  public TaxTotal?: TaxTotal;\n  public LegalMonetaryTotal?: LegalMonetaryTotal;\n  public PaymentMeans?: PaymentMeans;\n  public SupplementsList?: any;\n  private $_xmlns = \"http://isdoc.cz/namespace/2013\";\n  private $_version = \"6.0.1\";\n\n  constructor(data?: InvoiceType | string | Buffer) {\n    if (typeof data === \"string\" || data instanceof Buffer) {\n      // const validation = validateSchema(data, schema)\n      // if (validation !== true)\n      //   throw new InvoiceXMLError(validation)\n\n      const parsed = new XMLParser(xmlopts).parse(data);\n      if (parsed.Invoice) this.assign(parsed.Invoice, false);\n      else throw new InvoiceXMLError();\n    } else if (typeof data === \"object\") this.assign(data);\n  }\n\n  assign(data: InvoiceType, validate: boolean = true) {\n    if (typeof data.DocumentType === \"string\")\n      data.DocumentType = {\n        invoice: 1,\n        \"credit note\": 2,\n        \"debit note\": 3,\n        \"proforma invoice\": 4,\n        \"advance invoice\": 5,\n        \"advance credit note\": 6,\n        simplified: 7,\n      }[data.DocumentType] as 1 | 2 | 3 | 4 | 5 | 6 | 7;\n\n    Object.assign(this, data);\n    if (validate) this.validate();\n  }\n\n  validate(xml?: string): true {\n    // const validation = validateSchema(xml || this.toXML(), schema)\n    // if (validation !== true)\n    //   throw new InvoiceXMLError(validation)\n    return true;\n  }\n\n  toXML(validate: boolean = true): string {\n    const data = {\n      $_xmlns: this.$_xmlns,\n      $_version: this.$_version,\n      DocumentType: this.DocumentType,\n      SubDocumentType: this.SubDocumentType,\n      SubDocumentTypeOrigin: this.SubDocumentTypeOrigin,\n      TargetConsolidator: this.TargetConsolidator,\n      ClientOnTargetConsolidator: this.ClientOnTargetConsolidator,\n      ClientBankAccount: this.ClientBankAccount,\n      ID: this.ID,\n      ID36: this.ID36,\n      UUID: this.UUID,\n      EgovFlag: this.EgovFlag,\n      ISDS_ID: this.ISDS_ID,\n      FileReference: this.FileReference,\n      ReferenceNumber: this.ReferenceNumber,\n      EgovClassifiers: this.EgovClassifiers,\n      IssuingSystem: this.IssuingSystem,\n      IssueDate: this.IssueDate,\n      TaxPointDate: this.TaxPointDate,\n      // LastValidDate: this.LastValidDate,\n      VATApplicable: this.VATApplicable,\n      ElectronicPossibilityAgreementReference:\n        this.ElectronicPossibilityAgreementReference,\n      Note: this.Note,\n      LocalCurrencyCode: this.LocalCurrencyCode,\n      ForeignCurrencyCode: this.ForeignCurrencyCode,\n      CurrRate: this.CurrRate,\n      RefCurrRate: this.RefCurrRate,\n      Extensions: this.Extensions,\n      AccountingSupplierParty: this.AccountingSupplierParty,\n      SellerSupplierParty: this.SellerSupplierParty,\n      AnonymousCustomerParty: this.AnonymousCustomerParty,\n      AccountingCustomerParty: this.AccountingCustomerParty,\n      BuyerCustomerParty: this.BuyerCustomerParty,\n      OrderReferences: this.OrderReferences,\n      DeliveryNoteReferences: this.DeliveryNoteReferences,\n      OriginalDocumentReferences: this.OriginalDocumentReferences,\n      ContractReferences: this.ContractReferences,\n      Delivery: this.Delivery,\n      InvoiceLines: this.InvoiceLines,\n      NonTaxedDeposits: this.NonTaxedDeposits,\n      TaxedDeposits: this.TaxedDeposits,\n      TaxTotal: this.TaxTotal,\n      LegalMonetaryTotal: this.LegalMonetaryTotal,\n      PaymentMeans: this.PaymentMeans,\n      SupplementsList: this.SupplementsList,\n    };\n\n    // noinspection JSUnusedGlobalSymbols\n    const builderOptions = {\n      ...xmlopts,\n      tagValueProcessor: (name: string, value: any): string =>\n        value instanceof Date ? value.toISOString().substring(0, 10) : value,\n    };\n\n    const xml =\n      `<?xml version='1.0' encoding='utf-8' ?>\\n` +\n      new XMLBuilder(builderOptions).build({ Invoice: data });\n    if (validate) this.validate(xml);\n    return xml;\n  }\n\n  toJSON(): string {\n    return JSON.stringify({ ...this }, null, 2);\n  }\n}\n\nexport { InvoiceType };\n","export default class InvoiceXMLError extends Error {\n  public errors ?: Error[]\n  constructor(errors ?: Error[]) {\n    super('Invalid Invoice' + (errors?.length ? `: ${errors[0].message}` : ''))\n    if (errors)\n      this.errors = errors\n  }\n}\n"]}