Browse Source

NEW #29 Clonage des données actions OK (sauf liens aux colleges)

olivier.massot 7 years ago
parent
commit
8ec3f8b503

+ 20 - 0
CD67.FicheCollege.Entity/Extend/ActionEdu.cs

@@ -31,6 +31,26 @@ namespace CD67.FicheCollege.Entity
                 tiers = wsTiers.GetById(TiersSid);
             }
         }
+
+        public ActionEdu flat()
+        {
+            return new ActionEdu()
+            {
+                Id = Id,
+                Numero = Numero,
+                Nom = Nom,
+                Montant = Montant,
+                TiersSid = TiersSid,
+                Description = Description,
+                CommentaireInterne = CommentaireInterne,
+                CommentairePublic = CommentairePublic,
+                Neutralise = Neutralise,
+                Ordre = Ordre,
+                ActionEduThematiqueId = ActionEduThematiqueId,
+                AnneeId = AnneeId,
+                TokenId = TokenId
+            };
+        }
     }
 
     /// <summary>

+ 30 - 1
CD67.FicheCollege.Factory/ActionEduFactory.cs

@@ -64,5 +64,34 @@ namespace CD67.FicheCollege.Factory
             base.delete(ref entity);
             Sort();
         }
+
+        public ActionEdu clone_to_year(ActionEdu actionEdu, int annee_id)
+        {
+            if (base.getAll().Where(a => a.AnneeId == annee_id && a.TokenId == actionEdu.TokenId).Count() > 0)
+            {
+                throw new InvalidOperationException("Cette action éducative existe déjà pour l'année cible");
+            }
+
+            ActionEdu clone = new ActionEdu();
+            // Nouvelle année
+            clone.AnneeId = annee_id;
+            // On reprend les valeurs de tous les autres champs
+            clone.Numero = actionEdu.Numero;
+            clone.Nom = actionEdu.Nom;
+            clone.Montant = actionEdu.Montant;
+            clone.TiersSid = actionEdu.TiersSid;
+            clone.Description = actionEdu.Description;
+            clone.CommentaireInterne = actionEdu.CommentaireInterne;
+            clone.CommentairePublic = actionEdu.CommentairePublic;
+            clone.Neutralise = actionEdu.Neutralise;
+            clone.Ordre = actionEdu.Ordre;
+            clone.ActionEduThematiqueId = actionEdu.ActionEduThematiqueId;
+            clone.TokenId = actionEdu.TokenId;
+
+            // enregistre le clone en base
+            base.add(ref clone);
+
+            return clone;
+        }
     }
-}
+}

+ 2 - 0
CD67.FicheCollege.MVC/CD67.FicheCollege.MVC.csproj

@@ -195,6 +195,7 @@
     <Compile Include="Models\ActionEduCollegeViewModel.cs" />
     <Compile Include="Models\ActionEduAxeViewModel.cs" />
     <Compile Include="Models\ActionEduThematiqueViewModel.cs" />
+    <Compile Include="Models\ImportActionEduViewModel.cs" />
     <Compile Include="Models\ImportCollegeViewModel.cs" />
     <Compile Include="Models\TerritoireViewModel.cs" />
     <Compile Include="Models\TypeCollegeViewModel.cs" />
@@ -453,6 +454,7 @@
     <Content Include="Views\ActionsEduCollege\Edit.cshtml" />
     <Content Include="Views\Shared\_AdminLayout.cshtml" />
     <Content Include="Views\Colleges\Import.cshtml" />
+    <Content Include="Views\ActionsEdu\Import.cshtml" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="App_Data\" />

+ 48 - 0
CD67.FicheCollege.MVC/Controllers/ActionsEduController.cs

@@ -154,6 +154,54 @@ namespace CD67.FicheCollege.MVC.Controllers
             return RedirectToAction("Index", new { annee_id = actionEdu.AnneeId });
         }
 
