PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / modules / accounting / includes / classes / class-admin.php

class-admin.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/includes/classes/class-admin.php

499 lines 18.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\Accounting\Includes\Classes;
4
5 /**
6 * Admin Pages Handler
7 */
8 class Admin {
9 public function __construct() {
10 add_action( 'admin_menu', [ $this, 'admin_menu' ] );
11 add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_menu' ] );
12 add_action( 'admin_init', [ $this, 'init_hooks' ], 5 );
13 add_action( 'erp_hr_employee_new', [ $this, 'make_people_from_employee' ], 10, 2 );
14 add_action( 'admin_init', [ $this, 'save_accounting_settings' ] );
15 add_action( 'erp_hr_permission_management', [ $this, 'permission_management_field' ] );
16 add_action( 'erp_hr_after_employee_permission_set', [ $this, 'permission_set' ], 10, 2 );
17 }
18
19 /**
20 * Register the admin menu
21 *
22 * @return void
23 */
24 public function admin_menu() {
25 $dashboard = 'erp_ac_view_dashboard';
26 $customer = 'erp_ac_view_customer';
27 $vendor = 'erp_ac_view_vendor';
28 $sale = 'erp_ac_view_sale';
29 $expense = 'erp_ac_view_expense';
30 $account_charts = 'erp_ac_view_account_lists';
31 $bank = 'erp_ac_view_bank_accounts';
32 $journal = 'erp_ac_view_journal';
33 $reports = 'erp_ac_view_reports';
34
35 $slug = 'erp-accounting';
36
37 add_submenu_page( 'erp', __( 'Accounting', 'erp' ), 'Accounting', 'erp_ac_manager', $slug, [ $this, 'erp_accounting_page' ] );
38
39 erp_add_menu_header( 'accounting', 'Accounting', '<svg id="Group_235" data-name="Group 235" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 239 341.4"><defs><style>.cls-1{fill:#9ca1a6}</style></defs><path id="Path_281" data-name="Path 281" class="cls-1"/></svg>' );
40
41 erp_add_menu(
42 'accounting',
43 [
44 'title' => __( 'Dashboard', 'erp' ),
45 'capability' => $dashboard,
46 'slug' => 'dashboard',
47 'position' => 1,
48 ]
49 );
50 erp_add_menu(
51 'accounting',
52 [
53 'title' => __( 'Users', 'erp' ),
54 'capability' => $customer,
55 'slug' => 'users',
56 'position' => 5,
57 ]
58 );
59 erp_add_submenu(
60 'accounting',
61 'users',
62 [
63 'title' => __( 'Customers', 'erp' ),
64 'capability' => $customer,
65 'slug' => 'users/customers',
66 'position' => 5,
67 ]
68 );
69 erp_add_submenu(
70 'accounting',
71 'users',
72 [
73 'title' => __( 'Vendors', 'erp' ),
74 'capability' => $vendor,
75 'slug' => 'users/vendors',
76 'position' => 10,
77 ]
78 );
79 erp_add_submenu(
80 'accounting',
81 'users',
82 [
83 'title' => __( 'Employees', 'erp' ),
84 'capability' => 'erp_ac_manager',
85 'slug' => 'users/employees',
86 'position' => 15,
87 ]
88 );
89 erp_add_menu(
90 'accounting',
91 [
92 'title' => __( 'Transactions', 'erp' ),
93 'capability' => $expense,
94 'slug' => 'transactions',
95 'position' => 25,
96 ]
97 );
98 erp_add_submenu(
99 'accounting',
100 'transactions',
101 [
102 'title' => __( 'Sales', 'erp' ),
103 'capability' => $sale,
104 'slug' => 'transactions/sales',
105 'position' => 5,
106 ]
107 );
108 erp_add_submenu(
109 'accounting',
110 'transactions',
111 [
112 'title' => __( 'Expenses', 'erp' ),
113 'capability' => $expense,
114 'slug' => 'transactions/expenses',
115 'position' => 10,
116 ]
117 );
118 erp_add_submenu(
119 'accounting',
120 'transactions',
121 [
122 'title' => __( 'Purchases', 'erp' ),
123 'capability' => $sale,
124 'slug' => 'transactions/purchases',
125 'position' => 15,
126 ]
127 );
128 erp_add_submenu(
129 'accounting',
130 'transactions',
131 [
132 'title' => __( 'Journals', 'erp' ),
133 'capability' => $journal,
134 'slug' => 'transactions/journals',
135 'position' => 25,
136 ]
137 );
138 erp_add_menu(
139 'accounting',
140 [
141 'title' => __( 'Chart of Accounts', 'erp' ),
142 'capability' => $account_charts,
143 'slug' => 'charts',
144 'position' => 30,
145 ]
146 );
147 erp_add_menu(
148 'accounting',
149 [
150 'title' => __( 'Bank Accounts', 'erp' ),
151 'capability' => $bank,
152 'slug' => 'banks',
153 'position' => 35,
154 ]
155 );
156 erp_add_menu(
157 'accounting',
158 [
159 'title' => __( 'Products', 'erp' ),
160 'capability' => 'erp_ac_view_sale',
161 'slug' => 'products',
162 'position' => 45,
163 ]
164 );
165 erp_add_submenu(
166 'accounting',
167 'products',
168 [
169 'title' => __( 'Products & Services', 'erp' ),
170 'capability' => 'erp_ac_view_sale',
171 'slug' => 'products/product-service',
172 'position' => 5,
173 ]
174 );
175 erp_add_submenu(
176 'accounting',
177 'products',
178 [
179 'title' => __( 'Product Categories', 'erp' ),
180 'capability' => 'erp_ac_view_sale',
181 'slug' => 'products/product-categories',
182 'position' => 10,
183 ]
184 );
185 erp_add_menu(
186 'accounting',
187 [
188 'title' => __( 'Tax', 'erp' ),
189 'capability' => 'erp_ac_view_sale',
190 'slug' => 'taxes',
191 'position' => 45,
192 ]
193 );
194 erp_add_submenu(
195 'accounting',
196 'taxes',
197 [
198 'title' => __( 'Tax Rates', 'erp' ),
199 'capability' => $sale,
200 'slug' => 'taxes/tax-rates',
201 'position' => 5,
202 ]
203 );
204 erp_add_submenu(
205 'accounting',
206 'taxes',
207 [
208 'title' => __( 'Tax Payments', 'erp' ),
209 'capability' => $sale,
210 'slug' => 'taxes/tax-records',
211 'position' => 10,
212 ]
213 );
214 erp_add_menu(
215 'accounting',
216 [
217 'title' => __( 'Reports', 'erp' ),
218 'capability' => $reports,
219 'slug' => 'reports',
220 'position' => 100,
221 ]
222 );
223 erp_add_menu(
224 'accounting',
225 [
226 'title' => __( 'Help', 'erp' ),
227 'capability' => $dashboard,
228 'slug' => 'erp-ac-help',
229 'position' => 200,
230 ]
231 );
232 }
233
234 /**
235 * Render Admin bar menu
236 */
237 public function add_admin_bar_menu() {
238 global $wp_admin_bar;
239
240 /* Check that the admin bar is showing and user has permission... */
241 if ( ! is_admin_bar_showing() ) {
242 return;
243 }
244
245 $hide = [];
246 $header = erp_get_menu_headers();
247
248 if ( ! current_user_can( 'administrator' ) ) {
249 if ( ! current_user_can( 'erp_hr_manager' ) && ! current_user_can( 'erp_recruiter' ) && ! current_user_can( 'erp_list_employee' ) ) {
250 unset( $header['hr'] );
251 $hide[] = true;
252 }
253
254 if ( ! current_user_can( 'erp_crm_manager' ) && ! current_user_can( 'erp_crm_agent' ) ) {
255 unset( $header['crm'] );
256 $hide[] = true;
257 }
258
259 if ( ! current_user_can( 'erp_ac_manager' ) ) {
260 unset( $header['accounting'] );
261 $hide[] = true;
262 }
263 }
264
265 if ( count( $hide ) === 3 ) {
266 return;
267 }
268
269 $menu = erp_acct_quick_access_menu();
270
271 if ( current_user_can( 'erp_ac_manager' ) ) {
272 $wp_admin_bar->add_menu(
273 [
274 'parent' => 'top-secondary',
275 'id' => 'wp-erp-acct',
276 'title' => '<span class="ab-icon"><?xml version="1.0" encoding="UTF-8"?><svg width="17px" height="17px" viewBox="0 0 37 37" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g id="erp-logo" fill="#82878C" fill-rule="nonzero"><path d="M30.3450389,6.04059132 C25.6652413,7.67566309 21.1099947,9.64748663 16.7150389,11.9405913 C15.7850389,12.4605913 14.8150389,13.0405913 13.7150389,13.7205913 C13.0443119,14.2840176 12.1907283,14.5827719 11.3150389,14.5605913 C10.3150389,14.4305913 8.90503889,13.5605913 9.00503889,12.4205913 C9.14503889,10.8305913 11.7350389,9.50059132 12.9150389,8.73059132 C14.7401826,7.56812598 16.67637,6.58998289 18.6950389,5.81059132 C21.1378916,4.73528564 23.7587368,4.12160777 26.4250389,4.00059132 C26.9947028,3.99290686 27.5629047,4.06014969 28.1150389,4.20059132 C28.9137259,4.74357385 29.6602644,5.35955176 30.3450389,6.04059132 Z" id="Shape-Copy-2"></path><path d="M30.7933348,15.9696304 C29.4433348,16.4596304 17.5233348,20.8296304 15.7933348,24.3896304 C15.3633836,25.3434159 15.3633836,26.4358449 15.7933348,27.3896304 C16.3919857,28.7511327 17.7917314,29.5797177 19.2733348,29.4496304 C19.6790059,29.4462835 20.0837104,29.4094922 20.4833348,29.3396304 C21.6712939,29.1478656 22.8417823,28.8602599 23.9833348,28.4796304 C24.9833348,28.1296304 25.9833348,27.6996304 26.9833348,27.2896304 L27.8133348,26.9496304 C30.7383636,25.6650404 33.5343329,24.1046551 36.1633348,22.2896304 C34.4182609,29.8341877 28.08799,35.4468207 20.3887538,36.2759094 C12.6895175,37.104998 5.30944369,32.968757 1.99813915,25.9686936 C-1.31316539,18.9686301 0.170333077,10.6395718 5.69491091,5.21327782 C11.2194887,-0.213016153 19.5738319,-1.54678143 26.5133348,1.88963044 C16.9833348,3.59963044 12.0633348,7.18963044 9.18333477,9.75963044 L8.86333477,10.0896304 C7.93333477,11.0396304 6.66333477,12.3296304 7.47333477,14.4196304 C7.9783997,15.5890182 9.06827343,16.4007074 10.3333348,16.5496304 L10.7033348,16.5496304 C11.6270831,16.4741709 12.5076261,16.1261302 13.2333348,15.5496304 C14.2333348,14.8796304 15.2333348,14.3096304 16.1533348,13.7996304 C21.2778133,11.1185988 26.6326398,8.90303934 32.1533348,7.17963044 C33.6733348,7.84963044 36.5733348,11.2496304 36.3833348,14.1796304 C34.6633348,14.7396304 32.2733348,15.4396304 30.7933348,15.9696304 Z" id="Shape-Copy-3"></path><path d="M34.3722313,17.49 C34.3726197,18.4970161 34.2754999,19.501704 34.0822313,20.49 C31.869491,22.1051295 29.5283527,23.5365684 27.0822313,24.77 C24.8622313,25.93 22.7822313,27.32 20.2422313,27.89 C19.0122313,28.16 16.9222313,27.42 17.0022313,25.89 C17.0622313,24.8 19.0022313,23.54 19.7922313,22.98 C21.2689784,21.9825786 22.8358063,21.1255103 24.4722313,20.42 C27.682491,19.0175052 30.9997216,17.8738632 34.3922313,17 C34.3622313,17.16 34.3722313,17.32 34.3722313,17.49 Z" id="Shape-Copy-4"></path></g></g></svg></span>New Transaction',
277 ]
278 );
279 }
280
281 foreach ( $menu as $component => $items ) {
282 $wp_admin_bar->add_menu(
283 [
284 'parent' => 'wp-erp-acct',
285 'id' => 'wp-erp-acct-' . $items['slug'],
286 'href' => admin_url( 'admin.php?page=erp-accounting' ) . '#/' . $items['url'],
287 'title' => $items['title'],
288 ]
289 );
290 }
291 }
292
293 /**
294 * Initialize our hooks for the admin page
295 *
296 * @return void
297 */
298 public function init_hooks() {
299 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
300 }
301
302 /**
303 * Load scripts and styles for the app
304 *
305 * @return void
306 */
307 public function enqueue_scripts() {
308 // load styles
309 wp_enqueue_style( 'accounting-admin' );
310
311 // load hook
312 wp_enqueue_script( 'accounting-bootstrap' );
313
314 // JS do action
315 do_action( 'erp_acct_js_hook_loaded' );
316
317 // load scripts
318 wp_enqueue_script( 'accounting-admin' );
319 }
320
321 /**
322 * Render our admin page
323 *
324 * @return void
325 */
326 public function erp_accounting_page() {
327 ?>
328 <script>
329 window.erpAcct = JSON.parse('<?php echo wp_kses_post( wp_slash(
330 json_encode( apply_filters( 'erp_localized_data', [] ) )
331 ) ); ?>');
332 </script>
333 <?php
334 echo '<div class="wrap"><div id="erp-accounting"></div></div>';
335
336 $component = 'accounting';
337 $menu = erp_menu();
338 $menu = $menu[ $component ];
339 $section = ( isset( $_GET['section'] ) && isset( $menu[ $_GET['section'] ] ) ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : 'dashboard';
340 $sub = ( isset( $_GET['sub-section'] ) && ! empty( $menu[ $section ]['submenu'][ $_GET['sub-section'] ] ) ) ? sanitize_text_field( wp_unslash( $_GET['sub-section'] ) ) : false;
341 }
342
343 /**
344 * Make employee as people
345 *
346 * @param int $id
347 * @param array $data
348 *
349 * @return bool
350 */
351 public function make_people_from_employee( $id, $data ) {
352 if ( ! current_user_can( 'erp_create_employee' ) ) {
353 return;
354 }
355
356 $first_name = isset( $data['personal']['first_name'] ) ? $data['personal']['first_name'] : '';
357 $last_name = isset( $data['personal']['last_name'] ) ? $data['personal']['last_name'] : '';
358 $mobile = isset( $data['personal']['mobile'] ) ? $data['personal']['mobile'] : '';
359 $phone = isset( $data['personal']['phone'] ) ? $data['personal']['phone'] : '';
360 $email = isset( $data['user_email'] ) ? $data['user_email'] : '';
361
362 $args = [
363 'first_name' => $first_name,
364 'last_name' => $last_name,
365 'email' => $email,
366 'mobile' => $mobile,
367 'phone' => $phone,
368 'type' => 'employee',
369 ];
370
371 $people_id = erp_insert_people( $args );
372
373 if ( is_wp_error( $people_id ) ) {
374 return new \WP_Error( 'crate_people', __( 'Could not create people', 'erp' ) );
375 }
376
377 return $people_id;
378 }
379
380 /**
381 * Save Financial Year settings
382 *
383 * @param int $id
384 * @param array $data
385 *
386 * @return bool
387 */
388 public function save_accounting_settings() {
389 if ( $_SERVER['REQUEST_METHOD'] !== 'POST' || ! isset( $_POST['erp-ac-ob-fyears-add'] ) ) {
390 return;
391 }
392
393 global $wpdb;
394
395 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'erp-settings-nonce' ) ) {
396 die( 'Nonce failed.' );
397 }
398
399 $fin_years = [];
400
401 if (
402 empty( $_POST['ob_names'] )
403 || empty( $_POST['ob_starts'] )
404 || empty( $_POST['ob_ends'] )
405 ) {
406 return;
407 }
408
409 $ob_names = array_map( 'sanitize_text_field', wp_unslash( $_POST['ob_names'] ) );
410 $ob_starts = array_map( 'sanitize_text_field', wp_unslash( $_POST['ob_starts'] ) );
411 $ob_ends = array_map( 'sanitize_text_field', wp_unslash( $_POST['ob_ends'] ) );
412 $created_by = get_current_user_id();
413
414 if ( ! empty( $ob_names ) ) {
415 for ( $i = 0; $i < count( $ob_names ); $i++ ) {
416 $fin_years['ob_names'][] = $ob_names[ $i ];
417 }
418 }
419
420 if ( ! empty( $ob_starts ) ) {
421 for ( $i = 0; $i < count( $ob_starts ); $i++ ) {
422 $fin_years['ob_starts'][] = $ob_starts[ $i ];
423 }
424 }
425
426 if ( ! empty( $ob_ends ) ) {
427 for ( $i = 0; $i < count( $ob_ends ); $i++ ) {
428 $fin_years['ob_ends'][] = $ob_ends[ $i ];
429 }
430 }
431
432 $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . 'erp_acct_financial_years' );
433
434 for ( $i = 0; $i < count( $ob_names ); $i++ ) {
435 $wpdb->insert(
436 $wpdb->prefix . 'erp_acct_financial_years',
437 [
438 'name' => $fin_years['ob_names'][ $i ],
439 'start_date' => $fin_years['ob_starts'][ $i ],
440 'end_date' => $fin_years['ob_ends'][ $i ],
441 'created_at' => date( 'Y-m-d' ),
442 'created_by' => $created_by,
443 ],
444 ['%s', '%s', '%s', '%s', '%d']
445 );
446 }
447 }
448
449 /**
450 * Check accounting permission for users
451 *
452 * @since 1.5.12
453 *
454 * @param object $employee
455 *
456 * @return void
457 */
458 public function permission_management_field( $employee ) {
459 if ( ! erp_acct_is_hr_current_user_manager() ) {
460 return;
461 }
462
463 $is_manager = user_can( $employee->id, erp_ac_get_manager_role() ) ? 'on' : 'off';
464
465 erp_html_form_input( [
466 'label' => esc_html__( 'Accounting Manager', 'erp' ),
467 'name' => 'acct_manager',
468 'type' => 'checkbox',
469 'tag' => 'div',
470 'value' => $is_manager,
471 'help' => esc_html__( 'This Employee is Accounting Manager', 'erp' ),
472 ] );
473 }
474
475 /**
476 * Set employee permission as account manager
477 *
478 * @since 1.5.12
479 *
480 * @param array $post
481 * @param object $user
482 *
483 * @return void
484 */
485 public function permission_set( $post, $user ) {
486 $enable_acct_manager = isset( $post['acct_manager'] ) ? filter_var( $post['acct_manager'], FILTER_VALIDATE_BOOLEAN ) : false;
487
488 $acct_manager_role = erp_ac_get_manager_role();
489
490 if ( current_user_can( $acct_manager_role ) ) {
491 if ( $enable_acct_manager ) {
492 $user->add_role( $acct_manager_role );
493 } else {
494 $user->remove_role( $acct_manager_role );
495 }
496 }
497 }
498 }
499