Beispiel #1
0
 private void btnUpdateAddress_Click(object sender, EventArgs e)
 {
     if (companyAddressesBindingSource.Count > 0)
     {
         CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current);
         Address          CurrentAddress        = (Address)CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentCompanyAddress.AddressID).FirstOrDefault();
         using (frmAddUpdateAddresses frm = new frmAddUpdateAddresses(CurrentCompanyAddress.AddressID, CurrentAddress))
         {
             frm.ShowDialog();
             if (frm.CurrentAddress.AddressIsDefault)
             {
                 resetAdressDefaults(CurrentAddress.AddressID);
             }
             else if (CurrentlySelectedCompany.Addresses.Count == 1)
             {
                 using (var Dbconnection = new MCDEntities())
                 {
                     CurrentAddress.AddressIsDefault = true;
                     Address Add = Dbconnection.Addresses.Where(a => a.AddressID == CurrentAddress.AddressID).FirstOrDefault();
                     Add.AddressIsDefault = true;
                     //Dbconnection.Entry(Add).State = EntityState.Modified;
                     Dbconnection.SaveChanges();
                 };
             }
             refreshCompanyAddresses();
         }
     }
 }
Beispiel #2
0
        private void resetAdressDefaults(int AddressID = 0)
        {
            using (var Dbconnection = new MCDEntities())
            {
                using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction())
                {
                    try
                    {
                        //CRUD Operations
                        CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current);
                        Address          CurrentAddress        = (Address)CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentCompanyAddress.AddressID).FirstOrDefault();
                        // Dbconnection.Companies.Attach(CurrentlySelectedCompany);
                        foreach (Address Add in CurrentlySelectedCompany.Addresses)
                        {
                            if (AddressID != Add.AddressID)
                            {
                                Add.AddressIsDefault = false;
                                var AddressToupdate = Dbconnection.Addresses.FirstOrDefault(s => s.AddressID == Add.AddressID);
                                AddressToupdate.AddressIsDefault = false;
                            }
                        }


                        ////saves all above operations within one transaction
                        Dbconnection.SaveChanges();

                        //commit transaction
                        dbTran.Commit();
                    }
                    catch (Exception ex)
                    {
                        if (ex is DbEntityValidationException)
                        {
                            foreach (DbEntityValidationResult entityErr in ((DbEntityValidationException)ex).EntityValidationErrors)
                            {
                                foreach (DbValidationError error in entityErr.ValidationErrors)
                                {
                                    MessageBox.Show(error.ErrorMessage, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        //Rollback transaction if exception occurs
                        dbTran.Rollback();
                    }
                }
            };
        }
Beispiel #3
0
        public void SetAddress(Address address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            var existingAddress = CompanyAddresses.FirstOrDefault(a => a.AddressType.AddressTypeCode == address.AddressType.AddressTypeCode);

            if (existingAddress != null)
            {
                CompanyAddresses.Remove(existingAddress);
            }
            CompanyAddresses.Add(address);
        }
Beispiel #4
0
        private void SetAddress(AddressViewModel addressData)
        {
            if (addressData == null)
            {
                throw new ArgumentNullException(nameof(addressData));
            }
            var existingAddress = CompanyAddresses.FirstOrDefault(a => a.AddressType.AddressTypeCode == addressData.AddressType.AddressTypeCode);

            if (existingAddress != null)
            {
                CompanyAddresses.Remove(existingAddress);
            }
            CompanyAddresses.Add(addressData);
        }
        private void AfterCreateAddress(long addressType)
        {
            var addresses = AddressManager.GetRootAddressesByType((CustomAddressType)addressType);

            if (addressType == (long)CustomAddressType.Company)
            {
                this.AnyCompanyAddress = addresses.Count() > 0;
                CompanyAddresses.Clear();
                CompanyAddresses.AddRange(addresses);
            }
            else
            {
                this.AnyCustomerAddress = addresses.Count() > 0;
                CustomerAddresses.Clear();
                CustomerAddresses.AddRange(addresses);
            }
        }
        private async void GetBalances(object sender, EventArgs e)
        {
            var addressList = CompanyAddresses.Select(q => q.Address).ToList();

            addressList.AddRange(CustomerAddresses.Select(q => q.Address));

            var usdtBalanceDict = await AddressManager.BatchGetUSDTBalanceViaNode(addressList);

            var balanceDict = new Dictionary <string, Money>();

            await Task.Run(async() =>
            {
                balanceDict = await AddressManager.BatchGetBTCBalanceViaNode(addressList);
            });


            if (balanceDict.Count <= 0 && usdtBalanceDict.Count <= 0)
            {
                return;
            }

            foreach (var addr in CompanyAddresses)
            {
                if (balanceDict.Count > 0)
                {
                    addr.Balance = balanceDict[addr.Address];
                }
                if (usdtBalanceDict.Count > 0)
                {
                    addr.USDTBalance = usdtBalanceDict[addr.Address];
                }
            }
            foreach (var addr in CustomerAddresses)
            {
                if (balanceDict.Count > 0)
                {
                    addr.Balance = balanceDict[addr.Address];
                }
                if (usdtBalanceDict.Count > 0)
                {
                    addr.USDTBalance = usdtBalanceDict[addr.Address];
                }
            }
        }
        private void PopulateLists()
        {
            //populate installed printers
            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                InstalledPrinters.Add(printer);
            }

            //populate all companyaddresses saved in the DB
            using (var db = new Models.AppContext())
            {
                List <CompanyAddress> comps = db.CompanyAddresses.ToList();

                foreach (CompanyAddress comp in comps)
                {
                    CompanyAddresses.Add(new CompanyAddressModel(comp));
                }
            }
        }
