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 ActionResult IssueBill(int id, IssueBillModel model)
        {
            if (!User.IsInRole("buildingmanager")) { return new HttpUnauthorizedResult(); }

            LegalPerson legalPerson = personsRepository.GetLegalPersonByUsername(User.Identity.Name);
            var building = buildingsRepository.GetById(id);
            if (building == null) {
                return HttpNotFound();
            }

            if (!building.BuildingManager.LegalPerson.Equals(legalPerson)) {
                return new HttpUnauthorizedResult();
            }

            if (ModelState.IsValid) {
                try {
                    var bill = new Bill(legalPerson, building.Reserve, model.PaymentDescription, 23) {
                        ReferenceNumber = string.Format("{0}-{1}-{2}", building.Id, building.BuildingManager.Id, DateTime.Now.ToString("yyyy-MM-dd"))
                    };

                    foreach (var billItemModel in model.BillItems) {
                        var billItem = new BillItem(billItemModel.Quantity, billItemModel.Price, billItemModel.Description);
                        bill.AddBillItem(billItem);
                    }

                    billsRepository.SaveOrUpdate(bill);
                    var url = Url.Action("bill", "buildingmanager", new {Id = bill.Id}, "http");
                    emailNotifier.NotifyOfBilling(bill, url);
                    return RedirectToAction("bills");

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

            model.Roles = Roles.GetRolesForUser();
            model.Building = Mapper.Map<Building, BuildingListModel>(building);

            return View(model);
        }
Esempio n. 3
0
        /// <summary>
        /// Izadaje racun za pricuvu
        /// </summary>
        /// <param name="partitionSpace">stan za kojieg se izdaje racun za placanje pricuve</param>
        /// <param name="tax">porez</param>
        /// <returns>racun za pricuvu</returns>
        public virtual void IssueReserveBillFor(IPartitionSpace partitionSpace, short tax)
        {
            if (partitionSpace.Owner == null) { throw new BusinessRulesException("Owner doesn't exist."); }
            if(!building.LandRegistry.PartitionSpaces.Contains(partitionSpace)) {
                throw new BusinessRulesException("Etaža ne pripada ovoj zgradi, onodno pričuvi.");
            }

            StringBuilder paymentDescriptionSb = new StringBuilder();
            paymentDescriptionSb.AppendLine(string.Format("Račun mjesečne pričuve za zgradu {0}.", building.Address));
            paymentDescriptionSb.AppendLine(string.Format("Temeljem stana broj uloška: {0}", partitionSpace.RegistryNumber));
            paymentDescriptionSb.AppendLine(string.Format("Površine {0}, i koeficijenta pričuve {1}.", partitionSpace.SurfaceArea, building.ReserveCoefficient));

            Bill bill = new Bill(this, partitionSpace.Owner, paymentDescriptionSb.ToString(), tax);
            decimal price = building.ReserveCoefficient * partitionSpace.SurfaceArea;
            BillItem billItem = new BillItem(1, price, "Mjesečna naknada za pričuvu");
            bill.AddBillItem(billItem);
            bill.ReferenceNumber = string.Format("{0}-{1}-{2}", REFERENCE_NUMBER_PREFIX, partitionSpace.Id, bill.DateTimeIssued.ToString("yyyy-MM-dd"));

            reserveBills.Add(bill);
        }
        public ActionResult IssueBill(IssueBillModel model)
        {
            if (!User.IsInRole("contractor")) { return new HttpUnauthorizedResult(); }

            LegalPerson legalPerson = personsRepository.GetLegalPersonByUsername(User.Identity.Name);

            if(ModelState.IsValid) {
                var maintenance = maintenancesRepository.GetById(model.UnbilledMaintance);
                if(maintenance != null) {
                    try {
                        var bill = new Bill(legalPerson, maintenance.Building.Reserve, model.PaymentDescription, 23) {
                            ReferenceNumber = string.Format("{0}-{1}-{2}", maintenance.Building.Id, maintenance.Id, DateTime.Now.ToString("yyyy-MM-dd"))
                        };
                        foreach (var billItemModel in model.BillItems) {
                            var billItem = new BillItem(billItemModel.Quantity, billItemModel.Price, billItemModel.Description);
                            bill.AddBillItem(billItem);
                        }

                        maintenance.SetBill(bill);
                        billsRepository.SaveOrUpdate(bill);

                        var url = Url.Action("bill", "contractor", new {Id = bill.Id}, "http");
                        emailNotifier.NotifyOfBilling(bill, url);
                        return RedirectToAction("bills");

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

                } else {
                    ModelState.AddModelError("UnbilledMaintance", "Odabrali ste ne postojeće održavanje/popravak.");
                }
            }

            var maintenances = maintenancesRepository.GetAllMaintenancesWithNoBillByContractor(legalPerson);
            var mappedMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(maintenances);
            model.UnbilledMaintances = new SelectList(mappedMaintenances, "Id", "MaintenanceRequest.Subject");
            model.Roles = Roles.GetRolesForUser();

            return View(model);
        }