package com.axelor.apps.contract.web;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;

import com.axelor.apps.account.db.TaxLine;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Partner;
import com.axelor.apps.base.db.Product;
import com.axelor.apps.base.service.tax.AccountManagementService;
import com.axelor.apps.contract.db.Contract;
import com.axelor.apps.contract.db.ContractLine;
import com.axelor.apps.contract.db.ContractVersion;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;

public class ContractLineController {
	
	public void changeProduct(ActionRequest request, ActionResponse response) {
		ContractLine contractLine = request.getContext().asType(ContractLine.class);
		Partner partner = null;
		Company company = null;
		if(request.getContext().getParentContext().getContextClass() == Contract.class){
			Contract contract = request.getContext().getParentContext().asType(Contract.class);
			partner = contract.getPartner();
			company = contract.getCompany();
		}else if (request.getContext().getParentContext().getContextClass() == ContractVersion.class){
			ContractVersion contractVersion = request.getContext().getParentContext().asType(ContractVersion.class);
			Contract contract = contractVersion.getContract();
			partner = contract.getPartner();
			company = contract.getCompany();
		}
		
		
		Product product = contractLine.getProduct();
		
		if(contractLine == null || product == null) {
			this.resetProductInformation(response);
			return;
		}

		try  {
			BigDecimal price = product.getSalePrice();
			if (partner != null && company != null){
				TaxLine taxLine = Beans.get(AccountManagementService.class).getTaxLine(
						LocalDate.now(), product, company, partner.getFiscalPosition(), false);
				response.setValue("taxLine", taxLine);

				
				if(taxLine != null && product.getInAti()) {
					price = price.divide(taxLine.getValue().add(BigDecimal.ONE), 2, BigDecimal.ROUND_HALF_UP);
				}
			}
			

			response.setValue("productName", product.getName());
			response.setValue("unit", product.getSalesUnit() == null ? product.getUnit() : product.getSalesUnit());
			response.setValue("price", price);

		}
		catch(Exception e)  {
			response.setFlash(e.getMessage());
			this.resetProductInformation(response);
		}
	}
	
	private void resetProductInformation(ActionResponse response) {
		response.setValue("taxLine", null);
		response.setValue("productName", null);
		response.setValue("unit", null);
		response.setValue("price", null);
		response.setValue("exTaxTotal", null);
		response.setValue("inTaxTotal", null);
	}

	public void compute(ActionRequest request, ActionResponse response) {
		ContractLine contractLine = request.getContext().asType(ContractLine.class);
		Product product = contractLine.getProduct();

		if(contractLine == null || product == null) {
			this.resetProductInformation(response);
			return;
		}

		BigDecimal taxRate = BigDecimal.ZERO;
		if(contractLine.getTaxLine() != null)  {  taxRate = contractLine.getTaxLine().getValue();  }
		BigDecimal exTaxTotal = contractLine.getQty().multiply(contractLine.getPrice()).setScale(2, RoundingMode.HALF_EVEN);
		BigDecimal inTaxTotal = exTaxTotal.add(exTaxTotal.multiply(taxRate));

		response.setValue("exTaxTotal", exTaxTotal);
		response.setValue("inTaxTotal", inTaxTotal);
	}
}

@@ -806,49 +806,155 @@
 ;%0A%09%09
-Partner partner = null;%0A%09%09Company company
+%0A%09%09if(contractLine == null %7C%7C contractLine.getProduct() == null) %7B%0A%09%09%09this.resetProductInformation(response);%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09Contract contract
  = n
@@ -1040,33 +1040,24 @@
 .class)%7B%0A%09%09%09
-Contract 
 contract = r
@@ -1123,80 +1123,8 @@
 s);%0A
-%09%09%09partner = contract.getPartner();%0A%09%09%09company = contract.getCompany();%0A
 %09%09%7De
@@ -1327,25 +1327,16 @@
 ss);%0A%09%09%09
-Contract 
 contract
@@ -1369,94 +1369,97 @@
 ract
-();%0A%09%09%09partner = contract.getPartner();%0A%09%09%09company = contract.getCompany
+Next() == null ? contractVersion.getContract() : contractVersion.getContractNext
 ()
+ 
 ;%0A%09%09%7D%0A
-%09%09%0A%09%09%0A
 %09%09Pr
@@ -1510,112 +1510,8 @@
 %09%0A%09%09
-if(contractLine == null %7C%7C product == null) %7B%0A%09%09%09this.resetProductInformation(response);%0A%09%09%09return;%0A%09%09%7D%0A
 %0A%09%09t
@@ -1573,34 +1573,16 @@
 if (
-partner != null && company
+contract
  != 
@@ -1702,22 +1702,50 @@
 , co
-mpany, p
+ntract.getCompany(), contract.getP
 artner
+()
 .get

fix product onChange in contractLine