<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Modules\Invoices\Invoice;
use App\Console\Commands\AbstractCommand;

class InvoicePatchCommand extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'seraph:invoices-patch';

    /**
     * A friendly name of the command that will be used when loggin messages.
     *
     * @var string
     */
    protected $command_title = 'Invoices Patch';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Patches the total price & paid status for invoices.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // Login as admin
        Auth::loginUsingId(1);

        // Get tomorrows date
        $startDate = Carbon::now()->subDays(1)->toDateTimeString();
        $this->info('Looking for invoices created after: '.$startDate);

        // Get a list of repairs that are prebooks with tomorrows date
        $invoices = Invoice::where('created_at', '>=', $startDate)->get();

        foreach ($invoices as $invoice) {
            $this->info('Patching invoice #'.$invoice->id);

            try {
                if ($invoice->paid != $invoice->isPaid()) {
                    $invoice->save();
                }
            } catch (Exception $e) {
                $this->error($e->getMessage().' in: '.$e->getFile().' at line: '.$e->getLine());
            }
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }
}
