Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zpl code printer page #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 10 additions & 56 deletions app/Http/Controllers/Admin/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!';
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
45 changes: 45 additions & 0 deletions app/Http/Controllers/Admin/ZplCodeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Utils\ZplUtils;
use Illuminate\Http\Request;

class ZplCodeController extends Controller {


public function form(Request $request) {

return view('admin.zpl.printer');
}

public function generateStatic(Request $req) {
$data = $req->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
];
}

}
5 changes: 3 additions & 2 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
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
{
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');
}
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);
}
Expand Down
43 changes: 43 additions & 0 deletions app/Utils/ZplUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Utils;
use App\Models\Product;
use App\Models\Setting;

class ZplUtils {
private static function generateSingleZplCode(Product $product, string $print_layout) {
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): ?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;
}
}
85 changes: 46 additions & 39 deletions resources/views/admin/products/form.blade.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
@extends('layouts.admin')
@section('page')
<div class="mb-6">
<x-breadcrumb :home="[
<div class="mb-6">
<x-breadcrumb :home="[
'route' => 'admin.index',
'label' => 'Admin',
]" :items="[]" :active="$product ? 'Update Product' : 'Add Product'" />
</div>
<x-cards.card class="max-w-4xl">
<div class="flex items-center py-2 border-b px-6">
<h3 class="flex-grow font-medium">{{ $product ? 'Update' : 'Add New' }} Product</h3>
</div>
<form action="{{ route('products.store', ['id' => $product?->id]) }}" method="POST" class="p-6">
@csrf
<div class="space-y-4">
@csrf
<x-form.alert />
<x-cards.card class="max-w-4xl">
<div class="flex items-center py-2 border-b px-6">
<h3 class="flex-grow font-medium">{{ $product ? 'Update' : 'Add New' }} Product</h3>
</div>
<form action="{{ route('products.store', ['id' => $product?->id]) }}" method="POST" class="p-6">
@csrf
<div class="space-y-4">
@csrf
<x-form.alert />
@csrf
<div class="space-y-4">

<x-form.input label="Name" name="name" :value="old('name', $product?->name)" />
<x-form.input label="Vendor" name="vendor" :value="old('vendor', $product?->vendor)" />
<x-form.input label="Name" name="name" :value="old('name', $product?->name)" />
<x-form.input label="Vendor" name="vendor" :value="old('vendor', $product?->vendor)" />

<x-form.input type="number" min="0" step="0.01" label="Buying Price (Unit)" name="unit_price_buying" :value="old('unit_price_buying', $product?->unit_price_buying)" />
<x-form.input type="number" min="0" step="0.01" label="Buying Price (Unit)"
name="unit_price_buying" :value="old('unit_price_buying', $product?->unit_price_buying)" />

<x-form.input type="number" min="0" label="Quantity" name="quantity" :value="old('quantity', $product?->quantity)" />
<x-form.input type="number" min="0" label="Quantity" name="quantity" :value="old('quantity', $product?->quantity)" />

<x-form.input type="date" label="Date" name="date" :value="old('date', $product?->date??date('Y-m-d'))" />
<x-form.input type="date" label="Date" name="date" :value="old('date', $product?->date ?? date('Y-m-d'))" />

<x-form.textarea label="Remarks" name="remarks" placeholder=" ">{{old('remarks', $product?->remarks)}}</x-form.textarea>
<x-form.textarea label="Remarks" name="remarks"
placeholder=" ">{{ old('remarks', $product?->remarks) }}</x-form.textarea>

</div>
<x-button type="submit" class="my-4">
{{ $product ? 'Update' : 'Add' }}
</x-button>
</form>
</x-cards.card>
@if(Session::has("print_layout"))
<script>