+        public List<ActionEdu> actions_non_importees(int annee_id)
+        {
+            List<ActionEdu> non_importees = new List<ActionEdu>();
+            AnneeFactory fact = new AnneeFactory(db);
+            Annee annee_prec = fact.getById(annee_id - 1);
+            foreach (ActionEdu actionEdu in annee_prec.ActionsEdu)
+            {
+                // Pour chaque college de l'année précédente, on vérifie s'il existe dans l'année en cours en se basant sur le TokenId
+                if (!db.ActionsEdu.Any(a => a.TokenId == actionEdu.TokenId && a.AnneeId == annee_id))
+                {
+                    non_importees.Add(actionEdu.flat());
+                }
+            }
+            return non_importees;
+        }
+
+        // GET: Colleges
+        public ActionResult Import(int? annee_id)
+        {
+            if (annee_id == null)
+            {
+                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
+            }
+
+            ImportActionEduViewModel model = new ImportActionEduViewModel();
+            model.Annee_Id = annee_id.Value;
+            model.set_liste(actions_non_importees(annee_id.Value));
+
+            return View(model);
+        }
+
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Import(int Annee_Id, List<int> actionsEdu_ids, List<bool> selection)
+        {
+            ActionEduFactory fact = new ActionEduFactory(db);
+            for (int i = 0; i < selection.Count; i++)
+            {
+                if (selection[i])
+                {
+                    int actionEdu_id = actionsEdu_ids[i];
+                    ActionEdu actionEdu = fact.getById(actionEdu_id);
+                    fact.clone_to_year(actionEdu, Annee_Id);
+                }
+            }
+            return RedirectToAction("Index", new { annee_id = Annee_Id });
+        }
+
         public ActionResult Up(int? id)
         {
             if (id == null)

+ 0 - 2
CD67.FicheCollege.MVC/Controllers/CollegesController.cs

@@ -246,9 +246,7 @@ namespace CD67.FicheCollege.MVC.Controllers
 
         [HttpPost]
         [ValidateAntiForgeryToken]
-        //public ActionResult Import([Bind(Include = "Annee_id, Annee_Lib,liste")] ImportCollegeViewModel model)
         public ActionResult Import(int Annee_Id, List<int> colleges_ids, List<bool> selection)
-        //public ActionResult Import(ImportCollegeViewModel model)
         {
             CollegeFactory fact = new CollegeFactory(db);
             for (int i = 0; i < selection.Count; i++)

+ 30 - 0
CD67.FicheCollege.MVC/Models/ImportActionEduViewModel.cs

@@ -0,0 +1,30 @@
+using CD67.FicheCollege.Entity;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+
+namespace CD67.FicheCollege.MVC.Models
+{
+
+    public class ImportActionEduViewModel
+    {
+        public int Annee_Id;
+        public string Annee_Lib = "";
+
+        public List<int> actionsEdu_ids = new List<int>();
+        public List<string> actionsEdu_noms = new List<string>();
+        public List<string> actionsEdu_nums = new List<string>();
+        public List<bool> selection = new List<bool>();
+
+        public void set_liste(List<ActionEdu> actionsEdu)
+        {
+            foreach (ActionEdu actionEdu in actionsEdu)
+            {
+                actionsEdu_ids.Add(actionEdu.Id);
+                actionsEdu_noms.Add(actionEdu.Nom);
+                actionsEdu_nums.Add(actionEdu.Numero);
+                selection.Add(false);
+            }
+        }
+
+    }
+}

+ 62 - 0
CD67.FicheCollege.MVC/Views/ActionsEdu/Import.cshtml

@@ -0,0 +1,62 @@
+@using CD67.FicheCollege.MVC.Models
+@model ImportActionEduViewModel
+
+@{
+    ViewBag.Title = "Import";
+    Layout = "~/Views/Shared/_Layout.cshtml";
+}
+
+@*Selection des actions à importer*@
+<div>
+
+    @if(Model.actionsEdu_ids.Count == 0)
+    {
+        <span> Aucune action éducative à importer </span>
+        <div>
+            @Html.ActionLink("Annuler", "Index", new { annee_id = Model.Annee_Id })
+        </div>
+    }
+    else
+    {
+        using (Html.BeginForm())
+        {
+            @Html.AntiForgeryToken()
+            @Html.HiddenFor(model => Model.Annee_Id)
+
+            <table id="table-college-import" class="datatable table">
+                <thead>
+                </thead>
+                <tbody>
+                    @*@foreach (ImportCollegeItem item in Model.liste)*@
+                    @for(int i=0; i < Model.selection.Count; i++)
+                    {
+                        <tr>
+                            <td>
+                                @Html.EditorFor(model => Model.selection[i])
+                                @Html.HiddenFor(model => Model.actionsEdu_ids[i])
+                            </td>
+                            <td>
+                                @Html.DisplayFor(model => Model.actionsEdu_nums[i])
+                            </td>
+                            <td>
+                                @Html.DisplayFor(model => Model.actionsEdu_noms[i])
+                            </td>
+                        </tr>
+                    }
+                </tbody>
+            </table>
+
+            <div class="form-group">
+                <span style="margin-right:20px;">
+                    <input type="submit" value="Importer" class="btn btn-default" />
+                </span>
+                <span>
+                    @Html.ActionLink("Annuler", "Index", new { annee_id = Model.Annee_Id })
+                </span>
+            </div>
+        }
+    }
+
+</div>
+
+

+ 1 - 0
CD67.FicheCollege.MVC/Views/ActionsEdu/Index.cshtml

@@ -13,6 +13,7 @@
     <h2>Les Actions</h2>
     <span>
         <a href="@Url.Action("Create", new { annee_id = Model.Obj.Id })"> <span class="glyphicon glyphicon-plus-sign color1"></span> Nouvelle Action</a>
+        <a href="@Url.Action("Import", new { annee_id = Model.Obj.Id })"> <span class="glyphicon glyphicon-plus-sign color1"></span> Importer depuis @Model.Obj.AnneePrecLib</a>
         <a class="start-trace" href="@Url.Action("Index", "ActionEduAxes")"> <span class="glyphicon glyphicon-cog color1"></span> Axes</a>
         <a class="start-trace" href="@Url.Action("Index", "ActionEduThematiques")"> <span class="glyphicon glyphicon-cog color1"></span> Thématiques</a>
     </span>