import os import sys import django from decimal import Decimal sys.path.insert(0, 'C:/Users/team_/Desktop/test_qwen/myproject') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') django.setup() from tenants.models import Client from django.db import connection from products.models import Product from inventory.models import Warehouse, Sale, SaleBatchAllocation grach = Client.objects.get(schema_name='grach') connection.set_tenant(grach) print("=== Second Sale Test ===\n") product = Product.objects.get(sku='FLOWER-001') warehouse = Warehouse.objects.get(name='Main Warehouse') # Создаем вторую продажу на 20 единиц print("Creating second sale (qty=20)\n") sale2 = Sale.objects.create( product=product, warehouse=warehouse, quantity=Decimal('20'), sale_price=Decimal('250.00') ) print("Sale 2 created: qty=20\n") # Проверяем allocations allocations = SaleBatchAllocation.objects.filter(sale=sale2) total = sum(a.quantity for a in allocations) print("Allocations for Sale 2:") for alloc in allocations: print(" - qty={}, cost={}".format(alloc.quantity, alloc.cost_price)) print("\nTotal allocated: {}".format(total)) print("Sale qty: {}".format(sale2.quantity)) if total == sale2.quantity: print("PASS: Allocations match Sale qty") else: print("FAIL: Allocations ({}) != Sale qty ({})".format(total, sale2.quantity)) # Проверяем порядок FIFO (должны взяться из batch2 (7), batch3 (13)) expected = [Decimal('7'), Decimal('13')] actual = [a.quantity for a in allocations] print("\nExpected FIFO order: {}".format(expected)) print("Actual order: {}".format(actual)) if actual == expected: print("PASS: FIFO order correct") else: print("FAIL: FIFO order incorrect")