mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 00:04:30 -04:00
Remove inventory adjustments when deleting a invoice
This commit is contained in:
parent
bfcfdf2738
commit
c4e0295b32
@ -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)];
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user