mendela92/laravel-stock

为 Eloquent 模型保留库存

资助包维护!
Patreon

v1.0.0 2022-12-14 00:49 UTC

This package is auto-updated.

Last update: 2024-09-14 12:44:35 UTC


README

Software License

为 Eloquent 模型保留库存。此包将跟踪模型的库存变更。您可以增加、减少、清除和设置库存。还可以检查模型在某个日期/时间是否处于库存状态。但也可以监视模型的库存水平,并通过电子邮件发送通知。

此包最初是 laravel-stock 的分支。

功能

  • 增加库存
  • 减少库存
  • 清除库存
  • 设置库存
  • 检查模型在特定日期/时间是否有库存。
  • 当达到预定义的库存水平时发送通知

安装

您可以通过 composer 安装此包

composer require mendela92/laravel-stock

运行 php artisan vendor:publish --provider="Mendela92\Stock\StockServiceProvider",它将发布迁移和配置文件。

配置文件如下

<?php

use Mendela92\Stock\Notifications\LowStockLevelNotification;

return [

    /*
    |--------------------------------------------------------------------------
    | Default table name
    |--------------------------------------------------------------------------
    |
    | Table name to use to store mutations.
    |
    */

    'table' => 'stocks',

    /*
   |--------------------------------------------------------------------------
   | Default notification stock level
   |--------------------------------------------------------------------------
   |
   | Default stock alert configuration values
   |
   */
    'alert' => [

        'notification' => env("STOCK_NOTIFICATION", true),

        'at' => env("NOTIFICATION_STOCK_LEVEL", 10),

        'to' => ['email@example.com'],

        'model' => LowStockLevelNotification::class,
    ],
];

运行 php artisan migrate 来迁移表。现在您的数据库中将有一个 stocks 表。

使用方法

添加 HasStock 特性将在模型上启用库存功能。

use Mendela92\Stock\HasStock;

class Book extends Model
{
    use HasStock;
    
    ...
    
    public function getStockAlertAt(): mixed
    {
        return config('stock.alert.at', 10);
        // Change the value of the level of stock before being notification is sent.
    }

    public function getStockAlertTo(): mixed
    {
        // Array of emails that the notifications will be sent to.
    }

    public function getStockNotificationStatus(): mixed
    {
        // Conditioning notification of  status for each model instance
    }
}

基本变更

$book->increaseStock(10);
$book->decreaseStock(10);
$book->mutateStock(10);
$book->mutateStock(-10);

清除库存

您还可以清除库存并直接设置新的值。

$book->clearStock();
$book->clearStock(10);

设置库存

您可以设置库存。这将创建一个新的变更,其中包含新旧值之间的差异。

$book->setStock(10);

检查模型是否在库存中

您还可以检查产品是否在库存中(具有最小值)。

$book->inStock();
$book->inStock(10);

当前库存

获取特定日期的当前库存值。

$book->stock;
$book->stock(Carbon::now()->subDays(10));

库存参数

向库存变更添加描述和/或引用模型。

$book->increaseStock(10, [
    'description' => 'This is a description',
    'reference' => $otherModel,
]);

查询作用域

您还可以根据库存进行查询。

Book::whereInStock()->get();
Book::whereOutOfStock()->get();

测试

composer test

贡献

欢迎贡献,感谢大家 :)

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件