Search in sources :

Example 6 with InvoicingProject

use of com.axelor.apps.businessproject.db.InvoicingProject in project axelor-open-suite by axelor.

the class InvoicingProjectService method generateInvoicingProject.

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public InvoicingProject generateInvoicingProject(Project project, int consolidatePhaseSelect) {
    if (project == null) {
        return null;
    }
    InvoicingProject invoicingProject = new InvoicingProject();
    invoicingProject.setProject(project);
    if (consolidatePhaseSelect == ProjectInvoicingAssistantBatchRepository.CONSOLIDATE_PHASE_CONSOLIDATE_ALL) {
        invoicingProject.setConsolidatePhaseWhenInvoicing(true);
    } else if (consolidatePhaseSelect == ProjectInvoicingAssistantBatchRepository.CONSOLIDATE_PHASE_DONT_CONSOLIDATE) {
        invoicingProject.setConsolidatePhaseWhenInvoicing(false);
    } else if (consolidatePhaseSelect == ProjectInvoicingAssistantBatchRepository.CONSOLIDATE_PHASE_DEFAULT_VALUE) {
        invoicingProject.setConsolidatePhaseWhenInvoicing(invoicingProject.getProject().getConsolidatePhaseWhenInvoicing());
    }
    clearLines(invoicingProject);
    setLines(invoicingProject, project, 0);
    if (invoicingProject.getSaleOrderLineSet().isEmpty() && invoicingProject.getPurchaseOrderLineSet().isEmpty() && invoicingProject.getLogTimesSet().isEmpty() && invoicingProject.getExpenseLineSet().isEmpty() && invoicingProject.getProjectSet().isEmpty() && invoicingProject.getProjectTaskSet().isEmpty()) {
        return invoicingProject;
    }
    return invoicingProjectRepo.save(invoicingProject);
}
Also used : InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject) Transactional(com.google.inject.persist.Transactional)

Example 7 with InvoicingProject

use of com.axelor.apps.businessproject.db.InvoicingProject in project axelor-open-suite by axelor.

the class InvoicingProjectService method generateInvoice.

@Transactional(rollbackOn = { Exception.class })
public Invoice generateInvoice(InvoicingProject invoicingProject) throws AxelorException {
    Project project = invoicingProject.getProject();
    Partner customer = project.getClientPartner();
    Partner customerContact = project.getContactPartner();
    if (invoicingProject.getSaleOrderLineSet().isEmpty() && invoicingProject.getPurchaseOrderLineSet().isEmpty() && invoicingProject.getLogTimesSet().isEmpty() && invoicingProject.getExpenseLineSet().isEmpty() && invoicingProject.getProjectSet().isEmpty() && invoicingProject.getProjectTaskSet().isEmpty()) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_EMPTY));
    }
    if (invoicingProject.getProject() == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT));
    }
    if (invoicingProject.getProject().getClientPartner() == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT_PARTNER));
    }
    if (customerContact == null && customer.getContactPartnerSet().size() == 1) {
        customerContact = customer.getContactPartnerSet().iterator().next();
    }
    Company company = this.getRootCompany(project);
    if (company == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT_COMPANY));
    }
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_CLIENT_SALE, company, customer.getPaymentCondition(), customer.getInPaymentMode(), partnerService.getInvoicingAddress(customer), customer, customerContact, customer.getCurrency(), Beans.get(PartnerPriceListService.class).getDefaultPriceList(customer, PriceListRepository.TYPE_SALE), null, null, null, null, null, null) {

        @Override
        public Invoice generate() throws AxelorException {
            Invoice invoice = super.createInvoiceHeader();
            invoice.setProject(project);
            invoice.setPriceList(project.getPriceList());
            return invoice;
        }
    };
    Invoice invoice = invoiceGenerator.generate();
    AccountConfigService accountConfigService = Beans.get(AccountConfigService.class);
    AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
    invoice.setDisplayTimesheetOnPrinting(accountConfig.getDisplayTimesheetOnPrinting());
    invoice.setDisplayExpenseOnPrinting(accountConfig.getDisplayExpenseOnPrinting());
    invoiceGenerator.populate(invoice, this.populate(invoice, invoicingProject));
    Beans.get(InvoiceRepository.class).save(invoice);
    invoicingProject.setInvoice(invoice);
    invoicingProject.setStatusSelect(InvoicingProjectRepository.STATUS_GENERATED);
    invoicingProjectRepo.save(invoicingProject);
    return invoice;
}
Also used : InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject) Project(com.axelor.apps.project.db.Project) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) AccountConfigService(com.axelor.apps.account.service.config.AccountConfigService) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Partner(com.axelor.apps.base.db.Partner) AccountConfig(com.axelor.apps.account.db.AccountConfig) Transactional(com.google.inject.persist.Transactional)

