/// <summary>
        /// Generates the and update commercial warehouse valuations.
        /// </summary>
        /// <param name="document">The document that contains lines to generate and update valuations.</param>
        private void GenerateAndUpdateCommercialWarehouseValuations(WarehouseDocument document)
        {
            if (document.SkipManualValuations)
            {
                return;
            }

            this.ValuateFromOutcome(document);

            foreach (WarehouseDocumentLine line in document.Lines.Children)
            {
                int  cwvCount = line.CommercialWarehouseValuations.Children.Count;
                bool beforeSystemStartCorrectionLine = line.Direction < 0 && line.Quantity < 0;

                if (line.Value == 0)
                {
                    continue;
                    //line.CommercialWarehouseValuations.RemoveAll();
                    //document.Value = 0;
                }
                else if (line.Value != 0 && cwvCount >= 0 && cwvCount <= 1)                //create new valuation or remove and create (update)
                {
                    if (cwvCount == 1)
                    {
                        line.CommercialWarehouseValuations.RemoveAll();
                    }
                    CommercialWarehouseValuation valuation = line.CommercialWarehouseValuations.CreateNew();
                    valuation.Quantity = line.Quantity;
                    valuation.Price    = line.Price;
                    valuation.Value    = line.Value;
                    //Jeśli mamy do czynienia z korektą sprzed startu systemu wiąże wycenę z pozycją korekty sprzedażowej
                    //na podstawie commercialWarehouseRelation, która jest prawidłowo powiązana
                    //potrzebne to aby koszt na takiej fakturze był widoczny w zestawieniu
                    if (beforeSystemStartCorrectionLine)
                    {
                        CommercialWarehouseRelation cwr = line.CommercialWarehouseRelations.FirstOrDefault();
                        if (cwr != null && cwr.RelatedLine != null)
                        {
                            valuation.RelatedLine = cwr.RelatedLine;
                            cwr.Value             = valuation.Price;
                            cwr.IsValuated        = true;
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("Critical and fatal exception: too many commercial warehouse valuations to update 'mon!");
                }
            }
        }
        /// <summary>
        /// Creates object responsible for processing package of specified type.
        /// </summary>
        /// <param name="packageType">Type of the package to processed.</param>
        /// <param name="xmlPackage">The XML package to process.</param>
        /// <returns>Created package processing object.</returns>
        public IExecutingScript CreateScript(string packageType, XDocument xmlPackage)
        {
            if (packageType == null)
            {
                throw new ArgumentNullException("packageType");
            }

            CommunicationPackageType packageTypeVal;

            try
            {
                packageTypeVal = (CommunicationPackageType)Enum.Parse(typeof(CommunicationPackageType), packageType);
            }
            catch (ArgumentException)
            {
                packageTypeVal = CommunicationPackageType.Unknown;
            }
            IExecutingScript script = null;

            switch (packageTypeVal)
            {
            case CommunicationPackageType.ComplaintDocumentSnapshot:
                script = new ComplaintDocumentSnapshot(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.Configuration:
                script = new ConfigurationScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ContractorSnapshot:
                script = new ContractorScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ContractorRelations:
                script = new ContractorRelationsScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ContractorGroupMembership:
                script = new ContractorGroupMembershipScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.DictionaryPackage:
                script = new DictionarySnapshotScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.FileDescriptor:
                script = new FileDescriptorScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.FinancialReport:
                script = new FinancialReportScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.FinancialDocumentSnapshot:
                script = new FinancialDocumentScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.InventoryDocumentSnapshot:
                script = new InventoryDocumentScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ItemSnapshot:
                script = new ItemSnapshotScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ItemRelation:
                script = new ItemRelationScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ItemUnitRelation:
                script = new ItemUnitRelationScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ItemGroupMembership:
                script = new ItemGroupMembershipScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.CommercialDocumentSnapshot:
                script = new CommercialDocumentScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.CommercialDocumentSnapshotEx:
                script = new CommercialDocumentExScript(this.UnitOfWork, this.ExecutionController, this.IsHeadquarter);
                break;

            case CommunicationPackageType.Series:
                script = new SeriesScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.CommercialWarehouseValuation:
                script = new CommercialWarehouseValuation(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.CommercialWarehouseRelation:
                script = new CommercialWarehouseRelation(UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.DocumentRelation:
                script = new DocumentRelationScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.IncomeOutcomeRelation:
                script = new IncomeOutcomeRelation(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.Payment:
                script = new PaymentScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.WarehouseDocumentValuation:
                script = new WarehouseDocumentValuation(this.UnitOfWork, this.ExecutionController, this.IsHeadquarter);
                break;

            //TODO !! aktualnie WZk dotykajaca mmki bedzie traktowana jak by nie miala nic wspolnego z mmka
            //     czyli nie zostanie przeslana do oddzialu docelowego
            case CommunicationPackageType.WarehouseDocumentSnapshot:
                if (xmlPackage.Root.Element("warehouseDocumentLine").Elements("entry").All(row => row.Element("isDistributed").Value.Equals("1")))
                {
                    DocumentType docType = DictionaryMapper.Instance.GetDocumentType(new Guid(xmlPackage.Root.Element("warehouseDocumentHeader")
                                                                                              .Element("entry").Element("documentTypeId").Value));
                    if (docType.DocumentCategory != Makolab.Fractus.Kernel.Enums.DocumentCategory.IncomeWarehouseCorrection && docType.DocumentCategory != Makolab.Fractus.Kernel.Enums.DocumentCategory.OutcomeWarehouseCorrection)
                    {
                        script = new ShiftDocumentSnapshot(this.UnitOfWork, this.ExecutionController, this.IsHeadquarter);
                    }
                    else
                    {
                        script = new WarehouseDocumentSnapshot(this.UnitOfWork, this.ExecutionController, this.IsHeadquarter);
                    }
                }
                else
                {
                    script = new WarehouseDocumentSnapshot(this.UnitOfWork, this.ExecutionController, this.IsHeadquarter);
                }
                break;

            case CommunicationPackageType.WarehouseStock:
                script = new WarehouseStockScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.UnrelateCommercialDocument:
                script = new UnrelateCommercialDocumentScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.UnrelateWarehouseDocumentForOutcome:
                script = new UnrelateWarehouseDocumentForOutcomeScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.UnrelateWarehouseDocumentForIncome:
                script = new UnrelateWarehouseDocumentForIncomeScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.ShiftDocumentStatus:
                script = new ShiftDocumentStatusScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.PriceRule:
                script = new PriceRuleScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.PriceRuleList:
                script = new PriceRuleListScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.Custom:
                script = new CustomScript(this.UnitOfWork, this.ExecutionController);
                break;

            case CommunicationPackageType.Unknown:
            case CommunicationPackageType.Other:
            default:
                script = new NullScript();
                break;
            }
            script.LocalTransactionId = this.LocalTransactionId;
            return(script);
        }