public CustomerCheckInViewModel DoCheckInByContract(int branchID, string contractNo, string userName)
        {
            var      messages               = new List <string>();
            var      model                  = new CustomerCheckInViewModel();
            var      checkInConfiguration   = new CheckInConfiguration();
            Contract contract               = context.Contracts.FirstOrDefault(con => con.ContractNo == contractNo);
            var      customerStatusProvider = new CustomerStatusProvider(context, principal);

            if (contract != null)
            {
                var customer = contract.Customer;
                var package  = contract.PackageHeader;

                model.CustomerBarcode = customer.Barcode;
                model.CustomerName    = String.Format("{0} {1}", customer.FirstName, customer.LastName);
                model.PackageName     = package.Name;
                model.ContractNo      = contractNo;
                model.ExpiredDate     = contract.ExpiredDate.ToString("dddd, dd MMMM yyyy");
                model.MemberSince     = customer.CreatedWhen.ToString("dddd, dd MMMM yyyy");
                model.Photo           = customer.Photo;
                model.IsPhotoExist    = !String.IsNullOrEmpty(model.Photo);
                model.Age             = customer.DateOfBirth.GetValueOrDefault().ToAgeString();

                if (contract.ExpiredDate <= DateTime.Today)
                {
                    messages.Add("CONTRACT IS EXPIRED");
                }

                if (contract.ActiveDate.HasValue)
                {
                    messages.Add(String.Format("Contract has been activated since {0}",
                                               contract.ActiveDate.Value.ToString("dddd, dd MMMM yyyy")));
                }
                else
                {
                    messages.Add("CONTRACT IS NOT ACTIVE");
                }

                CustomerStatusHistory customerStatusHistory = customerStatusProvider.GetLatestStatus(customer.Barcode);
                model.CustomerStatus = customerStatusHistory == null
                    ? "OK"
                    : customerStatusHistory.CustomerStatus.Description;
                if (checkInConfiguration.BirthdayAlert)
                {
                    bool isBirthDay = customer.DateOfBirth.GetValueOrDefault().Month == DateTime.Today.Month &&
                                      customer.DateOfBirth.GetValueOrDefault().Day == DateTime.Today.Day;
                    if (isBirthDay)
                    {
                        messages.Add("HAPPY BIRTHDAY");
                    }
                }
                model.Messages = messages;

                /* Save checkin history */
                var checkinlog = new CheckInLog();
                checkinlog.BranchID   = branchID;
                checkinlog.CustomerID = customer.ID;

                checkinlog.CustomerStatusID = customerStatusHistory == null ? 1 : customerStatusHistory.CustomerStatusID;
                checkinlog.Employee         = context.Employees.SingleOrDefault(emp => emp.UserName == userName);
                checkinlog.CheckInWhen      = DateTime.Now;
                checkinlog.Messages         = String.Join("|", messages.ToArray());
                checkinlog.Allowed          = model.AllowCheckIn;
                checkinlog.ContractID       = contract.ID;
                context.Add(checkinlog);
                context.SaveChanges();
            }
            else
            {
                model.AllowCheckIn = false;
                messages.Add("INVALID CONTRACT NUMBER");
                model.Messages = messages;
            }

            return(model);
        }
        public CustomerCheckInViewModel DoCheckIn(int branchID, string customerBarcode, string userName, string path)
        {
            var checkInConfiguration = new CheckInConfiguration();

            var messages  = new List <string>();
            var viewModel = new CustomerCheckInViewModel();

            viewModel.PickUpPersons = new List <string>();
            viewModel.PickUpPhotos  = new List <string>();

            var customerStatusProvider = new CustomerStatusProvider(context, principal);

            Customer customer = context.Customers.SingleOrDefault(cust => cust.Barcode == customerBarcode);

            if (customer != null)
            {
                viewModel.CustomerBarcode = customerBarcode.ToUpper();
                viewModel.CustomerName    = String.Format("{0} {1}", customer.FirstName.Trim().ToUpper(), customer.LastName.Trim().ToUpper());
                viewModel.When            = DateTime.Now;
                viewModel.Photo           = customer.Photo;
                viewModel.Age             = customer.DateOfBirth.GetValueOrDefault().ToAgeString();
                viewModel.IsPhotoExist    = File.Exists(path + customer.Photo);
                viewModel.AllowCheckIn    = true;

                foreach (var person in customer.People.Where(p => p.Connection == 'P'))
                {
                    viewModel.PickUpPersons.Add(person.Name);
                    viewModel.PickUpPhotos.Add(person.Photo);
                }

                /* Get Messages */

                var contractProvider = new ContractProvider(context, principal);
                var activeContract   = contractProvider.GetActiveContracts(customerBarcode).FirstOrDefault(contract => contract.EffectiveDate <= DateTime.Today);
                if (activeContract != null)
                {
                    if (!activeContract.PackageHeader.OpenEnd)
                    {
                        if (activeContract.ExpiredDate < DateTime.Today)
                        {
                            messages.Add("CONTRACT " + activeContract.ContractNo + " EXPIRED");
                        }
                        else if (activeContract.ExpiredDate.Subtract(DateTime.Today) <= TimeSpan.FromDays(30))
                        {
                            messages.Add("CONTRACT " + activeContract.ContractNo + " EXPIRING");
                        }
                    }
                    viewModel.PackageName = activeContract.PackageHeader.Name;
                }
                else
                {
                    messages.Add("UNAPPROVED CONTRACT");
                }

                if (checkInConfiguration.ContractNotActiveAlert)
                {
                    var inactiveContracts = customer.Contracts.Where(contract => !contract.ActiveDate.HasValue && !contract.VoidDate.HasValue && contract.EffectiveDate <= DateTime.Today).ToList();
                    if (inactiveContracts.Any())
                    {
                        messages.Add("CONTRACT NOT ACTIVE: " + String.Join(", ", inactiveContracts.Select(contract => contract.ContractNo).ToArray()));
                    }
                }

                if (checkInConfiguration.ContractNotPaid)
                {
                    var unpaidContracts = customer.Contracts.Where(contract => !contract.PurchaseDate.HasValue && !contract.VoidDate.HasValue && contract.EffectiveDate <= DateTime.Today).ToList();
                    if (unpaidContracts.Any())
                    {
                        messages.Add("CONTRACT NOT PAID: " + String.Join(", ", unpaidContracts.Select(contract => contract.ContractNo).ToArray()));
                    }
                }

                if (checkInConfiguration.BirthdayAlert)
                {
                    bool isBirthDay = customer.DateOfBirth.GetValueOrDefault().Month == DateTime.Today.Month &&
                                      customer.DateOfBirth.GetValueOrDefault().Day == DateTime.Today.Day;
                    if (isBirthDay)
                    {
                        messages.Add("HAPPY BIRTHDAY");
                    }
                }

                if (customer.BillingType.ID > BillingTypeConstants.MANUAL_PAYMENT)
                {
                    // alert for credit card is valid only for non-manual payment
                    if (DateTime.Today >= customer.ExpiredDate.GetValueOrDefault() && checkInConfiguration.CreditCardExpired)
                    {
                        messages.Add("CREDIT CARD IS EXPIRED");
                    }
                    else
                    {
                        if (checkInConfiguration.CreditCardExpiringAlert)
                        {
                            bool isCreditCardExpiring = customer.ExpiredDate.GetValueOrDefault().Subtract(DateTime.Today) <= TimeSpan.FromDays(30);
                            if (isCreditCardExpiring)
                            {
                                messages.Add("CREDIT CARD IS EXPIRING");
                            }
                        }
                    }
                }

                CustomerStatusHistory customerStatusHistory = customerStatusProvider.GetLatestStatus(customerBarcode);
                viewModel.CustomerStatus = customerStatusHistory == null ? "OK" : customerStatusHistory.CustomerStatus.Description;
                string color = customerStatusProvider.GetStatusColor(viewModel.CustomerStatus);
                viewModel.CustomerStatusColor           = color.Split('|')[0];
                viewModel.CustomerStatusBackgroundColor = color.Split('|')[1];

                messages.AddRange(customer.CustomerNotes.Where(note => note.Priority == 1).Select(note => note.Notes));

                viewModel.Messages = messages;


                /* Save checkin history */
                var checkinlog = new CheckInLog();
                checkinlog.BranchID   = branchID;
                checkinlog.CustomerID = customer.ID;

                checkinlog.CustomerStatusID = customerStatusHistory == null ? 1 : customerStatusHistory.CustomerStatusID;
                checkinlog.Employee         = context.Employees.SingleOrDefault(emp => emp.UserName == userName);
                checkinlog.CheckInWhen      = viewModel.When.Value;
                checkinlog.Messages         = String.Join("|", messages.ToArray());
                checkinlog.Allowed          = viewModel.AllowCheckIn;
                context.Add(checkinlog);
                context.SaveChanges();
            }
            else
            {
                viewModel.AllowCheckIn = false;
                messages.Add("INVALID CUSTOMER BARCODE");
                viewModel.Messages = messages;
            }

            return(viewModel);
        }