PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
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 / functions-chart.php

functions-chart.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at modules/accounting/includes/functions-chart.php

533 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get all chart classes
5 *
6 * @return array
7 */
8 function erp_ac_get_chart_classes() {
9 $classes = [
10 1 => __( 'Assets', 'erp' ),
11 2 => __( 'Liabilities', 'erp' ),
12 3 => __( 'Expenses', 'erp' ),
13 4 => __( 'Income', 'erp' ),
14 5 => __( 'Equity', 'erp' ),
15 ];
16
17 return $classes;
18 }
19
20 /**
21 * Get all chart
22 *
23 * @param $args array
24 *
25 * @return array
26 */
27 function erp_ac_get_all_chart( $args = [] ) {
28 global $wpdb;
29
30 $defaults = array(
31 'number' => 20,
32 'offset' => 0,
33 'orderby' => 'id',
34 'order' => 'ASC',
35 'class_id' => '-1',
36 'count' => false
37 );
38
39 $args = wp_parse_args( $args, $defaults );
40 $cache_key = 'erp-ac-chart-all-' . md5( serialize( $args ) );
41 $items = wp_cache_get( $cache_key, 'erp' );
42
43 if ( false === $items ) {
44 $condition = '';
45 $limit = '';
46
47 if ( $args['number'] != '-1' && ! empty( $args['number'] ) ) {
48 $limit = "LIMIT {$args['offset']}, {$args['number']}";
49 }
50
51 if ( $args['class_id'] != '-1' && ! empty( $args['class_id'] ) ) {
52 $class_id = $args['class_id'];
53 $condition .= "WHERE ct.class_id = $class_id";
54 }
55
56 $select_sql = "SELECT ch.*, ct.class_id, ct.name as type_name, count(jour.ledger_id) as entries";
57
58 $group_by_sql = "GROUP BY ch.id";
59 $order_by_sql = "ORDER BY {$args['orderby']} {$args['order']}";
60
61 if ( $args['count'] ) {
62 $select_sql = "SELECT count(*)";
63 $group_by_sql = "";
64 $order_by_sql = "";
65 }
66
67 $sql = $select_sql . " FROM {$wpdb->prefix}erp_ac_ledger AS ch
68 LEFT JOIN {$wpdb->prefix}erp_ac_chart_types AS ct ON ct.id = ch.type_id
69 LEFT JOIN {$wpdb->prefix}erp_ac_journals as jour ON jour.ledger_id = ch.id
70 $condition
71 $group_by_sql
72 $order_by_sql
73 $limit";
74
75 $items = $args['count'] ? $wpdb->get_var( $sql ) : $wpdb->get_results( $sql );
76
77 wp_cache_set( $cache_key, $items, 'erp' );
78 }
79
80 return $items;
81 }
82
83 function erp_ac_get_bank_account() {
84 return WeDevs\ERP\Accounting\Model\Ledger::bank()->with( ['bank_details'] )->get()->toArray();
85 }
86
87 function erp_ac_get_individual_bank_balance( $bank_id, $currency = true ) {
88 $get_journals = \WeDevs\ERP\Accounting\Model\Journal::ofledger( $bank_id )->get()->toArray();
89 $debit = array_sum( wp_list_pluck( $get_journals, 'debit' ) );
90 $credit = array_sum( wp_list_pluck( $get_journals, 'credit' ) );
91
92 $total_amount = $debit - $credit;
93
94 return $currency ? erp_ac_get_price( $total_amount ) : $total_amount;
95 }
96
97 /**
98 * Fetch all chart from database
99 *
100 * @return array
101 */
102 function erp_ac_get_chart_count() {
103 global $wpdb;
104
105 return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'erp_ac_ledger' );
106 }
107
108 /**
109 * Fetch a single chart from database
110 *
111 * @param int $id
112 *
113 * @return array
114 */
115 function erp_ac_get_chart( $id = 0 ) {
116 return \WeDevs\ERP\Accounting\Model\Ledger::with('bank_details')->find( $id );
117 }
118
119 /**
120 * Insert a new chart
121 *
122 * @param array $args
123 */
124 function erp_ac_insert_chart( $args = array() ) {
125 global $wpdb;
126
127 $defaults = array(
128 'id' => null,
129 'name' => '',
130 'description' => '',
131 'type_id' => '',
132 'active' => 1,
133 'parent' => 0,
134 'system' => 0,
135 'created_by' => get_current_user_id()
136 );
137
138 $args = wp_parse_args( $args, $defaults );
139
140 $table_name = $wpdb->prefix . 'erp_ac_ledger';
141
142 // some basic validation
143 if ( empty( $args['name'] ) ) {
144 return new WP_Error( 'no-name', __( 'No Name provided.', 'erp' ) );
145 }
146
147 // remove row id to determine if new or update
148 $row_id = (int) $args['id'];
149 unset( $args['id'] );
150
151 if ( ! $row_id ) {
152 if ( ! erp_ac_create_account() ) {
153 return new WP_Error( 'error', __( 'You do not have sufficient permissions', 'erp' ) );
154 }
155
156 $ledger = WeDevs\ERP\Accounting\Model\Ledger::create( $args );
157
158 if ( $ledger->id ) {
159 return $ledger->id;
160 }
161
162 } else {
163
164 if ( ! erp_ac_edit_account() ) {
165 return new WP_Error( 'error', __( 'You do not have sufficient permissions', 'erp' ) );
166 }
167
168 // don't allow to change account type
169 unset( $args['type_id'] );
170
171 // do update method here
172 if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
173
174 return $row_id;
175 }
176 }
177
178 return false;
179 }
180
181 /**
182 * Get all chart types
183 *
184 * @param $args array
185 *
186 * @return array
187 */
188 function erp_ac_get_all_chart_types() {
189 global $wpdb;
190
191 $cache_key = 'erp-ac-chart-type-all';
192 $items = wp_cache_get( $cache_key, 'erp' );
193
194 if ( false === $items ) {
195 $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'erp_ac_chart_types ORDER BY class_id ASC' );
196
197 wp_cache_set( $cache_key, $items, 'erp' );
198 }
199
200 return $items;
201 }
202
203 /**
204 * Get all chart types by class id
205 *
206 * @param int $class_id
207 *
208 * @return array
209 */
210 function erp_ac_get_chart_types_by_class_id( $class_id ) {
211 global $wpdb;
212
213 $cache_key = 'erp-ac-chart-type-by-class-id';
214 $items = wp_cache_get( $cache_key, 'erp' );
215
216 if ( false === $items ) {
217 $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'erp_ac_chart_types WHERE class_id = '. $class_id .' ORDER BY class_id ASC' );
218
219 wp_cache_set( $cache_key, $items, 'erp' );
220 }
221
222 return $items;
223 }
224
225 /**
226 * Get chart type as array
227 *
228 * @return array
229 */
230 function erp_ac_get_all_chart_types_array() {
231 $classes = erp_ac_get_chart_classes();
232 $all_types = erp_ac_get_all_chart_types();
233 $types = [];
234
235 foreach ($all_types as $type) {
236 $types[ $type->class_id ][ $type->id ] = $type->name;
237 }
238
239 return $types;
240 }
241
242 function erp_ac_get_charts() {
243 $raw = [
244 1 => [],
245 2 => [],
246 3 => [],
247 4 => [],
248 5 => [],
249 ];
250
251 $cache_key = 'erp_account_charts';
252 $accounts = wp_cache_get( $cache_key );
253
254 if ( false === $accounts ) {
255 $accounts = \WeDevs\ERP\Accounting\Model\Chart_Of_Accounts::all()->toArray();
256 wp_cache_set( $cache_key, $accounts );
257 }
258
259 // group by account_type_id
260 foreach ($accounts as $ac) {
261 $raw[ $ac['type_id'] ][] = $ac;
262 }
263
264 return $raw;
265 }
266
267 function erp_ac_get_bank_accounts() {
268 return WeDevs\ERP\Accounting\Model\Ledger::bank()->active()->get()->toArray();
269 }
270
271 function erp_ac_get_bank_dropdown() {
272 $accounts = [];
273 $banks = erp_ac_get_bank_accounts();
274
275 if ( $banks ) {
276 foreach ($banks as $bank) {
277 $accounts[ $bank['id'] ] = sprintf( '%s - %s', $bank['code'], $bank['name'] );
278 }
279 }
280
281 return $accounts;
282 }
283
284 function erp_ac_get_chart_dropdown( $args = [] ) {
285 $account_charts = [];
286 $defaults = [
287 'select_text' => __( '&#8212; Select Account &#8212;', 'textdomain' ),
288 'selected' => '0',
289 'name' => 'chart-of-accounts',
290 'exclude' => false,
291 'class' => 'erp-select2',
292 'required' => false,
293 ];
294 $args = wp_parse_args( $args, $defaults );
295
296 global $wpdb;
297
298 $cache_key = 'erp-account-ledgers-list';
299 $ledgers = wp_cache_get( $cache_key, 'erp' );
300
301 if ( false === $ledgers ) {
302 $sql = "SELECT led.id, led.code, led.name, class.name as class_name, class.id as class_id from {$wpdb->prefix}erp_ac_ledger as led
303 LEFT JOIN {$wpdb->prefix}erp_ac_chart_types as types on led.type_id = types.id
304 LEFT JOIN {$wpdb->prefix}erp_ac_chart_classes as class on class.id = types.class_id
305 ORDER BY led.id ASC";
306
307 $ledgers = $wpdb->get_results( $sql );
308 wp_cache_set( $cache_key, $ledgers, 'erp' );
309 }
310
311 // build the array
312 if ( $ledgers ) {
313 foreach ($ledgers as $ledger) {
314 if ( ! isset( $account_charts[ $ledger->class_id ] ) ) {
315 $account_charts[ $ledger->class_id ]['label'] = $ledger->class_name;
316 $account_charts[ $ledger->class_id ]['options'][] = $ledger;
317 } else {
318 $account_charts[ $ledger->class_id ]['options'][] = $ledger;
319 }
320
321 }
322 }
323
324 // exclude charts
325 if ( is_array( $args['exclude'] ) && $args['exclude'] ) {
326 foreach ($args['exclude'] as $class_id) {
327 if ( array_key_exists( $class_id, $account_charts ) ) {
328 unset( $account_charts[ $class_id ] );
329 }
330 }
331 }
332
333 return $account_charts;
334 }
335
336 function erp_ac_render_account_dropdown_html( $account_charts = [], $args = [] ) {
337
338 $defaults = [
339 'select_text' => __( '&#8212; Select &#8212;', 'erp' ),
340 'selected' => '0',
341 'name' => 'chart-of-accounts',
342 'class' => 'erp-select2',
343 'required' => true,
344 ];
345 $args = wp_parse_args( $args, $defaults );
346
347 $dropdown = sprintf( '<select name="%1$s" id="%1$s" class="%2$s"%3$s>', $args['name'], $args['class'], $args['required'] == true ? ' required="required"' : '' );
348
349 $dropdown .= '<option value="">' . $args['select_text'] . '</option>';
350
351 if ( $account_charts ) {
352
353 foreach( $account_charts as $chart ) {
354 $dropdown .= '<optgroup label="' . $chart['label'] . '">';
355 foreach ($chart['options'] as $ledger) {
356 $dropdown .= sprintf( '<option value="%s" %s>%s - %s</option>', $ledger->id, selected( $args['selected'], $ledger->id, false ), $ledger->code, $ledger->name );
357 }
358 $dropdown .= '</optgroup>';
359 }
360 }
361
362 $dropdown .= '</select>';
363
364 return $dropdown;
365 }
366
367 function erp_ac_get_journals_by_class_id( $id ) {
368 global $wpdb;
369
370 $expense = new \WeDevs\ERP\Accounting\Model\Ledger();
371 $type_table = $wpdb->prefix . 'erp_ac_chart_types';
372 $class_table = $wpdb->prefix . 'erp_ac_chart_classes';
373 $ledger_table = $wpdb->prefix . 'erp_ac_ledger';
374 $journal_table = $wpdb->prefix . 'erp_ac_journals';
375
376 global $wpdb;
377
378 $cache_key = 'erp_ac_get_journals_by_class' . $id;
379 //$items = get_transient( $cache_key );
380
381 //if ( false === $items ) {
382 $sql = "SELECT led.id, led.code, led.name, led.type_id, types.name as type_name, types.class_id,
383 class.name as class_name, sum(jour.debit) as debit, sum(jour.credit) as credit, jour.type as jour_type
384 FROM $ledger_table as led
385 LEFT JOIN $type_table as types ON types.id = led.type_id
386 LEFT JOIN $class_table as class ON class.id = types.class_id
387 LEFT JOIN $journal_table as jour ON jour.ledger_id = led.id
388 WHERE class.id = $id
389 GROUP BY led.id";
390
391 $items = $wpdb->get_results( $sql );
392 //set_transient( $cache_key, $items, 12 * HOUR_IN_SECONDS );
393 // }
394
395 return $items;
396 }
397
398 function erp_ac_get_ledger_by_class_id( $id ) {
399 global $wpdb;
400
401 $expense = new \WeDevs\ERP\Accounting\Model\Ledger();
402 $type_table = $wpdb->prefix . 'erp_ac_chart_types';
403 $class_table = $wpdb->prefix . 'erp_ac_chart_classes';
404 $ledger_table = $wpdb->prefix . 'erp_ac_ledger';
405
406 global $wpdb;
407
408 $cache_key = 'erp_ac_get_ledger_by_class' . $id;
409 $items = get_transient( $cache_key );
410
411 if ( false === $items ) {
412 $sql = "SELECT led.id, led.code, led.name, led.type_id, types.name as type_name, types.class_id, class.name as class_name
413 FROM $ledger_table as led
414 LEFT JOIN $type_table as types ON types.id = led.type_id
415 LEFT JOIN $class_table as class ON class.id = types.class_id
416 WHERE class.id = $id
417 GROUP BY led.id";
418
419 $items = $wpdb->get_results( $sql );
420 set_transient( $cache_key, $items, 12 * HOUR_IN_SECONDS );
421 }
422
423 return $items;
424 }
425
426 function erp_ac_get_bank_journals() {
427 return \WeDevs\ERP\Accounting\Model\Ledger::bank()->with('journals')->get()->toArray();
428 }
429
430 /**
431 * Print the chart table
432 *
433 * @param string $title
434 * @param array $charts
435 *
436 * @return void
437 */
438 function erp_ac_chart_print_table( $title, $charts = [] ) {
439 $edit_url = admin_url( 'admin.php?page=erp-accounting-charts&action=edit&id=' );
440
441 include dirname( __FILE__ ) . '/views/accounts/chart-table.php';
442 }
443
444 function erp_ac_bank_journal( $bank_id ) {
445 return \WeDevs\ERP\Accounting\Model\Journal::where( 'ledger_id', $bank_id )->get()->toArray();
446 }
447
448 function erp_ac_bank_credit_total_amount( $bank_id ) {
449 $db = new \WeDevs\ORM\Eloquent\Database();
450 $dc = [];
451
452 $dc['debit'] = \WeDevs\ERP\Accounting\Model\Journal::select( array( $db->raw( 'SUM(debit) as debit_sum') ) )->where( 'ledger_id', $bank_id )->pluck('debit_sum');
453
454 $dc['credit'] = \WeDevs\ERP\Accounting\Model\Journal::select( array( $db->raw( 'SUM(credit) as credit_sum') ) )->where( 'ledger_id', $bank_id )->pluck('credit_sum');
455
456 return $dc;
457 }
458
459 function erp_ac_exclude_chart( $dropdown = [], $accounts_id = [] ) {
460 foreach ( $dropdown as $key => $account ) {
461 foreach ( $account['options'] as $key_opt => $acc_opt ) {
462 if ( in_array( $acc_opt->id, $accounts_id ) ) {
463 unset( $dropdown[$key]['options'][$key_opt] );
464 }
465 }
466 }
467
468 return $dropdown;
469 }
470
471 // Inside customer included Assets and Expenses
472 // Inside vendor included Liabilities and Equity
473 function erp_ac_chart_grouping() {
474 $chart_classes = \WeDevs\ERP\Accounting\Model\Chart_Classes::with(['types'])->get()->toArray();
475
476 // Inside customer included Assets and Expenses
477 // Inside vendor included Liabilities and Equity
478 $group['customer'] = [];
479 $group['vendor'] = [];
480
481 foreach ( $chart_classes as $key => $chart_classe ) {
482 if ( $chart_classe['id'] == 1 || $chart_classe['id'] == 3 ) {
483 $group['customer'] = array_merge( $group['customer'], wp_list_pluck( $chart_classe['types'], 'id' ) );
484 }
485
486 if ( $chart_classe['id'] == 2 || $chart_classe['id'] == 5 ) {
487 $group['vendor'] = array_merge( $group['vendor'], wp_list_pluck( $chart_classe['types'], 'id' ) );
488 }
489 }
490
491 return $group;
492 }
493
494 function erp_ac_delete_chart( $chart_id ) {
495
496 if ( ! erp_ac_delete_account() ) {
497 return new WP_Error( 'error', __( 'You do not have sufficient permissions', 'erp' ) );
498 }
499
500 $chart = erp_ac_bank_journal( $chart_id );
501
502 if ( $chart ) {
503 return new WP_Error( 'error', __('The account record can not be deleted as it contains one or more transactions.', 'erp') );
504 }
505
506 $delete = \WeDevs\ERP\Accounting\Model\Chart_Of_Accounts::find( $chart_id )->delete();
507
508 if ( $delete ) {
509 return true;
510 }
511
512 return new WP_Error( 'error', __('Unexpected error!', 'erp') );
513 }
514
515 /**
516 * Account code generator if its empty
517 *
518 * @since 1.1.5
519 *
520 * @return int
521 */
522 function erp_ac_accounting_code_generator() {
523 $ledger = WeDevs\ERP\Accounting\Model\Ledger::select( 'code' )->get()->toArray();
524 $ledger = wp_list_pluck( $ledger, 'code' );
525 $code = random_int( 0, 999 );
526
527 if ( in_array( $code, $ledger ) ) {
528 erp_ac_accounting_code_generator();
529 }
530
531 return $code;
532 }
533