haikal/scripts/run1.py
2025-06-22 13:25:54 +03:00

63 lines
1.9 KiB
Python

from dotenv import load_dotenv
from django_ledger.models import EntityModel
# from rich import print
from inventory.models import CarMake
from django.contrib.auth import get_user_model
from django_ledger.io import roles
User = get_user_model()
load_dotenv(".env")
def run():
car_makes = CarMake.objects.all()[:10]
# Fetch the entity and COGS account
entity = EntityModel.objects.get(admin__email="ismail.mosa.ibrahim@gmail.com")
coa = entity.get_default_coa()
cogs = entity.get_default_coa_accounts().filter(role=roles.COGS).first()
last_account = (
entity.get_all_accounts()
.filter(role=roles.LIABILITY_CL_ACC_PAYABLE)
.order_by("-created")
.first()
)
if len(last_account.code) == 4:
code = f"{int(last_account.code)}{1:03d}"
elif len(last_account.code) > 4:
code = f"{int(last_account.code) + 1}"
print(code)
# # Loop through car makes and create accounts
# for make in range(len(car_makes)): # Start from 0 to include all items
# # Generate a unique code
# code = f"{cogs.code}{make + 1:03d}" # Example: "COGS-001", "COGS-002", etc.
# account = entity.create_account(
# name=car_makes[make].name,
# code=code,
# role=roles.COGS,
# coa_model=coa,
# balance_type="debit",
# active=True
# )
# try:
# account = cogs.add_child(instance=account)
# account.move(cogs, pos="sorted-sibling")
# account.refresh_from_db()
# account.save()
# except Exception as e:
# print(e)
# form_data = {
# 'name': car_makes[make].name,
# 'code': code,
# 'role': roles.COGS,
# 'balance_type': 'debit',
# 'active': True,
# 'coa_model': coa # Ensure the COA model is included
# }