Skip to content

Commit

Permalink
[14.0][ADD] shopinvader_payment_gift_card module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kev-Roche committed Apr 19, 2022
1 parent 7059f93 commit 4e165c1
Show file tree
Hide file tree
Showing 16 changed files with 741 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/shopinvader_payment_gift_card/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
2 changes: 2 additions & 0 deletions shopinvader_payment_gift_card/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import services
25 changes: 25 additions & 0 deletions shopinvader_payment_gift_card/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "shopinvader_payment_gift_card",
"summary": "Shopinvader Services for Gift Cards and Payments with",
"version": "14.0.1.0.0",
"category": "e-commerce",
"website": "https://github.com/shopinvader/odoo-shopinvader-payment",
"author": "Akretion, Odoo Community Association (OCA)",
"maintainers": ["Kev-Roche"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"shopinvader_payment",
"shopinvader_gift_card",
"account_payment_gift_card",
"base_rest",
"base_jsonify",
"base_rest_datamodel",
"shopinvader_payment_manual",
],
}
83 changes: 83 additions & 0 deletions shopinvader_payment_gift_card/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * shopinvader_payment_gift_card
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-04-16 11:27+0000\n"
"PO-Revision-Date: 2022-04-16 13:28+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: \n"
"X-Generator: Poedit 3.0.1\n"

#. module: shopinvader_payment_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_gift_card__display_name
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_payment_transaction__display_name
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order__display_name
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order_line__display_name
msgid "Display Name"
msgstr "Nom affiché"

#. module: shopinvader_payment_gift_card
#: model:ir.model,name:shopinvader_payment_gift_card.model_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order_line__gift_card_id
msgid "Gift Card"
msgstr "Carte Cadeau"

#. module: shopinvader_payment_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_gift_card__id
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_payment_transaction__id
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order__id
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order_line__id
msgid "ID"
msgstr ""

#. module: shopinvader_payment_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_gift_card____last_update
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_payment_transaction____last_update
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order____last_update
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order_line____last_update
msgid "Last Modified on"
msgstr "Dernière modification le"

#. module: shopinvader_payment_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order__gift_card_line_ids
msgid "List of Gift Card uses"
msgstr "Liste des Utilisations Carte Cadeau"

#. module: shopinvader_payment_gift_card
#: model:ir.model,name:shopinvader_payment_gift_card.model_payment_transaction
msgid "Payment Transaction"
msgstr "Transaction"

#. module: shopinvader_payment_gift_card
#: model:ir.model.fields,field_description:shopinvader_payment_gift_card.field_sale_order__remain_amount
msgid "Remaining Amount"
msgstr "Montant Restant"

#. module: shopinvader_payment_gift_card
#: model:ir.model,name:shopinvader_payment_gift_card.model_sale_order
msgid "Sales Order"
msgstr "Bons de commande"

#. module: shopinvader_payment_gift_card
#: model:ir.model,name:shopinvader_payment_gift_card.model_sale_order_line
msgid "Sales Order Line"
msgstr "Lignes de bon de commande"

