Пример #1
0
 private AVOIR_FINANCIER GetAvoirInfo(int num)
 {
     using (var model = new ExpFinanceEntities())
     {
         return((from a in model.AVOIR_FINANCIER where a.numAvoir == num select a).FirstOrDefault());
     }
 }
Пример #2
0
 private void DetailForm_Load(object sender, EventArgs e)
 {
     using (var model = new ExpFinanceEntities())
     {
         listBoxFactures.DataSource    = GetFactures(model, _numGroupe);
         listBoxFactures.ValueMember   = "NUM_FACT";
         listBoxFactures.DisplayMember = "NUM_FACT";
     }
 }
Пример #3
0
 static bool Authentif()
 {
     try
     {
         using (var model = new ExpFinanceEntities())
         {
             return((from u in model.AF_UTILISATEUR
                     where u.Username == Environment.UserDomainName + @"\" + Environment.UserName
                     select u).Any());
         }
     }
     catch (Exception)
     {
         Messages.Error("La connexion à la base de données a échoué. vérifier votre connexion réseau ou contactez votre administrateur");
         return(false);
     }
 }
Пример #4
0
 List <AF_GET_LIGNES_FACTURE_Result> GetLignes(string ent, int numFact, DateTime date)
 {
     try
     {
         using (var model = new ExpFinanceEntities())
         {
             return((from l in model.AF_GET_LIGNES_FACTURE(ent, numFact, date)
                     select l).ToList());
         }
     }
     catch (Exception ex)
     {
         Messages.Error("Erreur: " + ex.Message);
         ErrorLog.LogError("Erreur lignes factures", ex);
         return(null);
     }
 }
Пример #5
0
 private bool InsertCheques(List <CHEQUE> cheques)
 {
     try
     {
         using (var model = new ExpFinanceEntities())
         {
             cheques.ForEach(c => model.CHEQUE.Add(c));
             model.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         Messages.Error(@"Erreur insertion cheques: " + e.InnerException.Message);
         ErrorLog.LogError(@"Erreur insertion cheques: ", e);
         return(false);
     }
 }
Пример #6
0
        private bool InsertCheques(List <CHEQUE> cheques)
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    //remove old entries

                    // ReSharper disable once AccessToDisposedClosure
                    (from c in model.CHEQUE where c.numAvoir == _numAvoir select c).ForEach(c => model.CHEQUE.Remove(c));

                    //insert new entries
                    cheques.ForEach(c => model.CHEQUE.Add(c));
                    model.SaveChanges();
                }
                return(true);
            }
            catch (Exception ex)
            {
                Messages.Error("Erreur insertion chèques:" + ex.Message);
                ErrorLog.LogError("Erreur insertion chèques", ex);
                return(false);
            }
        }
Пример #7
0
 public AvoirController()
 {
     _model = new ExpFinanceEntities();
 }
