<?php

namespace Tests\Unit;

use App\Carrier;
use App\WaybillAuditLog;
use App\Authority;
use App\WaybillPriceModel;
use App\Role;
use App\User;
use App\Waybill;
use App\WaybillPayoff;
use App\WaybillFinancialExcepted;
use App\WaybillFinancialSnapshot;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class WaybillTest extends TestCase
{
    public function testUserMark(){
        $userMark=factory(User::class)->create();
        $this->assertNotEmpty($userMark->id);
        return $userMark;
    }

    public function testRole(){
        $role=Role::create([
            'name'=>'测试admin'
        ]);
        $this->assertNotEmpty($role->id);
        $authorities= Authority::get();
        foreach ($authorities as $authority){
            DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
        }
        return $role;
    }

    /**
     * @depends testRole
     */
    public function testUser(Role $role){
        $user=factory(User::class)->create();
        DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
        $this->assertNotEmpty($user->id);
        return $user;
    }

    public function testAddWaybill(){
        $waybill=new Waybill([
            'type'=>'直发车',
            'waybill_number'=>'TestBSZF12345848854',
            'owner_id'=>1,
            'wms_bill_number'=>'test1858545465',
            'origination'=>'test',
            'destination'=>'test',
            'recipient'=>'test sister',
            'recipient_mobile'=>'025487456',
            'charge'=>586.5,
            'collect_fee'=>30,
            'ordering_remark'=>'test'
        ]);
        $result=$waybill->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($waybill->charge);
        return $waybill;
    }

    public function testAddWaybillZX(){
        $waybillZX=new Waybill([
            'type'=>'专线',
            'status'=>'未审核',
            'waybill_number'=>'testBSZX123451468',
            'owner_id'=>1,
            'wms_bill_number'=>'test01547860548',
            'origination'=>'上海',
            'destination'=>'郑州',
            'recipient'=>'elder sister',
            'recipient_mobile'=>'12364851478',
            'charge'=>586,
            'ordering_remark'=>'禁'
        ]);
        $waybillZX->save();
        $this->assertNotEmpty($waybillZX);
        return $waybillZX;
    }

    public function testAddWaybillPriceModel(){
        $waybillPriceModel=WaybillPriceModel::create([
            'logistic_id'=>1,
            'province_id'=>1,
            'city_id'=>1,
            'unit_id'=>1,
            'range_min'=>0,
            'range_max'=>2000,
            'unit_price'=>5,
            'base_fee'=>10,
            'initial_weight'=>10
        ]);
        $this->assertNotEmpty($waybillPriceModel);
        return $waybillPriceModel;
    }

    public function testAddWaybillPriceModelTwo(){
        $waybillPriceModelTwo=WaybillPriceModel::create([
            'logistic_id'=>1,
            'province_id'=>1,
            'unit_id'=>3,
            'unit_price'=>5,
            'base_fee'=>10,
            'initial_weight'=>10
        ]);
        $this->assertNotEmpty($waybillPriceModelTwo);
        return $waybillPriceModelTwo;
    }

    public function testAddCarrier(){
        $carrier=Carrier::create([
            'name'=>'test承运商',
            'mobile'=>'000005416',
            'remark'=>'test',
            'delivery_fee'=>170
        ]);
        $this->assertNotEmpty($carrier);
        return $carrier;
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
   public function testIndex(User $user,Waybill $waybill,User $userMark){
        $responseFalse=$this->actingAs($user)->get('waybill/index',[
            'waybill_number'=>'TestBSZF12345848854'
        ]);
       $responseFalseZX=$this->actingAs($user)->get('waybill/index/ZX');
       $responseFalseZF=$this->actingAs($user)->get('waybill/index/ZF');
       $responseFalseZX->assertOk();
       $responseFalseZF->assertOk();
       $responseFalse->assertOk()->assertSee('TestBSZF12345848854');
       $responseMark=$this->actingAs($userMark)->get('waybill/index',[
           'waybill_number'=>'TestBSZF12345848854'
       ]);
       $responseMark->assertStatus(302)->assertRedirect('/');
       $responseTrue=$this->actingAs($user)->get('waybill');
       $responseTrue->assertOk()->assertSee('TestBSZF12345848854');
   }

    /**
     * @depends testUser
     * @depends testUserMark
     */
   public function testCreate(User $user,User $userMark){
        $response=$this->actingAs($user)->get('waybill/create/ZX');
        $response->assertOk()->assertSee('WMS单号');
       $responseZF=$this->actingAs($user)->get('waybill/create/ZF');
       $responseZF->assertOk();
       $responseMark=$this->actingAs($userMark)->get('waybill/create/ZX');
       $responseMark->assertStatus(302)->assertRedirect('/');
   }

    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('waybill',[
            'type'=>'专线',
            'owner_id'=>1,
            'wms_bill_number'=>'ad544da6584',
            'origination'=>'test上海',
            'destination'=>'test商丘',
            'recipient'=>'test周某',
            'recipient_mobile'=>'01547896548',
            'charge'=>58.6,
            'ordering_remark'=>'test'
        ]);
        $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->post('waybill',[
            'type'=>'专线',
            'owner_id'=>1,
            'wms_bill_number'=>'ad544da6584',
            'origination'=>'test上海',
            'destination'=>'test商丘',
            'recipient'=>'test周某',
            'recipient_mobile'=>'01547896548',
            'charge'=>58.6,
            'ordering_remark'=>'test'
        ]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
    public function testEdit(User $user,Waybill $waybill,User $userMark){
        $response=$this->actingAs($user)->get('waybill/'.$waybill->id.'/edit');
        $response->assertOk()->assertSee('TestBSZF12345848854');
        $responseMark=$this->actingAs($userMark)->get('waybill/'.$waybill->id.'/edit');
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddWaybillPriceModel
     * @depends testAddWaybillPriceModelTwo
     */
    public function testIsWaybillPriceModel(User $user,WaybillPriceModel $waybillPriceModel,WaybillPriceModel $waybillPriceModelTwo){
        $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
            'logistic_id'=>'1',
            'destination_city_id'=>'1',
            'carrier_weight'=>['50','20'],
            'carrier_weight_unit_id'=>['1','1'],
        ]);
        $response->assertOk()->assertJson(['success'=>$waybillPriceModel->id]);
        $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
            'logistic_id'=>'2',
            'destination_city_id'=>'2',
            'carrier_weight'=>['50','20'],
            'carrier_weight_unit_id'=>['1','1'],
        ]);
        $response->assertOk()->assertJson(['success'=>false]);
        $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
            'logistic_id'=>'1',
            'destination_city_id'=>'2',
            'carrier_weight'=>['50','20'],
            'carrier_weight_unit_id'=>['1','1'],
        ]);
        $response->assertOk()->assertJson(['success'=>$waybillPriceModelTwo->id]);
    }


    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     * @depends testAddWaybillZX
     * @depends testAddWaybillPriceModel
     * @depends testAddCarrier
     */
    public function testUpdate(User $user,Waybill $waybill,User $userMark,Waybill $waybillZX,WaybillPriceModel $waybillPriceModel,Carrier $carrier){
//直发车数据校验通过
        $responseZFTrue=$this->actingAs($user)->put('waybill/'.$waybill->id,[
            'type'=>'直发车',
            'logistic_id'=>1,
            'carrier_bill'=>'0154786548',
            'carType_id'=>1,
            'fee'=>500,
            'other_fee'=>20,
            'collect_fee'=>30,
            'dispatch_remark'=>''
        ]);
        $responseZFTrue->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
        $responseZFMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
            'type'=>'直发车',
            'logistic_id'=>1,
            'carrier_bill'=>'0154786548',
            'carType_id'=>1,
            'fee'=>500,
            'other_fee'=>20,
            'collect_fee'=>30,
            'dispatch_remark'=>''
        ]);
        $responseZFMark->assertStatus(302)->assertRedirect('/');
        $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
        $this->assertNotEmpty($waybillPayoffs);
        $this->assertEquals(490.00,$waybillPayoffs->total_expense);
        $this->assertEquals(586.50,$waybillPayoffs->total_receivable);
        $this->assertEquals(96.50,$waybillPayoffs->gross_margin);
