EnCoach Odoo 19 custom modules
Full backend implementation with custom Odoo modules: - encoach_api: Core API, user management, JWT auth - encoach_exam: Exam generation (reading, writing, listening, speaking) - encoach_evaluate: AI-powered evaluation (writing, speaking) - encoach_training: Training tips and walkthrough - encoach_storage: File storage management - encoach_payment: Stripe, PayPal, Paymob integration - encoach_mail: Email notifications Made-with: Cursor
This commit is contained in:
52
encoach_subscription/services/subscription_helper.py
Normal file
52
encoach_subscription/services/subscription_helper.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def extend_subscription(env, user_id, package_id, provider, transaction_id, amount, currency):
|
||||
user = env["res.users"].sudo().browse(user_id)
|
||||
if not user.exists():
|
||||
_logger.error("extend_subscription: user %s not found", user_id)
|
||||
return
|
||||
|
||||
package = env["encoach.package"].sudo().browse(package_id)
|
||||
if not package.exists():
|
||||
_logger.error("extend_subscription: package %s not found", package_id)
|
||||
return
|
||||
|
||||
now = datetime.utcnow()
|
||||
current_exp = user.subscription_expiration
|
||||
if current_exp and current_exp > now:
|
||||
base = current_exp
|
||||
else:
|
||||
base = now
|
||||
new_exp = base + timedelta(days=package.duration_days or 30)
|
||||
user.sudo().write({"subscription_expiration": new_exp})
|
||||
|
||||
env["encoach.subscription.payment"].sudo().create({
|
||||
"user_id": user_id,
|
||||
"package_id": package_id,
|
||||
"provider": provider,
|
||||
"status": "completed",
|
||||
"amount": amount,
|
||||
"currency": currency,
|
||||
"transaction_id": transaction_id,
|
||||
})
|
||||
|
||||
if user.encoach_type in ("corporate", "mastercorporate"):
|
||||
rels = env["encoach.user.entity.rel"].sudo().search([
|
||||
("user_id", "=", user_id),
|
||||
])
|
||||
for rel in rels:
|
||||
members = env["encoach.user.entity.rel"].sudo().search([
|
||||
("entity_id", "=", rel.entity_id.id),
|
||||
("user_id", "!=", user_id),
|
||||
])
|
||||
for m in members:
|
||||
m.user_id.sudo().write({"subscription_expiration": new_exp})
|
||||
|
||||
_logger.info(
|
||||
"Subscription extended: user=%s package=%s provider=%s until=%s",
|
||||
user_id, package_id, provider, new_exp,
|
||||
)
|
||||
Reference in New Issue
Block a user