Search code examples
c#odoorpcpgadmin

Odoo rpc query with join in C#


I have this type of code to make a selection from Odoo Rpc:

XmlRpcClient client = new XmlRpcClient();
client.Url = LocalApplication.Url;
client.Path = "common";

// LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, LocalApplication.Pass, XmlRpcParameter.EmptyStruct());

XmlRpcResponse responseLogin = client.Execute(requestLogin);

// READ
client.Path = "object";

// var x = client.Execute("select * from res_partner");
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
                        LocalApplication.Pass, "res.partner", "search_read",
                        XmlRpcParameter.AsArray(
                           XmlRpcParameter.AsArray(
                              XmlRpcParameter.AsArray("is_agent", "=", "True".ToLower()))),
                        XmlRpcParameter.AsStruct(
                           XmlRpcParameter.AsMember("fields",
                              XmlRpcParameter.AsArray("name", "id", "phone", "email"))));

// SAVE
XmlRpcResponse responseSearch = client.Execute(requestSearch);

var agents = (responseSearch.GetObject() as List<object>);
Taswiq.Bussiness.Person p = new Person(LocalApplication.ConnectionString);

if (agents != null)
{
    foreach (var a in agents)
    {
        Dictionary<string, object> agent = a as Dictionary<string, object>;
        p.Name = agent["name"].ToString() == "False" ? "ND" : agent["name"].ToString();
        p.Marca = Convert.ToInt32(agent["id"]);
        p.Type = 1;
        p.Phone = agent["phone"].ToString() == "False" ? "ND" : agent["phone"].ToString();
        p.Email = agent["email"].ToString() == "False" ? "ND" : agent["email"].ToString();

        MessageStruct result = p.Save();

        if (result.HasErrors)
        {
            return result.Message;
        }
    }

    return "Success";
}

All works fine, but now I need to make some join from this 2 tables like this:

Select * 
from product_template as d 
inner join product_product as r on r.product_tmpl_id = d.id;

I can't find anything but Python code.

Can anyone help me with that?


Solution

  • I think I found a solution. I do not know if is the best way but in my case it solved the problem.

    I have made same list with every table that I need to go with join:

    public List<object> lstStock = new List<object>();
    
     public void PopulateStockList()
        {
            XmlRpcClient stock = new XmlRpcClient();
            stock.Url = LocalApplication.Url;
            stock.Path = "common";
    
            //LOGIN BG
            XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
            requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, 
    LocalApplication.Pass,
                XmlRpcParameter.EmptyStruct());
    
            XmlRpcResponse responseLogin = stock.Execute(requestLogin);
    
            //READ
    
            stock.Path = "object";
    
            XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
            requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
                LocalApplication.Pass, "stock.quant", "search_read",
                XmlRpcParameter.AsArray(
                    XmlRpcParameter.AsArray(
                        XmlRpcParameter.AsArray("id", ">", 0)
                    )
                ),
                XmlRpcParameter.AsStruct(
                    XmlRpcParameter.AsMember("fields",
                        XmlRpcParameter.AsArray("product_id", "quantity", "in_date", 
    "owner_id"))
                )
            );
            XmlRpcResponse responseSearch = stock.Execute(requestSearch);
            lstStock = (responseSearch.GetObject() as List<object>);
        }
    

    Same way for every table that i need to join with. And in the end, inside the foreach i made that join that i need to populate my sql table

     public string DownloadRpcStock()
        {
            try
            {
                PopulateProdTemplate();
                PopulateStockList();
                PopulateProductList();
    
                Stock s = new Stock(LocalApplication.ConnectionString);
    
                foreach (var st in lstStock)
                {
                    Dictionary<string, object> stc = st as Dictionary<string, object>;
                    var productId = stc["product_id"] as List<object>;
                    if (productId != null)
                        s.IdProduct = Convert.ToInt32(productId[0]); //Din lista care vine pe product_id imi iau valoarea dorita
                    s.IdStorage = 1;
                    s.StockQuantity = Convert.ToDouble(stc["quantity"]);
                    s.Rest = Convert.ToDouble(stc["quantity"]);
    
                    var prod = lstProduct.FirstOrDefault(
                        k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) == Convert.ToInt32((stc["product_id"] as List<object>)[0])) as Dictionary<string, object>;
    
                    var prodTemplate =
                        lstProdTemplate.FirstOrDefault(
                            k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) ==
                                 Convert.ToInt32((prod["product_tmpl_id"] as List<object>)[0])) as Dictionary<string, object>;
                    if (prodTemplate != null)
                        s.Price = Convert.ToDouble(prodTemplate["list_price"]);
                    s.Batch = "NoBatch"; // Provizoriu
                    s.Date = Convert.ToDateTime(stc["in_date"]);
                    s.ExpireDate = Convert.ToDateTime(stc["in_date"]); // Provizoriu
                    s.IdCustomerSuplier = Convert.ToInt32(stc["owner_id"]);
                    s.Id = 0; //if (Id == 0) act = insert
                    MessageStruct result = s.Save();
                    if (result.HasErrors)
                    {
                        return result.Message;
                    }
                }
    
                return "Success";
            }
    
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    

    I found many such questions on the internet without a solution. I hope my solution will help someone else in the future.