コード例 #1
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;
            }
        }
コード例 #2
0
        private void btnAddDep_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddDep.Content.ToString() != "Add")
                {
                    if (lblDFName.Content == "?" || lblDLName.Content == "?" || lblDMI.Content == "?" || lblDSchool.Content == "?" || lblDSuffix.Content == "?"
                        || String.IsNullOrWhiteSpace(txtDFName.Text) || String.IsNullOrWhiteSpace(txtDLName.Text) || String.IsNullOrWhiteSpace(txtDSchool.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                if (btnAddDep.Content.ToString() == "Add")
                {
                    grpDependents.Visibility = Visibility.Visible;
                    btnAddDep.Content = "Save";
                    btnEdtDep.Content = "Cancel";
                    btnEdtDep.IsEnabled = true;
                    btnDelDep.Visibility = Visibility.Hidden;
                    tbAddress.IsEnabled = false;
                    tbContact.IsEnabled = false;
                    tbDep.IsEnabled = false;
                    tbInfo.IsEnabled = false;
                    tbRef.IsEnabled = false;
                    tbSps.IsEnabled = false;
                    tbWork.IsEnabled = false;

                }
                else if (btnAddDep.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")
                    {

                        using (var ctx = new finalContext())
                        {
                            var ctr = ctx.Dependents.Where(x => x.ClientID == cId).Count() + 1;

                            Dependent tdp = new Dependent { ClientID = cId, Birthday = Convert.ToDateTime(dtDBDay.SelectedDate).Date, DependentNumber = ctr, FirstName = txtDFName.Text, LastName = txtDLName.Text, MiddleName = txtDMName.Text, School = txtDSchool.Text, Suffix = txtDSuffix.Text };
                            ctx.Dependents.Add(tdp);
                            ctx.SaveChanges();

                            var dps = from td in ctx.Dependents
                                      where td.ClientID == cId
                                      select new { DependentNumber = td.DependentNumber, LastName = td.LastName, FirstName = td.FirstName, MiddleName = td.MiddleName, Suffix = td.Suffix, Birthday = td.Birthday, School = td.School };
                            dgDependents.ItemsSource = dps.ToList();
                            reset();

                        }
                        return;
                    }


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

                        TempDependent tdp = new TempDependent { Birthday = Convert.ToDateTime(dtDBDay.SelectedDate).Date, DependentNumber = ctr, FirstName = txtDFName.Text, LastName = txtDLName.Text, MiddleName = txtDMName.Text, School = txtDSchool.Text, Suffix = txtDSuffix.Text };
                        ctx.TempDependents.Add(tdp);
                        ctx.SaveChanges();

                        var dps = from td in ctx.TempDependents
                                  select new { DependentNumber = td.DependentNumber, LastName = td.LastName, FirstName = td.FirstName, MiddleName = td.MiddleName, Suffix = td.Suffix, Birthday = td.Birthday, School = td.School };
                        dgDependents.ItemsSource = dps.ToList();
                        reset();

                    }

                    reset();
                }
                else //for update
                {
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            int num = Convert.ToInt32(getRow(dgDependents, 0));
                            var td = ctx.Dependents.Where(x => x.DependentNumber == num && x.ClientID == cId).First();
                            td.Birthday = Convert.ToDateTime(dtDBDay.SelectedDate).Date;
                            td.FirstName = txtDFName.Text;
                            td.LastName = txtDLName.Text;
                            td.MiddleName = txtDMName.Text;
                            td.School = txtDSchool.Text;
                            td.Suffix = txtDSuffix.Text;
                            ctx.SaveChanges();
                            var dps = from tdp in ctx.Dependents
                                      where tdp.ClientID == cId
                                      select new { DependentNumber = tdp.DependentNumber, LastName = tdp.LastName, FirstName = tdp.FirstName, MiddleName = tdp.MiddleName, Suffix = tdp.Suffix, Birthday = tdp.Birthday, School = tdp.School };
                            dgDependents.ItemsSource = dps.ToList();
                            reset();
                        }
                        return;
                    }


                    using (var ctx = new finalContext())
                    {
                        int num = Convert.ToInt32(getRow(dgDependents, 0));
                        var td = ctx.TempDependents.Where(x => x.DependentNumber == num).First();
                        td.Birthday = Convert.ToDateTime(dtDBDay.SelectedDate).Date;
                        td.FirstName = txtDFName.Text;
                        td.LastName = txtDLName.Text;
                        td.MiddleName = txtDMName.Text;
                        td.School = txtDSchool.Text;
                        td.Suffix = txtDSuffix.Text;
                        ctx.SaveChanges();
                        var dps = from tdp in ctx.TempDependents
                                  select new { DependentNumber = tdp.DependentNumber, LastName = tdp.LastName, FirstName = tdp.FirstName, MiddleName = tdp.MiddleName, Suffix = tdp.Suffix, Birthday = tdp.Birthday, School = tdp.School };
                        dgDependents.ItemsSource = dps.ToList();
                        reset();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }