using ExcelDataReader; using MALDFGASSURANCE.Areas.ADHESION.Controllers; using MALDFGASSURANCE.Models; using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; namespace MALDFGASSURANCE.Areas.COTISATION.Controllers { [Authorize(Roles = "Coordonnateur")] public class SIMUPAYEMENTSController : Controller { private MALDFGEntities db = new MALDFGEntities(); // GET: COTISATION/SIMUPAYEMENTS public ActionResult Index() { return View(db.SIMUPAYEMENT.ToList()); } // GET: COTISATION/SIMUPAYEMENTS/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } SIMUPAYEMENT sIMUPAYEMENT = db.SIMUPAYEMENT.Find(id); if (sIMUPAYEMENT == null) { return HttpNotFound(); } return View(sIMUPAYEMENT); } // GET: COTISATION/SIMUPAYEMENTS/Create public ActionResult Create() { return View(); } // POST: COTISATION/SIMUPAYEMENTS/Create // Afin de déjouer les attaques par survalidation, activez les propriétés spécifiques auxquelles vous voulez établir une liaison. Pour // plus de détails, consultez https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Iconsim,Reference,montant,periodedebut,periodefin,datepayement,status,resultat,commentaire")] SIMUPAYEMENT sIMUPAYEMENT) { if (ModelState.IsValid) { db.SIMUPAYEMENT.Add(sIMUPAYEMENT); db.SaveChanges(); return RedirectToAction("Index"); } return View(sIMUPAYEMENT); } // GET: COTISATION/SIMUPAYEMENTS/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } SIMUPAYEMENT sIMUPAYEMENT = db.SIMUPAYEMENT.Find(id); if (sIMUPAYEMENT == null) { return HttpNotFound(); } return View(sIMUPAYEMENT); } // POST: COTISATION/SIMUPAYEMENTS/Edit/5 // Afin de déjouer les attaques par survalidation, activez les propriétés spécifiques auxquelles vous voulez établir une liaison. Pour // plus de détails, consultez https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Iconsim,Reference,montant,periodedebut,periodefin,datepayement,status,resultat,commentaire")] SIMUPAYEMENT sIMUPAYEMENT) { if (ModelState.IsValid) { db.Entry(sIMUPAYEMENT).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(sIMUPAYEMENT); } // GET: COTISATION/SIMUPAYEMENTS/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } SIMUPAYEMENT sIMUPAYEMENT = db.SIMUPAYEMENT.Find(id); if (sIMUPAYEMENT == null) { return HttpNotFound(); } return View(sIMUPAYEMENT); } // POST: COTISATION/SIMUPAYEMENTS/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { SIMUPAYEMENT sIMUPAYEMENT = db.SIMUPAYEMENT.Find(id); db.SIMUPAYEMENT.Remove(sIMUPAYEMENT); db.SaveChanges(); return RedirectToAction("Index"); } public bool DeleteAll() { //SIMULATIONFIXEE sIMULATION = new SIMULATIONFIXEE(); var payement3 = db.SIMUPAYEMENT.ToList(); db.SIMUPAYEMENT.RemoveRange((IEnumerable)payement3); db.SaveChanges(); return true; } public bool ModifierPayement([Bind(Include = "Iconsim,Reference,montant,periodedebut,periodefin,datepayement,status,resultat,commentaire")] SIMUPAYEMENT sIMUPAYEMENT) { if (ModelState.IsValid) { db = new MALDFGEntities(); db.Entry(sIMUPAYEMENT).State = EntityState.Modified; db.SaveChanges(); return true; } return false; } public void ControleSimmulateurCotisation() { //Session.Add("MessageImport","Importation en cours veuillez ...."); ADHERENTsController ADHERENTS = new ADHERENTsController(); db = new MALDFGEntities(); var Payement1 = db.SIMUPAYEMENT.ToList(); bool anomaliRetrouver = false; foreach (var item in Payement1 as IList) { SIMUPAYEMENT Payement = new SIMUPAYEMENT(); Payement.Iconsim = item.Iconsim; Payement.montant = item.montant; Payement.Reference = item.Reference; Payement.periodedebut = item.periodedebut; Payement.periodefin = item.periodefin; Payement.datepayement = item.datepayement; Payement.status = 1; //Payement.resultat = "Verifier"; Payement.commentaire += ""; if (string.IsNullOrEmpty(item.Reference)) { Payement.commentaire += "Matricule est nul" + Environment.NewLine; anomaliRetrouver = true; } else { if (ADHERENTS.InfosMatriculeAdherent(item.Reference) != null) { Payement.resultat += ADHERENTS.InfosNomAdherent(item.Reference); } else { Payement.commentaire += "Matricule Inexistant " + Environment.NewLine; anomaliRetrouver = true; } } int myInt; bool successMontant = int.TryParse(item.montant.ToString(), out myInt); if (!successMontant) { Payement.commentaire += "Erreur sur Montant" + Environment.NewLine; anomaliRetrouver = true; } DateTime mydate; bool successdate1 = DateTime.TryParse(item.periodedebut.ToString(), out mydate); if (!successdate1) { Payement.commentaire += "Erreur sur la date debut" + Environment.NewLine; anomaliRetrouver = true; } bool successdate2 = DateTime.TryParse(item.periodefin.ToString(), out mydate); if (!successdate2) { Payement.commentaire += "Erreur sur la date fin" + Environment.NewLine; anomaliRetrouver = true; } bool successdate4 = DateTime.TryParse(item.datepayement.ToString(), out mydate); if (!successdate4) { Payement.commentaire += "Erreur sur date de Payement" + Environment.NewLine; anomaliRetrouver = true; } //Payement.resultat += string.IsNullOrEmpty(Payement.resultat) ? "_KO" : "_OK"; if (!ModifierPayement(Payement)) { error(); }; } if (!anomaliRetrouver) { PAYERCOTISATION pAYERCOTISATION = new PAYERCOTISATION(); PAYERCOTISATIONSController pAYERCOTISATIONSController = new PAYERCOTISATIONSController(); foreach (var item in db.SIMUPAYEMENT.ToList()) { pAYERCOTISATION.REFERENCE_ADHERENT = ADHERENTS.InfosMatriculeAdherent(item.Reference.Trim()); pAYERCOTISATION.CODE_TYPE_PAIEMENT = "001"; int Montant = 0; int.TryParse(item.montant.ToString().Trim(), out Montant); pAYERCOTISATION.MONTANT_PAYE = Montant; DateTime mydate2; DateTime.TryParse(item.periodefin.ToString(), out mydate2); pAYERCOTISATION.DATEFINPAYERCOTISATISATION = mydate2; DateTime.TryParse(item.periodedebut.ToString(), out mydate2); pAYERCOTISATION.DATEDEBUTPAYERCOTISATION = mydate2; DateTime.TryParse(item.datepayement.ToString(), out mydate2); pAYERCOTISATION.DATE_PAIEMENT = mydate2; pAYERCOTISATION.IDUTILISATEUR = "IMPORTATION"; pAYERCOTISATION.ETAT = 1; pAYERCOTISATION.DATECREATION = DateTime.Now; if (!pAYERCOTISATIONSController.AJouter(pAYERCOTISATION)) { error(); } } //Session.Add("MessageImport", "Le fichier a été importer avec success"); } else { //Session.Add("MessageImport", "Echec d'import de fichier"); } } public ActionResult error() { return View(); } private List GetDataFromCSVFile(Stream stream) { var empList = new List(); try { using (var reader = ExcelReaderFactory.CreateCsvReader(stream)) { var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true // To set First Row As Column Names } }); if (dataSet.Tables.Count > 0) { var dataTable = dataSet.Tables[0]; foreach (DataRow objDataRow in dataTable.Rows) { if (objDataRow.ItemArray.All(x => string.IsNullOrEmpty(x?.ToString()))) continue; empList.Add(new SIMUPAYEMENT() { Reference = objDataRow["Reference"].ToString(), montant = Convert.ToInt32(objDataRow["Montant"].ToString()), datepayement = Convert.ToDateTime(objDataRow["Datepayement"].ToString()), periodedebut = Convert.ToDateTime(objDataRow["DateDebut"].ToString()), periodefin = Convert.ToDateTime(objDataRow["DateFin"].ToString()), }); } } } } catch (Exception) { throw; } return empList; } [HttpPost] public ActionResult ImportFile(HttpPostedFileBase importFile) { if (importFile == null) return Json(new { Status = 0, Message = "Aucun fichier selectionné" }); try { var fileData = GetDataFromCSVFile(importFile.InputStream); DeleteAll(); SIMUPAYEMENT Payement = new SIMUPAYEMENT(); ADHERENTsController ADHERENTS = new ADHERENTsController(); bool verifier = false; foreach (var items in fileData as IList) { Payement.Reference = items.Reference; Payement.montant = items.montant; Payement.periodedebut = items.periodedebut; Payement.periodefin = items.periodefin; Payement.datepayement = items.datepayement; if (ADHERENTS.InfosMatriculeAdherent(items.Reference) != null) { Payement.resultat = ADHERENTS.InfosNomAdherent(items.Reference); } else { Payement.resultat = " Inexistant"; } if (ModelState.IsValid) { db.SIMUPAYEMENT.Add(Payement); db.SaveChanges(); verifier = true; } else { verifier = false; } } if (verifier) { // Thread myThread; //myThread = new Thread(new ThreadStart(ControleSimmulateurCotisation)); ControleSimmulateurCotisation(); // Lancement du thread //myThread.Start(); return Json(new { Status = 1, Message = "File Imported Successfully " }); } //await _dbContext.Database.ExecuteSqlCommandAsync("EXEC spBulkImportEmployee @tblEmployeeTableType", tblEmployeeParameter); return Json(new { Status = 0, Message = "Il y a des erreurs dans le fichier " }); } catch (Exception ex) { return Json(new { Status = 0, Message = ex.Message }); } } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }