Hi
i try to use angularjs & API in my Invoice project, i finally did it and it works fine but i have 3 problems on it:
1. (solved by workaround it, but really need an better way for that) - when open new invoice in any pc's - it was display the items on other opened invoices (need to open any new invoice empty):
** as example if i have an opened invoice in PC1 has item with code (1002) - when other user open new one in any other PC it's open and i found the item with id (1002) on it. i have workaround it by pass values on the list to other list and empty the first one as:
public class CustomerServiceController : ApiController { readonly Model1 db = new Model1(); private static List<Order_Details_VM> listOfOrders; private static List<Order_Details_VM> listOfOrderFinal; public CustomerServiceController() { listOfOrders = null; if (listOfOrders == null) { listOfOrders = new List<Order_Details_VM>(); } if (listOfOrderFinal == null) { listOfOrderFinal = new List<Order_Details_VM>(); } } // this action for saving the items to list to let me pass to database when user complete invoice public IHttpActionResult Post(Order_Details_VM newOrderDetails) { if (newOrderDetails != null) { listOfOrders.Add(newOrderDetails); listOfOrderFinal.Add(newOrderDetails); // for db return Ok(newOrderDetails); } return null; } }
i use angulrajs to send data to server as:
$scope.save = function () {$http.post("/api/customerService/Post", $scope.item).success(function (data) {$scope.orders.push(data); // Clear AutoComplete Text $("#Products").data("kendoAutoComplete").value("");$("#ProductID").focus();$('#ProductID').prop('readonly', false); }); };
2. I open new invoice in PC1 and type 3 item on it (I don't click save whole invoice yet) and Someone open other new invoice on his PC and add 2 items on it - when he save his invoice it saved with the 5 items (3 in my invoice on my PC & 2 from his invoice on his PC??? ¡¡)
Code for save the whole invoice after user is compete it:
public IHttpActionResult SaveInvoice(Order_VM newOrderMaster) { // save on header table to get invoice id if (newOrderMaster != null) { var order = new Order(); int? custom; if (newOrderMaster.CustomerID == null || newOrderMaster.CustomerID == 0) { custom = db.Customers.Where(i => i.ContactName == newOrderMaster.ContactName).SingleOrDefault().CustomerID; } else { custom = newOrderMaster.CustomerID; } order.CustomerID = custom; order.OrderDate = newOrderMaster.OrderDate; order.Freight = (decimal)newOrderMaster.Total; order.TypeId = 1; order.write_time = DateTime.UtcNow ; db.Orders.Add(order); db.SaveChanges(); newOrderMaster.OrderID = order.OrderID; } // loop throw items list to check Stock & add to database with invoice no foreach (var item in listOfOrderFinal) { item.OrderID = newOrderMaster.OrderID; var model = db.Products.Find(item.ProductID); if (model.stock >= item.Quantity) { model.stock = model.stock - item.Quantity; db.Entry(model).State = EntityState.Modified; statusCode = 200; } if (statusCode == 200) { // save to DB var orderdetails = new Order_Detail(); orderdetails.OrderID = item.OrderID; orderdetails.ProductID = item.ProductID; orderdetails.UnitPrice = (decimal)item.sell_price; orderdetails.write_time = DateTime.UtcNow; db.SaveChanges(); } } listOfOrders.Clear(); listOfOrderFinal.Clear(); return Ok(new { ID = newOrderMaster.OrderID }); }
so please how can i fix that and make it save only the current invoice item's on the PC user work on it and don't get any other items from any other opened invoice, nad what is between way to open an empty invoice in any PC to don't have any problem as problem NO1
hope to find help for that problem please
Thanks a lot ...