private DepartmentPurchaseOrder CreateNewDepartmentPurchaseOrder()
 {
     CoralPOS.Models.DepartmentPurchaseOrder order = new DepartmentPurchaseOrder
                                                         {
                                                             CreateDate = DateTime.Now,
                                                             CreateId = "admin",
                                                             UpdateDate = DateTime.Now,
                                                             UpdateId = "admin",
                                                             ExclusiveKey = 1,
                                                             DelFlg = 0,
                                                         };
     DepartmentPurchaseOrderPK pk = new DepartmentPurchaseOrderPK
                                        {
                                            DepartmentId = 1,
                                            PurchaseOrderId = DepartmentPurchaseOrderLogic.FindNextId()
                                        };
     order.DepartmentPurchaseOrderPK = pk;
     return order;
 }
 public void Delete(DepartmentPurchaseOrder data)
 {
     DepartmentPurchaseOrderDao.Delete(data);
 }
 public DepartmentPurchaseOrder Add(DepartmentPurchaseOrder data)
 {
     DepartmentPurchaseOrderDao.Add(data);
     return data;
 }
 public void Update(DepartmentPurchaseOrder data)
 {
     DepartmentPurchaseOrderDao.Update(data);
 }
        protected bool Equals(DepartmentPurchaseOrder entity)
        {
            if (entity == null) return false;
            if (!base.Equals(entity)) return false;

            return true;
        }
        public DepartmentPurchaseOrder Add(DepartmentPurchaseOrder data)
        {
            IList<DepartmentStock> needUpdateStocks = new List<DepartmentStock>();
            IList<Product> adhocProducts = new List<Product>();
            IList<DepartmentStock> adhocStocks = new List<DepartmentStock>();
            // process master

            // if has financial invoice
            // if has not financial invoice

            // process details
            var details = data.DepartmentPurchaseOrderDetails;
            DepartmentPurchaseOrderDao.Execute(
                delegate(ISession session)
                    {
                        foreach (DepartmentPurchaseOrderDetail detail in details)
                        {
                            // 1:NORMAL CASE
                            if (detail.Product.AdhocCase == 0)
                            {
                                // select all stocks
                                var stockList = from stk in session.Query<DepartmentStock>()
                                                where
                                                    stk.ProductMaster.ProductMasterId ==
                                                    detail.ProductMaster.ProductMasterId
                                                orderby stk.Product.ProductId
                                                select stk;
                                AddToList(needUpdateStocks, stockList.ToList());
                                // select stk correspond to product id
                                // find from founded stock
                                DepartmentStock deptStk = null;
                                deptStk = (from stk in needUpdateStocks
                                           where stk.Product.ProductId == detail.Product.ProductId
                                           select stk).FirstOrDefault();

                                // minus stock
                                if (detail.Quantity < 0 || deptStk.GoodQuantity >= detail.Quantity)
                                {
                                    deptStk.Quantity -= detail.Quantity;
                                    deptStk.GoodQuantity -= detail.Quantity;
                                    if (deptStk.HasChanges) deptStk.HasChanges = true;
                                    continue;
                                }
                                else // update relevant stock
                                {
                                    long quantity = detail.Quantity;
                                    quantity -= deptStk.GoodQuantity;
                                    deptStk.Quantity = 0;
                                    deptStk.GoodQuantity = 0;
                                    if (deptStk.HasChanges) deptStk.HasChanges = true;
                                    var otherStocks = from stk in needUpdateStocks
                                                      where
                                                          stk.ProductMaster.ProductMasterId.Equals(
                                                              detail.ProductMaster.ProductMasterId)
                                                          && !stk.Product.ProductId.Equals(detail.Product.ProductId)
                                                      orderby stk.Product.ProductId
                                                      select stk;
                                    foreach (DepartmentStock departmentStock in otherStocks)
                                    {
                                        if (departmentStock.GoodQuantity == 0) continue;
                                        if (departmentStock.GoodQuantity >= quantity)
                                        {
                                            departmentStock.GoodQuantity -= quantity;
                                            departmentStock.Quantity -= quantity;
                                            quantity = 0;
                                            if (deptStk.HasChanges) deptStk.HasChanges = true;
                                            break;
                                        }
                                        else
                                        {
                                            quantity -= deptStk.GoodQuantity;
                                            deptStk.Quantity = 0;
                                            deptStk.GoodQuantity = 0;
                                            if (deptStk.HasChanges) deptStk.HasChanges = true;
                                        }
                                    }
                                    if (quantity > 0)
                                        throw new ArgumentException("Available stock of " +
                                                                    detail.ProductMaster.ProductName +
                                                                    "does not enough for selling");
                                }
                            }
                            else // 2:ADHOC CASE
                            {
                                ObjectUtility.AddToList<Product>(adhocProducts,detail.Product,"ProductId");
                                var result = (from stk in session.Query<DepartmentStock>()
                                             where stk.DepartmentStockPK.ProductId == detail.Product.ProductId
                                             select stk)
                                             .Union
                                             (from stk in adhocStocks
                                             where stk.DepartmentStockPK.ProductId == detail.Product.ProductId
                                             select stk);
                                if (result.Count() == 0)
                                {
                                    DepartmentStock adhocStock = new DepartmentStock
                                                                     {
                                                                         CreateDate = DateTime.Now,
                                                                         CreateId = "admin",
                                                                         UpdateDate = DateTime.Now,
                                                                         UpdateId = "admin",
                                                                         GoodQuantity = 0,
                                                                         Quantity = 0,
                                                                         DelFlg = 0,
                                                                         ExclusiveKey = 1
                                                                     };
                                    adhocStocks.Add(adhocStock);
                                }
                            }
                        }
                        return null;
                    }
                );

            // save adhoc
            foreach (var adhocProduct in adhocProducts)
            {
                ProductDao.Add(adhocProduct);
            }
            foreach (DepartmentStock adhocStock in adhocStocks)
            {
                DepartmentStockDao.Add(adhocStock);
            }
            // save master
            DepartmentPurchaseOrderDao.Add(data);
            // save detail
            foreach (DepartmentPurchaseOrderDetail departmentPurchaseOrderDetail in details)
            {
                DepartmentPurchaseOrderDetailDao.Add(departmentPurchaseOrderDetail);
            }
            // update stock
            foreach (DepartmentStock departmentStock in needUpdateStocks)
            {
                if(departmentStock.HasChanges)
                {
                    DepartmentStockDao.Update(departmentStock);
                }
            }
            // process print

            return data;
        }