<?php


namespace Tests\Services\OwnerStoragePriceModelService;

use App\Owner;
use App\OwnerReport;
use App\OwnerStoragePriceModel;
use App\Services\OwnerStoragePriceModelService;
use App\TaxRate;
use App\Unit;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class OwnerStoragePriceModelServiceTest extends  TestCase
{
    /** @var OwnerStoragePriceModelService */
    public $service;
    public $data;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(OwnerStoragePriceModelService::class);

        $unit = Unit::query()->where("name","月")->first();
        $units = [];
        if (!$unit){
            $unit = Unit::query()->create([
                "name" => "月",
                "code" => "月"
            ]);
            $units[] = $unit;
        }
        $this->data["units"] = $units;
//        $this->data['taxRate']=factory(TaxRate::class)->create(['value'=>2]);
        $this->data['taxRate']=$res = DB::insert('insert ignore into tax_rates (value) values(2)');
        $this->data["models"] = [
            factory(OwnerStoragePriceModel::class)->create([
                "minimum_area" => 600,     //最低起租面积
                "price" => 80.4,            //单价
                "discount_type" => "按单减免",    //减免类型
                "discount_value" => 0.1,   //减免值
                "unit_id" => 1,          //单位ID
                "time_unit_id" => $unit->id,          //单位ID
                "tax_rate_id" => $this->data['taxRate']['id'],
            ])->toArray(),
            factory(OwnerStoragePriceModel::class)->create([
                "minimum_area" => 600,     //最低起租面积
                "price" => 80.4,            //单价
                "discount_type" => "固定减免",    //减免类型
                "discount_value" => 3.6,   //减免值
                "unit_id" => 1,          //单位ID
                "time_unit_id" => $unit->id,          //计时单位ID
                "tax_rate_id" => $this->data['taxRate']['id'],
            ])->toArray(),
        ];
        $this->data["owners"] = [
            factory(Owner::class)->create(["user_owner_group_id"=>1])->toArray()
        ];
        $this->data["reports"] = [
            factory(OwnerReport::class)->create([
                "owner_id" => $this->data["owners"][0]["id"],
                "owner_bill_report_id"  =>1,
                "counting_month" => "2020-10-10"
            ])->toArray()
        ];
    }

    /**
     * @group customer
     */
    public function testCalculationAmount()
    {
        $model = OwnerStoragePriceModel::query()->find($this->data["models"][0]["id"]);
        $result = $this->service->calculationAmount($model,548.98,$this->data["owners"][0]["id"],"2020-10-10");
        $this->assertEquals([0,0],$result);
        $model = OwnerStoragePriceModel::query()->find($this->data["models"][1]["id"]);
        $result = $this->service->calculationAmount($model,548.98,$this->data["owners"][0]["id"],"2020-10-10");
        $this->assertEquals([0,null],$result);
    }

    public function tearDown(): void
    {
        OwnerStoragePriceModel::destroy(array_column($this->data["models"],"id"));
        Owner::destroy(array_column($this->data["owners"],"id"));
        OwnerReport::destroy(array_column($this->data["reports"],"id"));
        Unit::destroy(array_column($this->data["units"],"id"));
        TaxRate::destroy($this->data["taxRate"]['id']);
        parent::tearDown();
    }
}
