Пример #1
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);
            }
        }
Пример #2
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;
            //}
        }