You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在CodeIgniter的Rise CRM中,如何在Projects类方法内调用Expenses类的save()方法

Hey there! Let's tackle this requirement for your Rise CRM built on CodeIgniter—automatically creating an expense entry when a user logs their time via the Projects::save_timelog() method. Here's a practical, step-by-step approach to make this work:

Step 1: Map Timelog Data to Expense Requirements

First, let's align the timelog data with the mandatory fields from your Expenses::save() method snippet. We know expense_date, category_id, and amount are required, so we'll derive these from the timelog:

  • expense_date: Reuse the date from the submitted timelog (e.g., $timelog_data['date'])
  • category_id: Set up a dedicated "Time Log Expense" category in your CRM, then hardcode its ID (or store it in a config file for easier updates)
  • amount: Calculate this by multiplying the timelog's hours by the user's hourly rate (you'll need to fetch this from the user's profile or project settings)
Step 2: Modify the Projects::save_timelog() Method

We'll trigger the expense creation right after the timelog is successfully saved. Here's how to adjust the method:

function save_timelog() {
    // Existing code to validate timelog input and save to the database
    $timelog_id = $this->db->insert_id(); // Grab the ID of the newly created timelog

    if ($timelog_id) {
        // Fetch full timelog details to pull necessary data
        $timelog = $this->projects_model->get_single_timelog($timelog_id); // Adjust model method to match your setup
        // Get the user's hourly rate (modify this to match your user data structure)
        $hourly_rate = $this->users_model->get_user_hourly_rate($timelog['user_id']);

        // Prepare the expense data array
        $expense_data = array(
            'expense_date' => $timelog['date'],
            'category_id' => 7, // Replace with your actual "Time Log" category ID
            'amount' => $timelog['hours'] * $hourly_rate,
            'description' => 'Auto-generated expense for timelog #' . $timelog_id,
            'project_id' => $timelog['project_id'],
            'user_id' => $timelog['user_id'],
            // Add any other required fields from your Expenses validation rules
        );

        // Load the Expenses controller and trigger the save method
        $this->load->controller('expenses');
        $this->expenses->save($expense_data);
    }

    // Existing response/redirect logic from your original method
}
Step 3: Adjust Expenses::save() to Accept Direct Data

By default, the save() method might expect POST data. Let's tweak it to accept a data array directly, so we don't have to fake POST requests:

function save($manual_data = array()) {
    // Use submitted POST data or the manually passed array
    $submitted_data = !empty($manual_data) ? $manual_data : $this->input->post();

    // Run your existing validation
    validate_submitted_data(array(
        "id" => "numeric",
        "expense_date" => "required",
        "category_id" => "required",
        "amount" => "required|numeric",
        // Include all your other validation rules here
    ));

    // Proceed with your existing save logic using $submitted_data
}
Step 4: Handle Edge Cases

Don't forget to cover potential issues to keep the system robust:

  • Missing Hourly Rate: Add a fallback (e.g., use a system-wide default rate stored in config) if the user doesn't have a rate set
  • Expense Save Failure: Add error logging or admin notifications if the expense fails to save (you can check the return value of $this->expenses->save())
  • Duplicate Entries: Before creating the expense, check if one already exists linked to the timelog ID to avoid duplicates
Step 5: Test Everything
  • Test time logging with different users (those with and without hourly rates)
  • Verify expenses are created with the correct amount, date, and category
  • Simulate save failures (e.g., invalid category ID) to ensure the system handles errors gracefully

内容的提问来源于stack exchange,提问作者fuji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:21:41