Exemple #1
0
        public void AddOrder(DateTime userDate, string userName, string userAbbreviation, string userProdType, string userArea)
        {
            ProductsRepo pRepo = new ProductsRepo();
            TaxesRepo    tRepo = new TaxesRepo();
            OrderInfo    ord   = new OrderInfo();

            ord.StateTax     = new Taxes();
            ord.OrderProduct = pRepo.GetProduct(userProdType);
            ord.StateTax     = tRepo.GetStateTax(userAbbreviation);


            ord.OrderDate    = userDate;
            ord.CustomerName = userName;
            ord.StateTax.StateAbbreviation = userAbbreviation;//todo validate
            ord.OrderProduct.ProductType   = userProdType;
            ord.Area = decimal.Parse(userArea);



            string fileName = $"Orders_{ord.OrderDate.ToString("MMddyyyy")}.txt";
            string file     = dirPath + fileName;

            if (!File.Exists(file))
            {
                ord.OrderNumber = 1;
                using (StreamWriter sw = File.CreateText(file))
                {
                    sw.WriteLine("OrderNumber,CustomerName,State,TaxRate,ProductType,Area,CostPerSquareFoot,LaborCostPerSquareFoot,MaterialCost,LaborCost,Tax,Total");
                    //add headers
                }
            }
            else
            {
                var list = _orderRepo.LoadOrders(userDate);
                ord.OrderNumber = list.Max(l => l.OrderNumber) + 1;
                //using (StreamReader sr = new StreamReader(file))
                //{
                //    //sr.ReadLine();
                //    string line;
                //    int counter = 0;

                //    while ((line = sr.ReadLine()) != null)
                //    {
                //        counter++;
                //    }
                //    ord.OrderNumber = counter;
                //}
            }
            using (StreamWriter sw = File.AppendText(file))
            {
                sw.WriteLine($"{ord.OrderNumber},{ord.CustomerName},{ord.StateTax.StateAbbreviation},{ord.StateTax.TaxRate},{ord.OrderProduct.ProductType},{ord.Area},{ord.OrderProduct.CostPerSquareFoot},{ord.OrderProduct.CostPerSquareFoot},{ord.OrderProduct.LaborCostPerSquareFoot},{ord.MaterialCost},{ord.LaborCost},{ord.Tax},{ord.Total}");
            }
        }