public override async Task <int> HandleCommand(UpdateTripCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderId == 0)
            {
                throw new BusinessException("Order.NotExisted");
            }

            var order = await retailerOrderQueries.Get(request.OrderId);

            if (order == null)
            {
                throw new BusinessException("Order.NotExisted");
            }

            order.TripId = request.TripId;
            order        = UpdateBuild(order, request.LoginSession);
            return(await retailerOrderRepository.Update(order));
        }
        public override async Task <int> HandleCommand(DeleteCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderId == 0)
            {
                return(-1);
            }

            var order = await retailerOrderQueries.Get(request.OrderId);

            if (order != null)
            {
                #warning Hoang Uncompleted
                // check business rule here

                order = DeleteBuild(order, request.LoginSession);
                return(await retailerOrderRepository.Update(order));
            }

            return(-1);
        }
Example #3
0
        public override async Task <int> HandleCommand(DeleteCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderId == 0)
            {
                return(-1);
            }

            var retailerId = -1;

            if (request.LoginSession.Roles.FirstOrDefault(r => r == "Administrator") == null)
            {
                var retailer = await WebHelper.HttpGet <Retailer>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetRetailerByUser}?userId={request.LoginSession.Id}", request.LoginSession.AccessToken);

                if (retailer == null)
                {
                    throw new BusinessException("Retailer.NotExisted");
                }
                retailerId = retailer.Id;
            }

            var order = await retailerOrderQueries.Get(request.OrderId);

            if (order != null)
            {
                if (order.CreatedDate.AddSeconds(GlobalConfiguration.TimeoutOfRetailerOrder) < DateTime.Now)
                {
                    return(-1); //Time out
                }

                if (retailerId != -1 && retailerId != order.RetailerId)
                {
                    throw new NotPermissionException();
                }

                order = DeleteBuild(order, request.LoginSession);
                return(await retailerOrderRepository.Update(order));
            }

            return(-1);
        }
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null || request.Order.Id == 0 ||
                request.Order.Items == null || request.Order.Items.Count == 0)
            {
                throw new BusinessException("Order.Retailer.Order.NotExisted");
            }

            var retailerId = -1;

            if (request.LoginSession.Roles.FirstOrDefault(r => r == "Administrator") == null)
            {
                var retailer = await WebHelper.HttpGet <Retailer>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetRetailerByUser}?userId={request.LoginSession.Id}", request.LoginSession.AccessToken);

                if (retailer == null)
                {
                    throw new BusinessException("Retailer.NotExisted");
                }
                retailerId = retailer.Id;
            }

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        retailerOrderRepository.JoinTransaction(conn, trans);
                        retailerOrderQueries.JoinTransaction(conn, trans);
                        productQueries.JoinTransaction(conn, trans);
                        var order = await retailerOrderQueries.Get(request.Order.Id);

                        if (order == null)
                        {
                            throw new BusinessException("Order.Retailer.Order.NotExisted");
                        }

                        if (retailerId != -1 && retailerId != order.RetailerId)
                        {
                            throw new NotPermissionException();
                        }

                        if (order.CreatedDate.AddSeconds(GlobalConfiguration.TimeoutOfRetailerOrder) < DateTime.Now)
                        {
                            throw new BusinessException("Order.Retailer.Order.TimeOut");
                        }

                        decimal totalAmount = 0;
                        await retailerOrderRepository.DeleteItems(request.Order.Id);

                        foreach (var item in request.Order.Items)
                        {
                            var prod = (await WebHelper.HttpGet <IEnumerable <ProductViewModel> >(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProduct}?productId={item.ProductId}", request.LoginSession.AccessToken)).FirstOrDefault(p => p.CurrentUoM == item.UoMId);
                            if (prod == null)
                            {
                                throw new BusinessException("Product.NotExisted");
                            }

                            item.RetailerOrderId    = request.Order.Id;
                            item.StatusId           = (int)RetailerOrderStatuses.Ordered;
                            item.AdapQuantity       = 0;
                            item.DeliveriedQuantity = 0;
                            item.Price = prod.Prices.FirstOrDefault(p => p.UoMId == item.UoMId).SellingPrice;
                            await retailerOrderRepository.AddItem(item);

                            totalAmount += item.Price * item.OrderedQuantity;
                        }

                        request.Order            = UpdateBuild(request.Order, request.LoginSession);
                        request.Order.RetailerId = order.RetailerId;
                        request.Order.StatusId   = (int)RetailerOrderStatuses.Ordered;
                        request.Order.RetailerBuyingCalendarId = order.RetailerBuyingCalendarId;
                        request.Order.TotalAmount = totalAmount;
                        request.Order.CreatedBy   = order.CreatedBy;
                        request.Order.CreatedDate = order.CreatedDate;
                        if (await retailerOrderRepository.Update(request.Order) != 0)
                        {
                            throw new Exception("Update Order failed");
                        }

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            trans.Rollback();
                        }
                        catch { }
                        throw ex;
                    }
                }
            }

            return(0);
        }
        public override async Task <int> HandleCommand(ProcessCommand request, CancellationToken cancellationToken)
        {
            var rs = 0;

            //Get statistic for this logic
            var products = await productQueries.Gets();

            var uoms = await uoMQueries.Gets();

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerBuyingCalendarRepository.JoinTransaction(conn, trans);
                        farmerBuyingCalendarQueries.JoinTransaction(conn, trans);
                        farmerRetailerOrderItemRepository.JoinTransaction(conn, trans);
                        retailerOrderQueries.JoinTransaction(conn, trans);
                        retailerOrderRepository.JoinTransaction(conn, trans);

                        var order = await retailerOrderQueries.Get(request.Processing.OrderId);

                        if (order == null)
                        {
                            throw new BusinessException("Order.Retailer.Order.NotExisted");
                        }

                        order.StatusId = (int)RetailerOrderStatuses.FarmerOrdered;
                        order          = UpdateBuild(order, request.LoginSession);
                        if ((await retailerOrderRepository.Update(order)) != 0)
                        {
                            return(rs = -1);
                        }

                        foreach (var item in request.Processing.Items)
                        {
                            if (item.Plannings == null)
                            {
                                continue;
                            }

                            var orderItem = order.Items.FirstOrDefault(i => i.Id == item.OrderItemId);

                            if (orderItem == null)
                            {
                                throw new BusinessException("OrderItem.NotExisted");
                            }

                            // wrong logic
                            if (!products.Any(p => p.Id == orderItem.ProductId) || !uoms.Any(u => u.Id == orderItem.UoMId))
                            {
                                return(rs = -1);
                            }

                            orderItem.StatusId = (int)RetailerOrderStatuses.FarmerOrdered;
                            if ((await retailerOrderRepository.UpdateItem(orderItem)) != 0)
                            {
                                return(rs = -1);
                            }

                            foreach (var planning in item.Plannings)
                            {
                                // Get farmer planning and check it existing
                                var checkingOrder = (await farmerBuyingCalendarQueries.Gets($"o.buying_date = '{order.BuyingDate.Date.ToString("yyyyMMdd")}' AND o.farmer_id = {planning.FarmerId} AND o.is_ordered = 0")).FirstOrDefault();
                                if (checkingOrder == null)
                                {
                                    //No existed

                                    //Create buying calendar
                                    var planningCode = await farmerBuyingCalendarQueries.GenarateCode();

                                    var addOrder = CreateBuild(new UI.Models.FarmerBuyingCalendar()
                                    {
                                        IsExpired  = order.BuyingDate <= DateTime.Now ? true : false,
                                        IsOrdered  = false,
                                        Code       = planningCode,
                                        Name       = planningCode,
                                        BuyingDate = order.BuyingDate,
                                        FarmerId   = planning.FarmerId
                                    }, request.LoginSession);
                                    var planningId = await farmerBuyingCalendarRepository.Add(addOrder);

                                    //Create item
                                    var planningItem = new FarmerBuyingCalendarItem()
                                    {
                                        ProductId              = orderItem.ProductId,
                                        UoMId                  = orderItem.UoMId,
                                        Quantity               = planning.Quantity,
                                        AdapQuantity           = planning.Quantity,
                                        FarmerBuyingCalendarId = planningId
                                    };
                                    var planningItemId = await farmerBuyingCalendarRepository.AddItem(planningItem);

                                    //Add mapping
                                    await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                    {
                                        IsPlanning          = true,
                                        FarmerId            = planning.FarmerId,
                                        FarmerOrderId       = planningId,
                                        FarmerOrderItemId   = planningItemId,
                                        RetailerId          = order.RetailerId,
                                        RetailerOrderId     = order.Id,
                                        RetailerOrderItemId = orderItem.Id,
                                        ProductId           = orderItem.ProductId,
                                        Quantity            = planning.Quantity,
                                        UoMId = orderItem.UoMId
                                    });
                                }
                                else
                                {
                                    //Existed

                                    // Update information for Order
                                    checkingOrder = UpdateBuild(checkingOrder, request.LoginSession);
                                    if (await farmerBuyingCalendarRepository.Update(checkingOrder) != 0)
                                    {
                                        return(rs = -1);
                                    }

                                    //Get the item and update quantity for item
                                    var checkingOrderItem = checkingOrder.Items.FirstOrDefault(i => i.ProductId == orderItem.ProductId && i.UoMId == orderItem.UoMId);
                                    if (checkingOrderItem == null)
                                    {
                                        //Create item
                                        var planningItem = new FarmerBuyingCalendarItem()
                                        {
                                            ProductId              = orderItem.ProductId,
                                            UoMId                  = orderItem.UoMId,
                                            Quantity               = planning.Quantity,
                                            AdapQuantity           = planning.Quantity,
                                            FarmerBuyingCalendarId = checkingOrder.Id
                                        };
                                        var planningItemId = await farmerBuyingCalendarRepository.AddItem(planningItem);

                                        //Add mapping
                                        await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                        {
                                            IsPlanning          = true,
                                            FarmerId            = planning.FarmerId,
                                            FarmerOrderId       = checkingOrder.Id,
                                            FarmerOrderItemId   = planningItemId,
                                            RetailerId          = order.RetailerId,
                                            RetailerOrderId     = order.Id,
                                            RetailerOrderItemId = orderItem.Id,
                                            ProductId           = orderItem.ProductId,
                                            Quantity            = planning.Quantity,
                                            UoMId = orderItem.UoMId
                                        });
                                    }
                                    else
                                    {
                                        checkingOrderItem.Quantity += planning.Quantity;
                                        if (await farmerBuyingCalendarRepository.UpdateItem(checkingOrderItem) != 0)
                                        {
                                            return(rs = -1);
                                        }

                                        //Add mapping
                                        await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                        {
                                            IsPlanning          = true,
                                            FarmerId            = planning.FarmerId,
                                            FarmerOrderId       = checkingOrder.Id,
                                            FarmerOrderItemId   = checkingOrderItem.Id,
                                            RetailerId          = order.RetailerId,
                                            RetailerOrderId     = order.Id,
                                            RetailerOrderItemId = orderItem.Id,
                                            ProductId           = orderItem.ProductId,
                                            Quantity            = planning.Quantity,
                                            UoMId = orderItem.UoMId
                                        });
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        rs = -1;
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(0);
        }
        public override async Task <int> HandleCommand(UpdateStatusCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderId == 0)
            {
                throw new BusinessException("Order.NotExisted");
            }

            if (!Enum.IsDefined(typeof(RetailerOrderStatuses), request.StatusId))
            {
                throw new BusinessException("Order.NotExistedStatus");
            }

            var order = await retailerOrderQueries.Get(request.OrderId);


            if (order != null)
            {
                if (request.StatusId == (int)RetailerOrderStatuses.Canceled && order.StatusId != (int)RetailerOrderStatuses.Ordered)
                {
                    //Cancel the order that is comfirmed.
                    throw new BusinessException("Order.CantCanceled");
                }
                else if (order.StatusId != request.StatusId - 1)
                {
                    //wrong step; dump data
                    throw new BusinessException("Order.WrongStep");
                }

                var rs = -1;

                using (var conn = DALHelper.GetConnection())
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        try
                        {
                            retailerOrderRepository.JoinTransaction(conn, trans);
                            foreach (var item in order.Items)
                            {
                                item.StatusId = request.StatusId;
                                await retailerOrderRepository.UpdateItem(item);
                            }

                            order          = UpdateBuild(order, request.LoginSession);
                            order.StatusId = request.StatusId;
                            await retailerOrderRepository.Update(order);

                            await retailerOrderRepository.AddAudit(new UI.Models.RetailerOrderAudit()
                            {
                                RetailerId          = order.RetailerId,
                                RetailerOrderId     = order.Id,
                                RetailerOrderItemId = null,
                                StatusId            = request.StatusId,
                                CreatedBy           = request.LoginSession.Id,
                                CreatedDate         = DateTime.Now,
                                Note = string.Empty
                            });

                            return(rs = 0);
                        }
                        finally
                        {
                            if (rs == 0)
                            {
                                trans.Commit();
                            }
                            else
                            {
                                try
                                {
                                    trans.Rollback();
                                }
                                catch { }
                            }
                        }
                    }
                }
            }

            throw new BusinessException("Order.NotExisted");
        }
