/// <summary>
 /// Create a new PurchaseOrderDetail object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="price">Initial value of the Price property.</param>
 /// <param name="qty">Initial value of the Qty property.</param>
 public static PurchaseOrderDetail CreatePurchaseOrderDetail(global::System.Int32 id, global::System.Decimal price, global::System.Int32 qty)
 {
     PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
     purchaseOrderDetail.Id = id;
     purchaseOrderDetail.Price = price;
     purchaseOrderDetail.Qty = qty;
     return purchaseOrderDetail;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the PurchaseOrderDetails EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPurchaseOrderDetails(PurchaseOrderDetail purchaseOrderDetail)
 {
     base.AddObject("PurchaseOrderDetails", purchaseOrderDetail);
 }
        /// <summary>
        ///  Logically delete the PurchaseOrderDetail table by setting the status to 2 in the PurchaseOrderDetail table
        ///  Return Constants.DB_STATUS
        /// </summary>
        /// <param name="purchaseOrderDetail"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Delete(PurchaseOrderDetail purchaseOrderDetail)
        {
             throw new NotImplementedException();
            //Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;

            //try
            //{
            //    inventory.SaveChanges();
            //    status = Constants.DB_STATUS.SUCCESSFULL;
            //}
            //catch (Exception e)
            //{
            //    status = Constants.DB_STATUS.FAILED;
            //}

            //return status;
        }
        /// <summary>
        /// Update PurchaseOrderDetail data to the PurchaseOrderDetail Table according to the purchaseOrderDetail Parameter
        /// Return Constans.DB_STATUS
        /// </summary>
        /// <param name="purchaseOrderDetail"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Update(PurchaseOrderDetail purchaseOrderDetail)
        {

            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;

            try
            {
               PurchaseOrderDetail purchasedetail = inventory.PurchaseOrderDetails.Where(p => p.Id == purchaseOrderDetail.Id).First();
                if(!purchaseorderdetail.Equals(null))
                {
                    purchaseorder = inventory.PurchaseOrders.Where(p => p.Id == purchaseOrderDetail.PurchaseOrder.Id).First();
                    Item item = inventory.Items.Where(i => i.Id == purchaseOrderDetail.Item.Id).First();

                    purchasedetail.Id = purchaseOrderDetail.Id;
                    purchasedetail.PurchaseOrder = purchaseorder;
                    purchasedetail.Price = purchaseOrderDetail.Price;
                    purchasedetail.Qty = purchaseOrderDetail.Qty;
                    purchasedetail.AcceptedQty = purchaseOrderDetail.AcceptedQty;
                    inventory.SaveChanges();
                    status = Constants.DB_STATUS.SUCCESSFULL;
                }
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }

            return status;
        }
        /// <summary>
        ///  Insert PurchaseOrderDetail data to the PurchaseOrderDetail Table according to the newPurchaseOrderDetail Parameter
        ///  Return Constants.DB_STATUS
        /// </summary>
        /// <param name="newPurchaseOrderDetail"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Insert(PurchaseOrderDetail newPurchaseOrderDetail)
        {
            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;

            try
            {

                inventory.AddToPurchaseOrderDetails(newPurchaseOrderDetail);
                inventory.SaveChanges();
                status = Constants.DB_STATUS.SUCCESSFULL;
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }

            return status;

        }
        /// <summary>
        /// Retrieve the PurchaseOrderdetail information  from  PurchaseOrderDetail Table according to the purchaseOrderDetail Parameter
        /// Return PurchaseOrderDetail
        /// </summary>
        /// <param name="purchaseOrderDetail"></param>
        /// <returns></returns>
        public PurchaseOrderDetail GetPurchaseOrderDetail(entity.PurchaseOrderDetail purchaseOrderDetail)
        {

            purchaseorderdetail = inventory.PurchaseOrderDetails.Where(reqObj => reqObj.Id == purchaseorderdetail.Id).First();
            if (!purchaseorderdetail.Equals(null))
                return purchaseorderdetail;
            return null;
        }
        public Constants.ACTION_STATUS SelectSave(string supplierCode, string deliveryAddress, string attn, DateTime expectedDate)
        {
            Constants.ACTION_STATUS saveStatus = Constants.ACTION_STATUS.UNKNOWN;

            Supplier supplier = new Supplier();
            supplier.Id = supplierCode;
            supplier = supplierBroker.GetSupplier(supplier);
            purchaseOrder.Supplier = supplier;

            purchaseOrder.DeliverAddress = deliveryAddress;
            purchaseOrder.Attn = attn;
            purchaseOrder.ExpectedDate = expectedDate;

            PurchaseOrderDetail purchaseOrderDetail;
            int addedDetail = 0;

            // reorder item
            foreach (Item temp in reorderList)
            {
                item = new Item();
                item.Id = temp.Id;
                item = itemBroker.GetItem(item);

                purchaseOrderDetail = new PurchaseOrderDetail(purchaseOrderBroker.GetPurchaseOrderDetailId() + (addedDetail++), purchaseOrder, item, temp.Cost, temp.ReorderQty, 0);

                purchaseOrder.PurchaseOrderDetails.Add(purchaseOrderDetail);
            }

            // manually added item
            foreach (Item item in addedItemList.Keys)
            {
                purchaseOrderDetail = new PurchaseOrderDetail(purchaseOrderBroker.GetPurchaseOrderDetailId() + (addedDetail++), purchaseOrder, item, (decimal)Converter.objToDouble(addedItemList[item][1]), Converter.objToInt(addedItemList[item][0]), 0);

                purchaseOrder.PurchaseOrderDetails.Add(purchaseOrderDetail);
            }

            if (purchaseOrderBroker.Insert(purchaseOrder) == Constants.DB_STATUS.SUCCESSFULL)
            {
                saveStatus = Constants.ACTION_STATUS.SUCCESS;
            }
            else
            {
                saveStatus = Constants.ACTION_STATUS.FAIL;
            }

            return saveStatus;
        }