Example #1
0
        public IActionResult Delete(string id)
        {
            var lot = _lotService.Get(id);

            if (lot == null)
            {
                return(NotFound());
            }

            _userService.Remove(lot.Id);

            return(NoContent());
        }
        public void updateEmploi(string compte, string emploi)
        {
            using (WakilRecouvContext WakilContext = new WakilRecouvContext())
            {
                using (UnitOfWork UOW = new UnitOfWork(WakilContext))
                {
                    LotService LotService = new LotService(UOW);


                    Lot lot = LotService.Get(l => l.IDClient == compte);

                    if (lot != null)
                    {
                        lot.Emploi = emploi;
                        Debug.WriteLine(lot.Compte);
                        LotService.Commit();
                    }
                    else
                    {
                        Debug.WriteLine("no no ===>" + compte);
                    }
                }
            }
        }
        public ActionResult UploadExcel(HttpPostedFileBase PostedFile)
        {
            using (WakilRecouvContext WakilContext = new WakilRecouvContext())
            {
                using (UnitOfWork UOW = new UnitOfWork(WakilContext))
                {
                    LotService LotService = new LotService(UOW);


                    string filename = "";
                    //Nthabtou li fichier mahouch feragh makenesh nabaathou erreur lel client
                    if (PostedFile != null)
                    {
                        filename = PostedFile.FileName;
                        //nsobou l fichier aana fel serveur
                        string filePath = string.Empty;
                        string path     = Server.MapPath("~/Uploads/");
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }

                        filePath = path + Path.GetFileName(PostedFile.FileName);
                        string extension = Path.GetExtension(PostedFile.FileName);
                        string conString = string.Empty;

                        //nekhdhou num mtaa lot men esm l fichier ex: Lot 11 => 11
                        string numLot = string.Join(string.Empty, Regex.Matches(PostedFile.FileName, @"\d+").OfType <Match>().Select(m => m.Value));
                        //string numLot = filename.Split('_')[1];

                        //Nthaabtou eli l client selectiona fichier excel moush haja okhra makanesh nabaathoulou erreur
                        if (PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                        {
                            PostedFile.SaveAs(filePath);

                            //besh nakhtarou connectionString selon l version mtaa excel (xls = 2003 o xlsx mel 2013 o ahna tal3in)
                            //L connectionString predefini fel web.config mteena
                            switch (extension)
                            {
                            case ".xls":
                                conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                                break;

                            case ".xlsx":
                                conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                                break;
                            }

                            //Taoua besh nebdew nakraw l fichier Excel bel library OleDd
                            DataTable dt = new DataTable();
                            conString = string.Format(conString, filePath);
                            using (OleDbConnection connExcel = new OleDbConnection(conString))
                            {
                                using (OleDbCommand cmdExcel = new OleDbCommand())
                                {
                                    using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                                    {
                                        // Houni nebdew naakraw awel sheet name mtaa l document excel mteena (eli ken jina fi table SQL rahou le nom de la table)
                                        cmdExcel.Connection = connExcel;
                                        connExcel.Open();
                                        DataTable dtExcelSchema;
                                        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                                        string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                                        connExcel.Close();

                                        //Houni recuperation mtaa les données
                                        connExcel.Open();
                                        cmdExcel.CommandText   = "SELECT * FROM [" + sheetName + "]";
                                        odaExcel.SelectCommand = cmdExcel;
                                        odaExcel.Fill(dt);
                                        connExcel.Close();



                                        //string argNumLot = dt.Columns[0].ColumnName;
                                        string argId             = "ID";
                                        string argCompte         = "Compte";
                                        string argNomClient      = "Nom Client";
                                        string argCompteDebiteur = "Compte Debiteur";
                                        string argTelPortable    = "Tel Portable";
                                        string argTelFixe        = "Tel Fixe";
                                        string argAgence         = "Agence";
                                        string argAdresse        = "Adresse";
                                        string argType           = "Type";
                                        string argNumero         = "Numero";
                                        string argEmploi         = "Travail";

                                        /*   string Compte1Arg = "Compte1";
                                         * string Compte2Arg = "Compte2";
                                         * string Nom1Arg = "Nom1";
                                         * string Nom2Arg = "Nom2";*/

                                        /*   List<ClientCompte> ccList1 = new List<ClientCompte>();
                                         * List<ClientCompte> ccList2 = new List<ClientCompte>();
                                         */


                                        foreach (DataRow row in dt.Rows)
                                        {
                                            string IdClient       = "";
                                            string NomClient      = "";
                                            string Compte         = "";
                                            string CompteDebiteur = "";
                                            string TelPortable    = "";
                                            string TelFixe        = "";
                                            string Agence         = "";
                                            string Adresse        = "";
                                            string Type           = "";
                                            string Numero         = "";
                                            string Emploi         = "";

                                            /* string Compte1 = "";
                                             * string Compte2 = "";
                                             * string Nom1= "";
                                             * string Nom2= "";*/


                                            //updateEmploi(row["Compte"].ToString(), row["Travail"].ToString());

/*
 *
 *                                          try
 *                                          {
 *                                              Compte1 = row[Compte1Arg].ToString();
 *                                          }
 *                                          catch (ArgumentException e)
 *                                          {
 *                                              ModelState.AddModelError("Importer", "La colonne " + Compte1Arg + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
 *                                              return View("ImportLot");
 *                                          }
 *
 *                                          try
 *                                          {
 *                                              Compte2 = row[Compte2Arg].ToString();
 *                                          }
 *                                          catch (ArgumentException e)
 *                                          {
 *                                              ModelState.AddModelError("Importer", "La colonne " + Compte2Arg + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
 *                                              return View("ImportLot");
 *                                          }
 *
 *
 */


                                            /*
                                             * try
                                             * {
                                             *    Nom1 = row[Nom1Arg].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *    ModelState.AddModelError("Importer", "La colonne " + Nom1Arg + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *    return View("ImportLot");
                                             * }
                                             *
                                             *
                                             * try
                                             * {
                                             *    Nom2 = row[Nom2Arg].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *    ModelState.AddModelError("Importer", "La colonne " + Nom2Arg + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *    return View("ImportLot");
                                             * }
                                             *
                                             * ClientCompte c1 = new ClientCompte()
                                             * {
                                             *
                                             *    Nom = Nom1,
                                             *    Compte = Compte1
                                             *
                                             * };
                                             * ClientCompte c2 = new ClientCompte()
                                             * {
                                             *
                                             *    Nom = Nom2,
                                             *    Compte = Compte2
                                             *
                                             * };*/

/*
 *                                          ccList1.Add(c1);
 *                                          ccList2.Add(c2);
 */


                                            //Debug.WriteLine(row["Travail"].ToString());
                                            updateEmploi(row["ID"].ToString(), row["Travail"].ToString());


                                            //On verifier aala koll colonne eli l fichier excel il respecte bien le syntaxe mtaa koll colonne bel try/catch
                                            //O nsobou l information/donné fi variable


                                            /*
                                             *
                                             * try
                                             * {
                                             *   IdClient = row[argId].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argId + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   NomClient = row[argNomClient].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argNomClient + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Compte = row[argCompte].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argCompte + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   CompteDebiteur = row[argCompteDebiteur].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argCompteDebiteur + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   TelPortable = row[argTelPortable].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argTelPortable + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   TelFixe = row[argTelFixe].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argTelFixe + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             *
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Agence = row[argAgence].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argAgence + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Adresse = row[argAdresse].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argAdresse + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Type = row[argType].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argType + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             *
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Emploi = row[argEmploi].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argEmploi + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             *
                                             * }
                                             *
                                             * try
                                             * {
                                             *   Numero = row[argNumero].ToString();
                                             * }
                                             * catch (ArgumentException e)
                                             * {
                                             *   ModelState.AddModelError("Importer", "La colonne " + argNumero + " n'appartient pas à la table Excel, verifier les espaces cachés et les majuscules.");
                                             *   return View("ImportLot");
                                             *
                                             * }
                                             *
                                             *
                                             *
                                             *                                          Lot Lot = new Lot()
                                             *                                          {
                                             *                                              NumLot = numLot,
                                             *                                              IDClient = IdClient,
                                             *                                              Compte = Compte,
                                             *                                              NomClient = NomClient,
                                             *                                              SoldeDebiteur = CompteDebiteur.Replace("-", ""),
                                             *                                              TelPortable = TelPortable,
                                             *                                              TelFixe = TelFixe,
                                             *                                              DescIndustry = Agence,
                                             *                                              Adresse = Adresse,
                                             *                                              Type = Type,
                                             *                                              Numero = Numero,
                                             *                                              Emploi = Emploi
                                             *
                                             *                                          };
                                             */
                                            //On verifie est ce que l client hedha aana menou fel base ouala le



                                            if (LotService.Get(l => l.Compte.Equals(Compte)) != null)
                                            {
                                                //Ken aana menou nshoufou ken identique ouala le
                                                //ken identique manaamlou chay
                                                //ken aaana menou ama fama difference => donc nhotouh a jour
                                                //sinon ken maanesh menou on l'ajouter fel base
                                                //Benesba l ViewData rahom des flag lel affichage
                                                Lot lot = LotService.Get(l => l.Compte.Equals(Compte));

                                                /*
                                                 *
                                                 * Debug.WriteLine(Compte);
                                                 *
                                                 * if (lot.Equals(Lot))
                                                 * {
                                                 *  dup++;
                                                 *  ViewData["nbDup"] = dup;
                                                 *  if (dup > 0)
                                                 *  {
                                                 *      ViewData["dup"] = "1";
                                                 *  }
                                                 * }
                                                 * else
                                                 * {
                                                 *  up++;
                                                 *  ViewData["nbUp"] = up;
                                                 *  if (up > 0)
                                                 *  {
                                                 *      ViewData["up"] = "1";
                                                 *  }
                                                 *
                                                 *  //lot.NumLot = Lot.NumLot;
                                                 *
                                                 *  lot.NomClient = Lot.NomClient;
                                                 *  lot.TelPortable = Lot.TelPortable;
                                                 *  lot.TelFixe = Lot.TelFixe;
                                                 *  lot.Emploi = Lot.Emploi;
                                                 *  lot.SoldeDebiteur = Lot.SoldeDebiteur;
                                                 *  lot.DescIndustry = Lot.DescIndustry;
                                                 *  lot.Adresse = Lot.Adresse;
                                                 *  lot.Type = Lot.Type;
                                                 *  lot.Numero = Lot.Numero;
                                                 *  lot.PostCode = Lot.PostCode;
                                                 *
                                                 *  LotService.Update(lot);
                                                 *
                                                 * }
                                                 */
                                            }
                                            else
                                            {
                                                x++;
                                                ViewData["nb"] = x;

                                                if (x > 0)
                                                {
                                                    ViewData["noDup"] = "1";
                                                }
                                                //LotService.Add(Lot);
                                                //AffecterClient(Lot, filename);
                                            }
                                        }

                                        /*
                                         * ccList1 = ccList1.Where(c => c.Compte != "").ToList();
                                         * ccList2 = ccList2.Where(c => c.Compte != "").ToList();
                                         *
                                         */
                                        List <ClientCompte> newList = new List <ClientCompte>();

                                        /*
                                         * foreach(ClientCompte c in ccList1)
                                         * {
                                         *
                                         *  if(!ccList2.Contains(c))
                                         *  {
                                         *
                                         *      newList.Add(c);
                                         *
                                         *  }
                                         *  else
                                         *  {
                                         *
                                         *  }
                                         *
                                         * }*/


                                        /*
                                         *
                                         * string path1 = GetFolderName() + "/" + "Diff" + "_MAJ_" + DateTime.Now.ToString("dd.MM.yyyy") + "_" + ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds() + ".xlsx";
                                         */

                                        ViewData["nbTotal"]  = dt.Rows.Count;
                                        ViewData["finished"] = "1";
                                        LotService.Commit();

                                        /*
                                         * Debug.WriteLine(newList.Count);
                                         *
                                         * GenerateExcel(GenerateDatatableFromJoinedList(newList), path1);
                                         */
                                    }
                                }
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Importer", "Le fichier selectionné n'est pas un fichier Excel");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Importer", "Vous devez sélectionner un fichier");
                    }

                    return(View("ImportLot"));
                }
            }
        }