// GET api/<controller>
        /// <summary>
        /// (obsolete) Get Smart Account Depended by the Features of Payment
        /// </summary>
        /// <param name="paymentB1Id">Id of Payment in Business One</param>
        /// <param name="companyId">Leave empty</param>
        /// <param name="companyCode">Id of Customers or Supplier in Business One</param>
        /// <param name="bank">Bank Information</param>
        /// <param name="amount">Amount of Payment</param>
        /// <param name="reference">Remark</param>
        /// <returns>JSON format</returns>
        /// <exception cref="Exception"></exception>
        public AlgorithmModels.Result Get(string paymentB1Id, int companyId, string companyCode,
                                          string bank, double amount, string reference)
        {
            try
            {
                var param = new AlgorithmModels.Parameter
                {
                    PaymentB1Id = paymentB1Id,
                    Company     = null,
                    BankCode    = string.Empty,
                    BankName    = bank,
                    Reference   = reference,
                    Amount      = amount
                };

                if (companyId <= 0 && string.IsNullOrEmpty(companyCode))
                {
                    throw new Exception("Company Information is required");
                }

                if (companyId <= 0 && !string.IsNullOrEmpty(companyCode))
                {
                    if (Company.Cache.CompanyListActive.Exists(x => x.CompanyCode.Equals(companyCode)))
                    {
                        param.Company = Company.Cache.CompanyListActive
                                        .Find(x => x.CompanyCode.Equals(param.Company.CompanyCode)).MapTo <Company, CompanyDto>();
                    }
                    else
                    {
                        param.Company = null;
                    }
                }
                else
                {
                    param.Company = Company.Cache.Load(companyId).MapTo <Company, CompanyDto>();
                }

                var result = AlgorithmV1.SmartMatching(param);

                return(result);
            }
            catch (Exception e)
            {
                return(new AlgorithmModels.Result
                {
                    ResultAccount = null,
                    Probability = 0,
                    ResultType = AlgorithmModels.ResultTypeEnum.Exception,
                    Remark = e.Message
                });
            }
        }
示例#2
0
        public void ApiPostTest()
        {
            var request = new
            {
                Amount      = 4000,
                Bank        = "boc.usd",
                CompanyCode = "CP100120",
                CompanyId   = -1,
                PaymentB1Id = "ab2306345e6f78",
                Reference   = "a"
            };

            var param = new AlgorithmModels.Parameter
            {
                PaymentB1Id = request.PaymentB1Id,
                Company     = null,
                BankCode    = string.Empty,
                BankName    = request.Bank,
                Reference   = request.Reference,
                Amount      = request.Amount
            };

            if (request.CompanyId <= 0 && string.IsNullOrEmpty(request.CompanyCode))
            {
                throw new Exception("Company Information is required");
            }

            if (request.CompanyId <= 0 && !string.IsNullOrEmpty(request.CompanyCode))
            {
                if (Company.Cache.CompanyListActive.Exists(x => x.CompanyCode.Equals(request.CompanyCode)))
                {
                    param.Company = Company.Cache.CompanyListActive
                                    .Find(x => x.CompanyCode.Equals(request.CompanyCode)).MapTo <Company, CompanyDto>();
                }
                else
                {
                    param.Company = null;
                }
            }
            else
            {
                param.Company = Company.Cache.Load(request.CompanyId).MapTo <Company, CompanyDto>();
            }

            var result = AlgorithmV1.SmartMatching(param);

            Assert.IsNotNull(result);
        }
示例#3
0
        public static AlgorithmModels.Result SmartMatching(AlgorithmModels.Parameter param)
        {
            if (param.Amount.Equals(0f))
            {
                throw new Exception("Amount is required or equals zero");
            }

            if (param.Company.ID <= 0 && string.IsNullOrEmpty(param.Company.B1Id))
            {
                throw new Exception("Company Information is required");
            }

            AccountDto account;

            AlgorithmModels.ResultTypeEnum resultType;
            double probability = 0f;
            var    remark      = string.Empty;

            using (IRepository repo = new Repository())
            {
                if (param.Company != null)
                {
                    var role = Role.Cache.RoleListActive.Find(x => x.CompanyId.Equals(param.Company.ID) &&
                                                              x.IsIncoming.Equals(param.Amount > 0));

                    if (role != null)
                    {
                        // Decide by Role
                        account     = Account.Cache.Load(role.AccountId).MapTo <Account, AccountDto>();
                        resultType  = AlgorithmModels.ResultTypeEnum.RoleBase;
                        probability = 1.0;
                        remark      = "Exactly matched";
                    }
                    else
                    {
                        // Decide by histric Data
                        if (string.IsNullOrEmpty(param.BankName) && string.IsNullOrEmpty(param.BankCode))
                        {
                            throw new Exception("Bank Information is required");
                        }

                        // ReSharper disable once RedundantBoolCompare
                        var payments = repo.Query <Payment>(x => x.IsActive == true &&
                                                            (x.CompanyName == param.Company.CompanyName ||
                                                             x.CompanyCode == param.Company.CompanyCode) &&
                                                            (x.BankName == param.BankName ||
                                                             x.BankCode == param.BankCode))
                                       .FindAll(x => x.AccountId > 0 && x.Status.Equals(PaymentStatusEnum.Posted) &&
                                                (x.Amount > 0).Equals(param.Amount > 0));

                        if (payments.Count > 0)
                        {
                            var query = (from p in payments
                                         group p by p.AccountId into g
                                         orderby g.Count() descending
                                         select new { g.Key, Nums = g.Count() }).ToList();

                            account     = Account.Cache.Load((int)query.First().Key).MapTo <Account, AccountDto>();
                            resultType  = AlgorithmModels.ResultTypeEnum.HistoricData;
                            probability = Math.Round((double)query[0].Nums / payments.Count, 2);
                            remark      = "Guessed";
                        }
                        else
                        {
                            // Decide by Reference Matching
                            if (string.IsNullOrEmpty(param.Reference))
                            {
                                throw new Exception("Reference is required");
                            }

                            // ReSharper disable once RedundantBoolCompare
                            payments = repo.Query <Payment>(x => x.IsActive == true)
                                       .FindAll(x => x.AccountId > 0 && x.Status.Equals(PaymentStatusEnum.Posted) &&
                                                (x.Amount > 0).Equals(param.Amount > 0) &&
                                                x.Reference.Equals(param.Reference, StringComparison.OrdinalIgnoreCase));

                            if (payments.Count > 0)
                            {
                                var query = (from p in payments
                                             group p by p.AccountId into g
                                             orderby g.Count() descending
                                             select new { g.Key, Nums = g.Count() }).ToList();

                                account     = Account.Cache.Load((int)query[0].Key).MapTo <Account, AccountDto>();
                                resultType  = AlgorithmModels.ResultTypeEnum.ReferenceMatching;
                                probability = Math.Round((double)query[0].Nums / payments.Count, 2);
                                remark      = "Guessed";
                            }
                            else
                            {
                                account     = null;
                                resultType  = AlgorithmModels.ResultTypeEnum.None;
                                probability = 0f;
                                remark      = "No matched";
                            }
                        }
                    }
                }
                else
                {
                    account     = null;
                    resultType  = AlgorithmModels.ResultTypeEnum.None;
                    probability = 0f;
                }
            }

            var result = new AlgorithmModels.Result
            {
                PaymentB1Id   = param.PaymentB1Id,
                ResultAccount = account,
                Probability   = probability,
                ResultType    = resultType,
                Remark        = remark
            };

            return(result);
        }