<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://axelor.com/xml/ns/object-views http://axelor.com/xml/ns/object-views/object-views_5.2.xsd">
	  
	<chart onInit="action.base.default.month.value" name="chart.stockMove.nbr.delivery.by.country" title="Customer deliveries by country">
		<search-fields>
			<field type="string" name="monthSelect" title="Historical Period" selection="account.year.period.duration.select" x-required="true"/>
		</search-fields>
		<dataset type="jpql">
			SELECT
				COUNT(self.id) AS _moves,
				country.alpha3Code AS _country
			FROM
				StockMove self,    
				StockLocation stockLocation,    
				Address address,
				Country country      
			WHERE
				self.typeSelect = 2 AND
				self.statusSelect = 3 AND
				self.company =:activeCompany AND    
				self.toStockLocation = stockLocation AND
				stockLocation.typeSelect = 2 AND
				self.toAddress = address AND
				address.addressL7Country = country AND
				self.realDate BETWEEN DATE(:todayDate) - (CAST(:monthSelect as int)) * 30 AND DATE(:todayDate)
			GROUP BY
				country.alpha3Code   
		</dataset>
		<category key="_country" type="text" title="Country"/>
		<series key="_moves" type="bar" title="Stock Moves"/>
	</chart>	
	
	<chart onInit="action.base.default.month.value" name="chart.stockMoveLine.product.delivery.by.country" title="Product deliveries in qty by country">
		<search-fields>
			<field type="string" name="monthSelect" title="Historical Period" selection="account.year.period.duration.select" x-required="true"/>
		</search-fields>
		<dataset type="jpql">
			SELECT
				SUM(self.realQty) AS _qty,
				country.alpha3Code AS _country
			FROM
				StockMoveLine self,    
				StockMove stockMove,
				StockLocation stockLocation,
				Address address,
				Country country
			WHERE
				stockMove.typeSelect = 2 AND
				stockMove.statusSelect = 3 AND
				stockMove.company = :activeCompany AND
				stockMove.realDate BETWEEN DATE(:todayDate) - (CAST(:monthSelect as int)) * 30 AND DATE(:todayDate) AND
				self.stockMove = stockMove AND
				stockMove.toStockLocation = stockLocation AND
				stockLocation.typeSelect = 2 AND
				stockMove.toAddress = address AND
				address.addressL7Country = country
			GROUP BY
				country.alpha3Code    
		</dataset>
		<category key="_country" type="text" title="Country"/>
		<series key="_qty" type="bar" title="Quantity"/>
	</chart>

	<chart onInit="action.base.default.month.value" name="chart.stockMoveLine.product.delivery.by.country.by.family" 
	       title="Product deliveries in qty by country by accounting family" stacked="true">
		<search-fields>
			<field type="string" name="monthSelect" title="Historical Period" selection="account.year.period.duration.select" x-required="true"/>
		</search-fields>
		<dataset type="jpql">
			SELECT
				SUM(self.realQty) AS _qty,
				country.alpha3Code AS _country,
				productFamily.name AS _family
			FROM
				StockMoveLine self,    
				StockMove stockMove,
				StockLocation stockLocation,
				Address address,
				Country country,
				ProductFamily productFamily
			WHERE
				stockMove.typeSelect = 2 AND
				stockMove.statusSelect = 3 AND
				stockMove.company = :activeCompany AND
				stockMove.realDate BETWEEN DATE(:todayDate) - (CAST(:monthSelect as int)) * 30 AND DATE(:todayDate) AND
				self.stockMove = stockMove AND
				stockMove.toStockLocation = stockLocation AND
				stockLocation.typeSelect = 2 AND
				stockMove.toAddress = address AND
				address.addressL7Country = country AND
				self.product.productFamily = productFamily
			GROUP BY
				country.alpha3Code, productFamily.name    
		</dataset>
		<category key="_country" type="text" title="Country"/>
		<series key="_qty" type="bar" title="Quantity" groupBy="_family"/>
	</chart>

	<chart onInit="action.base.default.month.value" name="chart.stockMove.deliveries.time.delay.in.nbr" 
	       title="Customer deliveries statistics" >
		<search-fields>
			<field type="string" name="monthSelect" title="Historical Period" selection="account.year.period.duration.select" x-required="true"/>
		</search-fields>
		<dataset type="sql">
			SELECT
				COUNT(self.id) AS moves,
				(CASE 
				      when self.estimated_date >= self.real_date then 'OnTime Deliveries'
				      when self.estimated_date &lt; self.real_date then 'Late Deliveries'                        
				END) AS status    
			FROM
 				stock_stock_move AS self
			JOIN
				auth_user AS _user
				ON _user.id = :__user__
				AND self.company = _user.active_company      
			JOIN
				stock_stock_location AS _location
				ON _location.type_select = 2
				AND self.to_stock_location = _location.id
			WHERE
				self.type_select = 2 AND
				self.status_select = 3 AND                 
				self.real_date BETWEEN DATE(:todayDate) - (CAST(:monthSelect as int)) * INTERVAL '1 month' AND DATE(:todayDate)    
			GROUP BY
				status    
		</dataset>
		<category key="status" type="text" title="Status"/>
		<series key="moves" type="donut" title="Stock Moves" />
	</chart>

	<chart onInit="action.base.default.month.value" name="chart.stockMove.average.delivery.time.delay" 
	       title="Customer average delivery delay" >
		<search-fields>	    
			<field type="string" name="monthSelect" title="Historical Period" selection="account.year.period.duration.select" x-required="true"/>
		</search-fields>
		<dataset type="sql">    
			SELECT
				ROUND(AVG((self.estimated_date - self.real_date)*-1)) AS moves,
				'Average Delay' AS label
			FROM
				stock_stock_move AS self
			JOIN
				auth_user AS _user
				ON _user.id = :__user__ 
				AND self.company = _user.active_company
			JOIN
				stock_stock_location AS _location
				ON self.to_stock_location = _location.id
				AND _location.type_select = 2
			WHERE
				self.type_select = 2 AND
				self.status_select = 3 AND
				self.real_date BETWEEN DATE(:todayDate) - (CAST(:monthSelect as int)) * INTERVAL '1 month' AND DATE(:todayDate)
			GROUP BY
				label    
		</dataset>
		<category key="label" type="text" title="Delay"/>
		<series key="moves" type="bar" title="Stock Moves"/>
	</chart>
	  
	<chart name="chart.stock.report.for.product" title="Stock" onInit="action-product-set-today-date">
	    <search-fields>
		    <field name="todayDate" type="date" title="Today" x-required="true"/>
	  	</search-fields>
		<dataset type="sql">
	    <![CDATA[
  			SELECT estimatedate,locat,SUM(qty) OVER (PARTITION BY locat ORDER BY locat,estimatedate) AS qty 
			FROM (
				(SELECT DATE(:todayDate) AS estimatedate,location.name AS locat,
	               		(SELECT SUM(CASE WHEN move.from_stock_location = loc.id THEN -1*qty
	                           WHEN move.to_stock_location = loc.id THEN qty
	                           END) AS qty                                                                 
               			FROM 	stock_stock_move_line line,
	                    		stock_stock_move move,
	                    		stock_stock_location loc 
	               		WHERE line.stock_move = move.id 
							AND move.real_date <= DATE(:todayDate)
							AND move.status_select in (2,3)
							AND line.product = :id
							AND loc.id = location.id
							AND (move.from_stock_location = loc.id OR move.to_stock_location = loc.id)
							AND loc.type_select = 1) AS qty
				FROM stock_stock_location_line loc_line, stock_stock_location location
				WHERE loc_line.stock_location = location.id 
					AND location.type_select != 3
					AND loc_line.product = :id
				GROUP BY location.id) 
		   		UNION ALL
		   		(SELECT estimated_date AS dt,locat,sum(qty)
		                FROM (SELECT location.name AS locat,move.estimated_date AS estimated_date,
		         	           	CASE WHEN move.from_stock_location = location.id THEN -1*line.qty
		                        WHEN move.to_stock_location = location.id THEN line.qty
		                        END AS qty
		                     FROM stock_stock_move_line line 
		                     LEFT JOIN stock_stock_move move ON (line.stock_move = move.id)
		                     LEFT JOIN stock_stock_location location ON (move.from_stock_location = location.id OR move.to_stock_location = location.id)
      	             		 WHERE  line.product = :id
			                     AND move.estimated_date > DATE(:todayDate)
			                     AND location.type_select = 1) location
						 GROUP BY estimated_date,locat) ORDER BY locat,estimatedate) location_stock;  
	    ]]>
	    </dataset>
		<category key="estimatedate" type="date" title="Estimated Date"/>
		<series key="qty" groupBy="locat" type="line" aggregate="sum" title="Quantity"/>
	</chart>
	
	 <chart name="chart.stock.history.report.for.product" title="Stock History" onInit="action-product-set-today-date">
		<search-fields>
		  <field name="fromDate" type="date" title="From" x-required="true"/>
		  <field name="toDate" type="date" title="To" x-required="true"/>
		</search-fields>
		<dataset type="sql">
		 <![CDATA[
		    SELECT 
		         real_date,locat,SUM(qty) OVER (PARTITION BY locat ORDER BY locat,real_date) AS qty 
			FROM 
				 ((SELECT DATE(:fromDate) AS real_date,location.name AS locat,
			     (SELECT SUM(CASE WHEN move.from_stock_location = loc.id THEN -1*qty WHEN move.to_stock_location = loc.id THEN qty END) AS qty                                                                 
       		FROM  
       			stock_stock_move_line line,
       			stock_stock_move move,	        
       			stock_stock_location loc 
      		WHERE 
      		    line.stock_move = move.id 
			    AND move.real_date <= DATE(:fromDate)
				AND move.status_select in (2,3)
				AND line.product = :id
				AND loc.id = location.id
				AND (move.from_stock_location = loc.id OR move.to_stock_location = loc.id)
				AND loc.type_select = 1) AS qty
		    FROM 
		   		stock_stock_location_line loc_line, stock_stock_location location
		    WHERE 
		        loc_line.stock_location = location.id 
				AND location.type_select != 3
				AND loc_line.product = :id
		    GROUP BY 
		   		location.id) 
		    UNION ALL
		   	    (SELECT real_date AS dt,locat,sum(qty)
		    FROM 
		        (SELECT location.name AS locat,move.real_date AS real_date,
     	           	CASE WHEN move.from_stock_location = location.id THEN -1*line.qty
                    	 WHEN move.to_stock_location = location.id THEN line.qty
                    END AS qty
		    FROM 
		     	stock_stock_move_line line 
            LEFT JOIN 
             	stock_stock_move move ON (line.stock_move = move.id)
            LEFT JOIN 
             	stock_stock_location location ON (move.from_stock_location = location.id OR move.to_stock_location = location.id)
		    WHERE  
		    	line.product = :id
	            AND move.real_date > DATE(:fromDate)
			    AND move.real_date <= DATE(:toDate)
	            AND location.type_select = 1) location
      	     GROUP BY 
      	          real_date,locat) 
      	     ORDER BY 
      	          locat,real_date) location_stock;
      	    ]]>  
		</dataset>
		<category key="real_date" type="date" title="Date"/>
		<series key="qty" groupBy="locat" type="line" aggregate="sum" title="Quantity"/>
	 </chart>
	 
	 <chart name="chart.planned.qty.by.location" title="Forecasted Stock">
		<dataset type="jpql">
	    <![CDATA[
		SELECT
	    SUM(_stockMoveLine.qty) AS qty,
	    MONTH(_stockMove.estimatedDate) as month,
	    _to_location.name AS location
		FROM
		    StockMoveLine _stockMoveLine
		LEFT JOIN
		    _stockMoveLine.product AS _product
		LEFT JOIN
		    _stockMoveLine.stockMove as _stockMove
		LEFT JOIN
		    _stockMove.fromStockLocation AS _from_location
		LEFT JOIN
		    _stockMove.toStockLocation AS _to_location
		WHERE
		    _product.id = :id AND _stockMoveLine.product.id = _product.id AND (_from_location.typeSelect = 1 OR _to_location.typeSelect = 1 ) AND _stockMove.estimatedDate > current_date
		GROUP BY
		    _from_location.id,
		    _to_location.id,
		    MONTH(_stockMove.estimatedDate)
		ORDER BY
		    MONTH(_stockMove.estimatedDate)
	    ]]>
	    </dataset>
		<category key="month" type="month" title="Month"/>
		<series key="qty" groupBy="location" type="bar" aggregate="sum" title="Quantity"/>
	</chart>

	<chart name="chart.stock.report.for.product.variant" title="Stock" onInit="action-product-set-today-date">
	    <search-fields>
		    <field name="todayDate" type="date" title="Today" x-required="true"/>
	  	</search-fields>
		<dataset type="sql">
	    <![CDATA[
  			SELECT estimatedate,locat,SUM(qty) OVER (PARTITION BY locat ORDER BY locat,estimatedate) AS qty 
					FROM (
						(SELECT DATE(:todayDate) AS estimatedate,location.name AS locat,
			               		(SELECT SUM(CASE WHEN move.from_stock_location = loc.id THEN -1*qty
			                           WHEN move.to_stock_location = loc.id THEN qty
			                           END) AS qty                                                                 
		               			FROM 	stock_stock_move_line line,
			                    		stock_stock_move move,
			                    		stock_stock_location loc,
			                    		base_product prod 
			               		WHERE line.stock_move = move.id 
									AND move.real_date <= DATE(:todayDate)
									AND move.status_select in (2,3)
									AND line.product = prod.id
									AND prod.parent_product = :id
									AND loc.id = location.id
									AND (move.from_stock_location = loc.id OR move.to_stock_location = loc.id)
									AND loc.type_select = 1) AS qty
						FROM stock_stock_location_line loc_line, 
							 stock_stock_location location,
							 base_product prod 
						WHERE loc_line.stock_location = location.id 
							AND location.type_select = 1 
							AND loc_line.product = prod.id 
							AND prod.parent_product = :id 
						GROUP BY location.id) 
				   		UNION ALL
				   		(SELECT estimated_date AS dt,locat,sum(qty)
				                FROM (SELECT location.name AS locat,move.estimated_date AS estimated_date,
				         	           	CASE WHEN move.from_stock_location = location.id THEN -1*line.qty
				                        WHEN move.to_stock_location = location.id THEN line.qty
				                        END AS qty
				                     FROM stock_stock_move_line line 
				                     LEFT JOIN stock_stock_move move ON (line.stock_move = move.id)
				                     LEFT JOIN stock_stock_location location ON (move.from_stock_location = location.id OR move.to_stock_location = location.id)
				                     LEFT JOIN base_product prod ON (line.product = prod.id)
		      	             		 WHERE prod.parent_product = :id 
					                     AND move.estimated_date > DATE(:todayDate)
					                     AND location.type_select = 1) location
              						 GROUP BY estimated_date,locat) ORDER BY locat,estimatedate) location_stock;  
	    ]]>
	    </dataset>
		<category key="estimatedate" type="date" title="Estimated Date"/>
		<series key="qty" groupBy="locat" type="line" aggregate="sum" title="Quantity"/>
	</chart>

	<action-record name="action-product-set-today-date" model="com.axelor.apps.base.db.Product">
    	<field name="todayDate" expr="eval:__config__.app.getTodayDate()" />
    	<field name="fromDate" expr="call:com.axelor.csv.script.ImportDateTime:importDate('TODAY[-6M]')"/>
    	<field name="toDate" expr="eval:__config__.app.getTodayDate()" />
    </action-record>
	
	<chart name="chart.stock.history.report.for.product.variant" title="Stock History" onInit="action-product-set-today-date">
		<search-fields>
			<field name="fromDate" type="date" title="From" x-required="true"/>
			<field name="toDate" type="date" title="To" x-required="true"/>
		</search-fields>
		<dataset type="sql">
		 <![CDATA[
		    SELECT 
		         real_date,locat,SUM(qty) OVER (PARTITION BY locat ORDER BY locat,real_date) AS qty 
			FROM 
				 ((SELECT DATE(:fromDate) AS real_date,location.name AS locat,
			     (SELECT SUM(CASE WHEN move.from_stock_location = loc.id THEN -1*qty WHEN move.to_stock_location = loc.id THEN qty END) AS qty                                                                 
       		FROM  
       			stock_stock_move_line line,
       			stock_stock_move move,	        
       			stock_stock_location loc,
       			base_product prod 
      		WHERE 
      		    line.stock_move = move.id 
			    AND move.real_date <= DATE(:fromDate)
				AND move.status_select in (2,3)
				AND line.product = prod.id
				AND prod.parent_product = :id
				AND loc.id = location.id
				AND (move.from_stock_location = loc.id OR move.to_stock_location = loc.id)
				AND loc.type_select = 1) AS qty
		    FROM 
		   		stock_stock_location_line loc_line, 
		   		stock_stock_location location,
		   		base_product prod 
		    WHERE 
		        loc_line.stock_location = location.id 
				AND location.type_select = 1 
				AND loc_line.product = prod.id
				AND prod.parent_product = :id
		    GROUP BY 
		   		location.id) 
		    UNION ALL
		   	    (SELECT real_date AS dt,locat,sum(qty)
		    FROM 
		        (SELECT location.name AS locat,move.real_date AS real_date,
     	           	CASE WHEN move.from_stock_location = location.id THEN -1*line.qty
                    	 WHEN move.to_stock_location = location.id THEN line.qty
                    END AS qty
		    FROM 
		     	stock_stock_move_line line 
            LEFT JOIN 
             	stock_stock_move move ON (line.stock_move = move.id)
            LEFT JOIN 
             	stock_stock_location location ON (move.from_stock_location = location.id OR move.to_stock_location = location.id)
		    WHERE  
		    	line.product = :id
	            AND move.real_date > DATE(:fromDate)
			    AND move.real_date <= DATE(:toDate)
	            AND location.type_select = 1) location
      	     GROUP BY 
      	          real_date,locat) 
      	     ORDER BY 
      	          locat,real_date) location_stock;
      	    ]]>  
		</dataset>
	    <category key="real_date" type="date" title="Date"/>
		<series key="qty" groupBy="locat" type="line" aggregate="sum" title="Quantity"/>
	 </chart>
	 
</object-views>