Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions press/press/doctype/invoice/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,28 +186,29 @@ def get_doc(self, doc):
currency_symbol = "₹" if currency == "INR" else "$"

for item in doc["items"]:
if item.document_type in ("Server", "Database Server"):
is_primary = frappe.get_value(item.document_type, item.document_name, "is_primary")
item.document_name = frappe.get_value(item.document_type, item.document_name, "title")
if server_plan := frappe.get_value("Server Plan", item.plan, price_field):
if not is_primary and item.document_type == "Server":
item.plan = (
f"{currency_symbol}{calculate_secondary_server_price(self.team, item.plan)}/hour"
)
else:
item.plan = f"{currency_symbol}{server_plan}/mo"
elif server_plan := frappe.get_value("Server Storage Plan", item.plan, price_field):
item.plan = f"Storage Add-on {currency_symbol}{server_plan}/GB"

elif item.document_type == "Marketplace App":
item.document_name = frappe.get_value(item.document_type, item.document_name, "title")
item.plan = (
f"{currency_symbol}{frappe.get_value('Marketplace App Plan', item.plan, price_field)}"
)
elif item.document_type == "Site":
hostname = frappe.get_value(item.document_type, item.document_name, "host_name")
if hostname:
item.document_name = hostname
self._enrich_invoice_item(item, price_field, currency_symbol)

def _enrich_invoice_item(self, item, price_field, currency_symbol):
if item.document_type in ("Server", "Database Server"):
is_primary = frappe.get_value(item.document_type, item.document_name, "is_primary")
item.document_name = frappe.get_value(item.document_type, item.document_name, "title")
if server_plan := frappe.get_value("Server Plan", item.plan, price_field):
if not is_primary and item.document_type == "Server":
item.plan = (
f"{currency_symbol}{calculate_secondary_server_price(self.team, item.plan)}/hour"
)
else:
item.plan = f"{currency_symbol}{server_plan}/mo"
elif server_plan := frappe.get_value("Server Storage Plan", item.plan, price_field):
item.plan = f"Storage Add-on {currency_symbol}{server_plan}/GB"

elif item.document_type == "Marketplace App":
item.document_name = frappe.get_value(item.document_type, item.document_name, "title")
item.plan = f"{currency_symbol}{frappe.get_value('Marketplace App Plan', item.plan, price_field)}"
elif item.document_type == "Site":
hostname = frappe.get_value(item.document_type, item.document_name, "host_name")
if hostname:
item.document_name = hostname

@dashboard_whitelist()
def stripe_payment_url(self):
Expand Down Expand Up @@ -291,21 +292,22 @@ def finalize_invoice(self): # noqa: C901

if self.status == "Paid" and self.stripe_invoice_id and self.amount_paid == 0:
stripe = get_stripe()
invoice = stripe.Invoice.retrieve(self.stripe_invoice_id)
payment_intent = stripe.PaymentIntent.retrieve(invoice.payment_intent)
if payment_intent.status == "processing":
# mark the fc invoice as Paid
# if the payment intent is processing, it means the invoice cannot be voided yet
# wait for invoice to be updated and then mark it as void if payment failed
# or issue a refund if succeeded
self.save() # status is already Paid, so no need to set again
else:
self.change_stripe_invoice_status("Void")
self.add_comment(
text=(
f"Stripe Invoice {self.stripe_invoice_id} voided because payment is done via credits."
stripe_invoice = stripe.Invoice.retrieve(self.stripe_invoice_id)
if stripe_invoice.status == "open":
payment_intent = stripe.PaymentIntent.retrieve(stripe_invoice.payment_intent)
if payment_intent.status == "processing":
# mark the fc invoice as Paid
# if the payment intent is processing, it means the invoice cannot be voided yet
# wait for invoice to be updated and then mark it as void if payment failed
# or issue a refund if succeeded
self.save() # status is already Paid, so no need to set again
else:
self.change_stripe_invoice_status("Void")
self.add_comment(
text=(
f"Stripe Invoice {self.stripe_invoice_id} voided because payment is done via credits."
)
)
)

self.save()

Expand Down
Loading