// 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 <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 <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)); } }