Example #1
0
        public int ProcessBillingUnpaidInvoice(string fileName)
        {
            // accepted
            List <BillingAcceptedViewModel> acceptedInvoices = ReadBillingUnpaidAcceptedFile(fileName).ToList();
            AutoNumberProvider autoNumberProvider            = new AutoNumberProvider(ctx);

            foreach (BillingAcceptedViewModel acceptedInvoice in acceptedInvoices)
            {
                InvoiceHeader invoice = ctx.InvoiceHeaders.SingleOrDefault(ih => ih.InvoiceNo == acceptedInvoice.InvoiceNo);
                Customer      cust    = invoice.Customer;
                if (invoice != null)
                {
                    var payments = invoice.PaymentHeaders.Where(ph => !ph.VoidDate.HasValue);
                    foreach (var payment in payments)
                    {
                        payment.VoidDate = DateTime.Today;
                        //payment.VoidReason = "AUTO PAY DECLINED: " + acceptedInvoice.DeclineCode;
                    }

                    string paymentNo = autoNumberProvider.Generate(invoice.BranchID, "PM", DateTime.Today.Month, DateTime.Today.Year);

                    PaymentHeader h = new PaymentHeader();
                    h.Date      = DateTime.Today;
                    h.InvoiceID = invoice.ID;
                    h.PaymentNo = paymentNo;
                    h.VoidDate  = (DateTime?)null;
                    EntityHelper.SetAuditFieldForInsert(h, HttpContext.Current.User.Identity.Name);

                    PaymentDetail d = new PaymentDetail();
                    d.CreditCardTypeID = cust.CreditCardTypeID;
                    d.PaymentTypeID    = 4; // credit  card
                    d.Amount           = invoice.InvoiceDetails.Sum(inv => (inv.Quantity * inv.UnitPrice) - (inv.Discount / 100 * (inv.Quantity * inv.UnitPrice)));
                    d.ApprovalCode     = acceptedInvoice.VerificationCode;
                    h.PaymentDetails.Add(d);
                    ctx.PaymentHeaders.InsertOnSubmit(h);

                    autoNumberProvider.Increment("PM", invoice.BranchID, DateTime.Today.Year);

                    CustomerStatusHistory status = new CustomerStatusHistory();
                    status.Customer         = invoice.Customer;
                    status.Date             = DateTime.Today;
                    status.CustomerStatusID = 1; // OK
                    status.Notes            = "AUTO PAY ACCEPTED FOR INVOICE " + invoice.InvoiceNo;
                    status.StartDate        = DateTime.Today;
                    EntityHelper.SetAuditFieldForInsert(status, HttpContext.Current.User.Identity.Name);
                }
            }

            ctx.SubmitChanges();

            return(acceptedInvoices.Count);
        }
Example #2
0
        private IList <BillingViewModel> CreateBillingInvoices(int branchID, int billingTypeID, IEnumerable <BillingViewModel> billings, DateTime processDate, int processedByEmployeeID, string billingFileName)
        {
            List <BillingViewModel> list               = new List <BillingViewModel>();
            InvoiceProvider         invoiceProvider    = new InvoiceProvider(ctx);
            AutoNumberProvider      autoNumberProvider = new AutoNumberProvider(ctx);
            string autoNumber = autoNumberProvider.Generate(branchID, "BL", processDate.Month, processDate.Year);

            BillingHeader billingHeader = new BillingHeader();

            billingHeader.BatchNo       = autoNumber;
            billingHeader.BillingTypeID = billingTypeID;
            billingHeader.BranchID      = branchID;
            billingHeader.UserName      = ctx.Employees.Single(emp => emp.ID == processedByEmployeeID).UserName;
            billingHeader.ProcessDate   = processDate;
            billingHeader.FileName      = billingFileName;

            foreach (BillingViewModel billing in billings)
            {
                Contract      contract = ctx.Contracts.SingleOrDefault(c => c.ContractNo == billing.ContractNo);
                Customer      customer = contract.Customer;
                PackageHeader package  = contract.PackageHeader;
                if (contract != null && package != null && customer != null)
                {
                    InvoiceDetailViewModel invoiceDetail = new InvoiceDetailViewModel();
                    invoiceDetail.InvoiceID = 0;
                    invoiceDetail.ItemID    = contract.BillingItemID.Value;
                    invoiceDetail.Quantity  = 1;
                    invoiceDetail.UnitPrice = billing.DuesAmount;
                    invoiceDetail.Discount  = 0;
                    invoiceDetail.IsTaxable = true;

                    PaymentDetailViewModel paymentDetail = new PaymentDetailViewModel();
                    paymentDetail.PaymentTypeID    = ctx.PaymentTypes.SingleOrDefault(p => p.Description == "Credit Card").ID;
                    paymentDetail.CreditCardTypeID = customer.CreditCardTypeID.HasValue ? customer.CreditCardTypeID.Value : ctx.CreditCardTypes.SingleOrDefault(cc => cc.Description == "Visa").ID;
                    paymentDetail.ApprovalCode     = String.Empty;
                    paymentDetail.Amount           = billing.DuesAmount;
                    paymentDetail.Notes            = "Auto Pay";
                    paymentDetail.PaymentID        = 0;

                    InvoiceHeader invoiceHeader = invoiceProvider.CreateExistingMemberInvoiceForBilling(branchID,
                                                                                                        processDate,
                                                                                                        customer.Barcode,
                                                                                                        processedByEmployeeID,
                                                                                                        "Auto pay " + contract.ContractNo,
                                                                                                        0,
                                                                                                        new List <InvoiceDetailViewModel>()
                    {
                        invoiceDetail
                    },
                                                                                                        new List <PaymentDetailViewModel>()
                    {
                        paymentDetail
                    });


                    BillingDetail billingDetail = new BillingDetail();
                    billingDetail.Amount        = billing.DuesAmount;
                    billingDetail.Contract      = contract;
                    billingDetail.Customer      = customer;
                    billingDetail.InvoiceHeader = invoiceHeader;
                    billingDetail.PackageHeader = package;
                    billingHeader.BillingDetails.Add(billingDetail);

                    ctx.BillingHeaders.InsertOnSubmit(billingHeader);

                    billing.Note += "," + invoiceHeader.InvoiceNo;
                    list.Add(billing);
                }

                if (contract.NextDuesDate.HasValue)
                {
                    contract.NextDuesDate = contract.NextDuesDate.Value.AddMonths(contract.DuesInMonth);
                }
            }


            autoNumberProvider.Increment("BL", branchID, processDate.Year);
            ctx.SubmitChanges();

            return(list);
        }