public IResult <PackSchedule> Execute(DateTime timestamp, CreatePackScheduleCommandParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var dateCreated    = parameters.Parameters.DateCreated?.Date ?? timestamp.Date;
            var sequence       = parameters.Parameters.Sequence ?? new EFUnitOfWorkHelper(_productionUnitOfWork).GetNextSequence <PackSchedule>(p => p.DateCreated == dateCreated, p => p.SequentialNumber);
            var employeeResult = new GetEmployeeCommand(_productionUnitOfWork).GetEmployee(parameters.Parameters);

            if (!employeeResult.Success)
            {
                return(employeeResult.ConvertTo <PackSchedule>());
            }

            Data.Models.Company customer = null;
            if (parameters.CustomerKey != null)
            {
                var companyResult = new GetCompanyCommand(_productionUnitOfWork).Execute(parameters.CustomerKey, CompanyType.Customer);
                if (!companyResult.Success)
                {
                    return(companyResult.ConvertTo <PackSchedule>());
                }
                customer = companyResult.ResultingObject;
            }

            var packSchedule = _productionUnitOfWork.PackScheduleRepository.Add(new PackSchedule
            {
                EmployeeId = employeeResult.ResultingObject.EmployeeId,
                TimeStamp  = timestamp,
                PackSchID  = DataConstants.SqlMinDate,
                PSNum      = parameters.Parameters.PSNum ?? _productionUnitOfWork.PackScheduleRepository.SourceQuery.Select(p => p.PSNum).Where(p => p != null).DefaultIfEmpty(0).Max().Value + 1,

                DateCreated      = dateCreated,
                SequentialNumber = sequence,

                WorkTypeId               = parameters.WorkTypeKey.WorkTypeKey_WorkTypeId,
                ChileProductId           = parameters.ChileProductKey.ChileProductKey_ProductId,
                PackagingProductId       = parameters.PackagingProductKey.PackagingProductKey_ProductId,
                ScheduledProductionDate  = parameters.Parameters.ScheduledProductionDate,
                ProductionDeadline       = parameters.Parameters.ProductionDeadline,
                ProductionLineLocationId = parameters.ProductionLocationKey.LocationKey_Id,
                SummaryOfWork            = parameters.Parameters.SummaryOfWork ?? "",
                CustomerId               = customer == null ? (int?)null : customer.Id,
                OrderNumber              = parameters.Parameters.OrderNumber.TrimTruncate(Constants.StringLengths.OrderNumber),

                DefaultBatchTargetParameters = new ProductionBatchTargetParameters(parameters.Parameters)
            });

            return(new SuccessResult <PackSchedule>(packSchedule));
        }
        internal IResult <SalesQuote> Execute(DateTime timestamp, SalesQuoteParameters parameters)
        {
            var employeeResult = new GetEmployeeCommand(_inventoryShipmentOrderUnitOfWork).GetEmployee(parameters.Parameters);

            if (!employeeResult.Success)
            {
                return(employeeResult.ConvertTo <SalesQuote>());
            }

            SalesQuote salesQuote;

            if (parameters.SalesQuoteNumber == null)
            {
                var date = timestamp.Date;
                var shipmentInfoResult = new CreateShipmentInformationCommand(_inventoryShipmentOrderUnitOfWork).Execute(date);
                if (!shipmentInfoResult.Success)
                {
                    return(shipmentInfoResult.ConvertTo <SalesQuote>());
                }

                var sequence            = new EFUnitOfWorkHelper(_inventoryShipmentOrderUnitOfWork).GetNextSequence <SalesQuote>(q => q.DateCreated == date, q => q.Sequence);
                var shipmentInformation = shipmentInfoResult.ResultingObject;
                var quoteNum            = (date.Year * 100) - 1;
                salesQuote = _inventoryShipmentOrderUnitOfWork.SalesQuoteRepository.Add(new SalesQuote
                {
                    DateCreated             = date,
                    Sequence                = sequence,
                    ShipmentInfoDateCreated = shipmentInformation.DateCreated,
                    ShipmentInfoSequence    = shipmentInformation.Sequence,
                    ShipmentInformation     = shipmentInformation,
                    Items = new List <SalesQuoteItem>(),

                    QuoteNum = _inventoryShipmentOrderUnitOfWork.SalesQuoteRepository.SourceQuery
                               .Select(q => q.QuoteNum)
                               .Where(q => q != null && q > quoteNum)
                               .DefaultIfEmpty(quoteNum)
                               .Max() + 1
                });
            }
            else
            {
                salesQuote = _inventoryShipmentOrderUnitOfWork.SalesQuoteRepository
                             .Filter(q => q.QuoteNum == parameters.SalesQuoteNumber,
                                     q => q.ShipmentInformation,
                                     q => q.Items)
                             .FirstOrDefault();
                if (salesQuote == null)
                {
                    return(new InvalidResult <SalesQuote>(null, string.Format(UserMessages.SalesQuoteNotFound_Num, parameters.SalesQuoteNumber)));
                }
            }

            Facility sourceFacility = null;

            if (parameters.SourceFacilityKey != null)
            {
                sourceFacility = _inventoryShipmentOrderUnitOfWork.FacilityRepository.FindByKey(parameters.SourceFacilityKey);
                if (sourceFacility == null)
                {
                    return(new InvalidResult <SalesQuote>(null, string.Format(UserMessages.FacilityNotFound, parameters.SourceFacilityKey)));
                }
            }

            Company customer          = null;
            var     getCompanyCommand = new GetCompanyCommand(_inventoryShipmentOrderUnitOfWork);

            if (parameters.CustomerKey != null)
            {
                var customerResult = getCompanyCommand.Execute(parameters.CustomerKey.ToCompanyKey(), CompanyType.Customer);
                if (!customerResult.Success)
                {
                    return(customerResult.ConvertTo <SalesQuote>());
                }
                customer = customerResult.ResultingObject;
            }

            Company broker = null;

            if (parameters.BrokerKey != null)
            {
                var brokerResult = getCompanyCommand.Execute(parameters.BrokerKey, CompanyType.Broker);
                if (!brokerResult.Success)
                {
                    return(brokerResult.ConvertTo <SalesQuote>());
                }
                broker = brokerResult.ResultingObject;
            }

            salesQuote.EmployeeId = employeeResult.ResultingObject.EmployeeId;
            salesQuote.TimeStamp  = timestamp;

            salesQuote.SourceFacilityId = sourceFacility == null ? (int?)null : sourceFacility.Id;
            salesQuote.CustomerId       = customer == null ? (int?)null : customer.CompanyKey_Id;
            salesQuote.BrokerId         = broker == null ? (int?)null : broker.CompanyKey_Id;

            salesQuote.QuoteDate    = parameters.Parameters.QuoteDate;
            salesQuote.DateReceived = parameters.Parameters.DateReceived;
            salesQuote.CalledBy     = parameters.Parameters.CalledBy;
            salesQuote.TakenBy      = parameters.Parameters.TakenBy;
            salesQuote.PaymentTerms = parameters.Parameters.PaymentTerms;

            ShippingLabel shipFromOrSoldTo = null;

            if (parameters.Parameters.ShipmentInformation != null && parameters.Parameters.ShipmentInformation.ShippingInstructions != null)
            {
                shipFromOrSoldTo = parameters.Parameters.ShipmentInformation.ShippingInstructions.ShipFromOrSoldTo;
            }
            salesQuote.SoldTo.SetShippingLabel(shipFromOrSoldTo);
            salesQuote.ShipmentInformation.SetShipmentInformation(parameters.Parameters.ShipmentInformation, false);

            var itemSequence  = 0;
            var itemsToRemove = salesQuote.Items.ToDictionary(i => i.ToSalesQuoteItemKey(), i =>
            {
                itemSequence = Math.Max(itemSequence, i.ItemSequence);
                return(i);
            });

            foreach (var item in parameters.Items)
            {
                SalesQuoteItem salesQuoteItem = null;
                if (item.SalesQuoteItemKey != null)
                {
                    if (itemsToRemove.TryGetValue(item.SalesQuoteItemKey, out salesQuoteItem))
                    {
                        itemsToRemove.Remove(item.SalesQuoteItemKey);
                    }
                    else
                    {
                        return(new InvalidResult <SalesQuote>(null, string.Format(UserMessages.SalesQuoteItemNotFound, item.SalesQuoteItemKey)));
                    }
                }
                else
                {
                    salesQuote.Items.Add(salesQuoteItem = new SalesQuoteItem
                    {
                        DateCreated  = salesQuote.DateCreated,
                        Sequence     = salesQuote.Sequence,
                        ItemSequence = ++itemSequence
                    });
                }

                salesQuoteItem.ProductId           = item.ProductKey.ProductKey_ProductId;
                salesQuoteItem.PackagingProductId  = item.PackagingProductKey.PackagingProductKey_ProductId;
                salesQuoteItem.TreatmentId         = item.InventoryTreatmentKey.InventoryTreatmentKey_Id;
                salesQuoteItem.Quantity            = item.Parameters.Quantity;
                salesQuoteItem.PriceBase           = item.Parameters.PriceBase;
                salesQuoteItem.PriceFreight        = item.Parameters.PriceFreight;
                salesQuoteItem.PriceTreatment      = item.Parameters.PriceTreatment;
                salesQuoteItem.PriceWarehouse      = item.Parameters.PriceWarehouse;
                salesQuoteItem.PriceRebate         = item.Parameters.PriceRebate;
                salesQuoteItem.CustomerProductCode = item.Parameters.CustomerProductCode;
            }

            foreach (var item in itemsToRemove.Values)
            {
                _inventoryShipmentOrderUnitOfWork.SalesQuoteItemRepository.Remove(item);
            }

            return(new SuccessResult <SalesQuote>(salesQuote));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(GetCompanyCommand cmd)
        {
            var fields = await _mediator.Send(cmd, HttpContext.RequestAborted);

            return(View(fields));
        }
        public IResult <PackSchedule> Execute(DateTime timestamp, UpdatePackScheduleCommandParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var packSchedule = _productionUnitOfWork.PackScheduleRepository.FindByKey(parameters.PackScheduleKey,
                                                                                      p => p.ChileProduct,
                                                                                      p => p.ProductionBatches.Select(b => b.Production.ResultingChileLot.Lot));

            if (packSchedule == null)
            {
                return(new InvalidResult <PackSchedule>(null, string.Format(UserMessages.PackScheduleNotFound, parameters.PackScheduleKey.KeyValue)));
            }

            var employeeResult = new GetEmployeeCommand(_productionUnitOfWork).GetEmployee(parameters.Parameters);

            if (!employeeResult.Success)
            {
                return(employeeResult.ConvertTo <PackSchedule>());
            }

            Data.Models.Company customer = null;
            if (parameters.CustomerKey != null)
            {
                var companyResult = new GetCompanyCommand(_productionUnitOfWork).Execute(parameters.CustomerKey, CompanyType.Customer);
                if (!companyResult.Success)
                {
                    return(companyResult.ConvertTo <PackSchedule>());
                }
                customer = companyResult.ResultingObject;
            }

            if (!parameters.ChileProductKey.Equals(packSchedule))
            {
                if (packSchedule.ProductionBatches.Any())
                {
                    var chileProduct = _productionUnitOfWork.ChileProductRepository.FindByKey(parameters.ChileProductKey);
                    if (chileProduct == null)
                    {
                        return(new InvalidResult <PackSchedule>(null, string.Format(UserMessages.ChileProductNotFound, parameters.ChileProductKey)));
                    }

                    if (chileProduct.ChileState.ToLotType() != packSchedule.ChileProduct.ChileState.ToLotType())
                    {
                        return(new InvalidResult <PackSchedule>(null, UserMessages.ChileProductDifferentLotType));
                    }

                    var completedBatch = packSchedule.ProductionBatches.FirstOrDefault(b => b.ProductionHasBeenCompleted);
                    if (completedBatch != null)
                    {
                        return(new InvalidResult <PackSchedule>(null, string.Format(UserMessages.ProductionBatchAlreadyComplete, new LotKey(completedBatch))));
                    }

                    foreach (var batch in packSchedule.ProductionBatches)
                    {
                        batch.Production.ResultingChileLot.ChileProductId = parameters.ChileProductKey.ChileProductKey_ProductId;
                    }
                }
                packSchedule.ChileProductId = parameters.ChileProductKey.ChileProductKey_ProductId;
            }

            if (!parameters.PackagingProductKey.Equals(packSchedule))
            {
                foreach (var batch in packSchedule.ProductionBatches)
                {
                    batch.Production.ResultingChileLot.Lot.ReceivedPackagingProductId = parameters.PackagingProductKey.PackagingProductKey_ProductId;
                }
                packSchedule.PackagingProductId = parameters.PackagingProductKey.PackagingProductKey_ProductId;
            }

            packSchedule.EmployeeId = employeeResult.ResultingObject.EmployeeId;
            packSchedule.TimeStamp  = timestamp;

            packSchedule.ScheduledProductionDate  = parameters.Parameters.ScheduledProductionDate;
            packSchedule.ProductionDeadline       = parameters.Parameters.ProductionDeadline;
            packSchedule.ProductionLineLocationId = parameters.ProductionLocationKey.LocationKey_Id;
            packSchedule.SummaryOfWork            = parameters.Parameters.SummaryOfWork;
            packSchedule.WorkTypeId  = parameters.WorkTypeKey.WorkTypeKey_WorkTypeId;
            packSchedule.CustomerId  = customer == null ? (int?)null : customer.Id;
            packSchedule.OrderNumber = parameters.Parameters.OrderNumber.TrimTruncate(Constants.StringLengths.OrderNumber);

            packSchedule.DefaultBatchTargetParameters = new ProductionBatchTargetParameters(parameters.Parameters);

            return(new SuccessResult <PackSchedule>(packSchedule));
        }