public IHttpActionResult updateRequistion([FromBody] store_requisitionTb model) { if (model != null) { var logInUserName = RequestContext.Principal.Identity.Name; var strq = db.store_requisition.Find(model.R_ID); if (model.qty_allocated < strq.qty_requested) { ulog.loguserActivities(logInUserName, "updated the quantity of item allocated for '" + strq.product_name + "' from '" + strq.qty_allocated + "' to '" + model.qty_allocated + "'"); int diffrence = strq.qty_allocated - model.qty_allocated; strq.qty_allocated = model.qty_allocated; strq.qty_supplied = strq.qty_allocated; db.SaveChanges(); var item = db.product.Find(strq.product_id); item.total_item_allocated_pending_approval -= diffrence; db.SaveChanges(); return(Ok()); } return(Content(HttpStatusCode.BadRequest, "Quantity Allocated cannot be more than Quantity Requested")); } return(BadRequest()); }
//[Route("api/storeRequest/{id}")] public IHttpActionResult createRequest([FromBody] cartViewModel model) { try { var logInUserName = RequestContext.Principal.Identity.Name; store_requisitionTb stRequest = new store_requisitionTb(); var cart = db.cart.Where(c => c.cart_id == model.cart_id).ToList(); if (cart.Count() > 0) { var request_order_no = string.Concat("OR", rd.Next(1000)); foreach (var item in cart) { stRequest.R_order_no = request_order_no; stRequest.S_R_V_No = item.s_r_v_no; stRequest.product_id = item.item_id; stRequest.product_name = item.item_name; stRequest.conversion_value = item.conversion_value; stRequest.Requested_qty_unit = item.Requested_qty_unit; stRequest.Requested_qty_unit_value = item.Requested_qty_unit_value; stRequest.item_base_unit = item.item_base_unit; stRequest.qty_requested = item.Qty_Requested; stRequest.qty_allocated = item.Qty_Allocated; stRequest.reqst_staff_id = item.staff_id; stRequest.reqst_staff_name = item.staff_name; stRequest.state_office = item.State; stRequest.isLabRequest = item.isLabRequest; stRequest.regional_office = item.Region; stRequest.department = item.dept_name; stRequest.dept_id = item.dept_id; stRequest.unit = item.unit_name; stRequest.unit_id = item.unit_id; stRequest.request_type = item.Request_type; stRequest.request_status = "Fresh"; stRequest.created_by = logInUserName; stRequest.Request_dt = (item.requested_date != null)?DateTime.ParseExact(item.requested_date.ToString(), "d/M/yyyy", CultureInfo.InvariantCulture):DateTime.UtcNow; stRequest.Created_Date = DateTime.UtcNow.Date; db.store_requisition.Add(stRequest); db.SaveChanges(); var item_in_stock = db.product.Find(item.item_id); if (item_in_stock != null) { item_in_stock.total_item_allocated_pending_approval += item.Qty_Allocated; item_in_stock.current_stock_pending_approval = item_in_stock.opening_stock_qty - item_in_stock.total_item_allocated_pending_approval; db.SaveChanges(); } } foreach (var item in cart) { // delete this item from cart var ctItem = db.cart.Find(item.id); db.cart.Remove(ctItem); db.SaveChanges(); } //this line return the order id of the newly created store request. ulog.loguserActivities(logInUserName, "Created new store request"); // var getOrderid = db.store_requisition.FirstOrDefault(order => order.R_order_no == request_order_no).R_order_no; return(Ok()); } return(Content(HttpStatusCode.NoContent, "no content in cart")); } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex)); } }