Add query.sql as proof of concept to extrat json data to feed the Facturador SUNAT app

This commit is contained in:
Enrique Barcelli 2021-12-03 00:53:42 +08:00
parent 77006e5275
commit c2239c2b3c

View file

@ -0,0 +1,74 @@
-- This query extract invoices from the Openbravo Postgres database to generate
-- a JSON document for submitting through the FACTURADOR SUNAT application.
-- FACTURADOR SUNAT is a free application for generating Facturas Electronicas SUNAT.
with
ventas as (
select division.name as organisation,parent.name as parentorg,ad_orginfo.taxid as ruc,
ad_client.name,documentno,c_invoice.description,dateinvoiced,totallines,grandtotal,qtyinvoiced,priceactual,
linenetamt,c_invoiceline.taxbaseamt,m_product.value,m_product.name,iso_code,
c_bpartner.name,c_bpartner.taxid,c_uom.name as uom,c_doctype.name,c_paymentterm.name as paymentterm,c_paymentterm.netdays,
c_invoice.isactive,c_invoice.issotrx,
c_invoicetax.taxbaseamt,c_invoicetax.taxamt,
taxinvoice.name,
c_invoicelinetax.taxbaseamt,c_invoicelinetax.taxamt,
taxline.name
from
c_invoice
left join c_invoiceline on c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
left join m_product on m_product.m_product_id = c_invoiceline.m_product_id
left join ad_client on ad_client.ad_client_id = c_invoice.ad_client_id
left join c_currency on c_currency.c_currency_id = c_invoice.c_currency_id
left join c_bpartner on c_bpartner.c_bpartner_id = c_invoice.c_bpartner_id
left join c_uom on c_uom.c_uom_id = c_invoiceline.c_uom_id
left join c_doctype on c_doctype.c_doctype_id = c_invoice.c_doctype_id
left join c_paymentterm on c_paymentterm.c_paymentterm_id = c_invoice.c_paymentterm_id
left join c_invoicetax on c_invoicetax.c_invoice_id = c_invoice.c_invoice_id
left join c_tax as taxinvoice on taxinvoice.c_tax_id = c_invoicetax.c_tax_id
left join c_invoicelinetax on c_invoicelinetax.c_invoiceline_id = c_invoiceline.c_invoiceline_id
left join c_tax as taxline on taxline.c_tax_id = c_invoicelinetax.c_tax_id
left join ad_org as division on division.ad_org_id = c_invoice.ad_org_id
left join ad_org as parent on parent.ad_org_id = division.ad_legalentity_org_id
left join ad_orginfo on ad_orginfo.ad_org_id = parent.ad_org_id
where
c_invoice.documentno = 'E001-27' and
c_invoice.issotrx = 'Y' and
c_invoice.isactive = 'Y' and
ad_client.name ='BLB'
)
-- Above this line is the full query select all the required invoice data
-- Below this line a JSON sturcture is created to be feed into the FACTURADOR SUNAT APP.
select
jsonb_build_object(
'encabezado', jsonb_agg(
jsonb_build_object(
'factura',ventas.documentno,
'terminopago',ventas.paymentterm,
'organizacion',ventas.organisation,
'parent',ventas.parentorg
)
)
)
||
jsonb_build_object(
'lineas', jsonb_agg(
jsonb_build_object(
'dias',ventas.netdays,
'unidad',ventas.uom
)
)
)
||
jsonb_build_object(
'manual','creado a mano'
)
from ventas;