Example #1
0
        public IActionResult Validation(int Id, ReportViewModel model, DateTime date1, DateTime date2)
        {
            ViewBag.Id = Id;

            if (!Directory.Exists(model.Puth))
            {
                ViewBag.Date1         = date1;
                ViewBag.Date2         = date2;
                ViewBag.Id            = Id;
                TempData["ErrorLack"] = "На данном компьютере нет такого пути";
                return(RedirectToAction("SendPuth", new { date1 = date1, date2 = date2, id = Id }));
            }
            if (String.IsNullOrEmpty(model.Name))
            {
                ViewBag.Date1 = date1;
                ViewBag.Date2 = date2;
                ViewBag.Id    = Id;
                ModelState.AddModelError("", "Введите навзание");
                return(RedirectToAction("SendPuth", new { date1 = date1, date2 = date2, id = Id }));
            }
            if (Id == -3)
            {
                Directory.CreateDirectory(model.Puth + model.Name);
                if (Directory.Exists(model.Puth + model.Name))
                {
                    var dogovors   = dogovor.ReadDate(date1, date2);  //все договоры
                    var oldproduct = dogovor.ReadP(new DogovorProductBM {
                        Id = 0
                    });                                                                 //устаревшие рейсы по договору
                    bool proverca = false;
                    foreach (var Dogovor in dogovors)
                    {
                        var product = dogovor.ReadP(new DogovorProductBM {
                            DogovorId = (int)Dogovor.Id
                        });

                        foreach (var dr in product)
                        {
                            oldproduct.Add(dogovor.ReadP(new DogovorProductBM {
                                Id = dr.Id
                            })[0]);
                        }
                        dogovor.Delete(new DogovorBM {
                            Id = Dogovor.Id
                        });
                    }
                    DataContractJsonSerializer jsonFormatter = new
                                                               DataContractJsonSerializer(typeof(List <DogovorVM>));
                    using (FileStream fs = new FileStream(string.Format("{0}/{1}.json",
                                                                        model.Puth + model.Name, "Dogovors"), FileMode.OpenOrCreate))
                    {
                        jsonFormatter.WriteObject(fs, dogovors);
                    }
                    jsonFormatter = new
                                    DataContractJsonSerializer(typeof(List <DogovorProductVM>));
                    using (FileStream fs = new FileStream(string.Format("{0}/{1}.json",
                                                                        model.Puth + model.Name, "DogovorProductVM"), FileMode.OpenOrCreate))
                    {
                        jsonFormatter.WriteObject(fs, oldproduct);
                    }
                    System.IO.File.Delete($"{model.Puth + model.Name}.zip");
                    ZipFile.CreateFromDirectory(model.Puth + model.Name, $"{model.Puth + model.Name}.zip");

                    Directory.Delete(model.Puth + model.Name, true);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    DirectoryInfo di = Directory.CreateDirectory(model.Puth + model.Name);
                    return(RedirectToAction("Index", "Home"));
                }
            }


            if (Id > 0)
            {
                ReportLogic.CreateDoc(new PdfInfo
                {
                    id             = 1,
                    FileName       = model.Puth + model.Name + ".pdf",
                    Title          = $"Договор № {Id}",
                    Products       = product.Read(null),
                    DogovorProduct = dogovor.ReadP(new DogovorProductBM {
                        DogovorId = Id
                    }),
                    Dogovors = dogovor.Read(new DogovorBM {
                        Id = Id
                    }),
                    User = user.Read(new UserBM {
                        Id = dogovor.Read(new DogovorBM {
                            Id = Id
                        })[0].UserId
                    })
                });
                ViewBag.Dogovor = dogovor.Read(null);
                ViewBag.User    = user.Read(null);
                return(View("Dogovors"));
            }
            if (Id == 0)
            {
                ReportLogic.CreateDoc(new PdfInfo
                {
                    id       = 0,
                    FileName = model.Puth + model.Name + ".pdf",
                    Title    = $"Все Договора",
                    Dogovors = dogovor.Read(null),
                    User     = user.Read(null)
                });
                ViewBag.Dogovor = dogovor.Read(null);
                ViewBag.User    = user.Read(null);
                return(View("Dogovors"));
            }
            if (Id == -1)
            {
                ReportLogic.CreateDoc(new PdfInfo
                {
                    id           = -1,
                    FileName     = model.Puth + model.Name + ".pdf",
                    Title        = $"Отчет по перемещения продукции по складам за {date1.ToShortDateString()}   {date2.ToShortDateString()}",
                    Sklad        = sklad.Read(null),
                    Products     = product.Read(null),
                    SkladProduct = product.ReadSPdate(date1, date2)
                });

                return(View("Report"));
            }
            if (Id == -2)
            {
                ReportLogic.CreateDoc(new PdfInfo
                {
                    id       = -2,
                    FileName = model.Puth + model.Name + ".pdf",
                    Title    = $"Отчет по работе сотрудников за пeриод {date1.ToShortDateString()}   {date2.ToShortDateString()}",
                    Dogovors = dogovor.Read(null),
                    User     = user.Read(null)
                });
                return(View("Report"));
            }

            return(RedirectToAction("Index", "Home"));
        }