Example #7
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null || request.Order.Items == null || request.Order.Items.Count == 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            if (request.LoginSession.Roles.FirstOrDefault(r => r == "Administrator") == null)
            {
                var retailer = await WebHelper.HttpGet <Retailer>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetRetailerByUser}?userId={request.LoginSession.Id}", request.LoginSession.AccessToken);

                if (retailer == null)
                {
                    throw new BusinessException("Retailer.NotExisted");
                }
                request.Order.RetailerId = retailer.Id;
            }
            else
            {
                if (request.Order.RetailerId == 0)
                {
                    throw new BusinessException("AddWrongInformation");
                }
                else
                {
                    var retailer = await WebHelper.HttpGet <Retailer>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetRetailerById}?retailerId={request.Order.RetailerId}", request.LoginSession.AccessToken);

                    if (retailer == null)
                    {
                        throw new BusinessException("Retailer.NotExisted");
                    }
                }
            }

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        retailerOrderRepository.JoinTransaction(conn, trans);

                        // update status for buying calendar
                        if (request.Order.RetailerBuyingCalendarId != null)
                        {
                            retailerBuyingCalendarQueries.JoinTransaction(conn, trans);
                            var buyingCalendar = await retailerBuyingCalendarQueries.Get(request.Order.RetailerBuyingCalendarId.Value);

                            if (buyingCalendar != null)
                            {
                                retailerBuyingCalendarRepository.JoinTransaction(conn, trans);
                                buyingCalendar           = UpdateBuild(buyingCalendar, request.LoginSession);
                                buyingCalendar.IsOrdered = true;
                                var rs = await retailerBuyingCalendarRepository.Update(buyingCalendar);

                                if (rs != 0)
                                {
                                    throw new Exception("Update Buying calendar failed");
                                }
                            }
                        }

                        request.Order          = CreateBuild(request.Order, request.LoginSession);
                        request.Order.StatusId = (int)RetailerOrderStatuses.Ordered;

                        //Genarate Code
                        request.Order.Code = await retailerOrderQueries.GenarateCode();

                        request.Order.Id = await retailerOrderRepository.Add(request.Order);

                        decimal totalAmount = 0;
                        foreach (var item in request.Order.Items)
                        {
                            var prod = (await WebHelper.HttpGet <IEnumerable <ProductViewModel> >(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProduct}?productId={item.ProductId}", request.LoginSession.AccessToken)).FirstOrDefault(p => p.CurrentUoM == item.UoMId);
                            if (prod == null)
                            {
                                throw new BusinessException("Product.NotExisted");
                            }

                            item.RetailerOrderId    = request.Order.Id;
                            item.StatusId           = (int)RetailerOrderStatuses.Ordered;
                            item.AdapQuantity       = 0;
                            item.DeliveriedQuantity = 0;
                            item.Price = prod.Prices.FirstOrDefault(p => p.UoMId == item.UoMId).SellingPrice;
                            await retailerOrderRepository.AddItem(item);

                            totalAmount += item.Price * item.OrderedQuantity;
                        }

                        request.Order.TotalAmount = totalAmount;
                        if (await retailerOrderRepository.Update(request.Order) != 0)
                        {
                            throw new Exception("Update Order failed");
                        }

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            trans.Rollback();
                        }
                        catch { }
                        throw ex;
                    }
                }
            }

            return(0);
        }