From 60bc6dd2367b62c948653b1a72bcabeb1f393ad1 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 13 Sep 2023 21:29:16 -0500 Subject: [PATCH] Add CORE-specific datetime for equity payments in case payments are added to CORE on a later date, want to keep original timestamp separate --- rattail_corepos/batch/equityimport.py | 53 ++++++++++++++++--- ...0c2_add_equity_payment_corepos_datetime.py | 33 ++++++++++++ rattail_corepos/db/model/people.py | 5 ++ rattail_corepos/importing/corepos/db.py | 11 ++-- rattail_corepos/importing/model.py | 1 + 5 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 rattail_corepos/db/alembic/versions/d426a274f0c2_add_equity_payment_corepos_datetime.py diff --git a/rattail_corepos/batch/equityimport.py b/rattail_corepos/batch/equityimport.py index 38b6daa..ab0b60f 100644 --- a/rattail_corepos/batch/equityimport.py +++ b/rattail_corepos/batch/equityimport.py @@ -24,6 +24,8 @@ Handler for CORE equity import batches """ +import datetime + from corepos.db.office_trans import model as coretrans, Session as CoreTransSession from rattail.batch import BatchHandler @@ -104,25 +106,56 @@ class CoreEquityImportBatchHandler(BatchHandler): self.export_payments_to_corepos(rows, progress=progress) return True + 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) + # import ipdb; ipdb.set_trace() + return self.app.localtime(dt, tzinfo=False).replace(microsecond=0) + + def get_transaction_status(self, row): + # TODO: what should this be? + return 'G' + def export_payments_to_corepos(self, rows, progress=None): coretrans_session = CoreTransSession() + self.next_timestamp = self.app.localtime() def export(row, i): + session = self.app.get_session(row) # 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), + '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), 'card_number': row.card_number, + 'date_time': self.get_next_timestamp(row), 'total': row.payment_amount, + 'transaction_status': self.get_transaction_status(row), } # first a record to ring up the equity item detail = coretrans.TransactionDetail(**host_data) + detail.transaction_id = 1 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 @@ -132,16 +165,24 @@ class CoreEquityImportBatchHandler(BatchHandler): # then a record to accept tender payment detail = coretrans.TransactionDetail(**host_data) + detail.transaction_id = 2 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 + # update payment in Rattail to indicate presence in CORE payment = row.payment if payment: + # nb. these must match exactly to be used later as importer key payment.corepos_card_number = row.card_number payment.corepos_department_number = row.department_number - payment.corepos_transaction_number = 'pending' + 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 self.progress_loop(export, rows, progress, message="Exporting payments to CORE-POS") diff --git a/rattail_corepos/db/alembic/versions/d426a274f0c2_add_equity_payment_corepos_datetime.py b/rattail_corepos/db/alembic/versions/d426a274f0c2_add_equity_payment_corepos_datetime.py new file mode 100644 index 0000000..97b99ab --- /dev/null +++ b/rattail_corepos/db/alembic/versions/d426a274f0c2_add_equity_payment_corepos_datetime.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8; -*- +"""add equity_payment.corepos_datetime + +Revision ID: d426a274f0c2 +Revises: 93978a7adc66 +Create Date: 2023-09-13 20:59:42.706994 + +""" + +# revision identifiers, used by Alembic. +revision = 'd426a274f0c2' +down_revision = '93978a7adc66' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa +import rattail.db.types + + + +def upgrade(): + + # corepos_member_equity_payment + op.add_column('corepos_member_equity_payment', sa.Column('corepos_datetime', sa.DateTime(), nullable=True)) + op.add_column('corepos_member_equity_payment_version', sa.Column('corepos_datetime', sa.DateTime(), autoincrement=False, nullable=True)) + + +def downgrade(): + + # corepos_member_equity_payment + op.drop_column('corepos_member_equity_payment_version', 'corepos_datetime') + op.drop_column('corepos_member_equity_payment', 'corepos_datetime') diff --git a/rattail_corepos/db/model/people.py b/rattail_corepos/db/model/people.py index 14df996..976c618 100644 --- a/rattail_corepos/db/model/people.py +++ b/rattail_corepos/db/model/people.py @@ -222,6 +222,10 @@ class CoreMemberEquityPayment(model.Base): ``stockpurchases.dept`` value for this payment, within CORE-POS. """) + corepos_datetime = sa.Column(sa.DateTime(), nullable=True, doc=""" + ``stockpurchases.datetime`` value for this payment, within CORE-POS. + """) + def __str__(self): return str(self.payment) @@ -229,3 +233,4 @@ CoreMemberEquityPayment.make_proxy(model.MemberEquityPayment, '_corepos', 'corep CoreMemberEquityPayment.make_proxy(model.MemberEquityPayment, '_corepos', 'corepos_transaction_number') CoreMemberEquityPayment.make_proxy(model.MemberEquityPayment, '_corepos', 'corepos_transaction_id') CoreMemberEquityPayment.make_proxy(model.MemberEquityPayment, '_corepos', 'corepos_department_number') +CoreMemberEquityPayment.make_proxy(model.MemberEquityPayment, '_corepos', 'corepos_datetime') diff --git a/rattail_corepos/importing/corepos/db.py b/rattail_corepos/importing/corepos/db.py index 7957e8d..924f493 100644 --- a/rattail_corepos/importing/corepos/db.py +++ b/rattail_corepos/importing/corepos/db.py @@ -269,6 +269,7 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu 'corepos_transaction_number', 'corepos_transaction_id', 'corepos_department_number', + 'corepos_datetime', ] def setup(self): @@ -319,20 +320,20 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu card_number, stock_purchase) return - received = stock_purchase.datetime - if received: - received = self.app.localtime(received) - received = self.app.make_utc(received) + dt = stock_purchase.datetime + if dt: + dt = self.app.make_utc(self.app.localtime(dt)) return { 'member_uuid': member.uuid, 'amount': stock_purchase.amount, - 'received': received, + 'received': dt, 'transaction_identifier': stock_purchase.transaction_number, 'corepos_card_number': stock_purchase.card_number, 'corepos_transaction_number': stock_purchase.transaction_number, 'corepos_transaction_id': stock_purchase.transaction_id, 'corepos_department_number': stock_purchase.department_number, + 'corepos_datetime': dt, } def create_object(self, key, host_data): diff --git a/rattail_corepos/importing/model.py b/rattail_corepos/importing/model.py index a71d52d..fa6ad5b 100644 --- a/rattail_corepos/importing/model.py +++ b/rattail_corepos/importing/model.py @@ -90,6 +90,7 @@ class MemberEquityPaymentImporter(importing.model.MemberEquityPaymentImporter): 'corepos_transaction_number', 'corepos_transaction_id', 'corepos_department_number', + 'corepos_datetime', ], }