diff --git a/framework/application/controller/widget/Home_Asset.php b/framework/application/controller/widget/Home_Asset.php index 995ce39..f4a1c22 100644 --- a/framework/application/controller/widget/Home_Asset.php +++ b/framework/application/controller/widget/Home_Asset.php @@ -22,6 +22,19 @@ function check_in($id) { echo json_encode($equipment->CheckIn()); } } + + function check_out($equipment, $person) { + if (($equipment = \Model\Equipment::Fetch($equipment)) && ($person = \Model\Person::Fetch($person))) { + + echo json_encode($equipment->CheckOut($person)); + } + } + + function switch_owner($equipment, $person) { + if (($equipment = \Model\Equipment::Fetch($equipment)) && ($person = \Model\Person::Fetch($person))) { + echo json_encode($equipment->SwitchOwner($person)); + } + } function search_list() { $search = explode(" ", $_GET['search']); @@ -46,4 +59,4 @@ function search_list() { } \Core\Router::loadView("widget/home_asset/list", array("results"=>$results)); } -} \ No newline at end of file +} diff --git a/framework/application/model/Equipment.php b/framework/application/model/Equipment.php index cad7f0d..7d9b6b2 100644 --- a/framework/application/model/Equipment.php +++ b/framework/application/model/Equipment.php @@ -56,4 +56,20 @@ public function CheckIn() { } return false; } -} \ No newline at end of file + + public function CheckOut(Person $p) { + if ($this->isCheckedOut()) { + return false; + } + \Model\EquipmentCheckout::Create(array("equipment"=>$this->id, "person"=>$p->id, "checkout"=>time())); + return true; + } + + public function SwitchOwner(Person $p) { + if ($this->isCheckedOut()) { + $this->CheckIn(); + } + \Model\EquipmentCheckout::Create(array("equipment"=>$this->id, "person"=>$p->id, "checkout"=>time())); + return true; + } +} diff --git a/framework/application/view/home.php b/framework/application/view/home.php index 7ff2015..ef3f3f8 100644 --- a/framework/application/view/home.php +++ b/framework/application/view/home.php @@ -19,6 +19,8 @@ class="form-control" id="person-id" />
+
@@ -59,4 +61,4 @@ class="form-control" /> \ No newline at end of file +?> diff --git a/htdocs/js/widget/home_asset.js b/htdocs/js/widget/home_asset.js index 5c36382..9518c48 100644 --- a/htdocs/js/widget/home_asset.js +++ b/htdocs/js/widget/home_asset.js @@ -8,6 +8,7 @@ $(document).ready(function() { }); $("#asset-checkout-btn").click(asset_checkout_btn); + $("#asset-switch-btn").click(asset_switch_btn); }); function asset_search(id, auto) { @@ -35,11 +36,11 @@ function _asset_search(id, auto) { return function(data) { $("#asset-description").html(data.html); if (data.exists && data.checkout) { - add_fastkey(32, "Space for Check-in asset #"+id, function(id) { + /*add_fastkey(32, "Space for Check-in asset #"+id, function(id) { return function() { asset_checkin(id); } - }(id)); + }(id));*/ } if (data.exists && data.in_service) { @@ -68,11 +69,16 @@ function _asset_search(id, auto) { } function asset_tryCheckoutActive() { - if (active_asset && active_person && !active_asset_out) { - $("#asset-checkout-btn").attr("disabled", false); - add_fastkey(32, "Space for Check-out asset #"+active_asset, asset_checkout_btn); - } else { - $("#asset-checkout-btn").attr("disabled", true); + $("#asset-checkout-btn").attr("disabled", true); + $("#asset-switch-btn").attr("disabled", true); + if (active_asset && active_person) { + if (!active_asset_out) { + //add_fastkey(79, "'O' for Check-out asset #"+active_asset, asset_checkout_btn); + $("#asset-checkout-btn").attr("disabled", false); + } else { + //add_fastkey(83, "'S' to switch asset #"+active_asset, asset_switch_btn); + $("#asset-switch-btn").attr("disabled", false); + } } } @@ -80,16 +86,48 @@ function asset_checkout_btn() { asset_checkout(active_asset, active_person); } +function asset_switch_btn() { + asset_switch(active_asset, active_person); +} + function asset_checkout(asset, person) { $.ajax({ - url: "/api/equipment_checkout", + url: "/widget/home_asset/check_out/"+asset+"/"+person, type: "POST", - data: {equipment: asset, person: person, checkout: (Date.now() / 1000)}, + dataType: "json", success: function(asset) { - return function() { - toastr.success("Checked out asset #"+asset, "Asset Management"); - $("#asset-id").val(""); - asset_search(""); + return function(data) { + if (data == true) { + toastr.success("Checked out asset #"+asset, "Asset Management"); + $("#asset-id").val(""); + asset_search(""); + } else { + toastr.error("Could not Check Out asset #"+asset+". Is it already out?", "ERROR: Asset Management"); + + } + } + }(asset), + error: function() { + alert("A problem has occurred"); + } + }); +} + +function asset_switch(asset, person) { + $.ajax({ + url: "/widget/home_asset/switch_owner/"+asset+"/"+person, + type: "POST", + dataType: "json", + success: function(asset) { + return function(data) { + if (data == true) { + toastr.success("Switched asset #"+asset, "Asset Management"); + $("#asset-id").val(""); + asset_search(""); + } else { + toastr.error("Could not switch asset #"+asset, "ERROR: Asset Management"); + + } } }(asset), error: function() { @@ -120,17 +158,20 @@ function asset_add() { } function asset_checkin(id) { - toastr.success("Checked in asset #"+id, "Asset Management"); $.ajax({ url: "/widget/home_asset/check_in/"+id, dataType: "json", - success: function() { - $("#asset-id").val(""); - asset_search(""); + success: function(data) { + if (data == true) { + toastr.success("Checked in asset #"+id, "Asset Management"); + $("#asset-id").val(""); + asset_search(""); + } else { + toastr.success("Error checking in #"+id+". Is it actually out?", "Asset Management"); + } }, error: function() { alert("A problem has occurred"); } - }); - -} \ No newline at end of file + }); +}