2023-09-13 13:15:47 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
|
|
|
# Copyright © 2010-2023 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 CORE equity import batches
|
|
|
|
"""
|
|
|
|
|
2023-09-13 21:29:16 -05:00
|
|
|
import datetime
|
|
|
|
|
2023-09-13 14:37:01 -05:00
|
|
|
from corepos.db.office_trans import model as coretrans, Session as CoreTransSession
|
|
|
|
|
2023-09-13 13:15:47 -05:00
|
|
|
from rattail.batch import BatchHandler
|
|
|
|
from rattail_corepos.db.model import CoreEquityImportBatch
|
|
|
|
|
|
|
|
|
|
|
|
class CoreEquityImportBatchHandler(BatchHandler):
|
|
|
|
"""
|
|
|
|
Handler for CORE member batches.
|
|
|
|
"""
|
|
|
|
batch_model_class = CoreEquityImportBatch
|
|
|
|
|
2023-11-05 14:13:22 -06:00
|
|
|
pseudo_remove_rows = False
|
|
|
|
|
2023-09-13 13:15:47 -05:00
|
|
|
def refresh_row(self, row):
|
|
|
|
session = self.app.get_session(row)
|
|
|
|
model = self.model
|
|
|
|
|
2023-09-13 14:37:01 -05:00
|
|
|
row.status_code = None
|
|
|
|
row.status_text = None
|
|
|
|
|
2023-09-13 13:15:47 -05:00
|
|
|
if not row.card_number:
|
|
|
|
row.status_code = row.STATUS_MISSING_VALUES
|
|
|
|
row.status_text = "card_number"
|
|
|
|
return
|
|
|
|
|
|
|
|
if not row.payment_amount:
|
|
|
|
row.status_code = row.STATUS_MISSING_VALUES
|
|
|
|
row.status_text = "payment_amount"
|
|
|
|
return
|
|
|
|
|
|
|
|
if not row.department_number:
|
|
|
|
row.status_code = row.STATUS_MISSING_VALUES
|
|
|
|
row.status_text = "department_number"
|
|
|
|
return
|
|
|
|
|
|
|
|
if not row.timestamp:
|
|
|
|
row.status_code = row.STATUS_MISSING_VALUES
|
|
|
|
row.status_text = "timestamp"
|
|
|
|
return
|
|
|
|
|
|
|
|
payment = row.payment
|
2023-09-13 14:37:01 -05:00
|
|
|
if payment and payment.corepos_transaction_number:
|
|
|
|
row.status_code = row.STATUS_ALREADY_IN_CORE
|
|
|
|
return
|
|
|
|
|
2023-09-13 13:15:47 -05:00
|
|
|
member = payment.member if payment else None
|
|
|
|
row.member = member
|
|
|
|
if not member:
|
|
|
|
row.status_code = row.STATUS_MEMBER_NOT_FOUND
|
|
|
|
return
|
|
|
|
|
|
|
|
memtype = member.membership_type
|
|
|
|
row.member_type_id = memtype.number if memtype else None
|
|
|
|
|
|
|
|
if member.person:
|
|
|
|
person = member.person
|
|
|
|
row.first_name = person.first_name
|
|
|
|
row.last_name = person.last_name
|
|
|
|
|
|
|
|
membership = self.app.get_membership_handler()
|
|
|
|
row.rattail_equity_total = membership.get_equity_total(member)
|
|
|
|
|
|
|
|
row.status_code = row.STATUS_OK
|
|
|
|
|
|
|
|
def describe_execution(self, batch, **kwargs):
|
|
|
|
return "New payment transactions will be added directly to CORE-POS."
|
|
|
|
|
|
|
|
def get_effective_rows(self, batch):
|
|
|
|
return [row for row in batch.active_rows()
|
|
|
|
if row.status_code not in (row.STATUS_MISSING_VALUES,
|
2023-09-13 14:37:01 -05:00
|
|
|
row.STATUS_MEMBER_NOT_FOUND,
|
|
|
|
row.STATUS_ALREADY_IN_CORE)]
|
2023-09-13 13:15:47 -05:00
|
|
|
|
|
|
|
def execute(self, batch, progress=None, **kwargs):
|
|
|
|
rows = self.get_effective_rows(batch)
|
2023-09-13 14:37:01 -05:00
|
|
|
self.export_payments_to_corepos(rows, progress=progress)
|
|
|
|
return True
|
2023-09-13 13:15:47 -05:00
|
|
|
|
2023-09-13 21:29:16 -05:00
|
|
|
def get_store_id(self, row):
|
|
|
|
# TODO: what should this be?
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def get_register_number(self, row):
|
|
|
|
# TODO: what should this be?
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def get_employee_number(self, row):
|
|
|
|
# TODO: what should this be?
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_next_transaction_number(self, session):
|
|
|
|
# TODO: how should we generate this?
|
|
|
|
return self.consume_batch_id(session)
|
|
|
|
|
|
|
|
def get_next_timestamp(self, row):
|
|
|
|
dt = self.next_timestamp
|
|
|
|
self.next_timestamp += datetime.timedelta(seconds=1)
|
|
|
|
return self.app.localtime(dt, tzinfo=False).replace(microsecond=0)
|
|
|
|
|
|
|
|
def get_transaction_status(self, row):
|
|
|
|
# TODO: what should this be?
|
|
|
|
return 'G'
|
|
|
|
|
2023-09-13 14:37:01 -05:00
|
|
|
def export_payments_to_corepos(self, rows, progress=None):
|
|
|
|
coretrans_session = CoreTransSession()
|
2023-09-13 21:29:16 -05:00
|
|
|
self.next_timestamp = self.app.localtime()
|
2023-09-13 14:37:01 -05:00
|
|
|
|
|
|
|
def export(row, i):
|
2023-09-13 21:29:16 -05:00
|
|
|
session = self.app.get_session(row)
|
2023-09-13 14:37:01 -05:00
|
|
|
|
|
|
|
# will insert 2 records in `core_trans.dtransactions` ...
|
|
|
|
|
|
|
|
# they will have this data in common
|
|
|
|
host_data = {
|
2023-09-13 21:29:16 -05:00
|
|
|
'store_id': self.get_store_id(row),
|
|
|
|
'register_number': self.get_register_number(row),
|
|
|
|
'transaction_number': self.get_next_transaction_number(session),
|
|
|
|
'employee_number': self.get_employee_number(row),
|
2023-09-13 14:37:01 -05:00
|
|
|
'card_number': row.card_number,
|
2023-09-13 21:29:16 -05:00
|
|
|
'date_time': self.get_next_timestamp(row),
|
2023-09-13 14:37:01 -05:00
|
|
|
'total': row.payment_amount,
|
2023-09-13 21:29:16 -05:00
|
|
|
'transaction_status': self.get_transaction_status(row),
|
2023-09-13 14:37:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# first a record to ring up the equity item
|
|
|
|
detail = coretrans.TransactionDetail(**host_data)
|
2023-09-13 21:29:16 -05:00
|
|
|
detail.transaction_id = 1
|
2023-09-13 14:37:01 -05:00
|
|
|
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)
|
2023-09-13 21:29:16 -05:00
|
|
|
detail.transaction_id = 2
|
2023-09-13 14:37:01 -05:00
|
|
|
detail.upc = 'TEST_TENDER' # TODO: what should this say?
|
|
|
|
detail.description = 'TEST_EQUITY_TENDER' # TODO: what should this say?
|
|
|
|
coretrans_session.add(detail)
|
|
|
|
|
2023-09-13 21:29:16 -05:00
|
|
|
# update payment in Rattail to indicate presence in CORE
|
2023-09-13 13:15:47 -05:00
|
|
|
payment = row.payment
|
|
|
|
if payment:
|
2023-09-13 21:29:16 -05:00
|
|
|
# nb. these must match exactly to be used later as importer key
|
2023-09-13 13:15:47 -05:00
|
|
|
payment.corepos_card_number = row.card_number
|
|
|
|
payment.corepos_department_number = row.department_number
|
2023-09-13 21:29:16 -05:00
|
|
|
payment.corepos_transaction_number = f"{host_data['employee_number']}-{host_data['register_number']}-{host_data['transaction_number']}"
|
|
|
|
payment.corepos_transaction_id = 1
|
|
|
|
payment.corepos_datetime = self.app.make_utc(self.app.localtime(host_data['date_time']))
|
|
|
|
# TODO: stop setting these, *after* importer stops using for key
|
|
|
|
# (or perhaps config should determine whether to set?)
|
|
|
|
payment.received = payment.corepos_datetime
|
|
|
|
payment.transaction_identifier = payment.corepos_transaction_number
|
2023-09-13 13:15:47 -05:00
|
|
|
|
2023-09-13 14:37:01 -05:00
|
|
|
self.progress_loop(export, rows, progress,
|
|
|
|
message="Exporting payments to CORE-POS")
|
|
|
|
|
|
|
|
coretrans_session.commit()
|
|
|
|
coretrans_session.close()
|