1

Buen dia.

Tengo el siguiente documento json de ejemplo.

Se trata de la estructura de un ticket o número de factura de un Retail, correspondiente a una empresa uruguaya del rubro llamada GEOCOM.

{
    "_id": "5f7c17c0c40a0ca0e9deb18e",
    "T": {
        "HD": {
            "HD01": "1",
            "HD02": "20201006",
            "HD03": "1001",
            "HD04": "4",
            "HD05": 4,
            "HD06": "20201006040539",
            "HD07": "",
            "HD08": "3736803",
            "HD09": "PYG",
            "HD10": "PYG",
            "HD11": "0010470000002",
            "HD12": "",
            "HD13": "",
            "HD14": "",
            "HD15": "",
            "HD16": "",
            "HD17": "",
            "HD18": "",
            "HD19": "",
            "HD20": "",
            "HD21": 46950,
            "HD22": "ICAE",
            "HD23": false,
            "HD24": false,
            "HD26": -34.0,
            "HDEL": {
                "HDE": [{
                        "HDE1": "DATA",
                        "HDE2": "stampedNumber",
                        "HDE3": "14350465"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ticketWait",
                        "HDE3": "0"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ticketRecovery",
                        "HDE3": ""
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ticketCancel",
                        "HDE3": "0"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ROUNDED",
                        "HDE3": "-34.0"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "REASON_CANCELLATION"
                    },
                    {
                        "HDE1": "ENCNC",
                        "HDE2": "NC_MOTI"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "VERSION",
                        "HDE3": "100"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "CASHIER_NAME",
                        "HDE3": "LETICIA MABEL"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "CASHIER_LAST_NAME",
                        "HDE3": "NOLDIN SARABIA"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "COUPON_CODES",
                        "HDE3": ""
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "BRANCH_OFFICE",
                        "HDE3": "CASA CENTRAL"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ECOMMERCE_SALE",
                        "HDE3": "0"
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ECOMMERCE_ORDER_NUMBER",
                        "HDE3": ""
                    },
                    {
                        "HDE1": "ENC",
                        "HDE2": "ECOMMERCE_ORDER_STATE",
                        "HDE3": ""
                    }
                ]
            },
            "ID": {
                "ID01": "SIN NOMBRE",
                "ID02": "444444017",
                "ID04": "N/A N/A",
                "ID05": "N/A",
                "ID06": "PARAGUAYAN_RUC"
            }
        },
        "SIEL": {
            "SIE": [{
                    "sequenceNumber": 0,
                    "itemId": "3010",
                    "itemDescription": "COSTELETA VACUNA BAN",
                    "unitAmount": 29950,
                    "quantity": 1.405,
                    "subTotalAmount": 42080,
                    "discountAmount": 0,
                    "totalAmount": 42080,
                    "taxes": {
                        "tax": [{
                            "taxRule": "2",
                            "affectedAmount": 40076,
                            "taxAmount": 2004,
                            "taxRuleRate": "0.05"
                        }]
                    }
                },
                {
                    "sequenceNumber": 1,
                    "itemId": "1010",
                    "itemDescription": "GALLETA CRIOLLA",
                    "unitAmount": 4800,
                    "quantity": 0.605,
                    "subTotalAmount": 2904,
                    "discountAmount": 0,
                    "totalAmount": 2904,
                    "taxes": {
                        "tax": [{
                            "taxRule": "1",
                            "affectedAmount": 2640,
                            "taxAmount": 264,
                            "taxRuleRate": "0.1"
                        }]
                    }
                },
                {
                    "sequenceNumber": 2,
                    "itemId": "4002",
                    "itemDescription": "LECHUGA NACIONAL X M",
                    "unitAmount": 1000,
                    "quantity": 2,
                    "subTotalAmount": 2000,
                    "discountAmount": 0,
                    "totalAmount": 2000,
                    "taxes": {
                        "tax": [{
                            "taxRule": "2",
                            "affectedAmount": 1905,
                            "taxAmount": 95,
                            "taxRuleRate": "0.05"
                        }]
                    }
                }
            ]
        },
        "SIL": {
            "SI": [{
                    "SI01": 0,
                    "SI02": "S",
                    "SI03": "2003010014058",
                    "SI04": "2003010014058",
                    "SI05": 1.405,
                    "SI06": "KG",
                    "SI07": 29950,
                    "SI08": 42080,
                    "SI09": "SCAN",
                    "TXL": [{
                        "taxRule": "2",
                        "taxAmount": 2004
                    }],
                    "ESI": [{
                        "name": "grossPrice",
                        "value": "42080"
                    }]
                },
                {
                    "SI01": 1,
                    "SI02": "S",
                    "SI03": "2001010006059",
                    "SI04": "2001010006059",
                    "SI05": 0.605,
                    "SI06": "KG",
                    "SI07": 4800,
                    "SI08": 2904,
                    "SI09": "SCAN",
                    "TXL": [{
                        "taxRule": "1",
                        "taxAmount": 264
                    }],
                    "ESI": [{
                        "name": "grossPrice",
                        "value": "2904"
                    }]
                },
                {
                    "SI01": 2,
                    "SI02": "S",
                    "SI03": "2404002000026",
                    "SI04": "2404002000026",
                    "SI05": 2.0,
                    "SI06": "UN",
                    "SI07": 1000,
                    "SI08": 2000,
                    "SI09": "SCAN",
                    "TXL": [{
                        "taxRule": "2",
                        "taxAmount": 95
                    }],
                    "ESI": [{
                        "name": "grossPrice",
                        "value": "2000"
                    }]
                }
            ]
        },
        "DIL": {
            "DI": [

            ]
        },
        "PML": {
            "PM": [{
                "PM01": "1",
                "PM02": 46950,
                "PM03": "PYG",
                "PM04": false,
                "PM05": 46950,
                "PM06": 0,
                "EPM": [{
                        "name": "Payment_Mode_Name",
                        "value": "Efectivo"
                    },
                    {
                        "name": "GROUP_Payment_Mode_Name",
                        "value": "DATA"
                    }
                ]
            }]
        }
    },
    "updated": "2020-10-06T07:07:44.433+0000"
}