Пример #8
0
        private void LoadAvoir(AVOIR_FINANCIER avoir)
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();
                    //Set références
                    _numAvoir            = avoir.numAvoir;
                    textBoxRefAvoir.Text = _numAvoir.ToString("D");
                    _montant             = avoir.montant;
                    _cheque                 = avoir.montantCheque;
                    _creance                = avoir.montantCreance;
                    _observation            = avoir.Observation;
                    textBoxDesc.Text        = avoir.designation;
                    textBoxObservation.Text = avoir.Observation;
                    dateAvoir.Value         = avoir.dateAvoir;

                    //Get cheques
                    _chequesList = (from c in model.CHEQUE where c.numAvoir == avoir.numAvoir select c).ToList();
                    RebindListBoxCheque();
                    TotalCheque();

                    //Set type
                    switch (avoir.typeAvoir)
                    {
                    case 1:
                        _isCheque  = true;
                        _isCreance = false;
                        break;

                    case 2:
                        _isCreance = true;
                        _isCheque  = false;
                        break;

                    case 3:
                        _isCheque = _isCreance = true;
                        break;

                    default:
                        _isCheque = _isCreance = false;
                        break;
                    }


                    //Get Groupe Factures related to the avoir
                    _numGroupe = (from afg in model.AF_AVOIR_GROUPE
                                  where afg.numAvoir == _numAvoir
                                  select afg.IDG).ToList();


                    // ReSharper disable once ConstantNullCoalescingCondition
                    if (_numGroupe.Count == 0)
                    {
                        throw new NullReferenceException("Les groupse factures liés à cette avoir n'existe plus !");
                    }

                    // var factures = detail.GetFactures(_numGroupe);
                    var factures = new List <FACTURE>();

                    //Get factures of each groupe then put them all in one list
                    _numGroupe.ForEach(g =>
                    {
                        var groupeFactures = detail.GetFactures(g);
                        //add each groupe factures to the global list
                        groupeFactures.ForEach(factures.Add);
                    });

                    PopulateFacutresListView(factures);

                    //disable facture item check
                    listViewFactures.ItemCheck -= listViewFactures_ItemCheck;
                    foreach (ListViewItem item in listViewFactures.Items)
                    {
                        item.Checked = true;
                    }



                    //set totals values
                    decimal t, m;
                    TotalFactures(out t, out m);

                    //set reste
                    _reste = _montant - (_creance + _cheque);

                    textBoxMontantAv.Text = $"{_montant:#,##0.00}";
                    textBoxCreance.Text   = $"{_creance:#,##0.00}";

                    //enable facture item check
                    listViewFactures.ItemCheck += listViewFactures_ItemCheck;

                    CheckType();
                }
            }
            catch (Exception e)
            {
                Messages.Error("Error Loading avoir :" + e.Message);
                ErrorLog.LogError("Error Loading avoir", e);
                Close();
            }
        }
Пример #9
0
        private bool InsertAvoir(AVOIR_FINANCIER avoir)
        {
            try
            {
                decimal mntAv;
                decimal.TryParse(textBoxMontantAv.Text, out mntAv);

                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();


                    //Set référence (if new avoir get the new num according to the year)
                    if (_avoir == null)
                    {
                        _numAvoir = detail.GetLastAvoirId(dateAvoir.Value);
                    }

                    avoir.numAvoir       = _numAvoir;
                    avoir.montant        = mntAv;
                    avoir.dateAvoir      = dateAvoir.Value.Date;
                    avoir.designation    = _designation;
                    avoir.montantCheque  = _cheque;
                    avoir.montantCreance = _creance;
                    avoir.Observation    = _observation;
                    avoir.typeAvoir      = (byte)_typeAvoir;
                    model.AVOIR_FINANCIER.AddOrUpdate(avoir);


                    var op = new AF_OPS_LOG
                    {
                        instant   = DateTime.Now,
                        username  = Environment.UserName,
                        numavoir  = _numAvoir,
                        Operation = "Avoir inséré"
                    };

                    model.AF_OPS_LOG.Add(op);


                    //insert the related groups
                    foreach (int g in _numGroupe)
                    {
                        var avoirGroupe = new AF_AVOIR_GROUPE
                        {
                            numAvoir = avoir.numAvoir,
                            IDG      = g,
                            Libre    = 0
                        };

                        model.AF_AVOIR_GROUPE.Add(avoirGroupe);
                    }

                    //Insert in the etat table
                    foreach (int g in _numGroupe)
                    {
                        var etat = new AF_ETAT_AVOIR
                        {
                            numDossier = detail.GetGroupeInfo(g).NDOSSIER,
                            numAvoir   = avoir.numAvoir,
                            IDG        = g,
                            Etat       = 3,
                            dateHeure  = DateTime.Now
                        };

                        model.AF_ETAT_AVOIR.Add(etat);
                    }


                    model.SaveChanges();
                    return(true);
                }
            }

            catch (Exception ex)
            {
                Messages.Error("Erreur insertion avoir:" + ex.Message);
                ErrorLog.LogError("Erreur insertion avoir", ex);
                return(false);
            }
        }
