Files
@ 827d8cf9243d
Branch filter:
Location: rattail-project/rattail/rattail/batch/labels.py
827d8cf9243d
9.3 KiB
text/x-python
Add basic "auto-execute" logic for new batches created via filemon
this is for the sake of label batches in particular, when deploying several
copies from host to all store nodes
this is for the sake of label batches in particular, when deploying several
copies from host to all store nodes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | # -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Handler for label batches
"""
from __future__ import unicode_literals, absolute_import
import csv
import logging
import six
import sqlalchemy as sa
from sqlalchemy import orm
from rattail import enum
from rattail.db import model, api
from rattail.gpc import GPC
from rattail.batch import BatchHandler
from rattail.csvutil import UnicodeDictReader
from rattail.util import progress_loop
from rattail.time import make_utc
from rattail.config import parse_bool
log = logging.getLogger(__name__)
class LabelBatchHandler(BatchHandler):
"""
Handler for Print Labels batches.
"""
batch_model_class = model.LabelBatch
def setup(self, batch, progress=None):
self.now = make_utc()
setup_populate = setup
setup_refresh = setup
def make_batch(self, session, progress=None, **kwargs):
"""
Make a new batch, with initial rows if applicable.
"""
self.skip_first_line = parse_bool(kwargs.pop('skip_first_line', False))
self.calc_check_digit = kwargs.pop('calc_check_digit', False)
if self.calc_check_digit != 'upc':
self.calc_check_digit = parse_bool(self.calc_check_digit)
return super(LabelBatchHandler, self).make_batch(session, progress, **kwargs)
def auto_executable(self, batch):
"""
Must return a boolean indicating whether the given bath is eligible for
"automatic" execution, i.e. immediately after batch is created.
"""
if batch.filename and '.autoexecute.' in batch.filename:
return True
return False
def populate(self, batch, progress=None):
"""
Pre-fill batch with row data from handheld batch, etc.
"""
assert batch.handheld_batch or batch.filename or batch.products
session = orm.object_session(batch)
self.label_profile = self.get_label_profile(session)
assert self.label_profile
self.setup_populate(batch, progress=progress)
def append(item, i):
row = model.LabelBatchRow()
row.label_code = self.label_profile.code
row.label_profile = self.label_profile
with session.no_autoflush:
if isinstance(item, model.Product):
row.product = item
row.upc = row.product.upc
row.label_quantity = 1
else: # item is handheld batch row
row.upc = item.upc
row.product = item.product
row.label_quantity = item.units or 1
# copy these in case product is null
row.brand_name = item.brand_name
row.description = item.description
row.size = item.size
batch.add_row(row)
self.refresh_row(row)
if batch.handheld_batch:
data = batch.handheld_batch.active_rows()
elif batch.filename:
data = self.read_products_from_file(batch, progress=progress)
elif batch.products:
data = batch.products
assert progress_loop(append, data, progress,
message="Adding initial rows to batch")
def read_products_from_file(self, batch, progress=None):
"""
Returns list of Product objects based on lookup from CSV file data.
# TODO: should this actually happen here? vs refresh and just mark product not found?
"""
path = batch.filepath(self.config)
with open(path, 'rb') as f:
if self.skip_first_line:
f.readline()
reader = csv.reader(f)
data = [{'upc': row[0]} for row in reader]
else:
reader = UnicodeDictReader(f)
data = list(reader)
products = []
session = orm.object_session(batch)
def append(entry, i):
upc = entry['upc'].strip()
if upc:
upc = GPC(upc, calc_check_digit=self.calc_check_digit)
product = api.get_product_by_upc(session, upc)
if product:
products.append(product)
else:
log.warning("product not found: {}".format(upc))
self.progress_loop(append, data, progress,
message="Reading data from CSV file")
return products
def get_label_profile(self, session):
code = self.config.get('rattail.batch', 'labels.default_code')
if code:
return session.query(model.LabelProfile)\
.filter(model.LabelProfile.code == code)\
.one()
else:
return session.query(model.LabelProfile)\
.order_by(model.LabelProfile.ordinal)\
.first()
def refresh_row(self, row):
"""
Inspect a row from the source data and populate additional attributes
for it, according to what we find in the database.
"""
if not row.product and row.upc:
session = orm.object_session(row)
row.product = api.get_product_by_upc(session, row.upc)
if not row.product:
row.status_code = row.STATUS_PRODUCT_NOT_FOUND
return
product = row.product
row.brand_name = six.text_type(product.brand or '')
row.description = product.description
row.size = product.size
department = product.department
row.department_number = department.number if department else None
row.department_name = department.name if department else None
category = product.category
row.category_code = category.code if category else None
row.category_name = category.name if category else None
regular_price = product.regular_price
row.regular_price = regular_price.price if regular_price else None
row.pack_quantity = regular_price.pack_multiple if regular_price else None
row.pack_price = regular_price.pack_price if regular_price else None
sale_price = product.current_price
if sale_price:
if (sale_price.type == enum.PRICE_TYPE_SALE and
sale_price.starts and sale_price.starts <= self.now and
sale_price.ends and sale_price.ends >= self.now):
pass # this is what we want
else:
sale_price = None
row.sale_price = sale_price.price if sale_price else None
row.sale_start = sale_price.starts if sale_price else None
row.sale_stop = sale_price.ends if sale_price else None
cost = product.cost
vendor = cost.vendor if cost else None
row.vendor_id = vendor.id if vendor else None
row.vendor_name = vendor.name if vendor else None
row.vendor_item_code = cost.code if cost else None
row.case_quantity = cost.case_size if cost else None
if row.regular_price:
row.status_code = row.STATUS_OK
else:
row.status_code = row.STATUS_REGULAR_PRICE_UNKNOWN
def execute(self, batch, progress=None, **kwargs):
"""
Print some labels!
"""
return self.print_labels(batch, progress)
def print_labels(self, batch, progress=None):
"""
Print all labels for the given batch.
"""
profiles = {}
def organize(row, i):
profile = row.label_profile
if profile.uuid not in profiles:
profiles[profile.uuid] = profile
profile.labels = []
profile.labels.append((row.product, row.label_quantity))
# filter out removed rows, and maybe inactive product rows
rows = [row for row in batch.data_rows if not row.removed]
if self.config.getbool('rattail.batch', 'labels.exclude_inactive_products', default=False):
rows = [row for row in rows if row.status_code != row.STATUS_PRODUCT_APPEARS_INACTIVE]
if not progress_loop(organize, rows, progress,
message="Organizing labels by type"):
return False # user canceled
# okay now print for real
# TODO: this is compatible with legacy code but will re-read all needed
# product data from master table instead of levaraging batch rows
for profile in profiles.itervalues():
printer = profile.get_printer(self.config)
printer.print_labels(profile.labels, progress=progress)
return True
|