#. module: shopinvader_payment_gift_card
#: code:addons/shopinvader_payment_gift_card/services/cart.py:0
#, python-format
msgid ""
"The Gift Card amount is higher than the current cart amount, impossible to "
"use the gift card in that case."
msgstr ""
"Le montant de la carte-cadeau est supérieur au montant actuel du panier, "
"impossible d’utiliser la carte-cadeau dans ce cas."
3 changes: 3 additions & 0 deletions shopinvader_payment_gift_card/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import gift_card
from . import sale
from . import payment_transaction
16 changes: 16 additions & 0 deletions shopinvader_payment_gift_card/models/gift_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2022 Akretion (<http://www.akretion.com>).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models


class GiftCard(models.Model):
_inherit = "gift.card"

@api.depends("start_date", "end_date", "available_amount", "duration")
def _compute_state(self):
for card in self:
if card.state == "soldout" and card.available_amount > 0:
card.state = "active"
return super()._compute_state()
48 changes: 48 additions & 0 deletions shopinvader_payment_gift_card/models/payment_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (C) 2022 Akretion (<http://www.akretion.com>).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class PaymentTransaction(models.Model):
_inherit = "payment.transaction"

def _check_amount_and_confirm_order(self):
self.ensure_one()
for order in self.sale_order_ids.filtered(
lambda so: so.state in ("draft", "sent")
):
if (
self.amount == order.remain_amount
and order.amount_total
== order.remain_amount
+ sum(order.gift_card_line_ids.mapped("amount_used"))
):
order.with_context(send_email=True).action_confirm()
else:
return super()._check_amount_and_confirm_order()

def _post_process_after_done(self):
res = super()._post_process_after_done()
if self.sale_order_ids.gift_card_line_ids:
for gift_card_line in self.sale_order_ids.gift_card_line_ids:
if not gift_card_line.transaction_id:
self._add_gift_card_transaction(
amount=gift_card_line.amount_used, line=gift_card_line
)
return res

def create_gift_card_payment(self, sale):
gift_card_acquirer = self.env.ref(
"account_payment_gift_card.payment_acquirer_gift_card"
)
for line in sale.gift_card_line_ids:
vals = {
"acquirer_id": gift_card_acquirer.id,
"amount": line.amount_used,
}
transaction = sale._create_payment_transaction(vals)
transaction._create_payment()
transaction._reconcile_after_transaction_done()
line.transaction_id = transaction
41 changes: 41 additions & 0 deletions shopinvader_payment_gift_card/models/sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (C) 2022 Akretion (<http://www.akretion.com>).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class SaleOrder(models.Model):
_inherit = "sale.order"

gift_card_line_ids = fields.One2many(
"gift.card.line",
inverse_name="sale_order_id",
string="List of Gift Card uses",
readonly=True,
)

remain_amount = fields.Float("Remaining Amount", compute="_compute_remain_amount")

@api.depends("gift_card_line_ids", "gift_card_line_ids.amount_used", "amount_total")
def _compute_remain_amount(self):
for rec in self:
if rec.gift_card_line_ids:
gift_card_amount_total = sum(
line.amount_used for line in rec.gift_card_line_ids
)
rec.remain_amount = rec.amount_total - gift_card_amount_total
else:
rec.remain_amount = rec.amount_total

def _invader_prepare_payment_transaction_data(self, acquirer_id):
vals = super()._invader_prepare_payment_transaction_data(acquirer_id)
if self.gift_card_line_ids:
vals["amount"] = self.remain_amount
return vals


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

gift_card_id = fields.Many2one("gift.card")
2 changes: 2 additions & 0 deletions shopinvader_payment_gift_card/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Kévin Roche <[email protected]>
* Pierrick Brun <[email protected]>
1 change: 1 addition & 0 deletions shopinvader_payment_gift_card/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Services for Gift Cards Transactions and Payments
2 changes: 2 additions & 0 deletions shopinvader_payment_gift_card/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import cart
from . import payment_gift_card
133 changes: 133 additions & 0 deletions shopinvader_payment_gift_card/services/cart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Copyright (C) 2022 Akretion (<http://www.akretion.com>).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from datetime import date

from odoo import _
from odoo.exceptions import UserError

from odoo.addons.base_rest import restapi
from odoo.addons.component.core import Component
from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel
from odoo.addons.shopinvader_gift_card.services.gift_card import (
JSONIFY_GIFT_CARD,
JSONIFY_GIFT_CARD_LINE,
)


def to_date(value):
return date.fromisoformat(value)


class GiftCardCodeInput(Datamodel):
_name = "gift.card.code.input"
code = fields.String()


class GiftCardAmountInput(Datamodel):
_name = "gift.card.amount.input"
card = fields.Integer()
code = fields.String()
gift_card_amount = fields.Float()


class GiftCardLineInput(Datamodel):
_name = "gift.card.line.input"
line_id = fields.Integer()


class CartService(Component):
_inherit = "shopinvader.cart.service"

@restapi.method(
routes=[(["/get_gift_card_from_code"], "GET")],
input_param=restapi.Datamodel("gift.card.code.input"),
)
def get_gift_card_from_code(self, params):
params = params.dump()
code = params.get("code", " no_code ")
gift_card = self.env["gift.card"].search([("code", "=", code)])
if gift_card:
self.env["gift.card"].check_gift_card_code(gift_card.code)
if gift_card.state == "active":
return gift_card.jsonify(self._parser_giftcard())

def _parser_giftcard(self):
return JSONIFY_GIFT_CARD

def _parser_giftcard_line(self):
return JSONIFY_GIFT_CARD_LINE

@restapi.method(
routes=[(["/gift_card_amount_validation"], "GET")],
input_param=restapi.Datamodel("gift.card.amount.input"),
)
def gift_card_amount_validation(self, params):
params = params.dump()
card = params.get("card", False)
code = params.get("code", False)
gift_card_amount = params.get("gift_card_amount")
gift_card = self.env["payment.transaction"]._get_and_check_gift_card(
card=card, code=code
)
gift_card_amount = self._check_gift_card_line_amount(card, gift_card_amount)
line = self.env["payment.transaction"]._create_gift_card_line(
amount=gift_card_amount, card=gift_card, code=code
)
cart = self._get()
line.beneficiary_id = cart.partner_id
cart.write({"gift_card_line_ids": [(4, line.id)]})
return line.jsonify(self._parser_giftcard_line())

def _check_gift_card_line_amount(self, card, gift_card_amount):
gift_card = self.env["gift.card"].browse(card)
cart = self._get()
if cart.remain_amount < gift_card_amount and gift_card.is_divisible:
return cart.remain_amount
elif not gift_card.is_divisible:
if cart.remain_amount < gift_card.initial_amount:
raise UserError(
_(
"The Gift Card amount is higher than the current"
" cart amount, impossible to use the gift card "
"in that case."
)
)
else:
return gift_card.initial_amount
else:
return gift_card_amount

@restapi.method(
routes=[(["/unlink_gift_card_line"], "GET")],
input_param=restapi.Datamodel("gift.card.line.input"),
)
def unlink_gift_card_line(self, params):
params = params.dump()
line = params.get("line_id")
cart = self._get()
cart.write({"gift_card_line_ids": [(2, line)]})
return self._to_json(cart)

def _convert_one_sale(self, sale):
res = super()._convert_one_sale(sale)
res.update(
{
"remain_amount": sale.remain_amount,
"gift_card_line_ids": self._convert_gift_card_line(
sale.gift_card_line_ids
),
}
)
return res

def _parser_gift_card_line(self):
return ["id", "name", "amount_used", "validation_mode", "beneficiary_id"]

def _convert_gift_card_line(self, gift_card_lines):
return {
"items": gift_card_lines.jsonify(self._parser_gift_card_line()),
"count": len(gift_card_lines),
}
Loading

0 comments on commit 4e165c1

Please sign in to comment.