using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using PagedList; using Newtonsoft.Json; using System.Collections; using System.IO; using System.Web; using System.Threading.Tasks; using static TreeView; [ezAuthorize] public class accountingController : BaseApiController { // GET api/ public IEnumerable Get() { var list = _db.accountings.ToList(); if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return list; } // GET api//5 public Model.accounting Get(int id) { var item = _db.accountings.Where(q => q.num == id).FirstOrDefault(); if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound); return item; } // GET api//5 public IEnumerable GetPage(int page) { var accounting = _db.accountings.Where(q => q.num < 10).ToList(); return accounting; } // POST api/ public void Post([FromBody] string value) { } // PUT api//5 public void Put(int id, [FromBody] string value) { } // DELETE api//5 public void Delete(int id) { var prod = _db.accountings.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料 if (prod != null) { var prod2 = prod.accounting_files; if (prod2.Count > 0) { publicFun publicFun = new publicFun(); foreach (var item in prod2) { if (!string.IsNullOrEmpty(item.pic1)) { publicFun.DeleteFile(Model.accounting.Dir + "/" + item.pic1); } } } _db.accountings.Remove(prod); _db.SaveChanges(); //執行 Model.admin_log admin_log = new Model.admin_log(); MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb if (admin.isLoign()) { admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Delete, prod.uptime.Value.ToString("yyyy/MM/dd") ); } } } [HttpDelete] [Route("api/accounting/Delete/{nums}")] public void Delete(string nums) { if (!string.IsNullOrEmpty(nums)) { var getDelItem = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s)); var prod = _db.accountings.AsEnumerable().Where(q => getDelItem.Contains(q.num)).ToList(); if (prod.Count() > 0) { var prod2 = _db.accounting_files.AsEnumerable().Where(q => q.accounting_num.HasValue && getDelItem.Contains(q.accounting_num.Value)).ToList(); if (prod2.Count() > 0) { publicFun publicFun = new publicFun(); foreach (var item in prod2) { if (!string.IsNullOrEmpty(item.pic1)) { publicFun.DeleteFile(Model.accounting.Dir + "/" + item.pic1); } } } _db.accountings.RemoveRange(prod); _db.SaveChanges(); Model.admin_log admin_log = new Model.admin_log(); MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb if (admin.isLoign()) { admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Delete, admin_log.LogViewBtn(prod.Select(x => x.uptime.Value.ToString("yyyy/MM/dd")).ToList())); } } } } [HttpPost] [Route("api/accounting/GetList")] public IHttpActionResult GetList([FromBody] Model.ViewModel.accounting q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) { var qry = _db.accountings.AsEnumerable(); if (q.category.HasValue) qry = qry.Where(o => o.category == q.category.Value); if (q.kind.HasValue) { var _subKinds = new TreeView().subKinds(_db.accounting_kind.Select(o => new TreeItem() { num = o.num, root = o.root, }).ToList(), q.kind.Value); qry = qry.Where(o => o.kind == q.kind.Value || _subKinds.Any(s => s == o.kind)); } if (q.kind2.HasValue) { var _subKinds = new TreeView().subKinds(_db.accounting_kind2.Select(o => new TreeItem() { num = o.num, root = o.root, }).ToList(), q.kind2.Value); qry = qry.Where(o => o.kind2 == q.kind2.Value || _subKinds.Any(s => s == o.kind2)); } if (q.uptime1.HasValue) qry = qry.Where(o => o.uptime >= q.uptime1.Value); if (q.uptime2.HasValue) qry = qry.Where(o => o.uptime < Convert.ToDateTime(q.uptime2.Value).AddDays(1)); if (!string.IsNullOrEmpty(q.activity_num_txt)) qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(q.activity_num_txt.Trim())); if (!string.IsNullOrEmpty(q.mem_num_txt)) qry = qry.Where(o => o.mem_num.HasValue && o.member.u_name.Contains(q.mem_num_txt.Trim())); if (!string.IsNullOrEmpty(q.debtor)) qry = qry.Where(o => (o.debtor ?? "").Contains(q.debtor.Trim())); if (sortBy.Equals("category_Txt")) { if (sortDesc) qry = qry.OrderByDescending(o => o.category); else qry = qry.OrderBy(o => o.category); } else if (sortBy.Equals("kindsTxt")) { if (sortDesc) qry = qry.OrderByDescending(o => o.kind); else qry = qry.OrderBy(o => o.kind); } else if (sortBy.Equals("kinds2Txt")) { if (sortDesc) qry = qry.OrderByDescending(o => o.kind2); else qry = qry.OrderBy(o => o.kind2); } else if (sortBy.Equals("uptime")) { if (sortDesc) qry = qry.OrderByDescending(o => o.uptime); else qry = qry.OrderBy(o => o.uptime); } else if (sortBy.Equals("price")) { if (sortDesc) qry = qry.OrderByDescending(o => o.price); else qry = qry.OrderBy(o => o.price); } else if (sortBy.Equals("tax")) { if (sortDesc) qry = qry.OrderByDescending(o => o.tax); else qry = qry.OrderBy(o => o.tax); } else if (sortBy.Equals("total")) { if (sortDesc) qry = qry.OrderByDescending(o => o.price??0+o.tax??0); else qry = qry.OrderBy(o => o.price ?? 0 + o.tax ?? 0); } else qry = qry.OrderByDescending(o => o.num); var tdesc = publicFun.enum_desc(); var ret = new { list = qry.ToPagedList(page, pageSize).Select(x => new { num = x.num, category = x.category, category_Txt = tdesc[x.category ?? 1], kind = x.kind, kindTxt = x.kind.HasValue? x.accounting_kind.kind :"", kindsTxt = x.kind.HasValue ? new TreeView().kindText(_db.accounting_kind.Select(o => new TreeItem() { kind = o.kind, num = o.num, root = o.root, }).ToList(), x.kind) : "", kind2 = x.kind2, kind2Txt = x.kind2.HasValue? x.accounting_kind2.kind :"", kinds2Txt = x.kind2.HasValue ? new TreeView().kindText(_db.accounting_kind2.Select(o => new TreeItem() { kind = o.kind, num = o.num, root = o.root, }).ToList(), x.kind2) : "", uptime = x.uptime, price = x.price, tax = x.tax?? 0, total =(x.price ?? 0)+( x.tax?? 0), }), count = qry.Count() }; if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return Ok(ret); } [HttpPost] [Route("api/accounting/GetTitleKindList")] public IHttpActionResult GetTitleKindList([FromBody] Model.ViewModel.accounting_kind q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) { var qry = _db.accounting_kind.AsEnumerable(); if (!string.IsNullOrEmpty(q.kind)) qry = qry.Where(o => o.kind.Contains(q.kind)); var qry2 = new TreeView().get_data2(qry.Select(o => new TreeItem() { kind = o.kind, num = o.num, root = o.root, range = o.range, }).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0); var ret = new { list = qry2.ToPagedList(page, pageSize).Select(x => new { num = x.num, kind = new TreeView().RptDash(x.Level) + x.kind, }), count = qry.Count() }; if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return Ok(ret); } [HttpPost] [Route("api/accounting/GetAccountKindList")] public IHttpActionResult GetAccountKindList([FromBody] Model.ViewModel.accounting_kind2 q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) { var qry = _db.accounting_kind2.AsEnumerable(); if (!string.IsNullOrEmpty(q.kind)) qry = qry.Where(o => o.kind.Contains(q.kind)); if (!string.IsNullOrEmpty(q.record_payment)) qry = qry.Where(o => o.record_payment !=null && o.record_payment==q.record_payment); var qry2 = new TreeView().get_data2(qry.Select(o => new TreeItem() { kind = o.kind, num = o.num, root = o.root, range = o.range, }).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0); var ret = new { list = qry2.ToPagedList(page, pageSize).Select(x => new { num = x.num, kind = new TreeView().RptDash(x.Level) + x.kind, }), count = qry.Count() }; if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return Ok(ret); } [HttpPost] [Route("api/accounting/GetAccFiles")] public IHttpActionResult GetItemFiles([FromBody] Model.accounting_files q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) { if (q.accounting_num.HasValue && q.accounting_num.Value > 0) { //檢查 var qry = _db.accounting_files.AsEnumerable(); qry = qry.Where(o => o.accounting_num == q.accounting_num.Value); qry.OrderByDescending(x => x.num); int i = 1; //已有值 var ret = new { list = qry.ToPagedList(page, pageSize).Select(x => new { id = i++, num = x.num, accounting_num = x.accounting_num, pic1 = x.pic1, pic1_name = x.pic1_name, }), count = qry.Count(), }; if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound); return Ok(ret); } else { return NotFound(); } } [HttpPost] [Route("api/accounting/uploadFiles")] public async Task uploadFiles() { if (!Request.Content.IsMimeMultipartContent()) { return BadRequest("無效的請求。"); } string uploadFolder = Path.Combine(HttpContext.Current.Server.MapPath("~/upload"), "accounting"); Directory.CreateDirectory(uploadFolder); var provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath(Model.accounting.Dir)); await Request.Content.ReadAsMultipartAsync(provider); if (provider.FileData.Count == 0) { return BadRequest("缺少檔案。"); } else { string tempFilePath = provider.FileData[0].LocalFileName; string fileName = provider.FileData[0].Headers.ContentDisposition.FileName.Trim('\"'); //string Dir = provider.FormData[0]; //re-name string[] n = Path.GetFileName(fileName).Split('.'); fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + n[n.Length - 1]; bool isAllowed = false; string[] type = { "jpg", "jpeg", "png", "pdf" }; for (int i = 0; i <= type.Length - 1; i++) { if (n[n.Length - 1].ToLower() == type[i]) { isAllowed = true; } } if (isAllowed) { //計算檔案大小 long result = -1; System.Net.WebRequest req = System.Net.WebRequest.Create(tempFilePath); req.Method = "HEAD"; using (System.Net.WebResponse resp = req.GetResponse()) { if (long.TryParse(resp.Headers.Get("Content-Length"), out long ContentLength)) { result = ContentLength;//位元組 } } //result / 1024 = kB if (result / 1000 / 1000 <= 2) { string filePath = Path.Combine(uploadFolder, fileName); File.Move(tempFilePath, filePath); return Ok(fileName); } else { return BadRequest("檔案限制 2 MB 以內"); } } else { return BadRequest("格式不符。"); } } } [HttpPost] [Route("api/accounting/SaveFileData")] public IHttpActionResult SaveFileData([FromBody] Model.accounting_files item) { item.reg_time = DateTime.Now; _db.accounting_files.Add(item); _db.SaveChanges(); return Ok(item.num); } [HttpDelete] [Route("api/accounting/DeleteFilesItem/{id}")]//刪除相關檔案 public void DeleteFilesItem(int id) { var prod = _db.accounting_files.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料 if (prod != null) { if (!string.IsNullOrEmpty(prod.pic1)) { publicFun publicFun = new publicFun(); publicFun.DeleteFile(Model.accounting.Dir + "/" + prod.pic1); } _db.accounting_files.Remove(prod); _db.SaveChanges(); //執行 } } }