<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\Models\Base\Currency;

use App\Services\Guest\PaymentService;

class PaymentServiceTest extends TestCase
{
    use DatabaseTransactions;

    public function testEuroPaymentWithoutRounding()
    {
        $service = new PaymentService;

        $php = Currency::where('name', '=', 'Peso')->first();
        $eur = Currency::where('name', '=', 'Euro')->first();

        $booking = factory(App\Models\Guest\Booking::class)->create();
        $paymentType = factory(App\Models\Guest\PaymentType::class)->create();

        $sale = factory(App\Models\Guest\Sale::class)->make();

        $sale->booking_id = $booking->id;
        $sale->quantity = 1;
        $sale->unit_price = 52;
        $sale->unit_price_currency_id = $php->id;
        $sale->total_in_default_currency = 52;
        $sale->conversion_rate = 1;
        $sale->conversion_fee = 0;
        $sale->save();

        $payment = $service->makeFinalPayment($booking->id, $eur->id, $paymentType->id);

        $this->assertEquals($payment->amount, 1);
        $this->assertEquals($payment->total_in_default_currency, 52);

        $bookingReloaded = App\Models\Guest\Booking::with('paymentsTotal')->findOrFail($booking->id);
        $this->assertEquals(52, $bookingReloaded->paymentsTotal);
    }

    public function testEuroPaymentWithRounding()
    {
        $service = new PaymentService;

        $php = Currency::where('name', '=', 'Peso')->first();
        $eur = Currency::where('name', '=', 'Euro')->first();

        $booking = factory(App\Models\Guest\Booking::class)->create();
        $paymentType = factory(App\Models\Guest\PaymentType::class)->create();

        $sale = factory(App\Models\Guest\Sale::class)->make();

        $sale->booking_id = $booking->id;
        $sale->quantity = 1;
        $sale->unit_price = 175;
        $sale->unit_price_currency_id = $php->id;
        $sale->total_in_default_currency = 175;
        $sale->conversion_rate = 1;
        $sale->conversion_fee = 0;
        $sale->save();

        $payment = $service->makeFinalPayment($booking->id, $eur->id, $paymentType->id);

        $this->assertEquals($payment->amount, 4);
        $this->assertEquals($payment->total_in_default_currency, 175);

        $bookingReloaded = App\Models\Guest\Booking::with('paymentsTotal')->findOrFail($booking->id);
        $this->assertEquals(175, $bookingReloaded->paymentsTotal);
    }
}
