Ejemplo n.º 1
0
        /// <summary>
        /// Расчет суммы накладной реализации с учетом всех сделанных по ней возвратов
        /// </summary>
        /// <param name="sale"></param>
        /// <returns></returns>
        public decimal CalculateSaleWaybillCostWithReturns(SaleWaybill sale, ReturnFromClientWaybill returnFromClientWaybillToExclude = null)
        {
            // Сумма всех возвратов по накладной реализации
            var returnedRow = returnFromClientWaybillRepository.Query <ReturnFromClientWaybillRow>();

            // TODO рефакторить, ну кто так через OneOf пишет? Надо выбрать в RetWaybillRow SaleWaybillRow, а в нем - SaleWaybill.Id !!! - Олег
            returnedRow.OneOf(x => x.SaleWaybillRow.Id, sale.Rows.Select(x => x.Id))
            .Restriction <ReturnFromClientWaybill>(x => x.ReturnFromClientWaybill)
            .Where(x => x.State == ReturnFromClientWaybillState.Receipted);

            if (returnFromClientWaybillToExclude != null)
            {
                returnedRow.Where(x => x.ReturnFromClientWaybill.Id != returnFromClientWaybillToExclude.Id);
            }

            var returnedSum = returnedRow.ToList <ReturnFromClientWaybillRow>()
                              .Sum(x => x.SalePrice.Value * x.ReturnCount); // Отпускная цена уже должна быть установлена (иметь Value)

            // Вычисляем сумму накладной реализации
            decimal saleSum = sale.As <ExpenditureWaybill>().SalePriceSum;

            return(saleSum - returnedSum);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Все возвраты указанного товара в указанные сроки, у которых МХ-отправитель входит в указанный список
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="storageIds"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public IEnumerable <ReturnFromClientWaybillRow> GetRows(int articleId, IEnumerable <short> storageIds, DateTime startDate, DateTime endDate, bool finallyMovedWaybillsOnly)
        {
            ISubQuery waybillSubQuery;

            if (finallyMovedWaybillsOnly)
            {
                waybillSubQuery = returnFromClientWaybillRepository.SubQuery <ReturnFromClientWaybill>()
                                  .OneOf(x => x.RecipientStorage.Id, storageIds)
                                  .Where(x => x.ReceiptDate >= startDate && x.ReceiptDate <= endDate)
                                  .Select(x => x.Id);
            }
            else
            {
                waybillSubQuery = returnFromClientWaybillRepository.SubQuery <ReturnFromClientWaybill>()
                                  .OneOf(x => x.RecipientStorage.Id, storageIds)
                                  .Where(x => x.AcceptanceDate >= startDate && x.AcceptanceDate <= endDate)
                                  .Select(x => x.Id);
            }

            return(returnFromClientWaybillRepository.Query <ReturnFromClientWaybillRow>()
                   .PropertyIn(x => x.ReturnFromClientWaybill, waybillSubQuery)
                   .Where(x => x.Article.Id == articleId)
                   .ToList <ReturnFromClientWaybillRow>());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Получение списка входящих позиций по товару, МХ и организации, из которых доступно резервирование товаров
        /// </summary>
        /// <param name="article">Товар</param>
        /// <param name="storage">Место хранения</param>
        /// <param name="organization">Собственная организация</param>
        /// <param name="batch">Опциональный параметр для фильтра по партии</param>
        /// <param name="waybillType">Опциональный параметр для фильтра по типу накладной</param>
        /// <param name="startDate">Опциональный параметр для фильтра по дате (начало интервала)</param>
        /// <param name="endDate">Опциональный параметр для фильтра по дате (конец интервала)</param>
        /// <param name="number">Опциональный параметр для фильтра по номеру накладной</param>
        /// <returns>Отфильтрованный список позиций накладных с товаром article на складе storage от собственной организации organization</returns>
        public IEnumerable <IncomingWaybillRow> GetAvailableToReserveList(Article article, Storage storage, AccountOrganization organization,
                                                                          ReceiptWaybillRow batch = null, Guid?waybillRowId = null, WaybillType?waybillType = null, DateTime?startDate = null, DateTime?endDate = null, string number = null)
        {
            var result = new List <IncomingWaybillRow>();

            var waybillRowArticleMovementSubquery = receiptWaybillRepository.SubQuery <WaybillRowArticleMovement>()
                                                    .Where(x => x.DestinationWaybillRowId == waybillRowId).Select(x => x.SourceWaybillRowId);

            #region Приходы

            if (waybillType == null || waybillType == WaybillType.ReceiptWaybill)
            {
                var receiptWaybillSubQuery = receiptWaybillRepository.SubQuery <ReceiptWaybill>()
                                             .Where(x => x.ReceiptStorage.Id == storage.Id && x.AccountOrganization.Id == organization.Id && x.AcceptanceDate != null).Select(x => x.Id);

                var receiptWaybillRowsQuery = receiptWaybillRepository.Query <ReceiptWaybillRow>()
                                              .Where(x => x.Article.Id == article.Id)
                                              .PropertyIn(x => x.ReceiptWaybill, receiptWaybillSubQuery);

                if (waybillRowId != null)
                {
                    var availableToReserveSubquery = receiptWaybillRepository.SubQuery <ReceiptWaybillRow>().Where(x => x.AvailableToReserveCount > 0).Select(x => x.Id);

                    receiptWaybillRowsQuery.Or(x => x.PropertyIn(y => y.Id, waybillRowArticleMovementSubquery), x => x.PropertyIn(y => y.Id, availableToReserveSubquery));
                }
                else
                {
                    receiptWaybillRowsQuery.Where(x => x.AvailableToReserveCount > 0);
                }

                if (batch != null)
                {
                    receiptWaybillRowsQuery.Where(x => x.Id == batch.Id);
                }
                if (startDate != null)
                {
                    receiptWaybillSubQuery.Where(x => x.Date >= startDate);
                }
                if (endDate != null)
                {
                    receiptWaybillSubQuery.Where(x => x.Date <= endDate);
                }
                if (number != null)
                {
                    receiptWaybillSubQuery.Like(x => x.Number, number);
                }

                var receiptWaybillRows = receiptWaybillRowsQuery.ToList <ReceiptWaybillRow>();

                foreach (var row in receiptWaybillRows)
                {
                    result.Add(ConvertToIncomingWaybillRow(row));
                }
            }

            #endregion

            #region Перемещения

            if (waybillType == null || waybillType == WaybillType.MovementWaybill)
            {
                var movementWaybillSubQuery = movementWaybillRepository.SubQuery <MovementWaybill>()
                                              .Where(x => x.RecipientStorage.Id == storage.Id && x.Recipient.Id == organization.Id && x.AcceptanceDate != null).Select(x => x.Id);

                var movementWaybillRowsQuery = movementWaybillRepository.Query <MovementWaybillRow>()
                                               .PropertyIn(x => x.MovementWaybill, movementWaybillSubQuery);

                if (waybillRowId != null)
                {
                    var availableToReserveSubquery = receiptWaybillRepository.SubQuery <MovementWaybillRow>().Where(x => x.AvailableToReserveCount > 0).Select(x => x.Id);

                    movementWaybillRowsQuery.Or(x => x.PropertyIn(y => y.Id, waybillRowArticleMovementSubquery), x => x.PropertyIn(y => y.Id, availableToReserveSubquery));
                }
                else
                {
                    movementWaybillRowsQuery.Where(x => x.AvailableToReserveCount > 0);
                }

                var batchSubQuery = movementWaybillRepository.SubQuery <ReceiptWaybillRow>().Where(x => x.Article.Id == article.Id).Select(x => x.Id);

                if (batch != null)
                {
                    batchSubQuery.Where(x => x.Id == batch.Id);
                }

                if (number != null || startDate != null || endDate != null)
                {
                    if (number != null)
                    {
                        movementWaybillSubQuery.Like(x => x.Number, number);
                    }
                    if (startDate != null)
                    {
                        movementWaybillSubQuery.Where(x => x.Date >= startDate);
                    }
                    if (endDate != null)
                    {
                        movementWaybillSubQuery.Where(x => x.Date <= endDate);
                    }
                }

                var movementWaybillRows = movementWaybillRowsQuery.PropertyIn(x => x.ReceiptWaybillRow, batchSubQuery).ToList <MovementWaybillRow>();

                foreach (var row in movementWaybillRows)
                {
                    result.Add(ConvertToIncomingWaybillRow(row));
                }
            }

            #endregion

            #region Смены собственника

            if (waybillType == null || waybillType == WaybillType.ChangeOwnerWaybill)
            {
                var changeOwnerWaybillSubQuery = changeOwnerWaybillRepository.SubQuery <ChangeOwnerWaybill>()
                                                 .Where(x => x.Storage.Id == storage.Id && x.Recipient.Id == organization.Id && x.AcceptanceDate != null).Select(x => x.Id);

                var changeOwnerWaybillRowsQuery = changeOwnerWaybillRepository.Query <ChangeOwnerWaybillRow>()
                                                  .PropertyIn(x => x.ChangeOwnerWaybill, changeOwnerWaybillSubQuery);

                if (waybillRowId != null)
                {
                    var availableToReserveSubquery = receiptWaybillRepository.SubQuery <ChangeOwnerWaybillRow>().Where(x => x.AvailableToReserveCount > 0).Select(x => x.Id);

                    changeOwnerWaybillRowsQuery.Or(x => x.PropertyIn(y => y.Id, waybillRowArticleMovementSubquery), x => x.PropertyIn(y => y.Id, availableToReserveSubquery));
                }
                else
                {
                    changeOwnerWaybillRowsQuery.Where(x => x.AvailableToReserveCount > 0);
                }

                var batchSubQuery = changeOwnerWaybillRepository.SubQuery <ReceiptWaybillRow>()
                                    .Where(x => x.Article.Id == article.Id).Select(x => x.Id);

                if (batch != null)
                {
                    batchSubQuery.Where(x => x.Id == batch.Id);
                }

                if (number != null)
                {
                    changeOwnerWaybillSubQuery.Like(x => x.Number, number);
                }
                if (startDate != null)
                {
                    changeOwnerWaybillSubQuery.Where(x => x.Date >= startDate);
                }
                if (endDate != null)
                {
                    changeOwnerWaybillSubQuery.Where(x => x.Date <= endDate);
                }

                var changeOwnerWaybillRows = changeOwnerWaybillRowsQuery.PropertyIn(x => x.ReceiptWaybillRow, batchSubQuery).ToList <ChangeOwnerWaybillRow>();

                foreach (var row in changeOwnerWaybillRows)
                {
                    result.Add(ConvertToIncomingWaybillRow(row));
                }
            }

            #endregion

            #region Возврат от клиента
            if (waybillType == null || waybillType == WaybillType.ReturnFromClientWaybill)
            {
                var returnFromClientWaybillSubQuery = returnFromClientWaybillRepository.SubQuery <ReturnFromClientWaybill>()
                                                      .Where(x => x.RecipientStorage.Id == storage.Id && x.Recipient.Id == organization.Id && x.AcceptanceDate != null).Select(x => x.Id);

                var returnFromClientWaybillRowsQuery = returnFromClientWaybillRepository.Query <ReturnFromClientWaybillRow>()
                                                       .PropertyIn(x => x.ReturnFromClientWaybill, returnFromClientWaybillSubQuery);

                if (waybillRowId != null)
                {
                    var availableToReserveSubquery = receiptWaybillRepository.SubQuery <ReturnFromClientWaybillRow>().Where(x => x.AvailableToReserveCount > 0).Select(x => x.Id);

                    returnFromClientWaybillRowsQuery.Or(x => x.PropertyIn(y => y.Id, waybillRowArticleMovementSubquery), x => x.PropertyIn(y => y.Id, availableToReserveSubquery));
                }
                else
                {
                    returnFromClientWaybillRowsQuery.Where(x => x.AvailableToReserveCount > 0);
                }

                var batchSubQuery = returnFromClientWaybillRepository.SubQuery <ReceiptWaybillRow>().Where(x => x.Article.Id == article.Id).Select(x => x.Id);

                if (batch != null)
                {
                    batchSubQuery.Where(x => x.Id == batch.Id);
                }

                if (number != null)
                {
                    returnFromClientWaybillSubQuery.Like(x => x.Number, number);
                }
                if (startDate != null)
                {
                    returnFromClientWaybillSubQuery.Where(x => x.Date >= startDate);
                }
                if (endDate != null)
                {
                    returnFromClientWaybillSubQuery.Where(x => x.Date <= endDate);
                }

                var returnFromClientWaybillRows = returnFromClientWaybillRowsQuery
                                                  .PropertyIn(x => x.ReceiptWaybillRow, batchSubQuery).ToList <ReturnFromClientWaybillRow>();

                foreach (var row in returnFromClientWaybillRows)
                {
                    result.Add(ConvertToIncomingWaybillRow(row));
                }
            }

            #endregion

            return(result);
        }