public void Can_Save_Person_To_DB()
        {
            // Arrange
            PhysicalPerson physicalPerson = new PhysicalPerson("12345678901", "Ivo", "Ivic");
            LegalPerson legalPerson = new LegalPerson("12345678902", "FER");

            PersonsNHRepository personsNhRepository = new PersonsNHRepository(SessionFactory);

            // Act
            using (var session = SessionFactory.OpenSession()) {
                using(var tx = Session.BeginTransaction()) {
                    personsNhRepository.SaveOrUpdate(physicalPerson);
                    personsNhRepository.SaveOrUpdate(legalPerson);
                    tx.Commit();
                }

            }

            IList<Person> persons = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = Session.BeginTransaction()) {
                    persons = personsNhRepository.GetAll().ToList();
                    tx.Commit();
                }

            }

            // Assert
            Assert.IsTrue(persons.Contains(physicalPerson), "PhysicalPerson doesn't exists in database.");
            Assert.IsTrue(persons.Contains(legalPerson), "LegalPerson doesn't exists in database.");
        }
        public void Can_Save_Person_With_Address_To_DB()
        {
            // Arrange
            City zagreb = new City(10000, "Zagreb");
            Person person = new PhysicalPerson("12345678901", "Ivo", "Ivic") {
                Address = new Address("Ulica", "1A", zagreb)
            };

            PersonsNHRepository personsNhRepository = new PersonsNHRepository(SessionFactory);

            // Act
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    session.Save(zagreb);
                    personsNhRepository.SaveOrUpdate(person);
                    tx.Commit();
                }

            }

            IList<Person> persons = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    persons = personsNhRepository.GetAll().ToList();
                    tx.Commit();
                }

            }

            // Assert
            Assert.IsTrue(persons.Contains(person), "PhysicalPerson doesn't exists in database.");
            Assert.AreEqual(person.Address, persons[0].Address, "Two persons haven't the sam addresses.");
            Assert.AreEqual(zagreb.Id, 1, "City isn't persisted to database.");
        }
        public void Can_Save_LandRegistry()
        {
            // Arrange
            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);
            var person = new PhysicalPerson("12345678901", "Ime", "Prezime");

            landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LandRegistriesNHRepository landRegistriesNhRepository = new LandRegistriesNHRepository(SessionFactory);

            // Act
            LandRegistry fetchedLandRegistry;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    session.Save(city);
                    session.Save(person);
                    session.Save(cadastre);
                    landRegistriesNhRepository.SaveOrUpdate(landRegistry);
                    tx.Commit();
                }

                using (var tx = session.BeginTransaction()) {
                    fetchedLandRegistry =
                        landRegistriesNhRepository.GetByNumberOfCadastralParticle(cadastralParticle.NumberOfCadastralParticle);
                    tx.Commit();
                }
            }

            // Assert
            Assert.AreEqual(landRegistry, fetchedLandRegistry, "LandRegistries aren't equal");
            Assert.IsTrue(fetchedLandRegistry.PartitionSpaces.Count == 1, "LandRegistry haven't exactly one partition space.");
        }
        public void Can_Save_Bill_To_Db()
        {
            // Arrange
            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);

            var person = new PhysicalPerson("12345678901", "Ime", "Prezime") {
                Address = new Address("Ulica", "1", city)
            };

            var partitionSpace = landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LegalPerson legalPerson = new LegalPerson("12345678902", "Ime") {
                NumberOfBankAccount = "123456",
                Address = new Address("Ulica", "2", city)
            };
            BuildingManager buildingManager = new BuildingManager(legalPerson);

            Building building = new Building(buildingManager) {
                LandRegistry = landRegistry
            };

            Bill bill = new Bill(legalPerson, building.Reserve, "opis plaćanja", 23) {
                ReferenceNumber = "123"
            };
            bill.AddBillItem(new BillItem(1, 12.3m, "Opis"));

            IBillsRepository billsRepository = new BillsNHRepository(SessionFactory);

            // Act
            using (var session = SessionFactory.OpenSession()) {
                using (var transaction = session.BeginTransaction()) {
                    session.Save(city);
                    session.Save(person);
                    session.Save(legalPerson);
                    session.Save(cadastre);
                    session.Save(landRegistry);
                    session.Save(buildingManager);
                    session.Save(building);

                    billsRepository.SaveOrUpdate(bill);

                    transaction.Commit();
                }
            }

            IList<Bill> billsFromDb = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var transaction = session.BeginTransaction()) {
                    billsFromDb = billsRepository.GetAll().ToList();
                    transaction.Commit();
                }
            }

            // Assert
            Assert.IsTrue(billsFromDb.Count == 1, "No Bill from database.");
        }
        public void Can_Save_Maintenance_To_DB()
        {
            // Arrange
            var person = new PhysicalPerson("12345678901", "Ime", "Prezime");
            MaintenanceRequest maintenanceRequest = new MaintenanceRequest(
                person
                , "Kvar na grijanju"
                , "Grijanje ne radi"
                , "Prvi kat, stan 2");

            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);

            var partitionSpace = landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LegalPerson legalPerson = new LegalPerson("12345678902", "Ime");
            BuildingManager buildingManager = new BuildingManager(legalPerson);

            Building building = new Building(buildingManager) {
                LandRegistry = landRegistry
            };

            RepairService repairService = new RepairService("Popravak grijanja");

            Maintenance maintenance = new Maintenance(maintenanceRequest, Urgency.Normal, repairService, building);

            IMaintenancesRepository maintenancesRepository = new MaintenancesNHRepository(SessionFactory);

            // Act
            IList<Maintenance> maintenancesFromDb = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    session.Save(person);
                    session.Save(city);
                    session.Save(legalPerson);
                    session.Save(cadastre);
                    session.Save(landRegistry);
                    session.Save(buildingManager);
                    session.Save(building);
                    session.Save(repairService);

                    maintenancesRepository.SaveOrUpdate(maintenance);

                    tx.Commit();
                }

                using (var tx = session.BeginTransaction()) {
                    maintenancesFromDb = maintenancesRepository.GetAll().ToList();
                    tx.Commit();
                }
            }

            // Assert
            Assert.IsTrue(maintenancesFromDb.Count == 1, "No Maintenance from database.");
        }
        public void Can_Read_Reserve_From_Db()
        {
            // Arrange
            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);

            var person = new PhysicalPerson("12345678901", "Ime", "Prezime");
            var partitionSpace = landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LegalPerson legalPerson = new LegalPerson("12345678902", "Ime") {
                NumberOfBankAccount = "12332213",
                Address = new Address("dsa", "2", city)
            };
            BuildingManager buildingManager = new BuildingManager(legalPerson);

            Building building = new Building(buildingManager) {
                LandRegistry = landRegistry
            };

            Bill bill = new Bill(legalPerson, building.Reserve, "račun", 23) {
                ReferenceNumber = "123"
            };
            building.Reserve.AddBillForPayment(bill);

            // Act
            using (var session = SessionFactory.OpenSession()) {
                using (var transaction = session.BeginTransaction()) {
                    session.Save(city);
                    session.Save(cadastre);
                    session.Save(person);
                    session.Save(landRegistry);
                    session.Save(legalPerson);
                    session.Save(buildingManager);
                    session.Save(building);

                    transaction.Commit();
                }
            }

            IReservesRepository reservesRepository = new ReservesNHRepository(SessionFactory);
            IList<Reserve> reservesFromDb = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var transaction = session.BeginTransaction()) {
                    reservesFromDb = reservesRepository.GetAll().ToList();
                    transaction.Commit();
                }
            }

            // Assert
            Assert.IsTrue(reservesFromDb.Count == 1, "No Reserves from database.");
            //Assert.IsTrue(reservesFromDb[0].ReserveBills.Count == 1);
        }
        public void Can_Save_AdminJobsVoting_To_DB()
        {
            // Arrange
            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);

            var person = new PhysicalPerson("12345678901", "Ime", "Prezime");
            var partitionSpace = landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LegalPerson legalPerson = new LegalPerson("12345678902", "Ime");
            BuildingManager buildingManager = new BuildingManager(legalPerson);

            Building building = new Building(buildingManager) {
                LandRegistry = landRegistry
            };

            AdministrationJobsVoting administrationJobsVoting = new AdministrationJobsVoting(
                AdministrationJobsType.Regular,
                building,
                "Subject",
                "Description",
                new DateTime(2011, 11, 11));

            AdminJobsVotingsNHRepository adminJobsVotingsNhRepository = new AdminJobsVotingsNHRepository(SessionFactory);

            // Act
            AdministrationJobsVoting adminJobsVotingFromDb = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    session.Save(city);
                    session.Save(person);
                    session.Save(legalPerson);
                    session.Save(cadastre);
                    session.Save(landRegistry);
                    session.Save(buildingManager);
                    session.Save(building);

                    adminJobsVotingsNhRepository.SaveOrUpdate(administrationJobsVoting);
                    tx.Commit();
                }

                using (var tx = session.BeginTransaction()) {
                    adminJobsVotingFromDb = adminJobsVotingsNhRepository.GetById(administrationJobsVoting.Id);
                    tx.Commit();
                }
            }

            // Assert
            Assert.AreEqual(administrationJobsVoting, adminJobsVotingFromDb, "Two administration jobs voting entities aren't equal.");
        }
        public void LandRegistry_With_PartitionSpaces_Has_Total_SurfaceAreas()
        {
            // Arrange
            landRegistry.CreatePartitionSpace("123", 23.52m, "Stan 1");

            var owner = new PhysicalPerson("12345678901", "Mile", "Milic");

            landRegistry.CreatePartitionSpace("123", 35.1m, "Stan 2", owner);

            // Act
            var result = landRegistry.TotalSurfaceOfPartitionSpaces;

            // Assert
            Assert.AreEqual(58.62, result);
        }
        public void Can_Add_Multiple_Telephone_Numbers()
        {
            // Arrange
            PhysicalPerson person = new PhysicalPerson("12345678901", "Mile", "Milic");
            Telephone telephone1 = new Telephone("Kucni telefon", "123456");
            Telephone telephone2 = new Telephone("Mobitel", "098123456");

            // Act
            person.AddTelephone(telephone1);
            person.AddTelephone(telephone2);

            // Assert
            Assert.AreEqual(2, person.Telephones.Count, "Trebala bi postojati dva telefonska broja.");
            Assert.AreEqual("123456", person.Telephones.ElementAt(0).TelephoneNumber, "Prvi broj telefona nije isti kao zadani.");
            Assert.AreEqual("098123456", person.Telephones.ElementAt(1).TelephoneNumber, "Drugi broj telefona nije isti kao zadani.");
        }
        public void Can_Take_Snapshot_Of_Person()
        {
            // Arrange
            PhysicalPerson person = new PhysicalPerson("12345678901", "Mile", "Milic");
            Telephone telephone1 = new Telephone("Kucni telefon", "123456");
            Telephone telephone2 = new Telephone("Mobitel", "098123456");

            person.AddTelephone(telephone1);
            person.AddTelephone(telephone2);

            // Act
            var personSnapshot = new PersonSnapshot(person);

            // Assert
            Assert.AreEqual(person.Oib, personSnapshot.Oib);
            Assert.AreEqual(person.FullName, personSnapshot.FullName);
            Assert.AreEqual(person.Address, personSnapshot.Address);
        }
        public void Can_Remove_Existing_Telehone_Number()
        {
            // Arange
            PhysicalPerson person = new PhysicalPerson("12345678901", "Mile", "Milic");
            Telephone telephone1 = new Telephone("Kucni telefon", "123456");
            Telephone telephone2 = new Telephone("Mobitel", "098123456");

            person.AddTelephone(telephone1);
            person.AddTelephone(telephone2);

            // Act
            var isSuccess = person.RemoveTelephone(telephone1);

            // Assert
            Assert.IsTrue(isSuccess);
            Assert.AreEqual(1, person.Telephones.Count, "Trebao bi postojati jedan telefonski broj.");
            Assert.AreEqual("098123456", person.Telephones.ElementAt(0).TelephoneNumber, "To nije broj mobitela, odnosno drugi zadani broj telefona..");
        }
        public void Can_Save_Building_To_DB()
        {
            // Arrange
            City city = new City(10000, "Zagreb");
            Cadastre cadastre = new Cadastre("Trnje", "12345", city);
            CadastralParticle cadastralParticle = new CadastralParticle(cadastre, "123", 23, "Opis");
            LandRegistry landRegistry = new LandRegistry(cadastralParticle);

            var person = new PhysicalPerson("12345678901", "Ime", "Prezime");
            var partitionSpace = landRegistry.CreatePartitionSpace("123", 12, "Opis etaže", person);

            LegalPerson legalPerson = new LegalPerson("12345678902", "Ime");
            BuildingManager buildingManager = new BuildingManager(legalPerson);

            Building building = new Building(buildingManager) {
                LandRegistry = landRegistry
            };

            BuildingsNHRepository buildingsNhRepository = new BuildingsNHRepository(SessionFactory);

            // Act
            Building buildingFromDb = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    session.Save(city);
                    session.Save(person);
                    session.Save(legalPerson);
                    session.Save(cadastre);
                    session.Save(landRegistry);
                    session.Save(buildingManager);

                    buildingsNhRepository.SaveOrUpdate(building);
                    tx.Commit();
                }

                using (var tx = session.BeginTransaction()) {
                    buildingFromDb = buildingsNhRepository.GetById(building.Id);
                    tx.Commit();
                }
            }

            // Assert
            Assert.AreEqual(building, buildingFromDb, "Two building entities aren't equal.");
        }
        public void Can_Save_Person_With_Telephone_To_DB()
        {
            // Arrange
            Person person = new PhysicalPerson("12345678901", "Ivo", "Ivic");
            person.AddTelephone(new Telephone("Posao", "098 888 999"));

            PersonsNHRepository personsNhRepository = new PersonsNHRepository(SessionFactory);

            // Act
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    personsNhRepository.SaveOrUpdate(person);
                    tx.Commit();
                }

            }

            IList<Person> persons = null;
            using (var session = SessionFactory.OpenSession()) {
                using (var tx = session.BeginTransaction()) {
                    persons = personsNhRepository.GetAll().ToList();
                    tx.Commit();
                }

            }

            // Assert
            Assert.IsTrue(persons.Contains(person), "PhysicalPerson doesn't exists in database.");
            Assert.IsTrue(person.Telephones.Contains(persons[0].Telephones.ElementAt(0)));
        }
        public ActionResult RegisterOwner(RegisterOwnerUserModel ownerUserModel)
        {
            if (ModelState.IsValid) {
                Person person = personsRepository.GetByOib(ownerUserModel.Oib);
                bool isUserExistingForPerson = false;
                if(person != null) {
                    var existingUser = housingMgmtUsersRepository.GetUserByPerson(person);
                    isUserExistingForPerson = existingUser != null;
                }

                if(!isUserExistingForPerson) {
                    MembershipCreateStatus createStatus;
                    NHibernateMembershipUser membershipUser =
                        Membership.CreateUser(ownerUserModel.UserName, ownerUserModel.Password, ownerUserModel.Email,
                                              null, null, true, null, out createStatus) as NHibernateMembershipUser;

                    if (createStatus == MembershipCreateStatus.Success) {
                        if (person == null) {
                            City city = citiesRepository.GetById(ownerUserModel.Address.City.Id);

                            if (string.IsNullOrEmpty(ownerUserModel.Surname)) {
                                person = new LegalPerson(ownerUserModel.Oib, ownerUserModel.Name);
                            } else {
                                person = new PhysicalPerson(ownerUserModel.Oib, ownerUserModel.Name, ownerUserModel.Surname);
                            }

                            person.Address = new Address(ownerUserModel.Address.StreetAddress, ownerUserModel.Address.StreetAddressNumber,
                                                         city);

                            if (!string.IsNullOrEmpty(ownerUserModel.TelephoneNumber)) {
                                person.AddTelephone(new Telephone("Telefon", ownerUserModel.TelephoneNumber));
                            }

                            if (!string.IsNullOrEmpty(ownerUserModel.MobileNumber)) {
                                person.AddTelephone(new Telephone("Mobitel", ownerUserModel.MobileNumber));
                            }
                        }

                        try {
                            membershipUser.User.Person = person;

                            var role = rolesRepository.GetRole("owner");
                            membershipUser.User.AddRole(role);

                            FormsAuthentication.SetAuthCookie(ownerUserModel.UserName, false);
                            personsRepository.SaveOrUpdate(person);
                            emailNotifier.NotifyOfRegistration(membershipUser.User);
                            return RedirectToAction("index", "dashboard");

                        } catch (BusinessRulesException ex) {
                            ex.CopyTo(ModelState);
                        }

                    } else {
                        ModelState.AddModelError("", errorCodeToString(createStatus));
                    }
                } else {
                    ModelState.AddModelError("", "Navedena osoba već ima kreiran korisnički račun.");
                }

            }

            ownerUserModel.Address.Cities = new SelectList(citiesRepository.GetAll(), "Id", "Name");

            return View(ownerUserModel);
        }
 public void If_Oib_Is_Invalid_Should_Throw_RuleException()
 {
     var person1 = new PhysicalPerson("123456", "Mile", "Milic");
     var person2 = new PhysicalPerson("abs", "Mile", "Milic");
 }
        private static Person getPerson()
        {
            // Dohvacanje osobe iz repozitorija
            Person personForMock = new PhysicalPerson("12345678901", "Mile", "Milic");

            Mock<IPersonsRepository> personRepositoryMock = new Mock<IPersonsRepository>();
            personRepositoryMock.Setup(x => x.GetByOib("12345678901")).Returns(personForMock);

            return personRepositoryMock.Object.GetByOib("12345678901");
        }
        public void Can_Receive_Money_From_Payed_Bill()
        {
            // Arrange
            Reserve reserve = building.Reserve;
            decimal currentMoneyStatus = reserve.Money;
            var person = new PhysicalPerson("12345678903", "Mile", "Milic");

            Bill bill = new Bill(building.Reserve, person, "opis plaćanja", 23);
            var partitionSpace = building.LandRegistry.CreatePartitionSpace("123", 12m, "Opis", person);
            reserve.IssueReserveBillFor(partitionSpace, 23);

            // Act
            reserve.ReceivePaymentFor(bill);

            // Assert
            Assert.AreEqual(currentMoneyStatus + bill.TotalAmountWithTax, reserve.Money);
            Assert.IsTrue(bill.IsPaid);
        }
        public ActionResult ChangeOwner(int id, ChangeOwnerModel model)
        {
            if (!User.IsInRole("buildingmanager") && !User.IsInRole("representative")) {
                return new HttpUnauthorizedResult();
            }

            var partitionSpace = partitionSpacesRepository.GetById(id);
            if (partitionSpace == null) {
                return HttpNotFound();
            }

            if (!partitionSpace.IsOwnedPartitionSpace) {
                return new HttpStatusCodeResult(405);
            }

            if(ModelState.IsValid) {
                var person = personsRepository.GetByOib(model.NewOwner.Oib);
                if(person != null) {
                    partitionSpace.Owner = person;
                } else {
                    Person newPerson = null;
                    if(string.IsNullOrEmpty(model.NewOwner.Surname)) {
                        newPerson = new LegalPerson(model.NewOwner.Oib, model.NewOwner.Name);
                    } else {
                        newPerson = new PhysicalPerson(model.NewOwner.Oib, model.NewOwner.Name, model.NewOwner.Surname);
                    }

                    var city = citiesRepository.GetById(model.NewOwner.Address.City.Id);
                    newPerson.Address = new Address(model.NewOwner.Address.StreetAddress, model.NewOwner.Address.StreetAddressNumber, city);

                    var ownerRole = rolesRepository.GetRole("owner");
                    var contractorRole = rolesRepository.GetRole("contractor");
                    var managerRole = rolesRepository.GetRole("buildingmanager");

                    var previousOwner = partitionSpace.Owner;
                    var previousOwnerUser = housingMgmtUsersRepository.GetUserByPerson(previousOwner);
                    if(previousOwnerUser.Roles.Contains(contractorRole) || previousOwnerUser.Roles.Contains(managerRole)) {
                        previousOwnerUser.RemoveRole(ownerRole);
                    }

                    partitionSpace.Owner = newPerson;
                    var newOwnerUser = housingMgmtUsersRepository.GetUserByPerson(newPerson);
                    if(newOwnerUser != null && !newOwnerUser.Roles.Contains(ownerRole)) {
                        newOwnerUser.AddRole(ownerRole);
                    }

                }

                return RedirectToAction("partitionspace", new {Id = id});
            }

            LinksModel links = new LinksModel();
            if (Session["lastPageId"] != null) {
                links.Id = (int)Session["lastPageId"];
            }

            var building = buildingsRepository.GetByPartitionSpace(partitionSpace);

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Zemljišna knjiga");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Zemljišna knjiga");
            }

            model.NewOwner.Address.Cities = new SelectList(citiesRepository.GetAll(), "Id", "Name", model.NewOwner.Address.City.Id);
            model.Roles = Roles.GetRolesForUser();
            model.CurrentRole = role;
            model.Links = links;

            return View(model);
        }