+use C4::Branch; # GetBranches GetBranchesLoop
+
+# this will be the script that chooses branch and printer settings....
+
+my $query = CGI->new();
+
+my ( $template, $borrowernumber, $cookie ) = get_template_and_user({
+ template_name => "circ/selectbranchprinter.tmpl",
+ query => $query,
+ type => "intranet",
+ debug => 1,
+ authnotrequired => 0,
+ flagsrequired => { catalogue => 1, },
+});
+
+my $sessionID = $query->cookie("CGISESSID");
+my $session = get_session($sessionID);
+
+# try to get the branch and printer settings from http, fallback to userenv
+my $branches = GetBranches();
+my $printers = GetPrinters();
+my $branch = $query->param('branch' );
+my $printer = $query->param('printer');
+# fallbacks for $branch and $printer after possible session updates
+
+my $userenv_branch = C4::Context->userenv->{'branch'} || '';
+my $userenv_printer = C4::Context->userenv->{'branchprinter'} || '';
+my @updated;
+
+# $session lddines here are doing the updating
+if ($branch and $branches->{$branch}) {
+ if (! $userenv_branch or $userenv_branch ne $branch ) {
+ my $branchname = GetBranchName($branch);
+ $template->param(LoginBranchname => $branchname); # update template for new branch
+ $template->param(LoginBranchcode => $branch); # update template for new branch
+ $session->param('branchname', $branchname); # update sesssion in DB
+ $session->param('branch', $branch); # update sesssion in DB
+ push @updated, {
+ updated_branch => 1,
+ old_branch => $userenv_branch,
+ };
+ } # else branch the same, no update
+} else {
+ $branch = $userenv_branch; # fallback value
+}