Пример #10
0
        private bool InsertAvoir(AVOIR_FINANCIER avoir)
        {
            try
            {
                decimal mntAv;
                decimal.TryParse(textBoxMontantAv.Text, out mntAv);

                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();

                    //Set référence
                    _numAvoir            = detail.GetLastAvoirId(dateAvoir.Value);
                    textBoxRefAvoir.Text = _numAvoir.ToString("D");
                    int codePromo;
                    int.TryParse(comboBoxPromotions.SelectedValue?.ToString(), out codePromo);

                    avoir.numAvoir          = _numAvoir;
                    avoir.montant           = mntAv;
                    avoir.dateAvoir         = dateAvoir.Value.Date;
                    avoir.designation       = textBoxDesc.Text;
                    avoir.montantCheque     = _cheque;
                    avoir.montantCreance    = _creance;
                    avoir.Observation       = _observation;
                    avoir.typeAvoir         = (byte)_typeAvoir;
                    avoir.numAvoirComptable = codePromo;

                    var op = new AF_OPS_LOG
                    {
                        instant   = DateTime.Now,
                        username  = Environment.UserName,
                        numavoir  = _numAvoir,
                        Operation = "Avoir inséré"
                    };

                    model.AF_OPS_LOG.Add(op);
                    //int nac;
                    //if (int.TryParse(textBoxNAC.Text, out nac))
                    //{
                    //    avoir.numAvoirComptable = nac;
                    //}

                    var libre = _isFacture ? (byte)1 : (byte)2;

                    //insert the related group
                    var avoirGroupe = new AF_AVOIR_GROUPE
                    {
                        numAvoir = avoir.numAvoir,
                        IDG      = _numGroupe,
                        Libre    = libre
                    };

                    model.AF_AVOIR_GROUPE.Add(avoirGroupe);


                    //Insert in the etat table
                    var etat = new AF_ETAT_AVOIR
                    {
                        numDossier = _nDossier,
                        numAvoir   = avoir.numAvoir,
                        IDG        = _numGroupe,
                        Etat       = 3,
                        dateHeure  = DateTime.Now
                    };

                    model.AF_ETAT_AVOIR.Add(etat);

                    model.AVOIR_FINANCIER.Add(avoir);
                    model.SaveChanges();
                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error(@"Erreur insertion avoir financier: ");
                ErrorLog.LogError(@"Erreur insertion avoir financier: ", e);
                return(false);
            }
        }
