- client and invoice scaffolding

This commit is contained in:
Peter Papp
2021-04-21 15:22:54 +02:00
parent aa585b60d5
commit 5a9583be5b
14 changed files with 648 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Http\Controllers\Oasis;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ClientController extends Controller
{
//
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Http\Controllers\Oasis;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvoiceController extends Controller
{
//
}

View File

@@ -0,0 +1,73 @@
<?php
use Laravel\Cashier\Cashier;
/**
* @param $invoice
* @param false $format
* @return float|int|mixed|string
*/
function invoice_total_discount($invoice, $format = false)
{
// Percent discount
if ($invoice['discount_type'] === 'percent') {
$discount = invoice_total_net($invoice) * ($invoice['discount_rate'] / 100);
if ($format) {
return Cashier::formatAmount($discount * 100, $invoice['currency'], 'cs');
}
return $discount;
}
// Value discount
if ($invoice['discount_type'] === 'value') {
if ($format) {
return Cashier::formatAmount($invoice['discount_rate'] * 100, $invoice['currency'], 'cs');
}
return $invoice['discount_rate'];
}
}
/**
* @param $invoice
* @param false $format
* @return float|int|string
*/
function invoice_total_net($invoice, $format = false)
{
$total = 0;
foreach ($invoice['items'] as $item) {
$total += $item['amount'] * $item['price'];
}
if ($format) {
return Cashier::formatAmount(($total * 100), $invoice['currency'], 'cs');
}
return $total;
}
/**
* @param $invoice
* @param false $format
* @return float|int|string
*/
function invoice_total_tax($invoice, $format = false)
{
$total = 0;
foreach ($invoice['items'] as $item) {
$total += ($item['amount'] * $item['price']) * ($item['tax_rate'] / 100);
}
if ($format) {
return Cashier::formatAmount(($total * 100), $invoice['currency'], 'cs');
}
return $total;
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Models\Oasis;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Client extends Model
{
use HasFactory;
public $guarded = ['id'];
public $incrementing = false;
protected $keyType = 'string';
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function invoices()
{
return $this->hasMany(Invoice::class, 'client_id', 'id');
}
protected static function boot()
{
parent::boot();
static::creating(function ($order) {
$order->id = Str::uuid();
});
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Models\Oasis;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Invoice extends Model
{
use HasFactory;
protected $casts = [
'items' => 'array',
'user' => 'array',
'client' => 'array',
];
public $guarded = ['id'];
public $incrementing = false;
protected $keyType = 'string';
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function client()
{
return $this->hasOne(Client::class, 'id', 'user_id');
}
protected static function boot()
{
parent::boot();
static::creating(function ($order) {
$order->id = Str::uuid();
});
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Traits;
use App\Models\Oasis\Client;
use App\Models\Oasis\Invoice;
use App\Models\Oasis\SubscriptionRequest;
trait Oasis
@@ -15,4 +17,14 @@ trait Oasis
{
return $this->hasOne(SubscriptionRequest::class);
}
public function clients()
{
return $this->hasMany(Client::class);
}
public function createdInvoices()
{
return $this->hasMany(Invoice::class);
}
}

View File

@@ -61,7 +61,8 @@
"database/factories"
],
"files": [
"app/Http/helpers.php"
"app/Http/helpers.php",
"app/Http/custom-helpers.php"
]
},
"autoload-dev": {

View File

@@ -0,0 +1,44 @@
<?php
namespace Database\Factories\Oasis;
use App\Models\Oasis\Client;
use Illuminate\Database\Eloquent\Factories\Factory;
class ClientFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Client::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'id' => $this->faker->uuid,
'user_id' => $this->faker->uuid,
'name' => $this->faker->company,
'email' => $this->faker->email,
'phone_number' => $this->faker->phoneNumber,
'address' => $this->faker->address,
'city' => $this->faker->city,
'postal_code' => $this->faker->postcode,
'country' => $this->faker->randomElement(
['SK', 'CZ', 'DE', 'FR']
),
'ico' => $this->faker->numberBetween(11111111, 99999999),
'dic' => $this->faker->numberBetween(11111111, 99999999),
'ic_dph' => 'CZ' . $this->faker->numberBetween(1111111111, 9999999999),
'created_at' => $this->faker->dateTimeBetween(
$startDate = '-36 months', $endDate = 'now', $timezone = null
),
];
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace Database\Factories\Oasis;
use App\Models\Oasis\Invoice;
use App\Models\User;
use Carbon\Carbon;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\Factory;
class InvoiceFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Invoice::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'id' => $this->faker->uuid,
'user_id' => $this->faker->uuid,
'client_id' => $this->faker->uuid,
'invoice_type' => $this->faker->randomElement(['invoice', 'advance_invoice']),
'invoice_number' => $this->faker->numberBetween(2120001, 2120999),
'variable_number' => $this->faker->numberBetween(2120001, 2120999),
'currency' => $this->faker->randomElement(['CZK', 'EUR']),
'author_name' => $this->faker->name,
'client' => [
'name' => $this->faker->company,
'email' => $this->faker->email,
'phone_number' => $this->faker->phoneNumber,
'address' => $this->faker->address,
'city' => $this->faker->city,
'postal_code' => $this->faker->postcode,
'country' => $this->faker->randomElement(
['SK', 'CZ', 'DE', 'FR']
),
'ico' => $this->faker->numberBetween(11111111, 99999999),
'dic' => $this->faker->numberBetween(11111111, 99999999),
'ic_dph' => 'CZ' . $this->faker->numberBetween(1111111111, 9999999999),
],
'user' => [
'name' => $this->faker->name,
'address' => $this->faker->address,
'state' => $this->faker->state,
'city' => $this->faker->city,
'postal_code' => $this->faker->postcode,
'country' => $this->faker->randomElement(
['SK', 'CZ', 'DE', 'FR']
),
'phone_number' => $this->faker->phoneNumber,
'bank_name' => $this->faker->randomElement(['Fio Banka', 'Tatra Banka']),
'iban' => $this->faker->iban('CZ'),
'swift' => $this->faker->swiftBicNumber,
],
'items' => [
[
'description' => $this->faker->realText(60),
'amount' => $this->faker->numberBetween(1, 3),
'tax_rate' => 20,
'price' => $this->faker->randomElement([120, 360, 400, 80, 90, 45, 16, 8]),
],
[
'description' => $this->faker->realText(60),
'amount' => $this->faker->numberBetween(1, 3),
'tax_rate' => 20,
'price' => $this->faker->randomElement([120, 360, 400, 80, 90, 45, 16, 8]),
],
],
'discount_type' => $this->faker->randomElement(['percent', 'value', null]),
'delivery_at' => $this->faker->dateTimeBetween(
$startDate = '-36 months', $endDate = 'now', $timezone = null
),
'created_at' => $this->faker->dateTimeBetween(
$startDate = '-36 months', $endDate = 'now', $timezone = null
),
];
}
/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterCreating(function (Invoice $invoice) {
$invoice->delivery_at = $invoice->created_at;
$invoice->due_at = Carbon::parse($invoice->created_at)->addWeeks(2);
if ($invoice->discount_type === 'percent') {
$invoice->discount_rate = $this->faker->randomElement([2, 5, 10, 15, 20]);
}
if ($invoice->discount_type === 'value') {
$invoice->discount_rate = $this->faker->randomElement([20, 10]);
}
$invoice->total_discount = invoice_total_discount($invoice);
$invoice->total_net = invoice_total_net($invoice);
$invoice->total_tax = invoice_total_tax($invoice);
$invoice->save();
});
}
}

