| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Accounting; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handle the form submissions |
| 7 |
* |
| 8 |
* @package Package |
| 9 |
* @subpackage Sub Package |
| 10 |
*/ |
| 11 |
class Form_Handler { |
| 12 |
public static $errors; |
| 13 |
|
| 14 |
/** |
| 15 |
* Hook 'em all |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'admin_init', array( $this, 'handle_customer_form' ) ); |
| 19 |
add_action( 'admin_init', array( $this, 'chart_form' ) ); |
| 20 |
add_action( 'admin_init', array( $this, 'report_filter' ) ); |
| 21 |
add_action( 'erp_action_ac-new-payment-voucher', array( $this, 'transaction_form' ) ); |
| 22 |
add_action( 'erp_action_ac-new-invoice', array( $this, 'transaction_form' ) ); |
| 23 |
add_action( 'erp_action_ac-new-sales-payment', array( $this, 'transaction_form' ) ); |
| 24 |
add_action( 'erp_action_ac-new-journal-entry', array( $this, 'journal_entry' ) ); |
| 25 |
|
| 26 |
$accounting = sanitize_title( __( 'Accounting', 'erp' ) ); |
| 27 |
add_action( "load-{$accounting}_page_erp-accounting-customers", array( $this, 'customer_bulk_action' ) ); |
| 28 |
add_action( "load-{$accounting}_page_erp-accounting-vendors", array( $this, 'vendor_bulk_action' ) ); |
| 29 |
add_action( "load-{$accounting}_page_erp-accounting-sales", array( $this, 'sales_bulk_action' ) ); |
| 30 |
add_action( "load-{$accounting}_page_erp-accounting-expense", array( $this, 'expense_bulk_action' ) ); |
| 31 |
add_action( 'load-{$accounting}_page_erp-accounting-journal', array( $this, 'journal_bulk_action' ) ); |
| 32 |
|
| 33 |
add_action( 'erp_hr_after_employee_permission_set', array( $this, 'employee_permission_set' ), 10, 2 ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Journal bulk action |
| 38 |
* |
| 39 |
* @since 1.1.6 |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function journal_bulk_action() { |
| 44 |
if ( ! $this->verify_current_page_screen( 'erp-accounting-journal', 'bulk-journals' ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$url = add_query_arg( array( |
| 49 |
'start_date' => isset( $_REQUEST['start_date'] ) ? $_REQUEST['start_date'] : '', |
| 50 |
'end_date' => isset( $_REQUEST['end_date'] ) ? $_REQUEST['end_date'] : '', |
| 51 |
'ref' => isset( $_REQUEST['ref'] ) ? $_REQUEST['ref'] : '', |
| 52 |
), erp_ac_get_journal_url() ); |
| 53 |
|
| 54 |
wp_safe_redirect( $url ); |
| 55 |
exit(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Filter report by date range |
| 60 |
* |
| 61 |
* @since 1.1.6 |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
function report_filter() { |
| 66 |
if ( ! isset( $_POST['erp_ac_report_filter'] ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp_ac_nonce_report' ) ) { |
| 70 |
return new \WP_Error( 'nonce', __( 'Error: Are you cheating!', 'erp' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$date = []; |
| 74 |
|
| 75 |
if ( isset( $_POST['start'] ) ) { |
| 76 |
$date['start'] = $_POST['start']; |
| 77 |
} |
| 78 |
|
| 79 |
if ( isset( $_POST['end'] ) ) { |
| 80 |
$date['end'] = $_POST['end']; |
| 81 |
} |
| 82 |
|
| 83 |
$url = add_query_arg( $date, $_POST['_wp_http_referer'] ); |
| 84 |
wp_safe_redirect( $url ); |
| 85 |
exit(); |
| 86 |
} |
| 87 |
|
| 88 |
function expense_bulk_action() { |
| 89 |
if ( ! $this->verify_current_page_screen( 'erp-accounting-expense', 'bulk-expenses' ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! isset( $_REQUEST['transaction_id'] ) || ! isset( $_REQUEST['submit_sales_bulk_action'] ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : ''; |
| 98 |
|
| 99 |
foreach ( $_REQUEST['transaction_id'] as $key => $trans_id ) { |
| 100 |
switch ( $action ) { |
| 101 |
case 'delete': |
| 102 |
erp_ac_remove_transaction( $trans_id ); |
| 103 |
break; |
| 104 |
case 'void': |
| 105 |
erp_ac_update_transaction_to_void( $trans_id ); |
| 106 |
break; |
| 107 |
|
| 108 |
default: |
| 109 |
erp_ac_update_transaction( $trans_id, [ 'status' => $action ] ); |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
wp_safe_redirect( $_REQUEST['_wp_http_referer'] ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Bulk action for sales list table |
| 119 |
* |
| 120 |
* @since 1.1.0 |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
function sales_bulk_action() { |
| 125 |
|
| 126 |
if ( ! $this->verify_current_page_screen( 'erp-accounting-sales', 'bulk-sales' ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! isset( $_REQUEST['transaction_id'] ) || ! isset( $_REQUEST['submit_sales_bulk_action'] ) ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : ''; |
| 135 |
|
| 136 |
foreach ( $_REQUEST['transaction_id'] as $key => $trans_id ) { |
| 137 |
switch ( $action ) { |
| 138 |
case 'delete': |
| 139 |
erp_ac_remove_transaction( $trans_id ); |
| 140 |
break; |
| 141 |
|
| 142 |
case 'void': |
| 143 |
erp_ac_update_transaction_to_void( $trans_id ); |
| 144 |
break; |
| 145 |
|
| 146 |
default: |
| 147 |
erp_ac_update_transaction( $trans_id, [ 'status' => $action ] ); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
wp_safe_redirect( $_REQUEST['_wp_http_referer'] ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Update employee role as accounting manager |
| 157 |
* |
| 158 |
* @param array $post |
| 159 |
* @param object $user |
| 160 |
* |
| 161 |
* @since 1.1.0 |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public static function employee_permission_set( $post, $user ) { |
| 166 |
$enable_ac_manager = isset( $post['ac_manager'] ) ? filter_var( $post['ac_manager'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 167 |
$ac_manager_role = erp_ac_get_manager_role(); |
| 168 |
|
| 169 |
// TODO::We are duplicating \WeDevs\ERP\Accounting\User_Profile->update_user() process here, |
| 170 |
// which we shouldn't. We should update above method and use that. |
| 171 |
if ( current_user_can( $ac_manager_role ) ) { |
| 172 |
if ( $enable_ac_manager ) { |
| 173 |
$user->add_role( $ac_manager_role ); |
| 174 |
} else { |
| 175 |
$user->remove_role( $ac_manager_role ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Bulk action for customer list table |
| 182 |
* |
| 183 |
* @since 1.1.0 |
| 184 |
* |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
function customer_bulk_action() { |
| 188 |
|
| 189 |
if ( ! $this->verify_current_page_screen( 'erp-accounting-customers', 'bulk-customers' ) ) { |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
if ( isset( $_POST['action'] ) && $_POST['action'] == '-1' ) { |
| 194 |
$this->bulk_search(); |
| 195 |
} |
| 196 |
|
| 197 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'trash' ) { |
| 198 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 199 |
$data = [ |
| 200 |
'id' => $customer_id, |
| 201 |
'hard' => 0, |
| 202 |
'type' => 'customer' |
| 203 |
]; |
| 204 |
erp_ac_customer_delete( $data ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'restore' ) { |
| 208 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 209 |
$data = [ |
| 210 |
'id' => $customer_id, |
| 211 |
'type' => 'customer' |
| 212 |
]; |
| 213 |
erp_restore_people( $data ); |
| 214 |
} |
| 215 |
|
| 216 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' ) { |
| 217 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 218 |
$data = [ |
| 219 |
'id' => $customer_id, |
| 220 |
'hard' => 1, |
| 221 |
'type' => 'customer' |
| 222 |
]; |
| 223 |
erp_ac_customer_delete( $data ); |
| 224 |
//erp_delete_people( $data ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Bulk action for vendor list table |
| 230 |
* |
| 231 |
* @since 1.1.0 |
| 232 |
* |
| 233 |
* @return void |
| 234 |
*/ |
| 235 |
function vendor_bulk_action() { |
| 236 |
|
| 237 |
if ( ! $this->verify_current_page_screen( 'erp-accounting-vendors', 'bulk-customers' ) ) { |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
if ( isset( $_POST['action'] ) && $_POST['action'] == '-1' ) { |
| 242 |
$this->bulk_search(); |
| 243 |
} |
| 244 |
|
| 245 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'trash' ) { |
| 246 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 247 |
$data = [ |
| 248 |
'id' => $customer_id, |
| 249 |
'hard' => 0, |
| 250 |
'type' => 'vendor' |
| 251 |
]; |
| 252 |
|
| 253 |
erp_ac_vendor_delete( $data ); |
| 254 |
} |
| 255 |
|
| 256 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'restore' ) { |
| 257 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 258 |
$data = [ |
| 259 |
'id' => $customer_id, |
| 260 |
'type' => 'vendor' |
| 261 |
]; |
| 262 |
erp_restore_people( $data ); |
| 263 |
} |
| 264 |
|
| 265 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' ) { |
| 266 |
$customer_id = isset( $_POST['customer_id'] ) && is_array( $_POST['customer_id'] ) ? $_POST['customer_id'] : false; |
| 267 |
$data = [ |
| 268 |
'id' => $customer_id, |
| 269 |
'hard' => 1, |
| 270 |
'type' => 'vendor' |
| 271 |
]; |
| 272 |
erp_ac_vendor_delete( $data ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Redirect after transaction list table submit for search |
| 278 |
* |
| 279 |
* @since 1.1.0 |
| 280 |
* |
| 281 |
* @return void |
| 282 |
*/ |
| 283 |
function bulk_search() { |
| 284 |
$redirect_to = add_query_arg( array( 's' => $_POST['s'] ), $_POST['_wp_http_referer'] ); |
| 285 |
wp_redirect( $redirect_to ); |
| 286 |
exit(); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Check is current page actions |
| 291 |
* |
| 292 |
* @since 0.1 |
| 293 |
* |
| 294 |
* @param integer $page_id |
| 295 |
* @param integer $bulk_action |
| 296 |
* |
| 297 |
* @return boolean |
| 298 |
*/ |
| 299 |
public function verify_current_page_screen( $page_id, $bulk_action ) { |
| 300 |
|
| 301 |
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! isset( $_GET['page'] ) ) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
if ( $_GET['page'] != $page_id ) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $bulk_action ) ) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* Handle the customer new and edit form |
| 319 |
* |
| 320 |
* @return void |
| 321 |
*/ |
| 322 |
public function handle_customer_form() { |
| 323 |
|
| 324 |
if ( ! isset( $_POST['submit_erp_ac_customer'] ) ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-ac-customer' ) ) { |
| 329 |
die( __( 'Are you cheating?', 'erp' ) ); |
| 330 |
} |
| 331 |
|
| 332 |
if ( ! current_user_can( 'read' ) ) { |
| 333 |
wp_die( __( 'Permission Denied!', 'erp' ) ); |
| 334 |
} |
| 335 |
|
| 336 |
$insert_id = erp_ac_new_customer( $_POST ); |
| 337 |
$message = __( 'new', 'erp' ); |
| 338 |
|
| 339 |
if ( $_POST['field_id'] ) { |
| 340 |
$message = __( 'update', 'erp' ); |
| 341 |
} |
| 342 |
|
| 343 |
if ( $_POST['type'] == 'customer' ) { |
| 344 |
$page_url = admin_url( 'admin.php?page=erp-accounting-customers' ); |
| 345 |
} else { |
| 346 |
$page_url = admin_url( 'admin.php?page=erp-accounting-vendors' ); |
| 347 |
} |
| 348 |
|
| 349 |
if ( is_wp_error( $insert_id ) ) { |
| 350 |
$redirect_to = add_query_arg( array( 'msg' => 'error' ), $page_url ); |
| 351 |
} else { |
| 352 |
$redirect_to = add_query_arg( array( 'msg' => $message ), $page_url ); |
| 353 |
} |
| 354 |
|
| 355 |
wp_safe_redirect( $redirect_to ); |
| 356 |
exit; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Handle the chart new and edit form |
| 361 |
* |
| 362 |
* @return void |
| 363 |
*/ |
| 364 |
public function chart_form() { |
| 365 |
if ( ! isset( $_POST['submit_erp_ac_chart'] ) ) { |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-ac-chart' ) ) { |
| 370 |
die( __( 'Are you cheating?', 'erp' ) ); |
| 371 |
} |
| 372 |
|
| 373 |
if ( ! current_user_can( 'read' ) ) { |
| 374 |
wp_die( __( 'Permission Denied!', 'erp' ) ); |
| 375 |
} |
| 376 |
|
| 377 |
$message = 'new'; |
| 378 |
$errors = array(); |
| 379 |
$page_url = admin_url( 'admin.php?page=erp-accounting-charts' ); |
| 380 |
$field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0; |
| 381 |
|
| 382 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : ''; |
| 383 |
$account_type_id = isset( $_POST['account_type_id'] ) ? sanitize_text_field( $_POST['account_type_id'] ) : ''; |
| 384 |
$code = isset( $_POST['code'] ) ? intval( $_POST['code'] ) : ''; |
| 385 |
$description = isset( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : 1; |
| 386 |
$active = isset( $_POST['active'] ) ? intval( $_POST['active'] ) : 1; |
| 387 |
|
| 388 |
// some basic validation |
| 389 |
if ( ! $field_id && Model\Ledger::code( $code )->get()->first() !== null ) { |
| 390 |
$errors[] = __( 'Error: The account code is already exists.', 'erp' ); |
| 391 |
} |
| 392 |
|
| 393 |
// some basic validation |
| 394 |
if ( $field_id && Model\Ledger::code( $code )->where( 'id', '!=', $field_id )->get()->first() !== null ) { |
| 395 |
$errors[] = __( 'Error: The account code is already exists.', 'erp' ); |
| 396 |
} |
| 397 |
|
| 398 |
if ( ! $name ) { |
| 399 |
$errors[] = __( 'Error: Name is required.', 'erp' ); |
| 400 |
} |
| 401 |
|
| 402 |
// bail out if error found |
| 403 |
if ( $errors ) { |
| 404 |
$first_error = reset( $errors ); |
| 405 |
$redirect_to = add_query_arg( array( 'error' => $first_error ), $page_url ); |
| 406 |
wp_safe_redirect( $redirect_to ); |
| 407 |
exit; |
| 408 |
} |
| 409 |
|
| 410 |
$fields = array( |
| 411 |
'code' => $code, |
| 412 |
'name' => $name, |
| 413 |
'description' => $description, |
| 414 |
'type_id' => $account_type_id, |
| 415 |
'active' => $active, |
| 416 |
); |
| 417 |
|
| 418 |
// bank account |
| 419 |
if ( $account_type_id == 6 ) { |
| 420 |
$fields['cash_account'] = 1; |
| 421 |
$fields['reconcile'] = 1; |
| 422 |
} |
| 423 |
|
| 424 |
// New or edit? |
| 425 |
if ( ! $field_id ) { |
| 426 |
|
| 427 |
$insert_id = erp_ac_insert_chart( $fields ); |
| 428 |
|
| 429 |
if ( $insert_id && $account_type_id == 6 ) { |
| 430 |
|
| 431 |
$ledger = Model\Ledger::find( $insert_id ); |
| 432 |
$ledger->bank_details()->create( [ |
| 433 |
'account_number' => sanitize_text_field( $_POST['bank']['account_number'] ), |
| 434 |
'bank_name' => sanitize_text_field( $_POST['bank']['bank_name'] ) |
| 435 |
] ); |
| 436 |
} |
| 437 |
|
| 438 |
} else { |
| 439 |
|
| 440 |
$fields['id'] = $field_id; |
| 441 |
$message = 'update'; |
| 442 |
$insert_id = erp_ac_insert_chart( $fields ); |
| 443 |
|
| 444 |
$ledger = Model\Ledger::find( $field_id ); |
| 445 |
$ledger->bank_details()->update( [ |
| 446 |
'account_number' => sanitize_text_field( $_POST['bank']['account_number'] ), |
| 447 |
'bank_name' => sanitize_text_field( $_POST['bank']['bank_name'] ) |
| 448 |
] ); |
| 449 |
} |
| 450 |
|
| 451 |
if ( is_wp_error( $insert_id ) ) { |
| 452 |
$redirect_to = add_query_arg( array( 'msg' => 'error' ), $page_url ); |
| 453 |
} else { |
| 454 |
$redirect_to = add_query_arg( array( 'msg' => $message ), $page_url ); |
| 455 |
} |
| 456 |
|
| 457 |
wp_safe_redirect( $redirect_to ); |
| 458 |
exit; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Transaction form data |
| 463 |
* |
| 464 |
* @since 1.1.0 |
| 465 |
* |
| 466 |
* @return void |
| 467 |
*/ |
| 468 |
public function transaction_form() { |
| 469 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-ac-trans-new' ) ) { |
| 470 |
die( __( 'Are you cheating?', 'erp' ) ); |
| 471 |
} |
| 472 |
|
| 473 |
if ( ! current_user_can( 'read' ) ) { |
| 474 |
wp_die( __( 'Permission Denied!', 'erp' ) ); |
| 475 |
} |
| 476 |
|
| 477 |
$insert_id = $this->transaction_data_process( $_POST ); |
| 478 |
$page_url = isset( $_POST['_wp_http_referer'] ) ? $_POST['_wp_http_referer'] : ''; |
| 479 |
|
| 480 |
if ( is_wp_error( $insert_id ) ) { |
| 481 |
self::$errors = $insert_id->get_error_message(); |
| 482 |
$redirect_to = add_query_arg( array( 'message' => $insert_id ), $page_url ); |
| 483 |
wp_safe_redirect( $redirect_to ); |
| 484 |
exit; |
| 485 |
|
| 486 |
} else { |
| 487 |
$redirect_to = add_query_arg( array( 'msg' => 'success' ), $page_url ); |
| 488 |
} |
| 489 |
|
| 490 |
if ( $_POST['redirect'] == 'same_page' ) { |
| 491 |
$redirect_to = remove_query_arg( [ 'transaction_id' ], wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 492 |
|
| 493 |
} else if ( $_POST['redirect'] == 'single_page' ) { |
| 494 |
|
| 495 |
if ( $_POST['type'] == 'sales' ) { |
| 496 |
$redirect_to = erp_ac_get_slaes_payment_invoice_url( $insert_id ); |
| 497 |
|
| 498 |
} else if ( $_POST['type'] == 'expense' ) { |
| 499 |
$redirect_to = erp_ac_get_expense_voucher_url( $insert_id ); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
$redirect_to = apply_filters( 'erp_ac_redirect_after_transaction', $redirect_to, $insert_id, $_POST ); |
| 504 |
wp_safe_redirect( $redirect_to ); |
| 505 |
exit; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Handle the transaction new and edit form |
| 510 |
* |
| 511 |
* @return void |
| 512 |
*/ |
| 513 |
public function transaction_data_process( $postdata ) { |
| 514 |
$status = erp_ac_get_btn_status( $postdata ); |
| 515 |
$errors = array(); |
| 516 |
$insert_id = 0; |
| 517 |
$field_id = isset( $postdata['field_id'] ) ? intval( $postdata['field_id'] ) : 0; |
| 518 |
$page = isset( $postdata['page'] ) ? sanitize_text_field( $postdata['page'] ) : ''; |
| 519 |
$type = isset( $postdata['type'] ) ? sanitize_text_field( $postdata['type'] ) : ''; |
| 520 |
$form_type = isset( $postdata['form_type'] ) ? sanitize_text_field( $postdata['form_type'] ) : ''; |
| 521 |
$account_id = isset( $postdata['account_id'] ) ? intval( $postdata['account_id'] ) : 0; |
| 522 |
$status = isset( $postdata['status'] ) ? sanitize_text_field( $postdata['status'] ) : 'closed'; |
| 523 |
$user_id = isset( $postdata['user_id'] ) ? intval( $postdata['user_id'] ) : 0; |
| 524 |
$billing_address = isset( $postdata['billing_address'] ) ? wp_kses_post( $postdata['billing_address'] ) : ''; |
| 525 |
$ref = isset( $postdata['ref'] ) ? sanitize_text_field( $postdata['ref'] ) : ''; |
| 526 |
$issue_date = isset( $postdata['issue_date'] ) ? sanitize_text_field( $postdata['issue_date'] ) : ''; |
| 527 |
$due_date = isset( $postdata['due_date'] ) ? sanitize_text_field( $postdata['due_date'] ) : ''; |
| 528 |
$summary = isset( $postdata['summary'] ) ? wp_kses_post( $postdata['summary'] ) : ''; |
| 529 |
$total = isset( $postdata['price_total'] ) ? sanitize_text_field( $postdata['price_total'] ) : ''; |
| 530 |
$files = isset( $postdata['files'] ) ? maybe_serialize( $postdata['files'] ) : ''; |
| 531 |
$currency = isset( $postdata['currency'] ) ? sanitize_text_field( $postdata['currency'] ) : 'USD'; |
| 532 |
$transaction_id = isset( $postdata['id'] ) ? $postdata['id'] : false; |
| 533 |
$line_account = isset( $postdata['line_account'] ) ? $postdata['line_account'] : array(); |
| 534 |
$page_url = admin_url( 'admin.php?page=' . $page ); |
| 535 |
$items_id = isset( $postdata['items_id'] ) ? $postdata['items_id'] : []; |
| 536 |
$journals_id = isset( $postdata['journals_id'] ) ? $postdata['journals_id'] : []; |
| 537 |
$partial_id = isset( $postdata['partial_id'] ) ? $postdata['partial_id'] : []; |
| 538 |
$sub_total = isset( $postdata['sub_total'] ) ? $postdata['sub_total'] : '0.00'; |
| 539 |
$invoice = isset( $postdata['invoice'] ) ? $postdata['invoice'] : 0; |
| 540 |
|
| 541 |
//clean price data and format as default setup |
| 542 |
$total = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $total ), 2 ); |
| 543 |
$sub_total = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $sub_total ), 2 ); |
| 544 |
|
| 545 |
// some basic validation |
| 546 |
if ( ! $issue_date ) { |
| 547 |
return new \WP_Error( 'required_issue_date', __( 'Error: Issue Date is required', 'erp' ) ); |
| 548 |
} |
| 549 |
|
| 550 |
if ( ! $account_id ) { |
| 551 |
return new \WP_Error( 'required_account_id', __( 'Error: Account ID is required', 'erp' ) ); |
| 552 |
} |
| 553 |
|
| 554 |
if ( ! $total ) { |
| 555 |
return new \WP_Error( 'required_total_amount', __( 'Error: Total is required', 'erp' ) ); |
| 556 |
} |
| 557 |
|
| 558 |
if ( $total < 1 ) { |
| 559 |
return new \WP_Error( 'ntotal_amount_greater_than_zero', __( 'Error: Total amount should be greater than zero', 'erp' ) ); |
| 560 |
} |
| 561 |
|
| 562 |
$fields = [ |
| 563 |
'id' => $transaction_id, |
| 564 |
'partial_id' => $partial_id, |
| 565 |
'items_id' => $items_id, |
| 566 |
'journals_id' => $journals_id, |
| 567 |
'type' => $type, |
| 568 |
'form_type' => $form_type, |
| 569 |
'account_id' => $account_id, |
| 570 |
'status' => $status, |
| 571 |
'user_id' => $user_id, |
| 572 |
'billing_address' => $billing_address, |
| 573 |
'ref' => $ref, |
| 574 |
'issue_date' => $issue_date, |
| 575 |
'due_date' => $due_date, |
| 576 |
'summary' => $summary, |
| 577 |
'total' => $total, |
| 578 |
'sub_total' => $sub_total, |
| 579 |
'invoice_number' => $invoice, |
| 580 |
'trans_total' => $total, |
| 581 |
'files' => $files, |
| 582 |
'currency' => $currency, |
| 583 |
'line_total' => isset( $postdata['line_total'] ) ? array_map( 'erp_ac_remove_thousand_sep', $postdata['line_total'] ) : array() |
| 584 |
]; |
| 585 |
|
| 586 |
// set invoice and vendor credit for due to full amount |
| 587 |
if ( $this->is_due_trans( $form_type, $postdata ) ) { //in_array( $form_type, [ 'invoice', 'vendor_credit' ] ) ) { |
| 588 |
$fields['due'] = $total; |
| 589 |
} |
| 590 |
|
| 591 |
$items = []; |
| 592 |
foreach ( $line_account as $key => $acc_id ) { |
| 593 |
$line_total = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['line_total'][ $key ] ), 2 ); |
| 594 |
if ( ! $acc_id || ! $line_total ) { |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
$unit_price = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['line_unit_price'][ $key ] ), 2 ); |
| 599 |
$tax_rate = 0; |
| 600 |
$line_tax = 0; |
| 601 |
$tax_amount = 0; |
| 602 |
|
| 603 |
if ( isset( $postdata['tax_rate'] ) ) { |
| 604 |
$tax_rate = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['tax_rate'][ $key ] ), 2 ); |
| 605 |
} |
| 606 |
|
| 607 |
if ( isset( $postdata['line_tax'] )) { |
| 608 |
$lien_tax = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['line_tax'][ $key ] ), 2 ); |
| 609 |
} |
| 610 |
|
| 611 |
if ( isset( $postdata['tax_amount'] )) { |
| 612 |
$lien_tax = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['tax_amount'][ $key ] ), 2 ); |
| 613 |
} |
| 614 |
|
| 615 |
$line_discount = erp_ac_format_decimal( erp_ac_remove_thousand_sep( $postdata['line_discount'][ $key ] ), 2 ); |
| 616 |
|
| 617 |
$items[] = apply_filters( 'erp_ac_transaction_lines', [ |
| 618 |
'item_id' => isset( $postdata['items_id'][ $key ] ) ? $postdata['items_id'][ $key ] : [], |
| 619 |
'journal_id' => isset( $postdata['journals_id'][ $key ] ) ? $postdata['journals_id'][ $key ] : [], |
| 620 |
'account_id' => (int) $acc_id, |
| 621 |
'description' => sanitize_text_field( $postdata['line_desc'][ $key ] ), |
| 622 |
'qty' => $postdata['line_qty'][ $key ], |
| 623 |
'unit_price' => $unit_price, |
| 624 |
'discount' => $line_discount, |
| 625 |
'tax' => isset( $postdata['line_tax'][ $key ] ) ? $line_tax : 0, |
| 626 |
'tax_rate' => isset( $postdata['tax_rate'][ $key ] ) ? $tax_rate : 0, |
| 627 |
'tax_amount' => isset( $postdata['tax_amount'][ $key ] ) ? $tax_amount : 0, |
| 628 |
'line_total' => $line_total, |
| 629 |
'tax_journal' => isset( $postdata['tax_journal'][ $key ] ) ? $postdata['tax_journal'][ $key ] : 0 |
| 630 |
], $key, $postdata ); |
| 631 |
} |
| 632 |
|
| 633 |
// New or edit? |
| 634 |
if ( ! $field_id ) { |
| 635 |
$insert_id = erp_ac_insert_transaction( $fields, $items ); |
| 636 |
} |
| 637 |
|
| 638 |
return $insert_id; |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Check is the payment type partial or not |
| 643 |
* |
| 644 |
* @param array $trans |
| 645 |
* |
| 646 |
* @return boolen |
| 647 |
*/ |
| 648 |
function is_due_trans( $form_type, $postdata ) { |
| 649 |
$due = apply_filters( 'erp_ac_is_due_trans', [ 'invoice', 'vendor_credit' ], $postdata ); |
| 650 |
|
| 651 |
if ( in_array( $form_type, $due ) ) { |
| 652 |
return true; |
| 653 |
} |
| 654 |
|
| 655 |
return false; |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
* New journal |
| 660 |
* |
| 661 |
* @since 1.1.0 |
| 662 |
* |
| 663 |
* @return boolen |
| 664 |
*/ |
| 665 |
public function journal_entry() { |
| 666 |
if ( ! erp_ac_create_journal() ) { |
| 667 |
return new \WP_Error( 'error', __( 'You do not have sufficient permissions', 'erp' ) ); |
| 668 |
} |
| 669 |
|
| 670 |
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'erp-ac-journal-entry' ) ) { |
| 671 |
die( __( 'Are you cheating?', 'erp' ) ); |
| 672 |
} |
| 673 |
|
| 674 |
$thousand_seperator = erp_ac_get_price_thousand_separator(); |
| 675 |
$ref = isset( $_POST['ref'] ) ? sanitize_text_field( $_POST['ref'] ) : ''; |
| 676 |
$issue_date = isset( $_POST['issue_date'] ) ? sanitize_text_field( $_POST['issue_date'] ) : ''; |
| 677 |
$summary = isset( $_POST['summary'] ) ? sanitize_text_field( $_POST['summary'] ) : ''; |
| 678 |
$debit_total = isset( $_POST['debit_total'] ) ? str_replace( $thousand_seperator, '', $_POST['debit_total'] ) : 0.00; |
| 679 |
$credit_total = isset( $_POST['credit_total'] ) ? str_replace( $thousand_seperator, '', $_POST['credit_total'] ) : 0.00; |
| 680 |
|
| 681 |
if ( $debit_total < 0 || $credit_total < 0 ) { |
| 682 |
wp_die( __( 'Value can not be negative', 'erp' ) ); |
| 683 |
} |
| 684 |
|
| 685 |
if ( $debit_total != $credit_total ) { |
| 686 |
wp_die( __( 'Debit and credit total did not match.', 'erp' ) ); |
| 687 |
} |
| 688 |
|
| 689 |
$args = [ |
| 690 |
'id' => isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false, |
| 691 |
'type' => 'journal', |
| 692 |
'ref' => $ref, |
| 693 |
'summary' => $summary, |
| 694 |
'issue_date' => $issue_date, |
| 695 |
]; |
| 696 |
|
| 697 |
$items = []; |
| 698 |
foreach ( $_POST['journal_account'] as $key => $account_id ) { |
| 699 |
$debit = floatval( $_POST['line_debit'][ $key ] ); |
| 700 |
$credit = floatval( $_POST['line_credit'][ $key ] ); |
| 701 |
$des = isset( $_POST['line_desc'][ $key ] ) ? $_POST['line_desc'][ $key ] : ''; |
| 702 |
|
| 703 |
if ( $debit ) { |
| 704 |
$type = 'debit'; |
| 705 |
$amount = $debit; |
| 706 |
} else { |
| 707 |
$type = 'credit'; |
| 708 |
$amount = $credit; |
| 709 |
} |
| 710 |
|
| 711 |
$items[] = [ |
| 712 |
'item_id' => isset( $_POST['item_id'][ $key ] ) ? intval( $_POST['item_id'][ $key ] ) : false, |
| 713 |
'journal_id' => isset( $_POST['journal_id'][ $key ] ) ? intval( $_POST['journal_id'][ $key ] ) : false, |
| 714 |
'ledger_id' => (int) $account_id, |
| 715 |
$type => $amount, |
| 716 |
'description' => $des |
| 717 |
]; |
| 718 |
} |
| 719 |
|
| 720 |
erp_ac_new_journal( $args, $items ); |
| 721 |
|
| 722 |
$location = admin_url( 'admin.php?page=erp-accounting-journal&msg=success' ); |
| 723 |
wp_redirect( $location ); |
| 724 |
} |
| 725 |
} |
| 726 |
|