//直发车数据校验失败
        $responseZFFalse=$this->actingAs($user)->put('waybill/'.$waybill->id,[
            'type'=>'直发车',
            'logistic_id'=>1,
            'carrier_bill'=>'20',
            'other_fee'=>'a456d',
            'collect_fee'=>30,
            'dispatch_remark'=>''
        ]);
        $responseZFFalse->assertStatus(302)->assertRedirect('/');          //保留
//无模型
        $responseZXFalse=$this->actingAs($user)->put('waybill/'.$waybillZX->id,[
            'type'=>'专线',
            'logistic_id'=>1,
            'carrier_bill'=>'01547860548',
            'origination_city_id'=>1,
            'destination_city_id'=>2,
            'warehouse_weight'=>25,
            'warehouse_weight_unit_id'=>1,
            'carrier_weight'=>25,
            'carrier_weight_unit_id'=>1,
            'pick_up_fee'=>500,
            'other_fee'=>20,
            'dispatch_remark'=>''
        ]);
        $responseZXFalse->assertStatus(302)->assertRedirect('waybill');
        $responseZXMark=$this->actingAs($userMark)->put('waybill/'.$waybillZX->id,[
            'type'=>'专线',
            'logistic_id'=>1,
            'carrier_bill'=>'01547860548',
            'origination_city_id'=>1,
            'destination_city_id'=>2,
            'warehouse_weight'=>25,
            'warehouse_weight_unit_id'=>1,
            'carrier_weight'=>25,
            'carrier_weight_unit_id'=>1,
            'pick_up_fee'=>500,
            'other_fee'=>20,
            'dispatch_remark'=>''
        ]);
        $responseZXMark->assertStatus(302)->assertRedirect('/');
        $fee=Waybill::find($waybillZX->id);
        $this->assertEquals(null,$fee->fee);
