private bool HasInStockDefectList(DepartmentStockView stock, DepartmentStockViewCollection list, out int stockDefIndex) { int count = 0; foreach (DepartmentStockView stockDefect in list) { if (stockDefect.ProductMaster.ProductMasterId.Equals(stock.ProductMaster.ProductMasterId)) { stockDefIndex = count; return true; } count += 1; } stockDefIndex = count; return false; }
private void UpdateScanType(DepartmentStockView stock) { ScanType scannedType = GetFromScanList(scanTypesList, stock); if (scannedType != null) { IList unscanList = scannedType.UnscanProducts; string productName = stock.ProductMaster.ProductName + "_" + stock.ProductMaster.ProductColor.ColorName + "_" + stock.ProductMaster.ProductSize.SizeName; int scanIndex = GetIndexFromList(unscanList, productName); if (scanIndex >= 0) { scannedType.ScannedProducts.Add(unscanList[scanIndex]); unscanList.RemoveAt(scanIndex); } } else { // create new scantype and add to scanTypeList ScanType scanType = new ScanType(); scanType.ScannedProducts = new ArrayList(); scanType.UnscanProducts = new ArrayList(); scanType.TypeName = stock.ProductMaster.ProductType.TypeName; DepartmentStockCheckingEventArgs eventArgs = new DepartmentStockCheckingEventArgs(); eventArgs.ScannedType = scanType; EventUtility.fireEvent(LoadProductNamesInTypeEvent, this, eventArgs); scanTypesList.Add(eventArgs.ScannedType); } cboTypeList.Refresh(); cboTypeList.Invalidate(); cboTypeList_SelectedIndexChanged(null, null); }
void departmentStockCheckingView_LoadTempInventoryCheckingEvent(object sender, DepartmentStockCheckingEventArgs e) { // search in temp stock ObjectCriteria criteria = new ObjectCriteria(); criteria.AddEqCriteria("DepartmentStockTempPK.DepartmentId", CurrentDepartment.Get().DepartmentId); criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO); criteria.AddEqCriteria("Fixed", CommonConstants.DEL_FLG_NO); criteria.AddEqCriteria("TempSave", CommonConstants.DEL_FLG_NO); criteria.AddOrder("ProductMaster.ProductMasterId", true); IList list = DepartmentStockTempLogic.FindAll(criteria); IList deptStockTempList = null; if (list != null && list.Count > 0) { deptStockTempList = new ArrayList(); foreach (DepartmentStockTemp stockTempSave in list) { int viewIndex = -1; DepartmentStock stockTemp = new DepartmentStockMapper().Convert(stockTempSave); if (HasInList(stockTemp, deptStockTempList, out viewIndex)) { DepartmentStockView view = (DepartmentStockView)deptStockTempList[viewIndex]; view.Quantity += stockTemp.Quantity; view.GoodQuantity += stockTemp.GoodQuantity; view.ErrorQuantity += stockTemp.ErrorQuantity; view.DamageQuantity += stockTemp.DamageQuantity; view.LostQuantity += stockTemp.LostQuantity; view.UnconfirmQuantity += stockTemp.UnconfirmQuantity; view.DepartmentStocks.Add(stockTemp); } else { DepartmentStockView view = new DepartmentStockView(); view.Quantity += stockTemp.Quantity; view.GoodQuantity += stockTemp.GoodQuantity; view.ErrorQuantity += stockTemp.ErrorQuantity; view.DamageQuantity += stockTemp.DamageQuantity; view.LostQuantity += stockTemp.LostQuantity; view.UnconfirmQuantity += stockTemp.UnconfirmQuantity; view.ProductMaster = stockTemp.ProductMaster; view.DepartmentStocks = new ArrayList(); view.DepartmentStocks.Add(stockTemp); deptStockTempList.Add(view); } } } e.ReturnStockViewList = deptStockTempList; }
private ScanType GetFromScanList(ScanTypeCollection types, DepartmentStockView stock) { string checkTypeName = stock.ProductMaster.ProductType.TypeName; foreach (ScanType scanType in types) { if(scanType.TypeName.Equals(checkTypeName)) { return scanType; } } return null; }
void departmentStockCheckingView_LoadGoodsByProductIdEvent(object sender, AppFrame.Presenter.GoodsIO.DepartmentGoodsIO.DepartmentStockCheckingEventArgs e) { string barCode = e.InputBarcode; Product product = ProductLogic.FindById(barCode); if(product==null) { return; } // search in temp stock ObjectCriteria criteria = new ObjectCriteria(); criteria.AddEqCriteria("DepartmentStockTempPK.ProductId", product.ProductId); criteria.AddEqCriteria("DepartmentStockTempPK.DepartmentId", CurrentDepartment.Get().DepartmentId); criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO); IList tempList =DepartmentStockTempLogic.FindAll(criteria); if(tempList!=null && tempList.Count > 0) { e.UnconfirmTempBarcode = true; throw new BusinessException("Mã vạch này đã kiểm trước đó và chưa được xác nhận. Xin liên hệ người quản lý kho."); } // search in stock ProductMaster searchProductMaster = product.ProductMaster; ObjectCriteria searchByProductMasterCriteria = new ObjectCriteria(); /*searchByProductMasterCriteria.AddEqCriteria("Department", CurrentDepartment.Get()); searchByProductMasterCriteria.AddEqCriteria("ProductMaster", searchProductMaster); searchByProductMasterCriteria.AddOrder("CreateDate", false); searchByProductMasterCriteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO); IList productsStockIn = DepartmentStockInDetailLogic.FindAllProductMaster(searchProductMaster);*/ searchByProductMasterCriteria.AddEqCriteria("ProductMaster", searchProductMaster); IList productsStockIn = ProductLogic.FindAll(searchByProductMasterCriteria); IList productsInStock = new ArrayList(); IList productIds = new ArrayList(); //foreach (DepartmentStockInDetail detail in productsStockIn) foreach (Product detail in productsStockIn) { productIds.Add(detail.ProductId); } ObjectCriteria stockCrit = new ObjectCriteria(); stockCrit.AddSearchInCriteria("DepartmentStockPK.ProductId", productIds); stockCrit.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId); stockCrit.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO); stockCrit.AddOrder("DepartmentStockPK.ProductId",true); IList departmentStocks = DepartmentStockLogic.FindAll(stockCrit); // create stock view if (departmentStocks != null && departmentStocks.Count > 0) { DepartmentStockView stockView = new DepartmentStockView(); stockView.ProductMaster = product.ProductMaster; stockView.DepartmentStocks = departmentStocks; foreach (DepartmentStock departmentStock in departmentStocks) { // empty old stock departmentStock.GoodQuantity = 0; departmentStock.ErrorQuantity = 0; departmentStock.UnconfirmQuantity = 0; departmentStock.LostQuantity = 0; departmentStock.DamageQuantity = 0; // sum new stock stockView.Quantity += departmentStock.Quantity; stockView.GoodQuantity += departmentStock.Quantity; stockView.ErrorQuantity += departmentStock.ErrorQuantity; stockView.UnconfirmQuantity += departmentStock.UnconfirmQuantity; stockView.LostQuantity += departmentStock.LostQuantity; stockView.DamageQuantity += departmentStock.DamageQuantity; stockView.ProductId = product.ProductId; stockView.ProductId = product.ProductId; } e.ScannedStockView = stockView; } }
public PurchaseOrder Add(PurchaseOrder data) { /*var maxId = PurchaseOrderDAO.SelectSpecificType(null, Projections.Max("PurchaseOrderPK.PurchaseOrderId")); var purchaseOrderId = maxId == null ? 1 : (Int64.Parse(maxId.ToString()) + 1);*/ string deptId = string.Format("{0:000}", CurrentDepartment.Get().DepartmentId); object maxId = PurchaseOrderDAO.SelectSpecificType(null, Projections.Max("PurchaseOrderPK.PurchaseOrderId")); string purchaseOrderId = "000000000001"; if (maxId != null) { purchaseOrderId = string.Format("{0:000000000000000}", Int64.Parse(maxId.ToString()) + 1); } else { purchaseOrderId = deptId + "000000000001"; } object maxReceptId = ReceiptDAO.SelectSpecificType(null, Projections.Max("ReceiptPK.ReceiptId")); string receiptId = "000000000001"; if (maxReceptId != null) { receiptId = string.Format("{0:000000000000000}", Int64.Parse(maxReceptId.ToString()) + 1); } else { receiptId = deptId + "000000000001"; } /*if(data.Receipts!= null && data.Receipts.Count > 0) { foreach (Receipt receipt in data.Receipts) { receipt.ReceiptPK.ReceiptId = receiptId; ReceiptDAO.Add(receipt); // increase receiptId receiptId = string.Format("{0:000000000000000}", Int64.Parse(receiptId.ToString()) + 1); } }*/ /*var purchaseOrderId = if(purchaseOrderId==null) { string shortDateTime = DateTime.Now.ToString("yyMMdd"); long departmentId = CurrentDepartment.Get().DepartmentId; purchaseOrderId = departmentId.ToString().PadLeft(3,'0') + shortDateTime + "0001"; } else { // add max string purchaseCount = purchaseOrderId.Substring(purchaseOrderId.Length - 4); int nextPurchaseOrderIdNumber = int.Parse(purchaseCount) + 1; string nextPurchaseOrderId = nextPurchaseOrderIdNumber.ToString().PadLeft(4, '0'); purchaseOrderId = purchaseOrderId.Substring(0, purchaseOrderId.Length - 4) + nextPurchaseOrderId; }*/ PurchaseOrderPK purchaseOrderPk = new PurchaseOrderPK { DepartmentId = CurrentDepartment.Get().DepartmentId, PurchaseOrderId = purchaseOrderId }; data.PurchaseOrderPK = purchaseOrderPk; data.UpdateDate = DateTime.Now; data.CreateDate = DateTime.Now; data.CreateId = ClientInfo.getInstance().LoggedUser.Name; data.UpdateId = ClientInfo.getInstance().LoggedUser.Name; // add description string description = ""; foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { description += detail.ProductMaster.ProductName+ " "+ System.Environment.NewLine; } data.PurchaseOrderDescription = description; // save customer Customer customer = data.Customer; bool isReturnOrder = IsReturnOrder(purchaseOrderPk.PurchaseOrderId,data.PurchaseOrderDetails); if(!isReturnOrder) { PurchaseOrderDAO.Add(data); } long id = 1; // create PurchaseOrderId for Undefined Purchase Order string undefinedPOId = null; long undefinedPODetailId = 1; foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { if (detail.PurchaseOrder != null && detail.PurchaseOrder.PurchaseOrderPK!=null && !string.IsNullOrEmpty(detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId) && detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId.Equals("000")) { undefinedPOId = deptId + "NA" + DateTime.Now.ToString("yyMMddHHmmss"); break; } } IDictionary<string,long> stockList = new Dictionary<string,long>(); IDictionary<string, Product> productList = new Dictionary<string, Product>(); foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { // nếu là hàng trả đổi if( detail.PurchaseOrder!= null && detail.PurchaseOrderDetailPK!= null && !CheckUtility.IsNullOrEmpty(detail.PurchaseOrderDetailPK.PurchaseOrderId) && !purchaseOrderId.Equals(detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId) ) { // Hàng trả đổi ReturnPo po = new ReturnPo(); po.CreateDate = DateTime.Now; po.CreateId = ClientInfo.getInstance().LoggedUser.Name; po.UpdateDate = DateTime.Now; po.UpdateId = ClientInfo.getInstance().LoggedUser.Name; ReturnPoPK poPK = new ReturnPoPK { DepartmentId = detail.PurchaseOrderDetailPK.DepartmentId, PurchaseOrderId = detail.PurchaseOrderDetailPK.PurchaseOrderId, PurchaseOrderDetailId = detail.PurchaseOrderDetailPK.PurchaseOrderDetailId, CreateDate = DateTime.Now }; po.ReturnPoPK = poPK; po.Quantity = detail.Quantity; if(detail.Price < 0) { po.Price = 0 - detail.Price; } else { po.Price = detail.Price; } po.ReturnDate = DateTime.Now; po.Product = detail.Product; if (detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId.Equals("000")) { po.ReturnPoPK.PurchaseOrderId = undefinedPOId; po.ReturnPoPK.PurchaseOrderDetailId = undefinedPODetailId++; } else { long originAmount = FindOriginAmount(detail); if (originAmount == 0) { throw new BusinessException("Có lỗi ở hoá đơn gốc, đề nghị kiểm tra"); } long returnedQuantity = (long) ReturnPoDAO.FindQuantityById(poPK); long currentReturnQuantity = returnedQuantity + po.Quantity; if (originAmount < currentReturnQuantity) { throw new BusinessException( "Lỗi :" + detail.Product.ProductMaster.ProductName + " .Tổng cộng :" + originAmount + " .Đã trả : " + returnedQuantity + " .Số lượng muốn trả: " + po.Quantity + " !"); } } ObjectCriteria stockCrit = new ObjectCriteria(); stockCrit.AddEqCriteria("DepartmentStockPK.ProductId", detail.Product.ProductId); IList deptStockList = DepartmentStockDAO.FindAll(stockCrit); if (deptStockList != null && deptStockList.Count > 0) { bool hasPrdFound = false; foreach (string productId in stockList.Keys) { if (productId.Equals(detail.Product.ProductId)) { long qty = stockList[productId]; qty = qty + detail.Quantity; stockList[productId] = qty; hasPrdFound = true; break; } } if (!hasPrdFound) { stockList.Add(detail.Product.ProductId,detail.Quantity); productList.Add(detail.Product.ProductId,detail.Product); } } else { throw new BusinessException("Không có mặt hàng này trong kho. Xin vui lòng kiểm tra dữ liệu"); } if(!isReturnOrder) { po.NextPurchaseOrderId = purchaseOrderId; } ReturnPoDAO.Add(po); // go to next detail continue; } /*var sql = new StringBuilder(FIND_STOCK_SQL); // load the stock var criteria = new ObjectCriteria(true); criteria.AddEqCriteria("s.DelFlg", CommonConstants.DEL_FLG_NO) .AddEqCriteria("s.DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId) .AddEqCriteria("pm.ProductMasterId", detail.ProductMaster.ProductMasterId) .AddEqCriteria("s.Product.ProductId",detail.Product.ProductId); foreach (SQLQueryCriteria crit in criteria.GetQueryCriteria()) { sql.Append(" AND ") .Append(crit.PropertyName) .Append(" ") .Append(crit.SQLString) .Append(" :") .Append(crit.PropertyName) .Append(" "); } sql.Append(" ORDER BY s.CreateDate ASC");*/ bool hasFound = false; foreach (string productId in stockList.Keys) { if(productId.Equals(detail.Product.ProductId)) { long qty = stockList[productId]; qty = qty - detail.Quantity; stockList[productId] = qty; hasFound = true; break; } } if(!hasFound) { stockList.Add(detail.Product.ProductId,0-detail.Quantity); productList.Add(detail.Product.ProductId, detail.Product); } detail.CreateDate = DateTime.Now; detail.UpdateDate = DateTime.Now; detail.CreateId = ClientInfo.getInstance().LoggedUser.Name; detail.UpdateId = ClientInfo.getInstance().LoggedUser.Name; var purchaseOrderDetailPk = new PurchaseOrderDetailPK{DepartmentId = CurrentDepartment.Get().DepartmentId, PurchaseOrderId = purchaseOrderId, PurchaseOrderDetailId = id++}; detail.PurchaseOrderDetailPK = purchaseOrderDetailPk; detail.PurchaseOrder = data; PurchaseOrderDetailDAO.Add(detail); // create Receipt. } // if departmentstock needs update if(stockList.Keys.Count > 0) { IList departmentStockViewList = new ArrayList(); // get stock by product master foreach (string productId in stockList.Keys) { if(!InDepartmentStockViewList(departmentStockViewList,productId)) { Product searchProduct = productList[productId]; ObjectCriteria pmCrit = new ObjectCriteria(); pmCrit.AddEqCriteria("ProductMaster.ProductMasterId", searchProduct.ProductMaster.ProductMasterId); IList list = ProductDAO.FindAll(pmCrit); IList searchPrdIds = new ArrayList(); foreach (Product product in list) { searchPrdIds.Add(product.ProductId); } ObjectCriteria crit = new ObjectCriteria(); crit.AddSearchInCriteria("DepartmentStockPK.ProductId", searchPrdIds); IList deptStockList = DepartmentStockDAO.FindAll(crit); DepartmentStockView stockView = new DepartmentStockView(); stockView.ProductMaster = searchProduct.ProductMaster; SortByProductId(deptStockList); stockView.DepartmentStocks = deptStockList; departmentStockViewList.Add(stockView); } } foreach (string productId in stockList.Keys) { /*ObjectCriteria crit = new ObjectCriteria(); crit.AddEqCriteria("DepartmentStockPK.ProductId", productId); IList list = DepartmentStockDAO.FindAll(crit); if(list!=null && list.Count == 1) { DepartmentStock stock = (DepartmentStock) list[0]; stock.GoodQuantity += stockList[productId]; if (stock.GoodQuantity < 0) { // strange throw new BusinessException("Số lượng trong kho không đáp ứng đủ số lượng bán cho sản phẩm có mã vạch: " + productId); } stock.Quantity += stockList[productId]; DepartmentStockDAO.Update(stock); } else { throw new DataLayerException(" Dữ liệu không đồng nhất"); }*/ if(InDepartmentStockViewList(departmentStockViewList,productId)) { // get origin product id and minus first DepartmentStockView view = GetDepartmentStockViewList(departmentStockViewList, productId); long updateQty = stockList[productId]; DepartmentStock departmentStock = GetDepartmentStockFromView(view, productId); // if stock has been negative if (departmentStock.GoodQuantity < 0) { // accept negative quantity and process later departmentStock.GoodQuantity += updateQty; departmentStock.Quantity += updateQty; continue; } long originUpdateQty = updateQty; updateQty = departmentStock.GoodQuantity + updateQty; // if not enough quantity and still remains update-needing quantity if(updateQty < 0) { // empty the origin product id in stock departmentStock.Quantity -= departmentStock.GoodQuantity; departmentStock.GoodQuantity = 0; // continue do minusing on other product id in stock; foreach (DepartmentStock otherStock in view.DepartmentStocks) { if(otherStock.DepartmentStockPK.ProductId.Equals(departmentStock.DepartmentStockPK.ProductId)) { continue; } long backupUpdateQty = updateQty; updateQty = otherStock.GoodQuantity + updateQty; if(updateQty < 0) { otherStock.Quantity -= otherStock.GoodQuantity; otherStock.GoodQuantity = 0; } else { otherStock.GoodQuantity += backupUpdateQty; otherStock.Quantity += backupUpdateQty; break; } } // if still remain, we accept the negative quantity if(updateQty < 0) { departmentStock.GoodQuantity += updateQty; departmentStock.Quantity += updateQty; } } else // enough quantity { departmentStock.GoodQuantity += originUpdateQty; departmentStock.Quantity += originUpdateQty; } } } // update stock foreach (DepartmentStockView departmentStockView in departmentStockViewList) { foreach (DepartmentStock stock in departmentStockView.DepartmentStocks) { DepartmentStockDAO.Update(stock); } } } return data; }
private DepartmentStock GetDepartmentStockFromView(DepartmentStockView view, string id) { foreach (DepartmentStock stock in view.DepartmentStocks) { if(stock.DepartmentStockPK.ProductId.Equals(id)) { return stock; } } return null; }
public PurchaseOrder Add(PurchaseOrder data) { string deptId = string.Format("{0:000}", CurrentDepartment.Get().DepartmentId); object maxId = PurchaseOrderDAO.SelectSpecificType(null, Projections.Max("PurchaseOrderPK.PurchaseOrderId")); string purchaseOrderId = "000000000001"; if (maxId != null) { purchaseOrderId = string.Format("{0:000000000000000}", Int64.Parse(maxId.ToString()) + 1); } else { long tempId = ClientSetting.MaxPOId; if(tempId ==1) { purchaseOrderId = deptId + "000000000001"; } else { purchaseOrderId = string.Format("{0:000000000000000}", Int64.Parse(tempId.ToString()) + 1); } } ClientSetting.MaxPOId = Int64.Parse(purchaseOrderId); ClientSetting.Save(); object maxReceptId = ReceiptDAO.SelectSpecificType(null, Projections.Max("ReceiptPK.ReceiptId")); string receiptId = "000000000001"; if (maxReceptId != null) { receiptId = string.Format("{0:000000000000000}", Int64.Parse(maxReceptId.ToString()) + 1); } else { receiptId = deptId + "000000000001"; } PurchaseOrderPK purchaseOrderPk = new PurchaseOrderPK { DepartmentId = CurrentDepartment.Get().DepartmentId, PurchaseOrderId = purchaseOrderId }; data.PurchaseOrderPK = purchaseOrderPk; data.UpdateDate = DateTime.Now; data.CreateDate = DateTime.Now; data.CreateId = ClientInfo.getInstance().LoggedUser.Name; data.UpdateId = ClientInfo.getInstance().LoggedUser.Name; // add description string description = ""; foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { description += detail.ProductMaster.ProductName+ " "+ System.Environment.NewLine; } data.PurchaseOrderDescription = description; // save customer Customer customer = data.Customer; bool isReturnOrder = IsReturnOrder(purchaseOrderPk.PurchaseOrderId,data.PurchaseOrderDetails); if(!isReturnOrder) { PurchaseOrderDAO.Add(data); } long id = 1; // create PurchaseOrderId for Undefined Purchase Order string undefinedPOId = null; long undefinedPODetailId = 1; foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { if (detail.PurchaseOrder != null && detail.PurchaseOrder.PurchaseOrderPK!=null && !string.IsNullOrEmpty(detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId) && detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId.Equals("000")) { undefinedPOId = deptId + "NA" + DateTime.Now.ToString("yyMMddHHmmss"); break; } } IDictionary<string,long> stockList = new Dictionary<string,long>(); IDictionary<string, Product> productList = new Dictionary<string, Product>(); foreach (PurchaseOrderDetail detail in data.PurchaseOrderDetails) { PurchaseOrderDetailPK currDetailKey = detail.PurchaseOrderDetailPK; // nếu là hàng trả đổi có xác định hóa đơn if( detail.PurchaseOrder!= null && currDetailKey != null && !CheckUtility.IsNullOrEmpty(currDetailKey.PurchaseOrderId) && !purchaseOrderId.Equals(detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId) ) { // Hàng trả đổi ReturnPo po = new ReturnPo(); po.CreateDate = DateTime.Now; po.CreateId = ClientInfo.getInstance().LoggedUser.Name; po.UpdateDate = DateTime.Now; po.UpdateId = ClientInfo.getInstance().LoggedUser.Name; ReturnPoPK poPK = new ReturnPoPK { DepartmentId = detail.PurchaseOrderDetailPK.DepartmentId, PurchaseOrderId = detail.PurchaseOrderDetailPK.PurchaseOrderId, PurchaseOrderDetailId = detail.PurchaseOrderDetailPK.PurchaseOrderDetailId, CreateDate = DateTime.Now }; po.ReturnPoPK = poPK; po.Quantity = detail.Quantity; if(detail.Price < 0) { po.Price = 0 - detail.Price; } else { po.Price = detail.Price; } po.ReturnDate = DateTime.Now; po.Product = detail.Product; // hàng trả không xác định if (detail.PurchaseOrder.PurchaseOrderPK.PurchaseOrderId.Equals("000")) { po.ReturnPoPK.PurchaseOrderId = undefinedPOId; po.ReturnPoPK.PurchaseOrderDetailId = undefinedPODetailId++; } else // hàng trả có xác định { // xác định số hàng đã trả so với hóa đơn gốc. long originAmount = FindOriginAmount(detail); if (originAmount == 0) { throw new BusinessException("Có lỗi ở hoá đơn gốc, đề nghị kiểm tra"); } long returnedQuantity = (long) ReturnPoDAO.FindQuantityById(poPK); long currentReturnQuantity = returnedQuantity + po.Quantity; if (originAmount < currentReturnQuantity) { throw new BusinessException( "Lỗi :" + detail.Product.ProductMaster.ProductName + " .Tổng cộng :" + originAmount + " .Đã trả : " + returnedQuantity + " .Số lượng muốn trả: " + po.Quantity + " !"); } } ObjectCriteria stockCrit = new ObjectCriteria(); stockCrit.AddEqCriteria("DepartmentStockPK.ProductId", detail.Product.ProductId); IList deptStockList = DepartmentStockDAO.FindAll(stockCrit); if (deptStockList != null && deptStockList.Count > 0) { bool hasPrdFound = false; foreach (string productId in stockList.Keys) { if (productId.Equals(detail.Product.ProductId)) { long qty = stockList[productId]; qty = qty + detail.Quantity; stockList[productId] = qty; hasPrdFound = true; break; } } if (!hasPrdFound) { stockList.Add(detail.Product.ProductId,detail.Quantity); productList.Add(detail.Product.ProductId,detail.Product); } } else { throw new BusinessException("Không có mặt hàng này trong kho. Xin vui lòng kiểm tra dữ liệu"); } if(!isReturnOrder) { po.NextPurchaseOrderId = purchaseOrderId; } ReturnPoDAO.Add(po); // go to next detail continue; } bool hasFound = false; foreach (string productId in stockList.Keys) { if(productId.Equals(detail.Product.ProductId)) { long qty = stockList[productId]; qty = qty - detail.Quantity; stockList[productId] = qty; hasFound = true; break; } } if(!hasFound) { // add update quantity under a negative number stockList.Add(detail.Product.ProductId,0-detail.Quantity); productList.Add(detail.Product.ProductId, detail.Product); } detail.CreateDate = DateTime.Now; detail.UpdateDate = DateTime.Now; detail.CreateId = ClientInfo.getInstance().LoggedUser.Name; detail.UpdateId = ClientInfo.getInstance().LoggedUser.Name; var purchaseOrderDetailPk = new PurchaseOrderDetailPK{DepartmentId = CurrentDepartment.Get().DepartmentId, PurchaseOrderId = purchaseOrderId, PurchaseOrderDetailId = id++}; detail.PurchaseOrderDetailPK = purchaseOrderDetailPk; detail.PurchaseOrder = data; PurchaseOrderDetailDAO.Add(detail); // create Receipt. } // if departmentstock needs update if(stockList.Keys.Count > 0) { IList departmentStockViewList = new ArrayList(); // get stock by product master foreach (string productId in stockList.Keys) { if(!InDepartmentStockViewList(departmentStockViewList,productId)) { Product searchProduct = productList[productId]; ObjectCriteria pmCrit = new ObjectCriteria(); pmCrit.AddEqCriteria("ProductMaster.ProductMasterId", searchProduct.ProductMaster.ProductMasterId); IList list = ProductDAO.FindAll(pmCrit); IList searchPrdIds = new ArrayList(); foreach (Product product in list) { searchPrdIds.Add(product.ProductId); } ObjectCriteria crit = new ObjectCriteria(); crit.AddSearchInCriteria("DepartmentStockPK.ProductId", searchPrdIds); IList deptStockList = DepartmentStockDAO.FindAll(crit); DepartmentStockView stockView = new DepartmentStockView(); stockView.ProductMaster = searchProduct.ProductMaster; ((ArrayList)deptStockList).Sort(); stockView.DepartmentStocks = deptStockList; departmentStockViewList.Add(stockView); } } bool allowTempNegativeSelling = false; // slide id selling foreach (string productId in stockList.Keys) { if(InDepartmentStockViewList(departmentStockViewList,productId)) { // get origin product id and minus first DepartmentStockView view = GetDepartmentStockViewList(departmentStockViewList, productId); long updateQty = stockList[productId]; long originUpdateQty = updateQty; DepartmentStock departmentStock = GetDepartmentStockFromView(view, productId); // if stock has been negative if (departmentStock.GoodQuantity <= 0) { // FIX : Using negative update setting // accept negative quantity and process later if (ClientSetting.NegativeSelling) { departmentStock.GoodQuantity += updateQty; departmentStock.Quantity += updateQty; continue; } // END FIX } else // do minusing to stock { updateQty = departmentStock.GoodQuantity + updateQty; } // if not enough quantity and still remains update-needing quantity if(updateQty < 0) { // empty the origin product id in stock departmentStock.Quantity -= departmentStock.GoodQuantity; departmentStock.GoodQuantity = 0; // continue do minusing on other product id in stock; foreach (DepartmentStock otherStock in view.DepartmentStocks) { if(otherStock.DepartmentStockPK.ProductId.Equals(departmentStock.DepartmentStockPK.ProductId)) { continue; } long backupUpdateQty = updateQty; updateQty = otherStock.GoodQuantity + updateQty; if(updateQty < 0) { otherStock.Quantity -= otherStock.GoodQuantity; otherStock.GoodQuantity = 0; } else { otherStock.GoodQuantity += backupUpdateQty; otherStock.Quantity += backupUpdateQty; break; } } // FIX : Do not accept negative quantity // if still remain, we accept the negative quantity if(updateQty < 0) { if (!ClientSetting.NegativeSelling) { throw new BusinessException("Mặt hàng " + departmentStock.Product.ProductMaster.ProductName + " đã hết."); } else { if (allowTempNegativeSelling == false) { // confirm before save DialogResult isConfirmed = System.Windows.Forms.DialogResult.Cancel; if (!ClientSetting.ConfirmByEmployeeId) { LoginForm loginForm = GlobalUtility.GetFormObject<LoginForm>(FormConstants.CONFIRM_LOGIN_VIEW); loginForm.ConfirmNegativeSelling = true; loginForm.StartPosition = FormStartPosition.CenterScreen; isConfirmed = loginForm.ShowDialog(); } else { EmployeeCheckingForm employeeCheckingForm = GlobalUtility.GetFormObject<EmployeeCheckingForm>( FormConstants.EMPLOYEE_CHECKING_VIEW); employeeCheckingForm.StartPosition = FormStartPosition.CenterScreen; isConfirmed = employeeCheckingForm.ShowDialog(); } if (isConfirmed != System.Windows.Forms.DialogResult.OK) { throw new BusinessException("Không xác nhận được nguoi gửi ...."); } else { allowTempNegativeSelling = true; } } departmentStock.GoodQuantity += updateQty; departmentStock.Quantity += updateQty; } } // END FIX } else // enough quantity { departmentStock.GoodQuantity += originUpdateQty; departmentStock.Quantity += originUpdateQty; } } } // update stock foreach (DepartmentStockView departmentStockView in departmentStockViewList) { foreach (DepartmentStock stock in departmentStockView.DepartmentStocks) { DepartmentStockDAO.Update(stock); } } } return data; }
public void stockSearchView_SearchStockEvent(object sender, DepartmentStockSearchEventArgs e) { var criteria = new SubObjectCriteria("ProductMaster"); if (!string.IsNullOrEmpty(e.ProductMasterId)) { criteria.AddLikeCriteria("ProductMasterId", "%" + e.ProductMasterId + "%"); } criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO); criteria.AddLikeCriteria("ProductName", "%" +e.ProductMasterName + "%"); if (e.ProductType != null && e.ProductType.TypeId > 0) { criteria.AddEqCriteria("ProductType.TypeId", e.ProductType.TypeId); } if (e.ProductSize != null && e.ProductSize.SizeId > 0) { criteria.AddEqCriteria("ProductSize.SizeId", e.ProductSize.SizeId); } if (e.ProductColor != null && e.ProductColor.ColorId > 0) { criteria.AddEqCriteria("ProductColor.ColorId", e.ProductColor.ColorId); } if (e.Country != null && e.Country.CountryId > 0) { criteria.AddEqCriteria("Country.CountryId", e.Country.CountryId); } if (!string.IsNullOrEmpty(e.Description)) { criteria.AddLikeCriteria("Description", "%" + e.Description +"%"); } criteria.AddOrder("ProductName",true); var objectCriteria = new ObjectCriteria(true); objectCriteria.AddEqCriteria("DelFlg", (long)0); if (!string.IsNullOrEmpty(e.ProductId)) { objectCriteria.AddLikeCriteria("DepartmentStockPK.ProductId", "%" + e.ProductMasterId + "%"); } objectCriteria.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId); objectCriteria.AddSubCriteria("ProductMaster",criteria); IList departmentStocks = DepartmentStockLogic.FindAll(objectCriteria); IList stockViewList = new ArrayList(); // create stock view if (departmentStocks != null && departmentStocks.Count > 0) { DepartmentStockView stockView = null; foreach (DepartmentStock departmentStock in departmentStocks) { if (stockView!=null) { if(!stockView.ProductMaster.ProductName.Equals( departmentStock.Product.ProductMaster.ProductName)) { stockViewList.Add(stockView); stockView = null; } } if(stockView == null) { stockView = new DepartmentStockView(); stockView.ProductMaster = departmentStock.Product.ProductMaster; stockView.DepartmentStocks = new ArrayList(); } stockView.DepartmentStocks.Add(departmentStock); stockView.Quantity += departmentStock.Quantity; stockView.GoodQuantity += departmentStock.GoodQuantity; } // add last item if(stockView!=null) { stockViewList.Add(stockView); stockView = null; } e.DepartmentStockList = stockViewList; } }