mendela92/laravel-stock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mendela92/laravel-stock

最新稳定版本:v1.0.0

Composer 安装命令:

composer require mendela92/laravel-stock

包简介

Keep stock for Eloquent models

README 文档

README

Software License

Keep stock for Eloquent models. This package will track stock mutations for your models. You can increase, decrease, clear and set stock. It's also possible to check if a model is in stock (on a certain date/time). But also watches the level of stock of a model and sends a notification via Email

This package was originally a fork form laravel-stock

Functionality

  • Increase stocks
  • Decrease stocks
  • Clear stocks
  • Set stocks
  • Check if model has stock or not (on a certain date/time).
  • Send notification when a pre-defined stock level is reached

Installation

You can install the package via composer:

composer require mendela92/laravel-stock

By running php artisan vendor:publish --provider="Mendela92\Stock\StockServiceProvider", it will publish migrations and config file.

The configuration file looks this:

<?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,
    ],
];

Run php artisan migrate to migrate the table. There will now be a stocks table in your database.

Usage

Adding the HasStock trait will enable stock functionality on the Model.

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
    }
}

Basic mutations

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

Clearing stock

It's also possible to clear the stock and directly setting a new value.

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

Setting stock

It is possible to set stock. This will create a new mutation with the difference between the old and new value.

$book->setStock(10);

Check if model is in stock

It's also possible to check if a product is in stock (with a minimal value).

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

Current stock

Get the current stock value (on a certain date).

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

Stock arguments

Add a description and/or reference model to de StockMutation.

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

Query Scopes

It is also possible to query based on stock.

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

Testing

composer test

Contributing

Contributions are welcome, thanks to y'all :)

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-14