diff --git a/app/Utils/Printer.php b/app/Utils/Printer.php index cafaf7731..2cd5ab6ad 100644 --- a/app/Utils/Printer.php +++ b/app/Utils/Printer.php @@ -18,7 +18,7 @@ class Printer private $number_of_copies; private $use_free_pages; private $print_account; - private $free_page_pool = []; + private $free_page_update = []; private $cost = 0; public function __construct($filename, $path, $use_free_pages = false, $is_two_sided = true, $number_of_copies = 1) @@ -41,49 +41,52 @@ public function print() // If using free pages, check the amount that can be used if ($this->use_free_pages) { - $this->calculateFreePagePool(); - } - // Calculate cost - $this->cost = PrintAccount::getCost($this->pages, $this->is_two_sided, $this->number_of_copies); + if (!$this->planFreePageUsage()) { + return back()->withInput()->with('error', __('print.no_balance')); + } - // Check balance - if (!$this->print_account->hasEnoughMoney($this->cost)) { - return back()->withInput()->with('error', __('print.no_balance')); - } + // Print document + return $this->printDocument(); + } else { + + // Calculate cost + $this->cost = PrintAccount::getCost($this->pages, $this->is_two_sided, $this->number_of_copies); + + // Check balance + if (!$this->print_account->hasEnoughMoney($this->cost)) { + return back()->withInput()->with('error', __('print.no_balance')); + } - // Print document - return $this->printDocument(); + // Print document + return $this->printDocument(); + } } /** * Only calculating the values here to see how many pages can be covered free of charge. */ - private function calculateFreePagePool() + private function planFreePageUsage() { - $this->free_page_pool = []; - $available_pages = 0; + $requested_pages = $this->number_of_copies * $this->pages; + $this->free_page_update = []; + $deducted_pages = 0; $all_pages = user()->freePages ->where('deadline', '>', Carbon::now()) ->sortBy('deadline'); foreach ($all_pages as $key => $free_page) { - if ($available_pages + $free_page->amount >= $this->pages) { - $this->free_page_pool[] = [ - 'page' => $free_page, - 'new_amount' => $free_page->amount - ($this->pages - $available_pages) - ]; - $available_pages = $this->pages; - break; - } - $this->free_page_pool[] = [ + $deduce_from_current = min($requested_pages - $deducted_pages, $free_page->amount); + $this->free_page_update[] = [ 'page' => $free_page, - 'new_amount' => 0 + 'new_amount' => $free_page->amount - $deduce_from_current ]; - $available_pages += $free_page->amount; + $deducted_pages += $deduce_from_current; + if($deducted_pages == $requested_pages) { + break; + } } - - $this->pages -= $available_pages; + return $deducted_pages == $requested_pages; } private function printDocument() @@ -95,7 +98,7 @@ private function printDocument() // Update print account history $this->print_account->update(['last_modified_by' => user()->id]); - foreach ($this->free_page_pool as $fp) { + foreach ($this->free_page_update as $fp) { $fp['page']->update([ 'amount' => $fp['new_amount'], 'last_modified_by' => user()->id diff --git a/resources/lang/en/print.php b/resources/lang/en/print.php index 31bda8c1d..fdc65f6e7 100644 --- a/resources/lang/en/print.php +++ b/resources/lang/en/print.php @@ -43,6 +43,7 @@ 'number_of_copies' => 'Number of copies', 'number_of_printed_documents' => 'Number of printed documents', 'options' => 'Printing options', + 'payment_methods_cannot_be_mixed' => 'A print job can be either free or paid as they cannot be mixed.', 'pdf_description' => 'Only .pdf files can be printed.', 'pdf_maxsize' => 'The maximum file size is :maxsize MB.', 'print' => 'Print', diff --git a/resources/lang/hu/print.php b/resources/lang/hu/print.php index 1fafbc5e3..72ee27cdf 100644 --- a/resources/lang/hu/print.php +++ b/resources/lang/hu/print.php @@ -43,6 +43,7 @@ 'number_of_copies' => 'Példányszám', 'number_of_printed_documents' => 'Nyomtatott dokumentumok száma', 'options' => 'Beállítások', + 'payment_methods_cannot_be_mixed' => 'Egy nyomtatás vagy ingyenes vagy fizetős lehet, nem keverhető.', 'pdf_description' => 'Csak .pdf fájl nyomtatható.', 'pdf_maxsize' => 'A fájl mérete legfeljebb :maxsize MB lehet.', 'print' => 'Nyomtatás', diff --git a/resources/views/dormitory/print/print.blade.php b/resources/views/dormitory/print/print.blade.php index 56d462e55..c34065181 100644 --- a/resources/views/dormitory/print/print.blade.php +++ b/resources/views/dormitory/print/print.blade.php @@ -10,6 +10,9 @@ @lang('print.available_money'): {{ user()->printAccount->balance }} HUF. @lang('print.upload_money')
++ @lang('print.payment_methods_cannot_be_mixed') +