//================================================================================ public Offhire AddOffhire(OffhireCommand command) { var vessel = vesselDomianService.Get(command.VesselInCompanyId); if (vessel == null) throw new ObjectNotFound("Vessel", command.VesselInCompanyId); var introducer = companyDomainService.Get(command.IntroducerId); if (introducer == null) throw new ObjectNotFound("Introducer", command.IntroducerId); Voyage voyage = null; if (command.VoyageId.HasValue) { voyage = voyageDomianService.Get(command.VoyageId.Value); if (voyage == null) throw new ObjectNotFound("Voyage", command.VoyageId.Value); } var offhireLocation = activityLocationDomainService.Get(command.OffhireLocationId); if (offhireLocation == null) throw new ObjectNotFound("OffhireLocation", command.OffhireLocationId); var voucherCurrency = currencyDomainService.Get(command.VoucherCurrencyId); if (voucherCurrency == null) throw new ObjectNotFound("VoucherCurrency", command.VoucherCurrencyId); var offhire = offhireFactory.CreateOffhire(command.ReferenceNumber, command.StartDateTime, command.EndDateTime, introducer, vessel, voyage, offhireLocation, command.VoucherDate, voucherCurrency); foreach (var commandDetail in command.OffhireDetails) { var good = goodDomainService.Get(commandDetail.GoodId); if (good == null) throw new ObjectNotFound("Good", commandDetail.GoodId); var unit = goodUnitDomainService.Get(commandDetail.UnitId); if (unit == null) throw new ObjectNotFound("Unit", commandDetail.UnitId); Tank tank = null; if (commandDetail.TankId.HasValue) { tank = tankDomainService.Get(commandDetail.TankId.Value); if (tank == null) throw new ObjectNotFound("Tank", commandDetail.TankId.Value); } if (!commandDetail.FeeInVoucherCurrency.HasValue) throw new InvalidArgument("FeeInVoucherCurrency For Good " + good.Name); if (!commandDetail.FeeInMainCurrency.HasValue) throw new InvalidArgument("FeeInMainCurrency For Good " + good.Name); var detail = offhireFactory.CreateOffhireDetail(commandDetail.Quantity, commandDetail.FeeInVoucherCurrency.Value, commandDetail.FeeInMainCurrency.Value, good, unit, tank, offhire); offhire.AddDetail(detail); } offhireRepository.Add(offhire); unitOfWorkScope.Commit(); return offhire; }
private OffhireCommand convertDtoToCommand(OffhireDto dto) { var command = new OffhireCommand() { Id = dto.Id, ReferenceNumber = dto.ReferenceNumber, OffhireLocationId = dto.OffhireLocation.Id, StartDateTime = dto.StartDateTime, EndDateTime = dto.EndDateTime, IntroducerId = dto.Introducer.Id, VesselInCompanyId = dto.Vessel.Id, VoyageId = dto.Voyage != null ? (long?)dto.Voyage.Id : null, VoucherCurrencyId = dto.VoucherCurrency == null ? -1 : dto.VoucherCurrency.Id, VoucherDate = dto.VoucherDate == null ? DateTime.MinValue : dto.VoucherDate.Value, OffhireDetails = dto.OffhireDetails.Select(od => new OffhireCommandDetail() { Id = od.Id, GoodId = od.Good.Id, Quantity = od.Quantity, UnitId = od.Unit.Id, TankId = od.Tank != null ? (long?)od.Tank.Id : null, FeeInMainCurrency = od.FeeInMainCurrency, FeeInVoucherCurrency = od.FeeInVoucherCurrency }).ToList() }; return command; }
//================================================================================ public Offhire UpdateOffhire(OffhireCommand command) { var voucherCurrency = currencyDomainService.Get(command.VoucherCurrencyId); if (voucherCurrency == null) throw new ObjectNotFound("VoucherCurrency", command.VoucherCurrencyId); var offhireToUpdate = offhireDomainService.Get(command.Id); offhireToUpdate.Update(command.VoucherDate, voucherCurrency, currencyDomainService); //offhireRepository.Update(offhireToUpdate); //unitOfWorkScope.Commit(); foreach (var commandDetail in command.OffhireDetails) { if (!commandDetail.FeeInVoucherCurrency.HasValue) throw new InvalidArgument("FeeInVoucherCurrency"); if (!commandDetail.FeeInMainCurrency.HasValue) throw new InvalidArgument("FeeInMainCurrency"); offhireToUpdate.UpdateDetail(commandDetail.Id, commandDetail.FeeInVoucherCurrency.Value, commandDetail.FeeInMainCurrency.Value, offhireDomainService, tankDomainService, currencyDomainService, goodDomainService, goodUnitDomainService); } try { unitOfWorkScope.Commit(); } catch (OptimisticConcurrencyException ex) { throw new ConcurencyException("UpdateOffhire"); } return offhireToUpdate; }