<?php


namespace App\Console\Commands;


use App\Models\HostelFee;
use App\Models\MoneyDetail;
use App\Models\MoneyInfo;
use App\User;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public function handle()
    {
        $phone = '0938456460';
        $owner = User::query()->where('phone', $phone)->first();
        $moneyInfos = MoneyInfo::query()
            ->whereHas('hostel', function($q) use ($owner) {
                $q->where('owner_id', $owner->id);
            })
            ->get();
        $fee = HostelFee::find(12893);
        foreach($moneyInfos as $moneyInfo)
        {
            $sumDetail = MoneyDetail::query()
                ->where('money_info_id', $moneyInfo->id)
                ->sum('amount');

            if($moneyInfo->amount > $sumDetail)
            {
                $diff = ($moneyInfo->amount - $sumDetail);
                if($diff == 70000)
                {
                    $detail = MoneyDetail::create([
                        'hostel_id' => $moneyInfo->hostel_id,
                        'room_id' => $moneyInfo->room_id,
                        'name' => $fee->name,
                        'value' => $fee->fee,
                        'money_info_id' => $moneyInfo->id,
                        'amount' => $fee->fee,
                        'hostel_fee_id' => $fee->id,
                        'qty' => 1,
                        'sum_amount' => $fee->fee
                    ]);

                    $this->info($moneyInfo->id);
                    $this->info($detail->id);
                }

            }
        }

    }
}