mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-09 20:52:56 +01:00
Tests for product cost migration
This commit is contained in:
parent
64793841d4
commit
293ad19bd8
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
use App\Utils\Ninja;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if(Ninja::isHosted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Invoice::withTrashed()
|
||||
->where('is_deleted', false)
|
||||
->cursor()
|
||||
->each(function (Invoice $invoice) {
|
||||
|
||||
|
||||
$line_items = $invoice->line_items;
|
||||
|
||||
foreach ($line_items as $key => $item)
|
||||
{
|
||||
|
||||
if($item?->product_cost == 0 && $product = Product::where('company_id', $invoice->company_id)->where('product_key', $item->product_key)->where('cost', '>', 0)->first())
|
||||
{
|
||||
$line_items[$key]->product_cost = $product->cost;
|
||||
}
|
||||
}
|
||||
|
||||
$invoice->line_items = $line_items;
|
||||
$invoice->saveQuietly();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
};
|
@ -11,14 +11,16 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\DataMapper\InvoiceItem;
|
||||
use Tests\TestCase;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Product;
|
||||
use Tests\MockAccountData;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* @test
|
||||
@ -49,6 +51,72 @@ class ProductTest extends TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testProductCostMigration()
|
||||
{
|
||||
$items = [];
|
||||
|
||||
$item = new InvoiceItem();
|
||||
$item->product_cost = 0;
|
||||
$item->product_key = 'test';
|
||||
$item->quantity = 1;
|
||||
$item->cost = 10;
|
||||
$item->notes = 'product';
|
||||
|
||||
$items[] = $item;
|
||||
|
||||
$p = Product::factory()
|
||||
->create([
|
||||
'user_id' => $this->user->id,
|
||||
'company_id' => $this->company->id,
|
||||
'product_key' => 'test',
|
||||
'cost' => 10,
|
||||
'price' => 20,
|
||||
'quantity' => 1,
|
||||
'notes' => 'product',
|
||||
]);
|
||||
|
||||
$i = Invoice::factory()
|
||||
->create([
|
||||
'client_id' => $this->client->id,
|
||||
'company_id' => $this->company->id,
|
||||
'user_id' => $this->user->id,
|
||||
'line_items' => $items,
|
||||
]);
|
||||
|
||||
|
||||
$line_items = $i->line_items;
|
||||
|
||||
$this->assertEquals(0, $line_items[0]->product_cost);
|
||||
|
||||
Invoice::withTrashed()
|
||||
->where('is_deleted', false)
|
||||
->cursor()
|
||||
->each(function (Invoice $invoice) {
|
||||
|
||||
|
||||
$line_items = $invoice->line_items;
|
||||
|
||||
foreach ($line_items as $key => $item) {
|
||||
|
||||
if($item?->product_cost == 0 && $product = Product::where('company_id', $invoice->company_id)->where('product_key', $item->product_key)->where('cost', '>', 0)->first()) {
|
||||
$line_items[$key]->product_cost = $product->cost;
|
||||
}
|
||||
}
|
||||
|
||||
$invoice->line_items = $line_items;
|
||||
$invoice->saveQuietly();
|
||||
|
||||
});
|
||||
|
||||
|
||||
$i = $i->fresh();
|
||||
$line_items = $i->line_items;
|
||||
|
||||
$this->assertEquals(10, $line_items[0]->product_cost);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testSetTaxId()
|
||||
{
|
||||
$p = Product::factory()->create([
|
||||
|
Loading…
Reference in New Issue
Block a user