private void ApproveSingleAdj(int AdjID)
        {
            using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
            {
                AdjustmentVoucherTransaction tran = avm.GetAdjustmentVoucherTransactionByID(AdjID);
                SA33.Team12.SSIS.DAL.AdjustmentVoucher voucher = new SA33.Team12.SSIS.DAL.AdjustmentVoucher();

                voucher.CreatedBy = tran.CreatedBy;
                voucher.ApprovedBy = Membership.GetCurrentLoggedInUser().UserID;
                voucher.DateApproved = DateTime.Now;
                voucher.DateIssued = tran.DateIssued;
                voucher.VoucherNumber = tran.VoucherNumber;
                avm.DeleteAdjustmentVoucherTransaction(tran);
                foreach (StockLogTransaction logTran in tran.StockLogTransactions)
                {
                    StockLog log = new StockLog();
                    log.AdjustmentVoucher = voucher;
                    log.Balance = logTran.Balance;
                    log.Quantity = logTran.Quantity;
                    log.Reason = logTran.Reason;
                    log.StationeryID = logTran.StationeryID;
                    log.Type = logTran.Type;
                    log.Price = logTran.Price;
                    log.DateCreated = DateTime.Now;
                    avm.CreateStockLog(log);
                    avm.DeleteStockLogTransaction(logTran);
                }
                avm.CreateAdjustmentVoucher(voucher);
              //        UtilityFunctions.SendEmail(voucher.AdjustmentVoucherID + " - Your adjustment voucher has been approved", "Dear " + voucher.CreatedByUser.FirstName + "<br />" + "Your request has been approved.", voucher.ApprovedByUser);

            }
            Populate();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int stationeryID = int.Parse(ddlDescription.SelectedValue);

            using (CatalogManager cm = new CatalogManager())
            {
                Stationery stationery = cm.FindStationeryByID(stationeryID);
                List<Stationery> stationeries = new List<Stationery>();
                stationeries.Add(stationery);
                dvStockCard.DataSource = stationeries;
                dvStockCard.DataBind();
            }

            using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
            {

                List<StockLogTransaction> trans
                    =
                    avm.GetAllStockLogTransactionByCriteria(new AdjustmentVoucherTransactionSearchDTO { StationeryID = stationeryID });
                this.gvTransactions.DataSource = trans;
                this.gvTransactions.DataBind();
            }

            btnPrint.Enabled = true;
            btnPrint.Visible = true;
        }
 protected void Page_Load(object sender, EventArgs e)
 {
     using (AdjustmentVoucherManager adjm = new AdjustmentVoucherManager())
     {
         List<StockLogTransaction> adj = adjm.FindAllStockLogTransaction();
         this.GridView1.DataSource = adj;
         this.GridView1.DataBind();
     }
 }
        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");
        }
        //Test Search by Criteria
        protected void ButtonFind_Click(object sender, EventArgs e)
        {
            AdjustmentVoucherTransactionSearchDTO criteria = new AdjustmentVoucherTransactionSearchDTO();

            if (rbnStockLogID.Checked)
            {
                criteria.StockLogTransactionID = Convert.ToInt32(TextBox1.Text.ToString());
            }

            if (rbnAdjVoucherTran.Checked)
            {
                criteria.AdjustmentVoucherTransactionID = Convert.ToInt32(TextBox1.Text.ToString());
            }

            if (rbnStationeryID.Checked)
            {
                criteria.StationeryID = Convert.ToInt32(TextBox1.Text.ToString());
            }

            if (rbnType.Checked)
            {
                criteria.Type = Convert.ToInt32(TextBox1.Text.ToString());
            }

            if (rbnReason.Checked)
            {
                criteria.Reason = TextBox1.Text.ToString();
            }

            if (rbnQty.Checked)
            {
                criteria.Quantity = Convert.ToInt32(TextBox1.Text.ToString());
            }

            if (rbnBal.Checked)
            {
                criteria.Balance = Convert.ToInt32(TextBox1.Text.ToString());
            }

            using (AdjustmentVoucherManager adjm = new AdjustmentVoucherManager())
            {
                List<StockLogTransaction> slt = adjm.GetAllStockLogTransactionByCriteria(criteria);
                this.GridView1.DataSource = slt;
                this.GridView1.DataBind();
            }
        }
        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 gvAdjustmentItems_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         int stationeryID = (int)DataBinder.Eval(e.Row.DataItem, "StationeryID");
         DAL.AdjustmentType adjType =  (AdjustmentType) DataBinder.Eval(e.Row.DataItem, "Type");
         if (stationeryID != 0)
         {
             Literal ltl = e.Row.FindControl("ltlDescription") as Literal;
             Literal type = e.Row.FindControl("ltlType") as Literal;
             if (ltl != null)
             {
                 using (CatalogManager cm = new CatalogManager())
                 {
                     Stationery s = cm.FindStationeryByID(stationeryID);
                     if (s != null) ltl.Text = s.Description;
                 }
             }
             if (type != null)
                 using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
                 {
                     type.Text = adjType.ToString();
                 }
         }
     }
 }
        //To write the data in the table into database
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ltMessage.Text != "")
            {
                ltMessage.Text = "Data saved already saved. No further action taken";
            }
            if (ltMessage.Text == "")
            {
                using (AdjustmentVoucherManager adjustmentVoucherManager = new AdjustmentVoucherManager())
                {
                    AdjustmentVoucherTransaction adjustmentVoucherTransaction = new AdjustmentVoucherTransaction();

                    foreach (GridViewRow r in GridView1.Rows)
                    {
                        StockLogTransaction item = new StockLogTransaction();
                        item.AdjustmentVoucherTransactionID = adjustmentVoucherTransaction.AdjustmentVoucherTransactionID;
                        item.StationeryID = Convert.ToInt32(r.Cells[1].Text);
                        item.Type = Convert.ToInt32(r.Cells[3].Text.ToString());
                        item.Quantity = Convert.ToInt32(r.Cells[4].Text);
                        item.Reason = r.Cells[5].Text;

                        adjustmentVoucherManager.CreateStockLogTransaction(item);

                        adjustmentVoucherTransaction.DateIssued = DateTime.Now;
                        adjustmentVoucherTransaction.VoucherNumber = "ME001"; //Must be unique system generated number
                        adjustmentVoucherTransaction.CreatedBy = 1; //Must be the userid of the person who creates it

               //         AdjustmentVoucherTransaction newAdjustmentVoucherTransaction = adjustmentVoucherManager.CreateAdjustmentVoucherTransaction(adjustmentVoucherTransaction);

                        ltMessage.Text = "Data saved";
                    }
                }
            }
        }
        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;
            }
        }
 private void Populate()
 {
     using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
     {
         List<AdjustmentVoucherTransaction> trans = avm.GetAllAdjustmentVoucherTransaction();
         gvAdjustments.DataSource = trans;
         gvAdjustments.DataBind();
     }
 }
        private void Populate()
        {
            if (Request.QueryString["ID"] != "")
            {
                int adjID = int.Parse(Request.QueryString["ID"]);
                using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
                {
                    AdjustmentVoucher tran = avm.FindAdjustmentVoucherByID(adjID);
                    this.gvAdjustmentItems.DataSource = tran.StockLogs.ToList<StockLog>();
                    this.gvAdjustmentItems.DataBind();

                    lblVoucherNumber.Text = tran.VoucherNumber;
                    lblIssueDate.Text = tran.DateIssued.ToShortDateString();
                    using (UserManager um = new UserManager())
                    {
                        User u = um.GetUserByID(tran.CreatedBy);
                        lblCreatedBy.Text = u.UserName;
                    }
                    decimal totalCost = avm.GetTotalCostVoucher(tran);
                    lblCost.Text = String.Format("{0:C}", totalCost);
                }
            }
        }
 private void RejectSingleAdj(int AdjID)
 {
     using (AdjustmentVoucherManager avm = new AdjustmentVoucherManager())
     {
         AdjustmentVoucherTransaction tran = avm.GetAdjustmentVoucherTransactionByID(AdjID);
         avm.DeleteAdjustmentVoucherTransaction(tran);
         foreach (StockLogTransaction logTran in tran.StockLogTransactions)
         {
             avm.DeleteStockLogTransaction(logTran);
         }
         //using (UserManager um = new UserManager())
         //{
         //    User u = um.GetUserByID(tran.CreatedBy);
         //    UtilityFunctions.SendEmail("Voucher Number " + tran.VoucherNumber + " has been Rejected", "Dear "
         //        + u.UserName + "<br />" + "Your adjustment Voucher has been Rejected. Reason:" + "<br />" + txtReason.Text, u);
         //}
     }
     Response.Redirect("~/Stock/ApproveAdjustmentVoucher.aspx");
 }