tmakinde/expense-tracker

这是一个面向在 Laravel 中构建银行/金融科技应用程序的开发者的包,使他们能够无缝地将费用跟踪功能集成到他们的应用程序中。

dev-master 2024-08-12 16:27 UTC

This package is auto-updated.

Last update: 2024-09-12 16:52:39 UTC


README

此包 Tmakinde/expensetracker 旨在帮助任何在 Laravel 中构建银行/金融科技应用程序的开发者,使他们能够将费用管理功能无缝集成到其应用程序中。它允许根据类别轻松管理用户的费用,并提供根据类别和时间段跟踪费用的方法。

特性

关键包特性包括

  • 能够创建不同类别供用户选择。类别可以是
    • 食物
    • 交通
    • 租金
    • 娱乐
    • 健康
    • 教育
    • 购物
    • 其他
  • 用户能够向所选类别添加费用。
  • 统计信息
    • 能够查看特定时间段内特定类别内的所有费用
    • 能够查看特定时间段内的所有费用,但按类别分组
  • 为特定时间段内的类别定义限制,以便知道该时间段的费用是否已超出限制
    • 每日限制
    • 每周限制
    • 每月限制
    • 每年限制

要求

  • PHP 8 或更高版本
  • Laravel 8 或更高版本

安装

您可以通过 composer 安装此包

composer require tmakinde/expense-tracker

配置

使用以下命令发布配置文件

迁移

php artisan vendor:publish --provider="Tmakinde\Expensetracker\ExpenseServiceProvider" --tag="expenses-migrations"

配置

php artisan vendor:publish --provider="Tmakinde\Expensetracker\ExpenseServiceProvider"
--tag="expenses-config"

用法

类别

类别是用户可以添加到费用列表的不同类型的费用。您可以使用以下命令创建类别

use Tmakinde\Expensetracker\Model\Category;
Category::create([
    'name' => 'Food'
]);

这将创建一个名为 Food 的类别,并默认将字段 is_active 设置为 true

标记类别为不活动

use Tmakinde\Expensetracker\Model\Category;
Category::markAsInactive($categoryId);

标记类别为活动

use Tmakinde\Expensetracker\Model\Category;
Category::markAsActive($categoryId);

使用类别外观来访问以下方法

use Tmakinde\ExpenseTracker\Facade\CategoryRequest;

// fetch categories of a user
CategoryRequest::for($user)->get();

// fetch categories of a user based on limit type
CategoryRequest::for($user)->whereLimitType('daily')->get();

// fetch categories of a user based on limit amount
CategoryRequest::for($user)->whereLimitAmountBetween(1000, 3000)->get();

// fetch categories of a user based on limit type and limit amount
CategoryRequest::for($user)->whereLimitType('daily')->whereLimitAmountBetween(1000, 3000)->get();

费用

费用是用户可以添加到费用列表的实际费用。您可以使用以下命令创建费用

use Tmakinde\Expensetracker\Model\Expense;
Expense::create([
    'user_id' => 1,
    'user_type' => 'App\Models\User',
    'category_id' => 1,
    'currency' => 'NGN',
    'amount' => 1000,
]);

注意:user_type 是用户模型,这意味着它可以作为多态关系。

获取所有用户费用

use Tmakinde\ExpenseTracker\Facade\ExpenseRequest;
ExpenseRequest::for($user)->get();

获取特定类别的所有用户费用

use Tmakinde\ExpenseTracker\Facade\ExpenseRequest;
ExpenseRequest::for($user)->whereCategory($categoryId)->get();

获取特定时间段内特定类别的所有用户费用

use Tmakinde\ExpenseTracker\Facade\ExpenseRequest;
ExpenseRequest::for($user)->whereCategory($categoryId)->whereDateBetween('2022-01-01', '2022-01-31')->get();

获取特定时间段内的所有用户费用

use Tmakinde\ExpenseTracker\Facade\ExpenseRequest;
ExpenseRequest::for($user)->whereDateBetween('2022-01-01', '2022-01-31')->get();

按类别分组获取用户费用

use Tmakinde\ExpenseTracker\Facade\ExpenseRequest;
ExpenseRequest::for($user)->groupByCategory()->get();

用户

用户是实际的用户,他们可以向费用列表添加费用。

要使用用户模型中的某些方法,您需要将特质 UserLimitInteractionUserCategoryInteraction 添加到用户模型中。

use Tmakinde\ExpenseTracker\Trait\UserCategoryInteraction;
use Tmakinde\ExpenseTracker\Trait\UserLimitInteraction;

class User extends Model
{
    use UserLimitInteraction, UserCategoryInteraction;
}

限制

限制是用户在特定时间段内可以花费在类别上的最大费用。您可以使用以下命令创建限制

// create limit for a user category
use Tmakinde\ExpenseTracker\Model\Category;
use Tmakinde\ExpenseTracker\Enum\LimitType;

$category = Category::find(1);
$category->createLimit(LimitType::Daily)->for($user)

// create limit using the user model

$categoryCallback = function(Category $category) {
    return $category->where('is_active', 1)->first();
}
auth()->user()->createCategoryLimit(LimitType::Daily, 1000, $categoryCallback)

其中 $limitType 可以是 dailyweeklymonthlyyearly

配置更改

  • 您可以在配置文件中更改默认货币

测试

使用以下命令运行测试

vendor/bin/phpunit