Skip to content

Commit

Permalink
feat(partner): Add partner utility APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrak98 committed Jan 6, 2025
1 parent 6c065f7 commit 01007a5
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion press/api/partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import frappe
from frappe.core.utils import find
from frappe.utils import flt
from frappe.utils.data import today
from frappe.utils.data import add_days, add_months, get_first_day, get_last_day, today

Check warning on line 4 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L4

Added line #L4 was not covered by tests

from press.utils import get_current_team

Expand Down Expand Up @@ -141,6 +141,101 @@ def get_partner_contribution_list(partner_email):
return invoices


@frappe.whitelist()
def get_total_partner_contribution(partner_email):
return

Check warning on line 146 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L144-L146

Added lines #L144 - L146 were not covered by tests


@frappe.whitelist()
def get_current_month_partner_contribution(partner_email):
partner_currency = frappe.db.get_value(

Check warning on line 151 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L149-L151

Added lines #L149 - L151 were not covered by tests
"Team", {"erpnext_partner": 1, "partner_email": partner_email}, "currency"
)
month_end = frappe.utils.get_last_day(today())

Check warning on line 154 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L154

Added line #L154 was not covered by tests

invoice = frappe.qb.DocType("Invoice")
query = (

Check warning on line 157 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L156-L157

Added lines #L156 - L157 were not covered by tests
frappe.qb.from_(invoice)
.select(invoice.total, invoice.currency, invoice.total_before_discount)
.where(
(invoice.partner_email == partner_email)
& (invoice.due_date == month_end)
& (invoice.type == "Subscription")
& (invoice.status == "Draft")
)
)
invoices = query.run(as_dict=True)
total = 0
for d in invoices:
if partner_currency != d.currency:
if partner_currency == "USD":
total += flt(d.total_before_discount / 83, 2)

Check warning on line 172 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L167-L172

Added lines #L167 - L172 were not covered by tests
else:
total += flt(d.total_before_discount * 83, 2)

Check warning on line 174 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L174

Added line #L174 was not covered by tests
else:
total += d.total_before_discount

Check warning on line 176 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L176

Added line #L176 was not covered by tests

return total

Check warning on line 178 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L178

Added line #L178 was not covered by tests


@frappe.whitelist()
def get_prev_month_partner_contribution(partner_email):
partner_currency = frappe.db.get_value(

Check warning on line 183 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L181-L183

Added lines #L181 - L183 were not covered by tests
"Team", {"erpnext_partner": 1, "partner_email": partner_email}, "currency"
)
first_day = get_first_day(today())
two_weeks = add_days(first_day, 14) # 15th day of the month
last_month_end = get_last_day(add_months(today(), -1))

Check warning on line 188 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L186-L188

Added lines #L186 - L188 were not covered by tests

invoice = frappe.qb.DocType("Invoice")
query = (

Check warning on line 191 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L190-L191

Added lines #L190 - L191 were not covered by tests
frappe.qb.from_(invoice)
.select(invoice.total, invoice.currency, invoice.total_before_discount)
.where(
(invoice.partner_email == partner_email)
& (invoice.due_date == last_month_end)
& (invoice.type == "Subscription")
)
)

if today() >= first_day and frappe.utils.getdate() <= frappe.utils.getdate(two_weeks):

Check warning on line 201 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L201

Added line #L201 was not covered by tests
# till 15th of the current month unpaid invoices can also be counted in contribution
query = query.where((invoice.status).isin(["Unpaid", "Paid"]))

Check warning on line 203 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L203

Added line #L203 was not covered by tests
else:
query = query.where(invoice.status == "Paid")

Check warning on line 205 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L205

Added line #L205 was not covered by tests

invoices = query.run(as_dict=True)

Check warning on line 207 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L207

Added line #L207 was not covered by tests

total = 0
for d in invoices:
total = 0
if partner_currency != d.currency:
if partner_currency == "USD":
total += flt(d.total / 83, 2)

Check warning on line 214 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L209-L214

Added lines #L209 - L214 were not covered by tests
else:
total += flt(d.total * 83, 2)

Check warning on line 216 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L216

Added line #L216 was not covered by tests
else:
total += d.total
return total

Check warning on line 219 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L218-L219

Added lines #L218 - L219 were not covered by tests


@frappe.whitelist()
def calculate_partner_tier(contribution, currency):
partner_tier = frappe.qb.DocType("Partner Teir")
query = frappe.qb.from_(partner_tier).select(partner_tier.name)
if currency == "INR":
query = query.where(partner_tier.target_in_inr <= contribution).orderby(

Check warning on line 227 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L222-L227

Added lines #L222 - L227 were not covered by tests
partner_tier.target_in_inr, order=frappe.qb.desc
)
else:
query = query.where(partner_tier.target_in_usd <= contribution).orderby(

Check warning on line 231 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L231

Added line #L231 was not covered by tests
partner_tier.target_in_usd, order=frappe.qb.desc
)

tier = query.run(as_dict=True)
return tier[0]

Check warning on line 236 in press/api/partner.py

View check run for this annotation

Codecov / codecov/patch

press/api/partner.py#L235-L236

Added lines #L235 - L236 were not covered by tests


@frappe.whitelist()
def add_partner(referral_code: str):
team = get_current_team(get_doc=True)
Expand Down

0 comments on commit 01007a5

Please sign in to comment.