def __set__(self, instance, value): recipe_categories = instance.xml.xpath('./head/recipe_categories') if len(recipe_categories) != 0: instance.xml.head.remove(recipe_categories[0]) value = self._remove_duplicates(value) if len(value) > 0: el = E.recipe_categories() for item in value: el.append(E.category(code=item.code)) instance.xml.head.append(el)
def invoice_begin(self, **kwargs): defined_args = dict(invoice_type=('invoice', 'pharmacy'), number=None, nip=None, description=('both', 'original'), paymentname=None, paymentdate=None, recipient=None, issuer=None, copies=None, margins=('yes', 'no'), signarea=('yes', 'no'), customernameoptions=('info', 'all', 'none'), sellernameoptions=('info', 'all', 'none'), paidlabel=None, selldate=None, buyerlabel=None, additionalinfo=None) cmd = E.invoice() cmd.set('action', 'begin') # Prepare options options = set(kwargs.pop('options', [])) if options: for op in options: if op in range(1, 20): op_tag = cmd.append(E.option('', id=str(op))) # Prepare customer info customer = kwargs.pop('customer', []) for cust in customer: cust_tag = cmd.append(E.customer(cust)) args = {} for name, value in kwargs.items(): if name not in defined_args: raise TypeError('Unknown argument: {}'.format(name)) allowed_values = defined_args.get(name) if allowed_values is not None and value not in allowed_values: raise TypeError( 'Argument {} has wrong value: {} (not in {})'.format( name, value, allowed_values)) if value is not None: cmd.set(name, str(value)) self.send_command(cmd, check_for_errors=True)
def non_fiscal_printout(self, lines, systemno='', nonfiscalheader='yes'): cmd = E.nonfiscalprintout('', systemno=systemno, nonfiscalheader=nonfiscalheader) for line in lines: if isinstance(line, collections.Mapping): text = line.pop('text', '') cmd.append(E.line(text, **line)) else: # Just a text line cmd.append(E.line(line)) self.send_command(cmd, check_for_errors=True)
def receipt_begin(self, mode='online', pharmaceutical='no'): cmd = E.receipt('', action='begin', mode=mode, pharmaceutical=pharmaceutical) self.send_command(cmd, check_for_errors=True)
def markup(self, value, name, descid): cmd = E.discount('', value=value, name=name, descid=str(descid), action='markup') self.send_command(cmd, check_for_errors=True)
def invoice_close(self, total, systemno, checkout, cashier, buyer): cmd = E.invoice('', action='close', total=str(total), systemno=systemno, checkout=checkout, cashier=cashier, buyer=buyer) self.send_command(cmd, check_for_errors=True)
def payment_add(self, type_, value, rate=1, mode='payment', name=''): cmd = E.payment('', action='add', type=type_, value=str(value), rate=str(rate), mode=mode, name=name) self.send_command(cmd, check_for_errors=True)
def ingredients(self, value): for node in self.xml.xpath('./ingredient'): node.getparent().remove(node) for item in value: self.xml.append( E.ingredient( code=item.code, amount=item.amount if hasattr(item, 'amount') else '', unit=item.unit if hasattr(item, 'unit') else '', details=item.details if hasattr(item, 'details') else ''))
def info_checkout(self, type_='receipt'): cmd = E.info('', action='checkout', type=type_, isfiscal='?', lasterror='?') return self.send_command( cmd, read_reply=True, ).info
def taxrates_get(self): cmd = E.taxrates('', action='get') res = self.send_command( cmd, read_reply=True, ) tax_rates = [(ptu.get('name'), ptu.text) for ptu in res.taxrates.ptu] return tax_rates
def init(self, title=None, style=None, **body): self.html = E.html(E.head(), E.body(E.div(), **body)) self.html.body.div.set('class', 'container') if title: self.html.head.append(E.title(title)) if style: self.html.head.append(E.style(style, type='text/css'))
def item(self, name, quantity, quantityunit, ptu, price, plu='', action='sale', recipe='', charge='', description='', discount_name='', discount_value=None, discount_descid=0): if discount_value is not None: dsc = E.discount('', action='discount', name=discount_name, value=discount_value, descid=str(discount_descid)) else: dsc = '' cmd = E.item(dsc, name=name, quantity=str(quantity), quantityunit=quantityunit, ptu=ptu, price=str(price), plu=plu, action=action, recipe=recipe, charge=charge, description=description) self.send_command(cmd, check_for_errors=True)
def receipt_close(self, total, systemno, checkout, cashier, charge=None, nip=None): cmd = E.receipt('', action='close', total=str(total), systemno=systemno, checkout=checkout, cashier=cashier) if charge is not None: cmd.set('charge', str(charge)) if nip is not None: cmd.set('nip', nip) self.send_command(cmd, check_for_errors=True)
def test_etree_to_bytes(): assert etree_to_bytes(E.dle()) == b'<dle/>'
def test_send_existing_command(printer): pkt = printer.send_command(E.dle(), True) assert pkt.dle.get('online') in ('yes', 'no')
def test_send_not_existing_command(printer): printer.set_error('silent') with pytest.raises(ProtocolError): pkt = printer.send_command(E.not_existing(), False, True)
def receipt_cancel(self): cmd = E.receipt('', action='cancel') self.send_command(cmd, check_for_errors=True)
def get_error(self): cmd = E.error('', action='get', value='') return int(self.send_command(cmd, True).error.get('value'))
def set_error(self, value): cmd = E.error('', action='set', value=value) self.send_command(cmd)
def open_drawer(self): cmd = E.control('', action='drawer') self.send_command(cmd, check_for_errors=True)
def test_assemble_packet_crc(): assert etree_to_bytes(assemble_packet( E.dle(), True)) == b'<packet crc="4259D34E"><dle/></packet>'
def test_assemble_packet_no_crc(): assert etree_to_bytes(assemble_packet( E.dle())) == b'<packet><dle/></packet>'
def dle(self): return self.send_command(E.dle(), True).dle.attrib
def __init__(self, package_name='data_server', folder='templates'): self.env = Environment(loader=PackageLoader(package_name, folder)) self.html = E.html()
def enq(self): return self.send_command(E.enq(), True).enq.attrib
def generate_table(self, col, **kwargs): table = E.table(**kwargs) for attr, value in col: table.append(E.tr(E.td(value, **attr))) return table
def invoice_cancel(self): cmd = E.invoice('', action='cancel') self.send_command(cmd, check_for_errors=True)