312 lines
9.5 KiB
Python
312 lines
9.5 KiB
Python
from django.shortcuts import redirect
|
|
from django.contrib import messages
|
|
from django.utils import timezone
|
|
from django_ledger.models.journal_entry import JournalEntryModel
|
|
from django_ledger.models.ledger import LedgerModel
|
|
from django_ledger.models.transactions import TransactionModel
|
|
import requests
|
|
from inventory import models
|
|
from django.conf import settings
|
|
from django.core.mail import send_mail
|
|
from django.utils.translation import gettext_lazy as _
|
|
from inventory.utilities.financials import get_financial_value
|
|
from django_ledger.models.items import ItemModel
|
|
from django_ledger.models import InvoiceModel, EstimateModel
|
|
from decimal import Decimal
|
|
|
|
|
|
def get_jwt_token():
|
|
url = "https://carapi.app/api/auth/login"
|
|
headers = {
|
|
"accept": "text/plain",
|
|
"Content-Type": "application/json",
|
|
}
|
|
data = {
|
|
"api_token": "f5204a00-6f31-4de2-96d8-ed998e0d230c",
|
|
"api_secret": "8c11320781a5b8f4f327b6937e6f8241",
|
|
}
|
|
try:
|
|
response = requests.post(url, headers=headers, json=data)
|
|
response.raise_for_status()
|
|
return response.text
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"Error obtaining JWT token: {e}")
|
|
return None
|
|
|
|
|
|
def localize_some_words():
|
|
success = _("success")
|
|
error = _("error")
|
|
forget = _("Forgot Password?")
|
|
|
|
return None
|
|
|
|
|
|
def get_calculations(quotation):
|
|
context = {}
|
|
qc_len = quotation.quotation_cars.count()
|
|
cars = [x.car for x in quotation.quotation_cars.all()]
|
|
finances = models.CarFinance.objects.filter(car__in=cars)
|
|
|
|
services = ItemModel.objects.filter(additional_finances__in=finances).all()
|
|
data = [
|
|
{
|
|
"name": x.name,
|
|
"price": x.default_amount,
|
|
"total_price": x.default_amount * qc_len,
|
|
"vated": float(x.default_amount) * 0.15 * float(qc_len),
|
|
"total_price_vat": float(x.default_amount)
|
|
+ (float(x.default_amount) * 0.15 * float(qc_len)),
|
|
}
|
|
for x in services
|
|
]
|
|
context["services"] = data
|
|
context["total_cost"] = 0
|
|
context["total_vat"] = 0
|
|
context["total_cost_vat"] = 0
|
|
for k in context["services"]:
|
|
context["total_cost"] += k["total_price"]
|
|
context["total_vat"] += k["vated"]
|
|
context["total_cost_vat"] = float(context["total_cost"]) + float(
|
|
context["total_vat"]
|
|
)
|
|
return context
|
|
|
|
|
|
def send_email(from_, to_, subject, message):
|
|
subject = subject
|
|
message = message
|
|
from_email = from_
|
|
recipient_list = [to_]
|
|
send_mail(subject, message, from_email, recipient_list)
|
|
|
|
|
|
def get_user_type(request):
|
|
dealer = ""
|
|
if hasattr(request.user, "dealer"):
|
|
dealer = request.user.dealer
|
|
elif hasattr(request.user, "staff"):
|
|
dealer = request.user.staff.dealer
|
|
return dealer
|
|
|
|
|
|
def get_dealer_from_instance(instance):
|
|
if instance.dealer.staff:
|
|
return instance.dealer
|
|
else:
|
|
return instance.dealer
|
|
|
|
|
|
def reserve_car(car, request):
|
|
try:
|
|
reserved_until = timezone.now() + timezone.timedelta(hours=24)
|
|
models.CarReservation.objects.create(
|
|
car=car, reserved_by=request.user, reserved_until=reserved_until
|
|
)
|
|
car.status = models.CarStatusChoices.RESERVED
|
|
car.save()
|
|
messages.success(request, _("Car reserved successfully."))
|
|
except Exception as e:
|
|
messages.error(request, f"Error reserving car: {e}")
|
|
|
|
return redirect("car_detail", pk=car.pk)
|
|
|
|
|
|
def calculate_vat_amount(amount):
|
|
vat = models.VatRate.objects.filter(is_active=True).first()
|
|
if vat:
|
|
return ((amount * Decimal(vat.rate)).quantize(Decimal("0.01")), vat.rate)
|
|
return amount
|
|
|
|
|
|
def get_financial_values(model):
|
|
vat = models.VatRate.objects.filter(is_active=True).first()
|
|
|
|
if not model.get_itemtxs_data()[0].exists():
|
|
return {
|
|
"vat_amount": 0,
|
|
"total": 0,
|
|
"grand_total": 0,
|
|
"discount_amount": 0,
|
|
"vat": 0,
|
|
"car_and_item_info": [],
|
|
"additional_services": [],
|
|
}
|
|
|
|
data = model.get_itemtxs_data()[0].all()
|
|
|
|
if isinstance(model, InvoiceModel):
|
|
data = model.ce_model.get_itemtxs_data()[0].all()
|
|
|
|
car_and_item_info = [
|
|
{
|
|
"car": models.Car.objects.get(vin=x.item_model.name),
|
|
"total": models.Car.objects.get(
|
|
vin=x.item_model.name
|
|
).finances.selling_price
|
|
* Decimal(x.ce_quantity),
|
|
"itemmodel": x,
|
|
}
|
|
for x in data
|
|
]
|
|
total = sum(
|
|
Decimal(models.Car.objects.get(vin=x.item_model.name).finances.total)
|
|
* Decimal(x.ce_quantity)
|
|
for x in data
|
|
)
|
|
discount_amount = sum(
|
|
models.CarFinance.objects.get(car__vin=i.item_model.name).discount_amount
|
|
for i in data
|
|
)
|
|
|
|
additional_services = []
|
|
|
|
for i in data:
|
|
cf = models.CarFinance.objects.get(car__vin=i.item_model.name)
|
|
if cf.additional_services.exists():
|
|
additional_services.extend(
|
|
[
|
|
{"name": x.name, "price": x.price}
|
|
for x in cf.additional_services.all()
|
|
]
|
|
)
|
|
|
|
grand_total = Decimal(total) - Decimal(discount_amount)
|
|
vat_amount = round(Decimal(grand_total) * Decimal(vat.rate), 2)
|
|
return {
|
|
"car_and_item_info": car_and_item_info,
|
|
"total": total,
|
|
"discount_amount": discount_amount,
|
|
"additional_services": additional_services,
|
|
"grand_total": grand_total + vat_amount,
|
|
"vat_amount": vat_amount,
|
|
"vat": vat.rate,
|
|
}
|
|
|
|
|
|
def set_invoice_payment(dealer, entity, invoice, amount, payment_method):
|
|
vat_amount = 0
|
|
total_amount = 0
|
|
|
|
if invoice.terms == "on_receipt":
|
|
for x in invoice.get_itemtxs_data()[0].all():
|
|
vat_amount += models.Car.objects.get(
|
|
vin=x.item_model.name
|
|
).finances.vat_amount * Decimal(x.quantity)
|
|
total_amount += Decimal(x.unit_cost) * Decimal(x.quantity)
|
|
|
|
grand_total = total_amount - Decimal(vat_amount)
|
|
|
|
ledger = LedgerModel.objects.filter(
|
|
name__icontains=str(invoice.pk), entity=entity
|
|
).first()
|
|
journal = JournalEntryModel.objects.create(
|
|
posted=False,
|
|
description=f"Payment for Invoice {invoice.invoice_number}",
|
|
ledger=ledger,
|
|
locked=False,
|
|
origin="Payment",
|
|
)
|
|
credit_account = entity.get_default_coa_accounts().get(name="Sales Revenue")
|
|
debit_account = None
|
|
if payment_method == "cash":
|
|
debit_account = entity.get_default_coa_accounts().get(name="Cash", active=True)
|
|
elif payment_method == "credit":
|
|
debit_account = entity.get_default_coa_accounts().get(
|
|
name="Accounts Receivable", active=True
|
|
)
|
|
else:
|
|
debit_account = entity.get_default_coa_accounts().get(
|
|
name="Cash in Bank", active=True
|
|
)
|
|
|
|
vat_payable_account = entity.get_default_coa_accounts().get(
|
|
name="VAT Payable", active=True
|
|
)
|
|
TransactionModel.objects.create(
|
|
journal_entry=journal,
|
|
account=debit_account, # Debit Cash
|
|
amount=amount, # Payment amount
|
|
tx_type="debit",
|
|
description="Payment Received",
|
|
)
|
|
|
|
TransactionModel.objects.create(
|
|
journal_entry=journal,
|
|
account=credit_account, # Credit Accounts Receivable
|
|
amount=grand_total, # Payment amount
|
|
tx_type="credit",
|
|
description="Payment Received",
|
|
)
|
|
|
|
if vat_amount > 0:
|
|
TransactionModel.objects.create(
|
|
journal_entry=journal,
|
|
account=vat_payable_account, # Credit VAT Payable
|
|
amount=vat_amount,
|
|
tx_type="credit",
|
|
description="VAT Payable on Invoice",
|
|
)
|
|
|
|
invoice.make_payment(amount)
|
|
invoice.save()
|
|
|
|
|
|
def set_bill_payment(dealer, entity, bill, amount, payment_method):
|
|
vat_amount = 0
|
|
total_amount = 0
|
|
|
|
if bill.terms == "on_receipt":
|
|
for x in bill.get_itemtxs_data()[0].all():
|
|
vat_amount += models.Car.objects.get(
|
|
vin=x.item_model.name
|
|
).finances.cost_price * Decimal(x.quantity)
|
|
total_amount += Decimal(x.unit_cost) * Decimal(x.quantity)
|
|
|
|
# grand_total = total_amount - Decimal(vat_amount)
|
|
|
|
journal = JournalEntryModel.objects.create(
|
|
posted=False,
|
|
description=f"Payment for bill {bill.bill_number}",
|
|
ledger=bill.ledger,
|
|
locked=False,
|
|
origin="Payment",
|
|
)
|
|
|
|
cash_account = entity.get_default_coa_accounts().get(name="Cash", active=True)
|
|
|
|
account_payable = entity.get_default_coa_accounts().get(
|
|
name="Accounts Payable", active=True
|
|
)
|
|
|
|
# vat_payable_account = entity.get_default_coa_accounts().get(
|
|
# name="VAT Payable", active=True
|
|
# )
|
|
TransactionModel.objects.create(
|
|
journal_entry=journal,
|
|
account=cash_account, # Debit Cash
|
|
amount=amount, # Payment amount
|
|
tx_type="debit",
|
|
description="Payment Received",
|
|
)
|
|
|
|
TransactionModel.objects.create(
|
|
journal_entry=journal,
|
|
account=account_payable, # Credit Accounts Receivable
|
|
amount=amount, # Payment amount
|
|
tx_type="credit",
|
|
description="Payment Received",
|
|
)
|
|
|
|
# if vat_amount > 0:
|
|
# TransactionModel.objects.create(
|
|
# journal_entry=journal,
|
|
# account=vat_payable_account, # Credit VAT Payable
|
|
# amount=vat_amount,
|
|
# tx_type="credit",
|
|
# description="VAT Payable on bill",
|
|
# )
|
|
|
|
bill.make_payment(amount)
|
|
bill.save()
|