2013-12-20 16:30:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import logging
|
|
|
|
import simplejson
|
|
|
|
import os
|
2013-12-24 15:11:12 +00:00
|
|
|
import io
|
|
|
|
import base64
|
2013-12-20 16:30:57 +00:00
|
|
|
import openerp
|
|
|
|
import time
|
|
|
|
import random
|
2013-12-26 16:56:20 +00:00
|
|
|
import math
|
2013-12-20 16:30:57 +00:00
|
|
|
import openerp.addons.hw_proxy.controllers.main as hw_proxy
|
|
|
|
import subprocess
|
|
|
|
import usb.core
|
2013-12-26 17:08:30 +00:00
|
|
|
from openerp.tools.translate import _
|
2013-12-24 15:11:12 +00:00
|
|
|
from .. import escpos
|
|
|
|
from ..escpos import printer
|
|
|
|
#import escpos
|
|
|
|
#import escpos.printer
|
|
|
|
from PIL import Image
|
2013-12-20 16:30:57 +00:00
|
|
|
|
|
|
|
from openerp import http
|
|
|
|
from openerp.http import request
|
|
|
|
from openerp.addons.web.controllers.main import manifest_list, module_boot, html_template
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class EscposDriver(hw_proxy.Proxy):
|
|
|
|
|
|
|
|
supported_printers = [
|
|
|
|
{ 'vendor' : 0x04b8, 'product' : 0x0e03, 'name' : 'Epson TM-T20' }
|
|
|
|
]
|
|
|
|
|
2013-12-26 16:56:20 +00:00
|
|
|
def get_escpos_printer(self):
|
|
|
|
printers = self.connected_usb_devices(self.supported_printers)
|
|
|
|
if len(printers) > 0:
|
|
|
|
return escpos.printer.Usb(printers[0]['vendor'], printers[0]['product'])
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-12-20 16:30:57 +00:00
|
|
|
@http.route('/hw_proxy/open_cashbox', type='json', auth='admin')
|
|
|
|
def open_cashbox(self):
|
|
|
|
print 'ESC/POS: OPEN CASHBOX'
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint = self.get_escpos_printer()
|
|
|
|
if eprint != None:
|
|
|
|
eprint.cashdraw(2)
|
|
|
|
eprint.cashdraw(5)
|
|
|
|
|
2013-12-20 16:30:57 +00:00
|
|
|
@http.route('/hw_proxy/print_receipt', type='json', auth='admin')
|
|
|
|
def print_receipt(self, receipt):
|
2013-12-26 16:56:20 +00:00
|
|
|
print 'ESC/POS: PRINT RECEIPT'
|
|
|
|
eprint = self.get_escpos_printer()
|
|
|
|
if eprint != None:
|
|
|
|
self.print_receipt_body(eprint,receipt)
|
|
|
|
eprint.cut()
|
|
|
|
|
|
|
|
def print_receipt_body(self,eprint,receipt):
|
2013-12-20 16:30:57 +00:00
|
|
|
|
|
|
|
def check(string):
|
|
|
|
return string != True and bool(string) and string.strip()
|
|
|
|
|
|
|
|
def price(amount):
|
2013-12-26 16:56:20 +00:00
|
|
|
return ("{0:."+str(receipt['precision']['price'])+"f}").format(amount)
|
|
|
|
|
|
|
|
def money(amount):
|
|
|
|
return ("{0:."+str(receipt['precision']['money'])+"f}").format(amount)
|
|
|
|
|
|
|
|
def quantity(amount):
|
|
|
|
if math.floor(amount) != amount:
|
|
|
|
return ("{0:."+str(receipt['precision']['quantity'])+"f}").format(amount)
|
|
|
|
else:
|
|
|
|
return str(amount)
|
|
|
|
|
2013-12-20 16:30:57 +00:00
|
|
|
|
|
|
|
def printline(left, right='', width=40, ratio=0.5, indent=0):
|
|
|
|
lwidth = int(width * ratio)
|
|
|
|
rwidth = width - lwidth
|
|
|
|
lwidth = lwidth - indent
|
|
|
|
|
|
|
|
left = left[:lwidth]
|
|
|
|
if len(left) != lwidth:
|
|
|
|
left = left + ' ' * (lwidth - len(left))
|
|
|
|
|
|
|
|
right = right[-rwidth:]
|
|
|
|
if len(right) != rwidth:
|
|
|
|
right = ' ' * (rwidth - len(right)) + right
|
|
|
|
|
|
|
|
return ' ' * indent + left + right + '\n'
|
2014-01-06 14:54:46 +00:00
|
|
|
|
|
|
|
def print_taxes():
|
|
|
|
taxes = receipt['tax_details']
|
|
|
|
for tax in taxes:
|
|
|
|
eprint.text(printline(tax['tax']['name'],price(tax['amount']), width=40,ratio=0.6))
|
2013-12-20 16:30:57 +00:00
|
|
|
|
2013-12-24 15:11:12 +00:00
|
|
|
logo = None
|
|
|
|
|
|
|
|
if receipt['company']['logo']:
|
|
|
|
img = receipt['company']['logo']
|
|
|
|
img = img[img.find(',')+1:]
|
|
|
|
f = io.BytesIO('img')
|
|
|
|
f.write(base64.decodestring(img))
|
|
|
|
f.seek(0)
|
|
|
|
logo_rgba = Image.open(f)
|
|
|
|
logo = Image.new('RGB', logo_rgba.size, (255,255,255))
|
|
|
|
logo.paste(logo_rgba, mask=logo_rgba.split()[3])
|
|
|
|
width = 300
|
|
|
|
wfac = width/float(logo_rgba.size[0])
|
|
|
|
height = int(logo_rgba.size[1]*wfac)
|
|
|
|
logo = logo.resize((width,height), Image.ANTIALIAS)
|
2013-12-20 16:30:57 +00:00
|
|
|
|
2013-12-26 16:56:20 +00:00
|
|
|
# Receipt Header
|
|
|
|
if logo:
|
|
|
|
eprint._convert_image(logo)
|
|
|
|
eprint.text('\n')
|
|
|
|
else:
|
|
|
|
eprint.set(align='center',type='b',height=2,width=2)
|
|
|
|
eprint.text(receipt['company']['name'] + '\n')
|
|
|
|
|
|
|
|
eprint.set(align='center',type='b')
|
|
|
|
if check(receipt['shop']['name']):
|
|
|
|
eprint.text(receipt['shop']['name'] + '\n')
|
|
|
|
if check(receipt['company']['contact_address']):
|
|
|
|
eprint.text(receipt['company']['contact address'] + '\n')
|
|
|
|
if check(receipt['company']['phone']):
|
|
|
|
eprint.text('Tel:' + receipt['company']['phone'] + '\n')
|
|
|
|
if check(receipt['company']['vat']):
|
|
|
|
eprint.text('VAT:' + receipt['company']['vat'] + '\n')
|
|
|
|
if check(receipt['company']['email']):
|
|
|
|
eprint.text(receipt['company']['email'] + '\n')
|
|
|
|
if check(receipt['company']['website']):
|
|
|
|
eprint.text(receipt['company']['website'] + '\n')
|
2014-01-02 14:15:19 +00:00
|
|
|
if check(receipt['header']):
|
|
|
|
eprint.text(receipt['header']+'\n')
|
2013-12-26 16:56:20 +00:00
|
|
|
if check(receipt['cashier']):
|
|
|
|
eprint.text('-'*32+'\n')
|
|
|
|
eprint.text('Served by '+receipt['cashier']+'\n')
|
|
|
|
|
|
|
|
# Orderlines
|
|
|
|
eprint.text('\n\n')
|
|
|
|
eprint.set(align='center')
|
|
|
|
for line in receipt['orderlines']:
|
|
|
|
pricestr = price(line['price_display'])
|
|
|
|
if line['discount'] == 0 and line['unit_name'] == 'Unit(s)' and line['quantity'] == 1:
|
|
|
|
eprint.text(printline(line['product_name'],pricestr,ratio=0.6))
|
2013-12-24 15:11:12 +00:00
|
|
|
else:
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint.text(printline(line['product_name'],ratio=0.6))
|
|
|
|
if line['discount'] != 0:
|
|
|
|
eprint.text(printline('Discount: '+str(line['discount'])+'%', ratio=0.6, indent=2))
|
|
|
|
if line['unit_name'] == 'Unit(s)':
|
|
|
|
eprint.text( printline( quantity(line['quantity']) + ' x ' + price(line['price']), pricestr, ratio=0.6, indent=2))
|
2013-12-20 16:30:57 +00:00
|
|
|
else:
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint.text( printline( quantity(line['quantity']) + line['unit_name'] + ' x ' + price(line['price']), pricestr, ratio=0.6, indent=2))
|
|
|
|
|
|
|
|
# Subtotal if the taxes are not included
|
|
|
|
taxincluded = True
|
|
|
|
if money(receipt['subtotal']) != money(receipt['total_with_tax']):
|
2013-12-20 16:30:57 +00:00
|
|
|
eprint.text(printline('','-------'));
|
2013-12-26 17:08:30 +00:00
|
|
|
eprint.text(printline(_('Subtotal'),money(receipt['subtotal']),width=40, ratio=0.6))
|
2014-01-06 14:54:46 +00:00
|
|
|
print_taxes()
|
|
|
|
#eprint.text(printline(_('Taxes'),money(receipt['total_tax']),width=40, ratio=0.6))
|
2013-12-26 16:56:20 +00:00
|
|
|
taxincluded = False
|
|
|
|
|
|
|
|
|
|
|
|
# Total
|
|
|
|
eprint.text(printline('','-------'));
|
|
|
|
eprint.set(align='center',height=2)
|
2013-12-26 17:08:30 +00:00
|
|
|
eprint.text(printline(_(' TOTAL'),money(receipt['total_with_tax']),width=40, ratio=0.6))
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint.text('\n\n');
|
|
|
|
|
|
|
|
# Paymentlines
|
|
|
|
eprint.set(align='center')
|
|
|
|
for line in receipt['paymentlines']:
|
|
|
|
eprint.text(printline(line['journal'], money(line['amount']), ratio=0.6))
|
|
|
|
|
|
|
|
eprint.text('\n');
|
|
|
|
eprint.set(align='center',height=2)
|
2013-12-26 17:08:30 +00:00
|
|
|
eprint.text(printline(_(' CHANGE'),money(receipt['change']),width=40, ratio=0.6))
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint.set(align='center')
|
|
|
|
eprint.text('\n');
|
|
|
|
|
|
|
|
# Extra Payment info
|
|
|
|
if receipt['total_discount'] != 0:
|
2013-12-26 17:08:30 +00:00
|
|
|
eprint.text(printline(_('Discounts'),money(receipt['total_discount']),width=40, ratio=0.6))
|
2013-12-26 16:56:20 +00:00
|
|
|
if taxincluded:
|
2014-01-06 14:54:46 +00:00
|
|
|
print_taxes()
|
|
|
|
#eprint.text(printline(_('Taxes'),money(receipt['total_tax']),width=40, ratio=0.6))
|
2013-12-26 16:56:20 +00:00
|
|
|
|
|
|
|
# Footer
|
2014-01-02 14:15:19 +00:00
|
|
|
if check(receipt['footer']):
|
|
|
|
eprint.text('\n'+receipt['footer']+'\n\n')
|
2013-12-26 16:56:20 +00:00
|
|
|
eprint.text(receipt['name']+'\n')
|
|
|
|
eprint.text( str(receipt['date']['date']).zfill(2)
|
|
|
|
+'/'+ str(receipt['date']['month']+1).zfill(2)
|
|
|
|
+'/'+ str(receipt['date']['year']).zfill(4)
|
|
|
|
+' '+ str(receipt['date']['hour']).zfill(2)
|
|
|
|
+':'+ str(receipt['date']['minute']).zfill(2) )
|
2013-12-20 16:30:57 +00:00
|
|
|
|