<?php

namespace App\Http\Resources;

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

class SummaryResource 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 [
            'material_type' => $this->get_type($this->material_type_code),
            'current_stock' => $this->total_current,
            'sap_stock' => $this->total_sap,
            'variance' => ($this->total_sap) - ($this->total_current),

        ];
    }

    private function get_type($code)
    {
        $type = Material_type::where('material_type_code', $code)->first();
        return $type->material_type_name;
    }
    private function calculate_variance($current, $sap)
    {
        return round(($sap - $current), 2);
    }

    private function calculate_cost_variance($current, $sap, $cost)
    {
        return round(($sap * $cost) - ($current * $cost), 2);
    }
}