tourze/coupon-core-bundle

优惠券核心模块,提供优惠券管理、券码生成和验证功能

Installs: 306

Dependents: 4

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/coupon-core-bundle

0.0.2 2025-06-03 08:56 UTC

This package is auto-updated.

Last update: 2025-10-31 19:54:03 UTC


README

English | 中文

Latest Version Total Downloads PHP Version Require License Code Coverage

A Symfony bundle providing core functionality for coupon management, including coupon categories, batches, codes, discounts, and channel management.

Features

  • Coupon Management: Comprehensive coupon lifecycle management
  • Batch Operations: Create and manage coupon batches
  • Channel Integration: Multi-channel coupon distribution
  • Discount Rules: Flexible discount configurations
  • Automatic Expiration: Scheduled tasks for handling expired coupons
  • Statistics Tracking: Built-in coupon usage statistics

Installation

composer require tourze/coupon-core-bundle

Bundle Registration

Register the bundle in your config/bundles.php:

return [
    // ...
    Tourze\CouponCoreBundle\CouponCoreBundle::class => ['all' => true],
];

Configuration

Database Migration

Run migrations to create the necessary database tables:

php bin/console doctrine:migrations:migrate

Console Commands

The bundle provides the following console commands:

Check Expired Categories

php bin/console coupon:check-expired-category

This command checks coupon categories for expiration and marks expired categories as invalid. It runs as a cron task every minute to ensure categories are automatically invalidated when they expire.

Features:

  • Automatically marks categories as invalid when outside their valid time range
  • Runs every minute via cron task integration
  • Ensures coupon availability is properly controlled by time constraints

Revoke Expired Codes

php bin/console coupon:revoke-expired-code

This command automatically revokes expired coupon codes that haven't been used. It processes up to 500 codes per execution to manage system load.

Features:

  • Finds unused coupon codes that have passed their expiration date
  • Marks expired codes as invalid
  • Processes in batches of 500 codes per run
  • Prevents expired coupons from being redeemed

Core Entities

Category

Represents coupon categories with validation time ranges.

Batch

Groups coupons for batch operations and management.

Code

Individual coupon codes with unique identifiers and validation status.

Coupon

The main coupon entity linking categories, batches, and codes.

Channel

Distribution channels for coupon allocation.

Discount

Discount rules and configurations for coupons.

CouponStat

Statistical data for coupon usage tracking.

Usage Examples

Creating a Coupon Category

use Tourze\CouponCoreBundle\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;

$category = new Category();
$category->setName('Summer Sale');
$category->setStartTime(new \DateTime('2024-06-01'));
$category->setEndTime(new \DateTime('2024-08-31'));
$category->setValid(true);

$entityManager->persist($category);
$entityManager->flush();

Generating Coupon Codes

use Tourze\CouponCoreBundle\Entity\Code;
use Tourze\CouponCoreBundle\Entity\Batch;

$batch = new Batch();
$batch->setName('SUMMER2024');
$batch->setCategory($category);

for ($i = 0; $i < 100; $i++) {
    $code = new Code();
    $code->setCode(generateUniqueCode()); // Your code generation logic
    $code->setBatch($batch);
    $code->setValid(true);
    $code->setExpireTime(new \DateTime('+30 days'));
    
    $entityManager->persist($code);
}

$entityManager->flush();

Integration with Other Bundles

This bundle integrates with:

  • tourze/benefit-bundle - For benefit calculations
  • tourze/condition-system-bundle - For conditional coupon rules
  • tourze/doctrine-snowflake-bundle - For unique ID generation
  • tourze/symfony-cron-job-bundle - For scheduled tasks

Testing

Run the test suite:

./vendor/bin/phpunit packages/coupon-core-bundle/tests

Contributing

Please ensure all tests pass and code meets PHPStan level 8 standards before submitting pull requests.

License

This bundle is released under the MIT License.