//有模型
        $responseZX=$this->actingAs($user)->put('waybill/'.$waybillZX->id,[
            'type'=>'专线',
            'logistic_id'=>$carrier->id,
            'carrier_bill'=>'test01547860548',
            'origination_city_id'=>1,
            'destination_city_id'=>1,
            'warehouse_weight'=>25,
            'warehouse_weight_unit_id'=>1,
            'carrier_weight'=>50,
            'carrier_weight_unit_id'=>1,
            'carrier_weight_other'=>5,
            'carrier_weight_unit_id_other'=>2,
            'pick_up_fee'=>20,
            'other_fee'=>20,
            'waybillPriceModel'=>$waybillPriceModel->id,
            'dispatch_remark'=>''
        ]);
        $responseZX->assertStatus(302)->assertRedirect('waybill');
        $waybillPayoff=WaybillPayoff::where('waybill_id',$waybillZX->id)->first();
        $this->assertNotEmpty($waybillPayoff);
        $this->assertEquals(460.00,$waybillPayoff->total_expense);
        $this->assertEquals(126.00,$waybillPayoff->gross_margin);
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
    public function testWaybillUpdate(User $user,Waybill $waybill,User $userMark){
        $response=$this->actingAs($user)->put('waybill/'.$waybill->id,[
            'type'=>'直发车',
            'logistic_id'=>2,
            'carrier_bill'=>'5a4d664585',
            'carType_id'=>2,
            'fee'=>600,
            'other_fee'=>30,
            'collect_fee'=>50,
            'dispatch_remark'=>'测试更改'
        ]);
        $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
            'type'=>'直发车',
            'logistic_id'=>2,
            'carrier_bill'=>'5a4d664585',
            'carType_id'=>2,
            'fee'=>600,
            'other_fee'=>30,
            'collect_fee'=>50,
            'dispatch_remark'=>'测试更改'
        ]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
    public function testWaybillAudit(User $user,Waybill $waybill,User $userMark){
        //第一次请求成功
        $response=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
        $response->assertJson(['success'=>true,'status'=>'已审核']);
        $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        //第二次请求拦截
        $responseRe=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
        $responseRe->assertJson(['exception'=>'请勿重复审核！']);
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
    public function testWaybillEdit(User $user,Waybill $waybill,User $userMark){
        $response=$this->actingAs($user)->get('waybill/waybillEdit/'.$waybill->id);
        $response->assertOk();
        $responseMark=$this->actingAs($userMark)->get('waybill/waybillEdit/'.$waybill->id);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     */
    public function testWaybillRetreatAudit(User $user,Waybill $waybill,User $userMark){
        $response=$this->actingAs($user)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
        $response->assertJson(['success'=>true,'status'=>'待重审']);
        $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddWaybill
     * @depends testUserMark
     * @depends testAddWaybillZX
     */
    public function testWaybillEndAudit(User $user,Waybill $waybill,User $userMark,Waybill $waybillZX){
        //第一次请求成功
        $response=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
        $response->assertJson(['success'=>true]);
        $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        //异常时
        $waybillZX->waybill_price_model_id=null;
        $waybillZX->save();
        $response=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybillZX->id]);
        $response->assertJson(['success'=>true]);
        //第二次请求拦截
        $responseRe=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
        $responseRe->assertJson(['exception'=>'请勿重复审核！']);
    }


    /**
     * @depends testAddWaybill
     * @depends testUser
     * @depends testUserMark
     * @depends testRole
     * @depends testAddWaybillZX
     * @depends testAddWaybillZX
     * @depends testAddWaybillPriceModel
     * @depends testAddCarrier
     * @depends testAddWaybillPriceModelTwo
     */
    public function testDestroy(Waybill $waybill,User $user,User $userMark,Role $role,Waybill $waybillZX,WaybillPriceModel $waybillPriceModel,Carrier $carrier,WaybillPriceModel $waybillPriceModelTwo){
        $this->assertNotEmpty($waybill);
        $result=$waybill->delete();
        $this->assertTrue($result);
        $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
        $this->assertNotEmpty($waybillPayoffs);
        $resultWaybillPayoffs=$waybillPayoffs->delete();
        $this->assertTrue($resultWaybillPayoffs);
        $waybillStore=Waybill::where('wms_bill_number','=','ad544da6584')->first();
        $this->assertNotEmpty($waybillStore);
        $resultWaybillStore=$waybillStore->delete();
        $this->assertTrue($resultWaybillStore);
        $waybillPayoffsZX=WaybillPayoff::where('waybill_id','=',$waybillZX->id)->first();
        $this->assertNotEmpty($waybillPayoffsZX);
        $waybillPayoffsZX->delete();
        $result=$waybillZX->delete();
        $this->assertTrue($result);
        $this->assertNotEmpty($waybillPriceModel);
        $resultWaybillPriceModel=$waybillPriceModel->delete();
        $this->assertTrue($resultWaybillPriceModel);
        $this->assertNotEmpty($waybillPriceModelTwo);
        $resultWaybillPriceModelTwo=$waybillPriceModelTwo->delete();
        $this->assertTrue($resultWaybillPriceModelTwo);
        $this->assertNotEmpty($carrier);
        $resultCarrier=$carrier->delete();
        $this->assertTrue($resultCarrier);
        $waybillFinancialSnapshots=WaybillFinancialSnapshot::where('waybill_id','=',$waybill->id)->first();
        $r=$waybillFinancialSnapshots->delete();
        $this->assertTrue($r);
        $waybillFinancialSnapshotsZX=WaybillFinancialExcepted::where('waybill_id','=',$waybillZX->id)->first();
        $resultZX=$waybillFinancialSnapshotsZX->delete();
        $this->assertTrue($resultZX);
        $isAudit=WaybillAuditLog::withTrashed()->whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"运单阶段"])->first();
        $resultWaybillAuditLogWaybill=$isAudit->forceDelete();
        $this->assertTrue($resultWaybillAuditLogWaybill);
        $waybillAuditLogDispatch=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"调度阶段"])->first();
        $resultWaybillAuditLogDispatch=$waybillAuditLogDispatch->forceDelete();
        $this->assertTrue($resultWaybillAuditLogDispatch);
        $result=$user->delete();
        $this->assertTrue($result);
        $this->assertNotEmpty($userMark->id);
        $resultUserMark=$userMark->delete();
        $this->assertTrue($resultUserMark);
        $resultRole=$role->delete();
        $this->assertTrue($resultRole);
        DB::table('authority_role')->where('id_role','=',$role->id)->delete();
        DB::table('user_role')->where('id_role','=',$role->id)->delete();
    }


}