Пример #11
0
        private bool InsertAnte()
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();

                    var tempdata = (from temp in model.AF_TEMP_DATA_ANT
                                    where temp.entite == _entite
                                    select temp);


                    foreach (var temp in tempdata)
                    {
                        //EXERCICE ANTERIEUR
                        ErrorLog.LogMessage($"treating avoir{temp.numAvoir}");
                        var year     = temp.Exercice;
                        var factures = temp.factures.Split('-').ToList();


                        //Get the liste of facture
                        var liste = detail.GetListeFacturesClientAllYears(factures, year.Value.Year);

                        if (liste.Count == 0 || liste.All(f => f == null))
                        {
                            ErrorLog.LogMessage($"No factures in avoir{temp.numAvoir}");
                        }
                        //Creating Groupe Details
                        //var entite = liste.Where(f=>f!=null).Select(f => f.entite).First();
                        //var codeClient = liste.Where(f => f != null).Select(f => f.code_clien).First();
                        decimal?mntHt = 0, mntRz = 0, mntTtc = 0, mntTva = 0, marge = 0;

                        if (liste.Count != 0 || liste.All(f => f != null))
                        {
                            //calculating totales in factures
                            liste.ForEach(f =>
                            {
                                mntHt  += f?.total_HT; //this is HTR !!
                                mntRz  += f?.T_remise;
                                mntTtc += f?.total_TTC;
                                mntTva += f?.total_TVA;
                                marge  += f?.Marge;
                            });
                        }

                        var newGroupeCvn = new AF_TEMP_GROUPE_FACTURE
                        {
                            ID_GROUPE_FACT = temp.idgroupe ?? 0,
                            DATE_CADEAU    = year,
                            DATE_DOSSIER   = DateTime.Today,
                            ENTITE         = _entite,
                            CODE_CLIENT    = temp.codeClient,
                            //MNT_HT = temp.CA, //this info is given by entite (we switch to avoid to insert les avoirs clients)
                            MNT_HT         = mntHt,
                            MNT_RZ         = mntRz,
                            MNT_TVA        = mntTva,
                            MNT_TTC        = mntTtc,
                            MONTANT_CHEQUE = mntHt,//this by the db
                            MARGE          = marge,
                            ETAT           = 20,
                        };

                        model.AF_TEMP_GROUPE_FACTURE.Add(newGroupeCvn);


                        if (liste.Count != 0 || liste.All(f => f != null))
                        {
                            liste.ForEach(facture =>
                            {
                                if (facture != null)
                                {
                                    var newFacture = new AF_TEMP_FACTURE
                                    {
                                        NUM_FACT       = facture.num_trans,
                                        CODE_CLIENT    = facture.code_clien,
                                        DATE_FACT      = facture.date_trans ?? default(DateTime),
                                        ENTITE         = facture.entite,
                                        MARGE          = facture.Marge,
                                        MNT_HT_RZ      = facture.total_HT - facture.T_remise,
                                        MNT_TTC        = facture.total_TTC,
                                        MNT_RZ         = facture.T_remise,
                                        MNT_TVA        = facture.total_TVA,
                                        ID_GROUPE_FACT = temp.idgroupe ?? 0,
                                    };

                                    model.AF_TEMP_FACTURE.Add(newFacture);
                                }
                            });
                        }

                        //model.SaveChanges();
                        //_numGroupe = newGroupeCvn.ID_GROUPE_FACT;



                        //Insert AVOIR


                        _numAvoir = temp.numAvoir;


                        var avoir = new AF_TEMP_AVOIR_FINANCIER
                        {
                            numAvoir          = _numAvoir,
                            montant           = temp.Montant ?? 0,
                            dateAvoir         = temp.dateAvoir ?? DateTime.Now,
                            designation       = "REMISE FINANCIERE",
                            montantCheque     = temp.cheque ?? 0,
                            montantCreance    = temp.creance ?? 0,
                            Observation       = "",
                            typeAvoir         = byte.Parse(temp.typeAvoir),
                            numAvoirComptable = 20,
                        };


                        var libre = (byte)2;

                        //insert the related group
                        var avoirGroupe = new AF_TEMP_AVOIR_GROUPE
                        {
                            numAvoir = avoir.numAvoir,
                            IDG      = temp.idgroupe ?? 0,
                            Libre    = libre
                        };

                        model.AF_TEMP_AVOIR_GROUPE.Add(avoirGroupe);


                        //Insert in the etat table
                        var etat = new AF_TEMP_ETAT_AVOIR
                        {
                            numDossier = temp.refAvoir,
                            numAvoir   = avoir.numAvoir,
                            IDG        = temp.idgroupe ?? 0,
                            Etat       = 3,
                            dateHeure  = DateTime.Today
                        };

                        model.AF_TEMP_ETAT_AVOIR.Add(etat);

                        model.AF_TEMP_AVOIR_FINANCIER.Add(avoir);

                        //model.SaveChanges();
                    }
                    model.SaveChanges();

                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error(@"Erreur insertion factures: " + e.Message);
                ErrorLog.LogError(@"Erreur insertion factures: ", e);
                return(false);
            }
        }
