<commit_before><?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Secrets
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		<NAME>
 * @link		http://philsturgeon.co.uk/code/
 */

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';
error_reporting(!E_DEPRECATED);
class Money_Tracker extends REST_Controller{

    private $_db_config;
    private $_db_config_file = '/../../config/money-tracker.db_config.php';
    private $_origin;
    private $_model_dir = 'money_tracker/';

    public function __construct(){
        parent::__construct();
        if(!file_exists(__DIR__.$this->_db_config_file)){
            $this->send_response('DB config file not found');
        } else {
            $this->_db_config = require_once(__DIR__.$this->_db_config_file);
        }
    }

    public function delete_get() {
        // TODO - test
        $this->validate_access();

        $id = $this->get('id');
        $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
        $attachment_id = $this->get('attachment');
        if(!empty($attachment_id)){
            $this->Attachment->delete($id, $attachment_id);
        } else {
            $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
            $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);
            $this->Attachment->delete($id);
            $entry_value = $this->Entry->delete($id);
            $account_id = $this->Account->get_account_id_from_entry($id);
            $this->Account->update_balance(-1*$entry_value, $account_id);
        }
        $this->send_response(1, __FUNCTION__);
    }

    public function entry_get() {
        $this->validate_access();

        $id = $this->get('id');
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $entry = $this->Entry->get($id);
        if(empty($entry)){
            $this->send_response('Entry not found');
        }
        if($entry['has_attachment']){
            $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
            $attachments = $this->Attachment->get_entry($id);
            if(empty($attachments)){
                $entry['has_attachment']=0;
                $entry['attachments'] = array();
            } else {
                $entry['attachments'] = $attachments;
            }
        }

        $tag_ids = json_decode($entry['tags'], true);
        $entry['tags'] = $this->Entry->get_select_tags('id', $tag_ids);
        $this->send_response($entry, __FUNCTION__);
    }

    public function count_post() {
        $this->validate_access();

        $where_array = $this->process_where_array(json_decode(base64_decode($this->post('where')), true));
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $total_entries = $this->Entry->count($where_array);
        $this->send_response($total_entries, __FUNCTION__);
    }

    public function list_post() {
        $this->validate_access();

        $limit = $this->post('limit');
        $start = $this->post('start');
        $where_array = $this->process_where_array(json_decode(base64_decode($this->post('where')), true));
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $entries = $this->Entry->list_entries($where_array, $start, $limit);

        $this->send_response($entries, __FUNCTION__);
    }

    public function list_accounts_get(){
        $this->validate_access();

        $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);
        $accounts = $this->Account->list_accounts();
        $this->send_response($accounts, __FUNCTION__);
    }

    public function save_post() {
        $this->validate_access();

        $entry_data = json_decode(base64_decode($this->post('data')), true);
        if(empty($entry_data)){
            $this->send_response(0, __FUNCTION__);
        }

        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
        $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);

        $account_id = null;
        if(!empty($entry_data['id']) && $entry_data['id'] != -1){
            $existing_entry_data = $this->Entry->get($entry_data['id']);
            if(!empty($existing_entry_data)){
                $existing_entry_data['value'] *= ($existing_entry_data['expense'] ? -1 : 1);
                $account_id = $this->Account->get_account_id_from_entry($existing_entry_data['id']);
                $this->Account->update_balance((-1*$existing_entry_data['value']), $account_id);
            }
        }

        $entry_id = $this->Entry->save($entry_data);
        $this->Attachment->save($entry_id, $entry_data['attachments']);
        $entry_data['value'] *= ($entry_data['expense'] ? -1 : 1);
        if(is_null($account_id)){
            $account_id = $this->Account->get_account_id_from_entry($entry_id);
        }
        $this->Account->update_balance($entry_data['value'], $account_id);
        $this->send_response(1, __FUNCTION__);
    }

    public function tags_get(){
        $this->validate_access();

        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $tags = $this->Entry->get_all_tags();
        $this->send_response($tags, __FUNCTION__);<commit_msg>Corrected a bug that didn't adjust account total when moving an entry from one account type to another.<commit_after><?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Secrets
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		<NAME>
 * @link		http://philsturgeon.co.uk/code/
 */

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';
error_reporting(!E_DEPRECATED);
class Money_Tracker extends REST_Controller{

    private $_db_config;
    private $_db_config_file = '/../../config/money-tracker.db_config.php';
    private $_origin;
    private $_model_dir = 'money_tracker/';

    public function __construct(){
        parent::__construct();
        if(!file_exists(__DIR__.$this->_db_config_file)){
            $this->send_response('DB config file not found');
        } else {
            $this->_db_config = require_once(__DIR__.$this->_db_config_file);
        }
    }

    public function delete_get() {
        // TODO - test
        $this->validate_access();

        $id = $this->get('id');
        $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
        $attachment_id = $this->get('attachment');
        if(!empty($attachment_id)){
            $this->Attachment->delete($id, $attachment_id);
        } else {
            $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
            $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);
            $this->Attachment->delete($id);
            $entry_value = $this->Entry->delete($id);
            $account_id = $this->Account->get_account_id('entry', $id);
            $this->Account->update_balance(-1*$entry_value, $account_id);
        }
        $this->send_response(1, __FUNCTION__);
    }

    public function entry_get() {
        $this->validate_access();

        $id = $this->get('id');
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $entry = $this->Entry->get($id);
        if(empty($entry)){
            $this->send_response('Entry not found');
        }
        if($entry['has_attachment']){
            $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
            $attachments = $this->Attachment->get_entry($id);
            if(empty($attachments)){
                $entry['has_attachment']=0;
                $entry['attachments'] = array();
            } else {
                $entry['attachments'] = $attachments;
            }
        }

        $tag_ids = json_decode($entry['tags'], true);
        $entry['tags'] = $this->Entry->get_select_tags('id', $tag_ids);
        $this->send_response($entry, __FUNCTION__);
    }

    public function count_post() {
        $this->validate_access();

        $where_array = $this->process_where_array(json_decode(base64_decode($this->post('where')), true));
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $total_entries = $this->Entry->count($where_array);
        $this->send_response($total_entries, __FUNCTION__);
    }

    public function list_post() {
        $this->validate_access();

        $limit = $this->post('limit');
        $start = $this->post('start');
        $where_array = $this->process_where_array(json_decode(base64_decode($this->post('where')), true));
        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $entries = $this->Entry->list_entries($where_array, $start, $limit);

        $this->send_response($entries, __FUNCTION__);
    }

    public function list_accounts_get(){
        $this->validate_access();

        $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);
        $accounts = $this->Account->list_accounts();
        $this->send_response($accounts, __FUNCTION__);
    }

    public function save_post() {
        $this->validate_access();

        $entry_data = json_decode(base64_decode($this->post('data')), true);
        if(empty($entry_data)){
            $this->send_response(0, __FUNCTION__);
        }

        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $this->load->model($this->_model_dir.'attachment_model', 'Attachment', $this->_db_config);
        $this->load->model($this->_model_dir.'account_model', 'Account', $this->_db_config);

        $account_id = null;
        if(!empty($entry_data['id']) && $entry_data['id'] != -1){
            $existing_entry_data = $this->Entry->get($entry_data['id']);
            if(!empty($existing_entry_data)){
                $existing_entry_data['value'] *= ($existing_entry_data['expense'] ? -1 : 1);
                $account_id = $this->Account->get_account_id('entry', $existing_entry_data['id']);
                $this->Account->update_balance((-1*$existing_entry_data['value']), $account_id);
            }
        }

        $entry_id = $this->Entry->save($entry_data);
        $this->Attachment->save($entry_id, $entry_data['attachments']);
        $entry_data['value'] *= ($entry_data['expense'] ? -1 : 1);
        if(is_null($account_id)){
            $account_id = $this->Account->get_account_id('entry', $entry_id);
        } elseif(isset($existing_entry_data) && $entry_data['account_type']!=$existing_entry_data['account_type']){
            $account_id = $this->Account->get_account_id('type', $entry_data['account_type']);
        }
        $this->Account->update_balance($entry_data['value'], $account_id);
        $this->send_response(1, __FUNCTION__);
    }

    public function tags_get(){
        $this->validate_access();

        $this->load->model($this->_model_dir.'entry_model', 'Entry', $this->_db_config);
        $tags = $this->Entry->get_all_tags();
        $this->send_response($tags, __FUNCTION__);
{'fraction_non_alphanumeric': 0.17622778198972286, 'fraction_numerical': 0.00286412265184062, 'mean_word_length': 3.0546448087431695, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 239, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1206083', 'n_tokens_mistral': 3616, 'n_tokens_neox': 3570, 'n_words': 540}