Exemplo n.º 1
0
 public Item(string name, string code, ItemType type, Guid itemGroupId, bool status, string note, Guid unitId, string unitBarcode)
 {
     Initialize();
     Name        = name;
     Code        = code;
     Type        = type;
     ItemGroupId = itemGroupId;
     Note        = note;
     ItemUnits.Add(new ItemUnit(unitId, unitBarcode, 1, true));
 }
Exemplo n.º 2
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (Item.Name == "FIX_CHRS.List")
            {
                string srcFile  = Path.Combine(hostEnv.WebRootPath, "FixDB", "ChrsSrc.txt");
                string destFile = Path.Combine(hostEnv.WebRootPath, "FixDB", "ChrsList.txt");

                using (StreamReader sr = new StreamReader(srcFile))
                    using (StreamWriter sw = new StreamWriter(destFile))
                    {
                        string line;
                        while (sr.Peek() >= 0)
                        {
                            line = sr.ReadLine();
                            sw.WriteLine(line);
                            int sidx = line.IndexOf("[");
                            int eidx = line.IndexOf("]");
                            if ((sidx != -1) && (eidx != -1))
                            {
                                string[] chrsList = line.Substring(sidx, eidx - sidx).Trim().Replace("[", "").Replace("]", "").Split(',');
                                bool     isFirst  = true;
                                int      NewCID   = -1;
                                foreach (var cid in chrsList)
                                {
                                    int    id  = int.Parse(cid.Trim());
                                    Church chr = _context.Church.FirstOrDefault(p => p.Id == id);

                                    if (chr != null)
                                    {
                                        if (isFirst)
                                        {
                                            sw.WriteLine("KEY=" + id + " Name=" + chr.Name);
                                            isFirst = false;
                                            NewCID  = chr.Id;
                                        }
                                        else
                                        {
                                            sw.WriteLine(" id=" + id + " Name=" + chr.Name);

                                            // DELETE CHURCH
                                            _context.Church.Remove(chr);
                                            try
                                            {
                                                await _context.SaveChangesAsync();
                                            }
                                            catch (Exception)
                                            {
                                                sw.WriteLine("**** DB ERROR: ***** Changing person.cid. cid=[" + chr.Id + "]");
                                                return(RedirectToPage("./Index"));
                                            }

                                            //// MERGE CHURCH
                                            //var parr = _context.Person.Where(p => p.ChurchId == chr.Id).ToList<Person>();
                                            //foreach (var p in parr)
                                            //{
                                            //    p.ChurchId = NewCID;
                                            //    _context.Attach(p).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch (Exception e)
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing person.cid. cid=[" + chr.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}
                                        }
                                    }
                                    else
                                    {
                                        sw.WriteLine("**** Cant Find id [" + cid + "]");
                                    }
                                }
                            }
                            sw.WriteLine("----------------------------------------------");
                        }
                    }
            }

            if (Item.Name == "FIX_PPL.List")
            {
                string srcFile  = Path.Combine(hostEnv.WebRootPath, "FixDB", "PplSrc.txt");
                string destFile = Path.Combine(hostEnv.WebRootPath, "FixDB", "PplList.txt");

                using (StreamReader sr = new StreamReader(srcFile))
                    using (StreamWriter sw = new StreamWriter(destFile))
                    {
                        string line;
                        while (sr.Peek() >= 0)
                        {
                            line = sr.ReadLine();
                            sw.WriteLine(line);
                            int sidx = line.IndexOf("[");
                            int eidx = line.IndexOf("]");
                            if ((sidx != -1) && (eidx != -1))
                            {
                                string[] pplList = line.Substring(sidx, eidx - sidx).Trim().Replace("[", "").Replace("]", "").Split(',');
                                bool     isFirst = true;
                                int      NewPID  = -1;
                                foreach (var pid in pplList)
                                {
                                    int    id  = int.Parse(pid.Trim());
                                    Person psn = _context.Person.FirstOrDefault(p => p.Id == id);

                                    if (psn != null)
                                    {
                                        if (isFirst)
                                        {
                                            sw.WriteLine("KEY=" + id + " First=" + psn.FirstName + " Last=" + psn.LastName + " Cell=" + psn.CellPhone + " EMail=" + psn.Email);
                                            isFirst = false;
                                            NewPID  = psn.Id;
                                        }
                                        else
                                        {
                                            sw.WriteLine(" id=" + id + " First=" + psn.FirstName + " Last=" + psn.LastName + " Cell=" + psn.CellPhone + " EMail=" + psn.Email);

                                            //**** DELETE ****
                                            _context.Person.Remove(psn);
                                            try
                                            {
                                                await _context.SaveChangesAsync();
                                            }
                                            catch (Exception)
                                            {
                                                sw.WriteLine("**** DB ERROR: ***** Delete person. id=[" + psn.Id + "]");
                                            }

                                            //**** UPDATE ****
                                            //var pg1arr = _context.Person.Where(p => p.Pg1PersonId == psn.Id).ToList<Person>();
                                            //foreach (var p in pg1arr)
                                            //{
                                            //    p.Pg1PersonId = NewPID;
                                            //    _context.Attach(p).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch (Exception e)
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing PG1 pid=[" + psn.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}

                                            //var pg2arr = _context.Person.Where(p => p.Pg2PersonId == psn.Id).ToList<Person>();
                                            //foreach (var p in pg2arr)
                                            //{
                                            //    p.Pg2PersonId = NewPID;
                                            //    _context.Attach(p).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch (Exception e)
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing PG2 pid=[" + psn.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}

                                            //var attarr = _context.Attendance.Where(a => a.PersonId == psn.Id).ToList<Attendance>();
                                            //foreach (var a in attarr)
                                            //{
                                            //    a.PersonId = NewPID;
                                            //    _context.Attach(a).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch (Exception e)
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing Attendance pid=[" + psn.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}
                                        }
                                    }
                                    else
                                    {
                                        sw.WriteLine("**** Cant Find id [" + pid + "]");
                                    }
                                }
                            }
                            sw.WriteLine("----------------------------------------------");
                        }
                    }
            }

            if (Item.Name == "FIX_ADRS.List")
            {
                string srcFile  = Path.Combine(hostEnv.WebRootPath, "FixDB", "AdrSrc.txt");
                string destFile = Path.Combine(hostEnv.WebRootPath, "FixDB", "AdrList.txt");

                using (StreamReader sr = new StreamReader(srcFile))
                    using (StreamWriter sw = new StreamWriter(destFile))
                    {
                        string line;
                        while (sr.Peek() >= 0)
                        {
                            line = sr.ReadLine();
                            sw.WriteLine(line);
                            int idx = line.IndexOf("[");
                            if (idx != -1)
                            {
                                string[] adrList  = line.Substring(idx).Trim().Replace("[", "").Replace("]", "").Split(',');
                                bool     isFirst  = true;
                                int      NewAdrID = -1;
                                foreach (var aid in adrList)
                                {
                                    int     id  = int.Parse(aid.Trim());
                                    Address adr = _context.Address.FirstOrDefault(a => a.Id == id);

                                    if (adr != null)
                                    {
                                        if (isFirst)
                                        {
                                            sw.WriteLine("KEY=" + id + " Street=" + adr.Street + " Town=" + adr.Town + " State=" + adr.State + " Zip=" + adr.ZipCode);
                                            isFirst  = false;
                                            NewAdrID = adr.Id;
                                        }
                                        else
                                        {
                                            sw.WriteLine(" id=" + id + " Street=" + adr.Street + " Town=" + adr.Town + " State=" + adr.State + " Zip=" + adr.ZipCode);

                                            _context.Address.Remove(adr);
                                            try
                                            {
                                                await _context.SaveChangesAsync();
                                            }
                                            catch (Exception)
                                            {
                                                sw.WriteLine("**** DB ERROR: ***** Changing person.aid. aid=[" + adr.Id + "]");
                                                return(RedirectToPage("./Index"));
                                            }

                                            //var parr = _context.Person.Where(p => p.AddressId == adr.Id).ToList<Person>();
                                            //foreach (var p in parr)
                                            //{
                                            //    p.AddressId = NewAdrID;
                                            //    _context.Attach(p).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch (Exception e)
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing person.aid. aid=[" + adr.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}

                                            //var carr = _context.Church.Where(c => c.AddressId == adr.Id).ToList<Church>();
                                            //foreach (var c in carr)
                                            //{
                                            //    c.AddressId = NewAdrID;
                                            //    _context.Attach(c).State = EntityState.Modified;

                                            //    try
                                            //    {
                                            //        await _context.SaveChangesAsync();
                                            //    }
                                            //    catch
                                            //    {
                                            //        sw.WriteLine("**** DB ERROR: ***** Changing church.aid. aid=[" + adr.Id + "]");
                                            //        return RedirectToPage("./Index");
                                            //    }
                                            //}
                                        }
                                    }
                                    else
                                    {
                                        sw.WriteLine("**** Cant Find id [" + aid + "]");
                                    }
                                }
                            }
                            sw.WriteLine("----------------------------------------------");
                        }
                    }
            }


            if (Item.Name == "ALL_ITEMS")
            {
                //Item itemA = new Item();
                //itemA.Name = "AAA";
                //itemA.Size = 1;
                //itemA.Units = 1;
                //itemA.Status = 1;
                //itemA.Upc = "111";
                //_context.Item.Add(itemA);
                //int resA = await _context.SaveChangesAsync();

                //InventoryItem invItemA = new InventoryItem();
                //invItemA.ItemId = itemA.Id;
                //invItemA.InventoryId = 1;
                //invItemA.ReorderThreshold = (float)10;
                //invItemA.Units = itemA.Units;
                //_context.InventoryItem.Add(invItemA);
                //int resA2 = await _context.SaveChangesAsync();

                //Item itemB = new Item();
                //itemB.Name = "BBB";
                //itemB.Size = 1;
                //itemB.Units = 2;
                //itemB.Status = 1;
                //itemB.Upc = "222";
                //_context.Item.Add(itemB);
                //int resB = await _context.SaveChangesAsync();

                //InventoryItem invItemB = new InventoryItem();
                //invItemB.ItemId = itemB.Id;
                //invItemB.InventoryId = 1;
                //invItemB.ReorderThreshold = (float)20;
                //invItemB.Units = itemB.Units;
                //_context.InventoryItem.Add(invItemB);
                //int resB2 = await _context.SaveChangesAsync();

                //Console.WriteLine(resA + "," + resA2 + "," + resB + "," + resB2);

                // Add all items and Inventory Items

                //Inventory inv = new Inventory();
                //inv.Id = 1;
                //inv.Name = "Non-Edible";
                //inv.Type = 1;
                //_context.Inventory.Add(inv);
                //await _context.SaveChangesAsync();

                foreach (var str in ItemDataStrings)
                {
                    ItemData idobj = JsonConvert.DeserializeObject <ItemData>(str);

                    Item item = new Item();
                    item.Name = idobj.name.Replace("^^", "\"").Replace("^", "'");
                    item.Size = 1;
                    ItemUnits units;
                    if (ItemUnits.TryParse(idobj.units, out units))
                    {
                        item.Units = (int)units;
                    }
                    else
                    {
                        item.Units = (int)ItemUnits.unknown;
                    }
                    item.Status = 1;
                    item.Upc    = "";

                    _context.Item.Add(item);
                    int res1 = await _context.SaveChangesAsync();

                    InventoryItem invItem = new InventoryItem();
                    invItem.ItemId           = item.Id;
                    invItem.InventoryId      = 1;
                    invItem.ReorderThreshold = (float)idobj.reorder;
                    invItem.Units            = item.Units;
                    _context.InventoryItem.Add(invItem);
                    int res2 = await _context.SaveChangesAsync();
                }
            }

            //if (!ModelState.IsValid)
            //{
            //    return Page();
            //}


            return(RedirectToPage("./Index"));
        }