<?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 name="chart.planned.charge.by.machine.hours" title="Previsional charge by machine (per hour)" onInit="action.production.chart.set.from.date.to.date">
		<search-fields>
			<field type="datetime" name="fromDateTime" title="From Date"/>
	 		<field type="datetime" name="toDateTime" title="To Date"/>
	 	</search-fields>
		<dataset type="rpc">com.axelor.apps.production.web.OperationOrderController:chargeByMachineHours</dataset>
		<category key="dateTime" type="text" />
		<series key="charge" groupBy="machine" type="bar"/>
	</chart>
	
 	
 	<chart name="chart.planned.charge.by.machine.days" title="Previsional charge by machine (per days)" onInit="action.production.chart.set.from.date.to.date">
		<search-fields>
			<field type="datetime" name="fromDateTime" title="From Date"/>
	 		<field type="datetime" name="toDateTime" title="To Date"/>
	 	</search-fields>
		<dataset type="rpc">com.axelor.apps.production.web.OperationOrderController:chargeByMachineDays</dataset>
		<category key="dateTime" type="text" />
		<series key="charge" groupBy="machine" type="bar"/>
	</chart>
	
	<chart name="chart.wastes.by.product" title="Products Wastes">
		<search-fields>
			<field name="name" title="Product" type="reference" target="com.axelor.apps.base.db.Product" x-required="true"/>
		</search-fields>
		<dataset type="jpql">
			<![CDATA[
				SELECT
					prod.name AS _product,
					SUM(self.qty) AS _quantity
				FROM
					ProdProduct self,
					Product prod
				WHERE
					self.wasteManufOrder != null
				AND
					prod.id = self.product
				AND 
					self.product = :name
				GROUP BY
					prod.name
			]]></dataset>
		<category key="_product" type="text" title="Product"/>
		<series key="_quantity" type="bar" title="Quantity"/>
	</chart> 

    <chart name="chart.duration.on.mos" title="">
        <dataset type="jpql">
            <![CDATA[
                SELECT
                    TO_CHAR(self.stoppingDateTime, 'YYYY-MM-DD') AS _date,
                    EXTRACT(EPOCH FROM SUM(self.stoppingDateTime - self.startingDateTime))/3600 AS _hours
                FROM
                    OperationOrderDuration self
                WHERE
                    self.startedBy.id = :userId
                    AND self.startingDateTime IS NOT NULL AND self.stoppingDateTime IS NOT NULL
                    AND TO_CHAR(self.startingDateTime, 'YYYY-MM-DD') >= :dateFrom AND TO_CHAR(self.stoppingDateTime, 'YYYY-MM-DD') <= :dateTo
                GROUP BY
                    TO_CHAR(self.stoppingDateTime, 'YYYY-MM-DD')
            ]]>
        </dataset>
        <category key="_date" type="text" title="User"/>
        <series key="_hours" type="bar" title="Duration (hours)"/>
    </chart>

	<action-record name="action.production.chart.set.from.date.to.date" model="com.axelor.apps.production.db.Machine">
 	    <field name="toDateTime" expr="eval:__datetime__.plusDays(1)" />
 	    <field name="fromDateTime" expr="eval:__datetime__"/>
 	</action-record>

</object-views>
