Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decimal and thousand separator for category commission. #2440

Merged
merged 14 commits into from
Nov 26, 2024
63 changes: 49 additions & 14 deletions src/admin/components/Commission/CategoryBasedCommission.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
id="percentage_commission"
name="percentage_commission"
ref='percentage'
:value="commission.all.percentage"
:value="formatValue( commission.all.percentage )"
v-on:input="e => handleAllCategoryInput(e.target.value, 'percentage', commission.all.percentage )"
style="border: none !important;"
/>
Expand All @@ -49,7 +49,7 @@
id="fixed_commission"
name="fixed_commission"
ref='fixed'
:value="commission.all.flat"
:value="formatValue( commission.all.flat )"
v-on:input="e => handleAllCategoryInput(e.target.value, 'flat', commission.all.flat )"
style="border: none !important;"
/>
Expand Down Expand Up @@ -79,7 +79,7 @@
id="percentage_commission"
name="percentage_commission"
ref='percentage'
:value="getCommissionValue( 'percentage', item.term_id )"
:value="formatValue( getCommissionValue( 'percentage', item.term_id ) )"
v-on:input="e => commissinItemHandler( e.target.value, 'percentage', item.term_id, getCommissionValue( 'percentage', item.term_id ) )"
style="border: none !important;"
/>
Expand All @@ -96,8 +96,8 @@
id="fixed_commission"
name="fixed_commission"
ref='flat'
:value="getCommissionValue( 'flat', item.term_id )"
v-on:input="e => commissinItemHandler( e.target.value, 'flat', item.term_id, getCommissionValue( 'percentage', item.term_id ) )"
:value="formatValue( getCommissionValue( 'flat', item.term_id ) )"
v-on:input="e => commissinItemHandler( e.target.value, 'flat', item.term_id, getCommissionValue( 'flat', item.term_id ) )"
style="border: none !important;"
/>
</div>
Expand All @@ -108,6 +108,8 @@
</template>

<script>
import Debounce from "debounce";

export default {
name: 'CategoryBasedCommission',
props: {
Expand Down Expand Up @@ -253,10 +255,13 @@
return this.commission.all[comission_type];
},

commissinItemHandler( value, commission_type, term_id, oldValue = '' ) {
if (isNaN( value )) {
value = oldValue
commissinItemHandler: Debounce( function( value, commission_type, term_id, oldValue = '' ) {
if ( 'percentage' === commission_type ) {
value = this.validatePercentage( this.unFormatValue( value ) );
} else {
value = this.unFormatValue( value );
}

let commissions = JSON.parse( JSON.stringify( this.commission.items ) );

let data = JSON.parse( JSON.stringify( this.commission.all ) );
Expand All @@ -277,17 +282,19 @@
}

this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) )
},
}, 500 ),

handleAllCategoryInput( value, commission_type, oldValue = '' ) {
if (isNaN( value )) {
value = oldValue
handleAllCategoryInput: Debounce( function( value, commission_type, oldValue = '' ) {
if ( 'percentage' === commission_type ) {
value = this.validatePercentage( this.unFormatValue( value ) );
} else {
value = this.unFormatValue( value );
}
this.$set( this.commission.all, commission_type, value );
this.$set(this.commission, 'items', {});

this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) )
},
}, 500 ),

deleteDuplicateCategories( items ) {
let self = this;
Expand Down Expand Up @@ -355,7 +362,35 @@
} );

this.$set( this.commission, 'items', children );
}
},

unFormatValue: ( value ) => {
if ( value === '' ) {
return value;
}

return accounting.unformat(value, dokan.currency.decimal);
},

formatValue: ( value ) => {
if ( value === '' ) {
return value;
}

return accounting.formatNumber( value, dokan.currency.precision, dokan.currency.thousand, dokan.currency.decimal );
},

validatePercentage( percentage ) {
if ( percentage === '' ) {
return percentage;
}

if ( Number( percentage ) < 0 || Number( percentage ) > 100 ) {
percentage = '';
}

return percentage;
},
}
}
</script>
Expand Down
Loading