Skip to content

Commit

Permalink
T1798 ADD group_visit trip payment
Browse files Browse the repository at this point in the history
  • Loading branch information
ecino committed Sep 5, 2024
1 parent 6060d01 commit 6943a76
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 19 deletions.
3 changes: 2 additions & 1 deletion website_event_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"name": "Compassion Events Website",
"summary": "Public website pages for Compassion Events with registration",
"version": "14.0.1.2.0",
"version": "14.0.1.3.0",
"development_status": "Production/Stable",
"category": "Marketing/Events",
"website": "https://github.com/CompassionCH/compassion-website",
Expand All @@ -51,6 +51,7 @@
"data/event_message_subtype.xml",
"data/event_type.xml",
"data/form_data.xml",
"data/product_template.xml",
"views/event_compassion_open_wizard.xml",
"views/event_compassion_view.xml",
"views/event_event_view.xml",
Expand Down
57 changes: 57 additions & 0 deletions website_event_compassion/data/product_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<!--
Resource: product.template
-->
<!-- Event registration products -->
<data noupdate="1">
<record id="product_template_single_room" model="product.template">
<field name="name">Single room extra fee</field>
<field name="type">service</field>
<field name="categ_id" ref="product.product_category_all" />
<field name="list_price">10</field>
<field name="description" />
<field
name="property_account_income_id"
search="[('code','=','1040')]"
/>
<field
name="property_account_expense_id"
search="[('code','=','1040')]"
/>
<field name="event_ok" eval="True" />
</record>
<record id="product_template_flight" model="product.template">
<field name="name">Flight</field>
<field name="type">service</field>
<field name="categ_id" ref="product.product_category_all" />
<field name="list_price">1000</field>
<field name="description" />
<field
name="property_account_income_id"
search="[('code','=','1040')]"
/>
<field
name="property_account_expense_id"
search="[('code','=','1040')]"
/>
<field name="event_ok" eval="True" />
</record>
<record id="product_template_trip_price" model="product.template">
<field name="name">Standard trip price</field>
<field name="type">service</field>
<field name="categ_id" ref="product.product_category_all" />
<field name="list_price">1000</field>
<field name="description" />
<field
name="property_account_income_id"
search="[('code','=','1040')]"
/>
<field
name="property_account_expense_id"
search="[('code','=','1040')]"
/>
<field name="event_ok" eval="True" />
</record>
</data>
</odoo>
126 changes: 108 additions & 18 deletions website_event_compassion/models/event_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class EventRegistration(models.Model):
related="sale_order_line_id.invoice_lines.move_id",
)
down_payment_link = fields.Char(compute="_compute_down_payment_link")
trip_invoice_id = fields.Many2one(
"account.move",
"Trip invoice",
)
payment_link = fields.Char(compute="_compute_payment_link")
single_room = fields.Boolean(help="The participant wants a single room")
# Change readonly attribute so that form creation is possible
event_id = fields.Many2one(
readonly=False,
Expand Down Expand Up @@ -205,32 +211,52 @@ def _compute_is_published(self):
for registration in self:
registration.is_published = registration.state in ("open", "done")

def _create_payment_link(self, move, description):
payment_link = (
request.env["payment.link.wizard"]
.sudo()
.create(
{
"res_id": move.id,
"res_model": "account.move",
"amount": move.amount_residual,
"currency_id": move.currency_id.id,
"partner_id": move.partner_id.id,
"amount_max": move.amount_residual,
"description": description,
}
)
)
return payment_link.link

def _compute_down_payment_link(self):
for registration in self:
if registration.down_payment_id:
move = registration.down_payment_id
payment_link = (
request.env["payment.link.wizard"]
.sudo()
.create(
{
"res_id": move.id,
"res_model": "account.move",
"amount": move.amount_residual,
"currency_id": move.currency_id.id,
"partner_id": move.partner_id.id,
"amount_max": move.amount_residual,
"description": _("Down payment for %s")
% registration.compassion_event_id.name,
}
)
description = (
_("Down payment for %s") % registration.compassion_event_id.name
)
registration.down_payment_link = (
payment_link.link + f"&return_url=/my/events/{registration.id}"
self._create_payment_link(move, description)
+ f"&return_url=/my/events/{registration.id}"
)
else:
registration.down_payment_link = False

def _compute_payment_link(self):
for registration in self:
if registration.trip_invoice_id:
move = registration.trip_invoice_id
description = (
_("Payment for %s") % registration.compassion_event_id.name
)
registration.payment_link = (
self._create_payment_link(move, description)
+ f"&return_url=/my/events/{registration.id}"
)
else:
registration.payment_link = False

def _default_website_meta(self):
default_meta = super()._default_website_meta()
company = request.website.company_id.sudo()
Expand Down Expand Up @@ -546,8 +572,11 @@ def show_invoice(self):
}

def create_down_payment(self):
down_payment_product = self.env.ref("event_sale.product_product_event")
for registration in self:
ticket = registration.event_id.event_ticket_ids[:1]
ticket = registration.event_id.event_ticket_ids.filtered(
lambda t: t.product_id == down_payment_product
)
if ticket and not self.down_payment_id:
order = self.env["sale.order"].create(
{
Expand All @@ -557,7 +586,7 @@ def create_down_payment(self):
0,
0,
{
"product_id": ticket.product_id.id,
"product_id": down_payment_product.id,
"name": ticket.name,
"price_unit": ticket.price,
"product_uom_qty": 1,
Expand All @@ -578,6 +607,67 @@ def create_down_payment(self):
order.action_confirm()
return True

def create_trip_invoice(self):
travel_cost = self.env.ref(
"website_event_compassion.product_template_trip_price"
)
single_room_cost = self.env.ref(
"website_event_compassion.product_template_single_room"
)
sales_journal = self.env["account.journal"].search(
[("type", "=", "sale")], limit=1
)
for registration in self:
travel_ticket = registration.event_id.event_ticket_ids.filtered(
lambda t, template=travel_cost: t.product_id.product_tmpl_id == template
)
if len(travel_ticket) > 1:
# Take the price set at the date of registration
travel_ticket = travel_ticket.filtered(
lambda t, reg=registration: t.start_sale_date
<= reg.create_date
<= t.end_sale_date
)[:1]
room_ticket = registration.event_id.event_ticket_ids.filtered(
lambda t: t.product_id.product_tmpl_id == single_room_cost
)
if travel_ticket and not registration.trip_invoice_id:
invoice_lines = [
(
0,
0,
{
"product_id": travel_ticket.product_id.id,
"name": travel_ticket.name,
"price_unit": travel_ticket.price,
"quantity": 1,
},
),
]
if registration.single_room and room_ticket:
invoice_lines.append(
(
0,
0,
{
"product_id": room_ticket.product_id.id,
"name": room_ticket.name,
"price_unit": room_ticket.price,
"quantity": 1,
},
),
)
registration.trip_invoice_id = self.env["account.move"].create(
{
"partner_id": registration.partner_id.id,
"move_type": "out_invoice",
"journal_id": sales_journal.id,
"invoice_date": fields.Date.today(),
"invoice_line_ids": invoice_lines,
}
)
return True

##########################################################################
# STAGE TRANSITION METHODS #
##########################################################################
Expand Down
11 changes: 11 additions & 0 deletions website_event_compassion/views/event_registration_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
string="Create Down Payment"
type="object"
attrs="{ 'invisible': [('down_payment_id', '!=', False)] }"
/>
<button
name="create_trip_invoice"
string="Create Trip Invoice"
type="object"
attrs="{ 'invisible': [('trip_invoice_id', '!=', False)] }"
/>
</button>

Expand Down Expand Up @@ -91,6 +97,11 @@
<field name="sale_order_line_id" position="after">
<field name="down_payment_id" groups="base.group_no_one" />
<field name="down_payment_link" groups="base.group_no_one" />
<field name="trip_invoice_id" groups="base.group_no_one" />
<field name="payment_link" groups="base.group_no_one" />
</field>
<field name="event_ticket_id" position="after">
<field name="single_room" />
</field>
<xpath expr="//sheet/group">
<field name="fundraising" invisible="1" />
Expand Down

0 comments on commit 6943a76

Please sign in to comment.