View File

@@ -0,0 +1,58 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInvoicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('invoices', function (Blueprint $table) {
$table->uuid('id')->primary()->index();
$table->uuid('user_id')->index();
$table->uuid('client_id')->index();
$table->enum('invoice_type', ['invoice', 'advance_invoice']);
$table->text('invoice_number')->nullable();
$table->text('variable_number')->nullable();
$table->longText('client');
$table->longText('user');
$table->longText('items');
$table->dateTime('delivery_at')->nullable();
$table->dateTime('due_at')->nullable();
$table->enum('discount_type', ['percent', 'value'])->nullable();
$table->integer('discount_rate')->nullable();
$table->text('currency');
$table->integer('total_discount')->nullable();
$table->integer('total_net')->nullable();
$table->integer('total_tax')->nullable();
$table->text('author_stamp')->nullable();
$table->text('author_name')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoices');
}
}

View File

@@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->uuid('id')->primary()->index();
$table->uuid('user_id')->index();
$table->text('name');
$table->text('avatar')->nullable();
$table->text('email')->nullable();
$table->text('phone_number')->nullable();
$table->text('address')->nullable();
$table->text('city')->nullable();
$table->text('postal_code')->nullable();
$table->text('country')->nullable();
$table->text('ico')->nullable();
$table->text('dic')->nullable();
$table->text('ic_dph')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('clients');
}
}

