예제 #1
0
 public IHttpActionResult updateCart([FromBody] cartViewModel cartmodel)
 {
     try
     {
         if (cartmodel != null)
         {
             var user  = (cartmodel.staff_id != null) ? db.Users.Find(cartmodel.staff_id) : null;
             var unit  = (cartmodel.unit_id != null) ? db.dept_unit.Find(cartmodel.unit_id) : null;
             var dept  = (cartmodel.dept_id != null) ? db.department.Find(cartmodel.dept_id) : null;
             var item  = (cartmodel.item_id != null) ? db.product.Find(cartmodel.item_id) : null;
             var model = db.cart.Find(cartmodel.id);
             if (cartmodel.Request_type == "internalRequest")
             {
                 model.cart_id = string.Concat("c-", cartmodel.dept_id);
             }
             else if (cartmodel.Request_type == "regionalRequest")
             {
                 model.cart_id = string.Concat("c-", cartmodel.Region);
             }
             else if (cartmodel.Request_type == "stateRequest")
             {
                 model.cart_id = string.Concat("c-", cartmodel.State);
             }
             model.dept_id                  = cartmodel.dept_id;
             model.dept_name                = (dept != null) ? dept.dept_name : null;
             model.unit_id                  = cartmodel.unit_id;
             model.unit_name                = (unit != null) ? unit.unit_name : null;
             model.staff_id                 = cartmodel.staff_id;
             model.staff_name               = (user != null) ? user.Name : null;
             model.State                    = (!string.IsNullOrEmpty(cartmodel.State)) ? cartmodel.State: null;
             model.isLabRequest             = cartmodel.isLabRequest;
             model.Requested_qty_unit       = cartmodel.Requested_qty_unit;
             model.Requested_qty_unit_value = cartmodel.Requested_qty_unit_value;
             model.conversion_value         = (cartmodel.conversion_value == 0) ? 1 : cartmodel.conversion_value;
             model.Qty_Allocated            = cartmodel.Qty_Allocated;
             model.Qty_Requested            = cartmodel.Qty_Requested;
             model.item_base_unit           = cartmodel.item_base_unit;
             model.item_id                  = cartmodel.item_id;
             model.item_name                = item.product_name;
             model.Request_type             = cartmodel.Request_type;
             model.s_r_v_no                 = cartmodel.s_r_v_no;
             model.Region                   = cartmodel.Region;
             var dt = cartmodel.requested_date;
             model.requested_date = cartmodel.requested_date;
             db.SaveChanges();
             return(Ok(cart));
             //db.cart.Add(model);
             //check if cart is not null
             //and check if new cart_id matches the one earlier created
             //if yes, proceed to add item to cart, other reject item from cart
         }
         return(BadRequest());
     }
     catch (Exception ex)
     {
         return(Content(HttpStatusCode.BadRequest, ex));
     }
 }
예제 #2
0
        //[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));
            }
        }
