public function journal_entry() { global $wpdb; if (!wp_verify_nonce($_POST['_wpnonce'], 'erp-ac-journal-entry')) { die(__('Are you cheating?', 'erp-accounting')); } $ref = isset($_POST['ref']) ? sanitize_text_field($_POST['ref']) : ''; $issue_date = isset($_POST['issue_date']) ? sanitize_text_field($_POST['issue_date']) : ''; $summary = isset($_POST['summary']) ? sanitize_text_field($_POST['summary']) : ''; $debit_total = isset($_POST['debit_total']) ? floatval($_POST['debit_total']) : 0.0; $credit_total = isset($_POST['credit_total']) ? floatval($_POST['credit_total']) : 0.0; if ($debit_total < 0 || $credit_total < 0) { wp_die(__('Value can not be negative', 'erp-accounting')); } if ($debit_total != $credit_total) { wp_die(__('Debit and credit total did not match.', 'erp-accounting')); } $args = ['type' => 'journal', 'ref' => $ref, 'summary' => $summary, 'issue_date' => $issue_date, 'total' => $debit_total, 'conversion_rate' => 1, 'trans_total' => $debit_total, 'created_by' => get_current_user_id(), 'created_at' => current_time('mysql')]; try { $wpdb->query('START TRANSACTION'); $transaction = new \WeDevs\ERP\Accounting\Model\Transaction(); $trans = $transaction->create($args); if (!$trans->id) { throw new Exception(__('Could not create transaction', 'erp-accounting')); } // insert items $order = 1; foreach ($_POST['line_account'] as $key => $account_id) { $debit = floatval($_POST['line_debit'][$key]); $credit = floatval($_POST['line_credit'][$key]); if ($debit) { $type = 'debit'; $amount = $debit; } else { $type = 'credit'; $amount = $credit; } $journal = $trans->journals()->create(['ledger_id' => $account_id, 'type' => 'line_item', $type => $amount]); if (!$journal->id) { throw new Exception(__('Could not insert journal item', 'erp-accounting')); } $trans_item = $trans->items()->create(['journal_id' => $journal->id, 'product_id' => null, 'description' => sanitize_text_field($_POST['line_desc'][$key]), 'qty' => 1, 'unit_price' => $amount, 'discount' => 0, 'line_total' => $amount, 'order' => $order]); if (!$trans_item->id) { throw new Exception(__('Could not insert transaction item', 'erp-accounting')); } $order++; } $wpdb->query('COMMIT'); } catch (Exception $e) { $wpdb->query('ROLLBACK'); wp_die($e->getMessage()); } $location = admin_url('admin.php?page=erp-accounting-journal&msg=success'); wp_redirect($location); }
/** * Insert a new transaction * * @param array $args */ function erp_ac_insert_transaction($args = [], $items = []) { global $wpdb; if (!$items) { return new WP_Error('no-items', __('No transaction items found', 'erp-accounting')); } $defaults = array('id' => null, 'type' => '', 'form_type' => '', 'account_id' => '', 'status' => '', 'user_id' => '', 'billing_address' => '', 'ref' => '', 'issue_date' => '', 'summary' => '', 'total' => '', 'files' => '', 'currency' => '', 'created_by' => get_current_user_id(), 'created_at' => current_time('mysql')); $args = wp_parse_args($args, $defaults); $table_name = $wpdb->prefix . 'erp_ac_transactions'; // get valid transaction type and form type if (!in_array($args['type'], ['expense', 'sales', 'transfer'])) { return new WP_Error('invalid-trans-type', __('Error: Invalid transaction type.', 'erp-accounting')); } $form_types = $args['type'] == 'expense' ? erp_ac_get_expense_form_types() : erp_ac_get_sales_form_types(); if ($args['type'] == 'expense') { $form_types = erp_ac_get_expense_form_types(); } else { if ($args['type'] == 'transfer') { $form_types = erp_ac_get_bank_form_types(); } else { $form_types = erp_ac_get_sales_form_types(); } } if (!array_key_exists($args['form_type'], $form_types)) { return new WP_Error('invalid-form-type', __('Error: Invalid form type', 'erp-accounting')); } $form_type = $form_types[$args['form_type']]; // some basic validation if (empty($args['issue_date'])) { return new WP_Error('no-issue_date', __('No Issue Date provided.', 'erp-accounting')); } if (empty($args['total'])) { return new WP_Error('no-total', __('No Total provided.', 'erp-accounting')); } // remove row id to determine if new or update $row_id = (int) $args['id']; $main_account_id = (int) $args['account_id']; unset($args['id']); unset($args['account_id']); // BEGIN INSERTION try { $wpdb->query('START TRANSACTION'); $transaction = new WeDevs\ERP\Accounting\Model\Transaction(); $trans = $transaction->create($args); if (!$trans->id) { throw new Exception(__('Could not create transaction', 'erp-accounting')); } // create the main journal entry $main_journal = $trans->journals()->create(['ledger_id' => $main_account_id, 'type' => 'main', $form_type['type'] => $args['total']]); if (!$main_journal->id) { throw new Exception(__('Could not insert main journal item', 'erp-accounting')); } // enter the transaction items $order = 1; $item_entry_type = $form_type['type'] == 'credit' ? 'debit' : 'credit'; //$item_entry_type = $args['invoice_payment'] ? 'credit' : $item_entry_type; foreach ($items as $item) { $journal = $trans->journals()->create(['ledger_id' => $item['account_id'], 'type' => 'line_item', $item_entry_type => $item['line_total']]); if (!$journal->id) { throw new Exception(__('Could not insert journal item', 'erp-accounting')); } $trans_item = $trans->items()->create(['journal_id' => $journal->id, 'product_id' => null, 'description' => $item['description'], 'qty' => $item['qty'], 'unit_price' => $item['unit_price'], 'discount' => $item['discount'], 'line_total' => $item['line_total'], 'order' => $order]); if (!$trans_item->id) { throw new Exception(__('Could not insert transaction item', 'erp-accounting')); } $order++; } $wpdb->query('COMMIT'); return $trans->id; } catch (Exception $e) { $wpdb->query('ROLLBACK'); return new WP_error('final-exception', $e->getMessage()); } return false; }