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

            var shipping = await cFShippingQueries.Get(request.Shipping.Id);

            if (shipping == null)
            {
                throw new BusinessException("Trip.NotExisted");
            }

            if (shipping.StatusId != (int)Distributions.UI.TripStatuses.Created)
            {
                throw new BusinessException("Common.ErrorWithStep");
            }

            var employee = await WebHelper.HttpGet <Employee>(GlobalConfiguration.APIGateWayURI, AppUrl.GetEmployeeByUser, request.LoginSession.AccessToken);

            if (employee == null)
            {
                throw new NotPermissionException();
            }
            var collection = (await collectionQueries.GetsByEmployeeId(employee.Id)).FirstOrDefault(c => c.Id == shipping.CollectionId);

            if (collection == null)
            {
                throw new NotPermissionException();
            }

            shipping = UpdateBuild(shipping, request.LoginSession);
            shipping.FulfillmentId = request.Shipping.FulfillmentId;
            shipping.VehicleId     = request.Shipping.VehicleId;
            shipping.ShipperId     = request.Shipping.ShipperId;
            shipping.DeliveryDate  = request.Shipping.DeliveryDate;
            return(await cFShippingRepository.Update(shipping));
        }
        public override async Task <int> HandleCommand(UpdateItemCommand request, CancellationToken cancellationToken)
        {
            if (request.ShippingId == 0 || request.Items == null && request.Items.Count == 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            var shipping = await cFShippingQueries.Get(request.ShippingId);

            if (shipping == null)
            {
                throw new BusinessException("Trip.NotExisted");
            }

            if (shipping.StatusId != (int)Distributions.UI.TripStatuses.Created)
            {
                throw new BusinessException("Common.ErrorWithStep");
            }

            var employee = await WebHelper.HttpGet <Employee>(GlobalConfiguration.APIGateWayURI, AppUrl.GetEmployeeByUser, request.LoginSession.AccessToken);

            if (employee == null)
            {
                throw new NotPermissionException();
            }
            var collection = (await collectionQueries.GetsByEmployeeId(employee.Id)).FirstOrDefault(c => c.Id == shipping.CollectionId);

            if (collection == null)
            {
                throw new NotPermissionException();
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        await cFShippingRepository.DeleteItems(shipping.Id);

                        foreach (var item in request.Items)
                        {
                            if (item.ShippedQuantity == 0)
                            {
                                throw new BusinessException("");
                            }
                            item.ShippingId         = shipping.Id;
                            item.DeliveriedQuantity = 0;
                            await cFShippingRepository.AddItem(item);
                        }

                        shipping = UpdateBuild(shipping, request.LoginSession);
                        rs       = await cFShippingRepository.Update(shipping);
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            tran.Commit();
                        }
                        else
                        {
                            try
                            {
                                tran.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }
            return(rs);
        }
Exemple #3
0
        public override async Task <int> HandleCommand(AddFcCommand request, CancellationToken cancellationToken)
        {
            if (request.FulfillmentCollection == null || request.FulfillmentCollection.ID == 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            var rs = -1;

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

                        ////Add flu-col
                        request.FulfillmentCollection          = CreateBuild(request.FulfillmentCollection, request.LoginSession);
                        request.FulfillmentCollection.StatusId = request.FulfillmentCollection.StatusFulCols.FirstOrDefault().Id;
                        var rsFulCol = await fulfillmentRepository.Add(request.FulfillmentCollection);

                        if (rsFulCol >= 0)
                        {
                            //Add flu_col_item
                            if (request.FulfillmentCollection.Items.Count != 0 && request.FulfillmentCollection.StatusFulCols.FirstOrDefault().Id != 2)
                            {
                                foreach (var fcItems in request.FulfillmentCollection.Items)
                                {
                                    fcItems.ShippingId = rsFulCol;

                                    rs = (await fulfillmentRepository.AddItems(fcItems) > 0) ? 0 : -1;;
                                }
                            }
                            if (rs != -1)
                            {
                                //Update status cf_shipping
                                var itemUp = await cFShippingQueries.Get(request.FulfillmentCollection.ID);

                                if (itemUp != null)
                                {
                                    itemUp.StatusId = 4;
                                    var cf_shpping = await iCFShippingRepository.Update(itemUp);
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(rs);
        }
        public override async Task <int> HandleCommand(UpdateStatusCommand request, CancellationToken cancellationToken)
        {
            if (request.ShippingId == 0 || request.StatusId == 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            if (!Enum.IsDefined(typeof(TripStatuses), (int)request.StatusId))
            {
                throw new BusinessException("Trip.NotExistedStatus");
            }
            var status = (TripStatuses)request.StatusId;

            var shipping = await cFShippingQueries.Get(request.ShippingId);

            if (shipping == null)
            {
                throw new BusinessException("Trip.NotExisted");
            }

            if (shipping.StatusId == (int)TripStatuses.Finished || shipping.StatusId == (int)TripStatuses.Canceled)
            {
                throw new BusinessException("Trip.WrongStep");
            }

            if (status != TripStatuses.Canceled)
            {
                if (request.StatusId != shipping.StatusId + 1)
                {
                    throw new BusinessException("Trip.WrongStep");
                }
            }

            if (status == TripStatuses.Confirmed || (status == TripStatuses.Canceled && shipping.StatusId != (int)TripStatuses.Created))
            {
                var employee = await WebHelper.HttpGet <Employee>(GlobalConfiguration.APIGateWayURI, UI.AppUrl.GetEmployeeByUser, request.LoginSession.AccessToken);

                if (employee == null)
                {
                    throw new NotPermissionException();
                }
                var collection = (await collectionQueries.GetsByEmployeeId(employee.Id)).FirstOrDefault(c => c.Id == shipping.CollectionId);
                if (collection == null)
                {
                    throw new NotPermissionException();
                }

                if (status == TripStatuses.Confirmed && shipping.ShipperId == null)
                {
                    throw new BusinessException("Distribution.Trip.RequiredDeliveryMan");
                }

                using (var conn = DALHelper.GetConnection())
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        var rs = -1;
                        try
                        {
                            cFShippingQueries.JoinTransaction(conn, trans);
                            cFShippingRepository.JoinTransaction(conn, trans);
                            collectionInventoryQueries.JoinTransaction(conn, trans);
                            collectionInventoryHistoryRepository.JoinTransaction(conn, trans);

                            var items = await cFShippingQueries.GetItems(shipping.Id);

                            if (items.Count() == 0)
                            {
                                throw new BusinessException("Collection.Shipping.NotExsitedItem");
                            }
                            foreach (var item in items)
                            {
                                var inventory = await collectionInventoryQueries.GetByTraceCode(item.TraceCode);

                                if (status == TripStatuses.Confirmed)
                                {
                                    if (inventory == null || inventory.CollectionId != shipping.CollectionId)
                                    {
                                        throw new BusinessException("Collection.Inventory.TraceCode.NotExisted");
                                    }

                                    if (inventory.Quantity < item.ShippedQuantity)
                                    {
                                        throw new BusinessException("Collection.Inventory.TraceCode.NotEnoughQuantity");
                                    }

                                    inventory.Quantity -= item.ShippedQuantity;
                                    if (inventory.Quantity == 0)
                                    {
                                        await collectionInventoryRepository.Delete(inventory.Id);
                                    }
                                    else
                                    {
                                        inventory.ModifiedDate = DateTime.Now;
                                        inventory.ModifiedBy   = request.LoginSession.Id;
                                        await collectionInventoryRepository.Update(inventory);
                                    }
                                    await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                                    {
                                        TraceCode    = inventory.TraceCode,
                                        CollectionId = inventory.CollectionId,
                                        ProductId    = inventory.ProductId,
                                        UoMId        = inventory.UoMId,
                                        Direction    = 0,
                                        Quantity     = item.ShippedQuantity,
                                        LastQuantity = inventory.Quantity,
                                        CreatedDate  = DateTime.Now,
                                        CreatedBy    = request.LoginSession.Id
                                    });
                                }
                                else
                                {
                                    if (inventory == null)
                                    {
                                        await collectionInventoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventory()
                                        {
                                            CollectionId = shipping.CollectionId,
                                            ProductId    = item.ProductId,
                                            UoMId        = item.UoMId,
                                            TraceCode    = item.TraceCode,
                                            Quantity     = item.ShippedQuantity,
                                            ModifiedBy   = request.LoginSession.Id,
                                            ModifiedDate = DateTime.Now
                                        });
                                    }
                                    else
                                    {
                                        inventory.Quantity    += item.ShippedQuantity;
                                        inventory.ModifiedDate = DateTime.Now;
                                        inventory.ModifiedBy   = request.LoginSession.Id;
                                        await collectionInventoryRepository.Update(inventory);
                                    }

                                    await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                                    {
                                        TraceCode    = item.TraceCode,
                                        CollectionId = shipping.CollectionId,
                                        ProductId    = item.ProductId,
                                        UoMId        = item.UoMId,
                                        Direction    = 1,
                                        Quantity     = item.ShippedQuantity,
                                        LastQuantity = inventory == null ? item.ShippedQuantity : item.ShippedQuantity + inventory.Quantity,
                                        CreatedDate  = DateTime.Now,
                                        CreatedBy    = request.LoginSession.Id
                                    });
                                }
                            }

                            shipping          = UpdateBuild(shipping, request.LoginSession);
                            shipping.StatusId = request.StatusId;
                            return(rs = await cFShippingRepository.Update(shipping));
                        }
                        finally
                        {
                            if (rs == 0)
                            {
                                trans.Commit();
                            }
                            else
                            {
                                try
                                {
                                    trans.Rollback();
                                }
                                catch { }
                            }
                        }
                    }
                }
            }
            else
            {
                shipping          = UpdateBuild(shipping, request.LoginSession);
                shipping.StatusId = request.StatusId;
                return(await cFShippingRepository.Update(shipping));
            }
        }