/// <summary>
 /// Creates a rule and apply it on deposit/withdrawal for bank accounts and on refund/charges for credit card accounts.
 /// </summary>
 /// <param name="rule_info">The rule_info is the rule object which contains the rule_name,target_account_id,apply_to,criteria_type,field,comparator and record_as parameters as the mandatory.<br></br>
 /// The allowed values for apply_to are <i>withdrawals, deposits, refunds</i> and <i>charges</i>.<br></br>
 /// The allowed values for criteria_type are <i>or </i> and <i>and</i>.<br></br>
 /// The allowed values for field<i>payee, description, reference_number</i> and <i>amount</i>.<br></br>
 /// The allowed values for comparator are <i>is, contains, starts_with, is_empty, greater_than, greater_than_or_equals, less_than</i>and <i>less_than_or_equals</i>.<br></br>
 /// The allowed values for record_as are <i>expense, deposit, refund, transfer_fund, card_payment, sales_without_invoices, expense_refund, interest_income, other_income</i> and <i>owner_drawings</i>
 /// </param>
 /// <returns>Rule object.</returns>
 public Rule Create(Rule rule_info)
 {
     string url = baseAddress;
     var json = JsonConvert.SerializeObject(rule_info);
     var jsonstring = new Dictionary<object, object>();
     jsonstring.Add("JSONString", json);
     var responce = ZohoHttpClient.post(url, getQueryParameters(jsonstring));
     return BankRuleParser.getRule(responce);
 }
 internal static Rule getRule(HttpResponseMessage responce)
 {
     var rule = new Rule();
     var jsonObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(responce.Content.ReadAsStringAsync().Result);
     if (jsonObj.ContainsKey("rule"))
     {
         rule = JsonConvert.DeserializeObject<Rule>(jsonObj["rule"].ToString());
     }
     return rule;
 }
 internal static RuleList getRuleList(HttpResponseMessage responce)
 {
     var ruleList = new RuleList();
     var jsonObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(responce.Content.ReadAsStringAsync().Result);
     if(jsonObj.ContainsKey("rules"))
     {
         var rulesArray = JsonConvert.DeserializeObject<List<object>>(jsonObj["rules"].ToString());
         foreach(var ruleObj in rulesArray)
         {
             var rule = new Rule();
             rule = JsonConvert.DeserializeObject<Rule>(ruleObj.ToString());
             ruleList.Add(rule);
         }
     }
     return ruleList;
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            try
            {
                var service = new ZohoBooks();
                service.initialize("{authtoken}", "{organizationId}");
                var rulesApi = service.GetBankRulesApi();
                var parameters = new Dictionary<object, object>();
                var accountsApi = service.GetBankAccountsApi();
                var accounts = accountsApi.GetBankAccounts(null);
                var chartOfAccountsApi = service.GetChartOfAccountsApi();
                var chartOfAccounts = chartOfAccountsApi.GetChartOfAcounts(null);
                parameters.Add("account_id", accounts[1].account_id);
                Console.WriteLine("--------------------------Rules List---------------");
                var rules = rulesApi.GetRules(parameters);
                foreach (var tempRule in rules)
                {
                    Console.WriteLine("{0},{1},{2}", tempRule.rule_id, tempRule.rule_name, tempRule.account_name);
                    var tempCriterions = tempRule.criterion;
                    Console.WriteLine("criterions");
                    foreach (var tempCriterion in tempCriterions)
                        Console.WriteLine("{0},{1}", tempCriterion.criteria_id, tempCriterion.value);
                }
                Console.WriteLine("--------------------------------------Specified Rule ----------------------------");
                var rule = rulesApi.Get(rules[0].rule_id);
                Console.WriteLine("{0},{1},{2}", rule.rule_id, rule.rule_name, rule.account_name);

                var ruleInfo = new Rule()
                {
                    rule_name = "hari",
                    target_account_id = accounts[0].account_id,
                    apply_to = "withdrawals",
                    criteria_type = "or",
                    criterion = new List<Criterion>(){
                         new Criterion(){
                             field="payee",
                             comparator="contains",
                             value="9"
                            }
                     },
                    record_as = "expense",
                    account_id = chartOfAccounts[41].account_id
                };
                Console.WriteLine("----------------------------New Rule-----------------------");
                var newRule = rulesApi.Create(ruleInfo);
                Console.WriteLine("{0},{1},{2}", newRule.rule_id, newRule.rule_name, newRule.account_name);
                var newcriterions = newRule.criterion;
                Console.WriteLine("criterions");
                foreach (var criterion in newcriterions)
                    Console.WriteLine("{0},{1}", criterion.criteria_id, criterion.value);
                var updateInfo = new Rule()
                {
                    rule_name = "krishna",
                    apply_to = "withdrawals",
                    criteria_type = "or",
                    criterion = new List<Criterion>(){
                         new Criterion(){
                             field="payee",
                             comparator="contains",
                             value="8"
                            }
                     },
                    record_as = "expense",
                };
                Console.WriteLine("----------------------------Updated Rule----------------------");
                var updatedRule = rulesApi.Update(newRule.rule_id, updateInfo);
                Console.WriteLine("{0},{1},{2}", updatedRule.rule_id, updatedRule.rule_name, updatedRule.account_name);
                var criterions1 = updatedRule.criterion;
                Console.WriteLine("criterions");
                foreach (var criterion in criterions1)
                    Console.WriteLine("{0},{1}", criterion.criteria_id, criterion.value);
                Console.WriteLine("-------------------------------Delete Rule--------------------------------------");
                var delRule = rulesApi.Delete(updatedRule.rule_id);
                Console.WriteLine(delRule);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }