Exemplo n.º 1
0
    }
    $response->write(json_encode($names));
    return $response->withHeader('Content-type', 'application/json');
});
$app->post('/api/v1/checkin', function ($request, $response, $args) use($app) {
    $params = $request->getParsedBody();
    $json = ["success" => false, "message" => "There was an issue checking in the item (" . $params["sku"] . "). Please contact the tech team!"];
    if (Identity::where('name', $params["identity"])->count() < 1) {
        $json["message"] = "You are not authorized to use the inventory system! Contact your supervisor for access.";
        $response->write(json_encode($json));
        return $response->withHeader('Content-type', 'application/json');
    }
    // Retrieve the item by its SKU or create a new record if one does not exist.
    $create_mode = false;
    if (array_key_exists('create', $params) && $params["create"] == "true") {
        $item = InventoryItem::firstOrCreate(['sku' => $params["sku"]]);
        $item->available++;
        $item->stock++;
        $item->save();
        $create_mode = true;
    } else {
        $item = InventoryItem::where('sku', $params["sku"])->first();
    }
    if ($item && ($item->available < $item->stock || $create_mode)) {
        if (!$create_mode) {
            $item->returnFrom($params["identity"]);
        }
        $item->updateName();
        $json["success"] = true;
        $json["message"] = "The item (" . $params["sku"] . ") has been checked in successfully!";
    } elseif ($item) {