internal DateTime GetCalculateDueDaysOnPaymentSubtrahend() { DateTime subtrahend = this.IssueDate; //Sprawdzenie czy faktura zakupu if (this.DocumentType.DocumentCategory == DocumentCategory.Purchase) { DocumentAttrValue supplierDocumentDate = this.Attributes[DocumentFieldName.Attribute_SupplierDocumentDate]; if (supplierDocumentDate != null) { subtrahend = Convert.ToDateTime(supplierDocumentDate.Value.Value); } } return(subtrahend.Trunc(DateTimeAccuracy.Day)); }
/// <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 }