<?php

namespace App\Http\Resources;

use App\Models\Material;
use Illuminate\Http\Resources\Json\JsonResource;

class PostStockResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'item_id' => $this->get_item_id($this->material_no),
            'material_no' => $this->material_no,
            'material_name' => $this->material_name($this->material_no),
            'stocktake_count' => $this->current_stock,
            'unit_of_measure' => $this->get_unit($this->material_no),
        ];
    }

    private function get_unit($material_no)
    {
        $material = Material::where('material_no', $material_no)->first();
        return $material->unit_of_measure;
    }

    private function get_item_id($material_no)
    {
        $material = Material::where('material_no', $material_no)->first();
        return $material->item_id;
    }

    private function material_name($material_no)
    {
        $material = Material::where('material_no', $material_no)->first();
        return $material->material_name;
    }
}