1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/database/migrations/2016_04_23_182223_payments_changes.php

171 lines
6.1 KiB
PHP
Raw Normal View History

2016-04-23 22:40:19 +02:00
<?php
use App\Models\PaymentStatus;
2016-04-23 22:40:19 +02:00
use Illuminate\Database\Migrations\Migration;
class PaymentsChanges extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('payment_statuses');
2016-05-09 22:29:02 +02:00
2017-01-30 17:05:31 +01:00
Schema::create('payment_statuses', function ($table) {
2016-04-23 22:40:19 +02:00
$table->increments('id');
$table->string('name');
});
2016-05-09 22:29:02 +02:00
$statuses = [
['id' => '1', 'name' => 'Pending'],
['id' => '2', 'name' => 'Voided'],
['id' => '3', 'name' => 'Failed'],
['id' => '4', 'name' => 'Completed'],
['id' => '5', 'name' => 'Partially Refunded'],
['id' => '6', 'name' => 'Refunded'],
];
Eloquent::unguard();
foreach ($statuses as $status) {
$record = PaymentStatus::find($status['id']);
if ($record) {
$record->name = $status['name'];
$record->save();
} else {
PaymentStatus::create($status);
}
}
Eloquent::reguard();
2016-05-09 22:29:02 +02:00
Schema::dropIfExists('payment_methods');
2017-01-30 17:05:31 +01:00
Schema::create('payment_methods', function ($table) {
$table->increments('id');
$table->unsignedInteger('account_id');
2016-06-20 16:14:43 +02:00
$table->unsignedInteger('user_id');
$table->unsignedInteger('contact_id')->nullable();
$table->unsignedInteger('account_gateway_token_id');
$table->unsignedInteger('payment_type_id');
$table->string('source_reference');
$table->unsignedInteger('routing_number')->nullable();
$table->smallInteger('last4')->unsigned()->nullable();
$table->date('expiration')->nullable();
$table->string('email')->nullable();
$table->unsignedInteger('currency_id')->nullable();
$table->string('status')->nullable();
$table->timestamps();
$table->softDeletes();
2016-11-29 20:04:08 +01:00
$table->unsignedInteger('public_id')->index();
});
2017-01-30 17:05:31 +01:00
Schema::table('payment_methods', function ($table) {
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
2016-06-20 16:14:43 +02:00
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
$table->foreign('account_gateway_token_id')->references('id')->on('account_gateway_tokens');
$table->foreign('payment_type_id')->references('id')->on('payment_types');
$table->foreign('currency_id')->references('id')->on('currencies');
2017-01-30 20:40:43 +01:00
$table->unique(['account_id', 'public_id']);
});
2017-01-30 17:05:31 +01:00
Schema::table('payments', function ($table) {
2016-04-23 22:40:19 +02:00
$table->decimal('refunded', 13, 2);
2016-05-06 23:05:42 +02:00
$table->unsignedInteger('payment_status_id')->default(PAYMENT_STATUS_COMPLETED);
2016-05-09 22:29:02 +02:00
$table->unsignedInteger('routing_number')->nullable();
$table->smallInteger('last4')->unsigned()->nullable();
$table->date('expiration')->nullable();
2016-04-30 23:54:56 +02:00
$table->text('gateway_error')->nullable();
2016-05-07 04:33:03 +02:00
$table->string('email')->nullable();
$table->unsignedInteger('payment_method_id')->nullable();
2016-04-23 22:40:19 +02:00
});
2017-01-30 17:05:31 +01:00
Schema::table('payments', function ($table) {
2016-09-05 07:18:22 +02:00
$table->foreign('payment_status_id')->references('id')->on('payment_statuses');
$table->foreign('payment_method_id')->references('id')->on('payment_methods');
});
2017-01-30 17:05:31 +01:00
Schema::table('invoices', function ($table) {
2016-05-09 22:29:02 +02:00
$table->boolean('client_enable_auto_bill')->default(false);
});
\DB::table('invoices')
->where('auto_bill', '=', 1)
2017-01-30 20:40:43 +01:00
->update(['client_enable_auto_bill' => 1, 'auto_bill' => AUTO_BILL_OPT_OUT]);
\DB::table('invoices')
->where('auto_bill', '=', 0)
->where('is_recurring', '=', 1)
2017-01-30 20:40:43 +01:00
->update(['auto_bill' => AUTO_BILL_OFF]);
2017-01-30 17:05:31 +01:00
Schema::table('account_gateway_tokens', function ($table) {
$table->unsignedInteger('default_payment_method_id')->nullable();
2016-09-05 07:18:22 +02:00
});
2017-01-30 17:05:31 +01:00
Schema::table('account_gateway_tokens', function ($table) {
2016-09-05 07:18:22 +02:00
$table->foreign('default_payment_method_id')->references('id')->on('payment_methods');
});
2016-04-23 22:40:19 +02:00
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
2017-01-30 17:05:31 +01:00
Schema::table('payments', function ($table) {
2016-04-23 22:40:19 +02:00
$table->dropColumn('refunded');
$table->dropForeign('payments_payment_status_id_foreign');
$table->dropColumn('payment_status_id');
2016-06-20 16:14:43 +02:00
$table->dropColumn('routing_number');
$table->dropColumn('last4');
$table->dropColumn('expiration');
2016-04-30 23:54:56 +02:00
$table->dropColumn('gateway_error');
2016-05-07 04:33:03 +02:00
$table->dropColumn('email');
2016-09-05 07:18:22 +02:00
$table->dropForeign('payments_payment_method_id_foreign');
$table->dropColumn('payment_method_id');
2016-04-23 22:40:19 +02:00
});
\DB::table('invoices')
->where('auto_bill', '=', AUTO_BILL_OFF)
2017-01-30 20:40:43 +01:00
->update(['auto_bill' => 0]);
2016-05-09 22:29:02 +02:00
\DB::table('invoices')
2017-01-30 17:05:31 +01:00
->where(function ($query) {
2016-05-09 22:29:02 +02:00
$query->where('auto_bill', '=', AUTO_BILL_ALWAYS);
2017-01-30 17:05:31 +01:00
$query->orwhere(function ($query) {
$query->where('auto_bill', '!=', 0);
2016-05-09 22:29:02 +02:00
$query->where('client_enable_auto_bill', '=', 1);
});
})
2017-01-30 20:40:43 +01:00
->update(['auto_bill' => 1]);
2016-05-09 22:29:02 +02:00
\DB::table('invoices')
->where('auto_bill', '!=', 1)
2017-01-30 20:40:43 +01:00
->update(['auto_bill' => 0]);
2016-05-09 22:29:02 +02:00
Schema::table('invoices', function ($table) {
2016-05-09 22:29:02 +02:00
$table->dropColumn('client_enable_auto_bill');
});
2016-06-20 16:14:43 +02:00
2016-04-23 22:40:19 +02:00
Schema::dropIfExists('payment_statuses');
2017-01-30 17:05:31 +01:00
Schema::table('account_gateway_tokens', function ($table) {
2016-09-05 07:18:22 +02:00
$table->dropForeign('account_gateway_tokens_default_payment_method_id_foreign');
$table->dropColumn('default_payment_method_id');
});
Schema::dropIfExists('payment_methods');
2016-04-23 22:40:19 +02:00
}
}