Пример #1
0
 /**
  * Handle the chart new and edit form
  *
  * @return void
  */
 public function chart_form()
 {
     if (!isset($_POST['submit_erp_ac_chart'])) {
         return;
     }
     if (!wp_verify_nonce($_POST['_wpnonce'], 'erp-ac-chart')) {
         die(__('Are you cheating?', 'erp-accounting'));
     }
     if (!current_user_can('read')) {
         wp_die(__('Permission Denied!', 'erp-accounting'));
     }
     $message = 'new';
     $errors = array();
     $page_url = admin_url('admin.php?page=erp-accounting-charts');
     $field_id = isset($_POST['field_id']) ? intval($_POST['field_id']) : 0;
     $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
     $account_type_id = isset($_POST['account_type_id']) ? sanitize_text_field($_POST['account_type_id']) : '';
     $code = isset($_POST['code']) ? intval($_POST['code']) : '';
     $description = isset($_POST['description']) ? sanitize_text_field($_POST['description']) : 1;
     $active = isset($_POST['active']) ? intval($_POST['active']) : 1;
     // some basic validation
     if (Model\Ledger::code($code)->get()->first() !== null) {
         $errors[] = __('Error: The account code is already exists.', 'erp-accounting');
     }
     if (!$name) {
         $errors[] = __('Error: Name 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 = array('code' => $code, 'name' => $name, 'type_id' => $account_type_id, 'active' => $active);
     // bank account
     if ($account_type_id == 6) {
         $fields['cash_account'] = 1;
         $fields['reconcile'] = 1;
     }
     // New or edit?
     if (!$field_id) {
         $insert_id = erp_ac_insert_chart($fields);
         if ($insert_id && $account_type_id == 6) {
             $ledger = Model\Ledger::find($insert_id);
             $ledger->bank_details()->create(['account_number' => sanitize_text_field($_POST['bank']['account_number']), 'bank_name' => sanitize_text_field($_POST['bank']['bank_name'])]);
         }
     } else {
         $fields['id'] = $field_id;
         $message = 'update';
         $insert_id = erp_ac_insert_chart($fields);
     }
     if (is_wp_error($insert_id)) {
         $redirect_to = add_query_arg(array('msg' => 'error'), $page_url);
     } else {
         $redirect_to = add_query_arg(array('msg' => $message), $page_url);
     }
     wp_safe_redirect($redirect_to);
     exit;
 }
Пример #2
0
 public function page_chart_of_accounting()
 {
     $action = isset($_GET['action']) ? $_GET['action'] : 'list';
     $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
     switch ($action) {
         case 'view':
             $ledger = Model\Ledger::find($id);
             $template = dirname(__FILE__) . '/views/accounts/single.php';
             break;
         case 'edit':
             $template = dirname(__FILE__) . '/views/accounts/edit.php';
             break;
         case 'new':
             $template = dirname(__FILE__) . '/views/accounts/new.php';
             break;
         default:
             $template = dirname(__FILE__) . '/views/chart-of-accounts.php';
             break;
     }
     if (file_exists($template)) {
         include $template;
     }
 }