const printZpl = (zpl) => {
var printWindow = window.open();
printWindow.document.open('text/plain');
printWindow.document.write(zpl);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
};
printZpl(`{!!str_replace('`', '\\`', Session::get('print_layout'))!!}`);
</script>
@endif
@endsection
<div class="flex items-center mb-4 gap-2">
<input id="print-layout" type="checkbox" @checked(!old('print'))
name="print"
class="w-4 h-4 text-skin-accent bg-skin-foreground rounded focus:ring-skin-accent focus:ring-2">
<label for="print-layout" class="text-sm font-medium">Print Layout</label>
</div>
</div>
<x-button type="submit" class="my-4">
{{ $product ? 'Update' : 'Add' }}
</x-button>
</form>
</x-cards.card>
@if (Session::has('zpl-code'))
<script>
const printZpl = (zpl) => {
var printWindow = window.open();
printWindow.document.open('text/plain');
printWindow.document.write(zpl);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
};
printZpl(`{!! str_replace('`', '\\`', Session::get('zpl-code')) !!}`);
</script>
@endif
@endsection
2 changes: 1 addition & 1 deletion resources/views/admin/products/stock.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@extends('layouts.admin')
@section('head')
<script>
const ZPL_API_LINK = "{{ route('products.api.zpl', ['id'=>'::ID::']) }}";
const ZPL_API_LINK = "{{ route('zpl.get', ['id'=>'::ID::']) }}";
const PRODUCTS_API_LINK = "{{ route('products.api') }}";
const PRODUCT_EDIT_LINK = "{{ route('products.edit', ['id' => '::ID::']) }}";
const PRODUCT_DELETE_LINK = "{{ route('products.delete', ['id' => '::ID::']) }}";
Expand Down
43 changes: 43 additions & 0 deletions resources/views/admin/zpl/printer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@extends('layouts.admin')
@section('page')
<div class="mb-6">
<x-breadcrumb :home="[
'route' => 'admin.index',
'label' => 'Admin',
]" :items="[]" active="ZPL Code" />
</div>
<x-cards.card class="max-w-4xl">
<div class="flex items-center py-2 border-b px-6">
<h3 class="flex-grow font-medium">Print ZPL Code</h3>
</div>
<form id="zpl-code-form" method="POST" action="{{ route('zpl.static') }}" class="p-6">
@csrf
<x-form.alert />
<div class="space-y-4">
<div class="space-y-4">
<x-form.input label="ID" name="id" :value="old('id')" />
<x-form.input label="Name" name="name" :value="old('name')" />
<x-form.input type="number" min="0" step="0.01" label="Price" name="price"
:value="old('price')" />
<x-form.input type="number" min="0" label="Copies" name="quantity" :value="old('quantity')" />
</div>
<x-button type="submit" class="my-4" id="zpl-code-form-btn">
Print
</x-button>
</form>
</x-cards.card>
@if (Session::has('zpl-code'))
<script>
const printZpl = (zpl) => {
var printWindow = window.open();
printWindow.document.open('text/plain');
printWindow.document.write(zpl);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
};
printZpl(`{!! str_replace('`', '\\`', Session::get('zpl-code')) !!}`);
</script>
@endif
@endsection
9 changes: 8 additions & 1 deletion resources/views/layouts/inc/admin-sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<x-slot:svg viewBox="0 0 20 20">
<path fill="currentColor" d="M11 9h4v2h-4v4H9v-4H5V9h4V5h2v4zm-1 11a10 10 0 1 1 0-20a10 10 0 0 1 0 20zm0-2a8 8 0 1 0 0-16a8 8 0 0 0 0 16z" />
</x-slot:svg>
Add Item
Add Product
</x-sidebar-item>

<x-sidebar-item route="products.stock">
Expand Down Expand Up @@ -61,6 +61,13 @@
Sales List
</x-sidebar-item>

<x-sidebar-item route="zpl.form">
<x-slot:svg viewBox="0 0 32 32">
<path fill="currentColor" d="M28 28v-8.764l2.66-1.968a3 3 0 0 0 .627-4.196L29.79 11.05A2.989 2.989 0 0 0 28 9.9V9a1 1 0 0 0-.293-.707l-7-7A1 1 0 0 0 20 1H7a3 3 0 0 0-3 3v24a3 3 0 0 0 3 3h18a3 3 0 0 0 3-3M6 28V4a1 1 0 0 1 1-1h12v5.507c0 .825.669 1.493 1.493 1.493H26v.17c-.14.072-.275.156-.406.253l-2.615 1.935A.5.5 0 0 0 22.5 12h-13a.5.5 0 0 0 0 1h12.611l-2.703 2H9.5a.5.5 0 0 0 0 1h8.557l-2.703 2H9.5a.5.5 0 0 0 0 1h4.503l-2.647 1.958a1.009 1.009 0 0 0-.052.042H9.5a.5.5 0 0 0 0 1h1.333l-1.435 4.704a1 1 0 0 0 .956 1.292l5.472.004a1 1 0 0 0 .596-.196L26 20.716V28a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1M20.493 9A.493.493 0 0 1 20 8.507V3.414L25.586 9zm5.743 9.054l-2.681-3.634l1.17-.866l2.686 3.63zm-12.664 3.753l9.179-6.792l2.68 3.634l-9.082 6.72zm-.78.625l2.752 3.533l-.047.035l-2.614-.002l-.834-1.128z"/>
</x-slot:svg>
Print ZPL
</x-sidebar-item>


<!-- <x-sidebar-item route="settings.index">
<x-slot:svg viewBox="0 0 24 24">
Expand Down
Loading