From 04249a6fa4038ab00a91763106a9408dec8e5ff1 Mon Sep 17 00:00:00 2001
From: Oskar Stenberg <01ste02@gmail.com>
Date: Wed, 14 Jul 2021 19:10:48 +0200
Subject: [PATCH] Buxfix

---
 .../Consumables/ConsumablesController.php     | 103 ++++++++++++++++++
 1 file changed, 103 insertions(+)

diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php
index 69987f157..b6e3c1c43 100644
--- a/app/Http/Controllers/Consumables/ConsumablesController.php
+++ b/app/Http/Controllers/Consumables/ConsumablesController.php
@@ -7,8 +7,19 @@ use App\Http\Controllers\Controller;
 use App\Http\Requests\ImageUploadRequest;
 use App\Models\Company;
 use App\Models\Consumable;
+use App\Models\Location;
+use App\Models\Setting;
+use App\Models\User;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Input;
+use Intervention\Image\Facades\Image;
+use Paginator;
+use Redirect;
+use Response;
+use Slack;
+use Str;
+use TCPDF;
+use View;
 
 /**
  * This controller handles all actions related to Consumables for
@@ -192,5 +203,97 @@ class ConsumablesController extends Controller
         return redirect()->route('consumables.index')
             ->with('error', trans('admin/consumables/message.does_not_exist'));
     }
+    
+    /**
+     * Return a QR code for the consumable
+     *
+     * @author [A. Gianotto] [<snipe@snipe.net>]
+     * @param int $consumableId
+     * @since [v1.0]
+     * @return Response
+     */
+    public function getQrCode($consumableId = null)
+    {
+        $settings = Setting::getSettings();
+
+        if ($settings->qr_code == '1') {
+            $consumable = Consumable::withTrashed()->find($consumableId);
+            if ($consumable) {
+                $size = Helper::barcodeDimensions($settings->barcode_type);
+                $qr_file = public_path().'/uploads/barcodes/qr-consumable-'.str_slug($consumable->id).'-'.str_slug($consumable->id).'.png'; //There aren't enough unique required parameters. Making them follow the original format still.
+
+                if (isset($consumable->id)) {
+                    if (file_exists($qr_file)) {
+                        $header = ['Content-type' => 'image/png'];
+                        return response()->file($qr_file, $header);
+                    } else {
+                        $barcode = new \Com\Tecnick\Barcode\Barcode();
+                        $barcode_obj =  $barcode->getBarcodeObj($settings->barcode_type, route('consumables.view', $consumable->id), $size['height'], $size['width'], 'black', array(-2, -2, -2, -2));
+                        file_put_contents($qr_file, $barcode_obj->getPngData());
+                        return response($barcode_obj->getPngData())->header('Content-type', 'image/png');
+                    }
+                }
+            }
+            return 'That consumable is invalid';
+        }
+    }
+
 
+    /**
+     * Return a 2D barcode for the consumable
+     *
+     * @author [A. Gianotto] [<snipe@snipe.net>]
+     * @param int $consumableId
+     * @since [v1.0]
+     * @return Response
+     */
+    public function getBarCode($consumableId = null) //Following format "id-" + consumable id. NOT implemented in search box for barcode scanners.
+    {
+        $settings = Setting::getSettings();
+        $consumable = Consumable::find($consumableId);
+        $barcode_file = public_path().'/uploads/barcodes/'.str_slug($settings->alt_barcode).'-consumable-'.str_slug($consumable->id).'.png';
+
+        if (isset($consumable->id)) {
+            if (file_exists($barcode_file)) {
+                $header = ['Content-type' => 'image/png'];
+                return response()->file($barcode_file, $header);
+            } else {
+                // Calculate barcode width in pixel based on label width (inch)
+                $barcode_width = ($settings->labels_width - $settings->labels_display_sgutter) * 200.000000000001;
+
+                $barcode = new \Com\Tecnick\Barcode\Barcode();
+                try {
+                    $barcode_obj = $barcode->getBarcodeObj($settings->alt_barcode,'id-'.$consumable->id,($barcode_width < 300 ? $barcode_width : 300),50);
+                    file_put_contents($barcode_file, $barcode_obj->getPngData());
+                    return response($barcode_obj->getPngData())->header('Content-type', 'image/png');
+                } catch(\Exception $e) {
+                    \Log::debug('The barcode format is invalid.');
+                    return response(file_get_contents(public_path('uploads/barcodes/invalid_barcode.gif')))->header('Content-type', 'image/gif');
+                }
+
+
+            }
+        }
+    }
+
+    /**
+     * Return a label for an individual consumable.
+     *
+     * @author [L. Swartzendruber] [<logan.swartzendruber@gmail.com>
+     * @param int $assetId
+     * @return View
+     */
+    public function getLabel($consumableId = null)
+    {
+        if (isset($consumableId)) {
+            $consumable = Consumable::find($consumableId);
+            $this->authorize('view', $consumable);
+
+            return view('consumables/labels')
+                ->with('consumables', Consumable::find($consumable))
+                ->with('settings', Setting::getSettings())
+                ->with('bulkedit', false)
+                ->with('count', 0);
+        }
+    }
 }
-- 
GitLab