Search in sources :

Example 26 with Partner

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);
}
Also used : Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList) Journal(com.axelor.apps.account.db.Journal) FixedAsset(com.axelor.apps.account.db.FixedAsset) Partner(com.axelor.apps.base.db.Partner) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 27 with Partner

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JpaSecurity(com.axelor.db.JpaSecurity) User(com.axelor.auth.db.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Filter(com.axelor.rpc.filter.Filter) Partner(com.axelor.apps.base.db.Partner)

Example 28 with Partner

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);
}
Also used : PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) UserRepository(com.axelor.auth.db.repo.UserRepository) User(com.axelor.auth.db.User) Partner(com.axelor.apps.base.db.Partner)

Example 29 with Partner

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;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) EmailAccount(com.axelor.apps.message.db.EmailAccount) User(com.axelor.auth.db.User) MessagingException(javax.mail.MessagingException) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MessagingException(javax.mail.MessagingException) MailException(com.axelor.mail.MailException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) MailService(com.axelor.mail.service.MailService) MimeMessage(javax.mail.internet.MimeMessage) Model(com.axelor.db.Model) MailException(com.axelor.mail.MailException) Partner(com.axelor.apps.base.db.Partner) File(java.io.File) MetaAttachment(com.axelor.meta.db.MetaAttachment)

Example 30 with Partner

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserRepository(com.axelor.auth.db.repo.UserRepository) MetaAttachment(com.axelor.meta.db.MetaAttachment) Templates(com.axelor.text.Templates) Filter(com.axelor.rpc.filter.Filter) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) MessagingException(javax.mail.MessagingException) StringTemplates(com.axelor.text.StringTemplates) Map(java.util.Map) MailBuilder(com.axelor.mail.MailBuilder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Path(java.nio.file.Path) MailServiceMessageImpl(com.axelor.apps.message.service.MailServiceMessageImpl) MailFollower(com.axelor.mail.db.MailFollower) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) StringUtils(com.axelor.common.StringUtils) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) List(java.util.List) MailException(com.axelor.mail.MailException) StringUtils.isBlank(com.axelor.common.StringUtils.isBlank) MailFollowerRepository(com.axelor.mail.db.repo.MailFollowerRepository) Partner(com.axelor.apps.base.db.Partner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MailAddress(com.axelor.mail.db.MailAddress) Joiner(com.google.common.base.Joiner) Query(com.axelor.db.Query) MailTemplateAssociation(com.axelor.apps.base.db.MailTemplateAssociation) EntityHelper(com.axelor.db.EntityHelper) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) MailSender(com.axelor.mail.MailSender) Singleton(javax.inject.Singleton) MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) MailService(com.axelor.mail.service.MailService) ArrayList(java.util.ArrayList) InternetAddress(javax.mail.internet.InternetAddress) Strings(com.google.common.base.Strings) GroovyTemplates(com.axelor.text.GroovyTemplates) Predicates(com.google.common.base.Predicates) MailMessage(com.axelor.mail.db.MailMessage) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) MetaFiles(com.axelor.meta.MetaFiles) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) JpaSecurity(com.axelor.db.JpaSecurity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TraceBackService(com.axelor.exception.service.TraceBackService) PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) IOException(java.io.IOException) MimeMessage(javax.mail.internet.MimeMessage) TemplateRepository(com.axelor.apps.message.db.repo.TemplateRepository) Maps(com.google.common.collect.Maps) Template(com.axelor.apps.message.db.Template) File(java.io.File) Beans(com.axelor.inject.Beans) EmailAccount(com.axelor.apps.message.db.EmailAccount) Preconditions(com.google.common.base.Preconditions) TemplateMessageService(com.axelor.apps.message.service.TemplateMessageService) User(com.axelor.auth.db.User) MailFollowerRepository(com.axelor.mail.db.repo.MailFollowerRepository) MailAddress(com.axelor.mail.db.MailAddress) User(com.axelor.auth.db.User) MailFollower(com.axelor.mail.db.MailFollower) Partner(com.axelor.apps.base.db.Partner)

Aggregations

Partner (com.axelor.apps.base.db.Partner)199 AxelorException (com.axelor.exception.AxelorException)68 Company (com.axelor.apps.base.db.Company)67 Transactional (com.google.inject.persist.Transactional)54 BigDecimal (java.math.BigDecimal)43 ArrayList (java.util.ArrayList)34 MoveLine (com.axelor.apps.account.db.MoveLine)32 Move (com.axelor.apps.account.db.Move)30 PaymentMode (com.axelor.apps.account.db.PaymentMode)26 LocalDate (java.time.LocalDate)26 Account (com.axelor.apps.account.db.Account)25 Invoice (com.axelor.apps.account.db.Invoice)24 BankDetails (com.axelor.apps.base.db.BankDetails)24 List (java.util.List)22 PartnerService (com.axelor.apps.base.service.PartnerService)18 Map (java.util.Map)18 Journal (com.axelor.apps.account.db.Journal)17 Address (com.axelor.apps.base.db.Address)14 Currency (com.axelor.apps.base.db.Currency)14 AccountConfig (com.axelor.apps.account.db.AccountConfig)13