Пример #12
0
        private bool InsertFacturesEf(List <string> listeCode)
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();

                    //Get the liste of facture
                    var liste = detail.GetListeFacturesClientAll(listeCode);



                    //Creating Groupe Details
                    var     entite = liste.Select(f => f.entite).FirstOrDefault();
                    var     codeClient = liste.Select(f => f.code_clien).FirstOrDefault();
                    decimal?mntHt = 0, mntRz = 0, mntTtc = 0, mntTva = 0, marge = 0;

                    //calculating totales in factures
                    liste.ForEach(f =>
                    {
                        mntHt  += f.total_HT; //this is HTR !!
                        mntRz  += f.T_remise;
                        mntTtc += f.total_TTC;
                        mntTva += f.total_TVA;
                        marge  += f.Marge;
                    });


                    //Escompte Financier without factures
                    if (liste.Count == 0)
                    {
                        codeClient = comboBoxClient.SelectedValue.ToString();
                        entite     = Settings.Default.entite;
                    }

                    var margeRest = marge - _montant;

                    var etat = 5;

                    var newGroupe = new AF_EF_GROUPE_FACTURE
                    {
                        ENTITE       = entite,
                        CODE_CLIENT  = codeClient,
                        MNT_HT       = mntHt,
                        MNT_RZ       = mntRz,
                        MNT_TVA      = mntTva,
                        MNT_TTC      = mntTtc,
                        MARGE        = marge,
                        MARGE_REST   = margeRest,
                        ETAT         = etat,
                        DATE_DOSSIER = DateTime.Today,
                        ID_LIGNE     = _codePromo, //we store promotion here
                        NB_CADEAU    = _codePalier //we store wich palier here
                    };

                    model.AF_EF_GROUPE_FACTURE.Add(newGroupe);
                    model.SaveChanges();
                    _numGroupe = newGroupe.ID_GROUPE_FACT;

                    liste.ForEach(facture =>
                    {
                        var newFacture = new AF_EF_FACTURE
                        {
                            NUM_FACT       = facture.num_trans,
                            CODE_CLIENT    = facture.code_clien,
                            DATE_FACT      = facture.date_trans ?? default(DateTime),
                            ENTITE         = facture.entite,
                            MARGE          = facture.Marge,
                            MNT_HT_RZ      = facture.total_HT - facture.T_remise,
                            MNT_TTC        = facture.total_TTC,
                            MNT_RZ         = facture.T_remise,
                            MNT_TVA        = facture.total_TVA,
                            ID_GROUPE_FACT = newGroupe.ID_GROUPE_FACT
                        };

                        model.AF_EF_FACTURE.Add(newFacture);

                        //Insert lines if there is


                        if (_lignesList.Count > 0 && _lignesList.ContainsKey((int)newFacture.NUM_FACT))
                        {
                            var lignes = _lignesList[(int)newFacture.NUM_FACT];

                            lignes.ForEach(ligne =>
                            {
                                var newLigne = new AF_EF_LIGNE_FACTURE
                                {
                                    NUM_FACT  = newFacture.NUM_FACT,
                                    DATE_FACT = newFacture.DATE_FACT,
                                    ENTITE    = newFacture.ENTITE,
                                    NUM_LIGNE = ligne
                                };

                                model.AF_EF_LIGNE_FACTURE.Add(newLigne);
                            });
                        }
                    });

                    model.SaveChanges();
                    _numGroupe = newGroupe.ID_GROUPE_FACT;
                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error($@"Erreur insertion factures: \n {e.Message}
                                 \n {e.InnerException?.Message}
                                   \n {e.InnerException?.InnerException?.Message}");

                _numGroupe = 0;
                ErrorLog.LogError("Erreur insertion factures", e);
                return(false);
            }
        }
Пример #13
0
 public BulkPrintingViewModel()
 {
     _model = new ExpFinanceEntities();
 }
Пример #14
0
 public static List <FACTURE> GetFactures(ExpFinanceEntities model, int id)
 {
     return((from f in model.FACTURE where f.ID_GROUPE_FACT == id
             select f).Distinct().ToList());
 }
Пример #15
0
 public DetailsDossier()
 {
     _model = new ExpFinanceEntities();
 }
