Пример #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            var db = gen.GetNewEntity(); // khai bao new entity Framework
            var dt = db.BaoCaoTonKhoes.FirstOrDefault(x => x.StockCode == txtStockCode.Text && x.CompanyCode == Globals.companycode &&
                                                      x.InventoryItemCode == txtInventoryItemCode.Text && x.FiscalYear == FiscalYear && x.FiscalPeriod == FiscalPeriod);


            BaoCaoTonKho data = new BaoCaoTonKho();// class BaoCaoTonKho

            data.CompanyCode       = Globals.companycode;
            data.FiscalYear        = FiscalYear;
            data.FiscalPeriod      = FiscalPeriod;
            data.InventoryItemCode = txtInventoryItemCode.Text;
            data.StockCode         = txtStockCode.Text;
            //txtSQL.Text = "SELECT * from BaoCaoTonKho WHERE StockCode='" + StockCode + "' AND InventoryItemCode='" + inventoryItemCode + "' AND FiscalYear=" + FiscalYear + " AND FiscalPeriod=" + FiscalPeriod;
            //XtraMessageBox.Show("update_tonkho" + txtSQL.Text, "Error1", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (dt != null)
            {
                // chi can update lai so lieu
                //  XtraMessageBox.Show("update_tonkho2" + quantity.ToString() + Posted + INOut, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                data.QuantityDK        = dt.QuantityDK;
                data.QuantityCK        = dt.QuantityCK;
                data.QuantityCK2       = dt.QuantityCK2;
                data.QuantityNTK2      = dt.QuantityNTK2;
                data.QuantityXTK2      = dt.QuantityXTK2;
                data.QuantityNTK       = dt.QuantityNTK;
                data.QuantityXTK       = dt.QuantityXTK;
                data.InventoryItemName = dt.InventoryItemName;
                data.Unit      = dt.Unit;
                data.UnitPrice = dt.UnitPrice;

                try
                {
                    data.QuantityNTK  = sltk(txtInventoryItemCode.Text, FiscalPeriod, FiscalYear, 1, 1);
                    data.QuantityNTK2 = data.QuantityNTK + sltk(txtInventoryItemCode.Text, FiscalPeriod, FiscalYear, 0, 1); // plan
                    data.QuantityXTK  = sltk(txtInventoryItemCode.Text, FiscalPeriod, FiscalYear, 1, 0);
                    data.QuantityXTK2 = data.QuantityXTK + sltk(txtInventoryItemCode.Text, FiscalPeriod, FiscalYear, 0, 0); // plan

                    data.QuantityCK  = data.QuantityDK + data.QuantityNTK - data.QuantityXTK;
                    data.QuantityCK2 = data.QuantityDK + data.QuantityNTK2 - data.QuantityXTK2;

                    var db2 = gen.GetNewEntity();                                    // khai bao new entity Framework
                    db2.Entry(data).State = System.Data.Entity.EntityState.Modified; // update
                    db2.SaveChanges();

                    XtraMessageBox.Show("Update successful", "Hamaco", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtQuantityNTK.Text = data.QuantityNTK.ToString();
                    txtQuantityXTK.Text = data.QuantityXTK.ToString();
                    txtQuantityCK.Text  = data.QuantityCK.ToString();
                }
                catch (DbEntityValidationException ex) // exception khac
                {
                    XtraMessageBox.Show(ex.Message, "test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else // tao moi
            {
                XtraMessageBox.Show("Chưa có mã này", "test", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #2
0
        private void btn_Copy_Click(object sender, EventArgs e)
        {
            //copy role

            var ctx   = gen.GetNewEntity(); // khai bao new entity Framework
            var query = ctx.BaoCaoTonKhoes
                        .Where(c => c.StockCode == StockCode && c.CompanyCode == Globals.companycode && c.FiscalPeriod.ToString() == txtMonth.Text &&
                               c.FiscalYear.ToString() == txtYear.Text);

            foreach (var data in query)
            {
                BaoCaoTonKho obj = new BaoCaoTonKho();
                obj.StockCode         = data.StockCode;
                obj.CompanyCode       = data.CompanyCode;
                obj.FiscalPeriod      = Int32.Parse(txtMonth2.Text);
                obj.FiscalYear        = Int32.Parse(txtYear2.Text);
                obj.InventoryItemCode = data.InventoryItemCode;
                obj.InventoryItemName = data.InventoryItemName;
                obj.Unit         = data.Unit;
                obj.UnitPrice    = data.UnitPrice;
                obj.QuantityDK   = data.QuantityCK; // DK = CK
                obj.QuantityCK   = data.QuantityCK;
                obj.QuantityCK2  = data.QuantityCK; //plan
                obj.QuantityNTK2 = 0;
                obj.QuantityXTK2 = 0;
                obj.QuantityNTK  = 0;
                obj.QuantityXTK  = 0;

                ctx.BaoCaoTonKhoes.Add(obj); //insert
            }
            try
            {
                ctx.SaveChanges();
                XtraMessageBox.Show("Copy bao cao ton kho successfully", "Copy", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (DbEntityValidationException ex) // exception khac
            {
                foreach (var ve in ex.EntityValidationErrors)
                {
                    XtraMessageBox.Show(ve.Entry.Entity.GetType().Name + ve.Entry.State, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }