示例#1
0
        internal IResult <SalesOrder> Execute(DateTime timestamp, CreateSalesOrderConductorParameters parameters)
        {
            if (timestamp == null)
            {
                throw new ArgumentNullException("timestamp");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

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

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

            Customer customer = null;

            if (parameters.CustomerKey != null)
            {
                customer = SalesUnitOfWork.CustomerRepository.FindByKey(parameters.CustomerKey,
                                                                        c => c.Company.Contacts,
                                                                        c => c.Contracts.Select(r => r.ContractItems));
            }

            if (customer == null && !parameters.CreateParameters.IsMiscellaneous)
            {
                return(new InvalidResult <SalesOrder>(null, string.Format(UserMessages.CustomerNotFound, parameters.CustomerKey)));
            }

            var pickedInventoryResult = new CreatePickedInventoryCommand(SalesUnitOfWork).Execute(new CreatePickedInventoryCommandParameters
            {
                EmployeeKey  = employeeResult.ResultingObject.ToEmployeeKey(),
                PickedReason = PickedReason.SalesOrder,
                TimeStamp    = timestamp
            });

            if (!pickedInventoryResult.Success)
            {
                pickedInventoryResult.ConvertTo <SalesOrder>();
            }
            var pickedInventory = pickedInventoryResult.ResultingObject;

            pickedInventory.Items = null;

            var pickOrderResult = new CreateInventoryPickOrderCommand(SalesUnitOfWork).Execute(pickedInventory);

            if (!pickOrderResult.Success)
            {
                return(pickOrderResult.ConvertTo <SalesOrder>());
            }
            var pickOrder = pickOrderResult.ResultingObject;

            pickOrder.Items = null;

            var shipmentInformationResult = new CreateShipmentInformationCommand(SalesUnitOfWork).Execute(timestamp.Date);

            if (!shipmentInformationResult.Success)
            {
                return(shipmentInformationResult.ConvertTo <SalesOrder>());
            }
            var shipmentInfo = shipmentInformationResult.ResultingObject;

            var salesOrder = new SalesOrder
            {
                DateCreated = pickedInventory.DateCreated,
                Sequence    = pickedInventory.Sequence,

                CustomerId = customer == null ? (int?)null : customer.Id,
                BrokerId   = customer == null ? (int?)null : customer.BrokerId,

                InventoryShipmentOrder = new InventoryShipmentOrder
                {
                    DateCreated = pickedInventory.DateCreated,
                    Sequence    = pickedInventory.Sequence,
                    EmployeeId  = employeeResult.ResultingObject.EmployeeId,
                    TimeStamp   = timestamp,

                    OrderType = parameters.CreateParameters.IsMiscellaneous ? InventoryShipmentOrderTypeEnum.MiscellaneousOrder : InventoryShipmentOrderTypeEnum.SalesOrder,
                    ShipmentInfoDateCreated = shipmentInfo.DateCreated,
                    ShipmentInfoSequence    = shipmentInfo.Sequence,
                    ShipmentInformation     = shipmentInfo,

                    MoveNum = SalesUnitOfWork.SalesOrderRepository.SourceQuery.Select(s => s.InventoryShipmentOrder.MoveNum).Where(n => n != null).DefaultIfEmpty(0).Max() + 1
                },

                Customer    = customer,
                OrderStatus = SalesOrderStatus.Ordered
            };

            var setResult = SetSalesOrder(salesOrder, timestamp, parameters);

            if (!setResult.Success)
            {
                return(setResult);
            }

            salesOrder = SalesUnitOfWork.SalesOrderRepository.Add(salesOrder);

            return(new SuccessResult <SalesOrder>(salesOrder));
        }
        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));
        }
