Пример #1
0
        public ActionResult CommitInvoiceNoIntervalGroup(InvoiceNoViewModel viewModel)
        {
            ViewBag.ViewModel = viewModel;
            var profile = HttpContext.GetUser();

            if (viewModel.BookletBranchID == null || viewModel.BookletBranchID.Length < 3 || viewModel.BookletBranchID.Any(c => !c.HasValue))
            {
                ModelState.AddModelError("BookletBranchID", "請選擇分店!!");
            }

            viewModel.TrackCode = viewModel.TrackCode.GetEfficientString();
            if (viewModel.TrackCode == null || !Regex.IsMatch(viewModel.TrackCode, "[A-Z]{2}"))
            {
                ModelState.AddModelError("TrackCode", "字軌錯誤!!");
            }

            if (!viewModel.Year.HasValue)
            {
                ModelState.AddModelError("Year", "請選擇年份!!");
            }

            if (!viewModel.PeriodNo.HasValue)
            {
                ModelState.AddModelError("PeriodNo", "請選擇期別!!");
            }

            var trackCode = models.GetTable <InvoiceTrackCode>()
                            .Where(t => t.TrackCode == viewModel.TrackCode && t.Year == viewModel.Year && t.PeriodNo == viewModel.PeriodNo).FirstOrDefault();

            var table = models.GetTable <InvoiceNoInterval>()
                        .Where(n => n.EndNo > n.StartNo);
            var item = table.Where(i => i.IntervalID == viewModel.IntervalID).FirstOrDefault();
            int?range;

            if (!viewModel.StartNo.HasValue || !(viewModel.StartNo >= 0 && viewModel.StartNo < 100000000))
            {
                ModelState.AddModelError("StartNo", "起號非8位整數!!");
            }
            else if (!viewModel.EndNo.HasValue || !(viewModel.EndNo >= 0 && viewModel.EndNo < 100000000))
            {
                ModelState.AddModelError("EndNo", "迄號非8位整數!!");
            }
            else if (viewModel.EndNo <= viewModel.StartNo || (((range = viewModel.EndNo - viewModel.StartNo + 1)) % 50 != 0))
            {
                ModelState.AddModelError("StartNo", "不符號碼大小順序與差距為50之倍數原則!!");
            }
            else if (viewModel.BookletCount == null || viewModel.BookletCount.Length < 3 || viewModel.BookletCount.Any(c => !c.HasValue || c < 0))
            {
                ModelState.AddModelError("BookletCount", "請輸入有效本數!!");
            }
            else if (viewModel.BookletCount.Sum(c => c) > (range / 50))
            {
                ModelState.AddModelError("BookletCount", "輸入總本數超過配號區間!!");
            }
            else
            {
                if (item != null)
                {
                    if (item.InvoiceNoAssignment.Count > 0)
                    {
                        ModelState.AddModelError("StartNo", "該區間之號碼已經被使用,不可修改!!!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0 &&
                                       t.SellerID == item.SellerID))
                    {
                        ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法修改!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID &&
                                       ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                    {
                        ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                    }
                }
                else
                {
                    if (trackCode != null)
                    {
                        if (table.Any(t => t.TrackID == trackCode.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0))
                        {
                            ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法新增!!");
                        }
                        else if (table.Any(t => t.TrackID == trackCode.TrackID &&
                                           ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                        {
                            ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                        }
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View("~/Views/Shared/ReportInputError.ascx"));
            }

            if (item != null)
            {
                models.DeleteAllOnSubmit <InvoiceNoInterval>(i => i.GroupID == item.IntervalID);
            }

            if (trackCode == null)
            {
                trackCode = new InvoiceTrackCode
                {
                    TrackCode = viewModel.TrackCode,
                    PeriodNo  = viewModel.PeriodNo.Value,
                    Year      = viewModel.Year.Value
                };
                models.GetTable <InvoiceTrackCode>().InsertOnSubmit(trackCode);
            }

            InvoiceNoInterval prevItem = null;

            for (int i = 0; i < viewModel.BookletBranchID.Length; i++)
            {
                var branchID     = viewModel.BookletBranchID[i];
                var bookletCount = viewModel.BookletCount[i];

                var codeAssignment = trackCode.InvoiceTrackCodeAssignment.Where(t => t.SellerID == branchID.Value).FirstOrDefault();
                if (codeAssignment == null)
                {
                    codeAssignment = new InvoiceTrackCodeAssignment
                    {
                        SellerID         = branchID.Value,
                        InvoiceTrackCode = trackCode
                    };

                    trackCode.InvoiceTrackCodeAssignment.Add(codeAssignment);
                }

                item = new InvoiceNoInterval
                {
                };
                if (i == 0)
                {
                    item.StartNo = viewModel.StartNo.Value;
                    item.EndNo   = item.StartNo + (bookletCount.Value * 50) - 1;
                    item.InvoiceNoIntervalGroup = new InvoiceNoIntervalGroup {
                    };
                }
                else
                {
                    item.StartNo = prevItem.EndNo + 1;
                    item.EndNo   = item.StartNo + (bookletCount.Value * 50) - 1;
                    item.InvoiceNoIntervalGroup = prevItem.InvoiceNoIntervalGroup;
                }
                codeAssignment.InvoiceNoInterval.Add(item);
                prevItem = item;
            }

            try
            {
                models.SubmitChanges();
                return(Json(new { result = true, item.GroupID }));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(Json(new { result = false, message = ex.Message }));
            }
        }
Пример #2
0
        public ActionResult CommitInvoiceTrackNoInterval(InvoiceQueryViewModel viewModel)
        {
            ViewBag.ViewModel = viewModel;
            var profile = HttpContext.GetUser();

            if (viewModel.BookletBranchID == null || viewModel.BookletBranchID.Length == 0 || viewModel.BookletBranchID.Any(c => !c.HasValue))
            {
                ModelState.AddModelError("BookletBranchID", "請選擇分店!!");
            }

            if (viewModel.KeyID != null)
            {
                viewModel.TrackID = viewModel.DecryptKeyValue();
            }

            var trackCode = models.GetTable <InvoiceTrackCode>()
                            .Where(t => t.TrackID == viewModel.TrackID).FirstOrDefault();

            if (trackCode == null)
            {
                viewModel.TrackCode = viewModel.TrackCode.GetEfficientString();
                if (viewModel.TrackCode == null || !Regex.IsMatch(viewModel.TrackCode, "[A-Z]{2}"))
                {
                    ModelState.AddModelError("TrackCode", "請輸入字軌");
                }

                if (!viewModel.Year.HasValue)
                {
                    ModelState.AddModelError("Year", "請選擇發票年度");
                }

                if (!viewModel.PeriodNo.HasValue)
                {
                    ModelState.AddModelError("PeriodNo", "請選擇期別");
                }
            }

            int?range = 0, assignedBooklet = 0;;

            if (!viewModel.StartNo.HasValue || !(viewModel.StartNo >= 0 && viewModel.StartNo < 100000000))
            {
                ModelState.AddModelError("StartNo", "請輸入起號");
            }
            else if (!viewModel.EndNo.HasValue || !(viewModel.EndNo >= 0 && viewModel.EndNo < 100000000))
            {
                ModelState.AddModelError("EndNo", "請輸入迄號");
            }
            else if (viewModel.EndNo <= viewModel.StartNo || (((range = viewModel.EndNo - viewModel.StartNo + 1)) % 50 != 0))
            {
                ModelState.AddModelError("StartNo", "不符號碼大小順序與差距為50之倍數原則");
            }
            else if ((assignedBooklet = viewModel.BookletCount.Where(c => c.HasValue && c > 0).Sum(c => c)) > (range / 50))
            {
                ModelState.AddModelError("BookletCount", "輸入總本數超過配號區間");
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View(ConsoleHomeController.InputErrorView));
            }

            if (trackCode == null)
            {
                trackCode = models.GetTable <InvoiceTrackCode>()
                            .Where(t => t.TrackCode == viewModel.TrackCode && t.Year == viewModel.Year && t.PeriodNo == viewModel.PeriodNo).FirstOrDefault();
            }

            if (trackCode == null)
            {
                trackCode = new InvoiceTrackCode
                {
                    TrackCode = viewModel.TrackCode,
                    Year      = viewModel.Year.Value,
                    PeriodNo  = viewModel.PeriodNo.Value,
                };
                models.GetTable <InvoiceTrackCode>().InsertOnSubmit(trackCode);
            }

            int?startNo = trackCode.StartNo = viewModel.StartNo;

            trackCode.EndNo = viewModel.EndNo;

            foreach (var b in viewModel.BookletBranchID)
            {
                if (!trackCode.InvoiceTrackCodeAssignment.Any(t => t.SellerID == b))
                {
                    trackCode.InvoiceTrackCodeAssignment.Add(new InvoiceTrackCodeAssignment
                    {
                        SellerID = b.Value
                    });
                }
            }

            models.SubmitChanges();

            for (int idx = 0; idx < viewModel.BookletBranchID.Length; idx++)
            {
                var interval = models.GetTable <InvoiceNoInterval>()
                               .Where(i => i.SellerID == viewModel.BookletBranchID[idx])
                               .Where(i => i.TrackID == trackCode.TrackID).FirstOrDefault();

                if (interval == null)
                {
                    if (!viewModel.BookletCount[idx].HasValue || viewModel.BookletCount[idx] <= 0)
                    {
                        continue;
                    }

                    interval = new InvoiceNoInterval
                    {
                        TrackID  = trackCode.TrackID,
                        SellerID = viewModel.BookletBranchID[idx].Value,
                    };

                    models.GetTable <InvoiceNoInterval>().InsertOnSubmit(interval);
                }
                else
                {
                    if (interval.InvoiceNoAssignment.Any())
                    {
                        if (interval.StartNo != startNo ||
                            !viewModel.BookletCount[idx].HasValue ||
                            viewModel.BookletCount[idx] <= 0)
                        {
                            ModelState.AddModelError($"BranchID_{viewModel.BookletBranchID[idx]}", "字軌配號已使用,無法修改");
                            continue;
                        }
                        else if ((startNo + viewModel.BookletCount[idx] * 50 - 1) < interval.EndNo)
                        {
                            ModelState.AddModelError($"BranchID_{viewModel.BookletBranchID[idx]}", "字軌配號已使用本組數,只允許增加");
                            continue;
                        }
                    }
                    else if (!viewModel.BookletCount[idx].HasValue || viewModel.BookletCount[idx] <= 0)
                    {
                        models.GetTable <InvoiceNoInterval>().DeleteOnSubmit(interval);
                        continue;
                    }
                }

                interval.StartNo = startNo.Value;
                startNo         += viewModel.BookletCount[idx] * 50;
                interval.EndNo   = startNo.Value - 1;
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View(ConsoleHomeController.InputErrorView));
            }

            try
            {
                models.SubmitChanges();
                if (assignedBooklet > 0)
                {
                    return(Json(new { result = true, message = $"總本數:{range / 50},已配置本數:{assignedBooklet}" }));
                }
                else
                {
                    models.ExecuteCommand("delete InvoiceTrackCode where TrackID = {0}", trackCode.TrackID);
                    return(Json(new { result = true, message = "未配置本組數。" }));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(Json(new { result = false, message = ex.Message }));
            }
        }
Пример #3
0
        public ActionResult CommitInvoiceNoInterval(InvoiceNoViewModel viewModel)
        {
            ViewBag.ViewModel = viewModel;
            var profile = HttpContext.GetUser();

            if (!viewModel.BranchID.HasValue)
            {
                ModelState.AddModelError("BranchID", "請選擇分店!!");
            }

            viewModel.TrackCode = viewModel.TrackCode.GetEfficientString();
            if (viewModel.TrackCode == null || !Regex.IsMatch(viewModel.TrackCode, "[A-Z]{2}"))
            {
                ModelState.AddModelError("TrackCode", "字軌錯誤!!");
            }

            if (!viewModel.Year.HasValue)
            {
                ModelState.AddModelError("Year", "請選擇年份!!");
            }

            if (!viewModel.PeriodNo.HasValue)
            {
                ModelState.AddModelError("PeriodNo", "請選擇期別!!");
            }

            var trackCode = models.GetTable <InvoiceTrackCode>()
                            .Where(t => t.TrackCode == viewModel.TrackCode && t.Year == viewModel.Year && t.PeriodNo == viewModel.PeriodNo).FirstOrDefault();

            var table = models.GetTable <InvoiceNoInterval>();
            var item  = table.Where(i => i.IntervalID == viewModel.IntervalID).FirstOrDefault();

            if (!viewModel.StartNo.HasValue || !(viewModel.StartNo >= 0 && viewModel.StartNo < 100000000))
            {
                ModelState.AddModelError("StartNo", "起號非8位整數!!");
            }
            else if (!viewModel.EndNo.HasValue || !(viewModel.EndNo >= 0 && viewModel.EndNo < 100000000))
            {
                ModelState.AddModelError("EndNo", "迄號非8位整數!!");
            }
            else if (viewModel.EndNo <= viewModel.StartNo || ((viewModel.EndNo - viewModel.StartNo + 1) % 50 != 0))
            {
                ModelState.AddModelError("StartNo", "不符號碼大小順序與差距為50之倍數原則!!");
            }
            else
            {
                if (item != null)
                {
                    if (item.InvoiceNoAssignment.Count > 0)
                    {
                        ModelState.AddModelError("StartNo", "該區間之號碼已經被使用,不可修改!!!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0 &&
                                       t.SellerID == item.SellerID))
                    {
                        ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法修改!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID &&
                                       ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                    {
                        ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                    }
                }
                else
                {
                    if (trackCode != null)
                    {
                        if (table.Any(t => t.TrackID == trackCode.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0 &&
                                      t.SellerID == viewModel.BranchID))
                        {
                            ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法新增!!");
                        }
                        else if (table.Any(t => t.TrackID == trackCode.TrackID &&
                                           ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                        {
                            ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                        }
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View("~/Views/Shared/ReportInputError.ascx"));
            }

            if (item == null)
            {
                if (trackCode == null)
                {
                    trackCode = new InvoiceTrackCode
                    {
                        TrackCode = viewModel.TrackCode,
                        PeriodNo  = viewModel.PeriodNo.Value,
                        Year      = viewModel.Year.Value
                    };
                    models.GetTable <InvoiceTrackCode>().InsertOnSubmit(trackCode);
                }

                var codeAssignment = trackCode.InvoiceTrackCodeAssignment.Where(t => t.SellerID == viewModel.BranchID).FirstOrDefault();
                if (codeAssignment == null)
                {
                    codeAssignment = new InvoiceTrackCodeAssignment
                    {
                        SellerID         = viewModel.BranchID.Value,
                        InvoiceTrackCode = trackCode
                    };

                    trackCode.InvoiceTrackCodeAssignment.Add(codeAssignment);
                }

                item = new InvoiceNoInterval {
                };
                codeAssignment.InvoiceNoInterval.Add(item);
            }

            item.StartNo = viewModel.StartNo.Value;
            item.EndNo   = viewModel.EndNo.Value;

            try
            {
                models.SubmitChanges();
                return(Json(new { result = true, item.IntervalID }));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(Json(new { result = false, message = ex.Message }));
            }
        }