<?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">

	<grid name="sequence-grid" title="Sequences" model="com.axelor.apps.base.db.Sequence">
		<field name="name"/>
		<field name="code"/>
		<field name="company" form-view="company-form" grid-view="company-grid" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field name="prefixe"/>
		<field name="suffixe"/>
		<field name="padding"/>
		<field name="toBeAdded"/>
	</grid>
    
    <form name="sequence-form" title="Sequence" model="com.axelor.apps.base.db.Sequence" onNew="action-group-base-sequence-onnew" onLoad="action-sequence-attrs" onSave="action-sequence-validate">
		<panel name="mainPanel">
            <field name="fullName" showTitle="false" css="label-bold bold large" colSpan="12">
                <editor>
                    <field name="name" showTitle="false" colSpan="6" css="label-bold bold large" placeholder="Name" onChange="action-sequence-method-compute-fullname"/>
                </editor>
            </field>
			<field name="code" onChange="action-sequence-attrs,action-method-sequence-document-concerned-onchange"/>
			<field name="company" widget="SuggestBox" form-view="company-form" grid-view="company-grid" readonlyIf="$popup() &amp;&amp; company"/>
		</panel>
		<panel name="tipsPanel" title="Tips" sidebar="true" itemSpan="12">
			<label name="prefixOrSuffixLabel" title="The tags below can be used as a prefix or a suffix" />
			<label name="fullYearLabel" title="%YYYY : Full Year (2017)" />
			<label name="YYLabel" title="%YY : Year (17)" />
			<label name="MLabel" title="%M : Month (1, 2, 3, ...)" />
			<label name="FMLabel" title="%FM : Full Month (01, 02, 03, ...)" />
			<label name="DLabel" title="%D : Day" />
			<label name="WYLabel" title="%WY : Week number" />
		</panel>
		<panel name="sequenceDefinitionPanel" title="Sequence Definition" colSpan="12">
			<field name="sequenceTypeSelect"/>
			<field name="sequenceLettersTypeSelect" showIf="sequenceTypeSelect == 'LETTERS'" requiredIf="sequenceTypeSelect == 'LETTERS'"/>
			<field name="padding" onChange="action-sequence-method-compute-fullname" showIf="sequenceTypeSelect == 'NUMBERS'"/>
			<field name="toBeAdded" showIf="sequenceTypeSelect == 'NUMBERS'"/>
			<field name="prefixe" pattern="^[^\/\\?*:\|&quot;&lt;>]*$" onChange="action-sequence-method-compute-fullname"/>
			<field name="suffixe" pattern="^[^\/\\?*:\|&quot;&lt;>]*$" onChange="action-sequence-method-compute-fullname"/>
			<field name="yearlyResetOk" readonlyIf="monthlyResetOk" />
			<field name="monthlyResetOk" onChange="action-sequence-monthly-reset" />
		</panel>
		<panel-related name="sequenceVersionListPanel" field="sequenceVersionList" grid-view="sequence-version-grid" form-view="sequence-version-form"  />
	</form>
	
    <grid editable="true" name="sequence-version-grid" title="Versions" model="com.axelor.apps.base.db.SequenceVersion" orderBy="-startDate">
    	<field name="startDate" />
    	<field name="endDate" />
    	<field name="nextNum" />
    </grid>
	
    <form name="sequence-version-form" title="Version" model="com.axelor.apps.base.db.SequenceVersion" >
    	<panel name="mainPanel" itemSpan="4">
	    	<field name="startDate" />
	    	<field name="endDate" />
	    	<field name="nextNum" />
    	</panel>
    </form>
    
    <!-- ACTION GROUPS -->
    <action-group name="action-group-base-sequence-onnew">
    	<action name="action-sequence-default"/>
    	<action name="action-sequence-attrs"/>
    </action-group>
    
    <!-- ACTION RECORD -->
    <action-record name="action-sequence-default" model="com.axelor.apps.base.db.Sequence">
    	<field name="padding" expr="3"/>
    	<field name="toBeAdded" expr="1"/>
    	<field name="company" expr="eval: _parent?.company"/>
    	<field name="company" expr="eval:__user__.activeCompany" if="(_parent == null || _parent.company == null) &amp;&amp; __user__.activeCompany != null"/>
    	<field name="company" expr="eval:__repo__(Company).all().fetchOne()" if="(_parent == null || _parent.company == null) &amp;&amp; __user__.activeCompany == null &amp;&amp; __repo__(Company).all().count() == 1"/>
    </action-record>
    
	<action-record name="action-sequence-monthly-reset" model="com.axelor.apps.base.db.Sequence">
		<field if="monthlyResetOk" name="yearlyResetOk" expr="eval: true"/>
	</action-record>
    
    <action-attrs name="action-sequence-attrs">
		<attribute name="hidden" for="company" expr="eval: code in ['partner','productTrackingNumber','product']"/>
		<attribute name="required" for="company" expr="eval: !(code in ['partner','productTrackingNumber','product','bankOrder'])"/>
    </action-attrs>

    <action-validate name="action-sequence-validate">
    	<error message="Prefix or suffix must contain %YY or %YYYY since yearly reset is true" if="!com.axelor.apps.base.service.administration.SequenceService.isYearValid(__this__)"/>
		<error message="Prefix or suffix must contain %M or %FM, and %YY or %YYYY since monthly reset is true" if="!com.axelor.apps.base.service.administration.SequenceService.isMonthValid(__this__)"/>
		<error message="Total sequence length must be less than 15 characters." if="!com.axelor.apps.base.service.administration.SequenceService.isSequenceLengthValid(__this__)"/>
        <error message="Prefix or suffix must not contain characters forbidden in filenames." if="p = ~/^[^\/\\?*:\|&quot;&lt;>]*$/; prefixe &amp;&amp; !(prefixe ==~ p) || suffixe &amp;&amp; !(suffixe ==~ p)" />
        <error message="Prefix cannot start with #." if="prefixe ==~ /^#.*$/" />
    </action-validate>

	<action-method name="action-method-sequence-document-concerned-onchange">
		<call class="com.axelor.apps.base.web.SequenceController" method="getDefaultTitle"/>
	</action-method>

    <action-method name="action-sequence-method-compute-fullname">
        <call class="com.axelor.apps.base.web.SequenceController" method="computeFullName"/>
    </action-method>

</object-views>
