Exemplo n.º 1
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            int companyId;

            lblWarning.Text = "";
            if ((isCustomer && Convert.ToInt32(cboRateTier.SelectedValue) != 0) || isCustomer == false)
            {
                using (var db = new WTCCeresEntities())
                {
                    if (Information.IsNothing(c))
                    {
                        c = new Company();
                    }
                    //
                    c.CompanyCategoryId = Conversions.ToInteger(cboCompanyCategory.SelectedValue);
                    c.CompanyCode       = MdlDataCleasing.TrimAll(tbCode.Text);
                    c.CompanyName       = MdlDataCleasing.TrimAll(tbName.Text);
                    c.Email             = MdlDataCleasing.TrimAll(tbEmail.Text);
                    c.Website           = MdlDataCleasing.TrimAll(tbWebsite.Text);
                    c.Attention         = MdlDataCleasing.TrimAll(tbAttention.Text);
                    c.Notes             = MdlDataCleasing.TrimAll(tbNotes.Text);
                    c.Rating            = rating;
                    c.IsActive          = true;
                    if (cService.AddOrUpdate(c, db))
                    {
                        companyId = cService.GetLastCompanyIdInserted();
                        if (isCustomer)
                        {
                            ccrt.CustomerRateTypeId = Conversions.ToInteger(cboRateTier.SelectedValue);
                            ccrt.CompanyId          = companyId;
                            ccrService.AddOrUpdate(ccrt, db);
                        }

                        int count = 0;
                        foreach (var addr in addrList)
                        {
                            addr.CompanyId = companyId;
                            if (count == 0)
                            {
                                addr.MainAddress = true;
                            }
                            else
                            {
                                addr.MainAddress = false;
                            }

                            caddrService.AddOrUpdate(addr, db);
                            count += 1;
                        }

                        foreach (var cct in cctList)
                        {
                            cct.CompanyId     = companyId;
                            cct.CompanyTypeId = cct.CompanyTypeId;
                            cctService.AddOrUpdate(cct, db);
                        }

                        Close();
                    }
                    else
                    {
                        My.MyProject.Forms.MessageBoxCeresOK.ShowDialog("Error", "There was an error and the information was not saved", this);
                    }
                }
            }
            else
            {
                lblWarning.Text = "Customer Rate needed.";
            }
        }