function receive_payment() { $this->verify_nonce('erp-ac-nonce'); $user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : false; $account_id = isset($_POST['account_id']) ? intval($_POST['account_id']) : false; if (!$user_id) { $this->send_error(); } $transaction = new \WeDevs\ERP\Accounting\Model\Transaction(); $today = current_time('mysql'); $transaction_res = $transaction->where(function ($condition) use($today, $user_id) { $condition->where('due_date', '<', $today); $condition->where('form_type', '=', 'invoice'); $condition->where('status', '=', 'awaiting_payment'); $condition->where('user_id', '=', $user_id); $condition->where('parent', '=', 0); }); $results = $transaction_res->get()->toArray(); if (!$results) { $this->send_error(); } $total_due = 0; ob_start(); include_once WPERP_ACCOUNTING_VIEWS . '/bank-ransfer-form.php'; $this->send_success(ob_get_clean()); }
function erp_ac_get_expense_plot() { global $wpdb; $journal_table = $wpdb->prefix . 'erp_ac_journals'; $transaction_table = $wpdb->prefix . 'erp_ac_transaction'; $transaction = new \WeDevs\ERP\Accounting\Model\Transaction(); $last_date = current_time('mysql'); $first_date = date('Y-m-d', strtotime('-30 day', strtotime($last_date))); $transaction_res = $transaction->with(['journals' => function ($q) { $q->where('type', '=', 'main'); }]); $transaction_res = $transaction_res->where(function ($condition) use($first_date, $last_date) { $condition->where('issue_date', '>', $first_date); $condition->where('form_type', '=', 'payment_voucher'); $condition->where('status', '=', 'paid'); }); $results = $transaction_res->get()->toArray(); return $results; }
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); }
<div class="wrap"> <h2><?php echo $customer->get_full_name(); ?> <a href="#" class="add-new-h2"><?php _e('Edit', 'erp-accounting'); ?> </a></h2> <?php // var_dump( $customer ); $current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'transactions'; $trans = WeDevs\ERP\Accounting\Model\Transaction::OfUser($customer->id)->with('items')->get(); if ($trans) { foreach ($trans as $transaction) { // var_dump( $transaction->items ); } } // var_dump( $trans->toArray() ); ?> <h2 class="nav-tab-wrapper erp-nav-tab-wrapper" style="margin: 20px 0;"> <a class="nav-tab<?php echo $current_tab == 'transactions' ? ' nav-tab-active' : ''; ?> " href="<?php echo admin_url('admin.php?page=erp-accounting-customers&action=view&id=' . $id . '&tab=transactions'); ?> "><?php _e('Transactions', 'erp-accounting'); ?>
/** * 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; }