Exemplo n.º 1
0
        public static string Muhasebelestir(string quoteId)
        {
            string result = "false";

            try
            {
                IOrganizationService service     = MSCRM.AdminOrgService;
                Entity          quote            = service.Retrieve("quote", new Guid(quoteId), new ColumnSet("customerid", "new_financialaccountid", "transactioncurrencyid"));
                EntityReference customer         = quote.Attributes.Contains("customerid") ? (EntityReference)quote["customerid"] : null;
                EntityReference financialAccount = quote.Attributes.Contains("new_financialaccountid") ? (EntityReference)quote["new_financialaccountid"] : null;
                EntityReference currency         = quote.Attributes.Contains("transactioncurrencyid") ? (EntityReference)quote["transactioncurrencyid"] : null;
                string          customerName     = customer.Name;

                SqlDataAccess sda = new SqlDataAccess();
                sda.openConnection(Globals.ConnectionString);

                #region | UPDATE FINANCIAL ACCOUNT CODE -VOLKAN 08.03.2015 |

                if (!string.IsNullOrEmpty(customerName) && financialAccount != null && !financialAccount.Name.Contains("329") && currency != null)
                {
                    char character = customerName[0];
                    MsCrmResultObject fAccountResult = FinancialAccountHelper.GetFinancialAccountNumberByCharacter(character, sda);
                    if (fAccountResult.Success)
                    {
                        int fAccountCode = (int)fAccountResult.ReturnObject;
                        fAccountCode++;

                        string fAccountCodeLeft = fAccountCode.ToString().PadLeft(5, '0');

                        Entity fAccount = new Entity("new_financialaccount");
                        fAccount["new_financialaccountid"]        = financialAccount.Id;
                        fAccount["new_financialaccountnumber"]    = fAccountCode;
                        fAccount["new_financialaccountcharacter"] = character + "";
                        fAccount["new_name"] = "329CA20" + (currency.Name == "TL" ? "TRL" : (currency.Name == "Euro" ? "EUR" : currency.Name)) + character + fAccountCodeLeft;
                        service.Update(fAccount);
                    }
                    else
                    {
                        throw new Exception(fAccountResult.Result);
                    }
                }


                #endregion

                #region | SET GROUP CODE |
                if (customer.LogicalName == "contact")
                {
                    if (!string.IsNullOrEmpty(customerName) && !ContactHelper.CheckContactHasGroupCode(customer.Id, sda).Success)
                    {
                        char character = customerName[0];
                        MsCrmResultObject groupCodeResult = ContactHelper.GetContactGroupCodeByCharacter(character, sda);
                        if (groupCodeResult.Success)
                        {
                            int groupCode = (int)groupCodeResult.ReturnObject;
                            groupCode++;
                            string groupCodeLeft = groupCode.ToString().PadLeft(4, '0');

                            Entity entContact = new Entity("contact");
                            entContact["contactid"]              = customer.Id;
                            entContact["new_groupcodenumber"]    = groupCode;
                            entContact["new_groupcodecharacter"] = character + "";
                            entContact["new_groupcode"]          = "CRM" + "-" + character + groupCodeLeft;
                            service.Update(entContact);
                        }
                        else
                        {
                            throw new Exception(groupCodeResult.Result);
                        }
                    }
                }

                if (customer.LogicalName == "account")
                {
                    if (!string.IsNullOrEmpty(customerName) && !AccountHelper.CheckAccountHasGroupCode(customer.Id, sda).Success)
                    {
                        char character = customerName[0];
                        MsCrmResultObject groupCodeResult = AccountHelper.GetAccountGroupCodeByCharacter(character, sda);
                        if (groupCodeResult.Success)
                        {
                            int groupCode = (int)groupCodeResult.ReturnObject;
                            groupCode++;
                            string groupCodeLeft = groupCode.ToString().PadLeft(4, '0');

                            Entity entAccount = new Entity("account");
                            entAccount["accountid"]              = customer.Id;
                            entAccount["new_groupcodenumber"]    = groupCode;
                            entAccount["new_groupcodecharacter"] = character + "";
                            entAccount["new_groupcode"]          = "CRM" + "-" + character + groupCodeLeft;
                            service.Update(entAccount);
                        }
                        else
                        {
                            throw new Exception(groupCodeResult.Result);
                        }
                    }
                }
                #endregion

                #region | QUERY UPDATE STATUS|
                string sqlQuery = @"UPDATE
	                                   Quote
                                    SET
	                                   StatusCode=@StatusCode,
                                       ModifiedOn=GETUTCDATE()
                                    WHERE
	                                    QuoteId='{0}'"    ;
                #endregion

                sda.ExecuteNonQuery(string.Format(sqlQuery, quoteId), new SqlParameter[] { new SqlParameter("StatusCode", (int)QuoteStatus.MuhasebeyeAktarıldı) });



                #region | QUERY UPDATE LOGO TRANSFER |
                sqlQuery = string.Empty;
                sqlQuery = @"UPDATE
	                                   Quote
                                    SET
	                                   new_islogotransferred=@Islogotransferred
                                    WHERE
	                                    QuoteId='{0}'"    ;
                #endregion

                sda.ExecuteNonQuery(string.Format(sqlQuery, quoteId), new SqlParameter[] { new SqlParameter("Islogotransferred", 1) });
                result = "true";
            }
            catch (Exception)
            {
                result = "false";
            }
            return(result);
        }