Necesito hacer una consulta en mongo basada en la siguiente premisa:

  • La suma de todos los impuestos (taxes), agrupados por gravadas 10 e iva 10 (taxRule:2), gravadas 5 e iva 5 (taxRule:1) y exentas (taxRule:0), separados por tipo de documento (Venta y Nota de credito) y en un rango de tiempo especifico.

No estoy seguro de si esta consulta es simple o compleja de obtener. He realizado un curso básico-intermedio de Mongo en Udemy. Estoy en esto hace solo una semana. He logrado consultar varias cosas, pero esto parece muy dificil.

Espero que puedan ayudarme.

Referencia:

  • T.HD.HD11 corresponde al Número de factura

  • T.HD.HD02 es la fecha

  • T.HD.HD21 es el monto de la factura

  • T.SIEL.SIE son los detalles de la venta

    • T.SIEL.SIE.taxes.tax son los impuestos dentro del detalle
    • T.SIEL.SIE.taxes.tax.taxRule es el tipo de impuesto, el cual esta detallado mas arriba
    • T.SIEL.SIE.taxes.tax.affectedAmount es el monto de las gravadas
    • T.SIEL.SIE.taxes.tax.taxAmount es el monto de las ivas (impuesto al valor agregado, en mi pais)
  • T.SIL.SI.SI02 corresponde al tipo de documento (S para venta), (C para Nota de credito)

Añado el codigo SQL que utilizo para obtener esta información desde MariaDB

select t.localid, 
case
    when t.documenttype='sale' then 'Venta' 
    when t.documenttype='return' then 'Nota Credito' end 'Documento',
sum(if(tet.taxid=1,tet.affectedamount,0)) 'Gravada 10',
sum(if(tet.taxid=1,tet.amount,0)) as 'Iva 10',
sum(if(tet.taxid=2,tet.affectedamount,0)) 'Gravada 5', 
sum(if(tet.taxid=2,tet.amount,0)) as 'Iva 5',
sum(if(tet.taxid=0,tet.affectedamount,0)) 'Exenta' from tickets t
inner join ticketentriestaxes tet on
    (t.pos=tet.pos and t.localid=tet.localid and t.opendate=tet.opendate and t.ticketnumber=tet.ticketnumber)
    where date(t.opendate) >= '2021-04-16' and date(t.opendate) <= '2021-04-16' and t.state='D' and tet.taxid in (0,1,2) 
    and t.documenttype in ('sale','return') 
    group by t.documenttype;

Con el cual obtengo la informacion de esta manera

introducir la descripción de la imagen aquí

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.