Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.1 kB
5
Indexable
Never
public function detail($eid)
    {
        // $validator = Validator::make($request->all(), [
        //     'purchase_id' => 'required'
        // ]);

        // if ($validator->fails()) {
        //     return response()->json([
        //         'status' => 400,
        //         'errors' => $validator->errors()
        //     ]);
        // }

        $data = FlowerPurchases::where('id', $eid)->with(['files', 'items'])->firstOrFail();


        // if( $request->raw == "true" ){
        //     return response()->json($data);
        // }


        $purchases = [
            "purchase_id" => $data->id,
            "date" => $data->date_purchase,
            "date_readable" => Carbon::parse($data->date_purchase)->format('d M Y'),
            "title" => $data->title,
            "catatan" => $data->note
        ];

        $res = [
            "purchases" => $purchases,
            "items" => [],
            "files" => []
        ];

        $i = 1;
        $items = FlowerPurchasesItem::where('id_purchases', $data->id)->get();
        foreach ($items as $item) {
            $flower = Flower::where('name', 'LIKE', $item->item)->first();
            if ($flower->id !== null) {
                $flower_id = $flower->id;
            } else {
                throw new \Exception('Terjadi Kesalahan: Tidak dapat memuat Data Bunga untuk item: ' . $item->item);
            }
            array_push($res['items'], [
                "index" => padLeft($i),
                "nama" => $item->item,
                "qty" => $item->qty,
                "harga_satuan" => $item->price,
                "total" => $item->amount,
                "id_flower" => $flower_id
            ]);
            $i++;
        }

        $files = FlowerPurchasesMedia::where('id_purchases', $data->id)->get();
        foreach ($files as $file) {
            array_push($res['files'], [
                "name" => $file->media
            ]);
        }


        // return ResponseFormatter::success($res, 'detail');
        return view('flower_purchases.detail', compact('res'));
    }
Leave a Comment