public function column_form_type($item) { $types = erp_ac_get_sales_form_types(); if (array_key_exists($item->form_type, $types)) { return sprintf('<a href="%1$s">%2$s</a>', admin_url('admin.php?page=' . $this->slug . '&action=view&id=' . $item->id), $types[$item->form_type]['label']); } }
<div class="wrap"> <h2> <?php _e('Sales Transactions', 'erp-accounting'); ?> <?php $form_types = erp_ac_get_sales_form_types(); if ($form_types) { foreach ($form_types as $key => $form) { printf('<a class="add-new-h2" href="%s%s" title="%s">%s</a> ', admin_url('admin.php?page=erp-accounting-sales&action=new&type='), $key, esc_attr($form['description']), $form['label']); } } ?> </h2> <form method="get"> <input type="hidden" name="page" value="erp-accounting-sales"> <?php $list_table = new WeDevs\ERP\Accounting\Sales_Transaction_List_Table(); $list_table->prepare_items(); $list_table->views(); $list_table->display(); ?> </form> </div>
/** * 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; }