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

            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.BuyingCalendar.RetailerId = retailer.Id;

            var checkingPlanning = (await retailerBuyingCalendarQueries.Gets($"o.code = '{request.BuyingCalendar.Code}' AND o.retailer_id = {request.BuyingCalendar.RetailerId}")).FirstOrDefault();

            if (checkingPlanning != null)
            {
                throw new BusinessException("Order.Retailer.Planning.ExistedCode");
            }

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        retailerBuyingCalendarRepository.JoinTransaction(conn, trans);
                        retailerBuyingCalendarQueries.JoinTransaction(conn, trans);
                        request.BuyingCalendar.Code = await retailerBuyingCalendarQueries.GenarateCode();

                        request.BuyingCalendar           = CreateBuild(request.BuyingCalendar, request.LoginSession);
                        request.BuyingCalendar.IsExpired = request.BuyingCalendar.BuyingDate <= DateTime.Now ? true : false;
                        request.BuyingCalendar.IsOrdered = false;
                        request.BuyingCalendar.IsAdaped  = true;
                        request.BuyingCalendar.AdapNote  = string.Empty;
                        var id = await retailerBuyingCalendarRepository.Add(request.BuyingCalendar);

                        foreach (var item in request.BuyingCalendar.Items)
                        {
                            item.RetailerBuyingCalendarId = id;
                            item.AdapQuantity             = item.Quantity;
                            await retailerBuyingCalendarRepository.AddItem(item);
                        }

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

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

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

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        retailerBuyingCalendarRepository.JoinTransaction(conn, trans);
                        retailerBuyingCalendarQueries.JoinTransaction(conn, trans);
                        var order = await retailerBuyingCalendarQueries.Get(request.BuyingCalendar.Id);

                        //It's not a owner planning.
                        if (retailer.Id != order.RetailerId || retailer.Id != request.BuyingCalendar.RetailerId)
                        {
                            throw new NotPermissionException();
                        }

                        request.BuyingCalendar             = UpdateBuild(request.BuyingCalendar, request.LoginSession);
                        request.BuyingCalendar.Code        = order.Code;
                        request.BuyingCalendar.CreatedDate = order.CreatedDate;
                        request.BuyingCalendar.CreatedBy   = order.CreatedBy;
                        request.BuyingCalendar.IsExpired   = request.BuyingCalendar.BuyingDate <= DateTime.Now ? true : false;
                        request.BuyingCalendar.IsAdaped    = true;
                        request.BuyingCalendar.AdapNote    = string.Empty;
                        request.BuyingCalendar.Code        = order.Code;
                        await retailerBuyingCalendarRepository.Update(request.BuyingCalendar);

                        await retailerBuyingCalendarRepository.DeleteItems(request.BuyingCalendar.Id);

                        foreach (var item in request.BuyingCalendar.Items)
                        {
                            item.RetailerBuyingCalendarId = request.BuyingCalendar.Id;
                            item.AdapQuantity             = item.Quantity;
                            await retailerBuyingCalendarRepository.AddItem(item);
                        }

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

            return(0);
        }
Exemple #3
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);
        }
Exemple #4
0
        public override async Task <int> HandleCommand(UpdateAdapCommand request, CancellationToken cancellationToken)
        {
            if (request.BuyingCalendar == null || request.BuyingCalendar.Id == 0)
            {
                return(-1);
            }

            var rs = 0;

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

                        var order = await retailerBuyingCalendarQueries.Get(request.BuyingCalendar.Id);

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

                        order.IsAdaped = true;

                        foreach (var item in request.BuyingCalendar.Items)
                        {
                            var checkingItem = order.Items.First(i => i.Id == item.Id);
                            checkingItem.AdapQuantity = item.AdapQuantity;
                            await retailerBuyingCalendarRepository.UpdateItem(checkingItem);

                            order.IsAdaped = order.IsAdaped && checkingItem.AdapQuantity >= checkingItem.Quantity;
                        }

                        order.AdapNote = request.BuyingCalendar.AdapNote;
                        order          = UpdateBuild(order, request.LoginSession);
                        if (await retailerBuyingCalendarRepository.Update(order) != 0)
                        {
                            throw new BusinessException("RetailerPlanning.AdapFailed");
                        }
                    }
                    catch (Exception ex)
                    {
                        rs = -1;
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                            // notify
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }
            return(rs);
        }