protected void btnConfirm_Click(object sender, EventArgs e)
        {
            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                PurchaseOrder po = pom.FindPurchaseOrderByID(int.Parse(Request.QueryString["ID"]));
                po.DONumber = txtDONumber.Text.ToString();
                po.IsDelivered = true;
                po.DateReceived = Convert.ToDateTime(txtReceivedDate.Text.ToString());
                po.ReceivedBy = Membership.GetCurrentLoggedInUser().UserID;
                pom.UpdatePurchaseOrder(po);

                using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
                {
                    SA33.Team12.SSIS.DAL.AdjustmentVoucher av = new SA33.Team12.SSIS.DAL.AdjustmentVoucher();
                    av.VoucherNumber = avm.GenerateVoucherNumber();
                    av.CreatedBy = Membership.GetCurrentLoggedInUser().UserID;
                    av.DateIssued = po.DateOfOrder;
                    av.DateApproved = DateTime.Now;
                    av.ApprovedBy = Membership.GetCurrentLoggedInUser().UserID;

                    foreach (PurchaseOrderItem item in po.PurchaseOrderItems)
                    {
                        // generate stocklog for each poItem
                        StockLog log = new StockLog();
                        log.AdjustmentVoucher = av;
                        log.Balance = item.Stationery.QuantityInHand;
                        log.Quantity = item.QuantityToOrder;
                        log.Reason = "Supplier - " + po.Supplier.CompanyName;
                        log.StationeryID = item.StationeryID;
                        log.Type = 3;           // "replenishment" accroding to enu in DAL.AdjustmentVoucherDAO
                        log.Price = item.Price;
                        log.DateCreated = DateTime.Now;
                        av.StockLogs.Add(log);

                        // update replensih stationery stock
                         using (CatalogManager cm = new CatalogManager())
                        {
                            item.Stationery.QuantityInHand = item.Stationery.QuantityInHand + item.QuantityToOrder;
                            cm.UpdateStationery(item.Stationery);
                        }
                    }
                    avm.CreateAdjustmentVoucher(av);
                }
            }

            Response.Redirect("ViewPurchaseOrder.aspx");
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
            {
                AdjustmentVoucherTransaction tran = new AdjustmentVoucherTransaction();

                tran.VoucherNumber = avm.GenerateVoucherNumber();
                tran.DateIssued = DateTime.Now;
                tran.CreatedBy = Membership.GetCurrentLoggedInUser().UserID;

                foreach (StockLogTransaction adj in adjustments)
                {
                    adj.AdjustmentVoucherTransaction = tran;
                    tran.StockLogTransactions.Add(adj);
                }

                avm.CreateAdjustmentVoucherTransaction(tran);
            }

            Session["adjustments"] = null;
        }
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            try
            {
                AdjustmentVoucherTransaction adjustmentVoucher = new AdjustmentVoucherTransaction();
                for (int i = 0; i < DisbursementGridView.Rows.Count; i++)
                {
                    GridViewRow gridViewRow = DisbursementGridView.Rows[i];
                    int disbursementItemID = (int)DisbursementGridView.DataKeys[gridViewRow.RowIndex].Value;
                    DisbursementItem item = null;
                    using (DisbursementManager dm = new DisbursementManager())
                    {
                        item = dm.FindDisbursementItemByID(disbursementItemID);
                    }

                    if (item != null && item.QuantityDamaged > 0)
                    {
                        Stationery stationery = item.Stationery;

                        StockLogTransaction adj = new StockLogTransaction();
                        adj.Reason = item.Reason;
                        adj.Quantity = item.QuantityDamaged ?? 0;

                        if (item.StationeryID != null && item.StationeryID > 0)
                        {
                            adj.StationeryID = item.StationeryID;
                            StationeryPrice price = stationery.StationeryPrices.First();
                            adj.Price = price.Price;
                            adj.Balance = stationery.QuantityInHand;
                        }
                        else
                        {
                            adj.SpecialStationeryID = item.SpecialStationeryID;
                            adj.Price = 0.0m;
                            adj.Balance = 0;
                        }

                        adj.DateCreated = DateTime.Now;
                        adj.Type = (int)AdjustmentType.Damage;
                        adjustmentVoucher.StockLogTransactions.Add(adj);
                    }
                    if (adjustmentVoucher.StockLogTransactions.Count > 0)
                    {
                        using (TransactionScope ts = new TransactionScope())
                        {
                            using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
                            {
                                adjustmentVoucher.VoucherNumber = avm.GenerateVoucherNumber();
                                adjustmentVoucher.DateIssued = DateTime.Now;
                                adjustmentVoucher.CreatedBy = Utilities.Membership.LoggedInuser.UserID;
                                avm.CreateAdjustmentVoucherTransaction(adjustmentVoucher);
                            }
                            using (DisbursementManager dm = new DisbursementManager())
                            {
                                DAL.Disbursement disbursement = dm.FindDisbursementByID(this.DisbursementId);
                                disbursement.IsDistributed = true;
                                dm.UpdateDisbursement(disbursement);
                            }
                            ts.Complete();
                        }
                        Response.Redirect("~/HandleRequest/Disbursements.aspx");
                    }
                    else
                    {
                        using (TransactionScope ts = new TransactionScope())
                        {
                            using (DisbursementManager dm = new DisbursementManager())
                            {
                                DAL.Disbursement disbursement = dm.FindDisbursementByID(this.DisbursementId);
                                disbursement.IsCompleted = true;
                                dm.UpdateDisbursement(disbursement);
                            }
                            ts.Complete();
                        }
                        Response.Redirect("~/HandleRequest/Disbursements.aspx");
                    }
                }
            }
            catch (Exception exception)
            {
                this.ErrorMessage.Text = exception.Message;
            }
        }