diff --git a/app/InvoiceItem.php b/app/InvoiceItem.php index ec999b8..aa9ca37 100644 --- a/app/InvoiceItem.php +++ b/app/InvoiceItem.php @@ -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); @@ -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. * @@ -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. * diff --git a/app/Providers/InventoryManagementProvider.php b/app/Providers/InventoryManagementProvider.php index cb988b3..d947f3f 100644 --- a/app/Providers/InventoryManagementProvider.php +++ b/app/Providers/InventoryManagementProvider.php @@ -5,6 +5,7 @@ use Illuminate\Support\ServiceProvider; use App\Item; use App\InvoiceItem; +use App\Invoice; use Log; class InventoryManagementProvider extends ServiceProvider @@ -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 @@ -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) { @@ -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) { diff --git a/database/migrations/2016_06_01_221835_add_soft_delete_to_invoice_items_table.php b/database/migrations/2016_06_01_221835_add_soft_delete_to_invoice_items_table.php new file mode 100644 index 0000000..1edb4a2 --- /dev/null +++ b/database/migrations/2016_06_01_221835_add_soft_delete_to_invoice_items_table.php @@ -0,0 +1,31 @@ +softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('invoice_items', function (Blueprint $table) { + // + }); + } +}