Example #1
0
        public ActionResult Create(string id)
        {
            Permission p    = new Permission();
            bool       auth = p.IsGranted(User.Identity.Name, this.ControllerContext.RouteData.Values["controller"].ToString() + "_" + this.ControllerContext.RouteData.Values["action"].ToString());

            if (!auth)
            {
                return(new ViewResult()
                {
                    ViewName = "Unauthorized"
                });
            }
            else
            {
                decimal total = 0; decimal due = 0;
                List <SaleInvoiceItemsDetails> listDetails = new List <SaleInvoiceItemsDetails>();
                string[] ids = id.Split(',');
                foreach (string inv_id in ids)
                {
                    var list = db.SaleInvoiceItems.Where(x => x.SaleInvoices_Id.ToString() == inv_id).OrderBy(x => x.RowNo).ToList();
                    foreach (var item in list)
                    {
                        SaleInvoiceItemsDetails saleInvoiceItemsDetails = new SaleInvoiceItemsDetails
                        {
                            Invoice     = db.SaleInvoices.Where(x => x.Id.ToString() == inv_id).FirstOrDefault().No,
                            Description = item.Description,
                            Notes       = item.Notes,
                            Qty         = item.Qty,
                            Price       = item.Price,
                            Travel      = item.TravelCost,
                            Tutor       = item.TutorTravelCost,
                            Voucher     = (item.SaleInvoiceItems_Vouchers_Id.HasValue) ? db.SaleInvoiceItems_Vouchers.Where(x => x.Id == item.SaleInvoiceItems_Vouchers_Id).FirstOrDefault().Amount : 0,
                            Discount    = item.DiscountAmount
                        };
                        saleInvoiceItemsDetails.Amount = (item.Qty * item.Price) + item.TravelCost - item.DiscountAmount - saleInvoiceItemsDetails.Voucher;
                        listDetails.Add(saleInvoiceItemsDetails);
                        total += saleInvoiceItemsDetails.Amount;
                    }
                    due += db.SaleInvoices.Where(x => x.Id.ToString() == inv_id).FirstOrDefault().Due;
                }
                ViewBag.Total = total.ToString("#,##0");
                ViewBag.Due   = due.ToString("#,##0");
                Guid branch_id = db.User.Where(x => x.UserName == User.Identity.Name).FirstOrDefault().Branches_Id;
                ViewBag.listConsignment = new SelectList(db.Consignments.Where(x => x.Branches_Id == branch_id).OrderBy(x => x.Name).ToList(), "Id", "Name");
                ViewBag.Invoices        = id;
                return(View(listDetails));
            }
        }
