<?php

namespace App\Http\Resources;

use App\Http\Controllers\Helpers\HelperController;
use Illuminate\Http\Resources\Json\JsonResource;

class StocksResource 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_code' => $this->material_type_code,
            'material_type_name' => $this->types ? $this->types->material_type_name : "",
            'counted_stock' => HelperController::number_format($this->total_current),
            'sap_stock' => HelperController::number_format($this->total_sap),
            'stock_variance' => HelperController::number_format($this->calculate_stock_variance($this->total_sap, $this->total_current)),
            'variance_value' => HelperController::number_format($this->calculate_cost_variance($this->total_sap, $this->total_current, $this->total_price)),
        ];
    }

    private function calculate_stock_variance($sap, $current)
    {
        return round(($sap - $current), 2);
    }

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