<?php

namespace Modules\LandedCost\Entities;

use Illuminate\Database\Eloquent\Model;

class LcExpense extends Model
{
    protected $table = 'lc_expenses';

    protected $fillable = [
        'business_id',
        'parent_type',
        'parent_id',
        'name',
        'expense_type',
        'amount',
        'currency_id',
        'rate_to_base',
        'base_amount',
        'distribution_method',
        'notes',
        'created_by',
    ];

    protected $casts = [
        'amount'       => 'decimal:4',
        'base_amount'  => 'decimal:4',
        'rate_to_base' => 'decimal:6',
    ];

    const TYPES = [
        'shipping'  => 'Shipping',
        'customs'   => 'Customs',
        'insurance' => 'Insurance',
        'handling'  => 'Handling',
        'freight'   => 'Freight',
        'other'     => 'Other',
    ];

    const METHODS = [
        'equal_qty'    => 'Equal per Unit (Qty-weighted)',
        'equal_value'  => 'Proportional to Line Value',
        'equal_lines'  => 'Equal per Product Line',
        'custom'       => 'Custom %',
    ];

    public function distributions()
    {
        return $this->hasMany(LcExpenseDistribution::class, 'lc_expense_id');
    }

    public function createdBy()
    {
        return $this->belongsTo(\App\User::class, 'created_by');
    }

    /** Scope expenses to a given polymorphic parent. */
    public function scopeForParent($query, string $parentType, $parentId)
    {
        return $query->where('parent_type', $parentType)->where('parent_id', $parentId);
    }
}
