public void PersistPerformancesOpti()
        {
            foreach (COMMANDE com in processedOrders)
            {
                _context.COMMANDEs.Where(c => c.COMMANDE_ID == com.COMMANDE_ID).FirstOrDefault().COMMANDE_DATE_FABRICATION = com.COMMANDE_DATE_FABRICATION;
            }

            foreach (PERF_COMMANDES c in savedPerformances)
            {
                if (_context.PERF_COMMANDES.Any(p => p.COMMANDE_ID == c.COMMANDE_ID))
                {
                    PERF_COMMANDES copy = _context.PERF_COMMANDES.Where(p => p.COMMANDE_ID == c.COMMANDE_ID).FirstOrDefault();
                    copy.TEMPS_FABRICATION_LOT           = c.TEMPS_FABRICATION_LOT;
                    copy.PERF_MACHINE_FABRICATION_ID     = c.PERF_MACHINE_FABRICATION_ID;
                    copy.PERF_MACHINE_CONDITIONNEMENT_ID = -1;
                }
                else
                {
                    _context.PERF_COMMANDES.Add(c);
                }
            }

            _context.SaveChanges();
            Console.WriteLine("Fabrication Performances of Machine No " + _id + " SAVED IN DB !");
        }
        /*
         * void ProcessOrder()
         * {
         *  if(!inUse)
         *  {
         *      inUse = true;
         *      if(cmd.COMMANDE_QUANTITE == 0) { return; }
         *
         *      using (var context = new PalaisDuBonbonEntities())
         *      {
         *          DateTime entryDate = (DateTime)context.COMMANDEs.Where(c => c.COMMANDE_ID == cmd.COMMANDE_ID).FirstOrDefault().COMMANDE_DATE_FABRICATION;
         *          end_date_last_order = entryDate;
         *          string bonbon_type = context.BONBONs.Where(b => b.BONBON_ID == cmd.BONBON_ID).FirstOrDefault().BONBON_TYPE;
         *
         *          if(!(bonbon_type == lastUsedType))
         *          {
         *              lastUsedType = bonbon_type;
         *              end_date_last_order = end_date_last_order.AddMinutes(this.changement_outil);
         *          }
         *
         *          int qte_lots = (int)cmd.COMMANDE_QUANTITE;
         *          double tps_traitement = (double)qte_lots / ((double)this.cadence / 60.0f);
         *          end_date_last_order = end_date_last_order.AddMinutes(tps_traitement);
         *
         *          PERF_COMMANDES pc = context.PERF_COMMANDES.Where(p => p.COMMANDE_ID == cmd.COMMANDE_ID).FirstOrDefault();
         *          pc.TEMPS_CONDITIONNEMENT = (decimal)tps_traitement;
         *          pc.PERF_MACHINE_CONDITIONNEMENT_ID = _id;
         *          savedPerf.Add(pc);
         *
         *          cmd.COMMANDE_DATE_CONDITIONNEMENT = end_date_last_order;
         *          processedCmds.Add(cmd);
         *      }
         *      inUse = false;
         *  }
         * }*/

        void ProcessOrder()
        {
            if (!inUse)
            {
                inUse = true;
                if (cmd.COMMANDE_QUANTITE == 0)
                {
                    return;
                }
                DateTime startProcessDate = end_date_last_order;
                using (var context = new PalaisDuBonbonEntities())
                {
                    DateTime entryDate = (DateTime)context.COMMANDEs.Where(c => c.COMMANDE_ID == cmd.COMMANDE_ID).FirstOrDefault().COMMANDE_DATE_FABRICATION;
                    end_date_last_order = entryDate;
                    string bonbon_type = context.BONBONs.Where(b => b.BONBON_ID == cmd.BONBON_ID).FirstOrDefault().BONBON_TYPE;

                    if (!(bonbon_type == lastUsedType))
                    {
                        lastUsedType        = bonbon_type;
                        end_date_last_order = end_date_last_order.AddMinutes(this.changement_outil);
                    }

                    int    qte_lots       = (int)cmd.COMMANDE_QUANTITE;
                    double tps_traitement = (double)qte_lots / ((double)this.cadence / 60.0f);
                    end_date_last_order = end_date_last_order.AddMinutes(tps_traitement);

                    PERF_COMMANDES pc = context.PERF_COMMANDES.Where(p => p.COMMANDE_ID == cmd.COMMANDE_ID).FirstOrDefault();
                    pc.TEMPS_CONDITIONNEMENT           = (decimal)end_date_last_order.Subtract(startProcessDate).TotalMinutes;
                    pc.PERF_MACHINE_CONDITIONNEMENT_ID = _id;
                    savedPerf.Add(pc);

                    cmd.COMMANDE_DATE_CONDITIONNEMENT = end_date_last_order;
                    processedCmds.Add(cmd);
                }
                inUse = false;
            }
        }
        void ProcessOrder()
        {
            if (!inUse)
            {
                inUse = true;
                if (cmd.COMMANDE_QUANTITE == 0)
                {
                    return;
                }

                int      qte_cond;
                DateTime startProcessDate = end_date_last_order;
                using (var context = new PalaisDuBonbonEntities())
                {
                    qte_cond = (int)context.CONDITIONNEMENTs.Where(c => c.CONDITIONNEMENT_ID == cmd.CONDITIONNEMENT_ID).FirstOrDefault().CONDITIONNEMENT_QUANTITE_MAX;

                    int quantite_bonbon = (int)(cmd.COMMANDE_QUANTITE * qte_cond);

                    string bonbon_type     = context.BONBONs.Where(c => c.BONBON_ID == cmd.BONBON_ID).FirstOrDefault().BONBON_TYPE;
                    string bonbon_variante = context.BONBONs.Where(c => c.BONBON_ID == cmd.BONBON_ID).FirstOrDefault().VARIANTE.VARIANTE_NOM;
                    int    varianteIndex   = variantes.IndexOf(bonbon_variante);

                    if (last_used_candy_type != bonbon_type)
                    {
                        last_used_candy_type = bonbon_type;
                        end_date_last_order  = end_date_last_order.AddMinutes(this.changements_outil[varianteIndex]);
                    }

                    double tps_traitement = (float)quantite_bonbon / (float)(cadences[varianteIndex] / 60); // Tps en minutes
                    end_date_last_order = end_date_last_order.AddMinutes(tps_traitement);

                    cmd.COMMANDE_DATE_FABRICATION = end_date_last_order;

                    processedOrders.Add(cmd);

                    PERF_COMMANDES perfCmd = new PERF_COMMANDES();
                    perfCmd.COMMANDE_ID                 = cmd.COMMANDE_ID;
                    perfCmd.TEMPS_FABRICATION_LOT       = (decimal)end_date_last_order.Subtract(startProcessDate).TotalMinutes;
                    perfCmd.BONBON_FABRIQUE_COUNT       = quantite_bonbon;
                    perfCmd.PERF_MACHINE_FABRICATION_ID = _id;
                    savedPerformances.Add(perfCmd);

                    /*******************************
                     *            DEBUG
                     ********************************/
/*
 *                  dynamic jsonCmd = new System.Dynamic.ExpandoObject();
 *                  jsonCmd.machine = _id;
 *                  jsonCmd.date = cmd.COMMANDE_DATE_FABRICATION;
 *                  jsonCmd.id = cmd.COMMANDE_ID;
 *                  jsonCmd.numCmd = cmd.COMMANDE_NUM_COMMANDE;
 *                  jsonCmd.qte_bonbon = quantite_bonbon;
 *                  jsonCmd.temps_fab_lot = tps_traitement;
 *
 *                  string jsonData = JsonConvert.SerializeObject(jsonCmd);
 *                  Utils.GenerateFabricationData_Test(jsonData);
 */
                    /*******************************
                     *            END DEBUG
                     ********************************/
                }
                inUse = false;
            }

            //Console.ReadLine();
        }