Exemple #1
0
 private void ValidateAfterSave(FinancialDocument document)
 {
     #region Blokada zejścia ze stanem finansowym poniżej 0
     FinancialRegister financialRegister = DictionaryMapper.Instance.GetFinancialRegister(document.FinancialReport.FinancialRegisterId);
     if (financialRegister.ValidateBalanceBelowZero)
     {
         if (this.mapper.GetFinancialReportBalance(document.FinancialReport.FinancialRegisterId) < 0)
         {
             throw new ClientException(ClientExceptionId.FinancialRegisterBalanceBelowZero);
         }
     }
     #endregion
 }
Exemple #2
0
        /// <summary>
        /// Validates the <see cref="BusinessObject"/>.
        /// </summary>
        public override void Validate()
        {
            base.Validate();

            if (!this.IsNew && this.FinancialReport.Id.Value != ((FinancialDocument)this.AlternateVersion).FinancialReport.Id.Value)
            {
                throw new ClientException(ClientExceptionId.FinancialDocumentException3);
            }

            FinancialRegister register = DictionaryMapper.Instance.GetFinancialRegister(this.FinancialReport.FinancialRegisterId);

            if (this.IsNew && ((this.FinancialDirection == FinancialDirection.Income && register.IncomeNumberSettingId != this.Number.NumberSettingId) ||
                               (this.FinancialDirection == FinancialDirection.Outcome && register.OutcomeNumberSettingId != this.Number.NumberSettingId)))
            {
                throw new ClientException(ClientExceptionId.FinancialDocumentException1);
            }

            if ((this.FinancialDirection == FinancialDirection.Income && register.IncomeDocumentTypeId != this.DocumentTypeId) ||
                (this.FinancialDirection == FinancialDirection.Outcome && register.OutcomeDocumentTypeId != this.DocumentTypeId))
            {
                throw new ClientException(ClientExceptionId.FinancialDocumentException2);
            }

            //Nie można edytować KW powiązanego z ZSP
            if (!this.IsNew && this.FinancialDirection == FinancialDirection.Outcome &&
                this.DocumentType.FinancialDocumentOptions.RegisterCategoryAsEnum == RegisterCategory.CashRegister &&
                this.Relations.HasRelations(DocumentRelationType.SalesOrderToOutcomeFinancialDocument))
            {
                throw new ClientException(ClientExceptionId.UnableToEditFinancialOutcomeDocumentRelatedWithSalesOrder);
            }

            #region Currency must match financial register currency

            if (this.DocumentCurrencyId != DictionaryMapper.Instance.GetFinancialRegister(this.FinancialReport.FinancialRegisterId).CurrencyId)
            {
                throw new ClientException(ClientExceptionId.IncompatibleDocumentAndFinancialRegisterCurrencies);
            }

            #endregion

            if (this.Payments.Children.Count == 0)
            {
                throw new ClientException(ClientExceptionId.NoLines);
            }

            if (this.Payments != null)
            {
                this.Payments.Validate();
            }
        }
        public static void CreateFinancialReportToFinancialRegister(XElement source, FinancialReport destination)
        {
            Guid registerId            = new Guid(source.Element("financialRegisterId").Value);
            FinancialRegister register = DictionaryMapper.Instance.GetFinancialRegister(registerId);

            destination.Number.NumberSettingId = register.FinancialReportNumberSettingId;
            destination.FinancialRegisterId    = registerId;

            DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>();
            bool           exists = mapper.CheckReportExistence(registerId);

            if (!exists)
            {
                destination.IsFirstReport  = true;
                destination.InitialBalance = 0;
            }
            else
            {
                destination.InitialBalance = mapper.CalculateReportInitialBalance(registerId);
            }
        }
