#!/usr/bin/env perl
use Mojolicious::Lite;

use lib '/srv/koha_ffzg';
BEGIN {
	$ENV{KOHA_CONF} = '/etc/koha/sites/ffzg/koha-conf.xml';
}
use C4::Context;
use C4::Circulation;

use Data::Dump qw(dump);



get '/' => sub {
	my $c = shift;

	my $barcode = $c->param('barcode');
	$c->stash( barcode => $barcode );

	my $dbh = C4::Context->dbh;
	my $sth_items = $dbh->prepare(qq{
select
	itemnumber,
	items.biblionumber,
	title,
	author
from items
join biblio on biblio.biblionumber = items.biblionumber where barcode = ?
});

	warn "# $barcode";
	$sth_items->execute( $barcode );
	while ( my $row = $sth_items->fetchrow_hashref ) {
		warn "# items row ",dump($row);
		$c->stash( $_ => $row->{$_} ) foreach keys %$row;
	}

my $sth_update = $dbh->prepare(qq{
update items set datelastseen = now() where barcode = ?
});

my $sth_inventura = $dbh->prepare(qq{
insert ignore into ffzg_inventura (date_scanned,barcode,source_id) values (date(now()), ?, 'abc')
});

my $sth_issues = $dbh->prepare(qq{
select firstname,surname,userid,email from issues join borrowers on issues.borrowernumber = borrowers.borrowernumber where itemnumber = ?
});

	$sth_update->execute( $barcode );
	$sth_inventura->execute( $barcode );

	$sth_issues->execute( $c->stash('itemnumber') );
	while ( my $row = $sth_issues->fetchrow_hashref ) {
		warn "# issues row ",dump($row);
		$c->stash( $_ => $row->{$_} ) foreach keys %$row;
		AddReturn( $barcode, 'FFZG' );
	}

	$c->render(template => 'index');
};

app->start;
__DATA__

@@ index.html.ep
% layout 'default';

%= form_for '/' => begin
	%#= text_field 'barcode'
	<input name=barcode size=10 autofocus>
	%= submit_button 'Scan'
% end

% if ( stash('title') ) {
<%= stash('author') %>
<b><%= stash('title') %></b>
<%= stash('barcode') %>
% } else {
<big style="color:red"><%= stash('barcode') %> not found</big>
% }

% if ( stash('userid') ) {
<br>Borrower:

<tt><%= stash('userid') %></tt>
<%= stash('firstname') %>
<%= stash('surname') %>
<tt><%= stash('email') %></tt>

% }

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <style>
	body {
		font-size: 200%;
	}
  </style>
  <title>Inventura</title>
</head>
  <body><%= content %></body>
</html>
