protected static FR_L6SO_IPCA_1039 Execute(DbConnection Connection, DbTransaction Transaction, P_L6SO_IPCA_1039 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_L6SO_IPCA_1039();
            returnValue.Result = new L6SO_IPCA_1039();

            #region Check Header

            var shipmentHeader = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Header();
            shipmentHeader.Load(Connection, Transaction, Parameter.LOG_SHP_Shipment_HeaderID);

            if (shipmentHeader.IsReadyForPicking == false)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = true;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "IsReadyForPicking == false";
                return(returnValue);
            }

            if (shipmentHeader.IsManuallyCleared_ForPicking == false)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = true;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "IsManuallyCleared_ForPicking == false";
                return(returnValue);
            }

            if (shipmentHeader.HasPickingStarted == false)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = true;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "HasPickingStarted == false";
                return(returnValue);
            }

            if (shipmentHeader.HasPickingFinished == true)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = false;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "HasPickingFinished == true";
                return(returnValue);
            }

            if (shipmentHeader.IsShipped == true)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = false;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "IsShipped == true";
                return(returnValue);
            }

            if (shipmentHeader.IsBilled == true)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = false;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "IsBilled == true";
                return(returnValue);
            }

            #endregion

            #region Check Positions

            var shipmentPositions = ORM_LOG_SHP_Shipment_Position.Query.Search(Connection, Transaction, new ORM_LOG_SHP_Shipment_Position.Query()
            {
                LOG_SHP_Shipment_Header_RefID = Parameter.LOG_SHP_Shipment_HeaderID,
                IsDeleted = false
            });

            if (shipmentPositions.Count() == 0)
            {
                returnValue.Result.IsPickingControlAllowed = false;
                returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = false;
                returnValue.Result.IfPickingControlNotAllowed_Reason             = "shipmentPositions.Count() == 0";
                return(returnValue);
            }

            foreach (var shipmentPosition in shipmentPositions)
            {
                var quantityToShip = shipmentPosition.QuantityToShip;

                var reservations = ORM_LOG_RSV_Reservation.Query.Search(Connection, Transaction, new ORM_LOG_RSV_Reservation.Query()
                {
                    LOG_SHP_Shipment_Position_RefID = shipmentPosition.LOG_SHP_Shipment_PositionID,
                    IsReservationExecuted           = false,
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID
                });

                var reservedQuantity = reservations.Sum(i => i.ReservedQuantity);

                if (quantityToShip != reservedQuantity || reservedQuantity == 0)
                {
                    returnValue.Result.IsPickingControlAllowed = false;
                    returnValue.Result.IfPickingControlNotAllowed_ResetShipmentFlags = true;
                    returnValue.Result.IfPickingControlNotAllowed_Reason             = String.Format("Shipment position {0} with quantity {1}, has {2} reserved quantity",
                                                                                                     shipmentPosition.LOG_SHP_Shipment_PositionID.ToString(),
                                                                                                     shipmentPosition.QuantityToShip,
                                                                                                     reservedQuantity);
                    return(returnValue);
                }
            }

            #endregion

            returnValue.Result.IsPickingControlAllowed = true;
            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_L5CO_CSHwP_1519 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Base();

            #region Get CustomerOrderPositions

            var param = new P_L5CO_GACOPaOUDfCH_1623()
            {
                CustomerOrderHeaderID = Parameter.CustomerOrderHeaderID
            };
            var customerOrderPositions = cls_Get_AllCustomerOrderPositions_and_OrganizationalUnitDistributions_for_CustomerOrderHeaderID.Invoke(Connection, Transaction, param, securityTicket).Result;

            #endregion

            #region Create ShipmentHeaders

            var orgUnit2ShipmentHeader = new Dictionary <Guid, Guid>();

            var allOrganizationalUnitsInOrder = customerOrderPositions.SelectMany(i => i.OrgUnitAssigments).Select(j => j.CMN_BPT_CTM_OrganizationalUnit_RefID).Distinct();

            foreach (var organizationalUnits in allOrganizationalUnitsInOrder)
            {
                var shipmentHeaderParam = new P_L5CO_CSHfCO_1528()
                {
                    CustomerOrderHeaderID = Parameter.CustomerOrderHeaderID
                };

                var shipmentHeaderID = cls_Create_ShipmentHeader_for_CustomerOrderID.Invoke(Connection, Transaction, shipmentHeaderParam, securityTicket).Result;

                orgUnit2ShipmentHeader.Add(organizationalUnits, shipmentHeaderID);
            }

            #endregion

            #region Positions

            if (customerOrderPositions != null)
            {
                foreach (var position in customerOrderPositions)
                {
                    #region Orders for office

                    foreach (var item in position.OrgUnitAssigments)
                    {
                        var shippmentHeaderID = orgUnit2ShipmentHeader[item.CMN_BPT_CTM_OrganizationalUnit_RefID];

                        var shipmentPosition = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Position
                        {
                            LOG_SHP_Shipment_PositionID   = Guid.NewGuid(),
                            LOG_SHP_Shipment_Header_RefID = shippmentHeaderID,
                            CMN_PRO_Product_RefID         = position.CMN_PRO_Product_RefID,
                            Tenant_RefID       = securityTicket.TenantID,
                            Creation_Timestamp = DateTime.Now,
                            QuantityToShip     = item.Quantity,
                            ShipmentPosition_PricePerUnitValueWithoutTax = position.Position_ValuePerUnit,
                            ShipmentPosition_ValueWithoutTax             = (decimal)item.Quantity * position.Position_ValuePerUnit
                        };
                        shipmentPosition.Save(Connection, Transaction);

                        var customerOrderToShipmentPosition = new CL1_ORD_CUO.ORM_ORD_CUO_CustomerOrder_Position_2_ShipmentPosition
                        {
                            AssignmentID       = Guid.NewGuid(),
                            Creation_Timestamp = DateTime.Now,
                            Tenant_RefID       = securityTicket.TenantID,
                            LOG_SHP_Shipment_Position_RefID      = shipmentPosition.LOG_SHP_Shipment_PositionID,
                            ORD_CUO_CustomerOrder_Position_RefID = position.ORD_CUO_CustomerOrder_PositionID,
                            CMN_BPT_CTM_OrganizationalUnit_RefID = item.CMN_BPT_CTM_OrganizationalUnit_RefID,
                            IsDeleted = false
                        };
                        customerOrderToShipmentPosition.Save(Connection, Transaction);

                        #region Update ShipmentHeader

                        var shipmentHeader = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Header();
                        shipmentHeader.Load(Connection, Transaction, shippmentHeaderID);

                        shipmentHeader.ShipmentHeader_ValueWithoutTax += shipmentPosition.ShipmentPosition_ValueWithoutTax;
                        shipmentHeader.Save(Connection, Transaction);

                        #endregion
                    }

                    #endregion

                    #region Orders for company

                    var quantitiesForOffices = position.OrgUnitAssigments.Sum(i => i.Quantity);
                    var quantitiesForCompany = position.Position_Quantity - quantitiesForOffices;

                    if (quantitiesForCompany > 0)
                    {
                        var shippmentHeaderID    = Guid.Empty;
                        var containsOrderCompany = orgUnit2ShipmentHeader.ContainsKey(Guid.Empty);

                        if (!containsOrderCompany)
                        {
                            var shipmentHeaderParam = new P_L5CO_CSHfCO_1528()
                            {
                                CustomerOrderHeaderID = Parameter.CustomerOrderHeaderID
                            };

                            shippmentHeaderID = cls_Create_ShipmentHeader_for_CustomerOrderID.Invoke(Connection, Transaction, shipmentHeaderParam, securityTicket).Result;
                            orgUnit2ShipmentHeader.Add(Guid.Empty, shippmentHeaderID);
                        }

                        shippmentHeaderID = orgUnit2ShipmentHeader[Guid.Empty];

                        var shipmentPosition = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Position
                        {
                            LOG_SHP_Shipment_PositionID   = Guid.NewGuid(),
                            LOG_SHP_Shipment_Header_RefID = shippmentHeaderID,
                            CMN_PRO_Product_RefID         = position.CMN_PRO_Product_RefID,
                            Tenant_RefID       = securityTicket.TenantID,
                            Creation_Timestamp = DateTime.Now,
                            QuantityToShip     = quantitiesForCompany,
                            ShipmentPosition_PricePerUnitValueWithoutTax = position.Position_ValuePerUnit,
                            ShipmentPosition_ValueWithoutTax             = (decimal)quantitiesForCompany * position.Position_ValuePerUnit
                        };
                        shipmentPosition.Save(Connection, Transaction);

                        var customerOrderToShipmentPosition = new CL1_ORD_CUO.ORM_ORD_CUO_CustomerOrder_Position_2_ShipmentPosition
                        {
                            AssignmentID       = Guid.NewGuid(),
                            Creation_Timestamp = DateTime.Now,
                            Tenant_RefID       = securityTicket.TenantID,
                            LOG_SHP_Shipment_Position_RefID      = shipmentPosition.LOG_SHP_Shipment_PositionID,
                            ORD_CUO_CustomerOrder_Position_RefID = position.ORD_CUO_CustomerOrder_PositionID,
                            CMN_BPT_CTM_OrganizationalUnit_RefID = Guid.Empty
                        };
                        customerOrderToShipmentPosition.Save(Connection, Transaction);

                        #region Update ShipmentHeader

                        var shipmentHeader = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Header();
                        shipmentHeader.Load(Connection, Transaction, shippmentHeaderID);

                        shipmentHeader.ShipmentHeader_ValueWithoutTax += shipmentPosition.ShipmentPosition_ValueWithoutTax;
                        shipmentHeader.Save(Connection, Transaction);

                        #endregion
                    }

                    #endregion
                }
            }

            #endregion

            #region Comments

            var allOrgUnitIDs = orgUnit2ShipmentHeader.Select(x => x.Key);

            foreach (var orgUnitID in allOrgUnitIDs)
            {
                var currentShipmentID = orgUnit2ShipmentHeader[orgUnitID];

                var custOrderNotes = ORM_ORD_CUO_CustomerOrder_Note.Query.Search(Connection, Transaction, new ORM_ORD_CUO_CustomerOrder_Note.Query()
                {
                    CustomerOrder_Header_RefID           = Parameter.CustomerOrderHeaderID,
                    CMN_BPT_CTM_OrganizationalUnit_RefID = orgUnitID,
                    IsDeleted = false
                }
                                                                                 );

                foreach (var custOrderNote in custOrderNotes)
                {
                    var shipmentHeader = new CL1_LOG_SHP.ORM_LOG_SHP_Shipment_Header();
                    shipmentHeader.Load(Connection, Transaction, currentShipmentID);

                    var shipmentNote = new ORM_LOG_SHP_Shipment_Note()
                    {
                        LOG_SHP_Shipment_NoteID      = Guid.NewGuid(),
                        Shipment_Header_RefID        = currentShipmentID,
                        Shipment_Position_RefID      = Guid.Empty,
                        IsNotePrintedOnDeliveryPaper = true,
                        Title               = custOrderNote.Title,
                        Comment             = custOrderNote.Comment,
                        NotePublishDate     = custOrderNote.NotePublishDate,
                        SequenceOrderNumber = custOrderNote.SequenceOrderNumber,
                        Creation_Timestamp  = DateTime.Now,
                        Tenant_RefID        = securityTicket.TenantID,
                        CreatedBy_BusinessParticipant_RefID = shipmentHeader.RecipientBusinessParticipant_RefID
                    };

                    shipmentNote.Save(Connection, Transaction);
                }
            }

            #endregion

            return(returnValue);

            #endregion UserCode
        }