/// <summary> /// Use an existint collection to load entities /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service"></param> /// <param name="resultset"></param> /// <param name="collection"></param> /// <returns></returns> public static void BuildEntities <T>(OdooService service, ResultSet resultset, ICollection <T> collection) where T : IOdooObject, new() { foreach (object entity in resultset.Data) { var xmlStruct = (XmlRpcStruct)entity; collection.Add(BuildObject <T>(service, xmlStruct)); } }
/// <summary> /// Create entities and load values /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service"></param> /// <param name="resultset"></param> /// <returns></returns> public static ICollection <T> BuildEntities <T>(OdooService service, ResultSet resultset) where T : IOdooObject, new() { var entityCollection = new OdooCollection <T>(service); BuildEntities(service, resultset, entityCollection); return(entityCollection); }
public void FirstOrDefaultItemsNotFoundTest() { var service = new OdooService(Connection); var partner = service.FirstOrDefault(OdooFilter <OdooPartner> .Where(x => x.Id == 0)); Assert.IsNull(partner); }
public void FirstOrDefaultItemsNotFoundTest() { var service = new OdooService(Connection); var partner = service.FirstOrDefault(OdooFilter<OdooPartner>.Where(x => x.Id == 0)); Assert.IsNull(partner); }
public void FirstItemsFoundTest() { var service = new OdooService(Connection); var partner = service.First(OdooFilter <OdooPartner> .Where(x => x.Id == 3354)); Assert.IsNotNull(partner); Assert.That(partner.Id == 3354); }
public void FirstItemsFoundTest() { var service = new OdooService(Connection); var partner = service.First(OdooFilter<OdooPartner>.Where(x => x.Id == 3354)); Assert.IsNotNull(partner); Assert.That(partner.Id == 3354); }
public static T First <T>(this OdooService service, OdooFilter <T> filter) where T : IOdooObject, new() { var result = service.GetEntities(filter.Filter).ToList(); if (result.Any()) { return(result.First()); } throw new ArgumentNullException(); }
public void ObjectWithlistTest() { var service = new OdooService(Connection); var project = service.FirstOrDefault <OdooProject>(OdooFilter <OdooProject> .Where(x => x.Id == 431)); var first = project.PartnerInvoices.First(); Assert.That(project.PartnerInvoices.Any()); }
public void BasicListTest() { var service = new OdooService(Connection); var results = service.List <OdooPartner>(OdooFilter <OdooPartner> .Where(x => true), OdooSorter <OdooPartner> .OrderBy(x => x.Id), 0, 4); Assert.IsNotNull(results); Assert.That(results.Any()); Assert.That(results.Count == 4); }
public void ObjectWithlistTest() { var service = new OdooService(Connection); var project = service.FirstOrDefault<OdooProject>(OdooFilter<OdooProject>.Where(x => x.Id == 431)); var first = project.PartnerInvoices.First(); Assert.That(project.PartnerInvoices.Any()); }
public void BasicListTest() { var service = new OdooService(Connection); var results = service.List<OdooPartner>(OdooFilter<OdooPartner>.Where(x=> true), OdooSorter<OdooPartner>.OrderBy(x=> x.Id), 0, 4); Assert.IsNotNull(results); Assert.That(results.Any()); Assert.That(results.Count == 4); }
public void CreateAccountVoucher() { var odooService = new OdooService(Connection); var partnerFilter = new object[] { new object[] { "vat", "=", "TR1234567890" } }; var partnerIds = odooService.Search("res.partner", partnerFilter, 0, 1); if (partnerIds.Length == 1) { var voucherDescription = string.Format("Card No:{0} - Card User Name:{1} - Installment:{2}", "402278******4543", "ABDURRAHMAN ISIK", 3); var accountJournalFilter = new object[] { new object[] { "code", "=", "GRTRY" } }; var accountJournalFields = new string[] { "id", "code", "default_debit_account_id", "default_credit_account_id", "type" }; var accountJournals = odooService.SearchAndRead("account.journal", accountJournalFilter, accountJournalFields); int accountId = 0, journalId = 0; foreach (var accountJournal in accountJournals) { foreach (DictionaryEntry de in accountJournal) { if ((string)de.Key == "id") { journalId = (int)de.Value; } if ((string)de.Key == "default_debit_account_id") { accountId = int.Parse((de.Value as object[])[0].ToString()); } } } var accountVoucher = new XmlRpcStruct(); accountVoucher.Add("partner_id", partnerIds[0]); accountVoucher.Add("journal_id", journalId); accountVoucher.Add("name", voucherDescription); accountVoucher.Add("account_id", accountId); accountVoucher.Add("type", "receipt"); accountVoucher.Add("reference", "10201713161549"); // Amount has to be double type, decimal is not supported by odoo // You'll getting an exception like below // Exception: A parameter is of, or contains an instance of, type CookComputing.XmlRpc.XmlRpcStruct which cannot be mapped to an XML-RPC type accountVoucher.Add("amount", 250.85); accountVoucher.Add("date", DateTime.Now.ToString("yyyy-MM-dd")); var result = odooService.Create("account.voucher", accountVoucher); Assert.IsTrue(result > 0); var trigger = odooService.ButtonProformaVoucher("account.voucher", new int[] { result }); Assert.IsTrue(trigger.Count > 0); } }
public static int AddOrUpdate <T>(this OdooService service, T item) where T : IOdooObject { if (item.Id == 0) { return(service.AddEntity(item)); } else { return(service.UpdateEntity(item)); } }
public void AddTest() { var service = new OdooService(Connection); var partner = new OdooPartner { Email = "*****@*****.**", IsCompany = true, Name = "Test Company" }; var id = service.AddOrUpdate(partner); Assert.That(id > 0); var check = service.First(OdooFilter<OdooPartner>.Where(x => x.Id == id)); Assert.That(check.Name == "Test Company"); }
private static void BuildSubOject <T>(OdooService service, T obj, OdooMapAttribute propInfo) { var props = obj.GetType() .GetProperties() .Where(p => p.GetCustomAttributes(typeof(OdooForeignKeyAttribute), true).Any()); var objProp = props.FirstOrDefault( x => x.GetCustomAttribute <OdooForeignKeyAttribute>() .PropertyName == propInfo.OdooName); if (objProp != null) { } }
public void ListPagingOrderByDescTest() { var service = new OdooService(Connection); var results = service.List <OdooPartner>(OdooFilter <OdooPartner> .Where(x => true), OdooSorter <OdooPartner> .OrderByDescending(x => x.Id), 0, 2); var results2 = service.List <OdooPartner>(OdooFilter <OdooPartner> .Where(x => true), OdooSorter <OdooPartner> .OrderByDescending(x => x.Id), 2, 2); Assert.IsNotNull(results); Assert.That(results.Any()); Assert.That(results.Count == 2); Assert.That(results.First().Id > results2.First().Id); Assert.IsNotNull(results2); Assert.That(results2.Any()); Assert.That(results2.Count == 2); }
public void AddTest() { var service = new OdooService(Connection); var partner = new OdooPartner { Email = "*****@*****.**", IsCompany = true, Name = "Test Company" }; var id = service.AddOrUpdate(partner); Assert.That(id > 0); var check = service.First(OdooFilter <OdooPartner> .Where(x => x.Id == id)); Assert.That(check.Name == "Test Company"); }
public void CreateResPartner() { var odooService = new OdooService(Connection); var resPartner = new XmlRpcStruct(); resPartner.Add("name", "TwoByFour Inc."); resPartner.Add("street", "Güneştepe Mh. Çalışkan Sk. No:7/9 Güngören - İstanbul, TR"); resPartner.Add("active", true); resPartner.Add("balance", 25.50); resPartner.Add("opt_out", false); resPartner.Add("credit", 25.50); resPartner.Add("credit_limit", 100.000); var result = odooService.Create("res.partner", resPartner); Assert.IsNotNull(result); }
public static ICollection <T> List <T>(this OdooService service, OdooFilter <T> filter, OdooSorter <T> sorter = null, int?offset = null, int?limit = null) where T : IOdooObject, new() { if (sorter != null & (offset == null || limit == null)) { throw new ArgumentNullException("sorter", "A sorter requires offset and limit"); } if ((offset != null && limit == null) || (offset == null && limit != null)) { throw new ArgumentNullException("offset", "page and offset are required"); } if (sorter != null) { return(service.GetEntities(filter.Filter, offset, limit, sorter.Order) .ToList()); } return(service.GetEntities(filter.Filter).ToList()); }
public static void Delete <T>(this OdooService service, T item) where T : IOdooObject { service.DeleteEntity(item); }
public void ListPagingOrderByDescTest() { var service = new OdooService(Connection); var results = service.List<OdooPartner>(OdooFilter<OdooPartner>.Where(x => true), OdooSorter<OdooPartner>.OrderByDescending(x => x.Id), 0, 2); var results2 = service.List<OdooPartner>(OdooFilter<OdooPartner>.Where(x => true), OdooSorter<OdooPartner>.OrderByDescending(x => x.Id), 2, 2); Assert.IsNotNull(results); Assert.That(results.Any()); Assert.That(results.Count == 2); Assert.That(results.First().Id > results2.First().Id); Assert.IsNotNull(results2); Assert.That(results2.Any()); Assert.That(results2.Count == 2); }
public void PartnerListTest() { var odooService = new OdooService(Connection); // res.partner search filter for user erp code // Sample erp code: TR1234567890 var partnerFilter = new object[] { new object[] { "vat", "=", "TR1234567890" }, }; // res.partner for partner id var partnerIds = odooService.SearchAndRead("res.partner", partnerFilter, new string[] { "id", "name", "user_id", "ref" }, 0, 1); // account.account.type search filter // fields: receivable, payable var accountTypeFilter = new object[] { new object[] { "code", "in", new object[] { "receivable", "payable" } }, }; var accountTypes = odooService.SearchAndRead("account.account.type", accountTypeFilter, new string[] { "id" }); var accountTypeIdList = new List <int>(10); foreach (var accountType in accountTypes) { foreach (DictionaryEntry de in accountType) { int accountTypeId = (int)de.Value; accountTypeIdList.Add(accountTypeId); } } // account.account search filter // fields: user_type var accountFilter = new object[] { new object[] { "user_type", "in", accountTypeIdList.ToArray() } }; var accounts = odooService.SearchAndRead("account.account", accountFilter, new string[] { "id" }); var accountIdList = new List <int>(50); foreach (var account in accounts) { foreach (DictionaryEntry de in account) { int accountId = (int)de.Value; accountIdList.Add(accountId); } } int partnerId = 0; foreach (var partner in partnerIds) { foreach (DictionaryEntry de in partner) { if ((string)de.Key == "id") { partnerId = (int)de.Value; } } } // account.move.line search filter // fields: partner_id and account_id var accountMoveFilter = new object[] { new object[] { "partner_id", "=", partnerId }, new object[] { "account_id", "in", accountIdList.ToArray() } }; // account.move.line select fields var accountMoveFields = new string[] { "name", "date", "date_maturity", "currency_id", "account_id", "amount_currency", "debit", "credit" }; var accountMoveList = odooService.SearchAndRead("account.move.line", accountMoveFilter, accountMoveFields); var accountMoveModelList = new List <AccountMoveLine>(50); foreach (var accountMove in accountMoveList) { var collection = new Dictionary <string, object>(); var accountMoveModel = new AccountMoveLine(); foreach (DictionaryEntry de in accountMove) { if (!collection.ContainsKey((string)de.Key)) { collection.Add((string)de.Key, de.Value); } } foreach (var parameter in collection) { // We're continue if parameter value is coming null from Odoo. if (parameter.Value is bool && !(bool)parameter.Value) { continue; } if (parameter.Key == "name") { accountMoveModel.Name = Convert.ToString(parameter.Value); } if (parameter.Key == "date") { accountMoveModel.Date = DateTime.ParseExact(parameter.Value.ToString(), "yyyy-MM-dd", System.Threading.Thread.CurrentThread.CurrentCulture); } if (parameter.Key == "date_maturity") { accountMoveModel.DateMaturity = DateTime.ParseExact(parameter.Value.ToString(), "yyyy-MM-dd", System.Threading.Thread.CurrentThread.CurrentCulture); } if (parameter.Key == "currency_id") { if (parameter.Value is object[]) { accountMoveModel.CurrencyId = int.Parse((parameter.Value as object[])[0].ToString()); } } if (parameter.Key == "account_id") { if (parameter.Value is object[]) { //var val = (parameter.Value as object[]).Length > 0 ? (parameter.Value as object[])[0] : null; accountMoveModel.AccountId = int.Parse((parameter.Value as object[])[0].ToString()); } } if (parameter.Key == "amount_currency") { accountMoveModel.AmountCurrency = decimal.Parse(parameter.Value.ToString()); } if (parameter.Key == "debit") { if (parameter.Value != null) { accountMoveModel.Debit = decimal.Parse(parameter.Value.ToString()); } } if (parameter.Key == "credit") { if (parameter.Value != null) { accountMoveModel.Credit = decimal.Parse(parameter.Value.ToString()); } } } accountMoveModelList.Add(accountMoveModel); } var currencies = odooService.SearchAndRead("res.currency", null, new string[] { "id", "name" }); var accountJournals = odooService.SearchAndRead("account.journal", null, new string[] { "id", "name" }); var currencyList = new List <Currency>(); foreach (var currencyType in currencies) { var collection = new Dictionary <string, object>(); var currencyModel = new Currency(); foreach (DictionaryEntry de in currencyType) { if (!collection.ContainsKey((string)de.Key)) { collection.Add((string)de.Key, de.Value); } } foreach (var parameter in collection) { if (parameter.Key == "id") { currencyModel.Id = (int)parameter.Value; } if (parameter.Key == "name") { currencyModel.Name = (string)parameter.Value; } } currencyList.Add(currencyModel); } var accountJournalList = new List <AccountJournal>(); foreach (var accountJournal in accountJournals) { var collection = new Dictionary <string, object>(); var accountJournalModel = new AccountJournal(); foreach (DictionaryEntry de in accountJournal) { if (!collection.ContainsKey((string)de.Key)) { collection.Add((string)de.Key, de.Value); } } foreach (var parameter in collection) { if (parameter.Key == "id") { accountJournalModel.Id = (int)parameter.Value; } if (parameter.Key == "name") { accountJournalModel.Name = (string)parameter.Value; } } accountJournalList.Add(accountJournalModel); } var query = from c in accountMoveModelList join k in accountJournalList on c.AccountId equals k.Id join t in currencyList on c.CurrencyId equals t.Id into cs from ces in cs.DefaultIfEmpty() select new AccountMove { DocumentDate = c.Date, ExpiryDate = c.DateMaturity, DocumentType = k.Name, DocumentNumber = c.Name, Description = c.Name, Currency = ces == null ? "TL" : ces.Name, CurrencyAmount = ces == null ? (c.Debit + (c.Credit * -1)) : c.AmountCurrency }; var customerMove = query.ToList(); Assert.IsNotNull(customerMove); }
public void FirstItemsNotFoundTest() { var service = new OdooService(Connection); Assert.Throws <ArgumentNullException>(() => service.First(OdooFilter <OdooPartner> .Where(x => x.Id == 0))); }
public OdooCollection(OdooService service, object[] ids) : this(service) { this.EntitiesId = ids; }
public OdooObject(OdooService service, int id) : this(service) { _id = id; }
public OdooCollection(OdooService service, int[] ids) : this(service) { this.EntitiesId = ids.Cast <object>().ToArray(); }
public void FirstItemsNotFoundTest() { var service = new OdooService(Connection); Assert.Throws<ArgumentNullException>(() => service.First(OdooFilter<OdooPartner>.Where(x => x.Id == 0))); }
public static T FirstOrDefault <T>(this OdooService service, OdooFilter <T> filter) where T : IOdooObject, new() { var result = service.GetEntities(filter.Filter).ToList(); return(result.FirstOrDefault()); }
public OdooCollection(OdooService service) { this.Service = service; this.DataLoaded = false; this.Entities = new List <T>(); }
private static T BuildObject <T>(OdooService service, XmlRpcStruct xmlStruct) where T : IOdooObject, new() { var obj = new T(); var props = obj.GetType() .GetProperties() .Where(p => p.GetCustomAttributes(typeof(OdooMapAttribute), true).Any()); var fkProps = obj.GetType() .GetProperties() .Where(p => p.GetCustomAttributes(typeof(OdooForeignKeyAttribute), true).Any()); foreach (var propertyInfo in props) { var info = propertyInfo.GetCustomAttribute <OdooMapAttribute>(); if (xmlStruct.ContainsKey(info.OdooName)) { var response = xmlStruct[info.OdooName]; object value; if (IsOdooNull(response)) { value = null; } else if (response as object[] != null) { var val = (response as object[]).Length > 0 ? (response as object[])[0] : null; //CHECK FOR FK MAPPING var fkProp = fkProps.FirstOrDefault( x => x.GetCustomAttribute <OdooForeignKeyAttribute>() .PropertyName == info.OdooName); if (fkProp != null) { if (val != null) { var type = typeof(OdooObject <>).MakeGenericType(fkProp.PropertyType); var @object = Activator.CreateInstance(type, service, val); var fkval = @object.GetType() .GetMethod("GetObject") .Invoke(@object, null); if (fkval != null) { fkProp.SetValue(obj, fkval); } } else { value = null; } } if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(int?)) { value = val; } else { if (val != null) { var type = typeof(OdooObject <>).MakeGenericType(propertyInfo.PropertyType); var @object = Activator.CreateInstance(type, service, val); value = @object.GetType() .GetMethod("GetObject") .Invoke(@object, null); } else { value = null; } } } else if (response as int[] != null) { var attibute = propertyInfo.PropertyType.GenericTypeArguments.First() .CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(OdooMapAttribute)); if (attibute != null) { var type = typeof(OdooCollection <>).MakeGenericType(propertyInfo.PropertyType.GetGenericArguments()); value = Activator.CreateInstance(type, service, response); } else { value = response; } } else { switch (info.OdooType) { case OdooType.Date: value = DateTime.ParseExact(response.ToString(), "yyyy-MM-dd", System.Threading.Thread.CurrentThread.CurrentCulture); break; case OdooType.Datetime: value = DateTime.ParseExact(response.ToString(), "yyyy-MM-dd HH:mm:ss", System.Threading.Thread.CurrentThread.CurrentCulture); break; case OdooType.Integer: value = int.Parse(response.ToString()); break; case OdooType.Float: value = Decimal.Parse(response.ToString()); break; case OdooType.Char: if (propertyInfo.Name.Equals("DateTime")) { value = DateTime.ParseExact( response.ToString(), "yyyy-MM-dd HH:mm:ss", System.Threading.Thread.CurrentThread.CurrentCulture); } else { value = Convert.ChangeType(response, propertyInfo.PropertyType); } break; default: value = Convert.ChangeType(response.ToString(), propertyInfo.PropertyType); break; } } propertyInfo.SetValue(obj, value); } else { //throw new OdooException(string.Format("Field {0} not found in server response", info.OdooName)); propertyInfo.SetValue(obj, null); } } return(obj); }
public OdooObject(OdooService service) { _service = service; _dataLoaded = false; }