skilld-labs / go-odoo

Golang wrapper for Odoo API
Apache License 2.0
86 stars 62 forks source link

Error when try to get ir.attachment #39

Closed jsebasmuller closed 1 year ago

jsebasmuller commented 1 year ago

Hello friends, I am trying to obtain the ir.attachment model that is related to the sale.order, I am already obtaining the id and name of the relationship but when obtaining all the fields of the ir.attachment by id, it fails me with the following mistake:

"reading body XML syntax error on line 88: illegal character code U+000C"

I would be very grateful if you could help me,

Thank you very much for the help

ahuret commented 1 year ago

Hello @jsebasmuller , it looks like there's an illegal caracter in the http response, could you please provider a dump of the response ?

jsebasmuller commented 1 year ago

Hello @ahuret I am doing the direct query with the go-odoo library with the following method:

attachment, err := r.conn.GetIrAttachment(idAttachment)

in the err is directly the message that I sent from the error of this post

ahuret commented 1 year ago

I was not able to reproduce when I tried to add this caracter on my odoo instance in an ir_attachment file... can you please provide more context ? which version of go-odoo are you using ? maybe you can share the concerning file too ? So I can reproduce and debug.

jsebasmuller commented 1 year ago

I am currently using the latest version, which is 1.5.8, I can share the part of the code where it is generating an error, r.conn is the client that I use to connect to odoo:

func GetOdooConnection(config *config.OdooConfig) (*odoo.Client, error) {
    var err error
    ones.Do(func() {
        clientOdoo, err = odoo.NewClient(&odoo.ClientConfig{
            Admin:    config.Admin,
            Password: config.Password,
            Database: config.DataBase,
            URL:      config.Url,
        })
    })
    return clientOdoo, err
}

This is the connection that I make when I start the project server and inject it into the repository file where I am doing the attachment query, this is the function that I use to get all the data from a user's sale.order and with that I get sale.order ir.attachment:

func (r *OrdersRepository) GetPurchaseOrdersHistory(rs models.UserId, page int, sales bool) (*[]models.PurchaseOrderHistory, error) {
    //Consulta a Odoo
    criteria := odoo.NewCriteria().Add("partner_id.id", "=", int(rs.UserId))
    if sales {
        criteria.Add("state", "=", "sale")
    }
    response, err := findSaleOrdersStructStaff(r.conn, criteria, odoo.NewOptions().Offset(page).Limit(10))
    if err != nil {
        return nil, err
    }
    ret := []models.PurchaseOrderHistory{}

    for _, order := range *response {
        if order.Attachment != nil {
            attachment, err := r.conn.GetIrAttachment(utils.DefaultIfNilMany2OneID(order.Attachment))
            if err != nil {
                return nil, err
            }
            fmt.Println(attachment)
        }
        idsOrderLine := utils.DefaultIfNilRelation(order.OrderLine)
        saleOrder := &odoo.SaleOrderLines{}
        if len(idsOrderLine) > 0 {
            saleOrder, err = r.conn.GetSaleOrderLines(idsOrderLine)
            if err != nil {
                return nil, err
            }
        }
        idsProducts := []int64{}
        for _, item := range *saleOrder {
            idProduct := utils.DefaultIfNilMany2OneID(item.ProductId)
            if idProduct != 0 {
                idsProducts = append(idsProducts, idProduct)
            }
        }
        products := &[]productProductStaff{}
        if len(idsProducts) > 0 {
            products, err = getProductProductsStaff(r.conn, idsProducts)
            if err != nil {
                return nil, err
            }
        }
        sku := []string{}
        for _, item := range *products {
            sku = append(sku, utils.DefaultIfNilMany2OneName(item.CodeId))
        }
        invoices := &[]AccountMoveStaff{}
        invoiceIds := utils.DefaultIfNilRelation(order.InvoiceIds)
        if len(invoiceIds) > 0 {
            invoices, err = getAccountMovesStaff(r.conn, invoiceIds)
            if err != nil {
                return nil, err
            }
        }

        paymentState := ""
        for _, item := range *invoices {
            if utils.DefaultIfNilSelection(item.State) == "posted" {
                paymentState = utils.DefaultIfNilSelection(item.PaymentState)
            }
        }

        fmt.Printf("\n Estado de pago \n %+v \n ", utils.DefaultIfNilSelection(order.PaymentState))

        ret = append(ret, models.PurchaseOrderHistory{
            Id:             order.Id.Get(),
            Incoterm:       utils.DefaultIfNilMany2OneName(order.Incoterm),
            DisplayName:    utils.DefaultIfNilString(order.DisplayName),
            CreateDate:     utils.DefaultIfNilDateTime(order.DateOrder),
            EndDate:        utils.DefaultIfNilDateTime(order.ValidityDate),
            ETA:            utils.DefaultIfNilDateTime(order.DeliveryDate),
            PaymentTerm:    utils.DefaultIfNilMany2OneName(order.PaymentTermId),
            State:          utils.DefaultIfNilSelection(order.State),
            SKU:            strings.Join(sku, ","),
            EffectiveDate:  utils.DefaultIfNilDateTime(order.EffectiveDate),
            ClientOrderRef: utils.DefaultIfNilString(order.ClientOrderRef),
            PickingIds:     utils.DefaultIfNilRelation(order.PickingIds),
            PaymentState:   paymentState,
            InvoiceStatus:  utils.DefaultIfNilSelection(order.InvoiceStatus),
        })
    }

    return &ret, nil
}
ahuret commented 1 year ago

Actually, the code is working as expected but issue here is about the ir_attachment itself which have invalid character. I believe the invalid character come from the file related to this attachment itself. This is related to the xmlrpc client we use that try to parse the received xml. Maybe you can share the file with me (in private) so I can try to reproduce using my odoo instance ?

ahuret commented 1 year ago

Hello @jsebasmuller ! Any news ? Have you been able to resolve this issue ? Please tell me if you still need help

ahuret commented 1 year ago

closing it for now feel free to open it again if you think this it still relevant

jsebasmuller commented 1 year ago

Hello @ahuret, I share the file that I download directly from the odoo website, this same file is the one that fails to download with the code that I shared earlier, I hope you can reproduce the error so that you can help me.

https://drive.google.com/file/d/1OP5H8UTUzULXzH3e3cf6pm_ewmV_rmiv/view?usp=sharing

jsebasmuller commented 1 year ago

I was finally able to get the file but it was consuming the endpoint directly from odoo with postman, here I leave you the answer to see if you can see what is wrong so that it fails in the response of the library

https://drive.google.com/file/d/1mSR5epH2_lu0Vsrub_6VTyX5Yibw3Piw/view?usp=share_link