public bool IncomePaymentMatch(Payment payment)
        {
            var sb     = new StringBuilder();
            var orders = new List <Order>();

            if (payment.Counterparty == null)
            {
                return(false);
            }

            var uniqueOrderNumbers = ParsePaymentPurpose(payment.PaymentPurpose);

            if (uniqueOrderNumbers.Any())
            {
                orders.AddRange(
                    uniqueOrderNumbers.Select(orderNumber => _uow.GetById <Order>(orderNumber))
                    .Where(order => order != null &&
                           !_orderUndeliveredStatuses.Contains(order.OrderStatus) &&
                           order.Client.Id == payment.Counterparty.Id &&
                           order.PaymentType == PaymentType.cashless &&
                           order.OrderPaymentStatus == OrderPaymentStatus.UnPaid &&
                           order.OrderSum > 0));

                if (!orders.Any())
                {
                    return(false);
                }

                var paymentSum = payment.Total;

                foreach (var order in orders)
                {
                    if (addedOrderIdsToAllocate.Contains(order.Id))
                    {
                        return(false);
                    }

                    if (paymentSum >= order.OrderSum)
                    {
                        payment.AddPaymentItem(order);
                        addedOrderIdsToAllocate.Add(order.Id);
                        sb.AppendLine(order.Id.ToString());
                        paymentSum -= order.OrderSum;
                    }

                    if (paymentSum == 0)
                    {
                        break;
                    }
                }
            }

            if (!payment.PaymentItems.Any())
            {
                return(false);
            }

            payment.NumOrders = sb.ToString().TrimEnd(new[] { '\r', '\n' });
            return(true);
        }
Пример #2
0
        public bool IncomePaymentMatch(Payment payment)
        {
            StringBuilder sb     = new StringBuilder();
            List <Order>  orders = new List <Order>();

            if (payment.Counterparty == null)
            {
                return(false);
            }

            var str = CheckPaymentPurpose(payment);

            if (str.Any())
            {
                foreach (string st in str)
                {
                    var order = UoW.GetById <Order>(int.Parse(st));

                    if (order == null)
                    {
                        return(false);
                    }

                    orders.Add(order);
                }

                var result = orders.Sum(x => x.ActualTotalSum);

                if (payment.Total != result)
                {
                    return(false);
                }

                if (payment.Total == result)
                {
                    foreach (var order in orders)
                    {
                        if (order.OrderPaymentStatus != OrderPaymentStatus.UnPaid)
                        {
                            return(false);
                        }
                        else
                        {
                            order.OrderPaymentStatus = OrderPaymentStatus.Paid;
                            payment.AddPaymentItem(order);
                            UoW.Save(order);
                            sb.AppendLine(order.Id.ToString());
                        }
                    }
                }
            }
            else
            {
                return(false);
            }

            payment.NumOrders = sb.ToString().TrimEnd('\n');
            return(true);
        }
Пример #3
0
        public CreatePaymentResponse CreatePayment(CreatePaymentRequest req)
        {
            var res = new CreatePaymentResponse();

            try
            {
                using (var idmClient = new IDMServiceClient())
                    using (var container = new TransactionModelContainer())
                    {
                        ValidateCreatePaymentRequest(idmClient, req);
                        var payment = new Payment(
                            req.CreateBy,
                            req.CustomerIDMPartyID,
                            req.CustomerName,
                            req.CustomerAddress,
                            req.PaymentItems.CreatePaymentItems()
                            );

                        var grandTotal = payment.GrandTotal();
                        var credits    = container.CustomerCredits.Where(x =>
                                                                         x.CustomerIdmPartyId == req.CustomerIDMPartyID &&
                                                                         x.IsUsedOrRefund == false).ToList();

                        var credit = credits.GetCustomerCredit(grandTotal);

                        foreach (var c in credit)
                        {
                            payment.AddPaymentItem(PaymentItem.CreateCustomerCredit(c.Id, c.Amount));
                        }

                        CustomerCredit.UpdateCustomerCredit(credit);

                        container.Payments.AddObject(payment);
                        container.SaveChanges();

                        res.SetPaymentResponse(payment);
                        res.Succeed();
                    }
            }
            catch (Exception x)
            {
                res.Fail(x);
                CreateLog(req, x);
            }

            return(res);
        }