Exemplo n.º 1
0
        public IActionResult Dogovor(int?id, ClientSpisokViewModel model)
        {
            Program.ClientId = (int)id;
            ViewBag.ClientId = id;
            ViewBag.Client   = _client.Read(new ClientBindingModel {
                Id = (int)id
            }).FirstOrDefault();
            if (id == null)
            {
                return(NotFound());
            }
            var dogovor = _dogovor.Read(null);

            foreach (var d in dogovor)
            {
                if (d.Dogovor_Reiss.Count == 0)
                {
                    _dogovor.Delete(new DogovorBindingModel {
                        Id = d.Id
                    });
                }
            }
            if (model.DogovorId > 0)
            {
                dogovor = _dogovor.Read(new DogovorBindingModel
                {
                    Id       = model.DogovorId,
                    ClientId = (int)id
                });
                foreach (var r in dogovor)
                {
                    ViewBag.Dogovors = _client.Read(new ClientBindingModel {
                        Id = r.ClientId
                    });
                }
                if (dogovor.Count != 0)
                {
                    ViewBag.Dogovors = dogovor;
                    return(View());
                }
                ModelState.AddModelError("Passport", "Такого Договора у клиента не существует");
            }
            ViewBag.Dogovors = _dogovor.Read(new DogovorBindingModel
            {
                ClientId = (int)id,
                AgentId  = (int)Program.Agent.Id
            });

            return(View());
        }
        public ActionResult Archivation(ReportViewModel model)
        {
            DateTime date1    = DateTime.Now;
            string   fileName = model.puth + $"\\ArchiveOf{date1.Year}";

            Directory.CreateDirectory(fileName);
            if (Directory.Exists(fileName))
            {
                var dogovors    = dogovorLogic.Read(null);//все договоры
                var olddogovors = dogovorLogic.Read(new DogovorBindingModel {
                    Id = 0
                });                                                                     //
                var oldreis = dogovorLogic.ReadReis(new Dogovor_ReisBM {
                    Id = 0
                });                                                                //устаревшие рейсы по договору
                bool proverca = false;
                foreach (var dogovor in dogovors)
                {
                    proverca = false;
                    foreach (var dr in dogovor.Dogovor_Reiss)
                    {
                        var reis1 = rlogic.Read(new ReisBindingModel {
                            Id = dr.Value.Item2
                        })[0];
                        DateTime dt1 = dogovor.data.AddDays(reis1.Time + dr.Value.Item4);
                        DateTime dt2 = DateTime.Now;
                        if (DateTime.Now.Month != 12)
                        {
                            dt2 = DateTime.Now.AddYears(-1);
                        }
                        if (dt1 > dt2)
                        {
                            proverca = true;
                        }
                    }
                    if (proverca != true)
                    {
                        olddogovors.Add(dogovor);
                        foreach (var dr in dogovor.Dogovor_Reiss)
                        {
                            oldreis.Add(dogovorLogic.ReadReis(new Dogovor_ReisBM {
                                Id = dr.Key
                            })[0]);
                        }
                        dogovorLogic.Delete(new DogovorBindingModel {
                            Id = dogovor.Id
                        });
                    }
                }
                DataContractJsonSerializer jsonFormatter = new
                                                           DataContractJsonSerializer(typeof(List <DogovorViewModel>));
                using (FileStream fs = new FileStream(string.Format("{0}/{1}.json",
                                                                    fileName, "Dogovors"), FileMode.OpenOrCreate))
                {
                    jsonFormatter.WriteObject(fs, olddogovors);
                }
                jsonFormatter = new
                                DataContractJsonSerializer(typeof(List <Dogovor_ReisVM>));
                using (FileStream fs = new FileStream(string.Format("{0}/{1}.json",
                                                                    fileName, "OldDogovorReis"), FileMode.OpenOrCreate))
                {
                    jsonFormatter.WriteObject(fs, oldreis);
                }
                ZipFile.CreateFromDirectory(fileName, $"{fileName}.zip");
                Directory.Delete(fileName, true);
                Mail.SendMail(model.SendMail, $"{fileName}.zip", $"Archive");
                return(RedirectToAction("Archive"));
            }
            else
            {
                DirectoryInfo di = Directory.CreateDirectory(fileName);
                return(RedirectToAction("Archive"));
            }
        }