public AdjustmentVoucherTransaction CreateAdjustmentVoucherTransaction(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
 {
     try
     {
         adjustmentVoucherDAO.CreateAdjustmentVoucherTransaction(adjustmentVoucherTransaction);
     }
     catch (Exception ex)
     {
         throw new Exception("Adjustment Voucher Transaction Creation Failed" + ex.Message);
     }
     return adjustmentVoucherTransaction;
 }
        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;
        }
        public void CreateAdjustmentVoucherTransaction(AdjustmentVoucherTransaction AdjustmentVoucherTransaction)
        {
            try
            {
                //Create a transaction scope
                using (TransactionScope ts = new TransactionScope())
                {
                    //Add adjustmentVoucherTransaction to context
                    context.AddToAdjustmentVoucherTransactions(AdjustmentVoucherTransaction);

                    //Save the changes
                    context.SaveChanges();

                    //Notify Transaction completed
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                //Exception thrown incase if insert fails
                throw new AdjustmentVoucherException("Create Adjustment Voucher Transaction failed." + ex.Message);
            }
        }
        public void UpdateAdjustmentVoucherTransaction(AdjustmentVoucherTransaction updateAdjustmentVoucherTransaction)
        {
            try
            {
                var tempReq = (from r in context.AdjustmentVoucherTransactions
                               where r.AdjustmentVoucherTransactionID == updateAdjustmentVoucherTransaction.AdjustmentVoucherTransactionID
                               select r).FirstOrDefault<AdjustmentVoucherTransaction>();

                tempReq = updateAdjustmentVoucherTransaction;
                context.SaveChanges();
                //                return (updateAdjustmentVoucherTransaction);
            }
            catch (Exception ex)
            {
                throw new AdjustmentVoucherException("Update Adjustment Voucher failed." + ex.Message);

            }
        }
        public void DeleteAdjustmentVoucherTransaction(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
        {
            try
            {
                AdjustmentVoucherTransaction persistedTransaction = (from t in context.AdjustmentVoucherTransactions
                                              where t.AdjustmentVoucherTransactionID == adjustmentVoucherTransaction.AdjustmentVoucherTransactionID
                                                                  select t).First<AdjustmentVoucherTransaction>();

                using (TransactionScope ts = new TransactionScope())
                {
                    persistedTransaction.StockLogTransactions.Load();
                    context.AdjustmentVoucherTransactions.DeleteObject(persistedTransaction);
                    context.SaveChanges();
                    ts.Complete();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 public void DeleteAdjustmentVoucherTransaction(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
 {
     adjustmentVoucherDAO.DeleteAdjustmentVoucherTransaction(adjustmentVoucherTransaction);
 }
 public decimal GetTotalCost(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
 {
     decimal totalCost = 0;
     foreach (StockLogTransaction logTran in adjustmentVoucherTransaction.StockLogTransactions)
     {
         totalCost += logTran.Quantity * logTran.Price;
     }
     return totalCost;
 }
 public void UpdateAdjustmentVoucherTransaction(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
 {
     try
     {
         adjustmentVoucherDAO.UpdateAdjustmentVoucherTransaction(adjustmentVoucherTransaction);
     }
     catch (Exception ex)
     {
         throw new Exception("Adjustment Voucher Transaction Update Failed" + ex.Message);
     }
 }
        //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 UpdateRetrievedQuantity()
        {
            GridView StationeryRetrievalFormItemGridView =
                StationeryRetrievalFormView.FindControl("StationeryRetrievalFormItemGridView") as GridView;

            if (StationeryRetrievalFormItemGridView != null)
            {
                try
                {
                    int srfID = Convert.ToInt32(this.StationeryRetrievalFormView.DataKey.Value);

                    AdjustmentVoucherTransaction adj = new AdjustmentVoucherTransaction();
                    List<Stationery> stationeries = new List<Stationery>();
                    List<SpecialStationery> specialStationeries = new List<SpecialStationery>();

                    using (StationeryRetrievalManager srm = new StationeryRetrievalManager())
                    {
                        DAL.StationeryRetrievalForm srf = srm.GetStationeryRetrievalFormByID(srfID);
                        List<StationeryRetrievalFormItem> srfis = srf.StationeryRetrievalFormItems.ToList();
                        foreach (GridViewRow row in StationeryRetrievalFormItemGridView.Rows)
                        {
                            HiddenField StationeryRetrievalFormItemIDHiddenField =
                                row.FindControl("StationeryRetrievalFormItemIDHiddenField") as HiddenField;
                            int srfiID = Convert.ToInt32(StationeryRetrievalFormItemIDHiddenField.Value);
                            TextBox QtyRetrieved = row.FindControl("QuantityRetrievedTextBox") as TextBox;
                            StationeryRetrievalFormItem srfi = (from s in srfis
                                                                where s.StationeryRetrievalFormItemID == srfiID
                                                                select s).FirstOrDefault();

                            int quantityRetrieved = Convert.ToInt32(QtyRetrieved.Text);
                            srfi.QuantityRetrieved = quantityRetrieved;

                            StockLogTransaction stockLogTransaction = new StockLogTransaction();
                            stockLogTransaction.Reason = "";
                            stockLogTransaction.Quantity = quantityRetrieved;

                            if (srfi.Stationery != null)
                            {
                                Stationery stationery = srfi.Stationery;
                                stockLogTransaction.StationeryID = srfi.Stationery.StationeryID;
                                StationeryPrice price = stationery.StationeryPrices.First();
                                stockLogTransaction.Price = price.Price;
                                stationery.QuantityInHand -= quantityRetrieved;
                                stationeries.Add(stationery);
                             stockLogTransaction.Balance = stationery.QuantityInHand;
                           }
                            else
                            {
                                SpecialStationery specialStationery = srfi.SpecialStationery;
                                stockLogTransaction.SpecialStationeryID = srfi.SpecialStationery.SpecialStationeryID;
                                stockLogTransaction.Price = 0.0m;
                                specialStationery.Quantity -= quantityRetrieved;
                                stockLogTransaction.Balance = specialStationery.Quantity;
                                specialStationeries.Add(specialStationery);
                            }

                            stockLogTransaction.DateCreated = DateTime.Now;
                            stockLogTransaction.Type = (int) AdjustmentType.Consumption;
                            adj.StockLogTransactions.Add(stockLogTransaction);

                        }
                        StationeryRetrievalForm newSRF = srm.UpdateReceivedQuantity(srf);
                        newSRF = srm.SetRecommendedQuantity(newSRF.StationeryRetrievalFormID);
                        using(CatalogManager cm = new CatalogManager())
                        {
                            foreach (Stationery stationery in stationeries)
                            {
                                cm.UpdateStationery(stationery);
                            }
                            foreach (SpecialStationery specialStationery in specialStationeries)
                            {
                                cm.UpdateSpecialStationery(specialStationery);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    this.ErrorMessage.Text = exception.Message;
                }
            }
        }
        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;
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the AdjustmentVoucherTransactions EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAdjustmentVoucherTransactions(AdjustmentVoucherTransaction adjustmentVoucherTransaction)
 {
     base.AddObject("AdjustmentVoucherTransactions", adjustmentVoucherTransaction);
 }
 /// <summary>
 /// Create a new AdjustmentVoucherTransaction object.
 /// </summary>
 /// <param name="adjustmentVoucherTransactionID">Initial value of the AdjustmentVoucherTransactionID property.</param>
 /// <param name="dateIssued">Initial value of the DateIssued property.</param>
 /// <param name="voucherNumber">Initial value of the VoucherNumber property.</param>
 /// <param name="createdBy">Initial value of the CreatedBy property.</param>
 public static AdjustmentVoucherTransaction CreateAdjustmentVoucherTransaction(global::System.Int32 adjustmentVoucherTransactionID, global::System.DateTime dateIssued, global::System.String voucherNumber, global::System.Int32 createdBy)
 {
     AdjustmentVoucherTransaction adjustmentVoucherTransaction = new AdjustmentVoucherTransaction();
     adjustmentVoucherTransaction.AdjustmentVoucherTransactionID = adjustmentVoucherTransactionID;
     adjustmentVoucherTransaction.DateIssued = dateIssued;
     adjustmentVoucherTransaction.VoucherNumber = voucherNumber;
     adjustmentVoucherTransaction.CreatedBy = createdBy;
     return adjustmentVoucherTransaction;
 }