|
17 | 17 | use App\Models\OrderItem; |
18 | 18 | use App\Models\Photo; |
19 | 19 | use App\Models\User; |
20 | | -use App\Services\MoneyService; |
| 20 | +use Illuminate\Database\Eloquent\Builder; |
21 | 21 | use Illuminate\Support\Facades\Auth; |
22 | 22 | use Illuminate\Support\Str; |
23 | 23 |
|
24 | 24 | class OrderService |
25 | 25 | { |
26 | 26 | public function __construct( |
27 | | - private MoneyService $money_service, |
28 | 27 | private PurchasableService $purchasable_service, |
29 | 28 | ) { |
30 | 29 | } |
@@ -123,4 +122,93 @@ public function getAll(): array |
123 | 122 | }) |
124 | 123 | ->orderBy('id', 'desc')->get()->all(); |
125 | 124 | } |
| 125 | + |
| 126 | + /** |
| 127 | + * Clear old orders that are older than 2 weeks, have no items, and have no user_id. |
| 128 | + * |
| 129 | + * @return int Number of orders deleted |
| 130 | + */ |
| 131 | + public function clearOldOrders(): int |
| 132 | + { |
| 133 | + // Delete all the order items first to avoid foreign key constraint issues |
| 134 | + OrderItem::whereIn('order_id', $this->getQueryOldOrders()->select('id'))->delete(); |
| 135 | + |
| 136 | + return $this->getQueryOldOrders()->delete(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Count the number of old orders. |
| 141 | + * |
| 142 | + * @return int |
| 143 | + */ |
| 144 | + public function countOldOrders(): int |
| 145 | + { |
| 146 | + return $this->getQueryOldOrders()->count(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Return the query builder for old orders. |
| 151 | + * |
| 152 | + * An old order is defined as being older than $weeks weeks, |
| 153 | + * - having no user_id, |
| 154 | + * - having no items |
| 155 | + * - or having items but status is still PENDING |
| 156 | + * |
| 157 | + * @param int $weeks |
| 158 | + * |
| 159 | + * @return Builder |
| 160 | + */ |
| 161 | + protected function getQueryOldOrders(int $weeks = 2): Builder |
| 162 | + { |
| 163 | + $threshold_date = now()->subWeeks($weeks); |
| 164 | + |
| 165 | + return Order::where('created_at', '<', $threshold_date) |
| 166 | + ->whereNull('user_id') |
| 167 | + ->where(function (Builder $query): void { |
| 168 | + $query->where('status', PaymentStatusType::PENDING) |
| 169 | + ->orWhereDoesntHave('items'); |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Mark an offline order as paid (completed). |
| 175 | + * |
| 176 | + * @param Order $order The order to mark as paid |
| 177 | + * |
| 178 | + * @return Order The updated order |
| 179 | + * |
| 180 | + * @throws \InvalidArgumentException If the order is not in offline status |
| 181 | + */ |
| 182 | + public function markAsPaid(Order $order): Order |
| 183 | + { |
| 184 | + if ($order->status !== PaymentStatusType::OFFLINE) { |
| 185 | + throw new \InvalidArgumentException('Order must be in offline status to be marked as paid'); |
| 186 | + } |
| 187 | + |
| 188 | + $order->status = PaymentStatusType::COMPLETED; |
| 189 | + $order->save(); |
| 190 | + |
| 191 | + return $order; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Mark a completed order as delivered (closed). |
| 196 | + * |
| 197 | + * @param Order $order The order to mark as delivered |
| 198 | + * |
| 199 | + * @return Order The updated order |
| 200 | + * |
| 201 | + * @throws \InvalidArgumentException If the order is not in completed status |
| 202 | + */ |
| 203 | + public function markAsDelivered(Order $order): Order |
| 204 | + { |
| 205 | + if ($order->status !== PaymentStatusType::COMPLETED) { |
| 206 | + throw new \InvalidArgumentException('Order must be in completed status to be marked as delivered'); |
| 207 | + } |
| 208 | + |
| 209 | + $order->status = PaymentStatusType::CLOSED; |
| 210 | + $order->save(); |
| 211 | + |
| 212 | + return $order; |
| 213 | + } |
126 | 214 | } |
0 commit comments