Beispiel #8
0
        private void btnRemoveStudentAddress_Click(object sender, EventArgs e)
        {
            Boolean bWasDefault = false;

            if (companyAddressesBindingSource.Count > 0)
            {
                CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current);
                Address          CurrentAddress        = (Address)CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentCompanyAddress.AddressID).FirstOrDefault();

                if (CurrentAddress.AddressIsDefault)
                {
                    bWasDefault = true;
                }
                using (frmAddUpdateAddresses frm = new frmAddUpdateAddresses(CurrentCompanyAddress.AddressID, CurrentAddress))
                {
                    using (var Dbconnection = new MCDEntities())
                    {
                        using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction())
                        {
                            try
                            {
                                //CRUD Operations
                                // return one instance each entity by primary key
                                var company         = Dbconnection.Companies.FirstOrDefault(p => p.CompanyID == CurrentlySelectedCompany.CompanyID);
                                var AddressToRemove = Dbconnection.Addresses.FirstOrDefault(s => s.AddressID == CurrentCompanyAddress.AddressID);

                                // call Remove method from navigation property for any instance
                                // supplier.Product.Remove(product);
                                // also works
                                company.Addresses.Remove(AddressToRemove);

                                ////saves all above operations within one transaction
                                Dbconnection.SaveChanges();

                                //commit transaction
                                dbTran.Commit();
                            }
                            catch (Exception ex)
                            {
                                if (ex is DbEntityValidationException)
                                {
                                    foreach (DbEntityValidationResult entityErr in ((DbEntityValidationException)ex).EntityValidationErrors)
                                    {
                                        foreach (DbValidationError error in entityErr.ValidationErrors)
                                        {
                                            MessageBox.Show(error.ErrorMessage, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                //Rollback transaction if exception occurs
                                dbTran.Rollback();
                            }
                        }
                    };
                    CurrentlySelectedCompany.Addresses.Remove(CurrentlySelectedCompany.Addresses.Where(a => a.AddressID == CurrentAddress.AddressID).First());
                    if (CurrentlySelectedCompany.Addresses.Count > 0)
                    {
                        if (bWasDefault)
                        {
                            var CurrentlySelectedToUpdate = CurrentlySelectedCompany.Addresses.First();
                            using (var Dbconnection = new MCDEntities())
                            {
                                var x = Dbconnection.Addresses.Where(a => a.AddressID == CurrentlySelectedToUpdate.AddressID).First();
                                x.AddressIsDefault          = true;
                                Dbconnection.Entry(x).State = EntityState.Modified;
                                Dbconnection.SaveChanges();
                            };
                            CurrentlySelectedToUpdate.AddressIsDefault = true;
                        }
                    }

                    refreshCompanyAddresses();
                }
            }
        }
Beispiel #9
0
        private void btnAddCompanyAddress_Click(object sender, EventArgs e)
        {
            frmAddUpdateAddresses frm = new frmAddUpdateAddresses(0);

            frm.ShowDialog();
            if (frm.CurrentAddress != null)
            {
                if (frm.CurrentAddress.AddressID != 0)
                {
                    if (frm.CurrentAddress.AddressIsDefault)
                    {
                        resetAdressDefaults();
                    }
                    else if (CurrentlySelectedCompany.Addresses.Count == 0)
                    {
                        frm.CurrentAddress.AddressIsDefault = true;
                    }
                    using (var Dbconnection = new MCDEntities())
                    {
                        using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction())
                        {
                            try
                            {
                                //CRUD Operations
                                CompanyAddresses CurrentCompanyAddress = ((CompanyAddresses)companyAddressesBindingSource.Current);

                                /*
                                 * this steps follow to both entities
                                 *
                                 * 1 - create instance of entity with relative primary key
                                 *
                                 * 2 - add instance to context
                                 *
                                 * 3 - attach instance to context
                                 */

                                // 1
                                Data.Models.Company C = new Data.Models.Company {
                                    CompanyID = CurrentlySelectedCompany.CompanyID
                                };
                                // 2
                                Dbconnection.Companies.Add(C);
                                // 3
                                Dbconnection.Companies.Attach(C);

                                // 1
                                Address A = new Address {
                                    AddressID = frm.CurrentAddress.AddressID
                                };
                                // 2
                                Dbconnection.Addresses.Add(A);
                                // 3
                                Dbconnection.Addresses.Attach(A);

                                // like previous method add instance to navigation property
                                C.Addresses.Add(A);



                                ////saves all above operations within one transaction
                                Dbconnection.SaveChanges();

                                //commit transaction
                                dbTran.Commit();
                            }
                            catch (Exception ex)
                            {
                                if (ex is DbEntityValidationException)
                                {
                                    foreach (DbEntityValidationResult entityErr in ((DbEntityValidationException)ex).EntityValidationErrors)
                                    {
                                        foreach (DbValidationError error in entityErr.ValidationErrors)
                                        {
                                            MessageBox.Show(error.ErrorMessage, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                //Rollback transaction if exception occurs
                                dbTran.Rollback();
                            }
                        }
                    };

                    using (var Dbconnection = new MCDEntities())
                    {
                        Dbconnection.Addresses.Attach(frm.CurrentAddress);
                        Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupAddressType).Load();
                        Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupProvince).Load();
                        Dbconnection.Entry(frm.CurrentAddress).Reference(a => a.LookupCountry).Load();
                        CurrentlySelectedCompany.Addresses.Add(frm.CurrentAddress);
                    };
                    refreshCompanyAddresses();
                }
            }
        }