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, Inventory, InventoryLine, WriteOff grach = Client.objects.get(schema_name='grach') connection.set_tenant(grach) print("=== Inventory Reconciliation Test ===\n") product = Product.objects.get(sku='FLOWER-001') warehouse = Warehouse.objects.get(name='Main Warehouse') # Проверяем текущий остаток print("Step 1: Current stock status\n") current_qty = sum(b.quantity for b in product.stock_batches.filter(warehouse=warehouse)) print("Current system stock: {} units".format(current_qty)) # Создаём инвентаризацию print("\nStep 2: Creating inventory (physical count)\n") inventory = Inventory.objects.create( warehouse=warehouse, status='draft' ) print("Inventory created (status=draft)") # Добавляем строку с физическим подсчётом (меньше, чем в системе - ДЕФИЦИТ) print("\nStep 3: Adding inventory line with deficit\n") # Физический подсчёт: 5 единиц (система имеет 7) inventory_line = InventoryLine.objects.create( inventory=inventory, product=product, quantity_system=current_qty, quantity_fact=Decimal('5') ) print("Inventory line: system={}, fact={}, difference={}".format( inventory_line.quantity_system, inventory_line.quantity_fact, inventory_line.difference )) # Завершаем инвентаризацию print("\nStep 4: Finishing inventory (processing reconciliation)\n") inventory.status = 'completed' inventory.save() print("Inventory status: completed") # Проверяем все WriteOff операции print("\nStep 5: Checking WriteOff operations\n") writeoffs = WriteOff.objects.all().order_by('-date') print("Total WriteOff records: {}".format(writeoffs.count())) if writeoffs.exists(): print("Recent WriteOffs:") for wo in writeoffs[:3]: print(" - qty={}, reason={}".format( wo.quantity, wo.reason )) print("PASS: WriteOff created for deficit") else: print(" No WriteOff found") # Проверяем новый остаток print("\nStep 6: Final stock status\n") final_qty = sum(b.quantity for b in product.stock_batches.filter(warehouse=warehouse)) print("Final system stock: {} units".format(final_qty)) print("Expected: 5 units") if final_qty == Decimal('5'): print("PASS: Stock reconciled correctly") else: print("FAIL: Stock mismatch (expected 5, got {})".format(final_qty))