/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2014 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.stock.web;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axelor.apps.ReportSettings;
import com.axelor.apps.base.db.Product;
import com.axelor.apps.base.service.ProductServiceImpl;
import com.axelor.apps.stock.service.InventoryService;
import com.axelor.apps.stock.service.LocationLineService;
import com.axelor.apps.stock.db.Inventory;
import com.axelor.apps.stock.db.InventoryLine;
import com.axelor.apps.stock.db.Location;
import com.axelor.apps.stock.db.LocationLine;
import com.axelor.apps.stock.report.IReport;
import com.axelor.apps.tool.net.URLService;
import com.axelor.auth.AuthUtils;
import com.axelor.auth.db.User;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.axelor.rpc.Context;
import com.google.inject.Inject;

public class InventoryController {

	@Inject
	InventoryService inventoryService;
	
	private static final Logger LOG = LoggerFactory.getLogger(InventoryController.class);
	
	/**
	 * Fonction appeler par le bouton imprimer
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	public void showInventory(ActionRequest request, ActionResponse response) {

		Inventory inventory = request.getContext().asType(Inventory.class);
		int format = inventory.getFormatSelect();

		StringBuilder url = new StringBuilder();

		User user = AuthUtils.getUser();
		String language = user != null? (user.getLanguage() == null || user.getLanguage().equals(""))? "en" : user.getLanguage() : "en"; 

		url.append(
				new ReportSettings(IReport.INVENTORY, ((format == 1) ? "pdf":(format == 2) ? "xls":"pdf"))
				.addParam("Locale", language)
				.addParam("InventoryId", inventory.getId().toString())
				.getUrl());
		
		LOG.debug("URL : {}", url);
		String urlNotExist = URLService.notExist(url.toString());
		if (urlNotExist == null){
			
			Map<String,Object> mapView = new HashMap<String,Object>();
			mapView.put("title", "Inventory "+inventory.getInventorySeq());
			mapView.put("resource", url);
			mapView.put("viewType", "html");
			response.setView(mapView);
		}
		else {
			response.setFlash(urlNotExist);
		}
	}
	
	public void importFile(ActionRequest request, ActionResponse response) throws IOException, AxelorException {
		
		Inventory inventory = request.getContext().asType(Inventory.class);
		String filePath = inventory.getImportFilePath();
		char separator = ',';
		
		inventoryService.importFile(filePath, separator, inventory);
		response.setFlash("File "+filePath+" successfully imported.");
	}
	
	public void generateStockMove(ActionRequest request, ActionResponse response) throws AxelorException {
		
		Inventory inventory = request.getContext().asType(Inventory.class);
		inventoryService.generateStockMove(inventory);
	}
	
	public void fillInventoryLineList(ActionRequest request, ActionResponse response) throws AxelorException {
		
		Inventory inventory = request.getContext().asType(Inventory.class);
		if(inventory != null) {
			List<InventoryLine> inventoryLineList = inventoryService.fillInventoryLineList(inventory);
			if(inventoryLineList == null)  {
				response.setFlash("Il n'y a aucun produit contenu dans l'emplacement de stock.");
			}
			else {
				if(inventoryLineList.size() > 0) {
					response.setFlash("La liste des lignes d'inventaire a été rempli.");
				}
				else  {
					response.setFlash("Aucune lignes d'inventaire n'a été créée.");
				}
			}
		}
		response.setReload(true);
	}
	
	
	public void setInventorySequence(ActionRequest request, ActionResponse response) throws AxelorException {
		
		Inventory inventory = request.getContext().asType(Inventory.class);
		
		if(inventory.getInventorySeq() ==  null) {
			
			Location location = inventory.getLocation();
			
			response.setValue("inventorySeq", inventoryService.getInventorySequence(location.getCompany()));
		}
	}
	
	public BigDecimal getCurrentQty(Location location, Product product){
		
		LocationLine locationLine = Beans.get(LocationLineService.class).getLocationLine(location, product);
		
		if(locationLine != null ){
			LOG.debug("Current qty found: {}",locationLine.getCurrentQty());
			return locationLine.getCurrentQty();
		}
		
		return BigDecimal.ZERO;
	}
	
	
}

  
@@ -4807,16 +4807,60 @@
 ct)%7B%0A%09%09%0A
+%09%09if(location != null && product != null)%7B%0A%09
 %09%09Locati
@@ -4952,26 +4952,24 @@
 product);%0A%09%09
-%0A%09
 %09if(location
@@ -4984,16 +4984,17 @@
 null )%7B%0A
+%09
 %09%09%09LOG.d
@@ -5056,16 +5056,17 @@
 ());%0A%09%09%09
+%09
 return l
@@ -5096,20 +5096,22 @@
 ty();%0A%09%09
+%09
 %7D%0A%09%09
+%7D
 %0A%09%09retur

Fix issue of onchange on product in inventory line