diff --git a/database/migrations/2024_01_09_084515_product_cost_field_population.php b/database/migrations/2024_01_09_084515_product_cost_field_population.php new file mode 100644 index 000000000000..4397f8e0fab7 --- /dev/null +++ b/database/migrations/2024_01_09_084515_product_cost_field_population.php @@ -0,0 +1,50 @@ +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 + { + // + } +}; diff --git a/tests/Feature/ProductTest.php b/tests/Feature/ProductTest.php index 7a709e398d33..8f39621a7de9 100644 --- a/tests/Feature/ProductTest.php +++ b/tests/Feature/ProductTest.php @@ -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([