private bool AddUnit() { bool ck = false; int C = 0; try { dgvData.EndEdit(); using (DataClasses1DataContext db = new DataClasses1DataContext()) { bool newDoc = false; int id = txtid.Text.ToInt(); var cstm = db.mh_Customers.Where(x => x.id == id).FirstOrDefault(); if (cstm == null) { newDoc = true; cstm = new mh_Customer(); db.mh_Customers.InsertOnSubmit(cstm); dbClss.AddHistory(this.Name, "Customer Contact", $"New Customer {txtNo.Text}", txtNo.Text); } if (!newDoc) { //dbClss.AddHistory(this.Name, "Customer Contact", $" {} to {}", cstm.No); if (cstm.BranchCode != txtBranchCOde.Text) { dbClss.AddHistory(this.Name, "Customer Contact", $"Branchcode {cstm.BranchCode} to {txtBranchCOde.Text}", cstm.No); } if (cstm.No != txtNo.Text) { dbClss.AddHistory(this.Name, "Customer Contact", $"Customer No {cstm.No} to {txtNo.Text}", cstm.No); } if (cstm.Name != txtName.Text) { dbClss.AddHistory(this.Name, "Customer Contact", $"Customer Name {cstm.Name} to {txtName.Text}", cstm.No); } if (cstm.Address != txtAddress.Text) { dbClss.AddHistory(this.Name, "Customer Contact", $"Address {cstm.Address} to {txtAddress.Text}", cstm.No); } if (cstm.CreditLimit != txtCreditLimit.Value.ToDecimal()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Credit Limit {cstm.CreditLimit} to {txtCreditLimit.Value.ToDecimal()}", cstm.No); } if (cstm.ShippingTime != txtShippingTime.Value.ToInt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Shipping Time {cstm.ShippingTime} to {txtShippingTime.Value.ToInt()}", cstm.No); } if (cstm.AttachFile != txtAttachFile.Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Attach file {cstm.AttachFile} to {txtAttachFile.Value.ToSt()}", cstm.No); } if (cstm.VATRegistration != cbVatRegis.Checked) { dbClss.AddHistory(this.Name, "Customer Contact", $"Vat Regis. {cstm.VATRegistration} to {cbVatRegis.Checked}", cstm.No); } if (cstm.PriceIncludeingVat != cbPriceIncVat.Checked) { dbClss.AddHistory(this.Name, "Customer Contact", $"Price Inc. Vat {cstm.PriceIncludeingVat} to {cbPriceIncVat.Checked}", cstm.No); } if (cstm.ShippingAddress != txtShippingAddress.Text) { dbClss.AddHistory(this.Name, "Customer Contact", $"Shipping Address {cstm.ShippingAddress} to {txtShippingAddress.Text}", cstm.No); } if (cstm.CustomerGroup != cbbCustomerGroup.SelectedValue.ToInt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Customer Group {cstm.CustomerGroup} to {cbbCustomerGroup.SelectedValue.ToInt()}", cstm.No); } if (cstm.VatGroup != cbbVatGroup.SelectedValue.ToInt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Vat Group {cstm.VatGroup} to {cbbVatGroup.SelectedValue.ToInt()}", cstm.No); } if (cstm.DefaultCurrency != cbbCurrency.SelectedValue.ToInt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Currency {cstm.DefaultCurrency} to {cbbCurrency.SelectedValue.ToInt()}", cstm.No); } if (cstm.Active != cbActive.Checked) { dbClss.AddHistory(this.Name, "Customer Contact", $"Status Customer {cbActive.Checked}", cstm.No); } if (cstm.VatRegisNo.ToSt() != txtVatRegisNo.Text.Trim()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Vat Registration No. from {cstm.VatRegisNo.ToSt()} to {txtVatRegisNo.Text.Trim()}", cstm.No); } } cstm.BranchCode = txtBranchCOde.Text; cstm.No = txtNo.Text.Trim(); cstm.Name = txtName.Text.Trim(); cstm.Address = txtAddress.Text.Trim(); cstm.CreditLimit = txtCreditLimit.Value.ToDecimal(); cstm.ShippingTime = txtShippingTime.Value.ToInt(); cstm.AttachFile = txtAttachFile.Value.ToSt(); cstm.VATRegistration = cbVatRegis.Checked; cstm.PriceIncludeingVat = cbPriceIncVat.Checked; cstm.ShippingAddress = txtShippingAddress.Text.Trim(); cstm.CustomerGroup = cbbCustomerGroup.SelectedValue.ToInt(); cstm.VatGroup = cbbVatGroup.SelectedValue.ToInt(); cstm.DefaultCurrency = cbbCurrency.SelectedValue.ToInt(); cstm.Active = cbActive.Checked; cstm.VatRegisNo = txtVatRegisNo.Text.Trim(); db.SubmitChanges(); //file string fullPath = txtAttachFile.Value.ToSt(); string fName = ""; try { if (Path.GetFileName(fullPath) != fullPath) { try { fName = cstm.id.ToSt() + "_" + Path.GetFileName(fullPath); File.Copy(fullPath, Path.Combine(baseClass.GetPathServer(PathCode.Customer), fName), true); fullPath = fName; } catch (Exception ex) { baseClass.Error(ex.Message); fName = ""; } } } catch (Exception ex) { baseClass.Error(ex.Message); } cstm.AttachFile = fName; db.SubmitChanges(); txtid.Text = cstm.id.ToSt(); foreach (var c in dgvData.Rows) { if (c.Cells["dgvC"].Value.ToSt() == "") { continue; } bool newC = false; int idDt = c.Cells["id"].Value.ToInt(); var con = db.mh_CustomerContacts.Where(x => x.id == idDt).FirstOrDefault(); if (con == null) { newC = true; con = new mh_CustomerContact(); db.mh_CustomerContacts.InsertOnSubmit(con); dbClss.AddHistory(this.Name, "Customer Contact", $"New Contact {c.Cells["ContactName"].Value.ToSt()}", cstm.No); } if (!newC) { if (con.Def != c.Cells["Def"].Value.ToBool()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Default from {con.Def} to {c.Cells["Def"].Value.ToBool()}", cstm.No); } if (con.ContactName != c.Cells["ContactName"].Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Contact Name from {con.ContactName} to {c.Cells["ContactName"].Value.ToSt()}", cstm.No); } if (con.Tel != c.Cells["Tel"].Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Telephone no. from {con.Tel} to {c.Cells["Tel"].Value.ToSt()}", cstm.No); } if (con.Fax != c.Cells["Fax"].Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Fax no. from {con.Fax} to {c.Cells["Fax"].Value.ToSt()}", cstm.No); } if (con.Email != c.Cells["Email"].Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Email from {con.Email} to {c.Cells["Email"].Value.ToSt()}", cstm.No); } if (con.Mobile != c.Cells["Mobile"].Value.ToSt()) { dbClss.AddHistory(this.Name, "Customer Contact", $"Mobile from {con.Mobile} to {c.Cells["Mobile"].Value.ToSt()}", cstm.No); } } con.idCustomer = cstm.id; con.Def = c.Cells["Def"].Value.ToBool(); con.ContactName = c.Cells["ContactName"].Value.ToSt(); con.Tel = c.Cells["Tel"].Value.ToSt(); con.Fax = c.Cells["Fax"].Value.ToSt(); con.Email = c.Cells["Email"].Value.ToSt(); con.Mobile = c.Cells["Mobile"].Value.ToSt(); con.Active = true; if (con.Def) { cstm.ContactName = con.ContactName; cstm.PhoneNo = con.Tel; cstm.FaxNo = con.Fax; cstm.Email = con.Email; cstm.Mobile = con.Mobile; } db.SubmitChanges(); } tAction = TypeAction.Edit; DataLoad(); if (dgvData.Rows.Count == 1) { int idDt = dgvData.Rows[0].Cells["id"].Value.ToInt(); dgvData.Rows[0].Cells["Def"].Value = true; var m = db.mh_CustomerContacts.Where(x => x.id == idDt).FirstOrDefault(); if (m != null) { m.Def = true; } db.SubmitChanges(); } } baseClass.Info("Save complete."); } catch (Exception ex) { MessageBox.Show(ex.Message); dbClss.AddError("เพิ่มผู้ซื้อ", ex.Message, this.Name); } //if (C > 0) // MessageBox.Show("บันทึกสำเร็จ!"); return(ck); }
private bool AddUnit() { bool ck = false; int C = 0; try { radGridView1.EndEdit(); using (DataClasses1DataContext db = new DataClasses1DataContext()) { foreach (var g in radGridView1.Rows) { if ( !Convert.ToString(g.Cells["VendorName"].Value).Equals("") && !Convert.ToString(g.Cells["DefaultCrrncy"].Value).Equals("") ) { if (Convert.ToString(g.Cells["dgvC"].Value).Equals("T")) { if (Convert.ToString(g.Cells["dgvCodeTemp"].Value).Equals("")) { // MessageBox.Show("11"); //tb_Vendor gy = new tb_Vendor(); //gy.VendorNo = dbClss.GetNo(1, 2); //gy.Active = Convert.ToBoolean(g.Cells["Active"].Value); //gy.VendorName = Convert.ToString(g.Cells["VendorName"].Value); //gy.CRRNCY = Convert.ToString(g.Cells["CRRNCY"].Value); //gy.Address = Convert.ToString(g.Cells["Address"].Value); //gy.Remark = Convert.ToString(g.Cells["Remark"].Value); //db.tb_Vendors.InsertOnSubmit(gy); //db.SubmitChanges(); var t = new mh_Customer(); t.No = dbClss.GetNo(24, 2); t.Name = g.Cells["VendorName"].Value.ToSt(); t.Active = g.Cells["Active"].Value.ToBool(); t.Address = g.Cells["Address"].Value.ToSt(); t.AttachFile = g.Cells["AttachFile"].Value.ToSt(); t.ContactName = g.Cells["ContactName"].Value.ToSt(); t.DefaultCurrency = g.Cells["DefaultCrrncy"].Value.ToInt(); t.Email = g.Cells["Email"].Value.ToSt(); t.FaxNo = g.Cells["FaxNo"].Value.ToSt(); t.PhoneNo = g.Cells["PhoneNo"].Value.ToSt(); t.PriceIncludeingVat = g.Cells["PriceIncludeVat"].ToBool(); t.ShippingAddress = g.Cells["ReceiveAddress"].Value.ToSt(); t.ShippingTime = g.Cells["ShippingTime"].Value.ToInt(); t.VatGroup = g.Cells["VatGroup"].Value.ToInt(); t.VATRegistration = g.Cells["VatRegis"].Value.ToBool(); t.CustomerGroup = g.Cells["VendorGroup"].Value.ToInt(); t.CreditLimit = g.Cells["CreditLimit"].Value.ToDecimal(); dbClss.AddHistory(this.Name, "เพิ่มผู้ซื้อ", "เพิ่มผู้ซื้อ [" + t.Name + "]", ""); //dbClss.AddHistory(this.Name, "เพิ่มผู้ขาย", "เพิ่มผู้ขาย [" + gy.VendorName + "]", ""); db.mh_Customers.InsertOnSubmit(t); db.SubmitChanges(); C += 1; } else { //var unit1 = (from ix in db.tb_Vendors // where ix.VendorNo == Convert.ToString(g.Cells["dgvCodeTemp"].Value) // select ix).First(); //unit1.VendorName = Convert.ToString(g.Cells["VendorName"].Value); //unit1.Active = Convert.ToBoolean(g.Cells["Active"].Value); //unit1.Address = Convert.ToString(g.Cells["Address"].Value); //unit1.CRRNCY = Convert.ToString(g.Cells["CRRNCY"].Value); //unit1.Remark = Convert.ToString(g.Cells["Remark"].Value); //// unit1.VendorName = Convert.ToString(g.Cells["VendorName"].Value); //C += 1; //db.SubmitChanges(); //dbClss.AddHistory(this.Name, "แก้ไข", "แก้ไขผู้ขาย [" + unit1.VendorName + "]", ""); var t = db.mh_Customers.Where(x => x.No == g.Cells["dgvCodeTemp"].Value.ToSt()).First(); t.Name = g.Cells["VendorName"].Value.ToSt(); t.Active = g.Cells["Active"].Value.ToBool(); t.Address = g.Cells["Address"].Value.ToSt(); t.AttachFile = g.Cells["AttachFile"].Value.ToSt(); t.ContactName = g.Cells["ContactName"].Value.ToSt(); t.DefaultCurrency = g.Cells["DefaultCrrncy"].Value.ToInt(); t.Email = g.Cells["Email"].Value.ToSt(); t.FaxNo = g.Cells["FaxNo"].Value.ToSt(); t.PhoneNo = g.Cells["PhoneNo"].Value.ToSt(); t.PriceIncludeingVat = g.Cells["PriceIncludeVat"].ToBool(); t.ShippingAddress = g.Cells["ReceiveAddress"].Value.ToSt(); t.ShippingTime = g.Cells["ShippingTime"].Value.ToInt(); t.VatGroup = g.Cells["VatGroup"].Value.ToInt(); t.VATRegistration = g.Cells["VatRegis"].Value.ToBool(); t.CustomerGroup = g.Cells["VendorGroup"].Value.ToInt(); t.CreditLimit = g.Cells["CreditLimit"].Value.ToDecimal(); C += 1; db.SubmitChanges(); dbClss.AddHistory(this.Name, "แก้ไข", "แก้ไขผู้ซื้อ [" + t.Name + "]", ""); } } } else { MessageBox.Show("กรอกข้อมูลไม่ครบ!"); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); dbClss.AddError("เพิ่มผู้ซื้อ", ex.Message, this.Name); } if (C > 0) { MessageBox.Show("บันทึกสำเร็จ!"); } return(ck); }