Make quick sale products on machines as orders paid when ready

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 17:11:54 +02:00
co-authored by Claude Sonnet 5.5
parent 920fecdeb2
commit 38b8075a9c
58 changed files with 3435 additions and 26 deletions
@@ -0,0 +1,105 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Making quick sale products (spec 19.7): a product's production process
* (steps on machines or by hand, with the hours and materials per piece),
* and quick sale orders made before they are paid. An order line keeps a
* snapshot of the process, so later changes do not touch sent orders.
*/
public function up(): void
{
Schema::create('production_steps', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->unsignedSmallInteger('sort_order');
$table->foreignId('machine_id')->nullable()->constrained()->restrictOnDelete();
$table->unsignedSmallInteger('duration_quarters')->default(0);
$table->foreignId('work_type_id')->constrained()->restrictOnDelete();
$table->timestamps();
});
Schema::create('production_step_materials', function (Blueprint $table) {
$table->id();
$table->foreignId('production_step_id')->constrained()->cascadeOnDelete();
$table->foreignId('stock_item_id')->constrained()->restrictOnDelete();
$table->bigInteger('quantity_milli');
$table->timestamps();
});
Schema::create('quick_sale_orders', function (Blueprint $table) {
$table->id();
$table->date('ordered_on');
$table->unsignedInteger('pickup_number');
$table->timestamp('ordered_at');
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->foreignId('customer_id')->nullable()->constrained()->restrictOnDelete();
$table->string('customer_name', 150)->nullable();
$table->string('status', 20);
$table->timestamp('made_at')->nullable();
$table->timestamp('paid_at')->nullable();
$table->timestamp('cancelled_at')->nullable();
$table->foreignId('cancelled_by')->nullable()->constrained('users')->restrictOnDelete();
$table->timestamps();
$table->unique(['ordered_on', 'pickup_number']);
$table->index('status');
});
Schema::create('quick_sale_order_lines', function (Blueprint $table) {
$table->id();
$table->foreignId('quick_sale_order_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->restrictOnDelete();
$table->string('product_name');
$table->unsignedInteger('quantity');
$table->unsignedInteger('unit_price_cents');
$table->unsignedSmallInteger('vat_basis_points');
$table->json('production_steps');
$table->unsignedSmallInteger('current_step')->nullable();
$table->timestamp('made_at')->nullable();
$table->unsignedSmallInteger('sort_order');
$table->timestamps();
});
Schema::table('quick_sales', function (Blueprint $table) {
$column = $table->foreignId('quick_sale_order_id')->nullable()->after('credits_quick_sale_id');
// SQLite (tests) could not drop the column again with a foreign key on it.
if (Schema::getConnection()->getDriverName() !== 'sqlite') {
$column->constrained()->restrictOnDelete();
}
});
Schema::table('machine_queue_entries', function (Blueprint $table) {
$table->foreign('quick_sale_order_line_id', 'machine_queue_order_line_foreign')->references('id')->on('quick_sale_order_lines')->cascadeOnDelete();
});
Schema::table('stock_movements', function (Blueprint $table) {
$table->foreign('quick_sale_order_line_id', 'stock_movements_order_line_foreign')->references('id')->on('quick_sale_order_lines')->restrictOnDelete();
});
}
public function down(): void
{
// SQLite (tests) cannot drop a foreign key; there the tables are dropped by the earlier migrations' rollback.
if (Schema::getConnection()->getDriverName() !== 'sqlite') {
Schema::table('stock_movements', fn (Blueprint $table) => $table->dropForeign('stock_movements_order_line_foreign'));
Schema::table('machine_queue_entries', fn (Blueprint $table) => $table->dropForeign('machine_queue_order_line_foreign'));
Schema::table('quick_sales', fn (Blueprint $table) => $table->dropForeign(['quick_sale_order_id']));
}
Schema::table('quick_sales', function (Blueprint $table) {
$table->dropColumn('quick_sale_order_id');
});
Schema::dropIfExists('quick_sale_order_lines');
Schema::dropIfExists('quick_sale_orders');
Schema::dropIfExists('production_step_materials');
Schema::dropIfExists('production_steps');
}
};