Example #2
0
        public async Task <ActionResult> Print(Guid?id)
        {
            Permission p    = new Permission();
            bool       auth = p.IsGranted(User.Identity.Name, this.ControllerContext.RouteData.Values["controller"].ToString() + "_" + this.ControllerContext.RouteData.Values["action"].ToString());

            if (!auth)
            {
                return(new ViewResult()
                {
                    ViewName = "Unauthorized"
                });
            }
            else
            {
                if (id == null || id == Guid.Empty) //show payment index
                {
                    //var login_session = Session["Login"] as LoginViewModel;
                    //Guid user_branch = db.User.Where(x => x.UserName == User.Identity.Name).FirstOrDefault().Branches_Id;
                    //List<PaymentsIndexModels> list_pim = new List<PaymentsIndexModels>();
                    //foreach (var item in db.Payments.ToList())
                    //{
                    //    var check_session = await (from pi in db.PaymentItems
                    //                               join si in db.SaleInvoices on pi.ReferenceId equals si.Id
                    //                               join sii in db.SaleInvoiceItems on si.Id equals sii.SaleInvoices_Id
                    //                               where pi.Payments_Id == item.Id && sii.SessionHours > sii.SessionHours_Remaining
                    //                               select new { pi, si, sii }).ToListAsync();

                    //    PaymentsIndexModels pim = new PaymentsIndexModels
                    //    {
                    //        Id = item.Id,
                    //        No = item.No,
                    //        Timestamp = TimeZoneInfo.ConvertTimeFromUtc(item.Timestamp, TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time")),
                    //        CashAmount = item.CashAmount,
                    //        DebitAmount = item.DebitAmount,
                    //        ConsignmentAmount = item.ConsignmentAmount,
                    //        Cancelled = item.Cancelled,
                    //        Confirmed = item.Confirmed,
                    //        Notes_Cancel = item.Notes_Cancel,
                    //        HasSession = check_session.Count > 0 ? true : false
                    //    };
                    //    Guid sales_invoice_id = db.PaymentItems.Where(x => x.Payments_Id == item.Id).FirstOrDefault().ReferenceId;
                    //    Guid branch_id = db.SaleInvoices.Where(x => x.Id == sales_invoice_id).FirstOrDefault().Branches_Id;
                    //    pim.Branch = db.Branches.Where(x => x.Id == branch_id).FirstOrDefault().Name;
                    //    if (branch_id == user_branch)
                    //        list_pim.Add(pim);
                    //}

                    ViewBag.Cancel  = p.IsGranted(User.Identity.Name, "payments_cancel");
                    ViewBag.Approve = p.IsGranted(User.Identity.Name, "payments_approve");
                    ViewBag.Log     = p.IsGranted(User.Identity.Name, "logs_view");
                    return(View()); //return View(list_pim);
                }
                else //show payment receipt
                {
                    PaymentsModels paymentsModels = await db.Payments.Where(x => x.Id == id).FirstOrDefaultAsync();

                    BranchesModels                 branchesModels = new BranchesModels();
                    List <PaymentItemsDetails>     listItems      = new List <PaymentItemsDetails>();
                    List <SaleInvoiceItemsDetails> listDetails    = new List <SaleInvoiceItemsDetails>();
                    decimal total_paid = 0;

                    //var list_PaymentItemsModels = db.PaymentItems.Where(x => x.Payments_Id == paymentsModels.Id).ToList();
                    var list_PaymentItemsModels = await(from pi in db.PaymentItems
                                                        join si in db.SaleInvoices on pi.ReferenceId equals si.Id
                                                        where pi.Payments_Id == paymentsModels.Id
                                                        orderby si.Timestamp ascending
                                                        select new { pi }).ToListAsync();
                    foreach (var item in list_PaymentItemsModels)
                    {
                        Guid branch_id = db.SaleInvoices.Where(x => x.Id == item.pi.ReferenceId).FirstOrDefault().Branches_Id;
                        branchesModels = await db.Branches.Where(x => x.Id == branch_id).FirstOrDefaultAsync();

                        PaymentItemsDetails paymentItemsDetails = new PaymentItemsDetails
                        {
                            Invoice   = db.SaleInvoices.Where(x => x.Id == item.pi.ReferenceId).FirstOrDefault().No,
                            Amount    = db.SaleInvoices.Where(x => x.Id == item.pi.ReferenceId).Sum(x => x.Amount),
                            DueBefore = item.pi.DueBefore,
                            Payment   = (item.pi.DueBefore > item.pi.DueAfter) ? item.pi.DueBefore - item.pi.DueAfter : item.pi.DueAfter - item.pi.DueBefore,
                            DueAfter  = item.pi.DueAfter
                        };
                        listItems.Add(paymentItemsDetails);
                        total_paid += paymentItemsDetails.Payment;

                        //decimal total = 0;
                        var list_SaleInvoiceItemsModels = await db.SaleInvoiceItems.Where(x => x.SaleInvoices_Id == item.pi.ReferenceId).OrderBy(x => x.RowNo).ToListAsync();

                        foreach (var subitem in list_SaleInvoiceItemsModels)
                        {
                            var data_customer = await(from si in db.SaleInvoices
                                                      join c in db.User on si.Customer_UserAccounts_Id equals c.Id
                                                      where si.Id == item.pi.ReferenceId
                                                      select new { c }).FirstOrDefaultAsync();
                            SaleInvoiceItemsDetails saleInvoiceItemsDetails = new SaleInvoiceItemsDetails();
                            saleInvoiceItemsDetails.Invoice     = paymentItemsDetails.Invoice;
                            saleInvoiceItemsDetails.Description = subitem.Description;
                            saleInvoiceItemsDetails.Notes       = subitem.Notes;
                            saleInvoiceItemsDetails.Customer    = data_customer.c.Firstname + " " + data_customer.c.Middlename + " " + data_customer.c.Lastname;
                            saleInvoiceItemsDetails.Qty         = subitem.Qty;
                            saleInvoiceItemsDetails.Price       = subitem.Price;
                            saleInvoiceItemsDetails.Travel      = subitem.TravelCost;
                            saleInvoiceItemsDetails.Tutor       = subitem.TutorTravelCost;
                            saleInvoiceItemsDetails.Voucher     = (subitem.SaleInvoiceItems_Vouchers_Id.HasValue) ? db.SaleInvoiceItems_Vouchers.Where(x => x.Id == subitem.SaleInvoiceItems_Vouchers_Id).FirstOrDefault().Amount : 0;
                            saleInvoiceItemsDetails.Discount    = subitem.DiscountAmount;
                            saleInvoiceItemsDetails.Amount      = (subitem.Qty * subitem.Price) + subitem.TravelCost - subitem.DiscountAmount - saleInvoiceItemsDetails.Voucher;
                            listDetails.Add(saleInvoiceItemsDetails);
                            //total += saleInvoiceItemsDetails.Amount;
                        }
                    }

                    ReceiptViewModels receiptViewModels = new ReceiptViewModels();
                    receiptViewModels.Branch  = branchesModels;
                    receiptViewModels.Payment = paymentsModels;
                    receiptViewModels.listSaleInvoiceItems = listDetails;
                    receiptViewModels.listPaymentItems     = listItems;
                    //receiptViewModels.TotalCash = paymentsModels.CashAmount;
                    //receiptViewModels.TotalDebit = paymentsModels.DebitAmount;
                    receiptViewModels.ConsignmentName = (paymentsModels.Consignments_Id.HasValue) ? db.Consignments.Where(x => x.Id == paymentsModels.Consignments_Id.Value).FirstOrDefault().Name : "";
                    receiptViewModels.TotalAmount     = total_paid;
                    return(View("Printed", receiptViewModels));
                }
            }
        }