protected void ButtonDelete_Click(object sender, EventArgs e)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         PurchaseOrderItem item = pom.FindPurchaseOrderItemByID(Convert.ToInt32(TextBox2.Text.ToString()));
         pom.DeletePurchaseOrderItem(item);
     }
 }
 // test findPOItemByPOItemID
 protected void Button2_Click(object sender, EventArgs e)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         PurchaseOrderItem item = pom.FindPurchaseOrderItemByID(Convert.ToInt32(TextBox2.Text.ToString()));
         item.QuantityToOrder = 80;
         PurchaseOrderItem itemUpdate = pom.UpdatePurchaseOrderItem(item);
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         List<PurchaseOrder> pos = pom.FindAllPurchaseOrder();
         this.GridView1.DataSource = pos;
         this.GridView1.DataBind();
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         List<PurchaseOrderItem> items = pom.FindAllPurchaseOrderItem();
         GridView1.DataSource = items;
         GridView1.DataBind();
     }
 }
 //protected void gvPurchaseOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
 //{
 //    this.gvPurchaseOrder.PageIndex = e.NewPageIndex;
 //    DataBindPurchaseOrdersGridView(criteria);
 //}
 protected void DataBindPurchaseOrdersGridView(PurchaseOrderSearchDTO criteria)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         List<PurchaseOrder> orders = pom.FindPurchaseOrderByCriteria(criteria);
         this.gvPurchaseOrder.DataSource = orders;
         this.gvPurchaseOrder.DataBind();
     }
 }
 protected void ButtonUpdate_Click(object sender, EventArgs e)
 {
     PurchaseOrderSearchDTO dto = new PurchaseOrderSearchDTO();
     dto.SupplierID = 3;
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         List<PurchaseOrder> po = pom.FindPurchaseOrderByCriteria(dto);
         po[0].DONumber = "88888";
         PurchaseOrder poUpdated = pom.UpdatePurchaseOrder(po[0]);
     }
 }
        protected void ButtonFind_Click(object sender, EventArgs e)
        {
            PurchaseOrderSearchDTO criteria = new PurchaseOrderSearchDTO();

            criteria.SupplierID = Convert.ToInt32(TextBoxSupplier.Text.ToString());
            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                List<PurchaseOrder> pos = pom.FindPurchaseOrderByCriteria(criteria);
                this.GridView1.DataSource = pos;
                this.GridView1.DataBind();
            }
        }
        // test findPOItemByCriteria - POID
        protected void Button1_Click(object sender, EventArgs e)
        {
            PurchaseOrderItemSearchDTO criteria = new PurchaseOrderItemSearchDTO();
            criteria.PurchaseOrderID = Convert.ToInt32(TextBox1.Text.ToString());

            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                List<PurchaseOrderItem> items = pom.FindPurchaseOrderItemByCriteria(criteria);
                GridView1.DataSource = items;
                GridView1.DataBind();
            }
        }
 //test CreatPOItem
 protected void ButtonAddNew_Click(object sender, EventArgs e)
 {
     using (PurchaseOrderManager pom = new PurchaseOrderManager())
     {
         PurchaseOrderItem item = pom.FindPurchaseOrderItemByID(509);
         PurchaseOrderItem newItem = new PurchaseOrderItem();
         newItem.QuantityToOrder = item.QuantityToOrder;
         newItem.StationeryID = item.StationeryID;
         newItem.PurchaseOrderID = item.PurchaseOrderID;
         newItem.Price = item.Price;
         newItem.DeliveryRemarks = item.DeliveryRemarks;
         PurchaseOrderItem newItem1 = pom.CreatePurchaseOrderItem(newItem);
     }
 }
        // create Purchase Order and together with Purchase Order Items
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //justify how many suppliers involed
            List<Supplier> suppliers = SupplierInvolved();

            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                foreach (Supplier s in suppliers)
                {
                    PurchaseOrder purchaseOrder = new PurchaseOrder();

                    purchaseOrder.PONumber = pom.CreatePONumber();
                    purchaseOrder.DateOfOrder = DateTime.Now;
                    purchaseOrder.AttentionTo = Convert.ToInt32(ddlAttentionTo.SelectedValue);
                    purchaseOrder.CreatedBy = Membership.GetCurrentLoggedInUser().UserID;
                    purchaseOrder.IsDelivered = false;
                    DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
                    dtfi.ShortDatePattern = "MM/dd/yyyy";
                    dtfi.DateSeparator = "/";

                    purchaseOrder.DateToSupply = Convert.ToDateTime(txtDateToSupply.Text, dtfi);
                    purchaseOrder.SupplierID = s.SupplierID;

                    foreach (GridViewRow r in gvPOItems.Rows)
                    {
                        if (Convert.ToInt32(((DropDownList)r.FindControl("ddlSupplier")).SelectedValue.ToString()) == s.SupplierID)
                        {
                            PurchaseOrderItem item = new PurchaseOrderItem();
                            item.PurchaseOrder = purchaseOrder;
                            item.StationeryID = (int)gvPOItems.DataKeys[r.RowIndex].Value;
                            //item.QuantityToOrder = 5;
                            item.QuantityToOrder = Convert.ToInt32(((TextBox)r.FindControl("txtRecommend")).Text.ToString());
                            using (CatalogManager cm = new CatalogManager())
                            {
                                StationeryPriceSearchDTO criteria = new StationeryPriceSearchDTO();
                                criteria.SupplierID = Convert.ToInt32(((DropDownList)r.FindControl("ddlSupplier")).SelectedValue.ToString());
                                criteria.StationeryID = (int)item.StationeryID;
                                item.Price = cm.FindStationeryPricesByCriteria(criteria)[0].Price;

                                // record supplier ID for the PO
                                purchaseOrder.SupplierID = criteria.SupplierID;
                            }
                            purchaseOrder.PurchaseOrderItems.Add(item); // only this way works
                        }
                    }
                    pom.CreatePurchaseOrder(purchaseOrder);
                }
            }
        }
        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 gvPOItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int specialStationeryID = (int)DataBinder.Eval(e.Row.DataItem, "SpecialStationeryID");
                if (specialStationeryID != 0)
                {
                    TextBox tb = e.Row.FindControl("txtOrderQuantity") as TextBox;
                    Literal requestedQuantity = e.Row.FindControl("ltlQuantity") as Literal;

                    using (PurchaseOrderManager pom = new PurchaseOrderManager())
                    {
                        requestedQuantity.Text = pom.GetQuantityToOrderSpecial(specialStationeryID).ToString();
                        tb.Text = requestedQuantity.Text;
                    }
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            PurchaseOrderSearchDTO criteria = new PurchaseOrderSearchDTO();
            criteria.SupplierID = Convert.ToInt32(ddlSupplier.SelectedValue);
            if (txtPONumber.Text != string.Empty)
                criteria.PONumber = txtPONumber.Text;
            if (txtStartDateOfOrder.Text != string.Empty)
                criteria.StartDateOfOrder = Convert.ToDateTime(txtStartDateOfOrder.Text.ToString());
            if (txtEndDateOfOrder.Text != string.Empty)
                criteria.EndDateOfOrder = Convert.ToDateTime(txtEndDateOfOrder.Text.ToString());

            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                List<PurchaseOrder> purchaseOrders = pom.FindPurchaseOrderByCriteria(criteria);
                gvPurchaseOrder.DataSource = purchaseOrders;
                gvPurchaseOrder.DataBind();
            }
        }
        private void Populate()
        {
            if (Request.QueryString["ID"] != "")
            {
                int purchaseOrderID = int.Parse(Request.QueryString["ID"]);
                using (PurchaseOrderManager pom = new PurchaseOrderManager())
                {
                    PurchaseOrder po = pom.FindPurchaseOrderByID(purchaseOrderID);
                    List<PurchaseOrderItem> items = po.PurchaseOrderItems.ToList<PurchaseOrderItem>();
                    this.gvPOitems.DataSource = items;
                    this.gvPOitems.DataBind();

                    lblPONumber.Text = po.PONumber;
                    lblSupplier.Text = po.Supplier.CompanyName;
                    lblOrderDate.Text = po.DateOfOrder.ToShortDateString();
                    txtReceivedDate.Text = DateTime.Now.ToShortDateString();
                    lblReceivedBy.Text = Membership.GetCurrentLoggedInUser().UserName;
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<Supplier> suppliers = SupplierInvolved();

            using (PurchaseOrderManager pom = new PurchaseOrderManager())
            {
                foreach (Supplier s in suppliers)
                {
                    PurchaseOrder purchaseOrder = new PurchaseOrder();

                    purchaseOrder.PONumber = pom.CreatePONumber();
                    purchaseOrder.DateOfOrder = DateTime.Now;
                    purchaseOrder.AttentionTo = Convert.ToInt32(ddlAttentionTo.SelectedValue);
                    purchaseOrder.CreatedBy = Membership.GetCurrentLoggedInUser().UserID;
                    purchaseOrder.IsDelivered = false;
                    DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
                    dtfi.ShortDatePattern = "MM/dd/yyyy";
                    dtfi.DateSeparator = "/";

                    purchaseOrder.DateToSupply = Convert.ToDateTime(txtDateToSupply.Text, dtfi);
                    purchaseOrder.SupplierID = s.SupplierID;

                    foreach (GridViewRow r in gvPOItems.Rows)
                    {
                        if (Convert.ToInt32(((DropDownList)r.FindControl("ddlSupplier")).SelectedValue.ToString()) == s.SupplierID)
                        {
                            PurchaseOrderItem item = new PurchaseOrderItem();
                            item.PurchaseOrder = purchaseOrder;
                            item.SpecialStationeryID = (int)gvPOItems.DataKeys[r.RowIndex].Value;
                            item.QuantityToOrder = Convert.ToInt32(((TextBox)r.FindControl("txtOrderQuantity")).Text.ToString());
                            item.Price = 5.0m;
                            purchaseOrder.SupplierID = Convert.ToInt32(((DropDownList)r.FindControl("ddlSupplier")).SelectedValue);
                            purchaseOrder.PurchaseOrderItems.Add(item); // only this way works
                        }
                    }
                    pom.CreatePurchaseOrder(purchaseOrder);
                }
            }
        }
        protected void Populate()
        {
            if (Request.QueryString["ID"] != "")
            {
                int purchaseOrderID = int.Parse(Request.QueryString["ID"]);
                using (PurchaseOrderManager pom = new PurchaseOrderManager())
                {
                    PurchaseOrder po = pom.FindPurchaseOrderByID(purchaseOrderID);
                    lblPONumber.Text = po.PONumber;
                    lblOrderDate.Text = po.DateOfOrder.ToShortDateString();
                    lblDateToSupply.Text = po.DateToSupply.ToShortDateString();
                    lblStatus.Text = (po.IsDelivered  ? "Delivered" : "Outstanding");
                    lblSupplier.Text = po.Supplier.CompanyName;
                    List<PurchaseOrderItem> items = po.PurchaseOrderItems.ToList<PurchaseOrderItem>();
                    this.gvPODetails.DataSource = items;
                    this.gvPODetails.DataBind();

                    // enable this button to go to replenish stock page when this PO has not yet been delivered
                    btnReplenish.Enabled = (po.IsDelivered ? false : true);
                }
            }
        }
        // to show recommended order quantity
        protected void gvPOItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int stationeryID = (int)DataBinder.Eval(e.Row.DataItem, "StationeryID");
                if (stationeryID != 0)
                {
                    TextBox tb = e.Row.FindControl("txtRecommend") as TextBox;
                    DropDownList SupplierDrowDownList = e.Row.FindControl("ddlSupplier") as DropDownList;
                    List<Supplier> suppliers = new List<Supplier>();

                    using (CatalogManager cm = new CatalogManager())
                    {
                        List<StationeryPrice> prices = cm.GetStationeryPricesByStationeryID(stationeryID);
                        Stationery stationery = cm.FindStationeryByID(stationeryID);
                        foreach (StationeryPrice p in prices)
                        {
                            suppliers.Add(p.Supplier);
                        }
                        SupplierDrowDownList.DataSource = suppliers;
                        SupplierDrowDownList.DataBind();

                        if (tb != null)
                        {
                            using (PurchaseOrderManager pom = new PurchaseOrderManager())
                            {
                                tb.Text = (pom.GetQuantityToOrder(stationeryID) - stationery.QuantityInHand
                                    + stationery.ReorderLevel + stationery.ReorderQuantity).ToString();
                            }
                        }
                    }
                }
            }
        }