use of com.axelor.apps.base.db.Partner in project axelor-open-suite by axelor.
the class FixedAssetLineMoveServiceImpl method generateDisposalMove.
@Override
@Transactional(rollbackOn = { Exception.class })
public void generateDisposalMove(FixedAssetLine fixedAssetLine) throws AxelorException {
FixedAsset fixedAsset = fixedAssetLine.getFixedAsset();
Journal journal = fixedAsset.getJournal();
Company company = fixedAsset.getCompany();
Partner partner = fixedAsset.getPartner();
LocalDate date = fixedAssetLine.getDepreciationDate();
// Creating move
Move move = moveCreateService.createMove(journal, company, company.getCurrency(), partner, date, null, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_FIXED_ASSET);
if (move != null) {
List<MoveLine> moveLines = new ArrayList<MoveLine>();
String origin = fixedAsset.getReference();
Account chargeAccount = fixedAsset.getFixedAssetCategory().getChargeAccount();
Account depreciationAccount = fixedAsset.getFixedAssetCategory().getDepreciationAccount();
Account purchaseAccount = fixedAsset.getPurchaseAccount();
BigDecimal chargeAmount = fixedAssetLine.getResidualValue();
BigDecimal cumulativeDepreciationAmount = fixedAssetLine.getCumulativeDepreciation();
// Creating accounting debit move line for charge account
MoveLine chargeAccountDebitMoveLine = new MoveLine(move, partner, chargeAccount, date, null, 1, chargeAmount, BigDecimal.ZERO, fixedAsset.getName(), origin, null, BigDecimal.ZERO, date);
moveLines.add(chargeAccountDebitMoveLine);
this.addAnalyticToMoveLine(fixedAsset.getAnalyticDistributionTemplate(), chargeAccountDebitMoveLine);
// Creating accounting debit move line for deprecation account
MoveLine deprecationAccountDebitMoveLine = new MoveLine(move, partner, depreciationAccount, date, null, 1, cumulativeDepreciationAmount, BigDecimal.ZERO, fixedAsset.getName(), origin, null, BigDecimal.ZERO, date);
moveLines.add(deprecationAccountDebitMoveLine);
this.addAnalyticToMoveLine(fixedAsset.getAnalyticDistributionTemplate(), deprecationAccountDebitMoveLine);
// Creating accounting credit move line
MoveLine creditMoveLine = new MoveLine(move, partner, purchaseAccount, date, null, 2, BigDecimal.ZERO, fixedAsset.getGrossValue(), fixedAsset.getName(), origin, null, BigDecimal.ZERO, date);
moveLines.add(creditMoveLine);
this.addAnalyticToMoveLine(fixedAsset.getAnalyticDistributionTemplate(), creditMoveLine);
move.getMoveLineList().addAll(moveLines);
}
moveRepo.save(move);
fixedAsset.setDisposalMove(move);
}
use of com.axelor.apps.base.db.Partner in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method findEmails.
@Override
public List<InternetAddress> findEmails(String matching, List<String> selected, int maxResult) {
JpaSecurity jpaSecurity = Beans.get(JpaSecurity.class);
// Users
Filter userPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, User.class);
List<String> selectedWithoutNull = new ArrayList<String>(selected);
for (int i = 0; i < selected.size(); i++) {
if (Strings.isNullOrEmpty(selected.get(i)))
selectedWithoutNull.remove(i);
}
final List<String> where = new ArrayList<>();
final Map<String, Object> params = new HashMap<>();
if (userPermissionFilter != null) {
where.add(userPermissionFilter.getQuery());
}
where.add("((self.partner is not null AND self.partner.emailAddress is not null) OR (self.email is not null))");
if (!isBlank(matching)) {
where.add("(LOWER(self.partner.emailAddress.address) like LOWER(:email) OR LOWER(self.partner.fullName) like LOWER(:email) OR LOWER(self.email) like LOWER(:email) OR LOWER(self.name) like LOWER(:email))");
params.put("email", "%" + matching + "%");
}
if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
where.add("self.partner.emailAddress.address not in (:selected)");
params.put("selected", selectedWithoutNull);
}
final String filter = Joiner.on(" AND ").join(where);
final Query<User> query = Query.of(User.class);
if (!isBlank(filter)) {
if (userPermissionFilter != null) {
query.filter(filter, userPermissionFilter.getParams());
} else {
query.filter(filter);
}
query.bind(params);
}
final List<InternetAddress> addresses = new ArrayList<>();
if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, User.class)) {
for (User user : query.fetch(maxResult)) {
try {
if (user.getPartner() != null && user.getPartner().getEmailAddress() != null && !Strings.isNullOrEmpty(user.getPartner().getEmailAddress().getAddress())) {
final InternetAddress item = new InternetAddress(user.getPartner().getEmailAddress().getAddress(), user.getFullName());
addresses.add(item);
selectedWithoutNull.add(user.getPartner().getEmailAddress().getAddress());
} else if (!Strings.isNullOrEmpty(user.getEmail())) {
final InternetAddress item = new InternetAddress(user.getEmail(), user.getFullName());
addresses.add(item);
selectedWithoutNull.add(user.getEmail());
}
} catch (UnsupportedEncodingException e) {
TraceBackService.trace(e);
}
}
}
// Partners
Filter partnerPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, Partner.class);
final List<String> where2 = new ArrayList<>();
final Map<String, Object> params2 = new HashMap<>();
if (partnerPermissionFilter != null) {
where2.add(partnerPermissionFilter.getQuery());
}
where2.add("self.emailAddress is not null");
if (!isBlank(matching)) {
where2.add("(LOWER(self.emailAddress.address) like LOWER(:email) OR LOWER(self.fullName) like LOWER(:email))");
params2.put("email", "%" + matching + "%");
}
if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
where2.add("self.emailAddress.address not in (:selected)");
params2.put("selected", selectedWithoutNull);
}
final String filter2 = Joiner.on(" AND ").join(where2);
final Query<Partner> query2 = Query.of(Partner.class);
if (!isBlank(filter2)) {
if (partnerPermissionFilter != null) {
query2.filter(filter2, partnerPermissionFilter.getParams());
} else {
query2.filter(filter2);
}
query2.bind(params2);
}
if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, Partner.class)) {
for (Partner partner : query2.fetch(maxResult)) {
try {
if (partner.getEmailAddress() != null && !Strings.isNullOrEmpty(partner.getEmailAddress().getAddress())) {
final InternetAddress item = new InternetAddress(partner.getEmailAddress().getAddress(), partner.getFullName());
addresses.add(item);
}
} catch (UnsupportedEncodingException e) {
TraceBackService.trace(e);
}
}
}
return addresses;
}
use of com.axelor.apps.base.db.Partner in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method resolve.
@Override
public Model resolve(String email) {
final UserRepository users = Beans.get(UserRepository.class);
final User user = users.all().filter("self.partner.emailAddress.address = ?1", email).fetchOne();
if (user != null) {
return user;
}
final PartnerRepository partners = Beans.get(PartnerRepository.class);
final Partner partner = partners.all().filter("self.emailAddress.address = ?1", email).fetchOne();
if (partner != null) {
return partner;
}
return super.resolve(email);
}
use of com.axelor.apps.base.db.Partner in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method send.
@Override
public void send(final MailMessage message) throws MailException {
if (!appBaseService.isApp("base") || !appBaseService.getAppBase().getActivateSendingEmail()) {
return;
}
final EmailAccount emailAccount = mailAccountService.getDefaultSender();
if (emailAccount == null) {
super.send(message);
return;
}
Preconditions.checkNotNull(message, "mail message can't be null");
final Model related = findEntity(message);
final MailSender sender = getMailSender(emailAccount);
final Set<String> recipients = recipients(message, related);
if (recipients.isEmpty()) {
return;
}
final MailMessageRepository messages = Beans.get(MailMessageRepository.class);
for (String recipient : recipients) {
MailBuilder builder = sender.compose().subject(getSubject(message, related));
this.setRecipients(builder, recipient, related);
Model obj = Beans.get(MailService.class).resolve(recipient);
userName = null;
if (obj != null) {
Class<Model> klass = EntityHelper.getEntityClass(obj);
if (klass.equals(User.class)) {
User user = (User) obj;
userName = user.getName();
} else if (klass.equals(Partner.class)) {
Partner partner = (Partner) obj;
userName = partner.getSimpleFullName();
}
}
for (MetaAttachment attachment : messages.findAttachments(message)) {
final Path filePath = MetaFiles.getPath(attachment.getMetaFile());
final File file = filePath.toFile();
builder.attach(file.getName(), file.toString());
}
MimeMessage email;
try {
builder.html(template(message, related));
email = builder.build(message.getMessageId());
final Set<String> references = new LinkedHashSet<>();
if (message.getParent() != null) {
references.add(message.getParent().getMessageId());
}
if (message.getRoot() != null) {
references.add(message.getRoot().getMessageId());
}
if (!references.isEmpty()) {
email.setHeader("References", Joiner.on(" ").skipNulls().join(references));
}
} catch (MessagingException | IOException e) {
throw new MailException(e);
}
// send email using a separate process to void thread blocking
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
send(sender, email);
return true;
}
});
}
}
use of com.axelor.apps.base.db.Partner in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method recipients.
@Override
protected Set<String> recipients(MailMessage message, Model entity) {
final Set<String> recipients = new LinkedHashSet<>();
final MailFollowerRepository followers = Beans.get(MailFollowerRepository.class);
String entityName = entity.getClass().getName();
if (message.getRecipients() != null) {
for (MailAddress address : message.getRecipients()) {
recipients.add(address.getAddress());
}
}
for (MailFollower follower : followers.findAll(message)) {
if (follower.getArchived()) {
continue;
}
User user = follower.getUser();
if (user != null) {
if (!(user.getReceiveEmails() && user.getFollowedMetaModelSet().stream().anyMatch(x -> x.getFullName().equals(entityName)))) {
continue;
} else {
Partner partner = user.getPartner();
if (partner != null && partner.getEmailAddress() != null) {
recipients.add(partner.getEmailAddress().getAddress());
} else if (user.getEmail() != null) {
recipients.add(user.getEmail());
}
}
} else {
if (follower.getEmail() != null) {
recipients.add(follower.getEmail().getAddress());
} else {
log.info("No email address found for follower : " + follower);
}
}
}
return Sets.filter(recipients, Predicates.notNull());
}
Aggregations