def test_current_items(): session = cashdesk_session_before_factory(create_items=False) buser = user_factory(troubleshooter=True, superuser=True) item_full = Item.objects.create(name='Full pass', description='', initial_stock=200) item_1d = Item.objects.create(name='One day pass', description='', initial_stock=100) prod_full = Product.objects.create(name='Full ticket', price=23, tax_rate=19) prod_1d = Product.objects.create(name='One day ticket', price=12, tax_rate=19) ProductItem.objects.create(product=prod_full, item=item_full, amount=1) ProductItem.objects.create(product=prod_1d, item=item_1d, amount=1) ItemMovement.objects.create(session=session, item=item_full, amount=20, backoffice_user=buser) ItemMovement.objects.create(session=session, item=item_1d, amount=10, backoffice_user=buser) for i in range(3): transaction_position_factory(transaction_factory(session), prod_full) for i in range(2): transaction_position_factory(transaction_factory(session), prod_1d) trans = transaction_position_factory(transaction_factory(session), prod_1d).transaction reverse_transaction(trans_id=trans.pk, current_session=session) session.end = now() session.save() ItemMovement.objects.create(session=session, item=item_full, amount=-17, backoffice_user=buser) ItemMovement.objects.create(session=session, item=item_1d, amount=-5, backoffice_user=buser) assert session.get_current_items() == [{ 'movements': 20, 'total': 0, 'transactions': 3, 'item': item_full, 'final_movements': 17 }, { 'movements': 10, 'total': 3, 'transactions': 2, 'item': item_1d, 'final_movements': 5 }]
def test_reverse_wrong_session(): session1 = cashdesk_session_before_factory() session2 = cashdesk_session_before_factory() trans = transaction_factory(session1) transaction_position_factory(transaction=trans) with pytest.raises(FlowError) as excinfo: reverse_transaction(trans_id=trans.pk, current_session=session2) assert excinfo.value.message == 'Only troubleshooters can reverse sales from other sessions.'
def test_reverse_wrong_session_troubleshooter(): session1 = cashdesk_session_before_factory() session2 = cashdesk_session_before_factory(user=user_factory( troubleshooter=True)) trans = transaction_factory(session1) transaction_position_factory(transaction=trans) reverse_transaction(trans_id=trans.pk, current_session=session2, authorized_by=user_factory(troubleshooter=True))
def test_reverse_double(): session = cashdesk_session_before_factory() trans = transaction_factory(session) transaction_position_factory(transaction=trans, product=product_factory(items=True)) transaction_position_factory(transaction=trans) reverse_transaction(trans_id=trans.pk, current_session=session) with pytest.raises(FlowError) as excinfo: reverse_transaction(trans_id=trans.pk, current_session=session) assert excinfo.value.message == 'At least one position of this transaction has already been reversed.'
def test_product_sales(): session = cashdesk_session_before_factory(create_items=False) prod_full = Product.objects.create(name='Full ticket', price=23, tax_rate=19) for i in range(3): transaction_position_factory(transaction_factory(session), prod_full) trans = transaction_position_factory(transaction_factory(session), prod_full) reverse_transaction(trans_id=trans.transaction_id, current_session=session) TransactionPosition.objects.create( type='redeem', value=10, tax_rate=19, product=prod_full, transaction=transaction_factory(session), has_constraint_bypass=True) TransactionPosition.objects.create( type='redeem', value=0, tax_rate=0, product=prod_full, transaction=transaction_factory(session), ) def keyfunc(d): return d['value_single'] assert sorted(session.get_product_sales(), key=keyfunc) == sorted([{ 'product': prod_full, 'presales': 0, 'reversals': 1, 'sales': 4, 'value_total': Decimal('69.00'), 'value_single': Decimal('23.00'), }, { 'product': prod_full, 'presales': 1, 'reversals': 0, 'sales': 0, 'value_total': Decimal('00.00'), 'value_single': Decimal('00.00'), }, { 'product': prod_full, 'presales': 1, 'reversals': 0, 'sales': 0, 'value_total': Decimal('10.00'), 'value_single': Decimal('10.00'), }], key=keyfunc)
def test_reverse_success(): session = cashdesk_session_before_factory() trans = transaction_factory(session) pos = [ transaction_position_factory(transaction=trans, product=product_factory(items=True)), transaction_position_factory(transaction=trans) ] revtransid = reverse_transaction(trans_id=trans.pk, current_session=session) revtrans = Transaction.objects.get(pk=revtransid) assert revtrans.session == session revpos = revtrans.positions.all() assert len(revpos) == len(pos) for lp, rp in zip(pos, revpos): assert rp.reverses == lp assert rp.type == 'reverse' assert rp.value == -1 * lp.value assert rp.tax_value == -1 * lp.tax_value assert rp.product == lp.product assert {i.id for i in lp.items.all()} == {i.id for i in rp.items.all()} ls = TransactionPositionItem.objects.filter(position=lp).aggregate( s=Sum('amount'))['s'] if ls: rs = TransactionPositionItem.objects.filter(position=rp).aggregate( s=Sum('amount'))['s'] assert rs == ls * -1
def test_cash_transaction_total(): session = cashdesk_session_before_factory(create_items=False) prod_full = Product.objects.create(name='Full ticket', price=23, tax_rate=19) for i in range(3): transaction_position_factory(transaction_factory(session), prod_full) trans = transaction_position_factory(transaction_factory(session), prod_full).transaction reverse_transaction(trans_id=trans.pk, current_session=session) TransactionPosition.objects.create( type='redeem', value=10, tax_rate=19, product=prod_full, transaction=transaction_factory(session), has_constraint_bypass=True) assert session.get_cash_transaction_total() == 23 * 3 + 10
def test_reverse_unknown(): session = cashdesk_session_before_factory() with pytest.raises(FlowError) as excinfo: reverse_transaction(trans_id=1234678, current_session=session) assert excinfo.value.message == 'Transaction ID not known.'