예제 #1
0
 /**
  * Handle the transaction new and edit form
  *
  * @return void
  */
 public function transaction_form()
 {
     if (!wp_verify_nonce($_POST['_wpnonce'], 'erp-ac-trans-new')) {
         die(__('Are you cheating?', 'erp-accounting'));
     }
     if (!current_user_can('read')) {
         wp_die(__('Permission Denied!', 'erp-accounting'));
     }
     $errors = array();
     $field_id = isset($_POST['field_id']) ? intval($_POST['field_id']) : 0;
     //$invoice_payment = isset( $_POST['invoice_payment'] ) && $_POST['invoice_payment'] ? $_POST['invoice_payment'] : false;
     $page = isset($_POST['page']) ? sanitize_text_field($_POST['page']) : '';
     $type = isset($_POST['type']) ? sanitize_text_field($_POST['type']) : '';
     $form_type = isset($_POST['form_type']) ? sanitize_text_field($_POST['form_type']) : '';
     $account_id = isset($_POST['account_id']) ? intval($_POST['account_id']) : 0;
     $status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : 'closed';
     $user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
     $billing_address = isset($_POST['billing_address']) ? wp_kses_post($_POST['billing_address']) : '';
     $ref = isset($_POST['ref']) ? sanitize_text_field($_POST['ref']) : '';
     $issue_date = isset($_POST['issue_date']) ? sanitize_text_field($_POST['issue_date']) : '';
     $due_date = isset($_POST['due_date']) ? sanitize_text_field($_POST['due_date']) : '';
     $summary = isset($_POST['summary']) ? wp_kses_post($_POST['summary']) : '';
     $total = isset($_POST['price_total']) ? sanitize_text_field($_POST['price_total']) : '';
     $files = isset($_POST['files']) ? sanitize_text_field($_POST['files']) : '';
     $currency = isset($_POST['currency']) ? sanitize_text_field($_POST['currency']) : 'USD';
     $line_account = isset($_POST['line_account']) ? $_POST['line_account'] : array();
     $page_url = admin_url('admin.php?page=' . $page);
     // some basic validation
     if (!$issue_date) {
         $errors[] = __('Error: Issue Date is required', 'erp-accounting');
     }
     if (!$account_id) {
         $errors[] = __('Error: Account ID is required', 'erp-accounting');
     }
     if (!$total) {
         $errors[] = __('Error: Total is required', 'erp-accounting');
     }
     // bail out if error found
     if ($errors) {
         $first_error = reset($errors);
         $redirect_to = add_query_arg(array('error' => $first_error), $page_url);
         wp_safe_redirect($redirect_to);
         exit;
     }
     $fields = ['type' => $type, 'form_type' => $form_type, 'account_id' => $account_id, 'status' => $status, 'user_id' => $user_id, 'billing_address' => $billing_address, 'ref' => $ref, 'issue_date' => $issue_date, 'due_date' => $due_date, 'summary' => $summary, 'total' => $total, 'trans_total' => $total, 'files' => $files, 'currency' => $currency];
     // set invoice and vendor credit due to full amount
     if (in_array($form_type, ['invoice', 'vendor_credit'])) {
         $fields['due'] = $total;
     }
     $items = [];
     foreach ($line_account as $key => $acc_id) {
         $line_total = (double) $_POST['line_total'][$key];
         if (!$acc_id || !$line_total) {
             continue;
         }
         $items[] = ['account_id' => (int) $acc_id, 'description' => sanitize_text_field($_POST['line_desc'][$key]), 'qty' => intval($_POST['line_qty'][$key]), 'unit_price' => floatval($_POST['line_unit_price'][$key]), 'discount' => floatval($_POST['line_discount'][$key]), 'line_total' => $line_total];
     }
     // New or edit?
     if (!$field_id) {
         $insert_id = erp_ac_insert_transaction($fields, $items);
         if ($_POST['form_type'] == 'payment') {
             $transaction_ids = isset($_POST['transaction_id']) ? $_POST['transaction_id'] : array();
             foreach ($transaction_ids as $key => $id) {
                 $line_total = isset($_POST['line_total'][$key]) ? $_POST['line_total'][$key] : 0;
                 $transaction = erp_ac_get_transaction($id);
                 $due = $transaction['due'];
                 if ($line_total > $due) {
                     continue;
                 }
                 $new_due = $due - $line_total;
                 if ($new_due <= 0) {
                     $update_field['status'] = 'closed';
                 }
                 $update_field['due'] = $new_due;
                 \WeDevs\ERP\Accounting\Model\Transaction::find($id)->update($update_field);
                 \WeDevs\ERP\Accounting\Model\Payment::create(array('transaction_id' => $insert_id, 'parent' => 0, 'child' => $id));
             }
         }
     }
     if (is_wp_error($insert_id)) {
         $redirect_to = add_query_arg(array('msg' => $insert_id->get_error_message()), $page_url);
     } else {
         $redirect_to = add_query_arg(array('msg' => 'success'), $page_url);
     }
     wp_safe_redirect($redirect_to);
     exit;
 }
 public function column_user_id($item)
 {
     $url = admin_url('admin.php?page=' . $this->slug . '&action=view&id=' . $item->id);
     $user_display_name = '';
     $actions = array();
     // $actions['view']   = sprintf( '<a href="%s" data-id="%d" title="%s">%s</a>', admin_url( 'admin.php?page=' . $this->slug . '&action=view&id=' . $item->id ), $item->id, __( 'View this transaction', 'erp-accounting' ), __( 'View', 'erp-accounting' ) );
     // $actions['delete'] = sprintf( '<a href="%s" class="submitdelete" data-id="%d" title="%s">%s</a>', admin_url( 'admin.php?page=' . $this->slug . '&action=delete&id=' . $item->id ), $item->id, __( 'Delete this item', 'erp-accounting' ), __( 'Delete', 'erp-accounting' ) );
     if (!$item->user_id) {
         $user_display_name = __('(no vendor)', 'erp-accounting');
     } else {
         $transaction = \WeDevs\ERP\Accounting\Model\Transaction::find($item->id);
         $user_display_name = $transaction->user->first_name . ' ' . $transaction->user->last_name;
     }
     return sprintf('<a href="%1$s">%2$s</a> %3$s', $url, $user_display_name, $this->row_actions($actions));
 }