Esempio n. 1
0
        public GoodsReturnChildForm(string refPurchaseOrderId)
        {
            InitializeComponent();

            if(ReturnPurchaseOrder== null)
            {
                ReturnPurchaseOrder = new PurchaseOrder();
                ReturnPurchaseOrder.PurchaseOrderPK = new
                PurchaseOrderPK{DepartmentId = CurrentDepartment.Get().DepartmentId };
                if(CheckUtility.IsNullOrEmpty(refPurchaseOrderId))
                {
                    refPurchaseOrderId = "000000000000";
                }
                ReturnPurchaseOrder.PurchaseOrderPK.PurchaseOrderId = refPurchaseOrderId;
            }
            btnLoadPO_Click(null,null);
        }
Esempio n. 2
0
 private void ClearGoodsSaleForm()
 {
     ClearInput();
     txtCustomer.Text = "";
     txtBillNumber.Text = "";
     pODList.Clear();
     txtPayment.Text = "";
     txtTotalAmount.Text = "";
     txtCharge.Text = "";
     txtTax.Text = "10%";
     PurchaseOrderBill = null;
     ReturnPurchaseOrder = null;
     GoodsSaleController.PurchaseOrder = null;
 }
Esempio n. 3
0
        private void btnLoadPO_Click(object sender, EventArgs e)
        {
            pODReturnList.Clear();
            pODList.Clear();
            bdsReturnBill.EndEdit();

            try
            {
                GoodsSaleReturnEventArgs goodsSaleReturnEventArgs = new GoodsSaleReturnEventArgs();
                goodsSaleReturnEventArgs.SearchPurchaseOrderId = txtBillNumber.Text.Trim();
                EventUtility.fireEvent(LoadPurchaseOrderEvent, this, goodsSaleReturnEventArgs);

                ReturnPurchaseOrder = goodsSaleReturnEventArgs.RefPurchaseOrder;
                txtBillDate.Text = ReturnPurchaseOrder.CreateDate.ToString("dd/MM/yyyy hh:mm:ss");

                foreach (PurchaseOrderDetail purchaseOrderDetail in ReturnPurchaseOrder.PurchaseOrderDetails)
                {
                    pODList.Add(purchaseOrderDetail);
                }
                bdsBill.EndEdit();
                dgvBill.Focus();
            }
            catch (Exception)
            {

                // do nothing
            }
        }
