Skip to content

Commit

Permalink
Added ability to switch assets
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Aug 15, 2015
1 parent de5ec45 commit 8da2582
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 23 deletions.
15 changes: 14 additions & 1 deletion framework/application/controller/widget/Home_Asset.php
Expand Up @@ -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']);
Expand All @@ -46,4 +59,4 @@ function search_list() {
}
\Core\Router::loadView("widget/home_asset/list", array("results"=>$results));
}
}
}
18 changes: 17 additions & 1 deletion framework/application/model/Equipment.php
Expand Up @@ -56,4 +56,20 @@ public function CheckIn() {
}
return false;
}
}

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;
}
}
4 changes: 3 additions & 1 deletion framework/application/view/home.php
Expand Up @@ -19,6 +19,8 @@ class="form-control" id="person-id" />
<div class="col-md-2">
<button class="btn btn-success btn-block" disabled
id="asset-checkout-btn">Check Out &gt;&gt;</button>
<button class="btn btn-success btn-block" disabled
id="asset-switch-btn">Switch &gt;&gt;</button>
</div>
<div class="col-md-5">
<div class="panel panel-default">
Expand Down Expand Up @@ -59,4 +61,4 @@ class="form-control" />
<!-- /.modal -->
<?php
\Core\Router::loadView("api/html/_template/" . \Core\Router::$disposition . "/bottom");
?>
?>
81 changes: 61 additions & 20 deletions htdocs/js/widget/home_asset.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -68,28 +69,65 @@ 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);
}
}
}

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() {
Expand Down Expand Up @@ -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");
}
});

}
});
}

0 comments on commit 8da2582

Please sign in to comment.