protected void btnOK_Clicked(object o, EventArgs args)
        {
            if (paymentToEdit != null)
            {
                paymentToEdit.PartnerId   = partner.Id;
                paymentToEdit.PartnerName = partner.Name;
                paymentToEdit.TypeId      = paymentWidget.GetPaymentTypeId();
                paymentToEdit.Quantity    = paymentWidget.Received;
                payments.Add(paymentToEdit);
            }
            else
            {
                double amountReceived = paymentWidget.Received;
                if (amountReceived > 0)
                {
                    Payment payment = AddPayment(amountReceived);
                    if (!BusinessDomain.OnPaymentProcessed(payment))
                    {
                        payments.Remove(payment);
                        paymentWidget.RefreshGrid();
                        return;
                    }
                }

                // Add the new payments
                for (int i = payments.Count - 1; i >= 0; i--)
                {
                    if (payments [i].Quantity.IsZero())
                    {
                        payments.RemoveAt(i);
                    }
                }
            }

            BusinessDomain.AppConfiguration.LastPaymentMethod = (BasePaymentType)paymentWidget.GetPaymentTypeId();
            dlgEditNewAdvancePayment.Respond(ResponseType.Ok);
        }
Ejemplo n.º 2
0
        protected void btnOK_Clicked(object o, EventArgs args)
        {
            if (!Validate())
            {
                return;
            }

            // Mark payments in the operation for deletion
            foreach (Payment payment in operation.Payments
                     .Where(payment => payment.Mode == PaymentMode.Paid && !payments.Any(p => p.Id >= 0 && p.Id == payment.Id)))
            {
                payment.Quantity = 0;
            }

            // Add the new payments
            foreach (Payment payment in payments)
            {
                if (payment.OperationType == (int)OperationType.AdvancePayment)
                {
                    double remainder = payment.Quantity;
                    foreach (var id in usedAdvances[payment])
                    {
                        long    id1     = id;
                        Payment advance = advances.Find(p => p.Id == id1);
                        double  sum     = Math.Min(remainder, advance.Quantity);
                        advance.Quantity -= sum;

                        // Make sure credit card transactions made for advances are not annulled
                        if (advance.Quantity.IsZero())
                        {
                            advance.TransactionNumber = null;
                        }

                        remainder -= sum;
                        if (remainder.IsZero())
                        {
                            break;
                        }
                    }
                    // mark as new
                    payment.Id            = -1;
                    payment.OperationType = (int)operation.OperationType;
                }

                // Add the payment to the operation if new
                if (payment.Id < 0)
                {
                    operation.Payments.Add((Payment)payment.Clone());
                    continue;
                }

                // Replace the payment if already used in the operation
                Payment currentPayment  = payment;
                Payment existingPayment = operation.Payments.Find(p => p.Id == currentPayment.Id);
                if (existingPayment != null)
                {
                    operation.Payments [operation.Payments.IndexOf(existingPayment)] = (Payment)payment.Clone();
                }
            }

            double subtractedChange = 0d;

            if (chkChangeIsReturned.Active)
            {
                subtractedChange = TotalReceived - Total;
            }

            if (subtractedChange > 0)
            {
                foreach (Payment payment in operation.Payments)
                {
                    if (payment.Mode != PaymentMode.Paid)
                    {
                        continue;
                    }

                    if (payment.Type.BaseType != BasePaymentType.Cash)
                    {
                        continue;
                    }

                    double newQtty = Math.Max(0, payment.Quantity - subtractedChange);
                    subtractedChange -= payment.Quantity - newQtty;
                    payment.Quantity  = newQtty;
                    if (subtractedChange <= 0)
                    {
                        break;
                    }
                }
            }

            if (subtractedChange > 0)
            {
                foreach (Payment payment in operation.Payments)
                {
                    if (payment.Mode != PaymentMode.Paid)
                    {
                        continue;
                    }

                    if (payment.Type.BaseType == BasePaymentType.Cash)
                    {
                        continue;
                    }

                    double newQtty = Math.Max(0, payment.Quantity - subtractedChange);
                    subtractedChange -= payment.Quantity - newQtty;
                    payment.Quantity  = newQtty;
                    if (subtractedChange <= 0)
                    {
                        break;
                    }
                }
            }

            DateTime endDate = BusinessDomain.GetDateValue(txtDueDate.Text.Trim());

            if (operation.Debt != null)
            {
                operation.Debt.EndDate = endDate;
            }

            foreach (Payment payment in operation.Payments)
            {
                payment.EndDate = endDate;

                if (!BusinessDomain.OnPaymentProcessed(payment))
                {
                    paymentWidget.Received = 0;
                    paymentWidget.FocusReceived();
                    return;
                }
            }

            BusinessDomain.AppConfiguration.LastPaymentMethod = (BasePaymentType)paymentWidget.GetPaymentTypeId();
            dlgEditNewPayment.Respond(ResponseType.Ok);
        }