Example 8 with InvoicingProject

use of com.axelor.apps.businessproject.db.InvoicingProject in project axelor-open-suite by axelor.

the class InvoiceProjectRepository method remove.

@Override
public void remove(Invoice entity) {
    if (Beans.get(AppBusinessProjectService.class).isApp("business-project")) {
        List<InvoicingProject> invoiceProjectList = Beans.get(InvoicingProjectRepository.class).all().filter("self.invoice.id = ?", entity.getId()).fetch();
        List<ProjectTask> projectTaskList = Beans.get(ProjectTaskRepository.class).all().filter("self.invoiceLine.invoice = ?1", entity).fetch();
        if (ObjectUtils.notEmpty(projectTaskList)) {
            for (ProjectTask projectTask : projectTaskList) {
                projectTask.setInvoiceLine(null);
            }
        }
        for (InvoicingProject invoiceProject : invoiceProjectList) {
            invoiceProject.setInvoice(null);
            invoiceProject.setStatusSelect(InvoicingProjectRepository.STATUS_DRAFT);
        }
    }
    super.remove(entity);
}
Also used : AppBusinessProjectService(com.axelor.apps.businessproject.service.app.AppBusinessProjectService) ProjectTaskRepository(com.axelor.apps.project.db.repo.ProjectTaskRepository) ProjectTask(com.axelor.apps.project.db.ProjectTask) InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject)

Example 9 with InvoicingProject

use of com.axelor.apps.businessproject.db.InvoicingProject in project axelor-open-suite by axelor.

the class BatchInvoicingProjectService method process.

@Override
protected void process() {
    Map<String, Object> contextValues = null;
    try {
        contextValues = ProjectInvoicingAssistantBatchService.createJsonContext(batch);
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
    List<Object> generatedInvoicingProjectList = new ArrayList<Object>();
    List<Project> projectList = projectRepo.all().filter("self.isBusinessProject = :isBusinessProject " + "AND self.toInvoice = :toInvoice AND " + "(self.statusSelect != :statusCanceled AND self.statusSelect != :statusFinished)").bind("isBusinessProject", true).bind("toInvoice", true).bind("statusCanceled", ProjectRepository.STATE_CANCELED).bind("statusFinished", ProjectRepository.STATE_FINISHED).fetch();
    for (Project project : projectList) {
        try {
            InvoicingProject invoicingProject = invoicingProjectService.generateInvoicingProject(project, batch.getProjectInvoicingAssistantBatch().getConsolidatePhaseSelect());
            if (invoicingProject != null && invoicingProject.getId() != null) {
                incrementDone();
                if (batch.getProjectInvoicingAssistantBatch().getActionSelect() == ProjectInvoicingAssistantBatchRepository.ACTION_GENERATE_INVOICING_PROJECT) {
                    invoicingProject.setDeadlineDate(batch.getProjectInvoicingAssistantBatch().getDeadlineDate());
                }
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", invoicingProject.getId());
                generatedInvoicingProjectList.add(map);
            }
        } catch (Exception e) {
            incrementAnomaly();
            TraceBackService.trace(new Exception(String.format(I18n.get(IExceptionMessage.BATCH_INVOICING_PROJECT_1), project.getId()), e), ExceptionOriginRepository.INVOICE_ORIGIN, batch.getId());
        }
    }
    ProjectInvoicingAssistantBatchService.updateJsonObject(batch, generatedInvoicingProjectList, "generatedInvoicingProjectSet", contextValues);
}
Also used : InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject) Project(com.axelor.apps.project.db.Project) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject)