Esempio n. 4
0
        private void btnInput_Click(object sender, EventArgs e)
        {
            if(GoodsSaleController.PurchaseOrder == null)
            {
                btnAdd_Click(null,e);
            }
            string deptId = string.Format("{0:000}", CurrentDepartment.Get().DepartmentId);
            GoodsSaleEventArgs eventArgs = new GoodsSaleEventArgs();
            PurchaseOrder searchRetPurchaseOrder = new PurchaseOrder();
            searchRetPurchaseOrder.PurchaseOrderPK = new PurchaseOrderPK
                                                                       {
                                                                           PurchaseOrderId = txtRefPurchaseOrder.Text,
                                                                           DepartmentId =
                                                                               CurrentDepartment.Get().DepartmentId
                                                                       };

            // if purchase order id is UNDEFINED
            if (!string.IsNullOrEmpty(txtRefPurchaseOrder.Text) && txtRefPurchaseOrder.Text.Trim().Equals("000"))
            {

                if (string.IsNullOrEmpty(txtRetProductName.Text) // product id is notavailable
                   && string.IsNullOrEmpty(txtRetBarCode.Text)) // bar code is not available
                {
                    MessageBox.Show(
                        "Nếu muốn trả hàng không đối chứng, xin nhập hoá đơn là 000 và chọn một mã vạch phù hợp.");
                    return;
                }

                //searchRetPurchaseOrder.PurchaseOrderPK.PurchaseOrderId = deptId + "UNDEF";
            }

            eventArgs.RefPurchaseOrder = searchRetPurchaseOrder;
            EventUtility.fireEvent(FindRefPurchaseOrder,this,eventArgs);

            PurchaseOrder retPurchaseOrder = eventArgs.RefPurchaseOrder;
            PurchaseOrderDetail retOrderDetail = null;
            if (retPurchaseOrder != null)
            {
                foreach (PurchaseOrderDetail orderDetail in retPurchaseOrder.PurchaseOrderDetails)
                {
                    if (orderDetail.Product.ProductId == txtRetBarCode.Text)
                    {
                        retOrderDetail = orderDetail;
                        break;
                    }
                }
            }
            if (retOrderDetail != null)
            {
                retOrderDetail.Quantity = Int64.Parse(txtRetQuantity.Text);
                retOrderDetail.Price = 0 - retOrderDetail.Price;
                pODList.Add(retOrderDetail);
                bdsBill.EndEdit();
                dgvBill.Refresh();
                dgvBill.Invalidate();

                GoodsSaleController.PurchaseOrder.PurchasePrice = CalculateTotalPrice(pODList);
                txtTotalAmount.Text = GoodsSaleController.PurchaseOrder.PurchasePrice.ToString();

                CreateRowNumbers();
                CalculateCharge();
                RemoveEmptyRowFromList(pODList);
                ClearReturnInput();
                ClearInput();
                txtBarcode.Focus();
            }
            else // in case undefined order
            {
                // if it's UNDEFINED purchase order
                if(!string.IsNullOrEmpty(txtRefPurchaseOrder.Text) && txtRefPurchaseOrder.Text.Trim().Equals("000"))
                {
                    // create new undefined order
                    PurchaseOrder undefPurchaseOrder = new PurchaseOrder();
                    undefPurchaseOrder.PurchaseOrderPK = new PurchaseOrderPK
                    {
                        PurchaseOrderId = txtRefPurchaseOrder.Text.Trim(),
                        DepartmentId =
                            CurrentDepartment.Get().DepartmentId
                    };
                    ReturnPurchaseOrder = undefPurchaseOrder;
                    // if defined barcode
                    /*if(!string.IsNullOrEmpty(txtRetProductName.Text))
                    {*/
                        PurchaseOrderDetail specialDetail = new PurchaseOrderDetail { Product = new Product()};
                        specialDetail.Product.ProductId = txtRetBarCode.Text;

                    if (!string.IsNullOrEmpty(specialDetail.Product.ProductId)
                            && specialDetail.Product.ProductId.Equals(CommonConstants.UNDEFINED_BARCODE_MARK))
                        {
                            specialDetail.Product.ProductId = string.Format("{0:000000000000}", 0);
                            if(CheckUtility.IsNullOrEmpty(txtRetPrice.Text))
                            {
                                /*MessageBox.Show("Xin hãy nhập giá của sản phẩm không có mã vạch");
                                return;*/
                            }
                            specialDetail.Price = Int64.Parse(txtRetPrice.Text);
                        }
                        try    // if null , will go to exception
                        {
                            GoodsSaleEventArgs goodsSaleEventArgs = new GoodsSaleEventArgs();
                            goodsSaleEventArgs.SelectedPurchaseOrderDetail = specialDetail;
                            goodsSaleEventArgs.NotAvailableInStock = true;
                            EventUtility.fireEvent(LoadGoodsEvent, this, goodsSaleEventArgs);

                            txtRetProductName.Text = goodsSaleEventArgs.SelectedPurchaseOrderDetail.Product.ProductMaster.ProductName;
                            txtRetPrice.Text = goodsSaleEventArgs.SelectedPurchaseOrderDetail.Price.ToString();
                            specialDetail = goodsSaleEventArgs.SelectedPurchaseOrderDetail;
                            specialDetail.PurchaseOrder = ReturnPurchaseOrder;
                            specialDetail.PurchaseOrderDetailPK = new PurchaseOrderDetailPK
                                                                      {
                                                                          DepartmentId = CurrentDepartment.Get().DepartmentId,
                                                                          PurchaseOrderId = ReturnPurchaseOrder.PurchaseOrderPK.PurchaseOrderId
                                                                      };
                            specialDetail.Quantity = 1;
                            specialDetail.Price = 0 - specialDetail.Price;
                            pODList.Add(specialDetail);
                            bdsBill.EndEdit();
                            dgvBill.Refresh();
                            dgvBill.Invalidate();
                        }
                        catch (Exception ex)
                        {
                            // do nothing
                            MessageBox.Show("Không có mã vạch này");
                        }
                        finally
                        {
                            GoodsSaleController.PurchaseOrder.PurchasePrice = CalculateTotalPrice(pODList);
                            txtTotalAmount.Text = GoodsSaleController.PurchaseOrder.PurchasePrice.ToString();

                            CreateRowNumbers();
                            CalculateCharge();
                            RemoveEmptyRowFromList(pODList);
                            ClearReturnInput();
                            ClearInput();
                            txtBarcode.Focus();
                        }
                    //}
                }
            }
        }
