meta-boxes-subscription.js
7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
jQuery(document).ready(function($){
var timezone = jstz.determine();
// Display the timezone for date changes
$( '#wcs-timezone' ).text( timezone.name() );
// Display times in client's timezone (based on UTC)
$( '.woocommerce-subscriptions.date-picker' ).each(function(){
var $date_input = $(this),
date_type = $date_input.attr( 'id' ),
$hour_input = $( '#'+date_type+'_hour' ),
$minute_input = $( '#'+date_type+'_minute' ),
time = $('#'+date_type+'_timestamp_utc').val(),
date = moment.unix(time);
if ( time > 0 ) {
date.local();
$date_input.val( date.year() + '-' + ( zeroise( date.months() + 1 ) ) + '-' + ( date.format( 'DD' ) ) );
$hour_input.val( date.format( 'HH' ) );
$minute_input.val( date.format( 'mm' ) );
}
});
// Make sure date pickers are in the future
$( '.woocommerce-subscriptions.date-picker:not(#start)' ).datepicker( 'option','minDate',moment().add(1,'hours').toDate());
// Validate date when hour/minute inputs change
$( '[name$="_hour"], [name$="_minute"]' ).on( 'change', function() {
$( '#' + $(this).attr( 'name' ).replace( '_hour', '' ).replace( '_minute', '' ) ).change();
});
// Validate entire date
$( '.woocommerce-subscriptions.date-picker' ).on( 'change',function(){
// The date was deleted, clear hour/minute inputs values and set the UTC timestamp to 0
if( '' == $(this).val() ) {
$( '#' + $(this).attr( 'id' ) + '_hour' ).val('');
$( '#' + $(this).attr( 'id' ) + '_minute' ).val('');
$( '#' + $(this).attr( 'id' ) + '_timestamp_utc' ).val(0);
return;
}
var time_now = moment(),
one_hour_from_now = moment().add(1,'hours' ),
$date_input = $(this),
date_type = $date_input.attr( 'id' ),
date_pieces = $date_input.val().split( '-' ),
$hour_input = $( '#'+date_type+'_hour' ),
$minute_input = $( '#'+date_type+'_minute' ),
chosen_hour = (0 == $hour_input.val().length) ? one_hour_from_now.format( 'HH' ) : $hour_input.val(),
chosen_minute = (0 == $minute_input.val().length) ? one_hour_from_now.format( 'mm' ) : $minute_input.val(),
chosen_date = moment({
years: date_pieces[0],
months: (date_pieces[1] - 1),
date: (date_pieces[2]),
hours: chosen_hour,
minutes: chosen_minute,
seconds: one_hour_from_now.format( 'ss' )
});
// Make sure start date is before now
if ( 'start' == date_type ) {
if ( false === chosen_date.isBefore( time_now ) ) {
alert( wcs_admin_meta_boxes.i18n_start_date_notice );
$date_input.val( time_now.year() + '-' + ( zeroise( time_now.months() + 1 ) ) + '-' + ( time_now.format( 'DD' ) ) );
$hour_input.val( time_now.format( 'HH' ) );
$minute_input.val( time_now.format( 'mm' ) );
}
}
// Make sure trial end and next payment are after start date
else if ( ( 'trial_end' == date_type || 'next_payment' == date_type ) && '' != $( '#start_timestamp_utc' ).val() ) {
var change_date = false,
start = moment.unix( $('#start_timestamp_utc').val() );
// Make sure trial end is after start date
if ( 'trial_end' == date_type && chosen_date.isBefore( start, 'minute' ) ) {
if ( 'trial_end' == date_type ) {
alert( wcs_admin_meta_boxes.i18n_trial_end_start_notice );
} else if ( 'next_payment' == date_type ) {
alert( wcs_admin_meta_boxes.i18n_next_payment_start_notice );
}
// Change the date
$date_input.val( start.year() + '-' + ( zeroise( start.months() + 1 ) ) + '-' + ( start.format( 'DD' ) ) );
$hour_input.val( start.format( 'HH' ) );
$minute_input.val( start.format( 'mm' ) );
}
}
// Make sure next payment is after trial end
if ( 'next_payment' == date_type && '' != $( '#trial_end_timestamp_utc' ).val() ) {
var trial_end = moment.unix( $('#trial_end_timestamp_utc').val() );
if ( chosen_date.isBefore( trial_end, 'minute' ) ) {
alert( wcs_admin_meta_boxes.i18n_next_payment_trial_notice );
$date_input.val( trial_end.year() + '-' + ( zeroise( trial_end.months() + 1 ) ) + '-' + ( trial_end.format( 'DD' ) ) );
$hour_input.val( trial_end.format( 'HH' ) );
$minute_input.val( trial_end.format( 'mm' ) );
}
}
// Make sure trial end is before next payment and expiration is after next payment date
else if ( ( 'trial_end' == date_type || 'end' == date_type ) && '' != $( '#next_payment' ).val() ) {
var change_date = false,
next_payment = moment.unix( $('#next_payment_timestamp_utc').val() );
// Make sure trial end is before or equal to next payment
if ( 'trial_end' == date_type && next_payment.isBefore( chosen_date, 'minute' ) ) {
alert( wcs_admin_meta_boxes.i18n_trial_end_next_notice );
change_date = true;
}
// Make sure end date is after next payment date
else if ( 'end' == date_type && chosen_date.isBefore( next_payment, 'minute' ) ) {
alert( wcs_admin_meta_boxes.i18n_end_date_notice );
change_date = true;
}
if ( true === change_date ) {
$date_input.val( next_payment.year() + '-' + ( zeroise( next_payment.months() + 1 ) ) + '-' + ( next_payment.format( 'DD' ) ) );
$hour_input.val( next_payment.format( 'HH' ) );
$minute_input.val( next_payment.format( 'mm' ) );
}
}
// Make sure the date is more than an hour in the future
if ( 'trial_end' != date_type && 'start' != date_type && chosen_date.unix() < one_hour_from_now.unix() ) {
alert( wcs_admin_meta_boxes.i18n_past_date_notice );
// Set date to current day
$date_input.val( one_hour_from_now.year() + '-' + ( zeroise( one_hour_from_now.months() + 1 ) ) + '-' + ( one_hour_from_now.format( 'DD' ) ) );
// Set time if current time is in the past
if ( chosen_date.hours() < one_hour_from_now.hours() || ( chosen_date.hours() == one_hour_from_now.hours() && chosen_date.minutes() < one_hour_from_now.minutes() ) ) {
$hour_input.val( one_hour_from_now.format( 'HH' ) );
$minute_input.val( one_hour_from_now.format( 'mm' ) );
}
}
if( 0 == $hour_input.val().length ){
$hour_input.val(one_hour_from_now.format( 'HH' ));
}
if( 0 == $minute_input.val().length ){
$minute_input.val(one_hour_from_now.format( 'mm' ));
}
// Update the UTC timestamp sent to the server
date_pieces = $date_input.val().split( '-' );
$('#'+date_type+'_timestamp_utc').val(moment({
years: date_pieces[0],
months: (date_pieces[1] - 1),
date: (date_pieces[2]),
hours: $hour_input.val(),
minutes: $minute_input.val(),
seconds: one_hour_from_now.format( 'ss' )
}).utc().unix());
$( 'body' ).trigger( 'wcs-updated-date',date_type);
});
function zeroise( val ) {
return (val > 9 ) ? val : '0' + val;
}
$('body.post-type-shop_subscription #post').submit(function(){
if('wcs_process_renewal' == $( "body.post-type-shop_subscription select[name='wc_order_action']" ).val()) {
return confirm(wcs_admin_meta_boxes.process_renewal_action_warning);
}
});
$('body.post-type-shop_subscription #post').submit(function(){
if ( typeof wcs_admin_meta_boxes.change_payment_method_warning != 'undefined' && wcs_admin_meta_boxes.payment_method != $('#_payment_method').val() ) {
return confirm(wcs_admin_meta_boxes.change_payment_method_warning);
}
});
});