View File

@@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddBankDetailsToUserSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_settings', function (Blueprint $table) {
$table->text('registration_notes')->nullable();
$table->text('dic')->nullable();
$table->text('ic_dph')->nullable();
$table->text('bank_name')->nullable();
$table->text('iban')->nullable();
$table->text('swift')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_settings', function (Blueprint $table) {
$table->dropColumn(['bank_name','iban','swift']);
});
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Tests\Feature\Oasis;
use App\Models\Oasis\Client;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class OasisClientTest extends TestCase
{
use DatabaseMigrations;
/**
* @test
*/
public function it_test_client_factory()
{
$client = Client::factory(Client::class)
->create();
$this->assertDatabaseHas('clients', [
'name' => $client->name,
]);
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Tests\Feature\Oasis;
use App\Models\Oasis\Invoice;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class OasisInvoiceTest extends TestCase
{
use DatabaseMigrations;
/**
* @test
*/
public function it_test_invoice_factory()
{
$invoice = Invoice::factory(Invoice::class)
->create();
$this->assertDatabaseHas('invoices', [
'id' => $invoice->id,
]);
}
/**
* @test
*/
public function it_test_invoice_total_net()
{
$invoice = [
'currency' => 'CZK',
'items' => [
[
'description' => 'Test 1',
'amount' => 1,
'tax_rate' => 20,
'price' => 20,
],
[
'description' => 'Test 2',
'amount' => 3,
'tax_rate' => 20,
'price' => 50,
],
]
];
$this->assertEquals(170, invoice_total_net($invoice));
$this->assertEquals('170,00 Kč', invoice_total_net($invoice, true));
}
/**
* @test
*/
public function it_test_invoice_total_discount_as_percent()
{
$invoice = [
'currency' => 'CZK',
'discount_type' => 'percent',
'discount_rate' => 15,
'items' => [
[
'description' => 'Test 1',
'amount' => 1,
'tax_rate' => 20,
'price' => 200,
],
]
];
$this->assertEquals(30, invoice_total_discount($invoice));
$this->assertEquals('30,00 Kč', invoice_total_discount($invoice, true));
}
/**
* @test
*/
public function it_test_invoice_total_discount_as_value()
{
$invoice = [
'currency' => 'CZK',
'discount_type' => 'value',
'discount_rate' => 18,
'items' => [
[
'description' => 'Test 1',
'amount' => 1,
'tax_rate' => 20,
'price' => 100,
],
]
];
$this->assertEquals(18, invoice_total_discount($invoice));
$this->assertEquals('18,00 Kč', invoice_total_discount($invoice, true));
}
/**
* @test
*/
public function it_test_invoice_total_tax()
{
$invoice = [
'currency' => 'CZK',
'discount_type' => 'value',
'discount_rate' => 18,
'items' => [
[
'description' => 'Test 1',
'amount' => 1,
'tax_rate' => 20,
'price' => 100,
],
[
'description' => 'Test 2',
'amount' => 2,
'tax_rate' => 10,
'price' => 100,
],
]
];
$this->assertEquals(40, invoice_total_tax($invoice));
$this->assertEquals('40,00 Kč', invoice_total_tax($invoice, true));
}
}