fix bug with invoice showing that doesn't belong to the order

This commit is contained in:
Omair Saleh
2023-05-25 15:32:19 +08:00
parent 7f73ec14a5
commit 0d555eedc9
+6 -2
View File
@@ -7,6 +7,7 @@ use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\TransportType;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -20,6 +21,8 @@ class OrderResource extends JsonResource
*/
public function toArray($request)
{
$orderId = $this->id;
return [
'id' => $this->id,
'reference' => $this->reference,
@@ -47,8 +50,9 @@ class OrderResource extends JsonResource
'received_packages' => PackingListResource::collection($this->packingLists()->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('packages')->get()),
];
}),
'invoices' => $this->whenLoaded('packingLists', function() {
return TransactionResource::collection($this->transactions()->whereNotIn('transactions.status', [0, 1])->where('transactions.type', TransactionType::SHIPPING_INVOICE)->get());
'invoices' => $this->whenLoaded('packingLists', function() use ($orderId) {
return TransactionResource::collection(Transaction::whereHas('owner', function($query) use ($orderId) { return $query->where('owner_id', $orderId); })->get());
// return TransactionResource::collection($this->transactions()->whereNotIn('transactions.status', [0, 1])->where('transactions.type', TransactionType::SHIPPING_INVOICE)->get());
}),
'remarks' => RemarkResource::collection($this->remarks),
'created_at' => $this->created_at->format('d-m-Y')