Exemplo n.º 1
0
 function transfer_money()
 {
     $this->verify_nonce('erp-ac-nonce');
     $from = intval($_POST['form_account_id']);
     $to = intval($_POST['to_account_id']);
     $amount = floatval($_POST['amount']);
     $debit_credit = erp_ac_bank_credit_total_amount($from);
     $ledger_amount = abs($debit_credit['debit'] - $debit_credit['credit']);
     if ($ledger_amount < $to) {
         $this->send_error(__('No enough money from your transfer account', 'wp-account'));
     }
     $args = array('type' => 'transfer', 'form_type' => 'bank', 'status' => 'closed', 'account_id' => $from, 'user_id' => get_current_user_id(), 'billing_address' => '', 'ref' => '', 'issue_date' => $_POST['date'], 'summary' => sanitize_text_field($_POST['memo']), 'total' => $amount, 'currency' => erp_ac_get_currency(), 'created_by' => get_current_user_id(), 'created_at' => current_time('mysql'));
     $items[] = array('account_id' => $to, 'type' => 'line_item', 'line_total' => $amount, 'description' => '', 'qty' => 1, 'unit_price' => $amount, 'discount' => 0);
     $transaction_id = erp_ac_insert_transaction($args, $items);
     if ($transaction_id) {
         $this->send_success();
     }
     $this->send_error();
 }
Exemplo n.º 2
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;
 }