public ActionResult Create(PurchaseOrders purchaseorders)
        {
            if (ModelState.IsValid)
            {
                db.PurchaseOrders.Add(purchaseorders);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(purchaseorders);
        }
 public ActionResult Edit(PurchaseOrders purchaseorders)
 {
     if (ModelState.IsValid)
     {
         db.Entry(purchaseorders).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(purchaseorders);
 }
        public ActionResult Edit(PurchaseOrders purchaseorders, string PODateHlp, string ShipDateHlp, string RequiredDateHlp)
        {
            int nYear = 0;
            int nMonth = 0;
            int nDay = 0;

            string[] szDateHlp = null;
            string[] szShipDateHlp = null;
            string[] szRequiredDateHlp = null;

            DateTime dDate = DateTime.Now;
            DateTime dShipDate = DateTime.Now;
            DateTime dRequiredDate = DateTime.Now;

            if (!string.IsNullOrEmpty(PODateHlp))
            {
                szDateHlp = PODateHlp.Split('/');
                if (szDateHlp != null)
                {
                    nMonth = Convert.ToInt32(szDateHlp[0]);
                    nDay = Convert.ToInt32(szDateHlp[1]);
                    nYear = Convert.ToInt32(szDateHlp[2]);
                    dDate = new DateTime(nYear, nMonth, nDay);
                }
                else
                {
                    dDate = Convert.ToDateTime(purchaseorders.PODate);
                }
            }
            else
            {
                dDate = Convert.ToDateTime(purchaseorders.PODate);
            }

            if (!string.IsNullOrEmpty(ShipDateHlp))
            {
                szShipDateHlp = ShipDateHlp.Split('/');
                if (szShipDateHlp != null)
                {
                    nMonth = Convert.ToInt32(szShipDateHlp[0]);
                    nDay = Convert.ToInt32(szShipDateHlp[1]);
                    nYear = Convert.ToInt32(szShipDateHlp[2]);
                    dShipDate = new DateTime(nYear, nMonth, nDay);
                }
                else
                {
                    dShipDate = Convert.ToDateTime(purchaseorders.ShipDate);
                }
            }

            if (!string.IsNullOrEmpty(RequiredDateHlp))
            {
                szRequiredDateHlp = RequiredDateHlp.Split('/');
                if (szRequiredDateHlp != null)
                {
                    nMonth = Convert.ToInt32(szRequiredDateHlp[0]);
                    nDay = Convert.ToInt32(szRequiredDateHlp[1]);
                    nYear = Convert.ToInt32(szRequiredDateHlp[2]);
                    dRequiredDate = new DateTime(nYear, nMonth, nDay);
                }
                else
                {
                    dRequiredDate = Convert.ToDateTime(purchaseorders.RequiredDate);
                }
            }

            if (ModelState.IsValid)
            {
                if (purchaseorders.PODate != dDate)
                {
                    purchaseorders.PODate = dDate;
                }
                if (purchaseorders.ShipDate != dShipDate)
                {
                    purchaseorders.ShipDate = dShipDate;
                }
                if (purchaseorders.RequiredDate != dRequiredDate)
                {
                    purchaseorders.RequiredDate = dRequiredDate;
                }

                db.Entry(purchaseorders).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(purchaseorders);
        }
        public ActionResult Create(PurchaseOrders purchaseorders, string PODateHlp)
        {
            int nYear = 0;
            int nMonth = 0;
            int nDay = 0;
            int nCustomerId = 0;
            int nSalesOrderId = 0;
            int nTradeId = 0;
            int nPurchaseOrderId = 0;
            int nItemPosition = 0;
            decimal dSetupCharge = 0;
            decimal dRunCharge = 0;
            string szVendorNo = "";
            string szPaymentType = "";
            string szSalesOrderNo = "";
            string szCardType = "";
            string szItemId = "";
            string[] szDateHlp = null;
            string[] szSetUpRunHlp = null;

            int nHas = 0;
            int nPos = -1;
            string szError = "";
            string szDecriptedData = "";
            string szDecriptedCode = "";
            string szMsg = "";

            DateTime dDate = DateTime.Now;
            DateTime dShipDate = DateTime.Now;

            PurchasOrderDetail purchaseorderdetail = null;
            SalesOrder salesorder = null;
            SalesOrderDetail salesorderdetail = null;
            CustomersContactAddress customeraddress = null;
            CustomersShipAddress customershipto = null;
            CustomersCreditCardShipping cardshipping = null;
            Trade trade = null;
            SalesOrderBlindShip blindship = null;
            VendorItem vendoritem = null;
            SUB_ITEM subitem = null;
            Payments payment = null;

            TimelyDepotContext db02 = new TimelyDepotContext();

            List<KeyValuePair<string, string>> listSelector = new List<KeyValuePair<string, string>>();
            IQueryable<Trade> qryTrade = null;
            IQueryable<SalesOrderDetail> qrysalesdetail = null;

            if (!string.IsNullOrEmpty(PODateHlp))
            {
                szDateHlp = PODateHlp.Split('/');
                if (szDateHlp != null)
                {
                    nMonth = Convert.ToInt32(szDateHlp[0]);
                    nDay = Convert.ToInt32(szDateHlp[1]);
                    nYear = Convert.ToInt32(szDateHlp[2]);
                    dDate = new DateTime(nYear, nMonth, nDay);
                }
                else
                {
                    dDate = Convert.ToDateTime(purchaseorders.PODate);
                }
            }
            else
            {
                dDate = Convert.ToDateTime(purchaseorders.PODate);
            }

            if (ModelState.IsValid)
            {
                if (purchaseorders.PODate != dDate)
                {
                    purchaseorders.PODate = dDate;
                }

                //Get the Sales Order Data
                salesorder = db.SalesOrders.Where(slor => slor.SalesOrderNo == purchaseorders.SalesOrderNo).FirstOrDefault<SalesOrder>();
                if (salesorder != null)
                {
                    nSalesOrderId = salesorder.SalesOrderId;
                    nCustomerId = Convert.ToInt32(salesorder.CustomerId);
                    nTradeId = Convert.ToInt32(salesorder.TradeId);
                    szVendorNo = purchaseorders.VendorId;
                    szSalesOrderNo = purchaseorders.SalesOrderNo;

                    purchaseorders.PurchaseOrderReference = string.Format("{0}-{1}", salesorder.SalesOrderNo, nCustomerId.ToString());

                    if (salesorder.ShipDate != null)
                    {
                        dShipDate = Convert.ToDateTime(salesorder.ShipDate);
                        purchaseorders.ShipDate = dShipDate;
                    }

                    purchaseorders.IsBlindShip = salesorder.IsBlindShip;

                    if (purchaseorders.IsBlindShip)
                    {
                        //Get blind ship address
                        blindship = db02.SalesOrderBlindShips.Where(blsh => blsh.SalesOrderId == nSalesOrderId).FirstOrDefault<SalesOrderBlindShip>();
                        if (blindship != null)
                        {
                            purchaseorders.ToAddress1 = blindship.Address1;
                            purchaseorders.ToAddress2 = blindship.Address2;
                            purchaseorders.ToCity = blindship.City;
                            purchaseorders.ToCompany = blindship.Title;
                            purchaseorders.ToCountry = blindship.Country;
                            //purchaseorders.FromEmail = blindship.Email;
                            //purchaseorders.FromFax = blindship.Fax;
                            purchaseorders.ToName = string.Format("{0} {1}", blindship.FirstName, blindship.LastName);
                            purchaseorders.ToState = blindship.State;
                            purchaseorders.ToTel = blindship.Tel;
                            purchaseorders.ToTitle = blindship.Title;
                            purchaseorders.ToZip = blindship.Zip;
                        }
                        db02.Dispose();
                        db02 = new TimelyDepotContext();

                        customershipto = db02.CustomersShipAddresses.Where(ctsh => ctsh.CustomerId == nCustomerId).FirstOrDefault<CustomersShipAddress>();
                        if (customershipto != null)
                        {
                            purchaseorders.FromAddress1 = customershipto.Address1;
                            purchaseorders.FromAddress2 = customershipto.Address2;
                            purchaseorders.FromCity = customershipto.City;
                            purchaseorders.FromCompany = customershipto.Title;
                            purchaseorders.FromCountry = customershipto.Country;
                            purchaseorders.FromEmail = customershipto.Email;
                            purchaseorders.FromFax = customershipto.Fax;
                            purchaseorders.FromName = string.Format("{0} {1}", customershipto.FirstName, customershipto.LastName);
                            purchaseorders.FromState = customershipto.State;
                            purchaseorders.FromTel = customershipto.Tel;
                            //purchaseorders.ToTitle = trade.Title;
                            purchaseorders.FromZip = customershipto.Zip;

                            //Get the blind drop data
                            purchaseorders.BlindDrop = string.Format("{0} a/c: {1}", customershipto.ShippingPreference, customershipto.ShipperAccount);

                        }
                        db02.Dispose();
                        db02 = new TimelyDepotContext();
                    }
                    else
                    {
                        //Get customer address
                        customeraddress = db02.CustomersContactAddresses.Where(ctad => ctad.CustomerId == nCustomerId).FirstOrDefault<CustomersContactAddress>();
                        if (customeraddress != null)
                        {
                            purchaseorders.FromAddress1 = customeraddress.Address;
                            purchaseorders.FromAddress2 = customeraddress.Note;
                            purchaseorders.FromCity = customeraddress.City;
                            purchaseorders.FromCompany = customeraddress.CompanyName;
                            purchaseorders.FromCountry = customeraddress.Country;
                            purchaseorders.FromEmail = customeraddress.Email;
                            purchaseorders.FromFax = customeraddress.Fax;
                            purchaseorders.FromName = string.Format("{0} {1}", customeraddress.FirstName, customeraddress.LastName);
                            purchaseorders.FromState = customeraddress.State;
                            purchaseorders.FromTel = customeraddress.Tel;
                            purchaseorders.FromTitle = customeraddress.Title;
                            purchaseorders.FromZip = customeraddress.Zip;
                        }
                        db02.Dispose();
                        db02 = new TimelyDepotContext();

                        //Get the trade data
                        //trade = db02.Trades.Where(trd => trd.TradeId == nTradeId).FirstOrDefault<Trade>();
                        //if (trade != null)
                        //{
                        //    purchaseorders.ToAddress1 = trade.Address;
                        //    //purchaseorders.ToAddress2 = trade.WebSite;
                        //    purchaseorders.ToCity = trade.City;
                        //    purchaseorders.ToCompany = trade.TradeName;
                        //    purchaseorders.ToCountry = trade.Country;
                        //    purchaseorders.ToEmail = trade.Email;
                        //    purchaseorders.ToFax = trade.Fax;
                        //    //purchaseorders.ToName = string.Format("{0} {1}", trade., trade.LastName);
                        //    purchaseorders.ToState = trade.State;
                        //    purchaseorders.ToTel = customeraddress.Tel;
                        //    //purchaseorders.ToTitle = trade.Title;
                        //    purchaseorders.ToZip = trade.PostCode;
                        //}
                        //db02.Dispose();
                        //db02 = new TimelyDepotContext();

                        //Get the ship to address for the sales order
                        customershipto = db02.CustomersShipAddresses.Where(ctsh => ctsh.Id == salesorder.CustomerShiptoId).FirstOrDefault<CustomersShipAddress>();
                        if (customershipto != null)
                        {
                            purchaseorders.ToAddress1 = customershipto.Address1;
                            purchaseorders.ToAddress2 = customershipto.Address2;
                            purchaseorders.ToCity = customershipto.City;
                            purchaseorders.ToCompany = customershipto.Title;
                            purchaseorders.ToCountry = customershipto.Country;
                            purchaseorders.ToEmail = customershipto.Email;
                            purchaseorders.ToFax = customershipto.Fax;
                            purchaseorders.ToName = string.Format("{0} {1}", customershipto.FirstName, customershipto.LastName);
                            purchaseorders.ToState = customershipto.State;
                            purchaseorders.ToTel = customershipto.Tel;
                            //purchaseorders.ToTitle = trade.Title;
                            purchaseorders.ToZip = customershipto.Zip;

                            //Get the blind drop data
                            purchaseorders.BlindDrop = string.Format("{0} a/c: {1}", customershipto.ShippingPreference, customershipto.ShipperAccount);

                        }
                        db02.Dispose();
                        db02 = new TimelyDepotContext();

                        customershipto = db02.CustomersShipAddresses.Where(ctsh => ctsh.CustomerId == nCustomerId).FirstOrDefault<CustomersShipAddress>();
                        if (customershipto != null)
                        {

                            //Get the blind drop data
                            purchaseorders.BlindDrop = string.Format("{0} a/c: {1}", customershipto.ShippingPreference, customershipto.ShipperAccount);

                        }
                        db02.Dispose();
                        db02 = new TimelyDepotContext();

                    }

                }

                //Get the pay by data
                payment = db.Payments.Where(pmt => pmt.SalesOrderNo == szSalesOrderNo).FirstOrDefault<Payments>();
                if (payment != null)
                {
                    szPaymentType = payment.PaymentType;
                    if (!string.IsNullOrEmpty(szPaymentType))
                    {
                        cardshipping = db.CustomersCreditCardShippings.Where(cdsp => cdsp.CreditNumber == szPaymentType && cdsp.CustomerId == nCustomerId).FirstOrDefault<CustomersCreditCardShipping>();
                        if (cardshipping != null)
                        {
                            szCardType = cardshipping.CardType;
                            if (!string.IsNullOrEmpty(szCardType))
                            {
                                szError = "";
                                szDecriptedData = TimelyDepotMVC.Controllers.PaymentController.DecodeInfo02(szPaymentType, ref szError);
                                if (!string.IsNullOrEmpty(szError))
                                {
                                    nPos = szError.IndexOf("data to decode");
                                    if (nPos != -1)
                                    {
                                        szDecriptedData = string.Empty;
                                    }
                                    else
                                    {
                                        szDecriptedData = string.Format("******");
                                    }
                                }
                                else
                                {
                                    //Mask the card number
                                    nHas = szDecriptedData.Length;
                                    if (nHas > 4)
                                    {
                                        szMsg = szDecriptedData.Substring(nHas - 4, 4);
                                        szDecriptedData = string.Format("******{0}", szMsg);
                                    }
                                    else
                                    {
                                        szDecriptedData = string.Format("******");
                                    }
                                }

                                purchaseorders.PaidBy = string.Format("{0} {1}", szCardType, szDecriptedData);
                            }
                        }
                    }
                }

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

                //Get the detail
                nPurchaseOrderId = purchaseorders.PurchaseOrderId;

                qrysalesdetail = db.SalesOrderDetails.Where(sldt => sldt.SalesOrderId == nSalesOrderId).OrderBy(sldt => sldt.ItemPosition).ThenBy(sldt => sldt.ItemOrder);
                if (qrysalesdetail.Count() > 0)
                {
                    foreach (var item in qrysalesdetail)
                    {

                        //Get the vendors data
                        if (item.ItemID != null)
                        {
                            if (string.IsNullOrEmpty(szItemId))
                            {
                                szItemId = item.ItemID;
                            }
                            purchaseorderdetail = new PurchasOrderDetail();
                            purchaseorderdetail.PurchaseOrderId = nPurchaseOrderId;
                            purchaseorderdetail.ItemID = item.ItemID;
                            purchaseorderdetail.Sub_ItemID = item.Sub_ItemID;
                            purchaseorderdetail.Description = item.Description;

                            nPos = -1;
                            nPos = item.Description.IndexOf("Set up");
                            if (nPos != -1)
                            {
                                szSetUpRunHlp = item.Description.Split(' ');
                                if (szSetUpRunHlp.Length > 0)
                                {
                                    purchaseorderdetail.Description = string.Format("Set up Charge {0} {1}", nPurchaseOrderId.ToString(), szSetUpRunHlp[4]);
                                }
                            }
                            nPos = -1;
                            szSetUpRunHlp = null;
                            nPos = item.Description.IndexOf("Run Charge");
                            if (nPos != -1)
                            {
                                szSetUpRunHlp = item.Description.Split(' ');
                                if (szSetUpRunHlp.Length > 0)
                                {
                                    purchaseorderdetail.Description = string.Format("Run Charge {0} {1}", nPurchaseOrderId.ToString(), szSetUpRunHlp[3]);
                                }
                            }

                            purchaseorderdetail.Quantity = item.Quantity;
                            purchaseorderdetail.UnitPrice = item.UnitPrice;
                            purchaseorderdetail.Tax = item.Tax;

                            purchaseorderdetail.ItemPosition = item.ItemPosition;
                            purchaseorderdetail.ItemOrder = item.ItemOrder;
                            nItemPosition = Convert.ToInt32(item.ItemPosition);

                            vendoritem = db02.VendorItems.Where(vdit => vdit.VendorNo == szVendorNo && vdit.ItemId == item.ItemID).FirstOrDefault<VendorItem>();
                            if (vendoritem != null)
                            {
                                if (purchaseorders.IsBlindShip)
                                {
                                    purchaseorderdetail.UnitPrice = vendoritem.CostBlind;
                                }
                                else
                                {
                                    if (vendoritem.Cost != null)
                                    {
                                        purchaseorderdetail.UnitPrice = vendoritem.Cost;

                                    }
                                }

                                purchaseorderdetail.VendorReference = vendoritem.VendorPartNo;
                            }
                            else
                            {
                                //purchaseorderdetail.UnitPrice = 0;
                            }

                            if (vendoritem != null)
                            {
                                dSetupCharge = Convert.ToDecimal(vendoritem.SetupCharge);
                                dRunCharge = Convert.ToDecimal(vendoritem.RunCharge);
                            }
                            else
                            {
                                dSetupCharge = 0;
                                dRunCharge = 0;
                            }
                            db02.Dispose();
                            db02 = new TimelyDepotContext();

                            //Get the vendor part no from the subitem table
                            subitem = db02.SUB_ITEM.Where(sbit => sbit.Sub_ItemID == item.Sub_ItemID).FirstOrDefault<SUB_ITEM>();
                            if (subitem != null)
                            {
                                purchaseorderdetail.VendorReference = subitem.PartNo;
                            }
                            db02.Dispose();
                            db02 = new TimelyDepotContext();

                            db.PurchasOrderDetails.Add(purchaseorderdetail);
                        }

                    }

                    ////Add setup and run charges
                    //purchaseorderdetail = new PurchasOrderDetail();
                    //purchaseorderdetail.PurchaseOrderId = nPurchaseOrderId;
                    //purchaseorderdetail.ItemID = szItemId;
                    //purchaseorderdetail.Description = "Setup charge";
                    //purchaseorderdetail.Quantity = 0;
                    //purchaseorderdetail.UnitPrice = dSetupCharge;
                    //purchaseorderdetail.Tax = 0;

                    //nItemPosition++;
                    //purchaseorderdetail.ItemPosition = nItemPosition;
                    //purchaseorderdetail.ItemOrder = 0;

                    //db.PurchasOrderDetails.Add(purchaseorderdetail);

                    //purchaseorderdetail = new PurchasOrderDetail();
                    //purchaseorderdetail.PurchaseOrderId = nPurchaseOrderId;
                    //purchaseorderdetail.ItemID = szItemId;
                    //purchaseorderdetail.Description = "Run charge";
                    //purchaseorderdetail.Quantity = 0;
                    //purchaseorderdetail.UnitPrice = dRunCharge;
                    //purchaseorderdetail.Tax = 0;

                    //nItemPosition++;
                    //purchaseorderdetail.ItemPosition = nItemPosition;
                    //purchaseorderdetail.ItemOrder = 0;

                    //db.PurchasOrderDetails.Add(purchaseorderdetail);

                    db.SaveChanges();
                }

                //Get the dropdown data

                qryTrade = db.Trades.OrderBy(trd => trd.TradeName);
                if (qryTrade.Count() > 0)
                {
                    foreach (var item in qryTrade)
                    {
                        listSelector.Add(new KeyValuePair<string, string>(item.TradeId.ToString(), item.TradeName));
                    }
                }
                SelectList tradeselectorlist = new SelectList(listSelector, "Key", "Value");
                ViewBag.TradeList = tradeselectorlist;

                return RedirectToAction("Edit", new { id = purchaseorders.PurchaseOrderId });
                //return RedirectToAction("Index");
            }

            return View(purchaseorders);
        }
        public ActionResult Create()
        {
            int nPurchaseOrderNo = 0;

            List<KeyValuePair<string, string>> listSelector = new List<KeyValuePair<string, string>>();
            IQueryable<Trade> qryTrade = null;

            InitialInfo initialinfo = null;

            //Get the next Purchase Order No
            initialinfo = db.InitialInfoes.FirstOrDefault<InitialInfo>();
            if (initialinfo == null)
            {
                initialinfo = new InitialInfo();
                initialinfo.InvoiceNo = 0;
                initialinfo.PaymentNo = 0;
                initialinfo.PurchaseOrderNo = 1;
                initialinfo.SalesOrderNo = 0;
                initialinfo.TaxRate = 0;
                db.InitialInfoes.Add(initialinfo);
            }
            else
            {
                nPurchaseOrderNo = initialinfo.PurchaseOrderNo;
                nPurchaseOrderNo++;
                initialinfo.PurchaseOrderNo = nPurchaseOrderNo;
                db.Entry(initialinfo).State = EntityState.Modified;
            }

            PurchaseOrders purchaseorder = new PurchaseOrders();
            purchaseorder.PurchaseOrderNo = nPurchaseOrderNo.ToString();
            purchaseorder.PODate = DateTime.Now;
            db.PurchaseOrders.Add(purchaseorder);
            db.SaveChanges();

            //Get the dropdown data
            qryTrade = db.Trades.OrderBy(trd => trd.TradeName);
            if (qryTrade.Count() > 0)
            {
                foreach (var item in qryTrade)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.TradeId.ToString(), item.TradeName));
                }
            }
            SelectList tradeselectorlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.TradeList = tradeselectorlist;

            return View(purchaseorder);
        }
        private void AddPurchaseOrder(TimelyDepotContext db01, PurchaseOrders item, ref List<PurchaseOrderListHlp> purchaseorderListHlp)
        {
            int nSalesOrderId = 0;
            decimal dAmount = 0;
            IQueryable<PurchasOrderDetail> qrypodetail = null;

            nSalesOrderId = item.PurchaseOrderId;
            dAmount = GetTotalPO01(db01, nSalesOrderId);

            qrypodetail = db01.PurchasOrderDetails.Where(podt => podt.PurchaseOrderId == nSalesOrderId);
            if (qrypodetail.Count() > 0)
            {
                foreach (var itemdetail in qrypodetail)
                {
                    if (!string.IsNullOrEmpty(itemdetail.ItemID))
                    {
                        PurchaseOrderListHlp pohlp = new PurchaseOrderListHlp();
                        pohlp.ItemNo = itemdetail.Sub_ItemID;
                        pohlp.PaymentAmount = dAmount;
                        pohlp.PODate = item.PODate;
                        pohlp.PurchaseOrderId = item.PurchaseOrderId;
                        pohlp.PurchaseOrderNo = item.PurchaseOrderNo;
                        pohlp.RequiredDate = item.RequiredDate;
                        pohlp.SalesOrderNo = item.SalesOrderNo;
                        pohlp.ShipDate = item.ShipDate;
                        pohlp.VendorId = item.VendorId;
                        pohlp.TradeId = item.TradeId;
                        pohlp.ReceiveStatus = item.ReceiveStatus;
                        purchaseorderListHlp.Add(pohlp);
                    }
                }
            }
        }