public EmployeeCardItem(EmployeeCard employee, NormItem normItem)
 {
     EmployeeCard = employee;
     ActiveNormItem = normItem;
     Item = normItem.Item;
     NextIssue = Created = DateTime.Today;
 }
        public static IList<EmployeeItemsBalanceDTO> ItemsBalance(IUnitOfWork uow, EmployeeCard employee)
        {
            EmployeeItemsBalanceDTO resultAlias = null;

            Expense reciveDocAlias = null;
            ExpenseItem recivedItemAlias = null;
            Nomenclature nomenclatureAlias = null;
            ItemsType itemtypesAlias = null;
            IncomeItem returnedItemAlias = null;
            WriteoffItem writeoffItemAlias = null;

            var incomes = uow.Session.QueryOver<Expense> (() => reciveDocAlias)
                .Where(d => d.EmployeeCard == employee)
                .JoinQueryOver (d => d.Items, () => recivedItemAlias)
                .Where (i => i.AutoWriteoffDate == null || i.AutoWriteoffDate > DateTime.Today);

            var subqueryRemove = QueryOver.Of<IncomeItem>(() => returnedItemAlias)
                .Where(() => returnedItemAlias.IssuedOn.Id == recivedItemAlias.Id)
                .Select (Projections.Sum<IncomeItem> (o => o.Amount));

            var subqueryWriteOff = QueryOver.Of<WriteoffItem>(() => writeoffItemAlias)
                .Where(() => writeoffItemAlias.IssuedOn.Id == recivedItemAlias.Id)
                .Select (Projections.Sum<WriteoffItem> (o => o.Amount));

            var incomeList = incomes
                .JoinAlias (() => recivedItemAlias.Nomenclature, () => nomenclatureAlias)
                .JoinAlias (() => nomenclatureAlias.Type, () => itemtypesAlias)
                .Where (Restrictions.Gt (
                    Projections.SqlFunction(
                        new VarArgsSQLFunction("(", "-", ")"),
                        NHibernateUtil.Int32,
                        Projections.Property (() => recivedItemAlias.Amount),
                        Projections.SqlFunction("COALESCE",
                            NHibernateUtil.Int32,
                            Projections.SubQuery (subqueryRemove),
                            Projections.Constant (0)
                        ),
                        Projections.SqlFunction("COALESCE",
                            NHibernateUtil.Int32,
                            Projections.SubQuery (subqueryWriteOff),
                            Projections.Constant (0)
                        )
                    ), 0)
                )
                .SelectList (list => list
                    .SelectGroup (() => recivedItemAlias.Id).WithAlias (() => resultAlias.ExpenseItemId)
                    .Select (() => reciveDocAlias.Date).WithAlias (() => resultAlias.LastReceive)
                    .Select (() => nomenclatureAlias.Id).WithAlias (() => resultAlias.NomenclatureId)
                    .Select (() => nomenclatureAlias.Type.Id).WithAlias (() => resultAlias.ItemsTypeId)
                    .Select (() => recivedItemAlias.Amount).WithAlias (() => resultAlias.Received)
                    .SelectSubQuery (subqueryRemove).WithAlias (() => resultAlias.Returned)
                    .SelectSubQuery (subqueryWriteOff).WithAlias (() => resultAlias.Writeoff)
                )
                .TransformUsing (Transformers.AliasToBean<EmployeeItemsBalanceDTO> ())
                .List<EmployeeItemsBalanceDTO> ();

            return incomeList;
        }
        public static IList<Nomenclature> MatchNomenclaturesBySize(IUnitOfWork uow, ItemsType itemType, EmployeeCard employee)
        {
            if(itemType.WearCategory == null)
            {
                logger.Warn ("Вызван подбор номерклатур по размеру, для типа <{0}>, но в нем не указан вид спецодежды.", itemType.Name);
                return null;
            }

            var query = uow.Session.QueryOver<Nomenclature> ()
                .Where (n => n.Type == itemType);

            if (SizeHelper.HasСlothesSizeStd(itemType.WearCategory.Value))
            {
                var disjunction = new Disjunction();
                var employeeSize = employee.GetSize(itemType.WearCategory.Value);

                if (employeeSize == null || String.IsNullOrEmpty(employeeSize.Size) || String.IsNullOrEmpty(employeeSize.StandardCode))
                {
                    logger.Warn("В карточке сотрудника не указан размер для спецодежды типа <{0}>.", itemType.Name);
                    return null;
                }

                foreach (var pair in SizeHelper.MatchSize (employeeSize, SizeUsePlace.Сlothes))
                {
                    disjunction.Add(
                        Restrictions.And(
                            Restrictions.Eq(Projections.Property<Nomenclature>(n => n.SizeStd), pair.StandardCode),
                            Restrictions.Eq(Projections.Property<Nomenclature>(n => n.Size), pair.Size)
                        ));
                }
                query.Where(disjunction);
                if (SizeHelper.HasGrowthStandart(itemType.WearCategory.Value))
                {
                    var growDisjunction = new Disjunction();
                    var growStds = SizeHelper.GetGrowthStandart(itemType.WearCategory.Value, employee.Sex, SizeUsePlace.Сlothes);
                    foreach (var pair in SizeHelper.MatchGrow (growStds, employee.WearGrowth, SizeUsePlace.Сlothes))
                    {
                        growDisjunction.Add(
                            Restrictions.And(
                                Restrictions.Eq(Projections.Property<Nomenclature>(n => n.WearGrowthStd), pair.StandardCode),
                                Restrictions.Eq(Projections.Property<Nomenclature>(n => n.WearGrowth), pair.Size)
                            ));
                    }

                    query.Where(growDisjunction);
                }
            }

            return query.List ();
        }