예제 #3
0
        public IHttpActionResult addToCart([FromBody] cartViewModel cartmodel)
        {
            try
            {
                if (cartmodel != null)
                {
                    #region fresh request
                    if (cartmodel.request_status.ToLower() == "fresh".ToLower())
                    {
                        var           user  = (cartmodel.staff_id != null) ? db.Users.Find(cartmodel.staff_id) : null;
                        var           unit  = (cartmodel.unit_id != null) ? db.dept_unit.Find(cartmodel.unit_id) : null;
                        var           dept  = (cartmodel.dept_id != null) ? db.department.Find(cartmodel.dept_id) : null;
                        var           item  = (cartmodel.item_id != null) ? db.product.Find(cartmodel.item_id) : null;
                        cartViewModel model = new cartViewModel();
                        if (cartmodel.Request_type == "internalRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.dept_id);
                        }
                        else if (cartmodel.Request_type == "regionalRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.Region);
                        }
                        else if (cartmodel.Request_type == "stateRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.State);
                        }
                        model.dept_id                  = cartmodel.dept_id;
                        model.dept_name                = (dept != null) ? dept.dept_name : null;
                        model.unit_id                  = cartmodel.unit_id;
                        model.unit_name                = (unit != null) ? unit.unit_name : null;
                        model.staff_id                 = cartmodel.staff_id;
                        model.staff_name               = (user != null) ? user.Name : null;
                        model.State                    = (!string.IsNullOrEmpty(cartmodel.State)) ? cartmodel.State : null;
                        model.isLabRequest             = cartmodel.isLabRequest;
                        model.Requested_qty_unit       = cartmodel.Requested_qty_unit;
                        model.Requested_qty_unit_value = cartmodel.Requested_qty_unit_value;
                        model.conversion_value         = (cartmodel.conversion_value == 0) ? 1 : cartmodel.conversion_value;
                        model.Qty_Allocated            = cartmodel.Qty_Allocated;
                        model.Qty_Requested            = cartmodel.Qty_Requested;
                        model.item_base_unit           = cartmodel.item_base_unit;
                        model.item_id                  = cartmodel.item_id;
                        model.item_name                = item.product_name;
                        model.Request_type             = cartmodel.Request_type;
                        model.s_r_v_no                 = cartmodel.s_r_v_no;
                        model.Region                   = cartmodel.Region;
                        model.request_status           = cartmodel.request_status;
                        var dt = cartmodel.requested_date;
                        model.requested_date = cartmodel.requested_date;
                        db.cart.Add(model);
                        //check if cart is not null
                        //and check if new cart_id matches the one earlier created
                        //if yes, proceed to add item to cart, other reject item from cart
                        var ct = db.cart.Where(c => c.request_status.ToLower() == "fresh".ToLower()).ToList();
                        if (ct.Count > 0)
                        {
                            #region
                            var cart_id_in_cart = db.cart.Where(m => m.cart_id == model.cart_id && m.request_status.ToLower() == "fresh".ToLower());
                            if (cart_id_in_cart.Count() > 0)
                            {
                                foreach (var b_item in cart_id_in_cart)
                                {
                                    if (b_item.Request_type == "stateRequest")
                                    {
                                        if (b_item.State == cartmodel.State)
                                        {
                                            var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "fresh".ToLower() && i.State == b_item.State);
                                            if (item_in_cart.Count() > 0)
                                            {
                                                return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else if (b_item.Request_type == "regionalRequest")
                                    {
                                        if (b_item.Region == cartmodel.Region)
                                        {
                                            var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "fresh".ToLower() && i.Region == b_item.Region);
                                            if (item_in_cart.Count() > 0)
                                            {
                                                return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else if (b_item.Request_type == "internalRequest")
                                    {
                                        if (b_item.dept_id == cartmodel.dept_id)
                                        {
                                            if (b_item.staff_id == cartmodel.staff_id)
                                            {
                                                if (!string.IsNullOrEmpty(b_item.unit_id))

                                                {
                                                    if (b_item.unit_id == cartmodel.unit_id)
                                                    {
                                                        var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "fresh".ToLower() && i.dept_id == b_item.dept_id);
                                                        if (item_in_cart.Count() > 0)
                                                        {
                                                            return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                                        }
                                                    }
                                                    else
                                                    {
                                                        return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the UNIT selected is different from '" + b_item.unit_name.ToString().ToUpper() + "', kindly proceed to checkout the existing cart item or delete items in cart."));
                                                    }
                                                }
                                                else
                                                {
                                                    var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "fresh".ToLower() && i.dept_id == b_item.dept_id);
                                                    if (item_in_cart.Count() > 0)
                                                    {
                                                        return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the STAFF selected is different from the one whose request is been process, kindly proceed to checkout the existing cart item or delete items in cart."));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else
                                    {
                                        return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                    }
                                }
                            }
                            else
                            {
                                return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                            }
                            #endregion
                        }
                        db.SaveChanges();
                        return(Ok(cart));
                    }
                    #endregion
                    #region borrow request
                    if (cartmodel.request_status.ToLower() == "borrow")
                    {
                        var           user  = (cartmodel.staff_id != null) ? db.Users.Find(cartmodel.staff_id) : null;
                        var           unit  = (cartmodel.unit_id != null) ? db.dept_unit.Find(cartmodel.unit_id) : null;
                        var           dept  = (cartmodel.dept_id != null) ? db.department.Find(cartmodel.dept_id) : null;
                        var           item  = (cartmodel.item_id != null) ? db.product.Find(cartmodel.item_id) : null;
                        cartViewModel model = new cartViewModel();
                        if (cartmodel.Request_type == "internalRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.dept_id);
                        }
                        else if (cartmodel.Request_type == "regionalRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.Region);
                        }
                        else if (cartmodel.Request_type == "stateRequest")
                        {
                            model.cart_id = string.Concat("c-", cartmodel.State);
                        }
                        model.dept_id                  = cartmodel.dept_id;
                        model.dept_name                = (dept != null) ? dept.dept_name : null;
                        model.unit_id                  = cartmodel.unit_id;
                        model.unit_name                = (unit != null) ? unit.unit_name : null;
                        model.staff_id                 = cartmodel.staff_id;
                        model.staff_name               = (user != null) ? user.Name : null;
                        model.State                    = (!string.IsNullOrEmpty(cartmodel.State)) ? cartmodel.State: null;
                        model.isLabRequest             = cartmodel.isLabRequest;
                        model.Requested_qty_unit       = cartmodel.Requested_qty_unit;
                        model.Requested_qty_unit_value = cartmodel.Requested_qty_unit_value;
                        model.conversion_value         = (cartmodel.conversion_value == 0) ? 1 : cartmodel.conversion_value;
                        model.Qty_Allocated            = cartmodel.Qty_Allocated;
                        model.Qty_Requested            = cartmodel.Qty_Requested;
                        model.item_base_unit           = cartmodel.item_base_unit;
                        model.item_id                  = cartmodel.item_id;
                        model.item_name                = item.product_name;
                        model.Request_type             = cartmodel.Request_type;
                        model.s_r_v_no                 = cartmodel.s_r_v_no;
                        model.Region                   = cartmodel.Region;
                        model.request_status           = cartmodel.request_status;
                        var dt = cartmodel.requested_date;
                        model.requested_date = cartmodel.requested_date;
                        db.cart.Add(model);
                        //check if cart is not null
                        //and check if new cart_id matches the one earlier created
                        //if yes, proceed to add item to cart, other reject item from cart
                        var ct = db.cart.Where(c => c.request_status.ToLower() == "borrow".ToLower()).ToList();
                        if (ct.Count > 0)
                        {
                            #region
                            var cart_id_in_cart = db.cart.Where(m => m.cart_id == model.cart_id && m.request_status.ToLower() == "borrow".ToLower());
                            if (cart_id_in_cart.Count() > 0)
                            {
                                foreach (var b_item in cart_id_in_cart)
                                {
                                    if (b_item.Request_type == "stateRequest")
                                    {
                                        if (b_item.State == cartmodel.State)
                                        {
                                            var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "borrow".ToLower() && i.State == b_item.State);
                                            if (item_in_cart.Count() > 0)
                                            {
                                                return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else if (b_item.Request_type == "regionalRequest")
                                    {
                                        if (b_item.Region == cartmodel.Region)
                                        {
                                            var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "borrow".ToLower() && i.Region == b_item.Region);
                                            if (item_in_cart.Count() > 0)
                                            {
                                                return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else if (b_item.Request_type == "internalRequest")
                                    {
                                        if (b_item.dept_id == cartmodel.dept_id)
                                        {
                                            if (b_item.staff_id == cartmodel.staff_id)
                                            {
                                                if (!string.IsNullOrEmpty(b_item.unit_id))

                                                {
                                                    if (b_item.unit_id == cartmodel.unit_id)
                                                    {
                                                        var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "borrow".ToLower() && i.dept_id == b_item.dept_id);
                                                        if (item_in_cart.Count() > 0)
                                                        {
                                                            return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                                        }
                                                    }
                                                    else
                                                    {
                                                        return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the UNIT selected is different from '" + b_item.unit_name.ToString().ToUpper() + "', kindly proceed to checkout the existing cart item or delete items in cart."));
                                                    }
                                                }
                                                else
                                                {
                                                    var item_in_cart = db.cart.Where(i => i.item_id == model.item_id && i.request_status.ToLower() == "borrow".ToLower() && i.dept_id == b_item.dept_id);
                                                    if (item_in_cart.Count() > 0)
                                                    {
                                                        return(Content(HttpStatusCode.BadRequest, " You cannot add '" + model.item_name + "' to cart more than once, Kindly delete and re-add in-order to make changes"));
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the STAFF selected is different from the one whose request is been process, kindly proceed to checkout the existing cart item or delete items in cart."));
                                            }
                                        }
                                        else
                                        {
                                            return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                        }
                                    }
                                    else
                                    {
                                        return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                                    }
                                }
                            }
                            else
                            {
                                return(Content(HttpStatusCode.BadRequest, "New item cannot be added to cart, because the department, state or region selected is different from the existing one in cart, kindly proceed to checkout the existing cart item or delete items in cart."));
                            }
                            #endregion
                        }

                        db.SaveChanges();
                        return(Ok(cart));
                    }
                    #endregion
                }
                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, ex));
            }
        }