diff --git a/src/org/openbravo/kikobar/intfactsunat/query-facturador.sql b/src/org/openbravo/kikobar/intfactsunat/query-facturador.sql index 9d75625..78f7add 100644 --- a/src/org/openbravo/kikobar/intfactsunat/query-facturador.sql +++ b/src/org/openbravo/kikobar/intfactsunat/query-facturador.sql @@ -8,7 +8,8 @@ ventas as ( select division.name as organisation,parent.name as parentorg,ad_orginfo.taxid as ruc, ad_client.name as client,documentno,c_invoice.description,dateinvoiced,totallines,grandtotal, qtyinvoiced,priceactual, -linenetamt,c_invoiceline.taxbaseamt,m_product.value as productcode,m_product.name as productname,iso_code, +linenetamt,c_invoiceline.taxbaseamt,m_product.value as productcode,m_product.name as productname, +c_invoiceline.description as productdesc, iso_code, c_invoiceline.line,c_invoice.c_invoice_id,c_invoiceline.c_invoiceline_id, c_bpartner.name as bpname,c_bpartner.taxid,c_uom.name as uom,c_doctype.name as doctype, c_paymentterm.name as paymentterm,c_paymentterm.netdays, @@ -17,7 +18,8 @@ c_invoicetax.taxbaseamt as invoicetaxbaseamt,c_invoicetax.taxamt as invoicetaxta taxinvoice.name, c_invoicelinetax.taxbaseamt as linetaxbaseamt,c_invoicelinetax.taxamt as linetaxtaxamt, taxline.name, -fin_payment_schedule.amount as cuota, fin_payment_schedule.duedate, fin_payment_schedule.fin_payment_schedule_id +fin_payment_schedule.amount as cuota, fin_payment_schedule.duedate, fin_payment_schedule.fin_payment_schedule_id, +concat(c_location.address1,' ',c_location.address2,' ',c_location.city,' ',c_location.postal,' ',c_country.name) as direccioncliente from @@ -38,6 +40,9 @@ 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 left join fin_payment_schedule on fin_payment_schedule.c_invoice_id = c_invoice.c_invoice_id +left join c_bpartner_location on c_bpartner_location.c_bpartner_location_id = c_invoice.c_bpartner_location_id +left join c_location on c_location.c_location_id = c_bpartner_location.c_location_id +left join c_country on c_country.c_country_id = c_location.c_country_id @@ -46,6 +51,7 @@ c_invoice.documentno = 'E001-27' and --c_invoice.documentno = '1000052' and c_invoice.issotrx = 'Y' and c_invoice.isactive = 'Y' and +c_invoice.docstatus = 'CO' and ad_client.name ='BLB' --ad_client.name ='Locate & Grow' --ad_client.name ='Uchu Wasi' @@ -69,10 +75,25 @@ select 'fecEmision',dateinvoiced::date, 'horEmision',dateinvoiced::time, 'codLocalEmisor','0001', - 'tipDocUsuario','6', + 'tipDocUsuario',case + when length(taxid)=11 then '6' + when length(taxid)=8 then '1' + else '0' end, 'numDocUsuario',taxid, 'rznSocialUsuario',bpname, 'tipMoneda',iso_code, + 'codMotivo',case + when substring(doctype,1,4)='Nota' then '01' + else '-' end, + 'desMotivo',case + when substring(doctype,1,4)='Nota' then 'Anulacion de operacion' + else '-' end, + 'tipDocAfectado',case + when substring(doctype,1,4)='Nota' then '01' + else '-' end, + 'numDocAfectado',case + when substring(doctype,1,4)='Nota' then description + else '-' end, 'sumTotTributos',(grandtotal-totallines)::varchar, 'sumTotValVenta',totallines::varchar, 'sumPrecioVenta',grandtotal::varchar, @@ -92,10 +113,15 @@ select 'invoiceid',c_invoice_id, 'codPaisCliente','PE', 'codUbigeoCliente','150140', - 'desDireccionCliente','AV. ANGAMOS 1111, SURCO, LIMA', + 'desDireccionCliente',direccioncliente, 'codPaisEntrega','PE', 'codUbigeoEntrega','150140', - 'desDireccionEntrega','AV. ANGAMOS 1111, SURCO, LIMA' + 'desDireccionEntrega',direccioncliente, + 'ctaBancoNacionDetraccion','11111111111', + 'codBienDetraccion','037', + 'porDetraccion','10.0', + 'mtoDetraccion','0.00', + 'codMedioPago','003' ) )->0 ) @@ -106,9 +132,9 @@ select 'invoiceline',c_invoiceline_id, 'codUnidadMedida','BX', 'ctdUnidadItem',qtyinvoiced::varchar, - 'codProducto',productcode, + 'codProducto',coalesce(productcode,'-'), 'codProductoSUNAT','50161813', - 'desItem',productname, + 'desItem',coalesce(productname,productdesc,'-'), 'mtoValorUnitario',priceactual::varchar, 'sumTotTributosItem',linetaxtaxamt::varchar, @@ -135,7 +161,8 @@ select 'codTipTributoOtroItem','OTH', 'porTriOtroItem','0.00', - 'codTriIcbper','7152', +-- 'codTriIcbper','7152', + 'codTriIcbper','-', 'mtoTriIcbperItem','0.00', 'ctdBolsasTriIcbperItem','0', 'nomTributoIcbperItem','ICBPER', @@ -182,26 +209,34 @@ select 'variablesGlobales','[]'::jsonb ) || - jsonb_build_object( - 'datoPago', jsonb_agg(DISTINCT - jsonb_build_object( - 'formaPago','Credito', - 'mtoNetoPendientePago',grandtotal::varchar, - 'tipMonedaMtoNetoPendientePago',iso_code - ) - )->0 - ) + case + when substring(doctype,1,4)='Bole' then jsonb_build_object('datoPagoX','[]'::jsonb) + else + jsonb_build_object( + 'datoPago', jsonb_agg(DISTINCT + jsonb_build_object( + 'formaPago','Credito', + 'mtoNetoPendientePago',grandtotal::varchar, + 'tipMonedaMtoNetoPendientePago',iso_code + ) + )->0 + ) + end || - jsonb_build_object( - 'detallePago', jsonb_agg(DISTINCT - jsonb_build_object( - 'cuotaid',fin_payment_schedule_id, - 'mtoCuotaPago',cuota::varchar, - 'fecCuotaPago',duedate::date, - 'tipMonedaCuotaPago',iso_code + case + when substring(doctype,1,4)='Bole' then jsonb_build_object('detallePagoX','[]'::jsonb) + else + jsonb_build_object( + 'detallePago', jsonb_agg(DISTINCT + jsonb_build_object( + 'cuotaid',fin_payment_schedule_id, + 'mtoCuotaPago',abs(cuota)::varchar, + 'fecCuotaPago',duedate::date, + 'tipMonedaCuotaPago',iso_code + ) ) ) - ) + end || jsonb_build_object( 'retencion', jsonb_agg(DISTINCT @@ -212,8 +247,9 @@ select ) )->0 ) - )::varchar + )::varchar,doctype,ruc --*/ -from ventas; +from ventas +group by doctype,ruc;