From 8179fc2f90ab8e84df29fd99d9da254d367cbf7d Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 23 Apr 2016 02:07:40 -0700 Subject: [PATCH] Changed checkout method to be more generic, for checkin and checkout --- app/Models/Asset.php | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index e81a8f26fa..4594ad2f31 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -1,7 +1,7 @@ save()) { - $log_id = $this->createCheckoutLog($checkout_at, $admin, $user, $expected_checkin, $note); + // $action, $admin, $user, $expected_checkin = null, $note = null, $checkout_at = null + $log_id = $this->createLogRecord('checkout', $this, $admin, $user, $expected_checkin, $note, $checkout_at); if ((($this->requireAcceptance()=='1') || ($this->getEula())) && ($user->email!='')) { $this->checkOutNotifyMail($log_id, $user, $checkout_at, $expected_checkin, $note); @@ -113,7 +115,6 @@ class Asset extends Depreciable public function checkOutNotifyMail($log_id, $user, $checkout_at, $expected_checkin, $note) { - $data['log_id'] = $log_id; $data['eula'] = $this->getEula(); $data['first_name'] = $user->first_name; @@ -176,24 +177,39 @@ class Asset extends Depreciable return $this->rules; } - public function createCheckoutLog($checkout_at = null, $admin, $user, $expected_checkin = null, $note = null) + + public function createLogRecord($action, $asset, $admin, $user, $expected_checkin = null, $note = null, $checkout_at = null) { - $logaction = new \App\Models\Actionlog(); + $logaction = new Actionlog(); $logaction->asset_id = $this->id; $logaction->checkedout_to = $this->assigned_to; $logaction->asset_type = 'hardware'; - if ($user) { - $logaction->location_id = $user->location_id; - } - - $logaction->adminlog()->associate($admin); $logaction->note = $note; if ($checkout_at) { - $logaction->created_at = $checkout_at; + $logaction->created_at = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s', strtotime($checkout_at))); + } else { + $logaction->created_at = \Carbon\Carbon::now(); } - $log = $logaction->logaction('checkout'); - return $logaction->id; + + if ($action=="checkout") { + if ($user) { + $logaction->location_id = $user->location_id; + } + } else { + // Update the asset data to null, since it's being checked in + $logaction->checkedout_to = $asset->assigned_to; + $logaction->checkedout_to = ''; + $logaction->asset_id = $asset->id; + $logaction->location_id = null; + $logaction->asset_type = 'hardware'; + $logaction->note = $note; + $logaction->user_id = $admin->id; + } + $logaction->adminlog()->associate($admin); + $log = $logaction->logaction($action); + + return $logaction; }