예제 #1
0
        /// <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;
                }
            }
        }
예제 #2
0
        /// <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);
            }
        }