Esempio n. 1
0
        public ActionResult ListOrderExcel(string DateFrom = "", string DateTo = "", string StaffCode = "", string ProcessId = "process", string StatusId = "", string SalePlace = "", string C1Code = "")
        {
            int permit = Utitl.CheckRoleShowInfo(db, User.Identity.Name);

            var current = DateTime.Now;

            DateTime dFrom;

            DateTime dTo;

            if (String.IsNullOrEmpty(DateFrom) || String.IsNullOrEmpty(DateTo))
            {
                dTo   = current;
                dFrom = current.AddMonths(-1);
            }
            else
            {
                dFrom = DateTime.ParseExact(DateFrom, "d/M/yyyy", null);
                dTo   = DateTime.ParseExact(DateTo, "d/MM/yyyy", null);
            }

            List <get_list_orders_product_Result> data = new List <get_list_orders_product_Result>();


            data = db.get_list_orders_product(dFrom.ToString("yyyy-MM-dd"), dTo.ToString("yyyy-MM-dd"), "%" + ProcessId + "%", "%" + StatusId + "%", "%" + StaffCode + "%", "%" + C1Code + "%").ToList();

            data = data.Where(p => p.SalePlace.Contains(SalePlace)).ToList();

            if (permit == 2)
            {
                // get list cn
                var branchPermit = db.UserBranchPermisses.Where(p => p.UserName == User.Identity.Name).Select(p => p.BranchCode).ToList();
                data = data.Where(p => branchPermit.Contains(p.BrachCode)).ToList();
            }

            string pathRoot = Server.MapPath("~/haiupload/list_order_form.xlsx");
            string name     = "donhang" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".xlsx";
            string pathTo   = Server.MapPath("~/temp/" + name);

            System.IO.File.Copy(pathRoot, pathTo);

            try
            {
                FileInfo newFile = new FileInfo(pathTo);
                using (ExcelPackage package = new ExcelPackage(newFile))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                    for (int i = 0; i < data.Count; i++)
                    {
                        try
                        {
                            worksheet.Cells[i + 2, 1].Value = i + 1;
                            worksheet.Cells[i + 2, 2].Value = data[i].CreateDate;
                            worksheet.Cells[i + 2, 3].Value = data[i].Code;
                            worksheet.Cells[i + 2, 4].Value = data[i].BrachCode;
                            worksheet.Cells[i + 2, 5].Value = data[i].StaffCode;
                            worksheet.Cells[i + 2, 6].Value = data[i].StaffName;

                            worksheet.Cells[i + 2, 7].Value = data[i].AgencyCode;
                            worksheet.Cells[i + 2, 8].Value = data[i].Store;

                            worksheet.Cells[i + 2, 9].Value = data[i].ExpectDate;

                            worksheet.Cells[i + 2, 10].Value = data[i].C1Code;
                            worksheet.Cells[i + 2, 11].Value = data[i].C1Name;

                            if (String.IsNullOrEmpty(data[i].C1Code))
                            {
                                worksheet.Cells[i + 2, 10].Value = data[i].BrachCode;
                                worksheet.Cells[i + 2, 11].Value = "LẤY TẠI CN";
                            }

                            if (data[i].OrderStatus == "process")
                            {
                                worksheet.Cells[i + 2, 12].Value = "Đang xử lý";
                            }
                            else if (data[i].OrderStatus == "finish")
                            {
                                worksheet.Cells[i + 2, 12].Value = "Hoàn thành";
                            }
                            else if (data[i].OrderStatus == "cancel")
                            {
                                worksheet.Cells[i + 2, 12].Value = "Đã hủy không giao";
                            }

                            if (data[i].QuantityFinish == 0)
                            {
                                worksheet.Cells[i + 2, 13].Value = "Chưa giao";
                            }
                            else if (data[i].QuantityFinish == data[i].OrderQuantity)
                            {
                                worksheet.Cells[i + 2, 13].Value = "Giao đủ";
                            }
                            else if (data[i].QuantityFinish > data[i].OrderQuantity)
                            {
                                worksheet.Cells[i + 2, 13].Value = "Giao nhiều hơn";
                            }
                            else
                            {
                                worksheet.Cells[i + 2, 13].Value = "Giao ít hơn";
                            }


                            //
                            if (data[i].HasBill == 1)
                            {
                                worksheet.Cells[i + 2, 14].Value = "Có";
                            }
                            else
                            {
                                worksheet.Cells[i + 2, 14].Value = "Không";
                            }

                            //
                            if (data[i].GoodType == "warehouse")
                            {
                                worksheet.Cells[i + 2, 15].Value = "Hàng gửi kho";
                            }
                            else if (data[i].GoodType == "new")
                            {
                                worksheet.Cells[i + 2, 15].Value = "Hàng xuất mới";
                            }
                            else
                            {
                                worksheet.Cells[i + 2, 15].Value = "Không xác định";
                            }

                            worksheet.Cells[i + 2, 16].Value = data[i].PName;

                            worksheet.Cells[i + 2, 17].Value = data[i].OrderQuantity / data[i].PQuantity;
                            worksheet.Cells[i + 2, 18].Value = data[i].OrderQuantity % data[i].PQuantity;
                            worksheet.Cells[i + 2, 19].Value = data[i].PPriceTotal;

                            worksheet.Cells[i + 2, 20].Value = data[i].QuantityFinish / data[i].PQuantity;
                            worksheet.Cells[i + 2, 21].Value = data[i].QuantityFinish % data[i].PQuantity;
                            worksheet.Cells[i + 2, 22].Value = data[i].QuantityFinish * data[i].PerPrice;


                            if (data[i].OrderType == "checkinorder")
                            {
                                worksheet.Cells[i + 2, 23].Value = "Trong checkin";
                            }
                            else
                            {
                                worksheet.Cells[i + 2, 23].Value = "Ngoài checkin";
                            }
                        }
                        catch
                        {
                            return(RedirectToAction("error", "home"));
                        }
                    }

                    package.Save();
                }
            }
            catch
            {
                return(RedirectToAction("error", "home"));
            }


            return(File(pathTo, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", string.Format("ds-don-hang-" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".{0}", "xlsx")));
        }