diff --git a/app/Jobs/Inventory/AdjustProductInventory.php b/app/Jobs/Inventory/AdjustProductInventory.php index 49cba52cecfb..7846a5ffdc28 100644 --- a/app/Jobs/Inventory/AdjustProductInventory.php +++ b/app/Jobs/Inventory/AdjustProductInventory.php @@ -61,6 +61,44 @@ class AdjustProductInventory implements ShouldQueue return $this->newInventoryAdjustment(); } + public function handleDeletedInvoice() + { + + MultiDB::setDb($this->company->db); + + foreach ($this->invoice->line_items as $item) { + + $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + + if (! $p) { + continue; + } + + $p->in_stock_quantity += $item->quantity; + + $p->saveQuietly(); + } + + } + + public function handleRestoredInvoice() + { + + MultiDB::setDb($this->company->db); + + foreach ($this->invoice->line_items as $item) { + $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + + if (! $p) { + continue; + } + + $p->in_stock_quantity -= $item->quantity; + $p->saveQuietly(); + } + + } + public function middleware() { return [new WithoutOverlapping($this->company->company_key)]; diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index fb33536b29cc..412013aceb4c 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\Jobs\Inventory\AdjustProductInventory; use App\Models\Invoice; use App\Models\Paymentable; use App\Services\AbstractService; @@ -68,6 +69,11 @@ class HandleRestore extends AbstractService ->setAdjustmentAmount() ->adjustPayments(); + if ($this->invoice->company->track_inventory) { + (new AdjustProductInventory($this->invoice->company, $this->invoice, []))->handleRestoredInvoice(); + } + + return $this->invoice; } diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 4b61f0a6f4c3..b16c468c7c1e 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\Jobs\Inventory\AdjustProductInventory; use App\Jobs\Ninja\TransactionLog; use App\Models\Invoice; use App\Models\TransactionEvent; @@ -22,7 +23,7 @@ class MarkInvoiceDeleted extends AbstractService { use GeneratesCounter; - private $invoice; + public $invoice; private $adjustment_amount = 0; @@ -41,6 +42,10 @@ class MarkInvoiceDeleted extends AbstractService return $this->invoice; } + if ($this->invoice->company->track_inventory) { + (new AdjustProductInventory($this->invoice->company, $this->invoice, []))->handleDeletedInvoice(); + } + $this->cleanup() ->setAdjustmentAmount() ->deletePaymentables() diff --git a/tests/Feature/Inventory/InventoryManagementTest.php b/tests/Feature/Inventory/InventoryManagementTest.php index a97ae0e6e025..eb62e19c884f 100644 --- a/tests/Feature/Inventory/InventoryManagementTest.php +++ b/tests/Feature/Inventory/InventoryManagementTest.php @@ -84,27 +84,23 @@ class InventoryManagementTest extends TestCase $this->assertEquals(90, $product->in_stock_quantity); - // $arr = $response->json(); - // $invoice_hashed_id = $arr['data']['id']; + $data = $response->json(); - // $invoice_item = new InvoiceItem; - // $invoice_item->type_id = 1; - // $invoice_item->product_key = $product->product_key; - // $invoice_item->notes = $product->notes; - // $invoice_item->quantity = 5; - // $invoice_item->cost = 100; + $invoice = Invoice::find($this->decodePrimaryKey($data['data']['id'])); - // $line_items2[] = $invoice_item; - // $invoice->line_items = $line_items2; + $invoice->service()->markDeleted()->save(); + $invoice->is_deleted = true; + $invoice->save(); + + $this->assertEquals(100, $product->fresh()->in_stock_quantity); + + $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($data['data']['id'])); + + $invoice->service()->handleRestore()->save(); + + $this->assertEquals(90, $product->fresh()->in_stock_quantity); - // $response = $this->withHeaders([ - // 'X-API-SECRET' => config('ninja.api_secret'), - // 'X-API-TOKEN' => $this->token, - // ])->put('/api/v1/invoices/'.$invoice_hashed_id, $invoice->toArray()) - // ->assertStatus(200); - // $product = $product->refresh(); - // $this->assertEquals(95, $product->in_stock_quantity); } }