mass delete relationships
This commit is contained in:
		| @@ -2,11 +2,12 @@ | ||||
|  | ||||
| namespace App\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Routing\Route; | ||||
| use Illuminate\Foundation\Bus\DispatchesJobs; | ||||
| use Illuminate\Routing\Controller as BaseController; | ||||
| use Illuminate\Foundation\Validation\ValidatesRequests; | ||||
| use Illuminate\Database\Eloquent\Collection; | ||||
| use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||||
| use Illuminate\Foundation\Bus\DispatchesJobs; | ||||
| use Illuminate\Foundation\Validation\ValidatesRequests; | ||||
| use Illuminate\Routing\Controller as BaseController; | ||||
| use Illuminate\Routing\Route; | ||||
|  | ||||
| class Controller extends BaseController | ||||
| { | ||||
| @@ -81,4 +82,27 @@ class Controller extends BaseController | ||||
|  | ||||
|         redirect('apps/token/create')->send(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Mass delete relationships with events being fired. | ||||
|      * | ||||
|      * @param  $model | ||||
|      * @param  $tables | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function deleteRelationships($model, $tables) | ||||
|     { | ||||
|         foreach ((array) $tables as $table) { | ||||
|             $items = $model->$table->all(); | ||||
|  | ||||
|             if ($items instanceof Collection) { | ||||
|                 $items = $items->all(); | ||||
|             } | ||||
|  | ||||
|             foreach ((array) $items as $item) { | ||||
|                 $item->delete(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -8,8 +8,8 @@ use App\Events\BillUpdated; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Http\Requests\Expense\Bill as Request; | ||||
| use App\Http\Requests\Expense\BillPayment as PaymentRequest; | ||||
| use Illuminate\Http\Request as ItemRequest; | ||||
| use App\Models\Banking\Account; | ||||
| use App\Models\Common\Media; | ||||
| use App\Models\Expense\BillStatus; | ||||
| use App\Models\Expense\Vendor; | ||||
| use App\Models\Expense\Bill; | ||||
| @@ -21,7 +21,6 @@ use App\Models\Common\Item; | ||||
| use App\Models\Setting\Category; | ||||
| use App\Models\Setting\Currency; | ||||
| use App\Models\Setting\Tax; | ||||
| use App\Models\Common\Media; | ||||
| use App\Traits\Currencies; | ||||
| use App\Traits\DateTime; | ||||
| use App\Traits\Uploads; | ||||
| @@ -30,6 +29,7 @@ use App\Utilities\ImportFile; | ||||
| use App\Utilities\Modules; | ||||
| use Date; | ||||
| use File; | ||||
| use Illuminate\Http\Request as ItemRequest; | ||||
| use Image; | ||||
| use Storage; | ||||
|  | ||||
| @@ -384,7 +384,7 @@ class Bills extends Controller | ||||
|         $bill_item['bill_id'] = $bill->id; | ||||
|  | ||||
|         if ($request['item']) { | ||||
|             BillItem::where('bill_id', $bill->id)->delete(); | ||||
|             $this->deleteRelationships($bill, 'items'); | ||||
|  | ||||
|             foreach ($request['item'] as $item) { | ||||
|                 unset($tax_object); | ||||
| @@ -462,10 +462,9 @@ class Bills extends Controller | ||||
|         } | ||||
|  | ||||
|         // Delete previous bill totals | ||||
|         BillTotal::where('bill_id', $bill->id)->delete(); | ||||
|         $this->deleteRelationships($bill, 'totals'); | ||||
|  | ||||
|         // Add bill totals | ||||
|         $bill->totals()->delete(); | ||||
|         $this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total); | ||||
|  | ||||
|         // Recurring | ||||
| @@ -490,20 +489,9 @@ class Bills extends Controller | ||||
|      */ | ||||
|     public function destroy(Bill $bill) | ||||
|     { | ||||
|         $bill->recurring()->delete(); | ||||
|         $this->deleteRelationships($bill, ['items', 'histories', 'payments', 'recurring', 'totals']); | ||||
|         $bill->delete(); | ||||
|  | ||||
|         /* | ||||
|         $bill->items->delete(); | ||||
|         $bill->payments->delete(); | ||||
|         $bill->histories->delete(); | ||||
|         */ | ||||
|  | ||||
|         BillItem::where('bill_id', $bill->id)->delete(); | ||||
|         BillTotal::where('bill_id', $bill->id)->delete(); | ||||
|         BillPayment::where('bill_id', $bill->id)->delete(); | ||||
|         BillHistory::where('bill_id', $bill->id)->delete(); | ||||
|  | ||||
|         $message = trans('messages.success.deleted', ['type' => trans_choice('general.bills', 1)]); | ||||
|  | ||||
|         flash($message)->success(); | ||||
|   | ||||
| @@ -8,8 +8,8 @@ use App\Events\InvoiceUpdated; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Http\Requests\Income\Invoice as Request; | ||||
| use App\Http\Requests\Income\InvoicePayment as PaymentRequest; | ||||
| use Illuminate\Http\Request as ItemRequest; | ||||
| use App\Models\Banking\Account; | ||||
| use App\Models\Common\Media; | ||||
| use App\Models\Income\Customer; | ||||
| use App\Models\Income\Invoice; | ||||
| use App\Models\Income\InvoiceHistory; | ||||
| @@ -21,7 +21,6 @@ use App\Models\Common\Item; | ||||
| use App\Models\Setting\Category; | ||||
| use App\Models\Setting\Currency; | ||||
| use App\Models\Setting\Tax; | ||||
| use App\Models\Common\Media; | ||||
| use App\Notifications\Income\Invoice as Notification; | ||||
| use App\Notifications\Common\Item as ItemNotification; | ||||
| use App\Traits\Currencies; | ||||
| @@ -33,6 +32,7 @@ use App\Utilities\ImportFile; | ||||
| use App\Utilities\Modules; | ||||
| use Date; | ||||
| use File; | ||||
| use Illuminate\Http\Request as ItemRequest; | ||||
| use Image; | ||||
| use Storage; | ||||
|  | ||||
| @@ -405,7 +405,7 @@ class Invoices extends Controller | ||||
|         $invoice_item['invoice_id'] = $invoice->id; | ||||
|  | ||||
|         if ($request['item']) { | ||||
|             InvoiceItem::where('invoice_id', $invoice->id)->delete(); | ||||
|             $this->deleteRelationships($invoice, 'items'); | ||||
|  | ||||
|             foreach ($request['item'] as $item) { | ||||
|                 unset($tax_object); | ||||
| @@ -483,10 +483,9 @@ class Invoices extends Controller | ||||
|         } | ||||
|  | ||||
|         // Delete previous invoice totals | ||||
|         InvoiceTotal::where('invoice_id', $invoice->id)->delete(); | ||||
|         $this->deleteRelationships($invoice, 'totals'); | ||||
|  | ||||
|         // Add invoice totals | ||||
|         $invoice->totals()->delete(); | ||||
|         $this->addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total); | ||||
|  | ||||
|         // Recurring | ||||
| @@ -511,20 +510,9 @@ class Invoices extends Controller | ||||
|      */ | ||||
|     public function destroy(Invoice $invoice) | ||||
|     { | ||||
|         $invoice->recurring()->delete(); | ||||
|         $this->deleteRelationships($invoice, ['items', 'histories', 'payments', 'recurring', 'totals']); | ||||
|         $invoice->delete(); | ||||
|  | ||||
|         /* | ||||
|         $invoice->items->delete(); | ||||
|         $invoice->payments->delete(); | ||||
|         $invoice->histories->delete(); | ||||
|         */ | ||||
|  | ||||
|         InvoiceItem::where('invoice_id', $invoice->id)->delete(); | ||||
|         InvoiceTotal::where('invoice_id', $invoice->id)->delete(); | ||||
|         InvoicePayment::where('invoice_id', $invoice->id)->delete(); | ||||
|         InvoiceHistory::where('invoice_id', $invoice->id)->delete(); | ||||
|  | ||||
|         $message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]); | ||||
|  | ||||
|         flash($message)->success(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user