duongtd's avatar

Structuring e-commerce Order adjustments

I am implementing the ecommerce order ajustments which can calculate the discount if coupon applied or increase the price if there is additional charge. And I need your suggest to structure it with the Eloquent as the following:

Model

  • Order (holding information of order)
  • OrderLine (holding information of product ordered)
  • Coupon (holding information of discount code and value)
  • Charge (Holding information of additional charges such as: gift wrap...)
  • Adjustment (Polymorphic relationship of the ajustment and it contain a foreign key to the order)

Migration

// Order
Schema::create('orders', function (Blueprint $table)
{
    $table->increments('id');
    $table->unsignedInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->longText('note')->nullable();
    $table->timestamps();
});

// OrderLine
Schema::create('order_lines', function (Blueprint $table)
{
    $table->increments('id');
    $table->unsignedInteger('order_id');
    $table->foreign('order_id')->references('id')->on('shop_orders')->onDelete('cascade');
    $table->decimal('price', 10, 2);
    $table->integer('quantity')->default(1);
    $table->text('note')->nullable();
    $table->timestamps();
});

// Coupon
Schema::create('coupons', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->enum('type', ['fixed', 'percentage']);
    $table->string('code')->unique();
    $table->decimal('value', 12, 2);
    $table->decimal('minimum_order_amount', 12, 2)->nullable();
    $table->integer('minimum_order_quantity')->nullable();
    $table->boolean('is_enabled')->default(true);
    $table->timestamp('start_at')->nullable();
    $table->timestamp('end_at')->nullable();
    $table->timestamps();
});

// Adjustment
Schema::create('shop_adjustments', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('order_id');
    $table->foreign('order_id')->references('id')->on('shop_orders')->onDelete('cascade');
    $table->nullableMorphs('adjustable');
    $table->decimal('value', 12, 2);
    $table->timestamps();
});

Thank you!

0 likes
0 replies

Please or to participate in this conversation.