コード例 #1
0
ファイル: CommercialDocument.cs プロジェクト: MakoLab/fractus
 /// <summary>
 /// Sprawdza czy dokument realizuje zamknięte ZSP oraz opcjonalnie czy na liniach realizujących ZSP niekoniecznie zamknięte doszło do zmiany ilości
 /// </summary>
 /// <param name="checkQuantityChange">czy sprawdzać zmianę ilości na pozycjach realizujących ZSP</param>
 internal void ValidateSalesOrderRealizedLines(bool checkQuantityChange)
 {
     if (!this.IsNew && !this.SkipRealizeClosedSalesOrderCheck)
     {
         CommercialDocument alternateDocument = this.AlternateVersion != null ?
                                                (CommercialDocument)this.AlternateVersion : null;
         DocumentMapper mapper =
             (DocumentMapper)DependencyContainerManager.Container.Get <DocumentMapper>();
         foreach (CommercialDocumentLine commLine in this.Lines)
         {
             Guid?relatedSalesOrderId = commLine.Attributes.
                                        GetGuidValueByFieldName(DocumentFieldName.LineAttribute_RealizedSalesOrderLineId);
             //test czy realizuje zamknięte ZSP
             if (relatedSalesOrderId.HasValue)
             {
                 //test czy pozmieniały się ilości
                 if (checkQuantityChange && alternateDocument != null)
                 {
                     CommercialDocumentLine altLine = alternateDocument.Lines.Where(line => line.Id == commLine.Id).FirstOrDefault();
                     if (altLine != null && commLine.Quantity != altLine.Quantity)
                     {
                         throw new ClientException(ClientExceptionId.LineQuantityEditForbiddenRelatedSalesOrder, null, "order:" + commLine.Order);
                     }
                 }
                 CommercialDocument relatedSalesOrder =
                     mapper.GetCommercialDocumentByLineId(relatedSalesOrderId.Value);
                 if (SalesOrderFactory.IsSalesOrderClosed(relatedSalesOrder))
                 {
                     throw new ClientException(ClientExceptionId.DocumentEditForbiddenRelatedSalesOrderClosed);
                 }
             }
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Gets the constant data that should be saved in present version to a separate column.
        /// </summary>
        /// <returns><see cref="XElement"/> containing constant data.</returns>
        private XElement GetConstantData()
        {
            XElement constant = new XElement("constant");

            if (this.Contractor != null)
            {
                XElement el = CommercialDocument.GetContractorConstantData(this.Contractor, "contractor");

                if (el != null)
                {
                    constant.Add(el);
                }
            }

            if (this.IssuingPerson != null)
            {
                XElement el = CommercialDocument.GetContractorConstantData(this.IssuingPerson, "issuingPerson");

                if (el != null)
                {
                    constant.Add(el);
                }
            }

            return(constant);
        }
コード例 #3
0
        /// <summary>
        /// Validates the collection.
        /// </summary>
        public override void Validate()
        {
            if (this.Children.Count == 0 && this.Parent.BOType != BusinessObjectType.ServiceDocument)
            {
                throw new ClientException(ClientExceptionId.NoLines);
            }

            DocumentType dt = DictionaryMapper.Instance.GetDocumentType(((CommercialDocumentBase)this.Parent).DocumentTypeId);

            if (dt.CommercialDocumentOptions != null && !dt.CommercialDocumentOptions.AllowMultiplePositions && this.Children.Count > 1 && this.Parent.BOType != BusinessObjectType.ServiceDocument)
            {
                throw new ClientException(ClientExceptionId.OnlyOneDocumentLineAllowed);
            }

            if (dt.DocumentCategory == DocumentCategory.SalesCorrection || dt.DocumentCategory == DocumentCategory.PurchaseCorrection)
            {
                bool hasCorrectedLine = false;

                foreach (CommercialDocumentLine line in this.Children)
                {
                    if (line.NetPrice != 0 ||
                        line.GrossPrice != 0 ||
                        line.Quantity != 0)
                    {
                        hasCorrectedLine = true;
                        break;
                    }
                }

                CommercialDocument correctiveCommercialDocument = (CommercialDocument)this.Parent;

                bool hasDescriptiveAttributes = correctiveCommercialDocument.Attributes.Children.Where(a => a.DocumentFieldName == DocumentFieldName.Attribute_DescriptiveCorrectionAfter || a.DocumentFieldName == DocumentFieldName.Attribute_DescriptiveCorrectionBefore).Count() != 0;

                /*
                 * bool numberChanged = correctiveCommercialDocument.Number.Number != correctiveCommercialDocument.CorrectedDocument.Number.Number || correctiveCommercialDocument.Number.FullNumber != correctiveCommercialDocument.CorrectedDocument.Number.FullNumber;
                 */

                if (!hasCorrectedLine && !hasDescriptiveAttributes)
                {
                    throw new ClientException(ClientExceptionId.NonCorrectiveCorrection);
                }
            }

            base.Validate();
        }
コード例 #4
0
ファイル: CommercialDocument.cs プロジェクト: MakoLab/fractus
        public override XElement Serialize(bool selfOnly)
        {
            XElement element = base.Serialize(selfOnly);

            if (!selfOnly && this.DocumentType != null && (this.DocumentType.DocumentCategory == DocumentCategory.SalesCorrection || this.DocumentType.DocumentCategory == DocumentCategory.PurchaseCorrection))
            {
                CommercialDocument previousCorrection = this.CorrectedDocument;
                XElement           vtBeforeCorrection = previousCorrection.VatTableEntries.Serialize("vatTableBeforeCorrection");

                decimal netValueBeforeCorrection   = previousCorrection.NetValue;
                decimal grossValueBeforeCorrection = previousCorrection.GrossValue;
                decimal vatValueBeforeCorrection   = previousCorrection.VatValue;

                if (previousCorrection.IsSettlementDocument)
                {
                    vtBeforeCorrection.Elements().Where(e => Convert.ToDecimal(e.Element("netValue").Value, CultureInfo.InvariantCulture) < 0 ||
                                                        Convert.ToDecimal(e.Element("grossValue").Value, CultureInfo.InvariantCulture) < 0 ||
                                                        Convert.ToDecimal(e.Element("vatValue").Value, CultureInfo.InvariantCulture) < 0).Remove();

                    netValueBeforeCorrection   = vtBeforeCorrection.Elements().Sum(s => Convert.ToDecimal(s.Element("netValue").Value, CultureInfo.InvariantCulture));
                    grossValueBeforeCorrection = vtBeforeCorrection.Elements().Sum(s => Convert.ToDecimal(s.Element("grossValue").Value, CultureInfo.InvariantCulture));
                    vatValueBeforeCorrection   = vtBeforeCorrection.Elements().Sum(s => Convert.ToDecimal(s.Element("vatValue").Value, CultureInfo.InvariantCulture));
                }

                element.Add(vtBeforeCorrection);

                element.Add(new XElement("netValueBeforeCorrection", netValueBeforeCorrection.ToString(CultureInfo.InvariantCulture)));
                element.Add(new XElement("grossValueBeforeCorrection", grossValueBeforeCorrection.ToString(CultureInfo.InvariantCulture)));
                element.Add(new XElement("vatValueBeforeCorrection", vatValueBeforeCorrection.ToString(CultureInfo.InvariantCulture)));

                CommercialDocument sourceInvoice = this.InitialCorrectedDocument;
                element.Add(new XElement("originalDocumentFullNumber", sourceInvoice.Number.FullNumber));
                element.Add(new XElement("originalDocumentIssueDate", sourceInvoice.IssueDate));
            }

            if (this.SalesOrderSettlements != null)
            {
                element.Add(this.SalesOrderSettlements.Serialize());
            }

            return(element);
        }
コード例 #5
0
ファイル: CommercialDocument.cs プロジェクト: MakoLab/fractus
        /// <summary>
        /// Sets the alternate version of the <see cref="BusinessObject"/>.
        /// </summary>
        /// <param name="alternate"><see cref="BusinessObject"/> that is to be considered as the alternate one.</param>
        public override void SetAlternateVersion(IBusinessObject alternate)
        {
            base.SetAlternateVersion(alternate);

            CommercialDocument alternateDocument = (CommercialDocument)alternate;

            if (this.Lines != null)
            {
                this.Lines.SetAlternateVersion(alternateDocument.Lines);
            }

            if (this.VatTableEntries != null)
            {
                this.VatTableEntries.SetAlternateVersion(alternateDocument.VatTableEntries);
            }

            if (this.Payments != null)
            {
                this.Payments.SetAlternateVersion(alternateDocument.Payments);
            }
        }
コード例 #6
0
ファイル: WarehouseDocument.cs プロジェクト: MakoLab/fractus
        internal void CheckDoesRealizeClosedSalesOrder(DocumentCoordinator coordinator)
        {
            //to ma działać tylko dla RW i tylko gdy jest on zapisywany bezpośrednio a nie jako powiązany dokument
            if (!this.IsNew &&
                this.WarehouseDirection == Enums.WarehouseDirection.Outcome &&
                !this.HasAnyCommercialCommercialWarehouseRelations)
            {
                DocumentMapper mapper =
                    (DocumentMapper)DependencyContainerManager.Container.Get <DocumentMapper>();

                IEnumerable <Document> relatedSalesOrders = this.Relations.GetRelatedDocuments(DocumentRelationType.SalesOrderToWarehouseDocument);

                foreach (Document relatedSalesOrder in relatedSalesOrders)
                {
                    CommercialDocument salesOrder = (CommercialDocument)mapper.LoadBusinessObject(BusinessObjectType.CommercialDocument, relatedSalesOrder.Id.Value);
                    if (relatedSalesOrder.Id.HasValue && SalesOrderFactory.IsSalesOrderClosed(salesOrder))
                    {
                        throw new ClientException(ClientExceptionId.DocumentEditForbiddenRelatedSalesOrderClosed);
                    }
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Recursively creates new children (BusinessObjects) and loads settings from provided xml.
        /// </summary>
        /// <param name="element">Xml element to attach.</param>
        public override void Deserialize(XElement element)
        {
            base.Deserialize(element);

            if (this.CommercialWarehouseRelations.Children != null && this.CommercialWarehouseRelations.Children.Count > 0 && this.Parent != null)
            {
                CommercialDocument parent = (CommercialDocument)this.Parent;
                DocumentCategory   cat    = parent.DocumentType.DocumentCategory;

                if (cat != DocumentCategory.SalesOrder)
                {
                    parent.DisableLinesChange = DisableDocumentChangeReason.LINES_RELATED_WAREHOUSE_DOCUMENT;
                }

                if (cat == DocumentCategory.Reservation || cat == DocumentCategory.Order || cat == DocumentCategory.SalesOrder)
                {
                    if (this.CommercialWarehouseRelations.Children.Where(r => r.IsOrderRelation).FirstOrDefault() != null)
                    {
                        parent.DisableContractorChange = DisableDocumentChangeReason.CONTRACTOR_PARTIALLY_REALIZED_ORDER;
                    }
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Validates the <see cref="BusinessObject"/>.
        /// </summary>
        public override void Validate()
        {
            base.Validate();

            CommercialDocumentBase parent           = this.Parent as CommercialDocumentBase;
            CommercialDocument     commercialParent = this.Parent as CommercialDocument;
            string minusPosition;

            try
            {
                if (parent.DocumentType.Options.Element("commercialDocument").Attribute("exchangeDoublePosition") != null)
                {
                    minusPosition = parent.DocumentType.Options.Element("commercialDocument").Attribute("exchangeDoublePosition").Value;
                }
                else
                {
                    minusPosition = "true";
                }
            } catch (Exception ex)
            {
                RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:3" + ex.Message + ex.StackTrace);
                minusPosition = "false";
            }

            if (commercialParent != null && commercialParent.IsCorrectiveDocument())
            {
                //mozemy korygowac na + jezeli korygujemy pozycje z naszego dokumentu
                //czyli jest to wybranie opcji w panelu "Stworz pozycje korygujaca"
                if (this.Quantity > 0 && this.CorrectedLine.Parent.Id.Value != commercialParent.Id.Value)
                {
                    throw new ClientException(ClientExceptionId.QuantityOnCorrectionAboveZero);
                }
            }
            else if (commercialParent != null && commercialParent.DocumentType.DocumentCategory == DocumentCategory.Technology)
            {
                DocumentLineAttrValue attr = this.Attributes[DocumentFieldName.LineAttribute_ProductionItemType];

                if (attr == null || attr.Value == null || attr.Value.Value.Length == 0)
                {
                    throw new ClientException(ClientExceptionId.MissingProductionItemType, null, "ordinalNumber:" + this.OrdinalNumber.ToString(CultureInfo.InvariantCulture));
                }
            }
            else if (commercialParent != null && commercialParent.DocumentType.DocumentCategory == DocumentCategory.ProductionOrder)
            {
                DocumentLineAttrValue attr = this.Attributes[DocumentFieldName.LineAttribute_ProductionTechnologyName];

                if (attr == null || attr.Value == null || attr.Value.Value.Length == 0)
                {
                    throw new ClientException(ClientExceptionId.MissingProductionTechnologyName, null, "ordinalNumber:" + this.OrdinalNumber.ToString(CultureInfo.InvariantCulture));
                }
            }
            else
            {
                if (this.Quantity <= 0 && minusPosition != "true")
                {
                    throw new ClientException(ClientExceptionId.QuantityBelowOrEqualZero, null, "itemName:" + this.ItemName);
                }

                DocumentCategory dc = parent.DocumentType.DocumentCategory;

                //jezeli jest to pozycja sprzedazowa na zamowieniu sprzedazowym (zdefiniowany atrybut rodzaju sprzedaży) to NIE dopusczamy
                if (dc == DocumentCategory.SalesOrder && this.NetPrice <= 0)
                {
                    if (this.Attributes != null)
                    {
                        var attr = this.Attributes[DocumentFieldName.LineAttribute_SalesOrderGenerateDocumentOption];

                        if (attr != null)
                        {
                            if (attr.Value.Value == "1" || attr.Value.Value == "3") //sprzedazowe
                            {
                                throw new ClientException(ClientExceptionId.LinePriceBelowOrEqualZero);
                            }
                        }
                    }
                }

                if (dc == DocumentCategory.Sales || dc == DocumentCategory.Purchase)
                {
                    if (this.NetPrice <= 0)
                    {
                        throw new ClientException(ClientExceptionId.LinePriceBelowOrEqualZero);
                    }
                }
                else if (dc == DocumentCategory.Reservation || dc == DocumentCategory.Order || dc == DocumentCategory.SalesOrder)
                {
                    if (this.NetPrice < 0 && minusPosition != "true")
                    {
                        throw new ClientException(ClientExceptionId.LinePriceBelowZero);
                    }
                }
            }
            //Sprawdzenie czy pozycja jest na magazyn lokalny
            Warehouse warehouse = DictionaryMapper.Instance.GetWarehouse(this.WarehouseId.Value);

            if (this.WarehouseId.HasValue && !warehouse.IsLocal)
            {
                throw new ClientException(ClientExceptionId.WarehouseNotLocal, null, "warehouse:" + warehouse.Symbol);
            }

            if (this.CommercialWarehouseValuations != null)
            {
                this.CommercialWarehouseValuations.Validate();
            }

            if (this.CommercialWarehouseRelations != null)
            {
                this.CommercialWarehouseRelations.Validate();
            }

            if (this.Attributes != null)
            {
                this.Attributes.Validate();
            }
        }
コード例 #9
0
ファイル: CommercialDocument.cs プロジェクト: MakoLab/fractus
        /// <summary>
        /// Validates the <see cref="BusinessObject"/>.
        /// </summary>
        public override void Validate()
        {
            base.Validate();
            CommercialDocument alternateDocument = this.AlternateVersion as CommercialDocument;

            #region Walidajca dat
            if (this.IssueDate.Date < this.EventDate.Date)
            {
                if (this.DocumentType.DocumentCategory == DocumentCategory.Sales || this.DocumentType.DocumentCategory == DocumentCategory.SalesCorrection)
                {
                    throw new ClientException(ClientExceptionId.IncorrectEventDateSales);
                }
                else if (this.DocumentType.DocumentCategory == DocumentCategory.Purchase || this.DocumentType.DocumentCategory == DocumentCategory.PurchaseCorrection)
                {
                    throw new ClientException(ClientExceptionId.IncorrectEventDatePurchase);
                }
            }

            if ((this.DocumentType.DocumentCategory == DocumentCategory.Sales ||
                 this.DocumentType.DocumentCategory == DocumentCategory.SalesCorrection) &&
                this.IssueDate.Date > SessionManager.VolatileElements.CurrentDateTime.Date)
            {
                throw new ClientException(ClientExceptionId.IncorrectIssueDateOnSalesDocument);
            }
            #endregion

            #region Walidacja korekt
            if (this.IsCorrectiveDocument())
            {
                CommercialDocument correctedParent = (CommercialDocument)this.Lines[0].CorrectedLine.Parent;
                //data wystawienia nie moze byc pozniejsza niz data na poprzednim dokumencie
                #region
                if (correctedParent.IssueDate > this.IssueDate)
                {
                    throw new ClientException(ClientExceptionId.EarlierIssueDateOnCorrectiveDocument);
                }
                #endregion

                //korekty paragonu moga korygowac tylko do zera - sprawdzamy czy tak na pewno jest
                #region
                if (this.InitialCorrectedDocument.DocumentType.CommercialDocumentOptions.IsInvoiceAppendable)
                {
                    //sprawdzamy czy wszystkie linie co cos korygowaly koryguja do zera
                    foreach (CommercialDocumentLine line in this.Lines.Children)
                    {
                        if (line.Quantity != 0 && line.Quantity + line.CorrectedLine.Quantity > 0 || line.Quantity > 0 ||
                            line.GrossPrice != 0 || line.NetPrice != 0)
                        {
                            throw new ClientException(ClientExceptionId.BillCorrectionError);
                        }
                    }
                }
                #endregion

                //waluta musi być taka sama jak na dokumencie korygowanym
                if (this.DocumentCurrencyId != correctedParent.DocumentCurrencyId)
                {
                    throw new ClientException(ClientExceptionId.IncompatibleDocumentAndCorrectionCurrencies);
                }
            }
            #endregion

            #region Walidacja zamówień MM
            if (this.DocumentType.CommercialDocumentOptions.IsShiftOrder && this.DocumentType.DocumentCategory == DocumentCategory.Order)
            {
                var attr = this.Attributes[DocumentFieldName.Attribute_TargetBranchId];

                if (attr == null)
                {
                    throw new ClientException(ClientExceptionId.NoTargetBranchSelected);
                }

                Guid?whId = null;

                foreach (var line in this.Lines)
                {
                    if (line.WarehouseId == null)
                    {
                        continue;
                    }

                    if (whId == null)
                    {
                        whId = line.WarehouseId.Value;
                    }
                    else if (whId != null && whId.Value != line.WarehouseId.Value)
                    {
                        throw new ClientException(ClientExceptionId.IncorrectWarehouseOnShiftOrder);
                    }
                }
            }
            #endregion

            #region Faktura do paragonu
            var invoiceToBill = this.Relations.Children.Where(r => r.RelationType == DocumentRelationType.InvoiceToBill).FirstOrDefault();

            if (invoiceToBill != null && this.IsNew)
            {
                //jezeli wystawiamy fakture do paragonu to musi on miec taka sama forme platnosci
                var oppositeDocument = (CommercialDocument)invoiceToBill.RelatedDocument;
                var oppositePayment  = oppositeDocument.Payments[0];

                if (this.Payments[0].PaymentMethodId.Value != oppositePayment.PaymentMethodId.Value)
                {
                    throw new ClientException(ClientExceptionId.InconsistentPaymentMethodOnInvoiceAndBill);
                }

                if (this.CalculationType != oppositeDocument.CalculationType)
                {
                    throw new ClientException(ClientExceptionId.InconsistentCalculationTypeOnInvoiceAndBill);
                }
            }
            else if (invoiceToBill != null) //edycja. wtedy nie mamy wczytanego do konca paragonu wiec walidujemy z alternateVersion
            {
                if (this.Payments[0].PaymentMethodId.Value != alternateDocument.Payments[0].PaymentMethodId.Value)
                {
                    throw new ClientException(ClientExceptionId.InconsistentPaymentMethodOnInvoiceAndBill);
                }

                if (this.CalculationType != alternateDocument.CalculationType)
                {
                    throw new ClientException(ClientExceptionId.InconsistentCalculationTypeOnInvoiceAndBill);
                }
            }
            #endregion

            DocumentType dt = DictionaryMapper.Instance.GetDocumentType(this.DocumentTypeId);

            if (!dt.CommercialDocumentOptions.AllowOtherCurrencies && this.DocumentCurrencyId != this.SystemCurrencyId)
            {
                throw new ClientException(ClientExceptionId.DocumentCurrencyException);
            }

            if (!dt.CommercialDocumentOptions.AllowCalculationTypeChange && this.CalculationType != dt.CommercialDocumentOptions.CalculationType &&
                this.Relations.Children.Where(r => r.RelationType == DocumentRelationType.InvoiceToBill).FirstOrDefault() == null)
            {
                throw new ClientException(ClientExceptionId.CalculationTypeChangeError);
            }

            #region Walidacja kontrahentów
            if (this.ReceivingPerson != null && this.Contractor == null)
            {
                throw new ClientException(ClientExceptionId.ReceivingPersonWithoutContractor);
            }

            if (dt.CommercialDocumentOptions.ContractorOptionality == Optionality.Mandatory && this.Contractor == null)
            {
                throw new ClientException(ClientExceptionId.ContractorIsMandatory);
            }

            if (dt.CommercialDocumentOptions.ContractorOptionality == Optionality.Forbidden && this.Contractor != null)
            {
                throw new ClientException(ClientExceptionId.ContractorIsForbidden);
            }

            //nie mozna zmieniac kontrahenta na czesciowo zrealizownym zamowieniu
            if (this.AlternateVersion != null)
            {
                bool related = false;

                foreach (var line in this.Lines.Children)
                {
                    if (line.CommercialWarehouseRelations.Children.Count != 0)
                    {
                        related = true;
                        break;
                    }
                }

                if (related &&
                    (dt.DocumentCategory == DocumentCategory.Order || dt.DocumentCategory == DocumentCategory.Reservation) &&
                    (this.Contractor != null && alternateDocument.Contractor == null ||
                     this.Contractor == null && alternateDocument.Contractor != null ||
                     (this.Contractor != null && alternateDocument.Contractor != null && this.Contractor.Id.Value != alternateDocument.Contractor.Id.Value)))
                {
                    throw new ClientException(ClientExceptionId.PartiallyRealizedOrderContractorChange);
                }
            }
            #endregion

            if (this.Lines != null)
            {
                this.Lines.Validate();
            }

            #region Walidacja technologii
            if (dt.DocumentCategory == DocumentCategory.Technology)
            {
                DocumentAttrValue attr = this.Attributes[DocumentFieldName.Attribute_ProductionTechnologyName];

                if (attr == null || attr.Value == null || attr.Value.Value.Length == 0)
                {
                    throw new ClientException(ClientExceptionId.MissingTechnologyNameOnTechnology);
                }

                if (this.Lines.Where(l => l.Attributes[DocumentFieldName.LineAttribute_ProductionItemType].Value.Value == "product").Count() != 1)
                {
                    throw new ClientException(ClientExceptionId.MissingMainProductOnTechnology);
                }

                if (this.Lines.Where(l => l.Attributes[DocumentFieldName.LineAttribute_ProductionItemType].Value.Value == "material").Count() == 0)
                {
                    throw new ClientException(ClientExceptionId.MissingMaterialOnTechnology);
                }
            }
            #endregion

            #region Walidacja stawek VAT

            if (this.VatTableEntries != null)
            {
                this.VatTableEntries.Validate();
            }

            #region Walidacja okresu obowiązywania

            if (this.DocumentType.DocumentCategory == DocumentCategory.Sales)
            {
                //jeśli wpisy dla stawki się bilansują do 0 nie zgłaszamy wyjątku
                if (this.VatTableEntries.Any(vte =>
                                             !DictionaryMapper.Instance.GetVatRate(vte.VatRateId).IsEventDateValid(this.EventDate) &&
                                             (this.VatTableEntries.Where(__vte => __vte.VatRateId == vte.VatRateId).Sum(__vte => __vte.GrossValue) != 0 ||
                                              this.VatTableEntries.Where(__vte => __vte.VatRateId == vte.VatRateId).Sum(__vte => __vte.NetValue) != 0 ||
                                              this.VatTableEntries.Where(__vte => __vte.VatRateId == vte.VatRateId).Sum(__vte => __vte.VatValue) != 0)))
                {
                    throw new ClientException(ClientExceptionId.VatRateNotAllowedForSelectedSaleDate);
                }
            }

            #endregion

            #endregion

            if (this.Payments != null)
            {
                this.Payments.Validate();
            }

            #region Walidacja blokad edycji pozycji
            if (!this.IsNew && this.DisableLinesChange != null && this.DisableLinesChange.Contains(DisableDocumentChangeReason.LINES_RELATED_WAREHOUSE_DOCUMENT) &&
                (this.Lines.IsAnyChildNew() || this.Lines.IsAnyChildModified() || this.Lines.IsAnyChildDeleted()) &&
                this.DocumentStatus != DocumentStatus.Canceled)
            {
                //teraz mozemy edytowac linie ale tylko na dok. sprzedazowych i tylko wartosci, wiec sprawzamy czy moze tak jest
                if (this.DocumentType.DocumentCategory == DocumentCategory.Sales && !this.Lines.IsAnyChildNew() && !this.Lines.IsAnyChildDeleted())
                {
                    //czyli same zmodyfikowane, sprawdzamy czy ilosciowo tylko
                    foreach (var line in this.Lines)
                    {
                        if (line.Quantity != ((CommercialDocumentLine)line.AlternateVersion).Quantity)
                        {
                            throw new ClientException(ClientExceptionId.UnableToEditCommercialDocument);
                        }
                    }
                }
                else
                {
                    throw new ClientException(ClientExceptionId.UnableToEditCommercialDocument);
                }
            }

            if (this.DisableDocumentChange != null && this.DisableDocumentChange.Contains(DisableDocumentChangeReason.DOCUMENT_RELATED_CORRECTIVE_DOCUMENTS) &&
                (this.Lines.IsAnyChildNew() || this.Lines.IsAnyChildModified() || this.Lines.IsAnyChildDeleted()))
            {
                throw new ClientException(ClientExceptionId.UnableToEditDocumentBecauseOfCorrections);
            }
            #endregion

            #region Walidacja faktury rozliczającej
            DocumentAttrValue attrib = this.Attributes[DocumentFieldName.Attribute_ProcessType];

            if (attrib != null && attrib.Value.Value == "processes.salesOrder" && this.DocumentType.DocumentCategory != DocumentCategory.SalesOrder)
            {
                //sprawdzamy czy jest to faktura rozliczajaca po tym ze ma ujemne wartosci w tabeli vat
                if (!this.IsSettlementDocument)
                {
                    ICollection <Guid> prepaidItems = ProcessManager.Instance.GetPrepaidItems(this);

                    foreach (var line in this.Lines)
                    {
                        if (!prepaidItems.Contains(line.ItemId))
                        {
                            throw new ClientException(ClientExceptionId.PrepaidInvoiceLinesError);
                        }
                    }
                }
            }
            #endregion

            #region Walidacja zamowienia sprzedazowego
            if (this.DocumentType.DocumentCategory == DocumentCategory.SalesOrder)
            {
                var salesmanAttr = this.Attributes[DocumentFieldName.Attribute_SalesmanId];
                //Nie zawsze potrzebny handlowiec
                var salesmanAttrConf = this.DocumentType.Options.Descendants("attribute").Attributes("name").Where(line => line.Value == "Attribute_SalesmanId").Count();
                if (salesmanAttrConf > 0)
                {
                    if (salesmanAttr == null || salesmanAttr.Value.Value.Length == 0 || !salesmanAttr.Value.Value.IsGuid())                                           //jezeli nie ma handlowca wybranego
                    {
                        if (alternateDocument == null || alternateDocument.DocumentStatus != DocumentStatus.Committed || this.DocumentStatus != DocumentStatus.Saved) //jezeli jest to nowy dokument lub edycja ale NIE jest to anulowanie rozliczenia
                        {
                            throw new ClientException(ClientExceptionId.MissingSalesman);
                        }
                    }
                }
                if (this.DocumentType.Options.Descendants("template").Attributes("LineAttribute_SalesOrderGenerateDocumentOption").Count() > 0)
                {
                    foreach (CommercialDocumentLine line in this.Lines.Children)
                    {
                        if (line.Attributes[DocumentFieldName.LineAttribute_SalesOrderGenerateDocumentOption] == null || line.Attributes[DocumentFieldName.LineAttribute_SalesOrderGenerateDocumentOption].Value.Value.Length == 0)
                        {
                            throw new ClientException(ClientExceptionId.MissingLineAttribute, null, "ordinalNumber:" + line.OrdinalNumber.ToString(CultureInfo.InvariantCulture));
                        }

                        CommercialDocumentLine alternativeLine = line.AlternateVersion as CommercialDocumentLine;

                        if (alternativeLine != null && alternativeLine.Attributes[DocumentFieldName.LineAttribute_SalesOrderGenerateDocumentOption].Value.Value !=
                            line.Attributes[DocumentFieldName.LineAttribute_SalesOrderGenerateDocumentOption].Value.Value &&
                            alternativeLine.CommercialWarehouseRelations.Children.Count != 0)
                        {
                            throw new ClientException(ClientExceptionId.GenerateDocumentOptionAttriuteChangeError, null, "ordinalNumber:" + line.OrdinalNumber.ToString(CultureInfo.InvariantCulture));
                        }

                        decimal quantityOnRelations = line.CommercialWarehouseRelations.Sum(s => s.Quantity);

                        if (line.Quantity < quantityOnRelations && line.Quantity > 0) //&& line.Quantity > 0 dodane na potrzeby ujemnej pozycji na zamówieniu
                        {
                            throw new ClientException(ClientExceptionId.QuantityBelowServiceRealized, null, "ordinalNumber:" + line.OrdinalNumber.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                }
                if (alternateDocument != null)
                {
                    var deletedRealized = alternateDocument.Lines.Children.Where(l => l.Status == BusinessObjectStatus.Deleted && l.CommercialWarehouseRelations.Children.Count != 0).FirstOrDefault();

                    if (deletedRealized != null)
                    {
                        throw new ClientException(ClientExceptionId.ServiceRealizedLineRemoval);
                    }
                }
            }
            #endregion
        }