Skip to content

Commit

Permalink
#220 initial implementation of barcode label rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jmiranda committed Nov 8, 2017
1 parent d0cfff2 commit 956a2b2
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 11 deletions.
Expand Up @@ -16,6 +16,14 @@ import org.pih.warehouse.jobs.DataCleaningJob
import org.springframework.web.multipart.MultipartFile
import util.ClickstreamUtil

import javax.print.Doc
import javax.print.DocFlavor
import javax.print.DocPrintJob
import javax.print.PrintService
import javax.print.SimpleDoc
import javax.print.attribute.Attribute
import java.awt.print.PrinterJob

//import java.net.HttpURLConnection;
//import java.net.URLConnection;
import java.util.concurrent.FutureTask;
Expand Down Expand Up @@ -199,6 +207,32 @@ class AdminController {
chain(action: "showUpgrade", model: [command : command])
//redirect (view: "showUpgrade", model: [command: command])
}


def testZebraPrinter = {
try {

PrintService[] printServices = PrinterJob.lookupPrintServices();
//PrinterJob
DocPrintJob job = psZebra.createPrintJob();

String s = "N" + "\n" +
"q305" + "\n" +
"Q203,26" + "\n" +
"B55,26,0,1,2,2,152,B,\"" + code + "\"" + "\n" +
"P1,1";

InputStream inputStream = new ByteArrayInputStream(s.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(inputStream, flavor, null);

job.print(doc, null);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}


def showSettings = {
def externalConfigProperties = []
Expand All @@ -216,8 +250,11 @@ class AdminController {
}
}

[
PrintService[] printServices = PrinterJob.lookupPrintServices();

[
quartzScheduler:quartzScheduler,
printServices:printServices,
externalConfigProperties: externalConfigProperties,
systemProperties : System.properties,
env: GrailsUtil.environment,
Expand Down
Expand Up @@ -9,7 +9,12 @@
**/
package org.pih.warehouse.core

import groovy.text.Template
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.springframework.web.context.request.RequestContextHolder
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.docx4j.openpackaging.packages.WordprocessingMLPackage
import org.pih.warehouse.inventory.InventoryItem
import org.pih.warehouse.order.Order
import org.pih.warehouse.requisition.Requisition
import org.pih.warehouse.shipping.Shipment
Expand All @@ -22,6 +27,9 @@ import javax.activation.MimetypesFileTypeMap
class DocumentController {

def fileService
def grailsApplication
GroovyPagesTemplateEngine groovyPagesTemplateEngine


static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

Expand Down Expand Up @@ -406,6 +414,67 @@ class DocumentController {
}
}

def printZebraTemplate = {
Document document = Document.load(params.id)
InventoryItem inventoryItem = InventoryItem.load(params.inventoryItemId)
Location location = Location.load(session.warehouse.id)

String lptPort = grailsApplication.config.openboxes.linePrinterTerminal.port

Map model = [document:document, inventoryItem:inventoryItem, location:location]
String renderedContent = renderTemplate(document, model)

try {
FileOutputStream os = new FileOutputStream(lptPort);
PrintStream ps = new PrintStream(os);
ps.println(renderedContent);
ps.print("\f");
ps.close();
flash.message = "Label printed successfully"
}
catch (Exception e) {
flash.message = e.message
}

redirect(controller: "inventoryItem", action: "showStockCard", id: inventoryItem?.id)
}

def renderZebraTemplate = {
Document document = Document.load(params.id)
InventoryItem inventoryItem = InventoryItem.load(params.inventoryItem?.id)
Location location = Location.load(session.warehouse.id)
Map model = [document:document, inventoryItem:inventoryItem, location:location]
String renderedContent = renderTemplate(document, model)
log.info "renderedContent: ${renderedContent}"
render (renderedContent)
}

def viewZebraTemplate = {
Document document = Document.load(params.id)
InventoryItem inventoryItem = InventoryItem.load(params.inventoryItem?.id)
Location location = Location.load(session.warehouse.id)
Map model = [document:document, inventoryItem:inventoryItem, location:location]
String renderedContent = renderTemplate(document, model)
String url = "http://labelary.com/viewer.html?zpl=" + renderedContent
redirect(url: url)
}


private String renderTemplate(Document document, Map model) {
String templateContent = new String(document.fileContents)
Template template =
groovyPagesTemplateEngine.createTemplate(templateContent, document.name)

log.info "Template content: " + templateContent
log.info "Model: " + model

Writable renderedTemplate = template.make(model)
StringWriter stringWriter = new StringWriter();
renderedTemplate.writeTo(stringWriter);
return stringWriter.toString();
}


}

/**
Expand Down
5 changes: 5 additions & 0 deletions grails-app/domain/org/pih/warehouse/core/Document.groovy
Expand Up @@ -64,5 +64,10 @@ class Document implements Serializable {
public boolean isImage() {
return contentType?.startsWith("image/")
}

static List findAllByDocumentCode(DocumentCode documentCode) {
List<DocumentType> documentTypes = DocumentType.findAllByDocumentCode(documentCode)
return Document.findAllByDocumentTypeInList(documentTypes)
}

}
67 changes: 63 additions & 4 deletions grails-app/views/admin/showSettings.gsp
Expand Up @@ -29,7 +29,10 @@
<a href="#tabs-4" id="system-properties-tab"><warehouse:message code="admin.systemProperties.header"/></a>
</li>
<li>
<a href="#tabs-5" id="background-jobs-tab"><warehouse:message code="admin.backgroundJobs.header"/></a>
<a href="#tabs-5" id="printers-tab"><warehouse:message code="admin.printers.header" default="Printers"/></a>
</li>
<li>
<a href="#tabs-6" id="background-jobs-tab"><warehouse:message code="admin.backgroundJobs.header"/></a>
</li>
</ul>
<div id="tabs-1">
Expand Down Expand Up @@ -213,11 +216,67 @@
</div>
<div id="tabs-5">
<div class="box">
<table>
<h2><g:message code="default.printers.label" default="Printers"/></h2>
<table>
<tr>
<th><g:message code="default.name.label"/></th>
<th><g:message code="printer.attributes.label" default="Attributes"/></th>
<th><g:message code="printer.supportedDocFlavors.label" default="Doc Flavors"/></th>
<th><g:message code="printer.supportedAttributeCategories.label" default="Attribute Categories"/></th>
</tr>
<g:each in="${printServices}" var="printService">
<tr class="prop">
<td>${printService.name}</td>
<td>
<table>
<g:each var="attribute" in="${printService.attributes.toArray()}">
<tr>
<td>
<label>${attribute.name}</label>
</td>
<td>
${attribute}
</td>
</tr>
</g:each>
</table>
</td>
<td>
<div style="overflow: auto; max-height: 200px;">
<table>
<g:each var="supportedDocFlavor" in="${printService.supportedDocFlavors}">
<tr>
<td>${supportedDocFlavor.mimeType}</td>
</tr>
</g:each>
</table>
</div>
</td>
<td>
<table>
<g:each var="supportedAttributeCategory" in="${printService.supportedAttributeCategories}">
<tr>
<td>
${supportedAttributeCategory.name}
</td>
<td>
${printService.getDefaultAttributeValue(supportedAttributeCategory)}
</td>
</tr>
</g:each>
</table>
</td>
</tr>
</g:each>
</table>
</div>
</div>
<div id="tabs-6">
<div class="box">

<h2> ${quartzScheduler.schedulerName} ${quartzScheduler.schedulerInstanceId}</h2>
<div class="message">
<pre>${quartzScheduler.metaData}</pre>
</div>
<textarea disabled="disabled" rows="10">${quartzScheduler.metaData}</textarea>

<div class="buttons">
<g:link controller="jobs" action="index" class="button">${g.message(code:'backgroundJobs.label', default: 'Background Jobs')}</g:link>
Expand Down
24 changes: 24 additions & 0 deletions grails-app/views/inventoryItem/_actionsCurrentStock.gsp
Expand Up @@ -37,6 +37,30 @@
<warehouse:message code="shipping.addToShipment.label"/>
</a>
</div>
<g:isSuperuser>
<g:set var="templates" value="${org.pih.warehouse.core.Document.findAllByDocumentCode(org.pih.warehouse.core.DocumentCode.ZEBRA_TEMPLATE)}"/>
<g:each in="${templates}" var="template">
<div class="action-menu-item">
<g:link controller="document" action="renderZebraTemplate" id="${template.id}" params="['inventoryItem.id': itemInstance?.id]" target="_blank">
<img src="${resource(dir: 'images/icons/silk', file: 'paintbrush.png')}"/>&nbsp;
<g:message code="default.button.render.label" default="Render"/> ${template.name}
</g:link>
</div>
<div class="action-menu-item">
<g:link controller="document" action="exportZebraTemplate" id="${template.id}" params="['inventoryItem.id': itemInstance?.id]" target="_blank">
<img src="${resource(dir: 'images/icons/silk', file: 'zoom.png')}"/>&nbsp;
<g:message code="default.button.export.label" default="Export"/> ${template.name}
</g:link>
</div>
<div class="action-menu-item">
<g:link controller="document" action="printZebraTemplate" id="${template.id}" params="['inventoryItem.id': itemInstance?.id]" target="_blank">
<img src="${resource(dir: 'images/icons/silk', file: 'printer.png')}"/>&nbsp;
<g:message code="default.button.print.label" default="Print"/> ${template.name}
</g:link>
</div>
</g:each>
</g:isSuperuser>

<g:render template="editItemDialog" model="[dialogId:dialogId, inventoryInstance:commandInstance?.inventory, binLocation:binLocation, itemInstance:itemInstance, itemQuantity: itemQuantity]"/>
<g:render template="adjustStock" model="[dialogId:dialogId, inventoryInstance:commandInstance?.inventory, binLocation:binLocation, itemInstance:itemInstance, itemQuantity: itemQuantity]" />
<g:render template="transferStock" model="[dialogId:dialogId, inventoryInstance:commandInstance?.inventory, binLocation:binLocation, itemInstance:itemInstance, itemQuantity: itemQuantity]" />
Expand Down
6 changes: 3 additions & 3 deletions src/groovy/org/pih/warehouse/core/DocumentCode.groovy
Expand Up @@ -15,11 +15,11 @@ enum DocumentCode {
THUMBNAIL,
PRODUCT_MANUAL,
SHIPPING_DOCUMENT,
SHIPPING_TEMPLATE

SHIPPING_TEMPLATE,
ZEBRA_TEMPLATE

static list() {
[ SHIPPING_TEMPLATE, THUMBNAIL, PRODUCT_MANUAL, SHIPPING_DOCUMENT, SHIPPING_TEMPLATE ]
[ SHIPPING_TEMPLATE, THUMBNAIL, PRODUCT_MANUAL, SHIPPING_DOCUMENT, SHIPPING_TEMPLATE, ZEBRA_TEMPLATE ]
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/groovy/util/StringUtil.groovy
Expand Up @@ -19,10 +19,10 @@ class StringUtil {
return value ? value?.replaceFirst(".*", { match -> return "".padLeft(match.length(), mask)}) : value
}

public static String substitute(text, binding) {
public static String renderTemplate(template, binding) {
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(text).make(binding)
return template.toString()
def content = engine.createTemplate(template).make(binding)
return content.toString()
}

public static String format(text, args) {
Expand Down

0 comments on commit 956a2b2

Please sign in to comment.