Esempio n. 5
0
 private void btnReset_Click(object sender, EventArgs e)
 {
     ClearGoodsSaleForm();
     GoodsSaleController.PurchaseOrder = null;
     PurchaseOrderBill = null;
     ReturnPurchaseOrder = null;
 }
Esempio n. 6
0
        void form_SelectReturnGoodsEvent(object sender, GoodsSaleReturnEventArgs e)
        {
            if(GoodsSaleController.PurchaseOrder == null)
            {
                btnAdd_Click(null,null);
            }
            foreach (PurchaseOrderDetail returnPO in e.ReturnPurchaseOrderDetails)
            {
                returnPO.Price = 0 - returnPO.Price;
                pODList.Add(returnPO);
            }
            bdsBill.EndEdit();
            dgvBill.Refresh();
            dgvBill.Invalidate();
            GoodsSaleController.PurchaseOrder.PurchasePrice = CalculateTotalPrice(pODList);
            txtTotalAmount.Text = GoodsSaleController.PurchaseOrder.PurchasePrice.ToString();

            CreateRowNumbers();
            CalculateCharge();
            RemoveEmptyRowFromList(pODList);
            ClearReturnInput();
            ClearInput();
            txtBarcode.Focus();
            ReturnPurchaseOrder = e.RefPurchaseOrder;
        }
Esempio n. 7
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            PurchaseOrderDetail orderDetail = pODList.AddNew();
            orderDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
            orderDetail.CreateDate = DateTime.Now;
            orderDetail.UpdateDate = DateTime.Now;
            orderDetail.UpdateId = orderDetail.CreateId;
            orderDetail.DelFlg = 0;
            orderDetail.DepartmentId = CurrentDepartment.Get().DepartmentId;
            orderDetail.Quantity = 1;
            if (GoodsSaleController.PurchaseOrder == null)
            {
                PurchaseOrder order = new PurchaseOrder();
                GoodsSaleController.PurchaseOrder = order;
            }
            orderDetail.PurchaseOrder = GoodsSaleController.PurchaseOrder;
            PurchaseOrderDetailPK purchaseOrderDetailPK = new PurchaseOrderDetailPK();
            purchaseOrderDetailPK.DepartmentId = CurrentDepartment.Get().DepartmentId;
            orderDetail.PurchaseOrderDetailPK = purchaseOrderDetailPK;

            // new product to test
            ProductMaster productMaster = new ProductMaster();
            orderDetail.ProductMaster = productMaster;
            Product product = new Product();
            product.ProductMaster = orderDetail.ProductMaster;
            orderDetail.Product = product;

            GoodsSaleController.PurchaseOrder.PurchaseOrderDetails =
                ObjectConverter.ConvertToNonGenericList<PurchaseOrderDetail>(pODList);
            bdsBill.EndEdit();
            //bdsBill.EndEdit();
        }
Esempio n. 8
0
 /// <summary>
 /// Add PurchaseOrder to database.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public PurchaseOrder Add(PurchaseOrder data)
 {
     HibernateTemplate.Save(data);
     return data;
 }
