<?php

namespace App\Console\Commands;

use App\Model\Data\Order\Pemusnahan\Pemusnahan;
use App\Model\Data\StockLogs;
use App\Model\Data\StockRelease;
use App\Model\Data\Stocks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Auth;

class ExpiredDateDarah extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'expired:date';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Hapus Kantong Darah Yang Expired';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('Cek cron job');
        $cek_data =  StockRelease::where('expired_date', '<', Carbon::now())
        ->get();

        foreach($cek_data as $key => $value){

            $cek_stock_release[$key] = StockRelease::where('id', $value->id)->first();

            $data_stock_release[$key] = [
                'status_id' => 3,
            ];

            DB::table('stock_releases')->where('id', $value->id)
            ->update($data_stock_release[$key]);

            $cek_stock[$key] = Stocks::where('golongan_darah_id', $value->golongan_darah_id)
            ->where('rhesus_id', $value->rhesus_id)
            ->where('produk_id', $value->produk_id)
            ->where('unit_donor_darah_id', $value->unit_donor_darah_id)
            ->first();

            if($cek_stock[$key]->quantity_kantong != 0){
                $data_stock[$key] = [
                    'quantity_kantong' => $cek_stock[$key]['quantity_kantong'] - 1,
                ];
                DB::table('stocks')->where('golongan_darah_id', $value->golongan_darah_id)
                    ->where('produk_id', $value->produk_id)
                    ->where('rhesus_id', $value->rhesus_id)
                    ->where('unit_donor_darah_id', $value->unit_donor_darah_id)
                    ->update($data_stock[$key]);
            }

            $data_log[$key] = [
                'log'              => 'Pemusnahan Darah dengan no kantong :  ' . $value->no_kantong,
                'user_id'          => 1,
                'quantity_kantong' => 1,
                'is_stock_in'      => 1,
                'is_stock_out'     => 0,
                'created_at'       => NOW(),
                'updated_at'       => NOW(),
            ];
            $data_pemusnahan[$key] = [
                'release_id' => $value->id,
                'keterangan' => 'Darah Sudah Expired',
                'petugas_id' => 1,
                'created_at' => NOW(),
                'updated_at' => NOW(),
            ];

        }
        $update_log             = StockLogs::insert($data_log);
        $insert_data_pemusnahan = Pemusnahan::insert($data_pemusnahan);
    }
}