Exemple #4
0
        private void ValidateDuringTransaction(FinancialDocument document)
        {
            FinancialRegister financialRegister = DictionaryMapper.Instance.GetFinancialRegister(document.FinancialReport.FinancialRegisterId);

            //Sprawdzenie czy stanowisko ma uprawnienia do dodawania określonych płatności do raportu finansowego na wybranym rejestrze
            if (!financialRegister.IsAllowedByProfile())
            {
                throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument4);
            }

            Guid?openedReportId = this.mapper.GetOpenedFinancialReportId(document.FinancialReport.FinancialRegisterId);

            if (openedReportId == null)
            {
                throw new ClientException(ClientExceptionId.OpenedFinancialReportDoesNotExist);
            }
            else if (openedReportId.Value != document.FinancialReport.Id.Value)
            {
                throw new ClientException(ClientExceptionId.UnableToIssueDocumentToClosedFinancialReport);
            }

            XElement dates = this.mapper.GetFinancialReportsDate(document.FinancialReport.FinancialRegisterId);
            DateTime openedReportCreationDate = DateTime.Parse(dates.Element("openedReportCreationDate").Value, CultureInfo.InvariantCulture);
            DateTime?nextReportCreationDate   = null;

            if (dates.Element("nextReportCreationDate") != null)
            {
                nextReportCreationDate = DateTime.Parse(dates.Element("nextReportCreationDate").Value, CultureInfo.InvariantCulture);
            }

            if (document.IssueDate < openedReportCreationDate)
            {
                throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument);
            }

            if (nextReportCreationDate != null && document.IssueDate >= nextReportCreationDate.Value)
            {
                throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument2);
            }
        }
        public static void UpdateFinancialDocumentsInCommercialDocument(CommercialDocument document)
        {
            if (document == null)
            {
                return;
            }

            DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>();

            ICollection <Guid> financialIdCol = mapper.GetRelatedFinancialDocumentsId(document.Id.Value);

            //wczytujemy i laczymy dokumenty po paymentach
            foreach (Guid id in financialIdCol)
            {
                FinancialDocument fDoc = null;

                try
                {
                    fDoc = (FinancialDocument)mapper.LoadBusinessObject(BusinessObjectType.FinancialDocument, id);
                }
                catch (ClientException ex)
                {
                    RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:126");
                    if (ex.Id == ClientExceptionId.ObjectNotFound)
                    {
                        continue;
                    }
                }

                document.AddRelatedObject(fDoc);

                //aktualizujemy dane kursu na dokumencie finansowym i na jego platnosciach
                fDoc.ExchangeDate  = document.ExchangeDate;
                fDoc.ExchangeRate  = document.ExchangeRate;
                fDoc.ExchangeScale = document.ExchangeScale;

                foreach (Payment payment in fDoc.Payments)
                {
                    payment.ExchangeDate  = document.ExchangeDate;
                    payment.ExchangeRate  = document.ExchangeRate;
                    payment.ExchangeScale = document.ExchangeScale;
                }

                var settlements = from p in document.Payments.Children
                                  from s in p.Settlements.Children
                                  select s;

                foreach (PaymentSettlement settlement in settlements)
                {
                    Payment pt = fDoc.Payments.Children.Where(x => x.Id.Value == settlement.RelatedPayment.Id.Value).FirstOrDefault();

                    if (pt != null)
                    {
                        settlement.RelatedPayment = pt;

                        if (settlement.AlternateVersion != null)
                        {
                            ((PaymentSettlement)settlement.AlternateVersion).RelatedPayment = pt;
                        }
                    }
                }

                //replace deleted payments/settlements
                if (document.AlternateVersion != null)
                {
                    CommercialDocument alternateDocument = (CommercialDocument)document.AlternateVersion;
                    var deletedSettlements = from p in alternateDocument.Payments.Children
                                             from s in p.Settlements.Children
                                             where p.AlternateVersion == null
                                             select s;

                    foreach (PaymentSettlement settlement in deletedSettlements)
                    {
                        Payment pt = fDoc.Payments.Children.Where(x => x.Id.Value == settlement.RelatedPayment.Id.Value).FirstOrDefault();

                        if (pt != null)
                        {
                            settlement.RelatedPayment = pt;
                        }
                    }
                }
            }

            if (!FinancialDocumentFactory.IsFinancialToCommercialRelationOneToOne(document))
            {
                throw new ClientException(ClientExceptionId.AutomaticFinancialDocumentUpdateException);
            }

            //aktualizujemy
            foreach (Payment payment in document.Payments.Children.Where(p => p.Status == BusinessObjectStatus.Modified))
            {
                Payment alternatePayment = (Payment)payment.AlternateVersion;
                string  cntId, altercntId;
                cntId = payment.Contractor != null?payment.Contractor.Id.ToString() : "brak";

                altercntId = alternatePayment.Contractor != null?alternatePayment.Contractor.Id.ToString() : "brak";

                //Jeśli zmienia się kontrhent to należy zmienić dowiązany dokument handlowy
                if (payment.PaymentMethodId.Value == alternatePayment.PaymentMethodId.Value &&
                    !(cntId != altercntId))        //zmienila sie wartosc
                {
                    PaymentSettlement settlement = payment.Settlements.Children.FirstOrDefault();

                    if (settlement != null)
                    {
                        settlement.Amount = Math.Abs(payment.Amount);

                        decimal difference = Math.Abs(Math.Abs(payment.Amount) - Math.Abs(settlement.RelatedPayment.Amount));

                        if ((settlement.RelatedPayment.Amount + difference) * settlement.RelatedPayment.Direction == -payment.Amount * payment.Direction)
                        {
                            settlement.RelatedPayment.Amount += difference;
                        }
                        else
                        {
                            settlement.RelatedPayment.Amount -= difference;
                        }

                        ((FinancialDocument)settlement.RelatedPayment.Parent).Amount = settlement.RelatedPayment.Amount;
                        settlement.RelatedPayment.Amount = settlement.RelatedPayment.Amount;
                        settlement.RelatedPayment.Settlements.Where(s => s.Id.Value == settlement.Id.Value).First().Amount = settlement.RelatedPayment.Amount;
                    }
                }
                else //zmienila sie forma platnosci
                {
                    //anulujemy powiazany dokument finansowy jezeli:
                    //1) powiazanie jest na pelna sume
                    //2) dokument istnieje (nie rozliczamy nic z innego oddzialu
                    //3) powiazany dokument ma taki sam typ jaki jaki generowala stara forma platnosci
                    if (payment.Settlements.Children.Count > 0)
                    {
                        //1
                        if (payment.Settlements.Children.Count != 1)
                        {
                            throw new ClientException(ClientExceptionId.AutomaticFinancialDocumentUpdateException);
                        }

                        if (alternatePayment.Amount != payment.Settlements.Children.First().Amount)
                        {
                            throw new ClientException(ClientExceptionId.AutomaticFinancialDocumentUpdateException);
                        }

                        //2
                        FinancialDocument fDoc = (FinancialDocument)payment.Settlements.Children.First().RelatedPayment.Parent;

                        if (fDoc == null)
                        {
                            throw new ClientException(ClientExceptionId.AutomaticFinancialDocumentUpdateException);
                        }

                        //3
                        FinancialRegister reg = DictionaryMapper.Instance.GetFinancialRegisterForSpecifiedPaymentMethod(alternatePayment.PaymentMethodId.Value, SessionManager.User.BranchId, alternatePayment.PaymentCurrencyId);

                        if (reg == null || (reg.IncomeDocumentTypeId != fDoc.DocumentTypeId && reg.OutcomeDocumentTypeId != fDoc.DocumentTypeId))
                        {
                            throw new ClientException(ClientExceptionId.AutomaticFinancialDocumentUpdateException);
                        }

                        //warunki spelnione, mozna anulowac
                        DocumentStatusChangeLogic.CancelFinancialDocument(fDoc);
                        payment.Settlements.RemoveAll();
                    }

                    FinancialDocumentFactory.GenerateFinancialDocumentToPayment(payment, document);
                }
            }
        }
        private static void GenerateFinancialDocumentToPayment(Payment payment, CommercialDocument document)
        {
            DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>();

            using (DocumentCoordinator coordinator = new DocumentCoordinator(false, false))
            {
                PaymentMethod paymentMethod = DictionaryMapper.Instance.GetPaymentMethod(payment.PaymentMethodId.Value);

                Guid branchId = SessionManager.User.BranchId;

                if (DictionaryMapper.Instance.IsPaymentMethodSupportedByRegister(paymentMethod.Id.Value, branchId))
                {
                    FinancialRegister register = DictionaryMapper.Instance.GetFinancialRegisterForSpecifiedPaymentMethod(paymentMethod.Id.Value, branchId, payment.PaymentCurrencyId);

                    if (register == null)
                    {
                        throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument3);
                    }

                    FinancialDirection direction = FinancialDocumentFactory.GetFinancialDirectionForPayment(document, payment);

                    Guid?reportId = mapper.GetOpenedFinancialReportId(register.Id.Value);

                    if (reportId == null)
                    {
                        throw new ClientException(ClientExceptionId.UnableToIssueDocumentToClosedFinancialReport);
                    }

                    FinancialDocument financialDoc = new FinancialDocument();
                    financialDoc.RelatedCommercialDocument = document;
                    coordinator.TrySaveProfileIdAttribute(financialDoc);

                    if (direction == FinancialDirection.Income)
                    {
                        financialDoc.DocumentTypeId         = register.IncomeDocumentTypeId;
                        financialDoc.Number.NumberSettingId = register.IncomeNumberSettingId;
                    }
                    else
                    {
                        financialDoc.DocumentTypeId         = register.OutcomeDocumentTypeId;
                        financialDoc.Number.NumberSettingId = register.OutcomeNumberSettingId;
                    }

                    financialDoc.Contractor          = payment.Contractor;
                    financialDoc.ContractorAddressId = payment.ContractorAddressId;
                    FinancialReport report = new FinancialReport();
                    report.Id = reportId;
                    report.FinancialRegisterId   = register.Id.Value;
                    financialDoc.FinancialReport = report;
                    financialDoc.DocumentStatus  = DocumentStatus.Committed;
                    DuplicableAttributeFactory.DuplicateAttributes(document, financialDoc);

                    Payment pt = financialDoc.Payments.CreateNew();
                    pt.Amount                       = Math.Abs(payment.Amount);
                    pt.ExchangeRate                 = financialDoc.ExchangeRate = document.ExchangeRate;
                    pt.ExchangeScale                = financialDoc.ExchangeScale = document.ExchangeScale;
                    pt.ExchangeDate                 = financialDoc.ExchangeDate = document.ExchangeDate;
                    financialDoc.Amount             = pt.Amount;
                    financialDoc.DocumentCurrencyId = payment.PaymentCurrencyId;
                    PaymentSettlement settlement = pt.Settlements.CreateNew();
                    settlement.IsAutoGenerated = true;
                    settlement.RelatedPayment  = payment;
                    settlement.Amount          = pt.Amount;

                    document.AddRelatedObject(financialDoc);
                }
            }
        }
