public void Settings(uint id)
        {
            var user = DbSession.Load <User>(id);

            PropertyBag["user"]      = user;
            PropertyBag["maxRegion"] = UInt64.MaxValue;
            if (user.Client == null)
            {
                var supplier = DbSession.Load <Supplier>(user.RootService.Id);
                if (supplier != null)
                {
                    PropertyBag["AllowWorkRegions"] = Region.GetRegionsByMask(DbSession, supplier.RegionMask);
                }
                PropertyBag["permissions"] = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.SupplierInterface);
                RenderView("SupplierSettings");
            }
            else
            {
                var setting = user.Client.Settings;
                PropertyBag["AllowOrderRegions"] = Region.GetRegionsByMask(DbSession, setting.OrderRegionMask);
                PropertyBag["AllowWorkRegions"]  = Region.GetRegionsByMask(DbSession, user.Client.MaskRegion);
                PropertyBag["permissions"]       = UserPermission.FindPermissionsForDrugstore(DbSession);
                PropertyBag["ExcelPermissions"]  = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFExcel);
                PropertyBag["AccessPermissions"] = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFPrint)
                                                   .Where(r => r.Shortcut == "ORDR" || r.Shortcut == "CASH" || r.Shortcut == "STCK").ToArray();
                PropertyBag["PrintPermissions"] = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFPrint)
                                                  .Where(r => r.Shortcut != "ORDR" && r.Shortcut != "CASH" && r.Shortcut != "STCK").ToArray();
                if (user.AFNetConfig != null)
                {
                    user.AFNetConfig.Channels = BinChannel.Load(DbSession, user);
                }
                RenderView("DrugstoreSettings");
            }
        }
        public void Add(uint clientId, User client = null)
        {
            /*Грязный ХАК, почему-то если принудительно не загрузить так, не делается Service.FindAndCheck<Service>(clientId)*/
            DbSession.Get <Client>(clientId);
            DbSession.Get <Supplier>(clientId);

            var service = client == null?Service.FindAndCheck <Service>(clientId) : client.RootService;

            var user = client ?? new User(service);

            PropertyBag["UserMessage"]     = "Текст сообщения";
            PropertyBag["deliveryAddress"] = "";
            PropertyBag["account"]         = user.Accounting;

            if (client == null)
            {
                PropertyBag["UseDefPermession"] = true;
                PropertyBag["SendToEmail"]      = true;
                //Для вьюшки - по умолчанию должен быть True.
                //Но в модели мы это не можем разместить, так как по здравому смыслу он по-умолчанию false.
                //Так мы избежим лишних действий по созданию прав на директории
                user.FtpAccess = true;
                var rejectWaibillParams = new RejectWaibillParams().Get(clientId, DbSession);
                user.SendWaybills = rejectWaibillParams.SendWaybills;
                user.SendRejects  = rejectWaibillParams.SendRejects;
            }

            PropertyBag["phonesForSendToUserList"]  = user.GetPhonesForSendingSms();            //"9031848398";
            PropertyBag["phonesForSendToAdminList"] = GetAdminByRegionForSms(user.RootService.HomeRegion.Id);

            PropertyBag["client"] = service;
            if (service.IsClient())
            {
                PropertyBag["drugstore"] = ((Client)service).Settings;
                var organizations = ((Client)service).Orgs().ToArray();
                if (organizations.Length == 1)
                {
                    PropertyBag["address"] = new Address {
                        LegalEntity = organizations.First()
                    };
                }
                PropertyBag["Organizations"] = organizations;
                PropertyBag["permissions"]   = UserPermission.FindPermissionsForDrugstore(DbSession);
            }
            else
            {
                PropertyBag["singleRegions"]        = true;
                PropertyBag["registerSupplierUser"] = true;
                PropertyBag["availibleRegions"]     = ((Supplier)service).RegionMask;
                PropertyBag["permissions"]          = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.SupplierInterface);
            }
            PropertyBag["user"]              = user;
            PropertyBag["ExcelPermissions"]  = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFExcel);
            PropertyBag["AccessPermissions"] = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFPrint)
                                               .Where(r => r.Shortcut == "ORDR" || r.Shortcut == "CASH" || r.Shortcut == "STCK").ToArray();
            PropertyBag["PrintPermissions"] = UserPermission.FindPermissionsByType(DbSession, UserPermissionTypes.AnalitFPrint)
                                              .Where(r => r.Shortcut != "ORDR" && r.Shortcut != "CASH" && r.Shortcut != "STCK").ToArray();
            PropertyBag["emailForSend"]     = user.GetAddressForSendingClientCard();
            PropertyBag["EmailContactType"] = ContactType.Email;
            PropertyBag["PhoneContactType"] = ContactType.Phone;
            PropertyBag["regions"]          = Region.All(DbSession).ToArray();
            IList <Payer> payers = new List <Payer>();

            if (service.IsClient())
            {
                payers = ((Client)service).Payers;
            }
            else
            {
                payers = new List <Payer> {
                    DbSession.Load <Supplier>(service.Id).Payer
                }
            };

            if (payers.Count == 1)
            {
                user.Payer = payers.First();
                PropertyBag["onePayer"] = true;
            }
            else
            {
                PropertyBag["onePayer"] = false;
            }
            PropertyBag["Payers"]           = payers;
            PropertyBag["maxRegion"]        = UInt64.MaxValue;
            PropertyBag["UserRegistration"] = true;
            PropertyBag["defaultSettings"]  = Defaults;
        }