public ActionResult LockInvLet(int Id)
        {
            if (Session["Guid"] != null)
            {
                string     Guid       = Session["Guid"].ToString();
                InvAccount invAccount = db.InvAccounts.Where(x => x.Guid == Guid).FirstOrDefault();

                if (ModelState.IsValid)
                {
                    //InvClientInfo invClientInfo = db.InvClientInfos.Find(Id); // 限主索引,且連表格會一起抓,故常與JsonIgnore並用
                    InvLetter invLetter = db.InvLetters.Where(L => L.AccountId == invAccount.Id && L.Id == Id).FirstOrDefault();

                    if (invLetter.InvLetterStatus == InvLetterStatusType.可使用)
                    {
                        invLetter.InvLetterStatus = InvLetterStatusType.已鎖定;
                    }
                    else if (invLetter.InvLetterStatus == InvLetterStatusType.已鎖定)
                    {
                        invLetter.InvLetterStatus = InvLetterStatusType.可使用;
                    }

                    db.Entry(invLetter).State = EntityState.Modified;
                    //db.InvClientInfos.Remove(invClientInfo);
                    db.SaveChanges();

                    return(Json(true));
                }
                return(Json(false));
            }
            return(Json(false));
        }
        public ActionResult CreadeInvLet(InvLetter invLetter)
        {
            ModelState.Remove("id");
            ModelState.Remove("initdate");
            ModelState.Remove("AccountId");
            ModelState.Remove("StartMonth");
            ModelState.Remove("InvLetterStatus");
            ModelState.Remove("StartDate");

            if (ModelState.IsValid)
            {
                if (Session["Guid"] != null)
                {
                    string     Guid       = Session["Guid"].ToString();
                    InvAccount invAccount = db.InvAccounts.Where(x => x.Guid == Guid).FirstOrDefault();

                    invLetter.AccountId  = invAccount.Id;
                    invLetter.StartMonth = Convert.ToInt32(invLetter.Period) * 2 + 1;
                    invLetter.StartDate  = Convert.ToDateTime($"{Convert.ToInt32(invLetter.Year)}" + "-" + $"{Convert.ToInt32(invLetter.Period) * 2 + 1}-1");
                    invLetter.Year       = Convert.ToInt32(invLetter.Year) - 1911;
                    //invLetter.InitDate = DateTime.Now.AddHours(15);
                    invLetter.InvLetterStatus = InvLetterStatusType.可使用; // =0

                    db.InvLetters.Add(invLetter);
                    db.SaveChanges();
                    return(Json(true));
                }
                return(Json(false));
            }
            return(Json(false));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            InvLetter invLetter = db.InvLetters.Find(id);

            db.InvLetters.Remove(invLetter);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "Id,InitDate,AccountId,Year,Period,StartMonth,Letter,StartNum,EndNum,InpLetterStatus")] InvLetter invLetter)
 {
     if (ModelState.IsValid)
     {
         db.Entry(invLetter).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.AccountId = new SelectList(db.InvAccounts, "Id", "Guid", invLetter.AccountId);
     return(View(invLetter));
 }
        // GET: InvLetters/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            InvLetter invLetter = db.InvLetters.Find(id);

            if (invLetter == null)
            {
                return(HttpNotFound());
            }
            return(View(invLetter));
        }
        // GET: InvLetters/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            InvLetter invLetter = db.InvLetters.Find(id);

            if (invLetter == null)
            {
                return(HttpNotFound());
            }
            ViewBag.AccountId = new SelectList(db.InvAccounts, "Id", "Guid", invLetter.AccountId);
            return(View(invLetter));
        }
        public ActionResult DetailInvLet(int Id)
        {
            if (Session["Guid"] != null)
            {
                string     Guid       = Session["Guid"].ToString();
                InvAccount invAccount = db.InvAccounts.Where(x => x.Guid == Guid).FirstOrDefault();

                if (ModelState.IsValid)
                {
                    //InvClientInfo invClientInfo = db.InvClientInfos.Find(Id); // 限主索引,且連表格會一起抓,故常與JsonIgnore並用
                    InvLetter invLetter = db.InvLetters.Where(L => L.AccountId == invAccount.Id && L.Id == Id).FirstOrDefault();

                    var invTable = db.InvTables
                                   .Where(T => T.AccountId == invAccount.Id && T.Letter == invLetter.Letter && (T.InvStatus == InvStatusType.已開立)).ToList(); // todo 需確認 已作廢 是否包含在內,個人認為不用,因這邊為上傳財政部使用,對財政部而言做廢發票始終不存在,以及 已鎖定須 "事後由前端或後端" 來判斷

                    //var invTables = db.InvTables
                    //    .Where(T => T.AccountId == invAccount.Id && T.Letter == invLetter.Letter).Select(T=>T.Letter).ToList();

                    StringBuilder stringBuilder = new StringBuilder();

                    foreach (var Inv in invTable)
                    {
                        stringBuilder.Append(Inv.Num + ",");
                    }

                    int Length = Convert.ToInt32(invLetter.EndNum) + 1 - Convert.ToInt32(invLetter.StartNum);

                    string[] listInvs = new string[Length];


                    int p = 0;
                    int r = 0;

                    JArray jsonOut = new JArray();

                    for (int i = Convert.ToInt32(invLetter.StartNum); i <= Convert.ToInt32(invLetter.EndNum); i++)
                    {
                        if (stringBuilder.ToString().IndexOf(i.ToString()) > -1)
                        {
                            JObject json = new JObject();
                            json.Add(new JProperty("YearPeriod", $"{invLetter.Year + 1911 } {(Convert.ToInt32(invLetter.Period) * 2 + 1)}-{(Convert.ToInt32(invLetter.Period) * 2 + 2)}月"));
                            json.Add(new JProperty("InvNum", invLetter.Letter + i));
                            json.Add(new JProperty("Client", invTable[p].Client));
                            json.Add(new JProperty("Total", invTable[p].Total.ToString()));
                            json.Add(new JProperty("Status", "已使用"));
                            jsonOut.Add(json);
                            p++;
                        }
                        else
                        {
                            JObject json = new JObject();
                            json.Add(new JProperty("YearPeriod", $"{invLetter.Year + 1911 } {(Convert.ToInt32(invLetter.Period) * 2 + 1)}-{(Convert.ToInt32(invLetter.Period) * 2 + 2)}月"));
                            json.Add(new JProperty("InvNum", invLetter.Letter + i));
                            json.Add(new JProperty("Client", ""));
                            json.Add(new JProperty("Total", ""));
                            json.Add(new JProperty("Status", "未使用"));
                            jsonOut.Add(json);
                        }

                        r++;
                    }

                    return(Content(JsonConvert.SerializeObject(jsonOut)));
                }
                return(Json(false));
            }
            return(Json(false));
        }