feat: Update Order/CustomOrder models and controllers with event integration
- Add integration fields to Order model: packing_*, courier_*, trello_card_id, qr_token
- Add integration fields to CustomOrder model: packing_*, proof_approved_*, courier_*, trello_card_id, qr_token
- Update Order model fillable array and add relationships (packedBy)
- Update CustomOrder model fillable array, casts, and add relationships (packedBy)
- Add isCustomOrder() method to both models for type checking
- Update OrderController to emit OrderCreated and DepositPaid events on successful payment
- For standard orders: full payment -> prep status, emit events
- For custom orders: deposit -> design status, balance -> printing status, emit respective events
- Add approveProof() method to CustomOrderController (POST /custom-orders/{id}/approve-proof)
- Add requestChanges() method to CustomOrderController (POST /custom-orders/{id}/request-changes)
- Add markBalancePaid() method to CustomOrderController (POST /custom-orders/{id}/pay-balance)
- All new methods emit appropriate events (ProofApproved, ProofRevisionRequested, BalancePaid)
- Add database migration for proof_approved and proof_approved_at fields on custom_orders
- Add routes for new custom order endpoints with UUID binding
- Import all required event classes in both controllers
This commit is contained in:
@@ -5,8 +5,14 @@ namespace App\Http\Controllers;
|
||||
use App\Models\CustomOrder;
|
||||
use App\Models\CustomOrderFile;
|
||||
use App\Models\CustomOrderSpecification;
|
||||
use App\Models\CustomOrderProof;
|
||||
use App\Models\AppSetting;
|
||||
use App\Models\PrintStock;
|
||||
use App\Events\OrderCreated;
|
||||
use App\Events\DepositPaid;
|
||||
use App\Events\ProofApproved;
|
||||
use App\Events\ProofRevisionRequested;
|
||||
use App\Events\BalancePaid;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -141,6 +147,9 @@ class CustomOrderController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Emit events for custom order creation with deposit
|
||||
OrderCreated::dispatch($customOrder, 'custom');
|
||||
|
||||
return redirect()->route('custom-orders.show', $customOrder)->with('success', 'Custom order created successfully. Please review the quote and proceed with deposit payment.');
|
||||
}
|
||||
|
||||
@@ -411,4 +420,147 @@ class CustomOrderController extends Controller
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve proof for custom order
|
||||
*
|
||||
* POST /custom-orders/{id}/approve-proof
|
||||
*/
|
||||
public function approveProof(Request $request, CustomOrder $customOrder)
|
||||
{
|
||||
// Authorization: only allow owner or admin
|
||||
if (auth()->user()->id !== $customOrder->user_id && !auth()->user()->is_admin) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
try {
|
||||
$customOrder->update([
|
||||
'proof_approved' => true,
|
||||
'proof_approved_at' => now(),
|
||||
]);
|
||||
|
||||
Log::info('Proof approved for custom order', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'approved_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
// Emit event
|
||||
ProofApproved::dispatch($customOrder);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Proof approved successfully',
|
||||
'proof_approved_at' => $customOrder->proof_approved_at,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to approve proof', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Failed to approve proof',
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request proof revision for custom order
|
||||
*
|
||||
* POST /custom-orders/{id}/request-changes
|
||||
*/
|
||||
public function requestChanges(Request $request, CustomOrder $customOrder)
|
||||
{
|
||||
// Authorization: only allow owner or admin
|
||||
if (auth()->user()->id !== $customOrder->user_id && !auth()->user()->is_admin) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'revision_notes' => 'required|string|min:10|max:1000',
|
||||
]);
|
||||
|
||||
try {
|
||||
$customOrder->update([
|
||||
'proof_approved' => false,
|
||||
]);
|
||||
|
||||
Log::info('Proof revision requested for custom order', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'requested_by' => auth()->id(),
|
||||
'notes' => $validated['revision_notes'],
|
||||
]);
|
||||
|
||||
// Emit event
|
||||
ProofRevisionRequested::dispatch($customOrder, $validated['revision_notes']);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Revision request sent successfully',
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to request proof revision', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Failed to request revision',
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark balance as paid for custom order
|
||||
*
|
||||
* POST /custom-orders/{id}/pay-balance
|
||||
*/
|
||||
public function markBalancePaid(Request $request, CustomOrder $customOrder)
|
||||
{
|
||||
// Authorization: only allow owner or admin
|
||||
if (auth()->user()->id !== $customOrder->user_id && !auth()->user()->is_admin) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Verify proof is approved before balance payment
|
||||
if (!$customOrder->proof_approved) {
|
||||
return response()->json([
|
||||
'error' => 'Proof must be approved before balance payment',
|
||||
'proof_approved' => $customOrder->proof_approved,
|
||||
], 409);
|
||||
}
|
||||
|
||||
try {
|
||||
$customOrder->update([
|
||||
'balance_status' => 'paid',
|
||||
'status' => 'printing',
|
||||
]);
|
||||
|
||||
Log::info('Balance paid for custom order', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'marked_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
// Emit event
|
||||
BalancePaid::dispatch($customOrder, $customOrder->balance_amount);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Balance payment recorded successfully',
|
||||
'status' => $customOrder->status,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to mark balance paid', [
|
||||
'custom_order_id' => $customOrder->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Failed to record balance payment',
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user