コード例 #1
0
        private void btnAddAddress_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddAddress.Content.ToString() != "Add")
                {
                    if (lblHCity.Content == "?" || lblHLength.Content == "?" || lblHProvince.Content == "?" || lblHStreet.Content == "?"
                        || String.IsNullOrWhiteSpace(txtHCity.Text) || String.IsNullOrWhiteSpace(txtHLength.Text) || String.IsNullOrWhiteSpace(txtHProvince.Text) || String.IsNullOrWhiteSpace(txtHStreet.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        return;
                    }
                }

                lblHStreet.Focus();

                String ownership = "";
                double val = 0;

                if (btnAddAddress.Content.ToString() == "Add")
                {
                    grpAddress.Visibility = Visibility.Visible;
                    lblHStreet.Focus();
                    btnAddAddress.Content = "Save";
                    btnEdtAddress.Content = "Cancel";
                    btnEdtAddress.IsEnabled = true;
                    btnDelAddress.Visibility = Visibility.Hidden;
                    lblHStreet.Focus();

                    tbAddress.IsEnabled = false;
                    tbContact.IsEnabled = false;
                    tbDep.IsEnabled = false;
                    tbInfo.IsEnabled = false;
                    tbRef.IsEnabled = false;
                    tbSps.IsEnabled = false;
                    tbWork.IsEnabled = false;
                }
                else if (btnAddAddress.Content.ToString() == "Save")
                {
                    //if (txtReqName.Text == "" || txtReqDesc.Text == "")
                    //{
                    //    System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    //    return;
                    //}

                    //for view
                    if (status == "View")
                    {
                        if (rdOwned1.IsChecked == true)
                        {
                            ownership = "Owned(Not Mortgage)";
                        }
                        else if (rdOwned2.IsChecked == true)
                        {
                            ownership = "Owned(Mortgage)";
                        }
                        else if (rdUsed.IsChecked == true)
                        {
                            ownership = "Used Free";
                        }
                        else
                        {
                            ownership = "Rented";
                            val = Convert.ToDouble(txtRentedVal.Text);
                        }
                        using (var ctx = new finalContext())
                        {
                            var ctr = ctx.HomeAddresses.Where(x => x.ClientID == cId).Count() + 1;

                            //HomeAddress th = new HomeAddress { ClientID = cId, AddressNumber = ctr, City = txtHCity.Text, LengthOfStay = txtHLength.Text, MonthlyFee = val, OwnershipType = ownership, Province = txtHProvince.Text, Street = txtHStreet.Text };
                            HomeAddress th = new HomeAddress { ClientID = cId, AddressNumber = ctr, City = txtHCity.Text, LengthOfStay = txtHLength.Text, MonthlyFee = 0, OwnershipType = ownership, Province = txtHProvince.Text, Street = txtHStreet.Text };
                            ctx.HomeAddresses.Add(th);
                            ctx.SaveChanges();

                            var ads = from ad in ctx.HomeAddresses
                                      where ad.ClientID == cId
                                      select new { AddressNumber = ad.AddressNumber, Street = ad.Street, Province = ad.Province, City = ad.City, OwnershipType = ad.OwnershipType, MonthlyFee = ad.MonthlyFee, LengthOfStay = ad.LengthOfStay };
                            dgAddress.ItemsSource = ads.ToList();
                        }
                        reset();
                        return;
                    }

                    if (rdOwned1.IsChecked == true)
                    {
                        ownership = "Owned(Not Mortgage)";
                        val = 0;
                    }
                    else if (rdOwned2.IsChecked == true)
                    {
                        ownership = "Owned(Mortgage)";
                        val = 0;
                    }
                    else if (rdUsed.IsChecked == true)
                    {
                        ownership = "Used Free";
                        val = 0;
                    }
                    else
                    {
                        ownership = "Rented";
                        val = Convert.ToDouble(txtRentedVal.Text);
                    }



                    using (var ctx = new finalContext())
                    {
                        var ctr = ctx.TempHomeAddresses.Count() + 1;

                        TempHomeAddress th = new TempHomeAddress { AddressNumber = ctr, City = txtHCity.Text, LengthOfStay = txtHLength.Text, MonthlyFee = val, OwnershipType = ownership, Province = txtHProvince.Text, Street = txtHStreet.Text };
                        ctx.TempHomeAddresses.Add(th);
                        ctx.SaveChanges();

                        var ads = from ad in ctx.TempHomeAddresses
                                  select new { AddressNumber = ad.AddressNumber, Street = ad.Street, Province = ad.Province, City = ad.City, OwnershipType = ad.OwnershipType, MonthlyFee = ad.MonthlyFee, LengthOfStay = ad.LengthOfStay };
                        dgAddress.ItemsSource = ads.ToList();
                        reset();

                    }

                    reset();
                }
                else //for update
                {
                    if (rdOwned1.IsChecked == true)
                    {
                        ownership = "Owned(Not Mortgage)";
                        val = 0;
                    }
                    else if (rdOwned2.IsChecked == true)
                    {
                        ownership = "Owned(Mortgage)";
                        val = 0;
                    }
                    else if (rdUsed.IsChecked == true)
                    {
                        ownership = "Used Free";
                        val = 0;
                    }
                    else
                    {
                        ownership = "Rented";
                        val = Convert.ToDouble(txtRentedVal.Text);
                    }
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            int num = Convert.ToInt32(getRow(dgAddress, 0));
                            var tr = ctx.HomeAddresses.Where(x => x.AddressNumber == num && x.ClientID == cId).First();
                            tr.City = txtHCity.Text;
                            tr.LengthOfStay = txtHLength.Text;
                            tr.MonthlyFee = val;
                            tr.OwnershipType = ownership;
                            tr.Province = txtHProvince.Text;
                            tr.Street = txtHStreet.Text;
                            ctx.SaveChanges();
                            var ads = from ad in ctx.HomeAddresses
                                      where ad.ClientID == cId
                                      select new { AddressNumber = ad.AddressNumber, Street = ad.Street, Province = ad.Province, City = ad.City, OwnershipType = ad.OwnershipType, MonthlyFee = ad.MonthlyFee, LengthOfStay = ad.LengthOfStay };
                            dgAddress.ItemsSource = ads.ToList();
                            reset();
                        }
                        return;
                    }
                    if (rdOwned1.IsChecked == true)
                    {
                        ownership = "Owned(Not Mortgage)";
                    }
                    else if (rdOwned2.IsChecked == true)
                    {
                        ownership = "Owned(Mortgage)";
                    }
                    else if (rdUsed.IsChecked == true)
                    {
                        ownership = "Used Free";
                    }
                    else
                    {
                        ownership = "Rented";
                        val = Convert.ToDouble(txtRentedVal.Text);
                    }

                    using (var ctx = new finalContext())
                    {
                        int num = Convert.ToInt32(getRow(dgAddress, 0));
                        var tr = ctx.TempHomeAddresses.Where(x => x.AddressNumber == num).First();
                        tr.City = txtHCity.Text;
                        tr.LengthOfStay = txtHLength.Text;
                        tr.MonthlyFee = val;
                        tr.OwnershipType = ownership;
                        tr.Province = txtHProvince.Text;
                        tr.Street = txtHStreet.Text;
                        ctx.SaveChanges();
                        var ads = from ad in ctx.TempHomeAddresses
                                  select new { AddressNumber = ad.AddressNumber, Street = ad.Street, Province = ad.Province, City = ad.City, OwnershipType = ad.OwnershipType, MonthlyFee = ad.MonthlyFee, LengthOfStay = ad.LengthOfStay };
                        dgAddress.ItemsSource = ads.ToList();
                        reset();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
コード例 #2
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                checkError();

                /*if (String.IsNullOrWhiteSpace(txtFName.Text) || String.IsNullOrWhiteSpace(txtLName.Text) || String.IsNullOrWhiteSpace(cmbSex.Text) || String.IsNullOrWhiteSpace(cmbStatus.Text))
                {
                    System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields");
                    return;
                }*/
                if(error == true)
                {
                    System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                if (status == "Add")
                {
                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to add this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        var ads = ctx.TempHomeAddresses.Count();
                        var con = ctx.TempClientContacts.Count();

                        if (ads == 0 || con == 0)
                        {
                            System.Windows.MessageBox.Show("Please input at least one address and one contact number", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;
                        }
                    }

                    try
                    {
                        string age = "0";
                        int years = DateTime.Now.Year - dtBDay.SelectedDate.Value.Year;
                        if (dtBDay.SelectedDate.Value.AddYears(years) > DateTime.Now) ;
                        years--;

                        age = years.ToString();
                        int iAge = Convert.ToInt32(age);
                        if (iAge < 18 || iAge > 65)
                        {
                            System.Windows.MessageBox.Show("Client's age must be between 18 and 65");
                            return;
                        }
                    }
                    catch
                    { 
                    
                    }

                    using (var ctx = new finalContext())
                    {
                        var num = ctx.Clients.Where(x => x.FirstName == txtFName.Text && x.LastName == txtLName.Text && x.MiddleName == txtMName.Text && x.Birthday == dtBDay.SelectedDate).Count();
                        if (num > 0)
                        {
                            System.Windows.MessageBox.Show("Client already exists", "Information", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                            return;
                        }
                        var result = "";
                        //if (txtEmail_Copy.Text != "")
                        //{
                            var c = ctx.Clients.Where(x => x.Email == txtEmail_Copy.Text).Count();
                            if (c > 0 && txtEmail_Copy.Text!="")
                            {
                                System.Windows.MessageBox.Show("Email has been already used", "Information", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                                return;
                            }
                            
                            do
                            {
                                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                                var random = new Random();
                                result = new string(
                                    Enumerable.Repeat(chars, 15)
                                              .Select(s => s[random.Next(s.Length)])
                                              .ToArray());
                                c = ctx.Clients.Where(x => x.TrackingNumber == result).Count();
                            } while (c > 0);
                        if (txtEmail_Copy.Text != "")
                        {
                            try
                            {
                                string email = txtEmail_Copy.Text;
                                string link = "http://*****:*****@gmail.com"); //See the note afterwards...
                                msg.Body = message;
                                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                                smtp.EnableSsl = true;
                                smtp.Port = 587;
                                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                                smtp.Credentials = new NetworkCredential("*****@*****.**", "312231212131");
                                smtp.Send(msg);
                            }
                            catch (Exception)
                            {

                            }
                        }

                        Client clt = new Client { Birthday = Convert.ToDateTime(dtBDay.SelectedDate).Date, Active = true, MiddleName = txtMName.Text, LastName = txtLName.Text, FirstName = txtFName.Text, Email = txtEmail_Copy.Text, Sex = cmbSex.Text, SSS = txtSSS.Text, Suffix = txtSuffix.Text, TIN = txtTIN.Text, Status = cmbStatus.Text, Photo = ConvertImageToByteArray(selectedFileName), isConfirmed=true, isRegistered=false, TrackingNumber = result };

                        var ads = from ad in ctx.TempHomeAddresses
                                  select ad;
                        foreach (var item in ads)
                        {
                            HomeAddress add = new HomeAddress { AddressNumber = item.AddressNumber, City = item.City, LengthOfStay = item.LengthOfStay, MonthlyFee = item.MonthlyFee, OwnershipType = item.OwnershipType, Province = item.Province, Street = item.Street };
                            ctx.HomeAddresses.Add(add);
                        }

                        var cts = from ct in ctx.TempClientContacts
                                  select ct;
                        foreach (var item in cts)
                        {
                            ClientContact con = new ClientContact { Contact = item.Contact, ContactNumber = item.ContactNumber, Primary = item.Primary };
                            ctx.ClientContacts.Add(con);
                        }

                        var dps = from dp in ctx.TempDependents
                                  select dp;
                        foreach (var item in dps)
                        {
                            Dependent dep = new Dependent { Birthday = item.Birthday, DependentNumber = item.DependentNumber, FirstName = item.FirstName, LastName = item.LastName, MiddleName = item.LastName, School = item.School, Suffix = item.Suffix };
                            ctx.Dependents.Add(dep);
                        }
                        var wks = from wk in ctx.TempWorks
                                  select wk;
                        foreach (var item in wks)
                        {
                            Work wrk = new Work { BusinessName = item.BusinessName, BusinessNumber = item.BusinessNumber, City = item.City, DTI = item.DTI, Employment = item.Employment, LengthOfStay = item.LengthOfStay, MonthlyIncome = item.MonthlyIncome, PLNumber = item.PLNumber, Position = item.Position, Province = item.Province, status = item.status, Street = item.Street, WorkNumber = item.WorkNumber };
                            ctx.Works.Add(wrk);
                        }
                        var rfs = from rf in ctx.TempReferences
                                  select rf;
                        foreach (var item in rfs)
                        {
                            Reference rfr = new Reference { City = item.City, Contact = item.Contact, FirstName = item.FirstName, LastName = item.LastName, MiddleName = item.MiddleName, Province = item.Province, ReferenceNumber = item.ReferenceNumber, Street = item.Street, Suffix = item.Suffix };
                            ctx.References.Add(rfr);
                        }

                        if (cmbStatus.Text == "Married")
                        {
                            //Spouse sps = new Spouse { Birthday = Convert.ToDateTime(dtSBday.SelectedDate).Date, BusinessName = txtSWName.Text, BusinessNumber = txtSBsNumber.Text, City = txtSCity.Text, DTI = txtSDTI.Text, Employment = cmbSEmployment.Text, FirstName = txtSFName.Text, LastName = txtSLName.Text, LengthOfStay = txtSLength.Text, MiddleName = txtSMName.Text, MonthlyIncome = Convert.ToDouble(txtSIncome.Text), PLNumber = txtSPLNumber.Text, Position = txtSPosition.Text, Province = txtSProvince.Text, status = cmbSStatus.Text, Street = txtSStreet.Text, Suffix = txtSuffix.Text };
                            Spouse sps = new Spouse { Birthday = Convert.ToDateTime(dtSBday.SelectedDate).Date, FirstName = txtSFName.Text, LastName = txtSLName.Text, MiddleName = txtSMName.Text, Suffix = txtSuffix.Text };
                            ctx.Spouses.Add(sps);
                        }

                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Client " + txtFName.Text + " " + txtMName.Text + " " + txtLName.Text + " " + txtSuffix.Text };
                        ctx.AuditTrails.Add(at);

                        ctx.Clients.Add(clt);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Client successfuly added", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
                else
                {
                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to update this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        var ads = ctx.HomeAddresses.Where(x=> x.ClientID == cId).Count();
                        var con = ctx.ClientContacts.Where(x => x.ClientID == cId).Count();

                        if (ads == 0 || con == 0)
                        {
                            System.Windows.MessageBox.Show("Please input at least one address and one contact number", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;
                        }
                    }

                    using (var ctx = new finalContext())
                    {
                        
                        var clt = ctx.Clients.Find(cId);
                        if (txtEmail_Copy.Text != clt.Email && clt.isRegistered == false)
                        {
                            var c = ctx.Clients.Where(x => x.Email == txtEmail_Copy.Text).Count();
                            if (c > 0)
                            {
                                System.Windows.MessageBox.Show("Email has been already used", "Information", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                                return;
                            }
                            else
                            {
                                try
                                {
                                    string email = txtEmail_Copy.Text;
                                    string link = "http://*****:*****@gmail.com"); //See the note afterwards...
                                    msg.Body = message;
                                    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                                    smtp.EnableSsl = true;
                                    smtp.Port = 587;
                                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                                    smtp.Credentials = new NetworkCredential("*****@*****.**", "312231212131");
                                    smtp.Send(msg);
                                }
                                catch (Exception)
                                { 
                                
                                }
                            }
                        }
                        clt.Birthday = Convert.ToDateTime(dtBDay.SelectedDate).Date;
                        clt.MiddleName = txtMName.Text;
                        clt.LastName = txtLName.Text;
                        clt.FirstName = txtFName.Text;
                        clt.Email = txtEmail_Copy.Text;
                        clt.Sex = cmbSex.Text;
                        clt.SSS = txtSSS.Text;
                        clt.Suffix = txtSuffix.Text;
                        clt.TIN = txtTIN.Text;
                        clt.Status = cmbStatus.Text;
                        if (cmbStatus.Text == "Married")
                        {
                            var sps = ctx.Spouses.Where(x => x.ClientID == clt.ClientID).First();
                            sps.Birthday = Convert.ToDateTime(dtSBday.SelectedDate).Date;
                            sps.FirstName = txtSFName.Text;
                            sps.LastName = txtSLName.Text;
                            sps.MiddleName = txtSMName.Text;
                            sps.Suffix = txtSuffix.Text;
                        }
                        if (isChanged == true)
                        {
                            clt.Photo = ConvertImageToByteArray(selectedFileName);
                        }

                        if (status2 == "Confirmation")
                        {
                            clt.isConfirmed = true;
                            AuditTrail at1 = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Confirmed Client " + txtFName.Text + " " + txtMName.Text + " " + txtLName.Text + " " + txtSuffix.Text };
                            ctx.AuditTrails.Add(at1);
                            ctx.SaveChanges();
                            System.Windows.MessageBox.Show("Client has been successfully confirmed", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            this.Close();
                            return;
                        }
                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Client " + txtFName.Text + " " + txtMName.Text + " " + txtLName.Text + " " + txtSuffix.Text };
                        ctx.AuditTrails.Add(at);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Client has been successfully updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }