Skip to content

Commit

Permalink
added soft deletes to invoice_items
Browse files Browse the repository at this point in the history
  • Loading branch information
TJTorola committed Jun 1, 2016
1 parent df9be81 commit 16d7035
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
14 changes: 12 additions & 2 deletions app/InvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use App\Invoice;
use App\Item;

class InvoiceItem extends Model
{
use SoftDeletes;

public static function saveMany($cart, $invoice_id)
{
$invoice = Invoice::findOrFail($invoice_id);
Expand All @@ -30,8 +33,8 @@ public static function saveMany($cart, $invoice_id)
$invoice->items()->saveMany($invoice_items);
}



/**
* The accessors to append to the model's array form.
*
Expand All @@ -45,6 +48,13 @@ public static function saveMany($cart, $invoice_id)
*/
protected $casts = [];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];

/**
* The attributes that aren't mass assignable.
*
Expand Down
43 changes: 40 additions & 3 deletions app/Providers/InventoryManagementProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;
use App\Item;
use App\InvoiceItem;
use App\Invoice;
use Log;

class InventoryManagementProvider extends ServiceProvider
Expand Down Expand Up @@ -35,6 +36,20 @@ public function boot()
}
});

InvoiceItem::restored(function($invoice_item) {
if (isset($invoice_item['item_id'])) {
$item = Item::findOrFail($invoice_item['item_id']);
if (!$item->infinite) {
$item->stock -= $invoice_item->count;
$item->sold += $invoice_item->count;
if ($item->stock < 0) {
$item->stock = 0;
}
$item->save();
}
}
});

/**
* When updating a cart item, we find that item and compare
* the old count value to the new and use this value to
Expand All @@ -55,10 +70,14 @@ public function boot()

/**
* When deleting a cart item, you need to make sure and
* add those items back to the inventory, also, make
* add those items back to the inventory, also, make
* sure that it doesn't look like those item's were sold.
*/
InvoiceItem::deleting(function($invoice_item) {
if ($invoice_item->trashed()) {
return;
}

if (isset($invoice_item['item_id'])) {
$item = Item::findOrFail($invoice_item['item_id']);
if (!$item->infinite) {
Expand All @@ -72,9 +91,27 @@ public function boot()
}
});

Invoice::deleted(function($invoice) {
$invoice->items()->delete();
});

Invoice::restored(function($invoice) {
$invoice->items()->withTrashed()->restore();
});

Invoice::deleting(function($invoice) {
if ($invoice->trashed()) {
return;
}

foreach ($variable as $key => $value) {
# code...
}
});

/**
* If a item changes in a dramatic way, make sure to decouple
* the item from it's cart item's, because the cart item's
* If a item changes in a dramatic way, make sure to decouple
* the item from it's cart item's, because the cart item's
* should not change from what the customer ordered
*/
Item::updating(function($item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSoftDeleteToInvoiceItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('invoice_items', function (Blueprint $table) {
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('invoice_items', function (Blueprint $table) {
//
});
}
}

0 comments on commit 16d7035

Please sign in to comment.