Ejemplo n.º 1
0
        /*
         * Generates text from database
         */
        public ReportViewModel GeneratePackets(TextGeneratorViewModel TGViewModel)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                ReportViewModel report = new ReportViewModel();
                string          key    = GenerateKey();
                report.Message = key;

                for (int i = 0; i < TGViewModel.StoreList.Count(); i++)
                {
                    if (TGViewModel.StoreList[i].Cb || TGViewModel.IncludeAllStores)
                    {
                        // Data creation
                        MemoryStream  ms             = new MemoryStream();
                        StreamWriter  sb             = new StreamWriter(ms);
                        TextGenerator tg             = new TextGenerator();
                        string        Store_No       = TGViewModel.StoreList[i].value;
                        string        Store_Name     = TGViewModel.StoreList[i].text;
                        string        DateAndTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                        DateTime      DateFrom       = DateTime.Parse(TGViewModel.DateFrom);
                        DateTime      DateTo         = DateTime.Parse(TGViewModel.DateTo);

                        // Append first line
                        sb.Write("01," + Store_No + "," + Store_Name + "," + DateAndTimeNow + "," + TGViewModel.PromoTitle + "\n");

                        if (TGViewModel.IncludeMIM || TGViewModel.IncludeAll)
                        {
                            List <CSHMIMP0> MIRows = GetMenutems(Store_No, DateFrom, DateTo);
                            foreach (CSHMIMP0 mi in MIRows)
                            {
                                sb.Write(tg.GenerateMenuItemMasterText(mi));
                                mi.STATUS = "0";
                            }
                        }
                        if (TGViewModel.IncludeRIM || TGViewModel.IncludeAll)
                        {
                            List <INVRIMP0> RIRows = GetRawItems(Store_No, DateFrom, DateTo);
                            foreach (INVRIMP0 ri in RIRows)
                            {
                                sb.Write(tg.GenerateRawItemMasterText(ri));
                                ri.STATUS = "0";
                            }
                        }
                        if (TGViewModel.IncludeREC || TGViewModel.IncludeAll)
                        {
                            List <INVRIRP0> RERows = GetRecipes(Store_No, DateFrom, DateTo);
                            foreach (INVRIRP0 re in RERows)
                            {
                                sb.Write(tg.GenerateRecipeText(re));
                                re.STATUS = "0";
                            }
                        }

                        List <CSHVMLP0> VMRows = GetValueMeal(Store_No, DateFrom, DateTo);
                        foreach (CSHVMLP0 vm in VMRows)
                        {
                            sb.Write(tg.GenerateValueMealText(vm));
                            vm.STATUS = "0";
                        }

                        List <CSHPMGP0> PMGRows = GetProductMixGroup(Store_No, DateFrom, DateTo);
                        foreach (CSHPMGP0 pmg in PMGRows)
                        {
                            sb.Write(tg.GenerateProductMixText(pmg));
                            pmg.STATUS = "0";
                        }

                        List <INVMGRP0> MGRows = GetMaterialGroup(Store_No, DateFrom, DateTo);
                        foreach (INVMGRP0 mg in MGRows)
                        {
                            sb.Write(tg.GenerateMaterialGroupText(mg));
                            mg.STATUS = "0";
                        }
                        List <INVUOMP0> UOMRows = GetUnitOfMeasure(Store_No, DateFrom, DateTo);
                        foreach (INVUOMP0 uom in UOMRows)
                        {
                            sb.Write(tg.GenerateInitOfMeasureText(uom));
                            uom.STATUS = "0";
                        }
                        List <INVVEMP0> VERows = GetVendor(Store_No, DateFrom, DateTo);
                        foreach (INVVEMP0 ve in VERows)
                        {
                            sb.Write(tg.GenerateVendorText(ve));
                            ve.STATUS = "0";
                        }
                        // File creation
                        //  *Used for file name
                        string PaddedStore_No = TGViewModel.StoreList[i].value;
                        int    len            = PaddedStore_No.Length;
                        for (int j = 0; j < (5 - len); j++)
                        {
                            PaddedStore_No = "0" + PaddedStore_No;
                        }
                        string filename = "CFM" + PaddedStore_No + "_" + DateTime.Now.ToString("MMddyyyy_HHmm");
                        if (TGViewModel.IncludeAll)
                        {
                            filename += ".ALL";
                        }
                        else
                        {
                            filename += ".PRO";
                        }
                        string path = @"C:/SMS/SMSWEBCFM/";

                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }

                        FileStream file = new FileStream(path + "e" + filename, FileMode.Create, FileAccess.ReadWrite);
                        ms.WriteTo(file);
                        file.Close();
                        ms.Close();
                        EncryptText(path + "e" + filename, path + filename, key);
                        File.Delete(path + "e" + filename);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine(e.Source);
                            System.Diagnostics.Debug.WriteLine(e.Message);
                            System.Diagnostics.Debug.WriteLine(e.StackTrace);
                            System.Diagnostics.Debug.WriteLine(e.InnerException);
                            Exception f = e.InnerException;
                            while (f != null)
                            {
                                System.Diagnostics.Debug.WriteLine("INNER:");
                                System.Diagnostics.Debug.WriteLine(f.Message);
                                System.Diagnostics.Debug.WriteLine(f.Source);
                                f = f.InnerException;
                            }
                            System.Diagnostics.Debug.WriteLine(e.Data);
                        }
                    }
                }
                return(report);
            }
        }