Example #1
0
        private void frmWarehouseIn_Load(object sender, EventArgs e)
        {
            IList <Product> lstProducts = entities.Product.OrderBy(p => p.ProductName).ToList();

            lstProducts.Add(new Product());
            ProductName.DataSource     = lstProducts;
            ProductName.DisplayMember  = "ProductName";
            ProductName.ValueMember    = "Id";
            gridIn.EditMode            = DataGridViewEditMode.EditOnEnter;
            gridIn.AutoGenerateColumns = false;

            BindingSource bs = new BindingSource();

            if (WarehouseInId > 0)
            {
                WarehouseIn objW = entities.WarehouseIn.Find(WarehouseInId);
                if (objW != null)
                {
                    txtVendor.Text     = objW.Vender;
                    txtReceivedBy.Text = objW.ReceivedBy;
                    txtAuditBy.Text    = objW.AuditBy;
                    dtBillDate.Value   = objW.BillDate;
                    txtReviewedBy.Text = objW.ReviewedBy;
                }
                IList <WarehouseInItem> lst =
                    entities.WarehouseInItem.Where(item => item.WarehouseIn_Id == WarehouseInId).ToList();
                foreach (var item in lst)
                {
                    var product = entities.Product.Find(item.Product_Id);
                    item.ProductName   = product.ProductName;
                    item.Brand         = product.Brand;
                    item.Unit          = product.Unit;
                    item.Specification = product.Specification;
                }
                bs.DataSource = lst;
            }
            else
            {
                bs.DataSource = new List <WarehouseInItem>();
            }
            bs.AllowNew       = true;
            bs.AddingNew     += bs_AddingNew;
            gridIn.DataSource = bs;
            dtBillDate.Focus();
        }
Example #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                WarehouseIn objW = new WarehouseIn();
                objW.Id         = WarehouseInId;
                objW.Vender     = txtVendor.Text.Trim();
                objW.ReceivedBy = txtReceivedBy.Text.Trim();
                objW.AuditBy    = txtAuditBy.Text.Trim();
                objW.BillDate   = dtBillDate.Value.ClearTime();
                objW.ReviewedBy = txtReviewedBy.Text.Trim();
                if (objW.ReceivedBy == objW.ReviewedBy)
                {
                    MessageBox.Show("收货人与复核人不能是同一个人!", "提示");
                    return;
                }
                IList <WarehouseInItem> lst = (gridIn.DataSource as BindingSource).DataSource as IList <WarehouseInItem>;
                //foreach (DataGridViewRow row in gridIn.Rows)
                //{
                //    WarehouseInItem item = new WarehouseInItem();
                //    item.Id = Convert.ToInt32(row.Cells["Id"].Value);
                //    item.Product_Id = Convert.ToInt32(row.Cells["ProductName"].Value);
                //    item.ProductName = Convert.ToString(row.Cells["ProductName"].FormattedValue);
                //    item.Brand = Convert.ToString(row.Cells["Brand"].Value);
                //    item.Specification = Convert.ToString(row.Cells["Specification"].Value);
                //    item.Unit = Convert.ToString(row.Cells["Unit"].Value);
                //    item.Memo = Convert.ToString(row.Cells["Memo"].Value);
                //    item.Quantity = Convert.ToDouble(row.Cells["Quantity"].Value);
                //    item.UnitPrice = Convert.ToDouble(row.Cells["UnitPrice"].Value);
                //    if (item.Quantity > 0)
                //        lst.Add(item);
                //}
                IList <int> needDelItem = new List <int>();
                var         oldItem     = (new Entities()).WarehouseInItem.Where(item => item.WarehouseIn_Id == WarehouseInId).ToList();
                if (WarehouseInId > 0)
                {
                    IList <int> lstId = oldItem.Where(item => item.WarehouseIn_Id == WarehouseInId).Select(d => d.Id).ToList();
                    needDelItem = lstId.Except(lst.Where(item => item.Id > 0 && item.Quantity > 0 && item.Product_Id > 0).Select(item => item.Id).ToList()).ToList();
                }
                using (TransactionScope scope = new TransactionScope())
                {
                    entities.Save <WarehouseIn>(objW);
                    foreach (WarehouseInItem item in lst)
                    {
                        if (item.Quantity <= 0 || item.Product_Id <= 0)
                        {
                            continue;
                        }
                        double changedQty = item.Quantity;
                        item.WarehouseIn_Id = objW.Id;
                        if (item.Id > 0)
                        {
                            var temp = oldItem.First(tmp => tmp.Id == item.Id);
                            changedQty = item.Quantity - temp.Quantity;
                        }
                        entities.Save <WarehouseInItem>(item);
                        var product = entities.Product.Find(item.Product_Id);
                        product.StockQuantity = product.StockQuantity + changedQty;
                        entities.Save <Product>(product);
                    }
                    if (needDelItem.Count > 0)
                    {
                        var delItems = oldItem.Where(tmp => needDelItem.Contains(tmp.Id));
                        var temp     = from t in delItems
                                       group t by t.Product_Id into tt
                                       select new
                        {
                            ProductId = tt.Key,
                            Quantity  = tt.Sum(x => x.Quantity)
                        };
                        foreach (var t1 in temp)
                        {
                            var product = entities.Product.Find(t1.ProductId);

                            product.StockQuantity = product.StockQuantity - t1.Quantity;
                            entities.Save <Product>(product);
                        }
                        entities.Delete <WarehouseInItem>(needDelItem);
                    }
                    scope.Complete();
                }
                MessageBox.Show("数据保存成功!", "提示");
                this.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show("数据保存失败!", "提示");
                LogHelper.WriteLog(LogType.Error, exp, this.GetType());
            }
        }