private void btnReturnToApprovalStep_Click(object sender, EventArgs e)
        {
            //int ordId = Convert.ToInt32(gridApprovedOrdersView.GetFocusedDataRow()["ID"].ToString());
            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();

            try
            {
                transaction.BeginTransaction();
                Order ord = new Order();
                ord.LoadByPrimaryKey(_orderID);
                if (ord.RowCount > 0 && ord.OrderStatusID == OrderStatus.Constant.ORDER_APPROVED)
                {
                    ord.OrderStatusID = OrderStatus.Constant.ORDER_FILLED;
                    ord.Save();
                    BindApprovedOrders();
                }
                else if (ord.RowCount > 0 && ord.OrderStatusID == OrderStatus.Constant.PICK_LIST_GENERATED)
                {
                    ord.ReleaseReservation();
                    ord.OrderStatusID = OrderStatus.Constant.ORDER_FILLED;
                    ord.Save();
                    BindApprovedOrders();
                }
                transaction.CommitTransaction();
            }
            catch (Exception exp)
            {
                transaction.RollbackTransaction();
                throw exp;
            }
            gridPickListDetail.DataSource = null;
            //HCMISLoader.MarkAsDirtyUsingOrderID(_orderID, NewMainWindow.UserId, true);
            if (BLL.Settings.IsCenter)
            {
                XtraMessageBox.Show("The Issue Order List has been returned to approval stage", "Confirmation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                XtraMessageBox.Show("The Pick list has been returned to approval stage", "Confirmation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void btnReturnToApprovalStep_Click(object sender, EventArgs e)
        {
            //int ordId = Convert.ToInt32(gridApprovedOrdersView.GetFocusedDataRow()["ID"].ToString());
            MyGeneration.dOOdads.TransactionMgr transaction =  MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();

            try
            {
                transaction.BeginTransaction();
                Order ord = new Order();
                ord.LoadByPrimaryKey(_orderID);
                if (ord.RowCount > 0 && ord.OrderStatusID == OrderStatus.Constant.ORDER_APPROVED)
                {
                    ord.OrderStatusID = OrderStatus.Constant.ORDER_FILLED;
                    ord.Save();
                    BindApprovedOrders();
                }
                else if (ord.RowCount > 0 && ord.OrderStatusID == OrderStatus.Constant.PICK_LIST_GENERATED)
                {
                    ord.ReleaseReservation();
                    ord.OrderStatusID = OrderStatus.Constant.ORDER_FILLED;
                    ord.Save();
                    BindApprovedOrders();
                }
                transaction.CommitTransaction();
            }
            catch(Exception exp)
            {
                transaction.RollbackTransaction();
                throw exp;
            }
            gridPickListDetail.DataSource = null;
            //HCMISLoader.MarkAsDirtyUsingOrderID(_orderID, NewMainWindow.UserId, true);
            if (BLL.Settings.IsCenter)
            {
                XtraMessageBox.Show("The Issue Order List has been returned to approval stage", "Confirmation",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                XtraMessageBox.Show("The Pick list has been returned to approval stage", "Confirmation",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private Order GenerateOrder()
        {
            var or = new Order();
            if (OrderID == null)
                or.AddNew();
            else
                or.LoadByPrimaryKey(OrderID.Value);

            or.RefNo = Order.GetNextOrderReference();
            txtRefNo.Text = or.RefNo;

            var oldOrderStatus = or.IsColumnNull("OrderStatusID") ? (int?)null : or.OrderStatusID;
            or.OrderStatusID = OrderStatus.Constant.PICK_LIST_GENERATED;
            //or.Remark = txtRemark.Text;

            or.EurDate = or.Date = DateTimeHelper.ServerDateTime; //Both fields are assigned dates.
            if (TransferTypeID == 1)
                or.RequestedBy = Convert.ToInt32(lkForHub.EditValue);

            or.FilledBy = CurrentContext.UserId;

            or.ContactPerson = txtContactPerson.Text;
            var activity = new Activity();
            activity.LoadByPrimaryKey(Convert.ToInt32(lkAccountType.EditValue));

            or.FromStore = activity.ModeID;

            if (BLL.Settings.IsCenter)
            {
                //What the Hell is this?
                or.PaymentTypeID = PaymentType.Constants.STV;
            }
            else
            {
                or.PaymentTypeID = PaymentType.Constants.STV;
            }
            or.FiscalYearID = FiscalYear.Current.ID;
            or.Save();
            or.LogRequisitionStatus(or.ID, oldOrderStatus, OrderStatus.Constant.PICK_LIST_GENERATED,CurrentContext.UserId);
            return or;
        }
        public void Save(Domain.Request _request,int userID, Domain.OrderStatus orderStatus = Domain.OrderStatus.Submitted)
        {
            var order = new Order();
            order.LoadByPrimaryKey(_request.RequestID);

            var dborderStatus = new BLL.OrderStatus();
            dborderStatus.LoadByCode(EnumService<Domain.OrderStatus>.GetCode(orderStatus));

            order.ChangeStatus(dborderStatus.ID,CurrentContext.UserId);
            order.ApprovedBy = userID;
            order.ApprovedDate = DateTimeHelper.ServerDateTime;

            var orderDetail = new OrderDetail();
            orderDetail.LoadAllByOrderID(_request.RequestID);
            while (!orderDetail.EOF)
            {
                var requestDetail = _request.RequestDetails.SingleOrDefault(r => r.RequestDetailId == orderDetail.ID);
                if(requestDetail== null)
                {
                    orderDetail.MarkAsDeleted();
                }
                else
                {

                    //Set Activity
                    if (requestDetail.ActivityGroup != null)
                    {
                        orderDetail.StoreID = requestDetail.ActivityGroup.Activity.ActivityID;
                        orderDetail.DeliveryNote = requestDetail.ActivityGroup.IsDeliveryNote;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("StoreID");
                    }
                    //Set Manufacturer
                    if (requestDetail.Manufacturer != null)
                    {
                        orderDetail.PreferredManufacturerID = requestDetail.Manufacturer.ManufacturerID;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredManufacturerID");
                    }

                    //Set PhysicalStore
                    if (requestDetail.physicalStore != null)
                    {
                        orderDetail.PreferredPhysicalStoreID = requestDetail.physicalStore.PhysicalStoreID;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredPhysicalStoreID");
                    }

                    //Set ExpiryDate
                    if (requestDetail.ExpiryDate.HasValue)
                    {
                        orderDetail.PreferredExpiryDate = requestDetail.ExpiryDate.Value;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredExpiryDate");
                    }
                        orderDetail.ApprovedQuantity = requestDetail.ApprovedQuantity;
                    orderDetail.StockedOut = requestDetail.StockedOut;
                }

                orderDetail.MoveNext();
            }
            foreach (var requestDetail in _request.RequestDetails.Where(r => r.RequestDetailId == 0).ToList())
            {
                orderDetail.AddNew();
                orderDetail.ItemID = requestDetail.Item.ItemID;
                orderDetail.UnitID = requestDetail.Unit.UnitID;
                orderDetail.Pack = requestDetail.RequestedQuantity;
                orderDetail.OrderID = order.ID;
                orderDetail.QtyPerPack = 1;
                orderDetail.Quantity = requestDetail.RequestedQuantity;
                orderDetail.ApprovedQuantity = requestDetail.ApprovedQuantity;
                orderDetail.StockedOut = requestDetail.StockedOut;
                if (requestDetail.ActivityGroup != null)
                {
                    orderDetail.StoreID = requestDetail.ActivityGroup.Activity.ActivityID;
                    orderDetail.DeliveryNote = requestDetail.ActivityGroup.IsDeliveryNote;
                }
                else
                {
                    orderDetail.SetColumnNull("StoreID");
                }
                //Set Manufacturer
                if (requestDetail.Manufacturer != null)
                {
                    orderDetail.PreferredManufacturerID = requestDetail.Manufacturer.ManufacturerID;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredManufacturerID");
                }

                //Set PhysicalStore
                if (requestDetail.physicalStore != null)
                {
                    orderDetail.PreferredPhysicalStoreID = requestDetail.physicalStore.PhysicalStoreID;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredPhysicalStoreID");
                }

                //Set ExpiryDate
                if (requestDetail.ExpiryDate.HasValue)
                {
                    orderDetail.PreferredExpiryDate = requestDetail.ExpiryDate.Value;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredExpiryDate");
                }

            }

            order.Save();
            orderDetail.Save();
        }