Untitled

doituongcontroller
mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 kB
2
Indexable
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WassProITC.Business;
using WassProITC.Business.Data;
using WassProITC.Models;
using WassProITC.Utilities;


namespace WassProITC.Modules.DLDoiTuong
{
    public class DMDoiTuongController : Controller
    {
        SPContext db = new SPContext();
        // GET: DMDoiTuong
        public ActionResult Index()
        {
           
            return View();
        }

        //GET DM DOI TUONG
        public JsonResult GetDMDoiTuong()
        {
            var search = "";
            if (!string.IsNullOrEmpty(Request.Form["search[value]"]))
            {
                search = Request.Form["search[value]"];
            }
            BusDMDoiTuong adapter = new BusDMDoiTuong();
            var doituong = adapter.GetDMDoiTuong(db, search).AsEnumerable();

            //sort
            var sortCol = "0";
            string sortDirection = "asc";

            if (!string.IsNullOrEmpty(Request.Form["order[0][column]"]))
            {
                sortCol = Request.Form["order[0][column]"];
            }

            if (!string.IsNullOrEmpty(Request.Form["order[0][dir]"]))
            {
                sortDirection = Request.Form["order[0][dir]"];
            }

            var sortOrder = sortCol + "_" + sortDirection;

            switch (sortOrder)
            {
                case "1_asc":
                    doituong = doituong.OrderBy(n => n.MaDT);
                    break;
                case "1_desc":
                    doituong = doituong.OrderByDescending(n => n.MaDT);
                    break;

                case "2_asc":
                    doituong = doituong.OrderBy(n => n.TenDT);
                    break;
                case "2_desc":
                    doituong = doituong.OrderByDescending(n => n.TenDT);
                    break;
                case "3_asc":
                    doituong = doituong.OrderBy(n => n.NhomDTID);
                    break;
                case "3_desc":
                    doituong = doituong.OrderByDescending(n => n.NhomDTID);
                    break;
                case "4_asc":
                    doituong = doituong.OrderBy(n => n.GiaBVMTID);
                    break;
                case "4_desc":
                    doituong = doituong.OrderByDescending(n => n.GiaBVMTID);
                    break;
                case "5_asc":
                    doituong = doituong.OrderBy(n => n.STT);
                    break;
                case "5_desc":
                    doituong = doituong.OrderByDescending(n => n.STT);
                    break;
                case " 6_asc":
                    doituong = doituong.OrderBy(n => n.ChiNhanhID);
                    break;
                case "6_desc":
                    doituong = doituong.OrderByDescending(n => n.ChiNhanhID);
                    break;
            }

            var data = doituong.Select(s => new
            {
                DoiTuongID = s.DoiTuongID,
                MaDT = s.MaDT,
                TenDT = s.TenDT,
                TenNhomDT = s.TenNhomDT,
                GiaBVMT = s.DienGiai,
                STT = s.STT,
                TenCN = s.TenCN,
                Tools = " <button type ='button' data-id=" + s.DoiTuongID + " class='btn btn-info btn-edit-dmdoituong btn-xs btn-flat  margin-right-5'><i class='fa fa-edit'></i></button>" + " <button type='button' data-id=" + s.DoiTuongID + " class='btn btn-danger btn-xs btn-flat btn-delete-dmdoituong'><i class='fa fa-trash-o'></i></button>"
            }).ToList();

            //Data length
            var length = Int32.Parse(Request.Form["length"]);
            var start = Int32.Parse(Request.Form["start"]);

            var result = new
            {
                draw = Request.Form["draw"],
                recordsTotal = data.Count(),
                recordsFiltered = data.Count(),
                data = data.Skip(start).Take(length) // total data array
            };


            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //MODAL
        public ActionResult Modal(decimal DoiTuongID = 0)
        {
           
            if (HttpContext.Request.IsAjaxRequest())
            {
                DMDoiTuong dmdt = new DMDoiTuong();
                if(DoiTuongID != 0)
                {
                    dmdt = db.DMDoiTuongs.FirstOrDefault(d => d.DoiTuongID == DoiTuongID);
                }
                ViewBag.NhomDTID = new SelectList(db.NhomDoiTuongs, "NhomDTID", "TenNhomDT", dmdt.NhomDTID);
                ViewBag.GiaBVMTID = new SelectList(db.GiaBVMTs, "GiaBVMTID", "DienGiai", dmdt.GiaBVMTID);
                ViewBag.ChiNhanhID = new SelectList(db.ChiNhanhs, "ChiNhanhID", "TenCN", dmdt.ChiNhanhID);
                return PartialView(dmdt);
            }
            else
            {
                return HttpNotFound();
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Update(DMDoiTuong dmdt)
        {
            if (HttpContext.Request.IsAjaxRequest())
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        if (dmdt.DoiTuongID == 0)
                        {
                            db.DMDoiTuongs.Add(dmdt);
                            db.SaveChanges();
                            return Json(new Result() { Code = ResultCode.Success, Message = "Đối tượng" + dmdt.MaDT + "đã được tạo thành công", Data = true }, JsonRequestBehavior.AllowGet);
                        }
                        else
                        {
                            var old_doituong = db.DMDoiTuongs.FirstOrDefault(d => d.DoiTuongID == dmdt.DoiTuongID);
                            old_doituong.MaDT = dmdt.MaDT;
                            old_doituong.TenDT= dmdt.TenDT;
                            old_doituong.NhomDTID = dmdt.NhomDTID;
                            old_doituong.GiaBVMTID = dmdt.GiaBVMTID;
                            old_doituong.STT = dmdt.STT;
                            old_doituong.ChiNhanhID = dmdt.ChiNhanhID;

                            db.SaveChanges();
                            return Json(new Result() { Code = ResultCode.Success, Message = "Cập nhật thành công", Data = true }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    return Json(new Result() { Code = ResultCode.Error, Message = "" }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    return Json(new Result() { Code = ResultCode.Error, Message = "Có lỗi trong quá trình thực hiện!" }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return HttpNotFound();
            }
        }

        [HttpPost]
        public ActionResult DeleteDMDT(decimal DoiTuongID)
        {
            if (HttpContext.Request.IsAjaxRequest())
            {
                var dtID = db.DMDoiTuongs.FirstOrDefault(d => d.DoiTuongID == DoiTuongID);
                if(dtID.DoiTuongID == DoiTuongID)
                {
                    db.DMDoiTuongs.Remove(dtID);
                    db.SaveChanges();
                    return Json(new Result() {Code = ResultCode.Success, Message= "Xóa đối tượng thành công" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new Result() { Code = ResultCode.Error, Message = "Dữ liệu đã được sử dụng!" }, JsonRequestBehavior.AllowGet);
                }

            }
            else
            {
                return HttpNotFound();
            }
        }


        public ActionResult ExportExcel(string search= "")
        {
            byte[] result = null;
            using (ExcelPackage package = new ExcelPackage())
            {
                BusDMDoiTuong apdater = new BusDMDoiTuong();
                var dmdts = apdater.GetDMDoiTuong(db, search);

                ExcelWorksheet workSheet = package.Workbook.Worksheets.Add(String.Format("{0}", "sheet1"));
                using (ExcelRange cells = workSheet.Cells["A1:G1"])
                {
                    cells.Value = "DANH MỤC BẢNG GIÁ";
                    cells.Merge = true;
                    cells.Style.Font.Size = 15;
                    cells.Style.Font.Name = "Times New Roman";
                    cells.Style.Font.Bold = true;
                    cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }

                //TIÊU ĐỀ
                string[] valHeaderTable = { "STT", "Mã đối tượng", "Tên đối tượng", "Nhóm DTID", "Giá BVMTID", "STT chi nhánh", "Chi nhánh ID" };
                int lengthHeaderTable = valHeaderTable.Length;
                int colHeader;

                //check lai gia tri nay neu export loi
                int rowHeader = 3;
                int valBegin = 1;
                for (colHeader = valBegin; colHeader < valBegin + lengthHeaderTable; colHeader++)
                {
                    int j = colHeader - valBegin;
                    workSheet.Cells[rowHeader, colHeader].Value = valHeaderTable[j];
                    workSheet.Cells[rowHeader, colHeader].Style.Font.Name = "Time New Roman";
                    workSheet.Cells[rowHeader, colHeader].Style.Font.Size = 11;
                    workSheet.Cells[rowHeader, colHeader].Style.Font.Bold = true;
                    workSheet.Cells[rowHeader, colHeader].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                int startRowFrom = rowHeader + 1;

                if (dmdts.Count() > 0)
                {
                    ExcelExportHelper.SetStyleHeader(workSheet.Cells["A" + rowHeader + ":G" + (rowHeader)]);

                    for (int j = 0; j < dmdts.Count(); j++)
                    {
                        var item = dmdts[j];
                        int i = j + 1;

                        //noi dung
                        workSheet.Cells[startRowFrom, 1].Value = i;
                        workSheet.Cells[startRowFrom, 2].Value = item.MaDT;
                        workSheet.Cells[startRowFrom, 3].Value = item.TenDT;
                        workSheet.Cells[startRowFrom, 4].Value = item.NhomDTID;
                        workSheet.Cells[startRowFrom, 5].Value = item.GiaBVMTID;
                        workSheet.Cells[startRowFrom, 6].Value = item.STT;
                        workSheet.Cells[startRowFrom, 7].Value = item.ChiNhanhID;

                        for (int style = 1; style <= lengthHeaderTable; style++)
                        {
                            workSheet.Cells[startRowFrom, style].Style.Font.Name = "Time New Roman";
                            workSheet.Cells[startRowFrom, style].Style.Font.Size = 11;
                        }
                        startRowFrom++;
                    }
                    ExcelExportHelper.SetStyleContent(workSheet.Cells[rowHeader, 1, startRowFrom - 1, lengthHeaderTable]);
                }
                ExcelExportHelper.SetAutoFit(workSheet, lengthHeaderTable);
                result = package.GetAsByteArray();
            }
            return File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "DanhMucDoiTuong.xlsx");
        }

    }
}