From 22a8fdfebf037d63b574400b085a6127f91a8e16 Mon Sep 17 00:00:00 2001 From: Saad Date: Mon, 25 Dec 2023 10:48:35 +0600 Subject: [PATCH 1/5] separated zpl functionality --- .../Controllers/Admin/ProductController.php | 51 +------------------ .../Controllers/Admin/ZplCodeController.php | 38 ++++++++++++++ app/Utils/ZplUtils.php | 41 +++++++++++++++ .../views/admin/products/stock.blade.php | 2 +- .../admin/products/zpl-printer.blade.php | 26 ++++++++++ .../views/layouts/inc/admin-sidebar.blade.php | 7 +++ routes/web.php | 13 ++++- 7 files changed, 127 insertions(+), 51 deletions(-) create mode 100644 app/Http/Controllers/Admin/ZplCodeController.php create mode 100644 app/Utils/ZplUtils.php create mode 100644 resources/views/admin/products/zpl-printer.blade.php diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 5133ef5..e193713 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use App\Models\Product; use App\Models\Setting; +use App\Utils\ZplUtils; use Illuminate\Http\Request; class ProductController extends Controller { @@ -50,18 +51,11 @@ public function store(Request $request, int $id = 0) { $msg = 'Product added successfully!'; } - $print_layout = $this->generateZplCode($product); + $print_layout = ZplUtils::generateZplCode($product); return $this->backToForm($msg)->with('print_layout', $print_layout); } - public function idToHex($id) { - return 'DKNCK>5' . explode('DKNCK', $id)[1]; - } - - public function priceEncode($price) { - return rand(100, 999) . $price * 2; - } public function api(Request $req) { $start = (int) $req->get('start', 0); @@ -118,47 +112,6 @@ public function infoApi(Request $req, int $id) { ]; } - public function generateSingleZplCode($product) { - $print_layout = Setting::where('name', 'print_layout')->first()?->value; - - foreach ($product->getAttributes() as $key => $value) { - if ($key == 'id') { - $value = sprintf('DKNCK%08d', $value); - $print_layout = str_replace('::ID_HEX::', $this->idToHex($value), $print_layout); - } - if ($key == 'unit_price_buying') { - $value = $this->priceEncode($value); - } - $print_layout = str_replace("::" . strtoupper($key) . "::", $value, $print_layout); - } - - return $print_layout; - } - - public function generateZplCode($product) { - $print_layout = ''; - for ($i = 0; $i < $product->quantity; $i++) { - $print_layout .= $this->generateSingleZplCode($product); - } - - return $print_layout; - } - - public function zplCodeApi(Request $req, int $id) { - $product = Product::find($id); - if (!$product) { - return [ - "data" => null, - "error" => "Invalid product id!" - ]; - } - - $print_layout = $this->generateZplCode($product); - - return [ - "data" => $print_layout - ]; - } public function delete(int $id) { if (Product::destroy($id)) { diff --git a/app/Http/Controllers/Admin/ZplCodeController.php b/app/Http/Controllers/Admin/ZplCodeController.php new file mode 100644 index 0000000..c0836f5 --- /dev/null +++ b/app/Http/Controllers/Admin/ZplCodeController.php @@ -0,0 +1,38 @@ + null, + "error" => "Invalid product id!" + ]; + } + + $print_layout = ZplUtils::generateZplCode($product); + + return [ + "data" => $print_layout + ]; + } + +} diff --git a/app/Utils/ZplUtils.php b/app/Utils/ZplUtils.php new file mode 100644 index 0000000..8c8b306 --- /dev/null +++ b/app/Utils/ZplUtils.php @@ -0,0 +1,41 @@ +first()?->value; + + foreach ($product->getAttributes() as $key => $value) { + if ($key == 'id') { + $value = sprintf('DKNCK%08d', $value); + $print_layout = str_replace('::ID_HEX::', self::idToHex($value), $print_layout); + } + if ($key == 'unit_price_buying') { + $value = self::priceEncode($value); + } + $print_layout = str_replace("::" . strtoupper($key) . "::", $value, $print_layout); + } + + return $print_layout; + } + + public static function generateZplCode(Product $product) { + $print_layout = ''; + for ($i = 0; $i < $product->quantity; $i++) { + $print_layout .= self::generateSingleZplCode($product); + } + + return $print_layout; + } + + public static function idToHex($id) { + return 'DKNCK>5' . explode('DKNCK', $id)[1]; + } + + public static function priceEncode($price) { + return rand(100, 999) . $price * 2; + } +} \ No newline at end of file diff --git a/resources/views/admin/products/stock.blade.php b/resources/views/admin/products/stock.blade.php index dc59a99..b6ba569 100644 --- a/resources/views/admin/products/stock.blade.php +++ b/resources/views/admin/products/stock.blade.php @@ -1,7 +1,7 @@ @extends('layouts.admin') @section('head')