<?php

namespace App\Model\pos;

use App\Model\product\ColorModel;
use App\Model\product\ProductModel;
use App\Model\product\SizeModel;
use App\Model\stock_inventory\TransferStockDetailModel;
use Illuminate\Database\Eloquent\Model;


class SaleDetailsModel extends Model
{
    protected $table = "nso007_sale_details";

    protected $fillable = [
        'id',
        'sale_id',
        'product_id',
        'qty',
        'return_qty',
        'sale_price',
        'dis',
        'barcode',
        'unit_id',
        'size_code',
        'color_code',
        'product_cost',
        'total_amount',
        'item_discount_in_cash',
        'total_discount_in_cash',
        'item_grand_total',
        'status',
        'date',
        'created_at',
        'updated_at',
    ];

    public function transfer_stock($type, $from_date, $to_date)
    {
        if ($type == 'in') {
            $type = 'nso007_transfer_stock.to_warehouse_id';
        } else {
            $type = 'nso007_transfer_stock.from_warehouse_id';
        }
        $transfer = TransferStockDetailModel::selectRaw('SUM(nso007_transfer_stock_detail.qty) as qty')
            ->join('nso007_transfer_stock', 'nso007_transfer_stock_detail.transfer_id', 'nso007_transfer_stock.id')
            ->where('nso007_transfer_stock_detail.barcode', $this->barcode)
            ->whereDate('nso007_transfer_stock.date', '>=', $from_date ? date('Y-m-d', strtotime($from_date)) : date('Y-m-d'))
            ->whereDate('nso007_transfer_stock.date', '<=', $to_date ? date('Y-m-d', strtotime($to_date)) : date('Y-m-d'))
            ->where($type, $this->warehouse_id)
            ->first();
        $unit = getProductById($this->product_id);
        $unit = $unit ? $unit->unit_id : '';
        $unit = findUnit($unit);
        $unit = $unit ? $unit->unit : '';
        $transfer = $transfer ? $transfer->qty : 0;
        if ($transfer) {
            $transfer = '<u><b class="text-success" data-qty="' . $transfer . '">' . __('administrator.in') . ' </b>:<span class="text-primary">' . ($transfer ? ($transfer . $unit) : '0') . '</span></u>';
        } else {
            $transfer = '';
        }

        return $transfer;
    }

    public function transfer_stock_qty($type, $from_date, $to_date)
    {
        if ($type == 'in') {
            $type = 'nso007_transfer_stock.to_warehouse_id';
        } else {
            $type = 'nso007_transfer_stock.from_warehouse_id';
        }
        $transfer = TransferStockDetailModel::selectRaw('SUM(nso007_transfer_stock_detail.qty) as qty')
            ->join('nso007_transfer_stock', 'nso007_transfer_stock_detail.transfer_id', 'nso007_transfer_stock.id')
            ->where('nso007_transfer_stock_detail.barcode', $this->barcode)
            ->whereDate('nso007_transfer_stock.date', '>=', $from_date ? date('Y-m-d', strtotime($from_date)) : date('Y-m-d'))
            ->whereDate('nso007_transfer_stock.date', '<=', $to_date ? date('Y-m-d', strtotime($to_date)) : date('Y-m-d'))
            ->where($type, $this->warehouse_id)
            ->first();
        $unit = getProductById($this->product_id);
        $unit = $unit ? $unit->unit_id : '';
        $transfer = $transfer ? $transfer->qty : 0;
        return $transfer;
    }

    public function size()
    {
        return $this->hasOne(SizeModel::class, 'code', 'size_code');
    }

    public function color()
    {
        return $this->hasOne(ColorModel::class, 'code', 'color_code');
    }

    public function product()
    {
        return $this->hasOne(ProductModel::class, 'id', 'product_id');
    }
}
