Browse Source

Corrections chargement Vehicules

Patrick Germain 4 months ago
parent
commit
3ba91762ba
2 changed files with 94 additions and 77 deletions
  1. 14 14
      Fuel_ASO/Models/Vehicule.cs
  2. 80 63
      Fuel_ASO/f_impvehi.cs

+ 14 - 14
Fuel_ASO/Models/Vehicule.cs

@@ -2,20 +2,20 @@
 {
     public class Vehicule
     {
-        public string key_vehi { get; set; }
-        public string ope_vehi { get; set; }        
-        public string num_vehi { get; set; }           
-        public string cmpnum_vehi { get; set; }
-        public string immat_vehi { get; set; }
-        public string type_vehi { get; set; }
-        public bool ok_vehi { get; set; }
-        public string carbu_vehi { get; set; }
-        public int kmdep_vehi { get; set; }
-        public int kmarr_vehi { get; set; }
-        public string cond_vehi { get; set; }
-        public string famille_vehi { get; set; }
-        public string order_vehi { get; set; }
-           
+        public string key_vehi { get; set; } = string.Empty;
+        public string ope_vehi { get; set; } = string.Empty;
+        public string num_vehi { get; set; } = string.Empty;
+        public string cmpnum_vehi { get; set; } = string.Empty;
+        public string immat_vehi { get; set; } = string.Empty;
+        public string type_vehi { get; set; } = string.Empty;
+        public bool ok_vehi { get; set; } = false;
+        public string carbu_vehi { get; set; } = string.Empty;
+        public int kmdep_vehi { get; set; } = 0;
+        public int kmarr_vehi { get; set; } = 0;
+        public string cond_vehi { get; set; } = string.Empty;
+        public string famille_vehi { get; set; } = string.Empty;
+        public string order_vehi { get; set; } = string.Empty;
+
         public Vehicule()
         {           
         }

+ 80 - 63
Fuel_ASO/f_impvehi.cs

@@ -36,7 +36,7 @@ namespace Fuel01
             {
                 Import_tdf();
             }
-            
+
         }
 
         private void Bt_openFile_Click(object sender, EventArgs e)
@@ -68,7 +68,7 @@ namespace Fuel01
                 var result = MessageBox.Show("Voulez-vous valider les madifications apportées ?", "Modifications en cours",
                     MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
 
-                if (result == DialogResult.Yes) 
+                if (result == DialogResult.Yes)
                 {
                     SaveData();
                 }
@@ -104,88 +104,103 @@ namespace Fuel01
                 return;
             }
 
-            uint[] indexCol = { 1, 2, 6, 8 };
-            string categ = "A";
+            uint[] indexCol = { 1, 3, 4, 5, 6, 7, 8, 9, 10 };
             int indiceVide = 0;
 
-            for (uint row = 2; row < 800 && indiceVide<10; row++)
+            for (uint row = 2; row < 800 && indiceVide < 10; row++)
             {
+                string categVehi = "A";
                 string model = "";
                 string immat = "";
                 string bandeau = "";
                 string cmp = "";
                 string famille = "";
                 string pilote = "";
+                string carbu = "";
                 bool ligneVide = true;
-                
-
+                int numBandeau = 0;
+                bool isError = false;
+                string fullLine = string.Empty;
                 int vide = 0;
                 for (int colNdx = 0; colNdx < indexCol.Length; colNdx++)
                 {
                     uint col = indexCol[colNdx];
                     var varex = xlWorkSheet.Cells[row, col].Value;
-                    if (varex != null)
+                    if (varex == null)
                     {
-                        ligneVide = false;
-                        string b = "";
-                        string mytype = varex.GetType().ToString();
-                        switch (mytype)
-                        {
-                            case "System.Int16":
-                            case "System.Int32":
-                            case "System.Int64":
-                            case "System.Double":
-                                b = ((Int32)varex).ToString();
-                                break;
+                        varex = string.Empty;
+                    }
 
-                            default:
-                                b = (string)varex;
-                                break;
-                        }
+                    string b = "";
+                    string mytype = varex.GetType().ToString();
+                    switch (mytype)
+                    {
+                        case "System.Int16":
+                        case "System.Int32":
+                        case "System.Int64":
+                        case "System.Double":
+                            b = ((Int32)varex).ToString();
+                            break;
+
+                        default:
+                            b = (string)varex;
+                            break;
+                    }
 
-                        int p;
-                        if (col == 12 && (p = b.IndexOf('\n')) != -1)
-                            b = b.Substring(0, p);
-                        b = b.Replace('\n', ' ');
-                        while (b.Contains("  "))
-                            b = b.Replace("  ", " ");
-                        switch (col)
-                        {
-                            case 1:
-                                bandeau = b;
-                                break;
-                            case 2:
-                                model = b;
-                                break;
+                    int p;
+                    if (col == 12 && (p = b.IndexOf('\n')) != -1)
+                        b = b.Substring(0, p);
+                    b = b.Replace('\n', ' ');
+                    while (b.Contains("  "))
+                        b = b.Replace("  ", " ");
 
-                            case 6:
-                                famille = b;
-                                break;
-                            case 8:
-                                immat = b;
-                                break;
-                        }
+                    fullLine += b;
+                    
+                    switch (col)
+                    {
+                        case 1:
+                            if (!"AM".Contains(b.ToUpperInvariant()))
+                            {
+                                isError = true;
+                            }
+                            categVehi = b;
+                            break;
+                        case 3:
+                            model = b;
+                            break;
+                        case 4:
+                        case 5:
+                        case 6:
+                            immat += string.IsNullOrWhiteSpace(immat) ? b : $" {b}";
+                            break;
+                        case 7:
+                            bandeau = b;
+                            break;
+                        case 8:
+                            famille = b;
+                            if (!Int32.TryParse(bandeau, out numBandeau))
+                            {
+                                isError = true;
+                            }
+                            break;
+                        case 9:
+                            pilote = b;
+                            break;
+                        case 10:
+                            carbu = string.IsNullOrWhiteSpace(b) ? "SP98" : b;
+                            break;
                     }
+
                 }
+                ligneVide = string.IsNullOrEmpty(fullLine);
+                indiceVide += ligneVide ? 1 : 0;
 
-                if (ligneVide)
+                if (isError && !string.IsNullOrEmpty(fullLine) )
                 {
-                    try
-                    {
-                        string m = (string)(xlWorkSheet.Cells[row, 4].Value);
-                        if (m != null && m.Contains("MOTOS"))
-                        {
-                            categ = "M";
-                        }
-                    }
-                    catch (Exception e)
-                    {
-                        Console.WriteLine(e.Message);
-                    }
-                    indiceVide++;
+                    MessageBox.Show(string.Format("Données hors valeur \r\nBandeau = {0} \r\nLa donnée doit être numérique\r\n\r\nType de Véhicule :{1}\r\nValeurs autorisées : A ou M ", bandeau, categVehi));
                 }
 
-                if (bandeau != "")
+                if (bandeau != "" && !isError)
                 {
                     vide = 0;
                     DataSet1.tb_vehiRow myNewRow = null;
@@ -214,10 +229,10 @@ namespace Fuel01
                         myNewRow["immat_vehi"] = immat;
                         myNewRow["type_vehi"] = model;
                         myNewRow["famille_vehi"] = famille;
-                        myNewRow["categ_vehi"] = categ;
+                        myNewRow["categ_vehi"] = categVehi;
                         myNewRow["cond_vehi"] = pilote;
-                        myNewRow["sort1_vehi"] = string.Format("{0}{1:00000}{2}", myNewRow["categ_vehi"], myNewRow["num_vehi"], myNewRow["cmpnum_vehi"]);
-                        myNewRow["carbu_vehi"] = categ == "M" ? "SP98" : "DIESEL";
+                        myNewRow["sort1_vehi"] = string.Format("{0}{1:00000}{2}", categVehi, numBandeau, cmp);
+                        myNewRow["carbu_vehi"] = carbu;
                         if (newrow)
                             tb_vehi.Addtb_vehiRow(myNewRow);
                         StatusLabel1.Text = string.Format("{0}{1:00000}{2}-{3}-{4}", myNewRow["categ_vehi"], myNewRow["num_vehi"], myNewRow["cmpnum_vehi"], myNewRow["cond_vehi"], myNewRow["immat_vehi"]);
@@ -229,7 +244,9 @@ namespace Fuel01
                     }
                 }
                 else
+                {
                     vide++;
+                }
                 if (vide > 10)
                     break;
             }
@@ -238,7 +255,7 @@ namespace Fuel01
             xlApp?.Quit();
 
             StatusLabel1.Text = "F I N I .................";
-            MessageBox.Show($"Importation de {textNomFile.Text} terminée.", "Importation",MessageBoxButtons.OK, MessageBoxIcon.Information);
+            MessageBox.Show($"Importation de {textNomFile.Text} terminée.", "Importation", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         private void Init_list_famille()
         {