<?php namespace Bixo\Account\Actions; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Litepie\Actions\Concerns\AsAction; use Bixo\Account\Models\Costcenter; class CostcenterAction { use AsAction; protected $model; protected $namespace = 'bixo.account.costcenter'; protected $eventClass = \Bixo\Account\Events\CostcenterAction::class; protected $action; protected $function; protected $request; public function handle(string $action, Costcenter $costcenter, array $request = []) { $this->action = $action; $this->request = $request; $this->model = $costcenter; $this->function = Str::camel($action); $this->executeAction(); return $this->model; } public function store(Costcenter $costcenter, array $request) { $attributes = $request; $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $costcenter = $costcenter->create($attributes); return $costcenter; } public function update(Costcenter $costcenter, array $request) { $attributes = $request; $costcenter->update($attributes); return $costcenter; } public function destroy(Costcenter $costcenter, array $request) { $costcenter->delete(); return $costcenter; } public function copy(Costcenter $costcenter, array $request) { $count = $request['count'] ?: 1; if ($count == 1) { $costcenter = $costcenter->replicate(); $costcenter->created_at = Carbon::now(); $costcenter->save(); return $costcenter; } for ($i = 1; $i <= $count; $i++) { $new = $costcenter->replicate(); $new->created_at = Carbon::now(); $new->save(); } return $costcenter; } }