예제 #1
0
        public async Task <IActionResult> CreateLoad([FromBody] LoadDetailViewModel load, [FromQuery] bool useSmartSpotPricing = false)
        {
            try
            {
                var loadData        = _mapper.Map <LoadDetailData>(load);
                var custIdentUserId = _user.UserId.Value;
                var autoPost        = load.AutoPostLoad ?? _loadService.GetAutoPostToLoadshop(custIdentUserId);

                if (autoPost)
                {
                    loadData.LoadTransaction = new LoadTransactionData
                    {
                        TransactionType = TransactionTypeData.New
                    };

                    /**
                     * Even when the shipper profile says the load should AutoPost, we do not
                     * allow that if the load does not have a line haul rate and the TOPS
                     * LoadshopShipperMapping is set to not use SmartSpot pricing.
                     * Also, do not allow the load to auto post if the load uses a Fuel Rate
                     * set by the customer and Fuel Rate is 0.
                     */
                    if ((loadData.LineHaulRate == 0m && !useSmartSpotPricing) ||
                        (load.UseCustomerFuel && load.FuelRate == 0m))
                    {
                        loadData.LoadTransaction = new LoadTransactionData
                        {
                            TransactionType = TransactionTypeData.PendingAdd
                        };
                    }
                }
                else
                {
                    loadData.LoadTransaction = new LoadTransactionData
                    {
                        TransactionType = TransactionTypeData.PendingAdd
                    };
                }

                var options = new CreateLoadOptionsDto()
                {
                    ManuallyCreated               = false,
                    AddSpecialInstructions        = true,
                    AddSmartSpot                  = useSmartSpotPricing,
                    OverrideLineHaulWithSmartSpot = useSmartSpotPricing
                };

                var response = await _loadService.CreateLoad(loadData, customerId : _user.UserId.Value, username : _systemUser, options);

                if (response.IsSuccess)
                {
                    return(Success(_mapper.Map <LoadDetailViewModel>(response.Data)));
                }

                throw new ValidationException(response.GetAllMessages());
            }
            catch (ValidationException e)
            {
                return(Error <LoadDetailViewModel>(e));
            }
        }