Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Marking order as paid

Anna Siwikff18a9367717
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index a533773196..1b48c8fca7 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -14,10 +14,10 @@ from ...core.exceptions import InsufficientStock
from ...core.utils import ZERO_TAXED_MONEY
from ...discount.models import Voucher
from ...discount.utils import decrease_voucher_usage, increase_voucher_usage
-from ...order import OrderStatus
-from ...order.emails import send_note_confirmation, send_order_confirmation
+from ...order import CustomPaymentChoices, OrderStatus
+from ...order.emails import send_note_confirmation
from ...order.models import (
- Fulfillment, FulfillmentLine, Order, OrderLine, OrderNote)
+ Fulfillment, FulfillmentLine, Order, OrderLine, OrderNote, Payment)
from ...order.utils import (
add_variant_to_order, cancel_fulfillment, cancel_order,
change_order_line_quantity, merge_duplicates_into_order_line,
@@ -328,6 +328,14 @@ class RefundPaymentForm(ManagePaymentForm):
clean_error = pgettext_lazy('Payment form error',
'Only confirmed payments can be refunded')
+ def clean(self):
+ super().clean()
+ if self.payment.variant == CustomPaymentChoices.MANUAL:
+ raise forms.ValidationError(
+ pgettext_lazy(
+ 'Payment form error',
+ 'Manual payments can not be refunded'))
+
def refund(self):
return self.try_payment_action(self.payment.refund)
@@ -359,6 +367,37 @@ class ReleasePaymentForm(forms.Form):
return True
+class OrderMarkAsPaidForm(forms.Form):
+ """Mark order as manually paid."""
+
+ def __init__(self, *args, **kwargs):
+ self.order = kwargs.pop('order')
+ super().__init__(*args, **kwargs)
+
+ def clean(self):
+ super().clean()
+ if self.order.payments.exists():
+ raise forms.ValidationError(
+ pgettext_lazy(
+ 'Mark order as paid form error',
+ 'Orders with payments can not be manually marked as paid'))
+
+ def save(self):
+ defaults = {
+ 'total': self.order.total.gross.amount,
+ 'tax': self.order.total.tax.amount,
+ 'currency': self.order.total.currency,
+ 'delivery': self.order.shipping_price.gross.amount,
+ 'description': pgettext_lazy(
+ 'Payment description', 'Order %(order)s') % {
+ 'order': self.order},
+ 'captured_amount': self.order.total.gross.amount}
+ Payment.objects.get_or_create(
+ variant=CustomPaymentChoices.MANUAL,
+ status=PaymentStatus.CONFIRMED, order=self.order,
+ defaults=defaults)
+
+
class CancelOrderLineForm(forms.Form):
def __init__(self, *args, **kwargs):
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index e0b505048a..dc3f974c38 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -57,6 +57,8 @@ urlpatterns = [
views.fulfillment_packing_slips, name='fulfillment-packing-slips'),
url(r'^(?P<order_pk>\d+)/invoice/$',
views.order_invoice, name='order-invoice'),
+ url(r'^(?P<order_pk>\d+)/mark-as-paid/$',
+ views.mark_order_as_paid, name='order-mark-as-paid'),
url('^(?P<order_pk>\d+)/ajax/shipping-methods/$',
views.ajax_order_shipping_methods_list,
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 4fe68b24f1..7f78b8aed8 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -15,12 +15,11 @@ from payments import PaymentStatus
from ...core.exceptions import InsufficientStock
from ...core.utils import ZERO_TAXED_MONEY, get_paginator_items
-from ...order import OrderStatus
+from ...order import CustomPaymentChoices, OrderStatus
from ...order.emails import (
send_fulfillment_confirmation, send_fulfillment_update,
send_order_confirmation)
-from ...order.models import (
- Fulfillment, FulfillmentLine, Order, OrderLine, OrderNote)
+from ...order.models import Fulfillment, FulfillmentLine, Order, OrderNote
from ...order.utils import update_order_status
from ...product.models import StockLocation
from ...shipping.models import ShippingMethodCountry
@@ -32,9 +31,9 @@ from .forms import (
CapturePaymentForm, ChangeQuantityForm, ChangeStockForm,
CreateOrderFromDraftForm, FulfillmentForm, FulfillmentLineForm,
FulfillmentTrackingNumberForm, OrderCustomerForm, OrderEditDiscountForm,
- OrderEditVoucherForm, OrderNoteForm, OrderRemoveCustomerForm,
- OrderRemoveShippingForm, OrderRemoveVoucherForm, OrderShippingForm,
- RefundPaymentForm, ReleasePaymentForm)
+ OrderEditVoucherForm, OrderMarkAsPaidForm, OrderNoteForm,
+ OrderRemoveCustomerForm, OrderRemoveShippingForm, OrderRemoveVoucherForm,
+ OrderShippingForm, RefundPaymentForm, ReleasePaymentForm)
from .utils import (
create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url,
save_address_in_order)
@@ -126,7 +125,9 @@ def order_details(request, order_pk):
payment.status == PaymentStatus.PREAUTH and
order.status not in {OrderStatus.DRAFT, OrderStatus.CANCELED})
can_release = payment.status == PaymentStatus.PREAUTH
- can_refund = payment.status == PaymentStatus.CONFIRMED
+ can_refund = (
+ payment.status == PaymentStatus.CONFIRMED and
+ payment.variant != CustomPaymentChoices.MANUAL)
preauthorized = payment.get_total_price()
if payment.status == PaymentStatus.CONFIRMED:
captured = payment.get_captured_price()
@@ -301,8 +302,6 @@ def add_variant_to_order(request, order_pk):
'Dashboard message related to an order',
'Added %(quantity)d x %(variant)s') % msg_dict
messages.success(request, msg)
- if order.is_draft():
- order.history.create(content=msg, user=request.user)
except InsufficientStock:
msg = pgettext_lazy(
'Dashboard message related to an order',
@@ -505,7 +504,7 @@ def order_voucher_remove(request, order_pk):
@staff_member_required
-@permission_required('order.edit_order')
+@permission_required('order.view_order')
def order_invoice(request, order_pk):
orders = Order.objects.prefetch_related(
'user', 'shipping_address', 'billing_address', 'voucher')
@@ -518,6 +517,29 @@ def order_invoice(request, order_pk):
return response
+@staff_member_required
+@permission_required('order.edit_order')
+def mark_order_as_paid(request, order_pk):
+ order = get_object_or_404(Order, pk=order_pk)
+ status = 200
+ form = OrderMarkAsPaidForm(request.POST or None, order=order)
+ if form.is_valid():
+ msg = pgettext_lazy(
+ 'Dashboard message',
+ 'Order manually marked as paid')
+ with transaction.atomic():
+ form.save()
+ order.history.create(content=msg, user=request.user)
+ messages.success(request, msg)
+ return redirect('dashboard:order-details', order_pk=order.pk)
+ elif form.errors:
+ status = 400
+ ctx = {'form': form, 'order': order}
+ return TemplateResponse(
+ request, 'dashboard/order/modal/mark_as_paid.html', ctx,
+ status=status)
+
+
@staff_member_required
@permission_required('order.edit_order')
def fulfillment_packing_slips(request, order_pk, fulfillment_pk):
diff --git a/saleor/order/__init__.py b/saleor/order/__init__.py
index a2c2746d65..fef3c2559f 100644
--- a/saleor/order/__init__.py
+++ b/saleor/order/__init__.py
@@ -49,3 +49,10 @@ class FulfillmentStatus:
'Status for a fulfilled group of products in an order marked '
'as canceled',
'Canceled'))]
+
+
+class CustomPaymentChoices:
+ MANUAL = 'manual'
+
+ CHOICES = [
+ (MANUAL, pgettext_lazy('Custom payment choice type', 'Manual'))]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 5b7fcb1448..fe8d916050 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -156,6 +156,9 @@ class Order(models.Model):
def can_cancel(self):
return self.status not in {OrderStatus.CANCELED, OrderStatus.DRAFT}
+ def can_mark_as_paid(self):
+ return not self.payments.exists()
+
class OrderLine(models.Model):
order = models.ForeignKey(
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 4436341c5d..6f0baa65b1 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -158,8 +158,8 @@
</tbody>
</table>
</div>
- {% if perms.order.edit_order %}
- {% if can_capture or can_refund or can_release %}
+ {% if perms.order.edit_order and not order.is_draft %}
+ {% if can_capture or can_refund or can_release or order.can_mark_as_paid %}
<div class="data-table-action">
{% if can_capture %}
<a href="#base-modal" data-href="{% url 'dashboard:capture-payment' order_pk=order.pk payment_pk=payment.pk %}" class="btn-flat modal-trigger-custom waves-effect">
@@ -175,9 +175,16 @@
{% trans "Release" context "Order summary card action" %}
</a>
{% endif %}
- <a href="{% url 'dashboard:order-invoice' order_pk=order.pk %}" data-href="" class="btn-flat waves-effect" target="_blank">
- {% trans "Invoice" context "Order detail action" %}
- </a>
+ {% if can_capture or can_refund or can_release %}
+ <a href="{% url 'dashboard:order-invoice' order_pk=order.pk %}" data-href="" class="btn-flat waves-effect" target="_blank">
+ {% trans "Invoice" context "Order detail action" %}
+ </a>
+ {% endif %}
+ {% if order.can_mark_as_paid %}
+ <a href="#base-modal" data-href="{% url 'dashboard:order-mark-as-paid' order_pk=order.pk %}" class="btn-flat modal-trigger-custom waves-effect">
+ {% trans "Mark as paid" context "Order detail action" %}
+ </a>
+ {% endif %}
</div>
{% endif %}
{% endif %}
diff --git a/templates/dashboard/order/modal/mark_as_paid.html b/templates/dashboard/order/modal/mark_as_paid.html
new file mode 100644
index 0000000000..ea6d5299ff
--- /dev/null
+++ b/templates/dashboard/order/modal/mark_as_paid.html
@@ -0,0 +1,28 @@
+{% extends "dashboard/base_modal.html" %}
+
+{% load i18n %}
+{% load materializecss %}
+
+{% block action_url %}
+{% url 'dashboard:order-mark-as-paid' order_pk=order.pk %}
+{% endblock %}
+
+{% block title %}
+{% trans "Mark order as paid" context "Modal mark order as paid title" %}
+{% endblock %}
+
+{% block content %}
+ <div class="col s12">
+ {% blocktrans trimmed with order=order context "Modal mark order as paid text" %}
+ Are you sure you want mark order as paid?
+ {% endblocktrans %}
+ </div>
+{% endblock %}
+
+{% block cancel_action %}
+{% trans "Abort" context "Modal mark order as paid cancel action" %}
+{% endblock %}
+
+{% block primary_action %}
+{% trans "Mark as paid" context "Modal mark order as paid primary action" %}
+{% endblock %}
diff --git a/templates/dashboard/order/modal/remove_customer.html b/templates/dashboard/order/modal/remove_customer.html
deleted file mode 100644
index ba5657e6eb..0000000000
--- a/templates/dashboard/order/modal/remove_customer.html
+++ /dev/null
@@ -1,27 +0,0 @@
-{% extends "dashboard/base_modal.html" %}
-
-{% load i18n %}
-{% load materializecss %}
-
-{% block action_url %}
-{% url 'dashboard:order-customer-remove' order_pk=order.pk %}
-{% endblock %}
-
-{% block title %}
-{% trans "Remove customer from order" context "Modal remove customer from order title" %}
-{% endblock %}
-
-{% block content %}
- <div class="col s12">
- {% blocktrans trimmed context "Modal remove customer from order text" %}
- Are you sure you want remove customer from order?
- {% endblocktrans %}
- {% if form.remove_addresses %}
- {{ form.remove_addresses|materializecss }}
- {% endif %}
- </div>
-{% endblock %}
-
-{% block primary_action %}
-{% trans "Remove" context "Modal remove customer from order primary action" %}
-{% endblock %}
diff --git a/templates/dashboard/order/pdf/invoice.html b/templates/dashboard/order/pdf/invoice.html
index 025e3d4f54..a54b30e78a 100644
--- a/templates/dashboard/order/pdf/invoice.html
+++ b/templates/dashboard/order/pdf/invoice.html
@@ -27,9 +27,11 @@
{% trans "Shipping Details" context "Invoice table header" %}
</th>
{% endif %}
- <th align="left" {% if order.shipping_address %} width="50%"{% endif %}>
- {% trans "Billing Details" context "Invoice table header" %}
- </th>
+ {% if order.billing_address %}
+ <th align="left" {% if order.shipping_address %} width="50%"{% endif %}>
+ {% trans "Billing Details" context "Invoice table header" %}
+ </th>
+ {% endif %}
</tr>
</thead>
<tbody>
@@ -44,14 +46,16 @@
{% endwith %}
</td>
{% endif %}
- <td valign="top">
- {% with order.billing_address as address %}
- <h2>
- {% trans "Billing address" context "Invoice billing address" %}
- </h2>
- {% include 'dashboard/includes/_address.html' with address=address only %}
- {% endwith %}
- </td>
+ {% if order.billing_address %}
+ <td valign="top">
+ {% with order.billing_address as address %}
+ <h2>
+ {% trans "Billing address" context "Invoice billing address" %}
+ </h2>
+ {% include 'dashboard/includes/_address.html' with address=address only %}
+ {% endwith %}
+ </td>
+ {% endif %}
</tr>
</tbody>
</table>
@@ -100,16 +104,6 @@
</td>
</tr>
{% endfor %}
- {% if order.shipping_price.gross or order.is_shipping_required %}
- <tr>
- <td>
- {% trans "Shipping" context "Shipment order table item column data" %}
- </td>
- <td colspan="3">
- {{ order.shipping_method_name }}
- </td>
- </tr>
- {% endif %}
</tbody>
</table>
{% endif %}
@@ -126,7 +120,7 @@
</tr>
<tr>
<td>
- {% trans "Shipping total" context "Order total shipping price header" %}
+ {% trans "Shipping" context "Order total shipping price header" %} {% if order.shipping_method_name %}({{ order.shipping_method_name }}){% endif %}
</td>
<td class="right-align">
{{ order.shipping_price.gross|amount:'html' }}