Add first draft logic for executing CORE equity import batch
This commit is contained in:
parent
2594245813
commit
c0cfc7714d
|
@ -24,6 +24,8 @@
|
|||
Handler for CORE equity import batches
|
||||
"""
|
||||
|
||||
from corepos.db.office_trans import model as coretrans, Session as CoreTransSession
|
||||
|
||||
from rattail.batch import BatchHandler
|
||||
from rattail_corepos.db.model import CoreEquityImportBatch
|
||||
|
||||
|
@ -38,6 +40,9 @@ class CoreEquityImportBatchHandler(BatchHandler):
|
|||
session = self.app.get_session(row)
|
||||
model = self.model
|
||||
|
||||
row.status_code = None
|
||||
row.status_text = None
|
||||
|
||||
if not row.card_number:
|
||||
row.status_code = row.STATUS_MISSING_VALUES
|
||||
row.status_text = "card_number"
|
||||
|
@ -59,6 +64,10 @@ class CoreEquityImportBatchHandler(BatchHandler):
|
|||
return
|
||||
|
||||
payment = row.payment
|
||||
if payment and payment.corepos_transaction_number:
|
||||
row.status_code = row.STATUS_ALREADY_IN_CORE
|
||||
return
|
||||
|
||||
member = payment.member if payment else None
|
||||
row.member = member
|
||||
if not member:
|
||||
|
@ -84,24 +93,58 @@ class CoreEquityImportBatchHandler(BatchHandler):
|
|||
def get_effective_rows(self, batch):
|
||||
return [row for row in batch.active_rows()
|
||||
if row.status_code not in (row.STATUS_MISSING_VALUES,
|
||||
row.STATUS_MEMBER_NOT_FOUND)]
|
||||
row.STATUS_MEMBER_NOT_FOUND,
|
||||
row.STATUS_ALREADY_IN_CORE)]
|
||||
|
||||
def execute(self, batch, progress=None, **kwargs):
|
||||
if self.config.production():
|
||||
raise NotImplementedError("TODO: not yet implemented for production")
|
||||
|
||||
session = self.app.get_session(batch)
|
||||
rows = self.get_effective_rows(batch)
|
||||
self.export_payments_to_corepos(rows, progress=progress)
|
||||
return True
|
||||
|
||||
def process(row, i):
|
||||
def export_payments_to_corepos(self, rows, progress=None):
|
||||
coretrans_session = CoreTransSession()
|
||||
|
||||
def export(row, i):
|
||||
|
||||
# will insert 2 records in `core_trans.dtransactions` ...
|
||||
|
||||
# they will have this data in common
|
||||
host_data = {
|
||||
'store_id': 1, # TODO: all "normal" txns have store_id=1
|
||||
'register_number': 1, # TODO: what should this be? surely can't be empty
|
||||
'transaction_number': 42, # TODO: how should we generate this? unique per register_no?
|
||||
'date_time': self.app.localtime(row.timestamp, from_utc=True, tzinfo=False),
|
||||
'card_number': row.card_number,
|
||||
'total': row.payment_amount,
|
||||
}
|
||||
|
||||
# first a record to ring up the equity item
|
||||
detail = coretrans.TransactionDetail(**host_data)
|
||||
detail.upc = 'TEST_ITEM' # TODO: what should this say?
|
||||
detail.description = 'TEST_EQUITY_ITEM' # TODO: what should this say?
|
||||
detail.department_number = row.department_number
|
||||
detail.quantity = 1 # TODO: should this ever be anything else?
|
||||
detail.unit_price = row.payment_amount
|
||||
coretrans_session.add(detail)
|
||||
|
||||
# then a record to accept tender payment
|
||||
detail = coretrans.TransactionDetail(**host_data)
|
||||
detail.upc = 'TEST_TENDER' # TODO: what should this say?
|
||||
detail.description = 'TEST_EQUITY_TENDER' # TODO: what should this say?
|
||||
coretrans_session.add(detail)
|
||||
|
||||
# update payment in Rattail to indicate pending status in CORE
|
||||
payment = row.payment
|
||||
if payment:
|
||||
payment.corepos_card_number = row.card_number
|
||||
payment.corepos_department_number = row.department_number
|
||||
payment.corepos_transaction_number = 'pending'
|
||||
if i % 200 == 0:
|
||||
session.flush()
|
||||
|
||||
self.progress_loop(process, rows, progress,
|
||||
message="Processing payments")
|
||||
return True
|
||||
self.progress_loop(export, rows, progress,
|
||||
message="Exporting payments to CORE-POS")
|
||||
|
||||
coretrans_session.commit()
|
||||
coretrans_session.close()
|
||||
|
|
|
@ -68,12 +68,14 @@ class CoreEquityImportBatchRow(model.BatchRowMixin, model.Base):
|
|||
STATUS_MEMBER_NOT_FOUND = 2
|
||||
STATUS_MISSING_VALUES = 3
|
||||
STATUS_NEEDS_ATTENTION = 4
|
||||
STATUS_ALREADY_IN_CORE = 5
|
||||
|
||||
STATUS = {
|
||||
STATUS_OK : "ok",
|
||||
STATUS_MEMBER_NOT_FOUND : "member not found",
|
||||
STATUS_MISSING_VALUES : "missing values",
|
||||
STATUS_NEEDS_ATTENTION : "needs attention",
|
||||
STATUS_ALREADY_IN_CORE : "already in CORE-POS",
|
||||
}
|
||||
|
||||
payment_uuid = sa.Column(sa.String(length=32), nullable=True)
|
||||
|
|
Loading…
Reference in a new issue