diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 5133ef5..d8ddca3 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -4,7 +4,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 { @@ -37,11 +37,7 @@ public function store(Request $request, int $id = 0) { 'remarks' => 'nullable|string', ]); - $print_layout = Setting::where('name', 'print_layout')->first()?->value; - if (!$print_layout) { - return $this->backToForm('Please add a print layout first!', 'error'); - } - + if ($product) { $product->update($data); $msg = 'Product updated successfully!'; @@ -50,18 +46,17 @@ public function store(Request $request, int $id = 0) { $msg = 'Product added successfully!'; } - $print_layout = $this->generateZplCode($product); - - return $this->backToForm($msg)->with('print_layout', $print_layout); - } + if ($request->has('print')) { + $zplCode = ZplUtils::generateZplCode($product); + if (!$zplCode) { + return $this->backToForm('Please add a print layout first!', 'error'); + } + return $this->backToForm($msg)->with('zpl-code', $zplCode); + } + return $this->backToForm($msg); - 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 +113,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..527501a --- /dev/null +++ b/app/Http/Controllers/Admin/ZplCodeController.php @@ -0,0 +1,45 @@ +validate([ + 'id' => 'required|string', + 'name' => 'nullable|string', + 'price' => 'required|numeric|min:0', + 'quantity' => 'required|numeric|min:0', + ]); + $zpl = ZplUtils::generateZplCode(new Product($data)); + return $this->backToForm('Printed successfully!')->with('zpl-code', $zpl); + } + + public function generate(Request $req, int $id) { + $product = Product::find($id); + if (!$product) { + return [ + "data" => null, + "error" => "Invalid product id!" + ]; + } + + $print_layout = ZplUtils::generateZplCode($product); + + return [ + "data" => $print_layout + ]; + } + +} diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 36898a0..74b57db 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; +use Illuminate\Http\RedirectResponse; use Illuminate\Routing\Controller as BaseController; class Controller extends BaseController @@ -12,7 +13,7 @@ class Controller extends BaseController use AuthorizesRequests, DispatchesJobs, ValidatesRequests; - public function backToForm(string $message, string $alertType = 'success') + public function backToForm(string $message, string $alertType = 'success') : RedirectResponse | array { if(request()->expectsJson()){ return compact('message', 'type'); @@ -20,7 +21,7 @@ public function backToForm(string $message, string $alertType = 'success') return redirect()->back()->with('form-alert', $message)->with('form-alert-type', $alertType); } - public function redirectToForm(string $route, array $params, string $message, string $alertType = 'success') + public function redirectToForm(string $route, array $params, string $message, string $alertType = 'success') : RedirectResponse { return redirect()->route($route, $params)->with('form-alert', $message)->with('form-alert-type', $alertType); } diff --git a/app/Utils/ZplUtils.php b/app/Utils/ZplUtils.php new file mode 100644 index 0000000..ddf6577 --- /dev/null +++ b/app/Utils/ZplUtils.php @@ -0,0 +1,43 @@ +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): ?string { + $print_layout = Setting::where('name', 'print_layout')->first()?->value; + if (!$print_layout) { + return null; + } + $zpl_code = ''; + for ($i = 0; $i < $product->quantity; $i++) { + $zpl_code .= self::generateSingleZplCode($product, $print_layout); + } + + return $zpl_code; + } + + 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/form.blade.php b/resources/views/admin/products/form.blade.php index 2b0313f..560785c 100644 --- a/resources/views/admin/products/form.blade.php +++ b/resources/views/admin/products/form.blade.php @@ -1,53 +1,60 @@ @extends('layouts.admin') @section('page') -