/// <summary> /// 保存单据并修改库存 /// </summary> public void DoSave() { //Do Verify if (this.cmbStorehouse.SelectedItem == null) { throw new ApplicationException("请指定仓库!"); } if (this.cmbCompany.SelectedItem == null) { throw new ApplicationException("请指定管理单位!"); } if (this.dgvGoodsList.Rows.Count <= 1) { throw new ApplicationException("请指定商品!"); } //Contruct model StockBill bill = new StockBill(); bill.BillType = this.BillType; bill.Actived = true; bill.Code = DateTime.Now.ToString("yyyyMMddHHmmssddd"); bill.Company = (Company)this.cmbCompany.SelectedItem; bill.IsCancelOut = false; bill.IsRedBill = false; bill.MakeDate = this.dtpMakeDate.Value; bill.Remark = this.txtRemark.Text.Trim(); bill.Storehouse = (Storehouse)this.cmbStorehouse.SelectedItem; bill.Maker = this.txtMaker.Text.Trim(); foreach (DataGridViewRow row in this.dgvGoodsList.Rows) { if (row.Tag != null) { StockBillItem billItem = (StockBillItem)row.Tag; bill.Items.Add(billItem); } } //Do Save StockBillService service = new StockBillService(); service.MakeBill(bill); if (MessageBox.Show("是否打印?", "打印提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { PrintService.GetInstance().PrintBill(bill); } //Reset the form this.DoReset(); }
/// <summary> /// 冲销一个票据 /// </summary> /// <param name="bill">要冲销的票据</param> /// <param name="maker">冲销人</param> /// <param name="makeDate">冲销日期</param> /// <param name="remark">备注</param> public StockBill CancelOutBill(StockBill bill, string maker, DateTime makeDate, string remark) { using (IDbConnection conn = DAOFactory.Instance.OpenConnection()) { bill = this.FindBill(bill.ID); // 1. 构造负字票 StockBill redBill = new StockBill(); redBill.Actived = true; redBill.BillType = bill.BillType; redBill.Code = bill.Code; redBill.Company = bill.Company; redBill.CreateDate = DateTime.Now; redBill.IsCancelOut = false; redBill.IsRedBill = true; redBill.MakeDate = makeDate; redBill.Maker = maker; redBill.Remark = "对冲票据" + bill.ID.ToString(); redBill.Storehouse = bill.Storehouse; foreach (StockBillItem item in bill.Items) { StockBillItem redBillItem = new StockBillItem(); redBillItem.Goods = item.Goods; redBillItem.Count = item.Count * (-1); redBillItem.UnitPrice = item.UnitPrice; redBill.Items.Add(redBillItem); } IDbTransaction trans = conn.BeginTransaction(); try { IStockBillDAO sbDao = DAOFactory.Instance.CreateStockBillDAO(); // 2. 保存红票 sbDao.InsertBill(redBill, conn, trans); // 3. 保存原票据信息 bill.IsCancelOut = true; bill.Remark += " 被冲销" + redBill.ID.ToString(); sbDao.UpdateBill(bill, conn, trans); // 4. 更改库存数据 IStockDAO sDao = DAOFactory.Instance.CreateStockDAO(); foreach (StockBillItem redBillItem in redBill.Items) { Stock stock = sDao.SelectStockByGoodsAndStorehouse(redBillItem.Goods.ID, bill.Storehouse.ID, conn, trans); //执行出入库操作 if (redBill.BillType.IsOutStorehouse)//出库 { stock.Count -= redBillItem.Count; } else// 入库 { stock.Count += redBillItem.Count; } sDao.UpdateStock(stock, conn, trans); } // 5. 提交 trans.Commit(); } catch (Exception ex) { // 6. 回滚 bill.IsCancelOut = false; trans.Rollback(); throw ex; } return(redBill); } }
public StockBill SelectBill(int id, IDbConnection conn) { string sql = @" SELECT Mb.ID,Mb.Maker,Mb.MakeDate,Mb.CreateDate,Mb.Actived,Mb.IsCancelOut,Mb.IsRedBill,Mb.Remark, Mbt.ID,Mbt.Name,Mbt.Actived,Mbt.IsOut, Ms.ID,Ms.StorehouseCode,Ms.StorehouseName,Ms.Actived,Ms.Remark, Mc.ID,Mc.CompanyCode,Mc.CompanyName,Mc.CompanyType,Mc.Actived,Mc.Remark FROM MD_Bill AS Mb INNER JOIN MD_BillType AS Mbt ON Mb.BillType_ID=Mbt.ID INNER JOIN MD_Storehouse AS Ms ON Mb.Storehouse_ID=Ms.ID INNER JOIN MD_Company AS Mc ON Mb.Company_ID=Mc.ID WHERE Mb.ID=@ID "; List <SqlParameter> paramList = new List <SqlParameter>(); paramList.Add(new SqlParameter("@ID", id)); SqlDataReader reader = DataAccessUtil.ExecuteReader(sql, paramList, (SqlConnection)conn); StockBill bill = null; while (reader.Read()) { bill = new StockBill(); bill.ID = reader.GetInt32(0); bill.Maker = reader.GetString(1); bill.MakeDate = reader.GetDateTime(2); bill.CreateDate = reader.GetDateTime(3); bill.Actived = reader.GetBoolean(4); bill.IsCancelOut = reader.GetBoolean(5); bill.IsRedBill = reader.GetBoolean(6); if (!reader.IsDBNull(7)) { bill.Remark = reader.GetString(7); } bill.BillType = new StockBillType(); bill.BillType.ID = reader.GetInt32(8); bill.BillType.Name = reader.GetString(9); bill.BillType.Actived = reader.GetBoolean(10); bill.BillType.IsOutStorehouse = reader.GetBoolean(11); // Ms.ID,Ms.StorehouseCode,Ms.StorehouseName,Ms.Actived,Ms.Remark, bill.Storehouse = new Storehouse(); bill.Storehouse.ID = reader.GetInt32(12); bill.Storehouse.Code = reader.GetString(13); bill.Storehouse.Name = reader.GetString(14); bill.Storehouse.Actived = reader.GetBoolean(15); if (!reader.IsDBNull(16)) { bill.Storehouse.Remark = reader.GetString(16); } //Mc.ID,Mc.CompanyCode,Mc.CompanyName,Mc.CompanyType,Mc.Actived,Mc.Remark bill.Company = new Company(); bill.Company.ID = reader.GetInt32(17); bill.Company.Code = reader.GetString(18); bill.Company.Name = reader.GetString(19); bill.Company.CompanyType = (CompanyType)reader.GetInt32(20); bill.Company.Actived = reader.GetBoolean(21); if (!reader.IsDBNull(22)) { bill.Company.Remark = reader.GetString(22); } } reader.Close(); if (bill != null) { sql = @" SELECT Mbi.ID,Mbi.UnitPrice,Mbi.Count,Mbi.Remark, Mg.ID,Mg.GoodsCode,Mg.GoodsName,Mg.Actived,Mg.Remark, Mgf.ID,Mgf.GoodsFromCode,Mgf.GoodsFromName,Mgf.Actived, Mgc.ID,Mgc.GoodsCategoryCode,Mgc.GoodsCategoryName,Mgc.Actived FROM MD_BillItem AS Mbi INNER JOIN MD_Goods AS Mg ON Mbi.Goods_ID=Mg.ID INNER JOIN MD_GoodsCategory AS Mgc ON Mg.GoodsCategory_ID=Mgc.ID INNER JOIN MD_GoodsFrom AS Mgf ON Mg.GoodsFrom_ID=Mgf.ID WHERE Mbi.Bill_ID=@ID "; paramList = new List <SqlParameter>(); paramList.Add(new SqlParameter("@ID", id)); reader = DataAccessUtil.ExecuteReader(sql, paramList, (SqlConnection)conn); while (reader.Read()) { StockBillItem item = new StockBillItem(); item.UnitPrice = reader.GetDecimal(1); item.Count = reader.GetInt32(2); item.Goods = new Goods(); item.Goods.ID = reader.GetInt32(4); item.Goods.Code = reader.GetString(5); item.Goods.Name = reader.GetString(6); item.Goods.Actived = reader.GetBoolean(7); if (!reader.IsDBNull(8)) { item.Goods.Remark = reader.GetString(8); } item.Goods.From = new GoodsFrom(); item.Goods.From.ID = reader.GetInt32(9); item.Goods.From.Code = reader.GetString(10); item.Goods.From.Name = reader.GetString(11); item.Goods.From.Actived = reader.GetBoolean(12); item.Goods.Category = new GoodsCategory(); item.Goods.Category.ID = reader.GetInt32(13); item.Goods.Category.Code = reader.GetString(14); item.Goods.Category.Name = reader.GetString(15); item.Goods.Category.Actived = reader.GetBoolean(16); bill.Items.Add(item); } reader.Close(); } return(bill); }
private void dgvGoodsList_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { DataGridViewRow row = this.dgvGoodsList.Rows[e.RowIndex]; object value = this.dgvGoodsList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (value == null) { return; } StockBillItem item = null; switch (e.ColumnIndex) { case 0: //输入商品编码 string goodsCode = value.ToString(); ModelService service = new ModelService(); Goods goods = service.GetGoodsByCode(goodsCode); if (goods == null) { throw new ApplicationException("编码输入错误,没有匹配的商品!"); } //Fill Goods Info into datagridview row DataGridViewTextBoxCell cell = null; cell = new DataGridViewTextBoxCell(); cell.Value = goods.Name; row.Cells[1] = cell; cell = new DataGridViewTextBoxCell(); cell.Value = goods.Standard; row.Cells[2] = cell; cell = new DataGridViewTextBoxCell(); cell.Value = goods.Unit; row.Cells[3] = cell; cell = new DataGridViewTextBoxCell(); cell.Value = goods.Category.Name; row.Cells[4] = cell; cell = new DataGridViewTextBoxCell(); cell.Value = goods.From.Name; row.Cells[5] = cell; //TODO: add StockBillItem into row's tag item = (StockBillItem)row.Tag; if (item == null) { item = new StockBillItem(); row.Tag = item; } item.Goods = goods; break; case 6: //输入数量编码 int count = int.Parse(value.ToString()); item = (StockBillItem)row.Tag; if (item == null) { item = new StockBillItem(); row.Tag = item; } item.Count = count; if (item.Count != 0 && item.UnitPrice != 0) { row.Cells[8].Value = item.Count * item.UnitPrice; //item.Money = item.Count * item.UnitPrice; } break; case 7: //输入单价编码 decimal unitPrice = decimal.Parse(value.ToString()); item = (StockBillItem)row.Tag; if (item == null) { item = new StockBillItem(); row.Tag = item; } item.UnitPrice = unitPrice; if (item.Count != 0 && item.UnitPrice != 0) { row.Cells[8].Value = item.Count * item.UnitPrice; //item.Money = item.Count * item.UnitPrice; } break; case 8: //输入金额编码 decimal money = decimal.Parse(value.ToString()); item = (StockBillItem)row.Tag; if (item == null) { item = new StockBillItem(); row.Tag = item; } //item.Money = money; break; default: break; } } catch (Exception ex) { ErrorHandler.OnError(ex); } }