Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                if (_Id == 0)
                {
                    // add new
                    Vendor obj = new Vendor();
                    obj.Code        = textBoxCode.Text;
                    obj.Name        = textBoxName.Text;
                    obj.Address     = textBoxAddress.Text;
                    obj.Tel         = textBoxTel.Text;
                    obj.Fax         = textBoxFax.Text;
                    obj.TaxNo       = textBoxTax.Text.Trim();
                    obj.Description = textBoxDesc.Text;
                    obj.CreateDate  = DateTime.Now;
                    obj.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                    obj.Enable      = true;
                    obj.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                    obj.UpdateDate  = DateTime.Now;
                    if (radioButtonCostOnly.Checked == true)
                    {
                        obj.FKPOCostType = MyConstant.POCostType.CostOnly;
                    }
                    else
                    {
                        obj.FKPOCostType = MyConstant.POCostType.CostAndVat;
                    }
                    db.Vendor.Add(obj);
                    var vendorsOnSingleton = Singleton.SingletonVender.Instance().Vendors;
                    if (vendorsOnSingleton.FirstOrDefault(w => w.Code == obj.Code) != null) // ถ้า code นี้ เคยมีแล้ว
                    {
                        MessageBox.Show("รหัสซ้ำบนระบบใหม่ (pos.Vendor) " + vendorsOnSingleton.FirstOrDefault(w => w.Code == obj.Code).Name);
                        return;
                    }
                    // add to wh customer and brand
                    using (WH_TRATEntities wh = new WH_TRATEntities())
                    {
                        var getCust = wh.WH_CUSTOMER_MAST.FirstOrDefault(w => (w.CUSTOMER_NO + "") == obj.Code);
                        if (getCust != null) // แปลว่าซ้ำยุ่ในระบบเก่า
                        {
                            MessageBox.Show("รหัสซ้ำบนระบบเก่า (dbo.WH_CUSTOMER_MAST) " + getCust.CUSTOMER_NAME);
                            return;
                        }
                        // check in brand
                        var getBrand = wh.WH_BRAND_MAST.FirstOrDefault(w => (w.BRAND_NO + "") == obj.Code);
                        if (getBrand != null) // ซ้ำ ยี่ห้อในระบบเก่า
                        {
                            MessageBox.Show("รหัสซ้ำบนระบบเก่า (dbo.WH_BRAND_MAST) " + getBrand.BRAND_NAME);
                            return;
                        }
                        // pass customer and brand to add 2 table
                        WH_CUSTOMER_MAST customer = new WH_CUSTOMER_MAST();
                        customer.CUSTOMER_NO       = int.Parse(obj.Code);
                        customer.CUSTOMER_NAME     = obj.Name;
                        customer.CUSTOMER_BRANCH   = "-";
                        customer.CUSTOMER_TAXID    = obj.TaxNo;
                        customer.CUSTOMER_ADDRESS  = obj.Address;
                        customer.CUSTOMER_PROVINCE = "";
                        customer.CUSTOMER_POSTNO   = null;
                        customer.CUSTOMER_PHONE    = obj.Tel;
                        customer.CUSTOMER_FAX      = obj.Fax;
                        customer.CUSTOMER_EMAIL    = "";
                        customer.CREATE_DATE       = DateTime.Now;
                        customer.UPDATE_DATE       = null;
                        customer.USER_ID           = obj.CreateBy;
                        wh.WH_CUSTOMER_MAST.Add(customer);
                        // brand
                        WH_BRAND_MAST brand = new WH_BRAND_MAST();
                        brand.BRAND_NO    = int.Parse(obj.Code);
                        brand.BRAND_NAME  = obj.Name;
                        brand.CREATE_DATE = DateTime.Now;
                        brand.UPDATE_DATE = null;
                        brand.USER_ID     = obj.CreateBy;
                        wh.WH_BRAND_MAST.Add(brand);
                        wh.SaveChanges();
                    }
                    db.SaveChanges();
                }
                else
                {
                    // edit
                    var obj          = db.Vendor.SingleOrDefault(w => w.Id == _Id);
                    var vendorSingle = Singleton.SingletonVender.Instance().Vendors.SingleOrDefault(w => w.Id == _Id);
                    Singleton.SingletonVender.Instance().Vendors.Remove(vendorSingle);
                    obj.UpdateDate = DateTime.Now;
                    obj.UpdateBy   = Singleton.SingletonAuthen.Instance().Id;
                    //obj.Code = textBoxCode.Text;
                    obj.Name        = textBoxName.Text;
                    obj.Address     = textBoxAddress.Text;
                    obj.Tel         = textBoxTel.Text;
                    obj.Fax         = textBoxFax.Text;
                    obj.Description = textBoxDesc.Text;
                    if (radioButton1.Checked)
                    {
                        obj.Enable = true;
                    }
                    else
                    {
                        obj.Enable = false;
                    }
                    if (radioButtonCostOnly.Checked == true)
                    {
                        obj.FKPOCostType = MyConstant.POCostType.CostOnly;
                    }
                    else
                    {
                        obj.FKPOCostType = MyConstant.POCostType.CostAndVat;
                    }
                    db.Entry(obj).State = EntityState.Modified;
                }
                db.SaveChanges();

                string user = Singleton.SingletonAuthen.Instance().Id;
                var    v    = db.Vendor.Include("POCostType").OrderByDescending(w => w.UpdateDate).Where(w => w.UpdateBy == user).FirstOrDefault();
                Singleton.SingletonVender.Instance().Vendors.Add(v);

                dataGrid();
                //this.Dispose();
                //_Id = 0;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                if (_Id == 0)
                {
                    // add new
                    ProductBrands obj = new ProductBrands();
                    obj.Code        = textBoxCode.Text;
                    obj.Name        = textBoxName.Text;
                    obj.Description = textBoxDesc.Text;
                    obj.CreateDate  = DateTime.Now;
                    obj.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                    obj.Enable      = true;
                    obj.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                    obj.UpdateDate  = DateTime.Now;
                    db.ProductBrands.Add(obj);

                    //add to wh
                    using (WH_TRATEntities wh = new WH_TRATEntities())
                    {
                        var getBrand = wh.WH_BRAND_MAST.FirstOrDefault(w => (w.BRAND_NO + "") == obj.Code);
                        if (getBrand == null)
                        {
                            // add now
                            WH_BRAND_MAST brand = new WH_BRAND_MAST();
                            brand.BRAND_NAME  = obj.Name;
                            brand.BRAND_NO    = int.Parse(obj.Code);
                            brand.CREATE_DATE = DateTime.Now;
                            brand.USER_ID     = obj.UpdateBy;
                            wh.WH_BRAND_MAST.Add(brand);
                            wh.SaveChanges();
                        }
                        else
                        {
                            // not add
                        }
                    }
                }
                else
                {
                    // edit
                    var obj = db.ProductBrands.SingleOrDefault(w => w.Id == _Id);
                    obj.UpdateDate = DateTime.Now;
                    obj.UpdateBy   = Singleton.SingletonAuthen.Instance().Id;
                    //obj.Code = textBoxCode.Text;
                    obj.Name        = textBoxName.Text;
                    obj.Description = textBoxDesc.Text;
                    if (radioButton1.Checked)
                    {
                        obj.Enable = true;
                    }
                    else
                    {
                        obj.Enable = false;
                    }
                    db.Entry(obj).State = EntityState.Modified;
                }
                db.SaveChanges();
                Singleton.SingletonPriority1.SetInstance();
                //Singleton.SingletonPriority1.Instance();

                dataGrid();
                //this.Dispose();
                //_Id = 0;
            }
        }