package com.axelor.apps.account.web;

import com.axelor.apps.account.db.Invoice;
import com.axelor.apps.account.db.InvoiceTemplate;
import com.axelor.apps.account.db.repo.InvoiceTemplateRepository;
import com.axelor.apps.account.service.InvoiceTemplateService;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.axelor.meta.schema.actions.ActionView;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.axelor.rpc.Context;
import java.time.LocalDate;

public class InvoiceTemplateControlller {

  public void showInvoice2(ActionRequest request, ActionResponse response) throws AxelorException {

    Context context = request.getContext();

    InvoiceTemplate invoiceTemplate =
        Beans.get(InvoiceTemplateRepository.class)
            .find(Long.parseLong(context.get("id").toString()));

    Invoice invoice =
        Beans.get(InvoiceTemplateService.class).generateInvoiceFromTemplate(invoiceTemplate);

    System.out.println("*********************Invoice***********************");
    System.out.println(invoice);
    System.out.println("*********************Invoice***********************");

    response.setView(
        ActionView.define("Invoice")
            .model(Invoice.class.getName())
            .add("grid", "invoice-grid")
            .add("form", "invoice-form")
            .param("forceEdit", "true")
            .context("_showRecord", String.valueOf(invoice.getId()))
            .context("_operationTypeSelect", invoice.getOperationTypeSelect())
            .context("todayDate", LocalDate.now())
            .map());
  }
}
