Skip to content

Commit

Permalink
generate orders and vouchers for batch order model
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanreveille committed Mar 10, 2025
1 parent 7efe3c8 commit 83d946d
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/backend/joanie/core/models/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -1899,3 +1899,66 @@ def submit_for_signature(self, user: User):
return backend_signature.get_signature_invitation_link(
self.owner.email, [self.contract.signature_backend_reference]
)

def generate_orders(self):
"""
Generate orders and vouchers once the convention has been signed by the company's
representative.
"""
if (
not self.contract.submitted_for_signature_on
or not self.contract.student_signed_on
):
message = "The convention has not been submitted nor signed by the company"
logger.error(
message,
extra={
"context": {
"batch_order": self.to_dict(),
"relation": self.relation.to_dict(),
}
},
)
raise ValidationError(message)

orders = [
Order.objects.create(
owner=None,
product_id=self.relation.product_id,
course_id=self.relation.course_id,
# organization_id=self.organization_id, # Should let the random for equity
# contract=self.contract #
) for _ in range(self.nb_seats)
]

self.orders.set(orders)
self.save()

return orders

def generate_vouchers(self):
"""Generate a voucher for each the prepared order"""
if not self.orders:
message = "You should first generate the orders before generating the vouchers"
logger.error(
message,
extra={
"context": {
"batch_order": self.to_dict(),
}
},
)
raise ValidationError(message)

# Create discount of 100 % because the batch order is paid in advance
discount = Discount.objects.create(rate=1)

vouchers = [
Voucher.objects.create(
discount=discount,
single_use=True,
)
for order in range(self.orders.all())
]

return vouchers

0 comments on commit 83d946d

Please sign in to comment.