1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS\API;
13
14 use WC_API_Resource;
15 use WC_API_Server;
16 use WC_POS\Admin\Settings;
17 use WC_POS\i18n;
18 use WC_POS\Tax;
19
20 class Params extends WC_API_Resource {
21
22 protected $base = '/pos/params';
23
24 25 26 27 28 29 30 31
32 public function register_routes( array $routes ){
33
34
35 $routes[ $this->base ] = array(
36 array( array( $this, 'get_params' ), WC_API_Server::READABLE )
37 );
38
39 return $routes;
40
41 }
42
43 44 45 46
47 public function get_params( $wc_pos_admin = null ){
48 if( $wc_pos_admin ){
49 $params = apply_filters( 'woocommerce_pos_{$wc_pos_admin}_params', $this->common_params() + $this->admin_params(), $this );
50 } else {
51 $params = apply_filters( 'woocommerce_pos_params', $this->common_params() + $this->frontend_params(), $this );
52 }
53 return $params;
54 }
55
56 57 58
59 private function common_params(){
60 return array(
61 'accounting' => $this->accounting(),
62 'customers' => $this->customers(),
63 'debug' => defined( '\SCRIPT_DEBUG' ) && \SCRIPT_DEBUG,
64 'emulateHTTP' => get_option( 'woocommerce_pos_emulateHTTP' ) === '1',
65 'idbVersion' => Settings::get_idb_version(),
66 'store' => array( 'name' => get_bloginfo( 'name' ) )
67 );
68 }
69
70 71 72
73 private function frontend_params(){
74 return array(
75 'auto_print' => wc_pos_get_option( 'checkout', 'auto_print_receipt' ),
76 'denominations' => i18n::currency_denominations(),
77 'discount_keys' => wc_pos_get_option( 'general', 'discount_quick_keys' ),
78 'fee' => wc_pos_get_option( 'general', 'fee' ),
79 'hotkeys' => wc_pos_get_option( 'hotkeys', 'hotkeys' ),
80 'menu' => $this->menu(),
81 'shipping' => $this->shipping(),
82 'tabs' => $this->product_tabs(),
83 'tax' => $this->tax(),
84 'tax_classes' => Tax::tax_classes(),
85 'tax_rates' => Tax::tax_rates(),
86 'user' => $this->user()
87 );
88 }
89
90 91 92
93 private function admin_params(){
94 return array(
95 'search_customers_nonce' => wp_create_nonce( 'search-customers' ),
96 'nonce' => wp_create_nonce( \WC_POS\PLUGIN_NAME ),
97 );
98 }
99
100 101 102 103 104
105 private function product_tabs() {
106 return array(
107 'all' => array(
108
109 'label' => __( 'All', 'woocommerce'),
110 'active' => true
111 ),
112 'featured' => array(
113
114 'label' => __( 'Featured', 'woocommerce'),
115 'id' => 'featured:true'
116 ),
117 'onsale' => array(
118 'label' => _x( 'On Sale', 'Product tab: \'On Sale\' products', 'woocommerce-pos'),
119 'id' => 'on_sale:true'
120 ),
121 );
122 }
123
124 125 126 127 128 129
130 private function accounting() {
131 $decimal = get_option( 'woocommerce_price_decimal_sep' );
132 $thousand = get_option( 'woocommerce_price_thousand_sep' );
133 $precision = get_option( 'woocommerce_price_num_decimals' );
134 return array(
135 'currency' => array(
136 'decimal' => $decimal,
137 'format' => $this->currency_format(),
138 'precision' => $precision,
139 'symbol' => get_woocommerce_currency_symbol( get_woocommerce_currency() ),
140 'thousand' => $thousand,
141 ),
142 'number' => array(
143 'decimal' => $decimal,
144 'precision' => $precision,
145 'thousand' => $thousand,
146 )
147 );
148 }
149
150 151 152 153 154
155 private function currency_format() {
156 $currency_pos = get_option( 'woocommerce_currency_pos' );
157
158 if( $currency_pos == 'right' )
159 return array('pos' => '%v%s', 'neg' => '- %v%s', 'zero' => '%v%s');
160
161 if( $currency_pos == 'left_space' )
162 return array('pos' => '%s %v', 'neg' => '- %s %v', 'zero' => '%s %v');
163
164 if( $currency_pos == 'right_space' )
165 return array('pos' => '%v %s', 'neg' => '- %v %s', 'zero' => '%v %s');
166
167
168 return array('pos' => '%s%v', 'neg' => '- %s%v', 'zero' => '%s%v');
169 }
170
171 172 173 174 175
176 private function customers() {
177
178 $user_id = wc_pos_get_option( 'customers', 'logged_in_user' ) ?
179 get_current_user_id() :
180 wc_pos_get_option( 'customers', 'default_customer' );
181
182 if( $user_id ) {
183 $user = get_userdata($user_id);
184 $customers['default'] = array(
185 'id' => $user->ID,
186 'first_name' => esc_html($user->first_name),
187 'last_name' => esc_html($user->last_name),
188 'email' => esc_html($user->user_email),
189 'username' => esc_html($user->user_login)
190 );
191 }
192
193 $customers['guest'] = array(
194 'id' => 0,
195
196 'first_name' => __( 'Guest', 'woocommerce' )
197 );
198
199 return $customers;
200 }
201
202 203 204 205 206
207 private function tax() {
208 return array(
209 'tax_label' => WC()->countries->tax_or_vat(),
210 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ),
211 'prices_include_tax' => get_option( 'woocommerce_prices_include_tax' ),
212 'tax_round_at_subtotal' => get_option( 'woocommerce_tax_round_at_subtotal' ),
213 'tax_display_cart' => get_option( 'woocommerce_tax_display_cart' ),
214 'tax_total_display' => get_option( 'woocommerce_tax_total_display' ),
215 );
216 }
217
218 219 220 221 222
223 private function user() {
224 global $current_user;
225
226 return array(
227 'id' => $current_user->ID,
228 'username' => $current_user->user_login,
229 'first_name' => $current_user->user_firstname,
230 'last_name' => $current_user->user_lastname,
231 'display_name' => $current_user->display_name,
232 'email' => $current_user->user_email
233 );
234 }
235
236 237 238
239 private function shipping(){
240 $shipping = wc_pos_get_option( 'general', 'shipping' );
241 $shipping['labels'] = self::shipping_labels();
242 return $shipping;
243 }
244
245 246 247
248 static public function shipping_labels() {
249
250
251 $labels = array( '' => __( 'N/A', 'woocommerce' ) );
252
253 $shipping_methods = WC()->shipping() ? WC()->shipping->load_shipping_methods() : array();
254
255 foreach( $shipping_methods as $method ){
256 $labels[$method->id] = $method->get_title();
257 }
258
259
260 $labels['other'] = __( 'Other', 'woocommerce' );
261
262 return $labels;
263 }
264
265 266 267
268 private function menu() {
269
270 return apply_filters( 'woocommerce_pos_menu', array(
271 array(
272 'id' => 'pos',
273 'label' => __( 'POS', 'woocommerce-pos' ),
274 'href' => '#'
275 ),
276 array(
277 'id' => 'products',
278
279 'label' => __( 'Products', 'woocommerce' ),
280 'href' => admin_url('edit.php?post_type=product')
281 ),
282 array(
283 'id' => 'orders',
284
285 'label' => __( 'Orders', 'woocommerce' ),
286 'href' => admin_url('edit.php?post_type=shop_order')
287 ),
288 array(
289 'id' => 'customers',
290
291 'label' => __( 'Customers', 'woocommerce' ),
292 'href' => admin_url('users.php')
293 ),
294 array(
295 'id' => 'coupons',
296
297 'label' => __( 'Coupons', 'woocommerce' ),
298 'href' => admin_url('edit.php?post_type=shop_coupon')
299 ),
300 array(
301 'id' => 'support',
302
303 'label' => __( 'Support', 'woocommerce' ),
304 'href' => '#support'
305 )
306 ));
307
308 }
309
310 }