<?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">
    
    <calendar name="operation-order-planned-calendar" model="com.axelor.apps.production.db.OperationOrder" 
    eventStart="plannedStartDateT" eventStop="plannedEndDateT" title="Planned operations" colorBy="machineWorkCenter.machine" mode="week"
    onChange="action-operation-order-method-set-planned-dates">
    	<field name="name"/>
    </calendar>
    
    <calendar name="operation-order-real-calendar" model="com.axelor.apps.production.db.OperationOrder" 
    eventStart="realStartDateT" eventStop="realEndDateT" title="Real operations" colorBy="machineWorkCenter.machine" mode="week"
    onChange="action-operation-order-method-set-real-dates">
    	<field name="name"/>
    </calendar>
    
    
    <grid name="operation-order-group-grid" title="Operations" model="com.axelor.apps.production.db.OperationOrder" groupBy="manufOrder" orderBy="-manufOrder,priority" canNew="false">
    	<toolbar>
    		<button name="printBtn" title="Print" icon="fa-print" onClick="action-operation-order-method-print"/>
    	</toolbar>
    	<field name="manufOrder" width="120" grid-view="manuf-order-grid" form-view="manuf-order-form"/>
        <field name="priority"/>
        <field name="operationName" readonlyIf="statusSelect == 6"/>
        <field name="prodProcessLine.outsourcing"/>
        <field name="machineWorkCenter" grid-view="prod-process-grid" form-view="prod-process-form"/>
        <field name="plannedStartDateT"/>
        <field name="plannedEndDateT"/>
        <field name="realStartDateT"/>
        <field name="realEndDateT"/>
        <field name="statusSelect"/>
		<field name="manufOrder.statusSelect" hidden="true"/>
		<button name="planBtn" icon="fa-calendar"
				onClick="action-operation-order-method-plan"
				readonlyIf="statusSelect != 2 || $get('manufOrder.statusSelect') &lt; 3"/>
        <button name="startBtn" icon="fa-play" onClick="action-operation-order-group-start" readonlyIf="statusSelect != 3 &amp;&amp; statusSelect != 5"/>
       	<button name="pauseBtn" icon="fa-pause" onClick="action-operation-order-method-pause,save" readonlyIf="statusSelect != 4"/>
   		<button name="finishBtn" icon="fa-power-off" onClick="action-operation-order-group-finish" readonlyIf="statusSelect != 4"/>
   		<button name="cancelBtn" icon="fa-times-circle"
				onClick="action-operation-order-method-cancel,save"
				readonlyIf="statusSelect == 2 || statusSelect == 6"/>
   		<button name="printBtn" title="Print" icon="fa-print" onClick="action-operation-order-method-print"/>
    </grid>
    
    
    <grid name="operation-order-grid" title="Operations" model="com.axelor.apps.production.db.OperationOrder" orderBy="priority" canNew="false">
        <field name="priority" width="80"/>
        <field name="operationName" readonlyIf="statusSelect == 6"/>
        <field name="machineWorkCenter" grid-view="prod-process-grid" form-view="prod-process-form"/>
        <field name="plannedStartDateT"/>
        <field name="plannedEndDateT"/>
        <field name="realStartDateT"/>
        <field name="realEndDateT"/>
        <field name="statusSelect"/>
		<field name="manufOrder.statusSelect" hidden="true"/>
        <button name="planBtn" icon="fa-calendar"
				onClick="save,action-operation-order-method-plan"
				readonlyIf="statusSelect != 2 || $get('manufOrder.statusSelect') &lt; 3"/>
        <button name="startBtn" icon="fa-play" onClick="save,action-operation-order-group-start" readonlyIf="statusSelect != 3 &amp;&amp; statusSelect != 5"/>
       	<button name="pauseBtn" icon="fa-pause" onClick="save,action-operation-order-method-pause,save" readonlyIf="statusSelect != 4"/>
   		<button name="finishBtn" icon="fa-power-off" onClick="action-operation-order-group-finish" readonlyIf="statusSelect != 4"/>
   		<button name="cancelBtn" icon="fa-times-circle"
				onClick="save,action-operation-order-method-cancel,save"
				readonlyIf="statusSelect == 2 || statusSelect == 6"/>
   		<button name="printBtn" title="Print" icon="fa-print" onClick="save,action-operation-order-method-print"/>
    </grid>
    
     
    
	<form name="operation-order-form" title="Operation" model="com.axelor.apps.production.db.OperationOrder" onLoad="action-operation-order-attrs-is-cons-pro-on-operation" onSave="action-operation-order-name" onNew="action-operation-order-attrs-is-cons-pro-on-operation,action-operation-order-default-record">
		<toolbar>
			<button name="printBtn" title="Print" icon="fa-print" onClick="action-operation-order-method-print"/>
		</toolbar>
		<panel name="mainPanel">
			<field name="priority" colSpan="3" readonlyIf="statusSelect == 6"/>
			<field name="operationName" readonlyIf="statusSelect == 6" required="true"/>
			<field name="manufOrder"  colSpan="3" hideIf="$popup()" requiredIf="!$popup()" readonlyIf="statusSelect == 6" onChange="action-operation-order-name" grid-view="manuf-order-grid" form-view="manuf-order-form"/>
			<field name="prodProcessLine" colSpan="3" onSelect="action-operation-order-attrs-domain-prod-process-line" required="true" readonlyIf="statusSelect == 6" grid-view="prod-process-line-grid" form-view="prod-process-line-form"/>
			<field name="prodProcessLine.outsourcing" colSpan="3"/>
			<field name="isToInvoice" colSpan="3"/>
			<field name="prodProcessLine.optional" colSpan="3" readonly="true" showTitle="false">
				<viewer>
				<![CDATA[
					<h4>
						<span class="label label-default" style="background-color: #FC6355; margin: 5px 0 !important; display: inline-table; line-height: initial;" ng-show="record.prodProcessLine.optional" x-translate>Optional</span>
					</h4>
				]]>
				</viewer>
			</field>
			<field name="statusSelect" showTitle="false" readonly="true" widget="NavSelect" colSpan="12"/>
			<field name="manufOrder.statusSelect" hidden="true"/>
		</panel>
		<panel name="datesPanel" title="Dates" colSpan="12" readonlyIf="statusSelect == 6">
			<panel name="plannedDates" title="Planned dates" colSpan="6">
				<field name="plannedStartDateT" onChange="save,action-operation-order-method-compute-duration,save"/>
				<field name="plannedEndDateT" onChange="save,action-operation-order-method-compute-duration,save"/>
				<field name="plannedDuration" colSpan="12" widget="duration" x-seconds="true" x-big="true"/>
			</panel>
			<panel name="realDatesPanel" title="Real dates" colSpan="6">
				<field name="realStartDateT" onChange="save,action-operation-order-method-compute-duration,save"/>
				<field name="realEndDateT" onChange="save,action-operation-order-method-compute-duration,save"/>
				<field name="realDuration" colSpan="12" widget="duration" x-seconds="true" x-big="true"/>
			</panel>
		</panel>
		<panel name="resourcesPanel" title="Resources" colSpan="12" readonlyIf="statusSelect == 6">
			<field name="workCenter" readonly="true" grid-view="work-center-grid" form-view="work-center-form"/>
			<field name="machineWorkCenter" hidden="true" readonlyIf="statusSelect &gt; 3" onSelect="action-operation-order-attrs-domain-machine" onChange="save,action-operation-order-method-machine-change" grid-view="work-center-machine-grid" form-view="work-center-machine-form"/>
			<panel-related name="prodHumanResourceListPanel" field="prodHumanResourceList" readonlyIf="statusSelect == 6" colSpan="12" grid-view="prod-human-resource-grid" form-view="prod-human-resource-form"/>
		</panel>

		<panel name="stockMovesPanel" hidden="true" title="Stock moves" colSpan="12" readonlyIf="statusSelect == 6">
			<field name="inStockMoveList" form-view="stock-move-form" grid-view="stock-move-grid" colSpan="12"/>
			<panel hidden="true" name="consumedProductPanel" title="Consumed products" colSpan="12" readonlyIf="statusSelect == 6">
				<panel-related name="toConsumeProdProductListPanel" field="toConsumeProdProductList" readonlyIf="statusSelect &gt;= 3" colSpan="12" form-view="prod-product-form" grid-view="prod-product-grid"/>
				<panel-related name="consumedStockMoveLineListPanel" field="consumedStockMoveLineList" onChange="action-operation-order-method-check-consumed-stock-move,save,action-operation-order-method-update-consumed-stock-move" form-view="stock-move-line-production-form" grid-view="stock-move-line-consumed-production-grid" colSpan="12"/>
				<panel-related name="diffConsumeProdProductListPanel" field="diffConsumeProdProductList" colSpan="12" form-view="prod-product-form" grid-view="prod-product-grid"/>
			</panel>
		</panel>
		<panel name="actionsPanel" title="Actions" sidebar="true" colSpan="12" showIf="id" readonlyIf="statusSelect == 6">
			<button name="planBtn" title="Plan" icon="fa-calendar" colSpan="12"
					hideIf="statusSelect != 2 || $get('manufOrder.statusSelect') &lt; 3"
					onClick="save,action-operation-order-method-plan"/>
			<button name="startBtn" title="Start" icon="fa-play" showIf="statusSelect == 3" colSpan="12" onClick="save,action-operation-order-group-start,save"/>
			<button name="cancelBtn" title="Cancel" icon="fa-times-circle"
					showIf="statusSelect != 2 &amp;&amp; statusSelect != 6"
					colSpan="12" onClick="save,action-operation-order-method-cancel,save"/>
			<button name="finishBtn" title="Finish" icon="fa-power-off"  showIf="statusSelect == 4" colSpan="12" onClick="save,action-operation-order-group-finish"/>
			<button name="finishPart" icon="fa-power-off" title="Partial Finish" colSpan="12" showIf="statusSelect == 4" onClick="save, action-operation-order-method-part-finish"/>
			<button name="pauseBtn" title="Pause" icon="fa-pause" showIf="statusSelect == 4" colSpan="12" onClick="save,action-operation-order-method-pause,save"/>
			<button name="resumeBtn" title="Resume" icon="fa-step-forward" showIf="statusSelect == 5" colSpan="12" onClick="save,action-operation-order-method-resume,save"/>
		</panel>
		
		<panel-related name="operationOrderDurationListPanel" field="operationOrderDurationList" canNew="false" canEdit="false" canRemove="false" sidebar="true" showIf="id" if="!__config__.app.getApp('production') || !__config__.app.getApp('production').getEnableTimesheetOnManufOrder()"/>

		<panel-related name="operationOrderDurationListPanel" field="timesheetLineList" grid-view="timesheet-line-timesheet-operation-order-grid" form-view="timesheet-line-timesheet-manuf-order-form" colSpan="12" canNew="false" canEdit="false" canRemove="false" if-module="axelor-business-production" if="__config__.app.getApp('production')?.getEnableTimesheetOnManufOrder()"/>
	</form>
	
	<action-attrs name="action-operation-order-attrs-domain-machine">
		<attribute name="domain" for="machineWorkCenter" expr="eval: &quot; self.id IN (${workCenter?.workCenterList?.collect{it.id}.join(',').isEmpty() ?
		 workCenter.id : workCenter.id + ',' + workCenter?.workCenterList?.collect{it.id}.join(',')}) &quot;"/>
	</action-attrs>
	
	<action-attrs name="action-operation-order-attrs-domain-prod-process-line">
		<attribute name="domain" for="prodProcessLine" expr="eval: &quot; self.prodProcess.id = ${_parent?.prodProcess?.id} &quot;" if="_parent != null"/>
		<attribute name="domain" for="prodProcessLine" expr="eval: &quot; self.prodProcess.id = ${manufOrder?.prodProcess?.id} &quot;" if="_parent == null"/>
	</action-attrs>
    
    <action-record name="action-operation-order-default-record" model="com.axelor.apps.production.db.OperationOrder">
    	<field name="statusSelect" expr="eval: 1"/>
    	<field name="manufOrder" expr="eval: _parent"/>
    </action-record>
    
    <action-record name="action-operation-order-name" model="com.axelor.apps.production.db.OperationOrder">
    	<field name="name" expr="eval: manufOrder?.manufOrderSeq + ' - ' + priority"/>
    </action-record>

	<action-group name="action-operation-order-group-start-or-resume">
		<action name="action-operation-order-method-start" if="statusSelect == 3"/>
		<action name="action-operation-order-method-resume" if="statusSelect == 5"/>
	</action-group>

    <action-group name="action-operation-order-group-start">
    	<action name="action-operation-order-method-start"/>
        <action name="action-operation-order-method-compute-duration"/>
    </action-group>

	<action-group name="action-operation-order-group-finish">
		<action name="action-operation-order-method-finish"/>
        <action name="action-operation-order-method-compute-duration"/>
	</action-group>

    <action-method name="action-operation-order-method-pause">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="pause"/>
    </action-method>

    <action-method name="action-operation-order-method-resume">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="resume"/>
    </action-method>

	<action-method name="action-operation-order-method-finish">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="finish"/>
	</action-method>

	<action-method name="action-operation-order-method-part-finish">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="partialFinish"/>
	</action-method>

	<action-method name="action-operation-order-method-cancel">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="cancel"/>
	</action-method>
    
    <action-method name="action-operation-order-method-start">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="start"/>
    </action-method>

    <action-method name="action-operation-order-method-print">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="print"/>
	</action-method>
    
    <action-method name="action-operation-order-method-compute-duration">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="computeDuration"/>
    </action-method>

    <action-method name="action-operation-order-method-set-planned-dates">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="setPlannedDates"/>
    </action-method>

    <action-method name="action-operation-order-method-set-real-dates">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="setRealDates"/>
    </action-method>

    <action-method name="action-operation-order-method-plan">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="plan"/>
    </action-method>

    <action-method name="action-operation-order-method-machine-change">
    	<call class="com.axelor.apps.production.web.OperationOrderController" method="machineChange"/>
    </action-method>

    <action-method name="action-operation-order-method-check-consumed-stock-move">
      <call class="com.axelor.apps.production.web.OperationOrderController" method="checkConsumedStockMoveLineList"/>
    </action-method>

	<action-method name="action-operation-order-method-update-consumed-stock-move">
		<call class="com.axelor.apps.production.web.OperationOrderController" method="updateConsumedStockMoveFromOperationOrder"/>
	</action-method>

	<action-attrs name="action-operation-order-attrs-is-cons-pro-on-operation">
    	<attribute name="hidden" for="consumedProductPanel" expr="eval: !manufOrder?.isConsProOnOperation"/>
    	<attribute name="hidden" for="stockMovesPanel" expr="eval: !manufOrder?.isConsProOnOperation"/>
    	<attribute name="hidden" for="consumedProductPanel" expr="eval: !_parent?.isConsProOnOperation"/>
    	<attribute name="hidden" for="stockMovesPanel" expr="eval: !_parent?.isConsProOnOperation"/>
    	<attribute name="hidden" for="machineWorkCenter" expr="eval: workCenter?.workCenterTypeSelect == 1"/>
    </action-attrs>
    
    <search-filters name="operation-order-filters" model="com.axelor.apps.production.db.OperationOrder" title="Operation order filters">
		<filter title="Draft Operations">
			<domain>self.statusSelect = 1</domain>
		</filter>
		<filter title="Planned Operations">
			<domain>self.statusSelect = 3</domain>
		</filter>
		<filter title="Operations In Progress">
			<domain>self.statusSelect = 4</domain>
		</filter>
		<filter title="Operations In Standby">
			<domain>self.statusSelect = 5</domain>
		</filter>
		<filter title="Finished Operations">
			<domain>self.statusSelect = 6</domain>
		</filter>
		<filter title="Cancelled Operations">
			<domain>self.statusSelect = 2</domain>
		</filter>
		<filter title="Operations to invoice">
			<domain>self.isToInvoice = true</domain>
		</filter>
		<filter title="Late Planned Operations">
			<domain>self.statusSelect = 3 AND self.plannedStartDateT &lt; :_todayDateTime</domain>
		</filter>
		<filter title="Operations Finished late">
			<domain>self.statusSelect = 6 AND self.plannedEndDateT &lt; self.realEndDateT</domain>
		</filter>
		<filter title="Operations with difference">
			<domain>self.isWithDifference = true</domain>
		</filter>
	</search-filters>
</object-views>
