//
        // CountDetails
        public static string CountDetails(TimelyDepotContext db, int DetailId, string szItemId, string szDescription)
        {
            bool bStatus = false;
            int nIdSubItem = 0;
            string szHas = "0";
            UserQuotationDetail userquotationdetail = null;
            SUB_ITEM subitem = null;

            TimelyDepotContext db02 = new TimelyDepotContext();

            IQueryable<SUB_ITEM> qrySubItem = null;
            IQueryable<UserQuotationDetail> qryUsrDet = db.UserQuotationDetails.Where(usdt => usdt.DetailId == DetailId);

            if (qryUsrDet.Count() > 0)
            {
                szHas = qryUsrDet.Count().ToString();
                foreach (var item in qryUsrDet)
                {
                    userquotationdetail = db.UserQuotationDetails.Find(item.Id);
                    if (userquotationdetail != null)
                    {
                        qrySubItem = db02.SUB_ITEM.Where(sbit => sbit.ItemID == szItemId && sbit.Description == userquotationdetail.ProductType);
                        if (qrySubItem.Count() > 0)
                        {
                            subitem = qrySubItem.FirstOrDefault<SUB_ITEM>();
                            if (subitem != null)
                            {
                                nIdSubItem = subitem.Id;
                            }
                        }
                        else
                        {
                            nIdSubItem = 0;
                        }

                        if (string.IsNullOrEmpty(userquotationdetail.ItemID))
                        {
                            userquotationdetail.ItemID = nIdSubItem.ToString();
                            bStatus = true;
                        }
                    }
                }
                if (bStatus)
                {
                    db.SaveChanges();

                }
            }

            return szHas;
        }
        private void DeleteSalesOrderDetails(int id)
        {
            int nCustomerID = 0;
            int nUserID = 0;
            int nQuoteId = 0;
            string szItemID = "";
            string szEmail = "";

            //IQueryable<UserQuotation> qryQuoteUsr = null;
            IQueryable<UserQuotationDetail> qryQuoteDetails = null;
            UserQuotation userquotation = null;
            CustomersContactAddress customercontactaddress = null;

            TimelyDepotContext db01 = new TimelyDepotContext();
            //SalesOrderDetail salesorderdetails = null;

            //Get customer email
            SalesOrder salesorder = db.SalesOrders.Find(id);
            if (salesorder != null)
            {
                customercontactaddress = db01.CustomersContactAddresses.Where(cuad => cuad.CustomerId == salesorder.CustomerId).FirstOrDefault<CustomersContactAddress>();
                if (customercontactaddress != null)
                {
                    szEmail = customercontactaddress.Email;
                }
            }

            IQueryable<SalesOrderDetail> qryalesorderdetails = db.SalesOrderDetails.Where(sodt => sodt.SalesOrderId == id);
            if (qryalesorderdetails.Count() > 0)
            {
                foreach (var item in qryalesorderdetails)
                {
                    if (string.IsNullOrEmpty(szItemID))
                    {
                        szItemID = item.ItemID;
                    }

                    db.SalesOrderDetails.Remove(item);
                }

                //Restore Quotation Details
                //qryQuoteUsr = db.UserQuotations.Where(qtus => qtus.ProductId == szItemID);
                var qryQuoteUsr = db01.UserQuotations.Join(db01.UserRegistrations, usqt => usqt.UserId, usrg => usrg.RId, (usqt, usrg)
                    => new { usqt, usrg }).Where(uqus => uqus.usqt.ProductId == szItemID && uqus.usrg.UserName == szEmail);
                if (qryQuoteUsr.Count() > 0)
                {
                    //userquotation = qryQuoteUsr.FirstOrDefault<UserQuotation>();
                    //if (userquotation != null)
                    //{
                    //    nQuoteId = userquotation.Id;
                    //}

                    foreach (var item in qryQuoteUsr)
                    {
                        //Get the quoteid
                        if (nQuoteId == 0)
                        {
                            nQuoteId = item.usqt.Id;
                        }
                    }

                }

                if (nQuoteId > 0)
                {
                    qryQuoteDetails = db.UserQuotationDetails.Where(qtdt => qtdt.DetailId == nQuoteId);
                    if (qryQuoteDetails.Count() > 0)
                    {
                        foreach (var item in qryQuoteDetails)
                        {
                            item.ShippedQuantity = null;
                            item.BOQuantity = null;
                            item.Status = 0;
                            db.Entry(item).State = EntityState.Modified;

                        }
                    }

                }

                db01.SaveChanges();
            }
        }
        private void UpdateInventory(string[] szHeaders, string[] szRow, TimelyDepotContext db, ITEM item, int nPos, ref string szError)
        {
            //Asume
            // 0 = ItemId
            // 1 = PROD_CD
            // 2 = UnitperCase
            // 3 = CaseWeight
            // 4 = UnitWeight
            // 5 = CaseDimensionL
            // 6 = CaseDimensionW
            // 7 = CaseDimensionH

            string szItemId = "";
            szError = "";

            try
            {
                szItemId = szRow[0];
                item = db.ITEMs.Where(itm => itm.ItemID == szItemId).FirstOrDefault<ITEM>();
                if (item != null)
                {
                    item.UnitPerCase = szRow[2];
                    item.CaseWeight = szRow[3];
                    item.UnitWeight = szRow[4];
                    item.CaseDimensionL = szRow[5];
                    item.CaseDimensionW = szRow[6];
                    item.CaseDimensionH = szRow[7];
                    db.Entry(item).State = EntityState.Modified;
                    db.SaveChanges();
                }

            }
            catch (Exception err)
            {
                szError = string.Format("Row {0}: {1} {2}", nPos, err.Message, err.StackTrace);
            }
        }
        //
        // GET: /PurchaseOrder/ReceivePurchaseOrder
        public ActionResult ReceivePurchaseOrder(int id)
        {
            double dPO_Dr = 0;
            DateTime dDate = DateTime.Now;
            TimelyDepotContext db01 = new TimelyDepotContext();

            SUB_ITEM subitem = null;
            IQueryable<PurchasOrderDetail> qrypodetails = null;

            PurchaseOrders purchaseorders = db.PurchaseOrders.Find(id);
            if (string.IsNullOrEmpty(purchaseorders.ReceiveStatus))
            {
                //Upate inventory
                qrypodetails = db.PurchasOrderDetails.Where(podt => podt.PurchaseOrderId == id);
                if (qrypodetails.Count() > 0)
                {
                    foreach (var item in qrypodetails)
                    {
                        if (!string.IsNullOrEmpty(item.ItemID))
                        {
                            subitem = db01.SUB_ITEM.Where(sbit => sbit.ItemID == item.ItemID && sbit.Sub_ItemID == item.Sub_ItemID).FirstOrDefault<SUB_ITEM>();
                            if (subitem != null)
                            {
                                dPO_Dr = Convert.ToDouble(item.Quantity);
                                subitem.OpenPO_Db = subitem.OpenPO_Db + dPO_Dr;
                                db01.Entry(subitem).State = EntityState.Modified;
                            }

                        }
                    }
                    db01.SaveChanges();
                }

                purchaseorders.ReceiveStatus = string.Format("Received on {0}", dDate.ToString("MMM/dd/yyyy"));

                db.Entry(purchaseorders).State = EntityState.Modified;
                db.SaveChanges();

            }
            return RedirectToAction("Index");
        }
        private void DeleteUnusedVendorItem(int nVendoritemId, string szItemId)
        {
            TimelyDepotContext db01 = new TimelyDepotContext();

            VendorItem vendoritem = null;
            IQueryable<VendorItem> qryVendorItem = db.VendorItems.Where(vdit => vdit.ItemId == szItemId && vdit.Id != nVendoritemId);
            if (qryVendorItem.Count() > 0)
            {
                foreach (var item in qryVendorItem)
                {
                    vendoritem = db01.VendorItems.Find(item.Id);
                    if (vendoritem != null)
                    {
                        db01.VendorItems.Remove(vendoritem);
                    }
                }
                db01.SaveChanges();
            }
        }
        private void DeleteSalesOrderDetails(int id)
        {
            TimelyDepotContext db01 = new TimelyDepotContext();
            //SalesOrderDetail salesorderdetails = null;

            IQueryable<SalesOrderDetail> qryalesorderdetails = db.SalesOrderDetails.Where(sodt => sodt.SalesOrderId == id);
            if (qryalesorderdetails.Count() > 0)
            {
                foreach (var item in qryalesorderdetails)
                {
                    db.SalesOrderDetails.Remove(item);
                }
                db01.SaveChanges();
            }
        }