Skip to content

Commit 931c018

Browse files
committed
Issue 399: Implement updates to API::PrintStation::release
1 parent 434eaad commit 931c018

3 files changed

Lines changed: 57 additions & 16 deletions

File tree

lib/Libki/Controller/API/PrintStation/v1_0.pm

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,22 @@ sub print_jobs : Path('print_jobs') : Args(0) {
134134

135135
my $print_jobs = [];
136136
while ( my $j = $jobs->next ) {
137-
my $cost = Libki::Utils::Printing::calculate_job_cost( $c, { print_job => $j } );
137+
my ( $cost, $gratis_discount ) = Libki::Utils::Printing::calculate_job_cost( $c, { print_job => $j } );
138138

139139
push(
140140
@$print_jobs,
141141
{
142-
print_job_id => $j->id,
143-
printer => $j->printer,
144-
copies => $j->copies,
145-
created_on => $c->format_dt( { dt => $j->created_on, include_time => 1 } ),
146-
print_file_id => $j->print_file_id,
147-
pages => $j->print_file->pages,
148-
cost => $cost,
142+
print_job_id => $j->id,
143+
printer => $j->printer,
144+
copies => $j->copies,
145+
created_on => $c->format_dt( { dt => $j->created_on, include_time => 1 } ),
146+
print_file_id => $j->print_file_id,
147+
pages => $j->print_file->pages,
148+
cost => $cost,
149+
gratis_discount => $gratis_discount,
149150
}
150151
);
152+
151153
}
152154

153155
my $data = {

lib/Libki/Controller/Public/API/DataTables.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ sub prints : Local Args(0) {
112112
foreach my $print_job (@prints) {
113113
my $print_file = $print_job->print_file;
114114

115-
my $total_cost = Libki::Utils::Printing::calculate_job_cost(
115+
my ($total_cost, $gratis_discount) = Libki::Utils::Printing::calculate_job_cost(
116116
$c,
117117
{
118118
print_job => $print_job,
@@ -131,7 +131,7 @@ sub prints : Local Args(0) {
131131
}
132132
keys %{$printers->{printers}} ) {
133133
my $printer = $printers->{printers}->{ $key };
134-
my $total_cost = Libki::Utils::Printing::calculate_job_cost(
134+
my ($total_cost, $gratis_discount) = Libki::Utils::Printing::calculate_job_cost(
135135
$c,
136136
{
137137
print_job => $print_job,
@@ -144,6 +144,7 @@ sub prints : Local Args(0) {
144144
selected => $key eq $print_job->printer ? 1 : 0,
145145
name => $printer->{public_printer_name},
146146
cost => $total_cost,
147+
gratis_discount => $gratis_discount,
147148
};
148149

149150
push( @printer_costs, $data );

lib/Libki/Utils/Printing.pm

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use Modern::Perl;
44

55
use File::Slurp qw( write_file );
66
use File::Temp qw( tempfile );
7+
use JSON qw( to_json );
78
use PDF::API2;
89
use Try::Tiny;
910

@@ -148,7 +149,10 @@ sub create_print_job_and_file {
148149

149150
=head2 calculate_job_cost
150151
151-
Helper function to calculate the cost of a print job for a given printer
152+
Helper function to calculate the cost of a print job for a given printer.
153+
Returns a list containing the calculated cost and the gratis discount applied.
154+
155+
my ( $cost, $gratis_discount ) = calculate_job_cost( $c, { print_job => $job } );
152156
153157
=cut
154158

@@ -179,18 +183,31 @@ sub calculate_job_cost {
179183

180184
my $total_pages = $pages * $copies;
181185

182-
# Fetch grais print settings and decrement pages or balance as needed
186+
my $gratis_discount = 0;
187+
183188
if ( $GratisPrintingMethod eq 'pages' ) {
184-
$total_pages -= $c->user->gratis_print_balance;
189+
if ( $total_pages >= $c->user->gratis_print_balance ) {
190+
$gratis_discount = $c->user->gratis_print_balance;
191+
$total_pages -= $c->user->gratis_print_balance;
192+
} else {
193+
$gratis_discount = $total_pages;
194+
$total_pages = 0;
195+
}
185196
}
186197

187198
my $cost = $total_pages * $cpp;
188199

189200
if ( $GratisPrintingMethod eq 'balance' ) {
190-
$cost -= $c->user->gratis_print_balance;
201+
if ( $cost >= $c->user->gratis_print_balance ) {
202+
$gratis_discount = $c->user->gratis_print_balance;
203+
$cost -= $c->user->gratis_print_balance;
204+
} else {
205+
$gratis_discount = $cost;
206+
$cost = 0;
207+
}
191208
}
192209

193-
return $cost;
210+
return ($cost, $gratis_discount);
194211
}
195212

196213
=head2 cancel
@@ -311,7 +328,7 @@ sub release {
311328
printer => $printer,
312329
};
313330

314-
my $total_cost = calculate_job_cost( $c,
331+
my ($total_cost, $gratis_discount) = calculate_job_cost( $c,
315332
{
316333
print_job => $print_job,
317334
printer => $printer,
@@ -323,12 +340,33 @@ sub release {
323340
if ( $total_cost <= $user->funds ) {
324341
$user->debit_funds( $c, $total_cost );
325342

343+
$user->gratis_print_balance( $user->gratis_print_balance - $gratis_discount );
344+
326345
$print_job->status(PRINT_STATUS_PENDING);
327346

328347
$c->model('DB')->txn_do(
329348
sub {
330349
$user->update();
331350
$print_job->update();
351+
352+
# Add statistic indicating gratis discount was used
353+
if ( $gratis_discount > 0 ) {
354+
$c->model('DB::Statistic')->create(
355+
{
356+
instance => $c->instance,
357+
username => $c->user->username,
358+
action => 'GRATIS_DISCOUNT',
359+
created_on => $c->now,
360+
session_id => $c->sessionid,
361+
info => to_json(
362+
{
363+
print_job_id => $print_job->id,
364+
gratis_discount => $gratis_discount,
365+
}
366+
),
367+
}
368+
);
369+
}
332370
}
333371
);
334372
}

0 commit comments

Comments
 (0)