save
This commit is contained in:
parent
b2c609afa6
commit
286265ac59
@ -2551,6 +2551,9 @@ class InvoicePreviewView(LoginRequiredMixin, DetailView):
|
||||
|
||||
def PaymentCreateView(request, pk=None):
|
||||
invoice = InvoiceModel.objects.filter(pk=pk).first()
|
||||
bill = BillModel.objects.filter(pk=pk).first()
|
||||
model = invoice if invoice else bill
|
||||
redirect_url = 'invoice_detail' if invoice else 'bill_detail'
|
||||
dealer = get_user_type(request)
|
||||
entity = dealer.entity
|
||||
form = forms.PaymentForm()
|
||||
@ -2559,92 +2562,35 @@ def PaymentCreateView(request, pk=None):
|
||||
if form.is_valid():
|
||||
amount = form.cleaned_data.get("amount")
|
||||
invoice = form.cleaned_data.get("invoice")
|
||||
bill = form.cleaned_data.get("bill")
|
||||
payment_method = form.cleaned_data.get("payment_method")
|
||||
model = invoice if invoice else bill
|
||||
if not model.is_approved():
|
||||
model.mark_as_approved(user_model=entity.admin)
|
||||
try:
|
||||
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=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()
|
||||
if invoice:
|
||||
set_invoice_payment(dealer,entity,invoice,amount,payment_method)
|
||||
elif bill:
|
||||
set_bill_payment(dealer,entity,bill,amount,payment_method)
|
||||
return redirect(redirect_url, pk=model.pk)
|
||||
except Exception as e:
|
||||
messages.error(request, f"Error creating payment: {str(e)}")
|
||||
else:
|
||||
messages.error(request, f"Invalid form data: {str(form.errors)}")
|
||||
return redirect("invoice_detail", pk=invoice.pk)
|
||||
messages.error(request, f"Invalid form data: {str(form.errors)}")
|
||||
# return redirect(redirect_url, pk=model.pk)
|
||||
form = forms.PaymentForm()
|
||||
form.initial["amount"] = invoice.amount_due
|
||||
|
||||
if invoice:
|
||||
form.initial["invoice"] = invoice
|
||||
form.initial["amount"] = model.amount_due - model.amount_paid
|
||||
if model:
|
||||
if isinstance(model, InvoiceModel):
|
||||
form.initial["invoice"] = model
|
||||
form.fields['bill'].widget = HiddenInput()
|
||||
elif isinstance(model, BillModel):
|
||||
form.initial["bill"] = model
|
||||
form.fields['invoice'].widget = HiddenInput()
|
||||
return render(
|
||||
request, "sales/payments/payment_form.html", {"invoice": invoice, "form": form}
|
||||
request, "sales/payments/payment_form.html", {"model": model, "form": form}
|
||||
)
|
||||
|
||||
|
||||
def PaymentListView(request):
|
||||
dealer = get_user_type(request)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user