<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOwnerPriceSystemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('owner_price_systems', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("owner_id")->index()->comment("货主");
            $table->decimal("usage_fee",9,3)->default(0)->comment("使用费");
            $table->char("operation",1)->index()->nullable()->comment("操作类型");
            $table->bigInteger("target_id")->index()->nullable()->comment("目标ID");
            $table->bigInteger("tax_rate_id")->nullable()->comment("税率");
            $table->timestamps();
        });
        \App\Authority::query()->firstOrCreate(["name"=>"计费模型-系统-录入"],["name"=>"计费模型-系统-录入","alias_name"=>"计费模型-系统-录入"]);
        Schema::table("logistics",function (Blueprint $table){
            $table->char("is_bunched")->default("N")->comment("是否子母件");
        });
        $tables = ["owner_storage_price_models","owner_price_operations","owner_price_logistics","owner_price_expresses","owner_price_direct_logistics"];
        foreach ($tables as $t){
            Schema::table($t, function (Blueprint $table) {
                $table->bigInteger("tax_rate_id")->nullable()->comment("税率");
            });
        }
        Schema::table("owner_fee_details", function (Blueprint $table) {
            $table->decimal("work_tax_fee",9,3)->nullable()->comment("作业税费");
            $table->decimal("logistic_tax_fee",9,3)->nullable()->comment("物流税费");
        });
        Schema::table("owner_bill_reports", function (Blueprint $table) {
            $table->decimal("other_fee",9,3)->nullable()->comment("其他费用");
        });
        Schema::table("owner_fee_detail_logistics", function (Blueprint $table) {
            $table->decimal("tax_fee",9,3)->nullable()->comment("物流税费");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('owner_price_systems');
        \App\Authority::query()->where("alias_name","计费模型-系统-录入")->delete();
        Schema::table("logistics",function (Blueprint $table){
            $table->dropColumn("is_bunched");
        });
        $tables = ["owner_storage_price_models","owner_price_operations","owner_price_logistics","owner_price_expresses","owner_price_direct_logistics"];
        foreach ($tables as $t){
            Schema::table($t, function (Blueprint $table) {
                $table->dropColumn("tax_rate_id");
            });
        }
        Schema::table("owner_fee_details", function (Blueprint $table) {
            $table->dropColumn("work_tax_fee");
            $table->dropColumn("logistic_tax_fee");
        });
        Schema::table("owner_bill_reports", function (Blueprint $table) {
            $table->dropColumn("other_fee");
        });
        Schema::table("owner_fee_detail_logistics", function (Blueprint $table) {
            $table->dropColumn("tax_fee");
        });
    }
}