Esempio n. 9
0
 /// <summary>
 /// Delete PurchaseOrder from database.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public void Delete(PurchaseOrder data)
 {
     HibernateTemplate.Delete(data);
 }
Esempio n. 10
0
        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;
        }
Esempio n. 11
0
 /// <summary>
 /// Update PurchaseOrder to database.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public void Update(PurchaseOrder data)
 {
     HibernateTemplate.Update(data);
 }
Esempio n. 12
0
 public void Update(PurchaseOrder data)
 {
     PurchaseOrderDAO.Update(data);
 }
Esempio n. 13
0
 public void Delete(PurchaseOrder data)
 {
     PurchaseOrderDAO.Delete(data);
 }
Esempio n. 14
0
        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;
        }
Esempio n. 15
0
 public void FillForm(PurchaseOrder purchaseOrder)
 {
     PurchaseOrder = purchaseOrder;
     ModelToForm();
 }
Esempio n. 16
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pODReturnList.Count <= 0)
            {
                MessageBox.Show("Không có hàng để trả !");
                return;
            }
            if(Int64.Parse(txtCharge.Text) < 0)
            {
                MessageBox.Show("Số tiền trả thêm chưa đủ !");
                return;
            }
            if (goodsSaleReturnController.ReturnPurchaseOrder == null)
            {
                // tra hang nghi van
            }
            else
            {
                // tra hang da co doi chieu
                GoodsSaleReturnEventArgs eventArgs = new GoodsSaleReturnEventArgs();
                eventArgs.RefPurchaseOrder = goodsSaleReturnController.ReturnPurchaseOrder;
                eventArgs.ReturnPurchaseOrderDetails = ObjectConverter.ConvertToNonGenericList<PurchaseOrderDetail>(pODReturnList);

                Receipt receipt = new Receipt();
                receipt.PurchaseOrder = goodsSaleReturnController.ReturnPurchaseOrder;
                receipt.ReceiptPK = new ReceiptPK
                                        {
                                            DepartmentId = CurrentDepartment.Get().DepartmentId
                                        };
                receipt.CreateDate = DateTime.Now;
                receipt.UpdateDate = DateTime.Now;
                receipt.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                receipt.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                receipt.ReceiptName = "HDDTH";
                receipt.ReceiptNumber = goodsSaleReturnController.ReturnPurchaseOrder.PurchaseOrderPK.PurchaseOrderId;
                receipt.TotalAmount = Int64.Parse(txtTotalAmount.Text);
                receipt.CustomerPayment = Int64.Parse(txtPayment.Text);
                receipt.Charge = Int64.Parse(txtCharge.Text);
                receipt.ReturnAmount = Int64.Parse(txtReturnPayment.Text);

                if(pODNewList!=null && pODNewList.Count > 0 )
                {
                   PurchaseOrder nextOrder = new PurchaseOrder();
                    nextOrder.PurchaseOrderPK = new PurchaseOrderPK
                                                    {DepartmentId = CurrentDepartment.Get().DepartmentId};
                    nextOrder.PurchaseOrderDescription = " Next Purchase Order ";
                    nextOrder.DelFlg = 0;
                    nextOrder.ExclusiveKey = 1;

                    nextOrder.PurchaseOrderDetails = ObjectConverter.ConvertToNonGenericList(pODNewList);
                    foreach (PurchaseOrderDetail orderDetail in nextOrder.PurchaseOrderDetails)
                    {
                        nextOrder.PurchasePrice += orderDetail.Price;
                    }

                    eventArgs.NextPurchaseOrder = nextOrder;
                }

                EventUtility.fireEvent(SavePurchaseOrderEvent, this, eventArgs);
                if(!eventArgs.HasErrors)
                {
                    MessageBox.Show("Lưu thành công !");
                    PrintDirectlyToPrinter(eventArgs,receipt);
                    ClearForm();
                }

            }
        }