Пример #16
0
        private bool InsertFactures(List <string> listeCode)
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    //EXERCICE ANTERIEUR
                    if (!_isFacture)
                    {
                        var ent = Settings.Default.entite;
                        var cli = comboBoxClient.SelectedValue.ToString();
                        //store the date just to keep the date
                        var year = new DateTime((int)dateExercice.Value, 1, 1);

                        var newGroupeCvn = new AF_GROUPE_FACTURE
                        {
                            ENTITE       = ent,
                            CODE_CLIENT  = cli,
                            MNT_HT       = _total,
                            DATE_CADEAU  = year,
                            DATE_DOSSIER = DateTime.Today,
                        };

                        model.AF_GROUPE_FACTURE.Add(newGroupeCvn);
                        model.SaveChanges();
                        _numGroupe = newGroupeCvn.ID_GROUPE_FACT;
                        return(true);
                    }


                    var detail = new DetailsDossier();

                    //Get the liste of facture
                    var liste = detail.GetListeFacturesClient(listeCode);

                    //Creating Groupe Details
                    var     entite = liste.Select(f => f.entite).First();
                    var     codeClient = liste.Select(f => f.code_clien).First();
                    decimal?mntHt = 0, mntRz = 0, mntTtc = 0, mntTva = 0, marge = 0;

                    //calculating totales in factures
                    liste.ForEach(f =>
                    {
                        mntHt  += f.total_HT; //this is HTR !!
                        mntRz  += f.T_remise;
                        mntTtc += f.total_TTC;
                        mntTva += f.total_TVA;
                        marge  += f.Marge;
                    });

                    var margeRest = marge - _montant;

                    var etat = 5;

                    var newGroupe = new AF_GROUPE_FACTURE
                    {
                        ENTITE       = entite,
                        CODE_CLIENT  = codeClient,
                        MNT_HT       = mntHt,
                        MNT_RZ       = mntRz, MNT_TVA = mntTva,
                        MNT_TTC      = mntTtc,
                        MARGE        = marge,
                        MARGE_REST   = margeRest,
                        ETAT         = etat,
                        DATE_DOSSIER = DateTime.Today,
                    };

                    model.AF_GROUPE_FACTURE.Add(newGroupe);
                    model.SaveChanges();
                    _numGroupe = newGroupe.ID_GROUPE_FACT;

                    liste.ForEach(facture =>
                    {
                        var newFacture = new AF_FACTURE
                        {
                            NUM_FACT       = facture.num_trans,
                            CODE_CLIENT    = facture.code_clien,
                            DATE_FACT      = facture.date_trans ?? default(DateTime),
                            ENTITE         = facture.entite,
                            MARGE          = facture.Marge,
                            MNT_HT_RZ      = facture.total_HT - facture.T_remise,
                            MNT_TTC        = facture.total_TTC,
                            MNT_RZ         = facture.T_remise,
                            MNT_TVA        = facture.total_TVA,
                            ID_GROUPE_FACT = newGroupe.ID_GROUPE_FACT
                        };

                        model.AF_FACTURE.Add(newFacture);

                        //Insert lines if there is


                        if (_lignesList.Count > 0 && _lignesList.ContainsKey((int)newFacture.NUM_FACT))
                        {
                            var lignes = _lignesList[(int)newFacture.NUM_FACT];

                            lignes.ForEach(ligne =>
                            {
                                var newLigne = new AF_LIGNE_FACTURE
                                {
                                    NUM_FACT  = newFacture.NUM_FACT,
                                    DATE_FACT = newFacture.DATE_FACT,
                                    ENTITE    = newFacture.ENTITE,
                                    NUM_LIGNE = ligne
                                };

                                model.AF_LIGNE_FACTURE.Add(newLigne);
                            });
                        }
                    });

                    model.SaveChanges();
                    _numGroupe = newGroupe.ID_GROUPE_FACT;
                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error(@"Erreur insertion factures: " + e.Message);
                ErrorLog.LogError(@"Erreur insertion factures: ", e);
                _numGroupe = 0;
                return(false);
            }
        }