Exemple #7
0
        public static void GetPrintXml(XDocument xml, string customLabelsLang)
        {
            string emptyGuid = Guid.Empty.ToString();

            var foreignKeys = from node in xml.Root.Descendants()
                              where node.Name.LocalName != "id" && node.Name.LocalName.EndsWith("Id", StringComparison.Ordinal) &&
                              node.Value != emptyGuid
                              select node;

            foreach (XElement element in foreignKeys)
            {
                ILabeledDictionaryBusinessObject bo = null;

                switch (element.Name.LocalName)
                {
                case "contractorFieldId":
                    bo = DictionaryMapper.Instance.GetContractorField(new Guid(element.Value));
                    element.Add(new XAttribute("name", ((ContractorField)bo).Name));
                    break;

                case "documentFieldId":
                    bo = DictionaryMapper.Instance.GetDocumentField(new Guid(element.Value));
                    DocumentField df = (DocumentField)bo;
                    element.Add(new XAttribute("name", df.Name));
                    //jezeli jest to id magazynu przeciwnego to dolaczamy jego label i symbol
                    if (df.Name == DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId.ToString())
                    {
                        Warehouse whOp = DictionaryMapper.Instance.GetWarehouse(new Guid(element.Parent.Element("value").Value));
                        element.Add(new XAttribute("warehouseLabel", BusinessObjectHelper.GetBusinessObjectLabelInUserLanguage(whOp, customLabelsLang).Value));
                        element.Add(new XAttribute("warehouseSymbol", whOp.Symbol));
                    }
                    //jeśli jest atrybut typu select to zamiast wartości musi się pojawić etykieta odpowiadająca tej wartości w języku usera
                    if (df.DataType == DataType.Select)
                    {
                        XElement valueElement = element.Parent.Element(XmlName.Value);
                        if (valueElement != null)
                        {
                            valueElement.Value = BusinessObjectHelper.GetSelectedValueInUserLanguage(df, valueElement.Value);
                        }
                    }
                    break;

                case "jobPositionId":
                    bo = DictionaryMapper.Instance.GetJobPosition(new Guid(element.Value));
                    break;

                case "countryId":
                case "nipPrefixCountryId":
                    bo = DictionaryMapper.Instance.GetCountry(new Guid(element.Value));
                    element.Add(new XAttribute("symbol", ((Country)bo).Symbol));
                    break;

                case "contractorRelationTypeId":
                    bo = DictionaryMapper.Instance.GetContractorRelationType(new Guid(element.Value));
                    break;

                case "itemFieldId":
                    bo = DictionaryMapper.Instance.GetItemField(new Guid(element.Value));
                    element.Add(new XAttribute("name", ((ItemField)bo).Name));
                    break;

                case "itemRAVTypeId":
                    bo = DictionaryMapper.Instance.GetItemRelationAttrValueType(new Guid(element.Value));
                    break;

                case "itemRelationId":
                    bo = DictionaryMapper.Instance.GetItemRelationType(new Guid(element.Value));
                    break;

                case "itemTypeId":
                    bo = DictionaryMapper.Instance.GetItemType(new Guid(element.Value));
                    break;

                case "unitId":
                    bo = DictionaryMapper.Instance.GetUnit(new Guid(element.Value));
                    element.Add(new XAttribute("symbol", BusinessObjectHelper.GetBusinessObjectLabelInUserLanguage(bo, customLabelsLang).Attribute("symbol").Value));
                    break;

                case "unitTypeId":
                    bo = DictionaryMapper.Instance.GetUnitType(new Guid(element.Value));
                    break;

                case "vatRateId":
                    bo = DictionaryMapper.Instance.GetVatRate(new Guid(element.Value));
                    VatRate vr = (VatRate)bo;
                    element.Add(new XAttribute("fiscalSymbol", vr.FiscalSymbol));
                    element.Add(new XAttribute("symbol", vr.Symbol));
                    break;

                case "documentCurrencyId":
                case "systemCurrencyId":
                case "paymentCurrencyId":
                    bo = DictionaryMapper.Instance.GetCurrency(new Guid(element.Value));
                    element.Add(new XAttribute("symbol", ((Currency)bo).Symbol));
                    break;

                case "documentTypeId":
                    bo = DictionaryMapper.Instance.GetDocumentType(new Guid(element.Value));
                    element.Add(new XAttribute("symbol", ((DocumentType)bo).Symbol));
                    break;

                case "issuePlaceId":
                    bo = null;
                    IssuePlace issuePlace = DictionaryMapper.Instance.GetIssuePlace(new Guid(element.Value));
                    if (issuePlace == null)
                    {
                        throw new ArgumentNullException("issuePlaceId");
                    }
                    element.Add(new XAttribute("label", issuePlace.Name));
                    break;

                case "paymentMethodId":
                    bo = DictionaryMapper.Instance.GetPaymentMethod(new Guid(element.Value));
                    element.Add(new XAttribute("isIncrementingDueAmount", ((PaymentMethod)bo).IsIncrementingDueAmount ? "1" : "0"));
                    break;

                case "warehouseId":
                    bo = DictionaryMapper.Instance.GetWarehouse(new Guid(element.Value));
                    Warehouse wh = (Warehouse)bo;
                    element.Add(new XAttribute("symbol", wh.Symbol));
                    break;

                case "financialRegisterId":
                    FinancialRegister fr = DictionaryMapper.Instance.GetFinancialRegister(new Guid(element.Value));
                    bo = fr;

                    element.Add(new XAttribute("registerCategory", ((int)fr.RegisterCategory).ToString(CultureInfo.InvariantCulture)));

                    Guid     systemCurrencyId = ConfigurationMapper.Instance.SystemCurrencyId;
                    Currency currency         = DictionaryMapper.Instance.GetCurrency(fr.CurrencyId);

                    element.Add(new XAttribute("currencyId", currency.Id.ToUpperString()));
                    element.Add(new XAttribute("currencySymbol", currency.Symbol));
                    element.Add(new XAttribute("systemCurrencyId", systemCurrencyId.ToUpperString()));

                    if (!String.IsNullOrEmpty(fr.AccountingAccount))
                    {
                        element.Add(new XAttribute("accountingAccount", fr.AccountingAccount));
                    }

                    if (!String.IsNullOrEmpty(fr.BankAccountNumber))
                    {
                        element.Add(new XAttribute("bankAccountNumber", fr.BankAccountNumber));
                    }

                    break;

                case "containerTypeId":
                    bo = DictionaryMapper.Instance.GetContainerType(new Guid(element.Value));
                    break;

                case "servicePlaceId":
                    element.Add(new XAttribute("label", DictionaryMapper.Instance.GetServicePlace(new Guid(element.Value)).Name));
                    break;

                case "itemGroupId":
                    string path = DictionaryMapper.Instance.GetItemGroupMembershipPath(element.Value);
                    if (path != null)
                    {
                        element.Add(new XAttribute("path", path));
                    }
                    break;
                }

                if (bo != null)
                {
                    element.Add(new XAttribute("label", BusinessObjectHelper.GetBusinessObjectLabelInUserLanguage(bo, customLabelsLang).Value));
                }
            }
        }