/// <summary> /// Рассчитать список сальдо по документам сделки /// </summary> /// <param name="teamIdList">Коллекция кодов команд, для которых нужно рассчитать сальдо</param> /// <param name="startDate">Начальная дата, на которую надо рассчитать сальдо</param> /// <param name="user">Пользователь, видимостью которого ограничивать множества документов. Null - не ограничивать видимостью</param> /// <param name="includeExpenditureWaybillsAndReturnFromClientWaybills">Включать ли реализации и возвраты</param> /// <param name="includeDealPayments">Включать ли оплаты и возвраты оплат</param> /// <param name="includeDealInitialBalanceCorrections">Включать ли корректировки сальдо</param> /// <param name="usePermission">Учитывать ли видимость сущностей</param> ///<returns>Список {AccountOrganizationId, ClientId, ClientOrganizationId, ContractId, TeamId, Сумма реализаций}</returns> private IList <InitialBalanceInfo> CalculateBalanceOnDate(IEnumerable <short> teamIdList, IEnumerable <int> clientIdList, IEnumerable <int> clientOrganizationIdList, DateTime startDate, User user, bool includeExpenditureWaybillsAndReturnFromClientWaybills = true, bool includeDealPayments = true, bool includeDealInitialBalanceCorrections = true, bool usePermission = true) { var result = new List <InitialBalanceInfo>(); #region Суммы по сущностям IQueryable <Team> teamSubQuery = null; switch (user.GetPermissionDistributionType(Permission.Team_List_Details)) { case PermissionDistributionType.All: teamSubQuery = teamRepository.GetTeamListByAllPermission(); break; case PermissionDistributionType.Teams: case PermissionDistributionType.Personal: teamSubQuery = teamRepository.GetTeamListByTeamPermission(user.Id); break; case PermissionDistributionType.None: teamSubQuery = teamRepository.GetTeamListByNonePermission(); break; } if (includeExpenditureWaybillsAndReturnFromClientWaybills) { var expenditureWaybillSubQuery = usePermission ? GetDealSubQueryByPermissionDistribution( expenditureWaybillRepository.GetExpenditureWaybillByAllPermission(), expenditureWaybillRepository.GetExpenditureWaybillByTeamPermission(user.Id), expenditureWaybillRepository.GetExpenditureWaybillByPersonalPermission(user.Id), Permission.ExpenditureWaybill_List_Details, user) : expenditureWaybillRepository.GetExpenditureWaybillByAllPermission(); if (expenditureWaybillSubQuery != null) { var saleWaybillSum = expenditureWaybillRepository.GetShippedSumOnDate(startDate, expenditureWaybillSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, saleWaybillSum, Decimal.One); } var returnFromClientWaybillDealSubQuery = usePermission ? GetDealSubQueryByPermissionDistribution( returnFromClientWaybillRepository.GetReturnFromClientWaybillByAllPermission(), returnFromClientWaybillRepository.GetReturnFromClientWaybillByTeamPermission(user.Id), returnFromClientWaybillRepository.GetReturnFromClientWaybillByPersonalPermission(user.Id), Permission.ReturnFromClientWaybill_List_Details, user) : returnFromClientWaybillRepository.GetReturnFromClientWaybillByAllPermission(); if (returnFromClientWaybillDealSubQuery != null) { var returnFromClientWaybillSum = returnFromClientWaybillRepository.GetReceiptedSumOnDate(startDate, returnFromClientWaybillDealSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, returnFromClientWaybillSum, Decimal.MinusOne); } } if (includeDealPayments) { var dealPaymentDealSubQuery = usePermission ? GetDealSubQueryByPermissionDistribution( dealPaymentDocumentRepository.GetDealPaymentDocumentByAllPermission(), dealPaymentDocumentRepository.GetDealPaymentDocumentByTeamPermission(user.Id), dealPaymentDocumentRepository.GetDealPaymentDocumentByPersonalPermission(user.Id), Permission.DealPayment_List_Details, user) : dealPaymentDocumentRepository.GetDealPaymentDocumentByAllPermission(); if (dealPaymentDealSubQuery != null) { var dealPaymentFromClientSum = dealPaymentDocumentRepository.GetDealPaymentFromClientSumOnDate(startDate, dealPaymentDealSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, dealPaymentFromClientSum, Decimal.MinusOne); var dealPaymentToClientSum = dealPaymentDocumentRepository.GetDealPaymentToClientSumOnDate(startDate, dealPaymentDealSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, dealPaymentToClientSum, Decimal.One); } } if (includeDealInitialBalanceCorrections) { var dealDebitInitialBalanceCorrectionDealSubQuery = usePermission ? GetDealSubQueryByPermissionDistribution( dealPaymentDocumentRepository.GetDealPaymentDocumentByAllPermission(), dealPaymentDocumentRepository.GetDealPaymentDocumentByTeamPermission(user.Id), dealPaymentDocumentRepository.GetDealPaymentDocumentByPersonalPermission(user.Id), Permission.DealInitialBalanceCorrection_List_Details, user) : dealPaymentDocumentRepository.GetDealPaymentDocumentByAllPermission(); if (dealDebitInitialBalanceCorrectionDealSubQuery != null) { var dealDebitInitialBalanceCorrectionSum = dealPaymentDocumentRepository.GetDealDebitInitialBalanceCorrectionSumOnDate(startDate, dealDebitInitialBalanceCorrectionDealSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, dealDebitInitialBalanceCorrectionSum, Decimal.One); var dealCreditInitialBalanceCorrectionSum = dealPaymentDocumentRepository.GetDealCreditInitialBalanceCorrectionSumOnDate(startDate, dealDebitInitialBalanceCorrectionDealSubQuery, teamSubQuery, teamIdList, clientIdList, clientOrganizationIdList); AddValuesToDealDictionary(result, dealCreditInitialBalanceCorrectionSum, Decimal.MinusOne); } } #endregion return(result); }