Пример #17
0
        private bool SetEtat(IEnumerable <int> idg, byte state)
        {
            try
            {
                if (idg == null)
                {
                    return(false);
                }
                idg = idg.ToList();
                if (!idg.Any())
                {
                    return(false);
                }

                using (var model = new ExpFinanceEntities())
                {
                    //If reject treat only one entry
                    if (state == 1)
                    {
                        var g = idg.LastOrDefault();

                        var oldState = (from e in model.AF_ETAT_AVOIR
                                        where e.IDG == g
                                        orderby e.dateHeure descending
                                        select e.Etat).FirstOrDefault();

                        //return if already rejected or if validated
                        if (oldState == state || oldState == 3)
                        {
                            Cursor.Current = Cursors.Default;
                            return(false);
                        }

                        var obs = Prompt.ShowDialog("Motif de rejet", "Observation");

                        var etat = new AF_ETAT_AVOIR
                        {
                            IDG         = g,
                            Etat        = state,
                            dateHeure   = DateTime.Now,
                            observation = obs
                        };

                        var op = new AF_OPS_LOG
                        {
                            instant   = DateTime.Now,
                            Operation = $"Etat mis à {state}",
                            username  = Environment.UserName,
                            idg       = g
                        };

                        model.AF_OPS_LOG.Add(op);
                        model.AF_ETAT_AVOIR.Add(etat);
                        model.SaveChanges();
                        return(true);
                    }

                    Cursor.Current = Cursors.WaitCursor;
                    foreach (int g in idg)
                    {
                        //Check the last state
                        var oldState = (from e in model.AF_ETAT_AVOIR
                                        where e.IDG == g
                                        orderby e.dateHeure descending
                                        select e.Etat).FirstOrDefault();

                        if (oldState == state)
                        {
                            continue;
                        }
                        //return if validated
                        if (oldState == 3)
                        {
                            Cursor.Current = Cursors.Default;
                            return(false);
                        }

                        var etat = new AF_ETAT_AVOIR
                        {
                            IDG       = g,
                            Etat      = state,
                            dateHeure = DateTime.Now
                        };

                        var op = new AF_OPS_LOG
                        {
                            instant   = DateTime.Now,
                            Operation = $"Etat mis à {state}",
                            username  = Environment.UserName,
                            idg       = g
                        };

                        model.AF_OPS_LOG.Add(op);
                        model.AF_ETAT_AVOIR.Add(etat);
                    }

                    model.SaveChanges();
                    Cursor.Current = Cursors.Default;
                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error("Error edit etat: " + e.Message);
                return(false);
            }
        }
Пример #18
0
        private bool InsertAvoir2016()
        {
            try
            {
                using (var model = new ExpFinanceEntities())
                {
                    var detail = new DetailsDossier();

                    var tempdata = (from temp in model.AF_TEMP_DATA
                                    where temp.entite == _entite
                                    select temp);

                    foreach (var temp in tempdata)
                    {
                        //EXERCICE 2016
                        var factures = temp.factures.Split('-').ToList();
                        ErrorLog.LogMessage($"treating avoir{temp.numAvoir}");


                        //Get the liste of facture
                        var liste = detail.GetListeFacturesClientAll(factures);
                        if (liste.Count == 0 || liste.All(f => f == null))
                        {
                            ErrorLog.LogMessage($"No factures in avoir{temp.numAvoir}");
                        }
                        //Creating Groupe Details
                        //var entite = liste.Where(f => f != null).Select(f => f.entite).First();
                        //var codeClient = liste.Where(f => f != null).Select(f => f.code_clien).First();
                        decimal?mntHt = 0, mntRz = 0, mntTtc = 0, mntTva = 0, marge = 0;

                        //calculating totales in factures
                        if (liste.Count != 0 || liste.All(f => f != null))
                        {
                            //calculating totales in factures
                            liste.ForEach(f =>
                            {
                                mntHt  += f?.total_HT; //this is HTR !!
                                mntRz  += f?.T_remise;
                                mntTtc += f?.total_TTC;
                                mntTva += f?.total_TVA;
                                marge  += f?.Marge;
                            });
                        }


                        var newGroupeCvn = new AF_TEMP_GROUPE_FACTURE
                        {
                            ID_GROUPE_FACT = temp.idgroupe ?? 0,
                            DATE_DOSSIER   = DateTime.Today,
                            ENTITE         = _entite,
                            CODE_CLIENT    = temp.codeClient,
                            MNT_HT         = mntHt,
                            MNT_RZ         = mntRz,
                            MNT_TVA        = mntTva,
                            MNT_TTC        = mntTtc,
                            MARGE          = marge,
                            ETAT           = 10,
                        };

                        model.AF_TEMP_GROUPE_FACTURE.Add(newGroupeCvn);

                        if (liste.Count != 0 || liste.All(f => f != null))
                        {
                            liste.ForEach(facture =>
                            {
                                if (facture != null)
                                {
                                    var newFacture = new AF_TEMP_FACTURE
                                    {
                                        NUM_FACT       = facture.num_trans,
                                        CODE_CLIENT    = facture.code_clien,
                                        DATE_FACT      = facture.date_trans ?? default(DateTime),
                                        ENTITE         = facture.entite,
                                        MARGE          = facture.Marge,
                                        MNT_HT_RZ      = facture.total_HT - facture.T_remise,
                                        MNT_TTC        = facture.total_TTC,
                                        MNT_RZ         = facture.T_remise,
                                        MNT_TVA        = facture.total_TVA,
                                        ID_GROUPE_FACT = temp.idgroupe ?? 0,
                                    };

                                    model.AF_TEMP_FACTURE.Add(newFacture);
                                }
                            });
                        }

                        //Insert AVOIR


                        _numAvoir = temp.numAvoir;


                        var avoir = new AF_TEMP_AVOIR_FINANCIER
                        {
                            numAvoir          = _numAvoir,
                            montant           = temp.Montant ?? 0,
                            dateAvoir         = temp.dateAvoir,
                            designation       = "REMISE FINANCIERE",
                            montantCheque     = temp.cheque ?? 0,
                            montantCreance    = temp.creance ?? 0,
                            Observation       = "",
                            typeAvoir         = byte.Parse(temp.typeAvoir),
                            numAvoirComptable = 10,
                        };


                        var libre = (byte)1;

                        //insert the related group
                        var avoirGroupe = new AF_TEMP_AVOIR_GROUPE
                        {
                            numAvoir = avoir.numAvoir,
                            IDG      = temp.idgroupe ?? 0,
                            Libre    = libre
                        };

                        model.AF_TEMP_AVOIR_GROUPE.Add(avoirGroupe);


                        //Insert in the etat table
                        var etat = new AF_TEMP_ETAT_AVOIR
                        {
                            numDossier = temp.refAvoir,
                            numAvoir   = avoir.numAvoir,
                            IDG        = temp.idgroupe ?? 0,
                            Etat       = 3,
                            dateHeure  = DateTime.Today
                        };

                        model.AF_TEMP_ETAT_AVOIR.Add(etat);

                        model.AF_TEMP_AVOIR_FINANCIER.Add(avoir);
                    }

                    model.SaveChanges();
                    return(true);
                }
            }
            catch (Exception e)
            {
                Messages.Error(@"Erreur insertion factures: " + e.Message);
                ErrorLog.LogError(@"Erreur insertion factures: ", e);
                return(false);
            }

            //catch (DbEntityValidationException e)
            //{
            //    foreach (var eve in e.EntityValidationErrors)
            //    {
            //        ErrorLog.LogError($"Entity of type \"{ eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:");
            //        foreach (var ve in eve.ValidationErrors)
            //        {
            //            ErrorLog.LogError($"- Property: \"{ ve.PropertyName}\", Error: \"{ve.ErrorMessage}\"");
            //        }
            //    }
            //    throw;
            //}
        }