Example 10 with InvoicingProject

use of com.axelor.apps.businessproject.db.InvoicingProject in project axelor-open-suite by axelor.

the class InvoicingProjectController method generateInvoice.

@SuppressWarnings("unchecked")
public void generateInvoice(ActionRequest request, ActionResponse response) throws AxelorException {
    Invoice invoice;
    List<Long> invoiceIdList = new ArrayList<Long>();
    List<InvoicingProject> projects = new ArrayList<InvoicingProject>();
    String ids = null;
    if (request.getContext().get("_ids") != null) {
        projects = invoicingProjectRepository.all().filter("self.id in ? and self.invoice = null", (List<Integer>) request.getContext().get("_ids")).fetch();
    } else if (request.getContext().asType(InvoicingProject.class).getId() != null) {
        projects.add(invoicingProjectRepository.find(request.getContext().asType(InvoicingProject.class).getId()));
    } else {
        response.setError(IExceptionMessage.LINES_NOT_SELECTED);
        return;
    }
    if (projects.size() > 0) {
        for (InvoicingProject invProject : projects) {
            invoice = Beans.get(InvoicingProjectService.class).generateInvoice(invProject);
            if (invoice != null) {
                invoiceIdList.add(invoice.getId());
                try {
                    Beans.get(InvoicingProjectService.class).generateAnnex(invProject);
                } catch (IOException e) {
                    TraceBackService.trace(response, e);
                }
            }
        }
        ids = StringUtils.join(invoiceIdList, ",");
        ActionViewBuilder view = ActionView.define(I18n.get("Invoice")).model(Invoice.class.getName()).add("grid", "invoice-grid").add("form", "invoice-form").param("search-filters", "customer-invoices-filters");
        response.setReload(true);
        response.setView((ids.contains(",")) ? view.domain("self.id IN (" + ids + ")").map() : view.context("_showRecord", ids).map());
    }
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvoicingProjectService(com.axelor.apps.businessproject.service.InvoicingProjectService) InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)

Aggregations

InvoicingProject (com.axelor.apps.businessproject.db.InvoicingProject)12 Project (com.axelor.apps.project.db.Project)6 Transactional (com.google.inject.persist.Transactional)6 Invoice (com.axelor.apps.account.db.Invoice)3 ProjectTask (com.axelor.apps.project.db.ProjectTask)3 InvoicingProjectService (com.axelor.apps.businessproject.service.InvoicingProjectService)2 ProjectRepository (com.axelor.apps.project.db.repo.ProjectRepository)2 AxelorException (com.axelor.exception.AxelorException)2 ArrayList (java.util.ArrayList)2 AccountConfig (com.axelor.apps.account.db.AccountConfig)1 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)1 AccountConfigService (com.axelor.apps.account.service.config.AccountConfigService)1 InvoiceGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)1 Company (com.axelor.apps.base.db.Company)1 Partner (com.axelor.apps.base.db.Partner)1 InvoicingProjectRepository (com.axelor.apps.businessproject.db.repo.InvoicingProjectRepository)1 AppBusinessProjectService (com.axelor.apps.businessproject.service.app.AppBusinessProjectService)1 ExpenseLine (com.axelor.apps.hr.db.ExpenseLine)1 TimesheetLine (com.axelor.apps.hr.db.TimesheetLine)1 AppProductionService (com.axelor.apps.production.service.app.AppProductionService)1