Exemplo n.º 1
0
        public virtual ActionResult Maintain(VoucherMaintainViewModel viewModel, FormCollection collection)
        {
            var intCodes = Enumerable.Range(Convert.ToInt16(viewModel.CodeFrom), Convert.ToInt16(viewModel.CodeTo) - Convert.ToInt16(viewModel.CodeFrom) + 1).ToList();
            var stringCodes = intCodes.ConvertAll<string>(delegate(int i) { return i.ToString().PadLeft(Properties.Settings.Default.VoucherLength, '0'); });

            if (collection["submit"].ToString() == "Create/Activate")
                ActivateVoucher(viewModel, stringCodes);
            else
                DeactivateVoucher(viewModel, stringCodes);

            return RedirectToAction("Index");
        }
Exemplo n.º 2
0
        private void ActivateVoucher(VoucherMaintainViewModel viewModel, List<string> stringCodes)
        {
            var codeFrom = viewModel.CodeFrom.PadLeft(Properties.Settings.Default.VoucherLength, '0');
            var codeTo = viewModel.CodeTo.PadLeft(Properties.Settings.Default.VoucherLength, '0');

            using (var context = new TTTEntities())
            {
                var results = context.tblvouchers
                                      .GroupJoin(context.trnsalesorders, a => a.ID, b => b.VoucheID, (a, b) => new { voucher = a, so = b })
                                      .Where(a => String.Compare(codeFrom, a.voucher.Code) <= 0 && String.Compare(a.voucher.Code, codeTo) <= 0)
                                      .Select(a => new
                                      {
                                          Voucher = a.voucher,
                                          SO = a.so.FirstOrDefault()
                                      }).ToList();

                var oldCodes = results.Where(a => a.SO == null).Select(a => a.Voucher.Code).ToList();
                var newCodes = stringCodes.Except(oldCodes).ToList();

                var oldVouchers = context.tblvouchers.Where(a => oldCodes.Contains(a.Code)).ToList();
                foreach (var voucher in oldVouchers)
                {
                    voucher.Value = viewModel.Value;
                    voucher.Active = true;
                    voucher.UpdateDT = DateTime.Now;
                }

                foreach (var code in newCodes)
                {
                    context.tblvouchers.Add(new tblvoucher()
                    {
                        Active = true,
                        Code = code.PadLeft(Properties.Settings.Default.VoucherLength, '0'),
                        CreateDT = DateTime.Now,
                        Type = "Cash",
                        Value = viewModel.Value,
                    });
                }

                context.SaveChanges();
            }
        }