/// <summary>
        /// 开票并更改库存
        /// </summary>
        /// <param name="bill"></param>
        public void MakeBill(StockBill bill)
        {
            using (IDbConnection conn = DAOFactory.Instance.OpenConnection())
            {
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    // 1. 保存票据信息
                    IStockBillDAO sbDao = DAOFactory.Instance.CreateStockBillDAO();
                    sbDao.InsertBill(bill, conn, trans);

                    // 2. 更改库存数据
                    IStockDAO sDao = DAOFactory.Instance.CreateStockDAO();
                    foreach (StockBillItem item in bill.Items)
                    {
                        Stock stock = sDao.SelectStockByGoodsAndStorehouse(item.Goods.ID, bill.Storehouse.ID, conn, trans);

                        //如果库存中没有该商品,则创建该商品的库存信息。
                        if (stock == null)
                        {
                            stock            = new Stock();
                            stock.Storehouse = bill.Storehouse;
                            stock.Goods      = item.Goods;
                            stock.Count      = 0;
                            sDao.InsertStock(stock, conn, trans);
                        }

                        //执行出入库操作
                        if (bill.BillType.IsOutStorehouse)//出库
                        {
                            stock.Count -= item.Count;
                        }
                        else// 入库
                        {
                            stock.Count += item.Count;
                        }
                        sDao.UpdateStock(stock, conn, trans);
                    }


                    // 3. 提交
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    // 4. 回滚
                    trans.Rollback();
                    throw ex;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 开出库或则入库的票
        /// </summary>
        /// <param name="bill">票据</param>
        public void MakeBill(Bills bill)
        {
            using (IDbConnection conn = DAOFactory.Instance.OpenConnection())
            {
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    //1、保存票据信息;
                    IBillsDAO billDao = DAOFactory.Instance.CreateBillsDAO();
                    billDao.InsertBill(bill, conn, trans);
                    //2、更改库存数据;
                    IStockDAO stockDao = DAOFactory.Instance.CreateStockDAO();
                    foreach (BillsItem item in bill.Items)
                    {
                        Stock stock = stockDao.SelectStockByDrugsAndStorehouse(item.Drugs.ID, bill.Storehouse.ID, conn, trans);
                        //如果库存中没有该商品,则创建库存信息
                        if (stock == null)
                        {
                            stock            = new Stock();
                            stock.Storehouse = bill.Storehouse;
                            stock.Drug       = item.Drugs;
                            stock.Count      = 0;
                            stockDao.InsertStock(stock, conn, trans);
                        }

                        if (bill.BillsType.IsOut)
                        {
                            stock.Count -= item.Count;
                        }
                        else
                        {
                            stock.Count += item.Count;
                        }
                        stockDao.UpdateStock(stock, conn, trans);
                    }

                    //3、提交
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    //4、回滚
                    trans.Rollback();
                    throw ex;
                }
            }
        }