// add new import invoice (this operation will affect on store quantity where the quantity in the store for specific product will increase with every added invoice) public async Task <Invoice> AddImportInvoceAsync(InvoiceCartViewModel impoInvoice) { try { if (impoInvoice.invoice.Paid < impoInvoice.invoice.FullCost) { impoInvoice.invoice.RemainForYamama = impoInvoice.invoice.FullCost - impoInvoice.invoice.Paid; } else if (impoInvoice.invoice.Paid > impoInvoice.invoice.FullCost) { impoInvoice.invoice.RemainForCustomer = impoInvoice.invoice.Paid - impoInvoice.invoice.FullCost; } else { impoInvoice.invoice.RemainForCustomer = impoInvoice.invoice.RemainForYamama = 0; } // if one of the previous conditions is true then save the invoice in the database await _db.Invoice.AddAsync(impoInvoice.invoice); //commit the changes await _db.SaveChangesAsync(); //get id for this invoice var RecentInvoice = _db.Invoice.OrderByDescending(p => p.Idinvoice).FirstOrDefault(); int RecentInvoiceID = RecentInvoice.Idinvoice; //add cart for this invoice await _cart.AddCartAsync(impoInvoice, RecentInvoiceID); _db.SaveChanges(); //get the last addes cart var RecentCart = _db.Cart.OrderByDescending(c => c.IdCart).FirstOrDefault(); //get the quantity from this cart Double qty = RecentCart.Qty; //get the product id from this cart int?id = RecentCart.ProductId; //pass the store records and add the quantity for the specific product id foreach (var item in _db.Store) { if (item.ProId == id) { item.Quantity += qty; } } //commit changes _db.SaveChanges(); return(impoInvoice.invoice); } catch { return(null); } }
public async Task <List <Cart> > AddCartAsync(InvoiceCartViewModel invoiceCart, int id) { try { //store invoice full_cost //Double fullcost = 0; for (int i = 0; i < invoiceCart.listcart.Count; i++) { //assign every item in the invoice with it's invoice_id invoiceCart.listcart[i].InvoiceId = id; //fullcost += Convert.ToDouble(invoiceCart.listcart[i].Price); await _yamamadbContext.Cart.AddAsync(invoiceCart.listcart[i]); await _yamamadbContext.SaveChangesAsync(); var store = _yamamadbContext.Store.Where(x => x.ProId == invoiceCart.listcart[i].ProductId).SingleOrDefault(); if (invoiceCart.invoice.Type == "Purchses" || invoiceCart.invoice.Type == "import") { if (store != null) { store.Quantity += invoiceCart.listcart[i].Qty; _yamamadbContext.Store.Update(store); _yamamadbContext.SaveChanges(); } else { Store newStore = new Store { ProId = invoiceCart.listcart[i].ProductId, Quantity = invoiceCart.listcart[i].Qty, Name = _yamamadbContext.Product.Where(x => x.Idproduct == invoiceCart.listcart[i].ProductId).Select(x => x.Name).SingleOrDefault() }; _yamamadbContext.Store.Add(store); _yamamadbContext.SaveChanges(); } } else if (invoiceCart.invoice.Type == "sell" || invoiceCart.invoice.Type == "export") { if (store != null) { store.Quantity -= invoiceCart.listcart[i].Qty; _yamamadbContext.Store.Update(store); _yamamadbContext.SaveChanges(); } } } return(invoiceCart.listcart); } catch (Exception) { return(null); } }
public async Task <InvoiceCartViewModel> getInvoiceDetailes(int invoiceId) { if (_yamamadbContext != null) { InvoiceCartViewModel invoiceCartViewModel = new InvoiceCartViewModel(); try { //get items from invoice table according to invoice_id Invoice invoiceInfo = GetAbstractInvoice(invoiceId); //check if there is invoice with this id if (invoiceInfo == null) { return(null); } invoiceCartViewModel.invoice = invoiceInfo; //get items from cart table according to invoice_id List <Cart> cartInfo = await(from helper in _yamamadbContext.Cart where helper.InvoiceId == invoiceId select new Cart { ProductId = helper.ProductId, Qty = helper.Qty, Price = helper.Price, SubCost = helper.SubCost }).ToListAsync(); invoiceCartViewModel.listcart = cartInfo; List <MoneyDelivered> MoneyList = await(from helper in _yamamadbContext.MoneyDelivered where helper.InvoiceId == invoiceId select new MoneyDelivered { Amount = helper.Amount, FirstDate = helper.FirstDate, FId = helper.FId, PId = helper.PId, State = helper.State }).ToListAsync(); invoiceCartViewModel.Money = MoneyList; return(invoiceCartViewModel); } catch (Exception) { return(null); } } return(null); }
public async Task <string> AddInvoiceAsync(InvoiceCartViewModel invoiceCart) { try { if (invoiceCart.invoice.Type == "sell" || invoiceCart.invoice.Type == "export") { for (int i = 0; i < invoiceCart.listcart.Count; i++) { //get the quantity of this product Double q = _yamamadbContext.Store.Where(x => x.ProId == invoiceCart.listcart[i].ProductId).Select(c => c.Quantity).SingleOrDefault(); if (q < invoiceCart.listcart[i].Qty) { return(" ERROR : The quantity in store is less than the invoice quantity !!!!"); } } } //calculate the remain for yamama and for the customer if (invoiceCart.invoice.Paid < invoiceCart.invoice.FullCost) { invoiceCart.invoice.RemainForYamama = invoiceCart.invoice.FullCost - invoiceCart.invoice.Paid; } else if (invoiceCart.invoice.Paid > invoiceCart.invoice.FullCost) { invoiceCart.invoice.RemainForCustomer = invoiceCart.invoice.Paid - invoiceCart.invoice.FullCost; } else { invoiceCart.invoice.RemainForCustomer = invoiceCart.invoice.RemainForYamama = 0; } await _yamamadbContext.Invoice.AddAsync(invoiceCart.invoice); await _yamamadbContext.SaveChangesAsync(); //get id for this invoice var RecentInvoice = _yamamadbContext.Invoice.OrderByDescending(p => p.Idinvoice).FirstOrDefault(); int RecentInvoiceID = RecentInvoice.Idinvoice; //save invoice's items var s_result = await _cart.AddCartAsync(invoiceCart, RecentInvoiceID); //save cashes var s_result1 = await _cart.addMoneyCashes(invoiceCart, RecentInvoiceID); return("SUCCESS : Invoice has been inserted successfuly "); } catch { return("ERROR : please retry with true informations"); } }
public async Task <IActionResult> InvoiceDetailes(int invoiceId) { InvoiceCartViewModel result = await _invoice.getInvoiceDetailes(invoiceId); if (result != null) { var Response = new ResponseViewModel(true, HttpStatusCode.OK, "SUCCESS", result); return(Ok(Response)); } else { var Response = new ResponseViewModel(false, HttpStatusCode.NoContent, "failed", null); return(Ok(Response)); } }
public async Task <IActionResult> Create(InvoiceCartViewModel invoiceCartViewModel) { var result = await _invoice.AddInvoiceAsync(invoiceCartViewModel); if (result != null) { var Response = new ResponseViewModel(true, HttpStatusCode.OK, "SUCCESS", result); return(Ok(Response)); } else { var Response = new ResponseViewModel(false, HttpStatusCode.NoContent, "failed", null); return(Ok(Response)); } }
public async Task <ActionResult> AddImportInvoice(InvoiceCartViewModel impoInvoice) { try { await i_ImportInvoce.AddImportInvoceAsync(impoInvoice); var Response = new ResponseViewModel(true, HttpStatusCode.OK, "SUCCESS", impoInvoice); return(Ok(Response)); } catch (Exception) { var Response = new ResponseViewModel(false, HttpStatusCode.NoContent, "failed", null); return(Ok(Response)); } }
public async Task <ActionResult> addimportcart(InvoiceCartViewModel invoiceCart, int id) { try { await _cart.AddCartAsync(invoiceCart, id); var Response = new ResponseViewModel(true, HttpStatusCode.OK, "SUCCESS", invoiceCart); return(Ok(Response)); } catch (Exception) { var Response = new ResponseViewModel(false, HttpStatusCode.NoContent, "failed", null); return(Ok(Response)); } }
public async Task <int> addMoneyCashes(InvoiceCartViewModel invoiceCart, int recentInvoiceID) { int result = 0; try { Invoice lastInvoice = _yamamadbContext.Invoice.Where(x => x.Idinvoice == recentInvoiceID).SingleOrDefault(); List <MoneyDelivered> moneyCashes = invoiceCart.Money; for (int i = 0; i < moneyCashes.Count; i++) { moneyCashes[i].InvoiceId = recentInvoiceID; if (lastInvoice.FactoryId != null) { moneyCashes[i].FId = lastInvoice.FactoryId; } else { moneyCashes[i].PId = lastInvoice.ProjectId; } moneyCashes[i].Amount = invoiceCart.Money[i].Amount; moneyCashes[i].FirstDate = invoiceCart.Money[i].FirstDate; moneyCashes[i].State = invoiceCart.Money[i].State; await _yamamadbContext.MoneyDelivered.AddAsync(moneyCashes[i]); await _yamamadbContext.SaveChangesAsync(); result = 1; } return(result); } catch (Exception) { return(result); } }
public async Task <List <(string, Double)> > GetSalesReports(string period, DateTime start, DateTime end) { //try //{ if (period == "daily") { //Define list of date and valyes to store the result List <(string, Double)> result = new List <(string, double)>(); System.TimeSpan diff = end.Subtract(start); for (var day = start.Date; day <= end; day = day.AddDays(1)) { //to store the full sales Double value = 0; string test = day.ToString("yyyy-MM-dd"); //return list of id_invoices in each day List <int> invoicesNumbers = _yamamadbContext.Invoice.Where(x => x.Date.ToString() == test).Select(x => x.Idinvoice).ToList(); for (int j = 0; j < invoicesNumbers.Count; j++) { InvoiceCartViewModel subResult = await getInvoiceDetailes(invoicesNumbers[j]); value += Convert.ToDouble(subResult.invoice.FullCost); } result.Add((test, value)); } return(result); } else if (period == "monthly") { //Define list of date and valyes to store the result List <(string, Double)> result = new List <(string, double)>(); System.TimeSpan diff = end.Subtract(start); for (var month = start.Month; month <= end.Month; month++) { string test = month + ""; //to store the full sales Double value = 0; //return list of id_invoices in each day List <int> invoicesNumbers = _yamamadbContext.Invoice.Where(x => x.Date.Value.Month == month).Select(x => x.Idinvoice).ToList(); for (int j = 0; j < invoicesNumbers.Count; j++) { InvoiceCartViewModel subResult = await getInvoiceDetailes(invoicesNumbers[j]); value += subResult.invoice.FullCost; } result.Add((test, value)); } return(result); } else if (period == "annual") { //Define list of date and valyes to store the result List <(string, Double)> result = new List <(string, double)>(); System.TimeSpan diff = end.Subtract(start); for (var year = start.Year; year <= end.Year; year++) { string test = year + ""; //to store the full sales Double value = 0; //return list of id_invoices in each day List <int> invoicesNumbers = _yamamadbContext.Invoice.Where(x => x.Date.Value.Year == year).Select(x => x.Idinvoice).ToList(); for (int j = 0; j < invoicesNumbers.Count; j++) { InvoiceCartViewModel subResult = await getInvoiceDetailes(invoicesNumbers[j]); value += Convert.ToDouble(subResult.invoice.FullCost); } result.Add((test, value)); } return(result); } /* else if (period == "weekly") * { * //Define list of invoices to store the result * List<Double> result = new List<double>(); * Double diff = (end - start).TotalDays; * * var fromDay = start.DayOfWeek; * if (fromDay <= DayOfWeek.Saturday) * { * Double duration = DayOfWeek.Saturday - fromDay; * diff = diff + duration; * } * else * { * Double duration = DayOfWeek.Saturday - fromDay; * diff = diff + duration; * } * * * * * * for (var year = start.Year; year <= end.Year; year++) * { * //to store the full sales * Double value = 0; * * //return list of id_invoices in each day * List<int> invoicesNumbers = _yamamadbContext.Invoice.Where(x => x.Date.Value.Year == year).Select(x => x.Idinvoice).ToList(); * * for (int j = 0; j < invoicesNumbers.Count; j++) * { * InvoiceCartViewModel subResult = await getInvoiceDetailes(invoicesNumbers[j]); * value += Convert.ToDouble(subResult.invoice.FullCost.Value); * * } * result.Add(value); * } * * * return result; * }*/ return(null); //} //catch (Exception) //{ // return null; //} }