示例#3
0
        public IResult <TreatmentOrder> CreateTreatmentOrder <TParams>(DateTime timestamp, CreateTreatmentOrderConductorParameters <TParams> parameters)
            where TParams : ICreateTreatmentOrderParameters
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var employeeResult = new GetEmployeeCommand(_treatmentOrderUnitOfWork).GetEmployee(parameters.Params);

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

            var dateCreated           = timestamp.Date;
            var pickedInventoryResult = new CreatePickedInventoryCommand(_treatmentOrderUnitOfWork).Execute(new CreatePickedInventoryCommandParameters
            {
                EmployeeKey  = new EmployeeKey(employeeResult.ResultingObject),
                TimeStamp    = dateCreated,
                PickedReason = PickedReason.TreatmentOrder
            });

            if (!pickedInventoryResult.Success)
            {
                return(pickedInventoryResult.ConvertTo <TreatmentOrder>());
            }

            var pickedOrderResult = new CreateInventoryPickOrderCommand(_treatmentOrderUnitOfWork).Execute(pickedInventoryResult.ResultingObject);

            if (!pickedOrderResult.Success)
            {
                return(pickedOrderResult.ConvertTo <TreatmentOrder>());
            }

            var shipmentInfoResult = new CreateShipmentInformationCommand(_treatmentOrderUnitOfWork).Execute(dateCreated);

            if (!shipmentInfoResult.Success)
            {
                return(shipmentInfoResult.ConvertTo <TreatmentOrder>());
            }

            var treatmentOrder = _treatmentOrderUnitOfWork.TreatmentOrderRepository.Add(new TreatmentOrder
            {
                DateCreated          = pickedInventoryResult.ResultingObject.DateCreated,
                Sequence             = pickedInventoryResult.ResultingObject.Sequence,
                InventoryTreatmentId = parameters.TreatmentKey.InventoryTreatmentKey_Id,

                InventoryShipmentOrder = new InventoryShipmentOrder
                {
                    DateCreated = pickedInventoryResult.ResultingObject.DateCreated,
                    Sequence    = pickedInventoryResult.ResultingObject.Sequence,

                    OrderType   = InventoryShipmentOrderTypeEnum.TreatmentOrder,
                    OrderStatus = OrderStatus.Scheduled,

                    ShipmentInfoDateCreated = shipmentInfoResult.ResultingObject.DateCreated,
                    ShipmentInfoSequence    = shipmentInfoResult.ResultingObject.Sequence,
                    ShipmentInformation     = shipmentInfoResult.ResultingObject,

                    InventoryPickOrder = pickedOrderResult.ResultingObject,
                    PickedInventory    = pickedInventoryResult.ResultingObject,
                    MoveNum            = new GetMoveNum(_treatmentOrderUnitOfWork.InventoryShipmentOrderRepository).Get(pickedInventoryResult.ResultingObject.DateCreated.Year)
                }
            });

            return(new UpdateTreatmentOrderConductor(_treatmentOrderUnitOfWork).Update(treatmentOrder, timestamp, parameters));
        }
        internal IResult <InventoryShipmentOrder> Create <TParams>(DateTime timestamp, SetInventoryShipmentOrderConductorParameters <TParams> parameters)
            where TParams : ISetOrderParameters
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var employeeResult = new GetEmployeeCommand(_interWarehouseOrderUnitOfWork).GetEmployee(parameters.Params);

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

            var pickedInventoryResult = new CreatePickedInventoryCommand(_interWarehouseOrderUnitOfWork).Execute(new CreatePickedInventoryCommandParameters
            {
                EmployeeKey  = new EmployeeKey(employeeResult.ResultingObject),
                TimeStamp    = timestamp,
                PickedReason = PickedReason.InterWarehouseMovement
            });

            if (!pickedInventoryResult.Success)
            {
                return(pickedInventoryResult.ConvertTo <InventoryShipmentOrder>());
            }

            var pickedOrderResult = new CreateInventoryPickOrderCommand(_interWarehouseOrderUnitOfWork).Execute(pickedInventoryResult.ResultingObject);

            if (!pickedOrderResult.Success)
            {
                return(pickedOrderResult.ConvertTo <InventoryShipmentOrder>());
            }

            var shipmentInfoResult = new CreateShipmentInformationCommand(_interWarehouseOrderUnitOfWork).Execute(timestamp, parameters.Params.SetShipmentInformation);

            if (!shipmentInfoResult.Success)
            {
                return(shipmentInfoResult.ConvertTo <InventoryShipmentOrder>());
            }

            var order = new InventoryShipmentOrder
            {
                OrderType               = InventoryShipmentOrderTypeEnum.InterWarehouseOrder,
                OrderStatus             = OrderStatus.Scheduled,
                DateCreated             = pickedInventoryResult.ResultingObject.DateCreated,
                Sequence                = pickedInventoryResult.ResultingObject.Sequence,
                PickedInventory         = pickedInventoryResult.ResultingObject,
                InventoryPickOrder      = pickedOrderResult.ResultingObject,
                ShipmentInformation     = shipmentInfoResult.ResultingObject,
                ShipmentInfoDateCreated = shipmentInfoResult.ResultingObject.DateCreated,
                ShipmentInfoSequence    = shipmentInfoResult.ResultingObject.Sequence,
                MoveNum = new GetMoveNum(_interWarehouseOrderUnitOfWork.InventoryShipmentOrderRepository).Get(pickedInventoryResult.ResultingObject.DateCreated.Year)
            };

            if (parameters.Params.HeaderParameters != null)
            {
                order.SetHeaderParameters(parameters.Params.HeaderParameters);
            }

            order = _interWarehouseOrderUnitOfWork.InventoryShipmentOrderRepository.Add(order);

            return(new UpdateInterWarehouseOrderConductor(_interWarehouseOrderUnitOfWork).Update(order, timestamp, parameters));
        }