/// <summary>
        /// Inititalise tous les controles de l'IHM pour une nouvelle
        /// saisie de données
        /// </summary>
        ///

        void CleanIHM()
        {
            try
            {
                for (int i = 1; i < NumericUpDown.Value; i++)
                {
                    TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                    TextBox date    = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
                    amounts.Text = string.Empty;
                    date.Text    = string.Empty;
                }
                prgBar.Visibility     = System.Windows.Visibility.Collapsed;
                txtClientName.Text    = string.Empty;
                txtReference.Text     = string.Empty;
                txtClientAdresse.Text = string.Empty;
                //txtDue.Text = string.Empty;

                if (listFacture != null && listFacture.Count > 0)
                {
                    listFacture.Clear();
                }
                if (listeDateMoratoire != null && listeDateMoratoire.Count > 0)
                {
                    listeDateMoratoire.Clear();
                }
                if (listeAmountMoratoire != null && listeAmountMoratoire.Count > 0)
                {
                    listeAmountMoratoire.Clear();
                }



                rdbMonth.IsChecked = true;
                frequenceMoratoire = 1;

                txtSoldeDue.Text = string.Empty;


                if (listFacture != null && listFacture.Count > 0)
                {
                    listFacture.Clear();
                }

                if (lstFactureDeMoratoire != null && lstFactureDeMoratoire.Count > 0)
                {
                    lstFactureDeMoratoire.Clear();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        void FillDetailData(List <ServiceRecouvrement.CsDetailMoratoire> l)
        {
            try
            {
                this.NumericUpDown.Value = l.Count;
                //for (int j = 0; j <= 10; j++)
                //{
                //    j++;
                //    TextBox amounts = AllInOne.FindControl<TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + j);
                //    TextBox dateBox = AllInOne.FindControl<TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + j);
                //    amounts.Text = string.Empty;
                //    dateBox.Text = string.Empty;
                //}


                soldeFactureSelect = l.Sum(t => t.MONTANT).Value;
                int i = 0;
                List <ServiceRecouvrement.CsDetailMoratoire> listeIsCret = new List <ServiceRecouvrement.CsDetailMoratoire>();
                foreach (ServiceRecouvrement.CsDetailMoratoire moratoires in l)
                {
                    // fill les données des échéances
                    i++;

                    TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                    TextBox dateBox = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);

                    if (!string.IsNullOrEmpty(moratoires.CRET))
                    {
                        listeIsCret.Add(moratoires);
                        amounts.Text = moratoires.MONTANT == null ? string.Empty : moratoires.MONTANT.Value.ToString(SessionObject.FormatMontant);
                        dateBox.Text = (moratoires.EXIGIBILITE == null ? string.Empty : moratoires.EXIGIBILITE.Value.ToString("d"));
                    }
                    else
                    {
                        // listeIsCret.Add(moratoires);
                        amounts.Text = moratoires.MONTANT == null ? string.Empty : moratoires.MONTANT.Value.ToString(SessionObject.FormatMontant);
                        dateBox.Text = (moratoires.EXIGIBILITE == null ? string.Empty : moratoires.EXIGIBILITE.Value.ToString("d"));
                    }
                }

                Decimal fraisFacture   = listeIsCret.Sum(p => p.FRAISDERETARD == null ? 0 : p.FRAISDERETARD.Value);
                Decimal montantFacture = listeIsCret.Sum(p => p.MONTANT == null ? 0 : p.MONTANT.Value);
                amountselected  = txtSoldeDue.Text = (montantFacture - fraisFacture).ToString(SessionObject.FormatMontant);
                nombreEchaeance = listeIsCret.Count.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Inititalise tous les controles de l'IHM pour une nouvelle
        /// saisie de données
        /// </summary>
        ///

        void CleanIHM()
        {
            try
            {
                for (int i = 1; i < 13; i++)
                {
                    TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                    TextBox date    = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
                    TextBox charges = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtCharg" + i);
                    //amounts.Text = string.Empty;
                    //date.Text = string.Empty;
                    //charges.Text = string.Empty;
                }
                prgBar.Visibility = System.Windows.Visibility.Collapsed;

                txtClientName.Text    = string.Empty;
                txtClientAdresse.Text = string.Empty;
                //txtReference.Text = string.Empty;
                txtDue.Text         = string.Empty;
                txtNoInstalmnt.Text = string.Empty;

                Cbo_ListeClient.ItemsSource = null;

                if (listeDateMoratoire != null)
                {
                    listeDateMoratoire.Clear();
                }
                if (listeAmountMoratoire != null)
                {
                    listeAmountMoratoire.Clear();
                }

                amountselected = string.Empty;

                if (lientFactureDUcLIENT != null)
                {
                    lientFactureDUcLIENT.Clear();
                }
                if (lientFactureDUcLIENTSplit != null)
                {
                    lientFactureDUcLIENTSplit.Clear();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        void txt_box_Date_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox txtDate1 = (TextBox)sender;

            if (txtDate1.Name == "txtDate1")
            {
                if (!string.IsNullOrEmpty(txtDate1.Text))
                {
                    //int frequence = int.Parse(cboNoInstall.SelectedItem.ToString());
                    int frequence = int.Parse(NumericUpDown.Value.ToString());
                    InitialiserDateMoratoiresParFacture(frequenceMoratoire, frequence, Convert.ToDateTime(txtDate1.Text));
                    int i = 0;
                    foreach (string date in listeDateMoratoire)
                    {
                        i++;
                        TextBox dateBox = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
                        dateBox.Text = date;
                    }
                }
            }
        }
 void ClearAmountTextBox()
 {
     try
     {
         for (int i = 1; i < NumericUpDown.Value + 1; i++)
         {
             TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
             TextBox date    = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
             if (amounts != null)
             {
                 amounts.Text = string.Empty;
             }
             if (date != null)
             {
                 date.Text = string.Empty;
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        void txt_box_Montant_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox txtAmount1 = (TextBox)sender;
            int     numero     = (int)txtAmount1.Tag;

            if (!string.IsNullOrEmpty(txtAmount1.Text))
            {
                txtAmount1.Text = Convert.ToDecimal(txtAmount1.Text).ToString(SessionObject.FormatMontant);
                decimal MontantRestant = 0;
                //int frequence = int.Parse(cboNoInstall.SelectedItem.ToString());
                int frequence = int.Parse(NumericUpDown.Value.ToString());
                IsProposition = true;
                for (int i = 1; i <= 1; i++)
                {
                    TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                    if (!string.IsNullOrEmpty(amounts.Text))
                    {
                        MontantRestant = MontantRestant + Convert.ToDecimal(amounts.Text);
                    }
                }
                CalculerMoratoire(frequence - numero, (soldeFactureSelect - MontantRestant), numero, true);
            }
        }
        private void CalculerMoratoire(int frequence, decimal montant, int position, bool isProposition, DateTime DateDebut)
        {
            try
            {
                InitialiseMontantMoratoires(frequence, montant);
                InitialiserDateMoratoiresParFacture(frequenceMoratoire, frequence, DateDebut);
                if (listeAmountMoratoire.Count > 0 && listeAmountMoratoire != null)
                {
                    int i = position;
                    foreach (string amount in listeAmountMoratoire)
                    {
                        i++;
                        TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                        amounts.Text       = amount;
                        amounts.IsReadOnly = !isProposition;
                    }
                }

                if (listeDateMoratoire.Count > 0 && listeDateMoratoire != null)
                {
                    int i = 0;
                    foreach (string date in listeDateMoratoire)
                    {
                        i++;
                        TextBox dateBox = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
                        dateBox.Text = date;
                        //btnPrintQuote.IsEnabled = !isProposition;
                        dateBox.IsReadOnly = !isProposition;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        List <CsDetailMoratoire> CreateListeFromDetailMoratoire(List <CsDetailMoratoire> lesfactureMoratoire)
        {
            try
            {
                string refem = MoisComptable;
                if (lesfactureMoratoire.Count == 1)
                {
                    refem = lesfactureMoratoire.First().REFEM;
                }

                List <CsDetailMoratoire> moratoires = new List <CsDetailMoratoire>();
                selectedoccurMoratoire = int.Parse(NumericUpDown.Value.ToString());
                for (int i = 0; i < selectedoccurMoratoire + 1; i++)
                {
                    int     inc     = i + 1;
                    TextBox amounts = null;
                    TextBox dates   = null;
                    if (i != 0)
                    {
                        amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                        dates   = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtDate" + i);
                    }
                    if (i == 0)
                    {
                        continue;
                    }

                    CsDetailMoratoire echeanceMoratoire = new CsDetailMoratoire()
                    {
                        COPER               = SessionObject.Enumere.CoperMOR,
                        REFEM               = MoisComptable,
                        CENTRE              = lesfactureMoratoire.First().CENTRE,
                        CLIENT              = lesfactureMoratoire.First().CLIENT,
                        ORDRE               = lesfactureMoratoire.First().ORDRE,
                        CRET                = SessionObject.Cret()[i - 1],
                        DATEVALEUR          = null,
                        DC                  = SessionObject.Enumere.Debit,
                        MONTANT             = Convert.ToDecimal(amounts.Text),
                        EXIGIBILITE         = DateTime.Parse(dates.Text),
                        TOP1                = SessionObject.Enumere.TopMoratoire,
                        MATRICULE           = UserConnecte.matricule,
                        DATECREATION        = DateTime.Now.Date,
                        USERCREATION        = UserConnecte.matricule,
                        FK_IDCLIENT         = lesfactureMoratoire.First().FK_IDCLIENT,
                        FK_IDCENTRE         = lesfactureMoratoire.First().FK_IDCENTRE,
                        FK_IDADMUTILISATEUR = UserConnecte.PK_ID,
                        FK_IDCOPER          = SessionObject.LstDesCopers.FirstOrDefault(t => t.CODE == SessionObject.Enumere.CoperMOR).PK_ID,
                        FK_IDLIBELLETOP     = SessionObject.LstDesLibelleTop.FirstOrDefault(t => t.CODE == SessionObject.Enumere.TopMoratoire).PK_ID,
                        IDMORATOIRE         = lesfactureMoratoire.First().IDMORATOIRE,
                        FK_IDMORATOIRE      = lesfactureMoratoire.First().IDMORATOIRE
                    };
                    CsDetailMoratoire leAncMor = lesfactureMoratoire.FirstOrDefault(t => t.CRET == echeanceMoratoire.CRET);
                    if (leAncMor != null)
                    {
                        echeanceMoratoire.REFEM = leAncMor.REFEM;
                        echeanceMoratoire.NDOC  = leAncMor.NDOC;
                    }

                    moratoires.Add(echeanceMoratoire);
                }

                return(moratoires);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtReference.Text))
                {
                    return;
                }
                if (IsProposition)
                {
                    decimal montantTotal = 0;
                    if (listeAmountMoratoire.Count > 0 && listeAmountMoratoire != null)
                    {
                        int i = 0;
                        foreach (string amount in listeAmountMoratoire)
                        {
                            i++;
                            TextBox amounts = AllInOne.FindControl <TextBox>((UIElement)LayoutRoot, typeof(TextBox), "txtAmount" + i);
                            if (!string.IsNullOrEmpty(amounts.Text))
                            {
                                montantTotal = montantTotal + Convert.ToDecimal(amounts.Text);
                            }
                        }
                    }
                    if (montantTotal > Convert.ToDecimal(soldeFactureSelect))
                    {
                        Message.ShowInformation("Montant des moratoires supperieur au montant de facture selectionnée", "Moratoire");
                        return;
                    }
                }

                btnOk.IsEnabled = false;
                List <CsDetailMoratoire> listeMORA = new List <CsDetailMoratoire>();
                listeMORA.AddRange(CreateListeFromDetailMoratoire(listeDetailsMoratoires));
                this.prgBar.Visibility = System.Windows.Visibility.Visible;

                RecouvrementServiceClient cl = new RecouvrementServiceClient(Utility.ProtocoleFacturation(), Utility.EndPoint("Recouvrement"));
                cl.UpdateDetailMoratoireCompleted += (s, results) =>
                {
                    try
                    {
                        this.prgBar.Visibility = System.Windows.Visibility.Collapsed;

                        if (results.Cancelled || results.Result == false)
                        {
                            Message.Show(results.Error.Message, Galatee.Silverlight.Resources.Langue.wcf_error);
                            return;
                        }
                        btnOk.IsEnabled = true;
                        string reffact = string.Empty;
                        int    i       = 0;
                        foreach (var item in lstFactureDeMoratoire)
                        {
                            if (i == 6)
                            {
                                break;
                            }
                            reffact = reffact + item.REFEM + "-" + item.NDOC + "    ";
                            i++;
                        }
                        ////impression du recu du moratoire
                        //Dictionary<string, string> param = new Dictionary<string, string>();
                        //param.Add("pCentre", lstFactureDeMoratoire.First().CENTRE);
                        //param.Add("pClient", lstFactureDeMoratoire.First().CLIENT);
                        //param.Add("pOrdre", lstFactureDeMoratoire.First().ORDRE);
                        //param.Add("pNombreEcheance", (listeMORA.Count - 1).ToString());
                        //param.Add("pMontantGlobal", soldeFactureSelect.ToString(SessionObject.FormatMontant));
                        //param.Add("pName", txtClientName.Text);
                        //param.Add("pNombreFacture", lstFactureDeMoratoire.Count().ToString());
                        //param.Add("pReferencefacture", reffact);
                        //param.Add("pOperationMor", "MORATOIRE");


                        //param.Add("pExigibilite", listeMORA.First(t => t.EXIGIBILITE != null).DC.ToString());
                        ////Utility.ActionDirectOrientation<ServicePrintings.CsLclient, ServiceRecouvrement.CsLclient>(listeMORA.Where(t => t.EXIGIBILITE != null).ToList(), param, SessionObject.CheminImpression, "moratoire", "Recouvrement", true);
                        //CleanIHM();
                    }
                    catch (Exception ex)
                    {
                        this.prgBar.Visibility = System.Windows.Visibility.Collapsed;
                        Message.ShowError(ex, "Erreur");
                    }
                };
                cl.UpdateDetailMoratoireAsync(listeMORA);
            }
            catch (Exception ex)
            {
                Message.ShowError(ex, "Erreur");
            }
        }