Skip to content

Commit

Permalink
Add function to generate billing
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeconnolly committed Aug 11, 2016
1 parent 79dfa27 commit d7e8c80
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 2 deletions.
40 changes: 40 additions & 0 deletions django/santropolFeast/billing/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-08-10 17:02
from __future__ import unicode_literals

import annoying.fields
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('order', '0006_auto_20160803_2217'),
('member', '0011_client_delivery_note'),
]

operations = [
migrations.CreateModel(
name='Billing',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('total_amount', models.DecimalField(decimal_places=2, max_digits=6, verbose_name='total_amount')),
('billing_month', models.IntegerField()),
('billing_year', models.IntegerField()),
('generation_date', models.DateTimeField(auto_now=True)),
('detail', annoying.fields.JSONField()),
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='member.Client')),
],
),
migrations.CreateModel(
name='OrderBilling',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('billing_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='client_billing', to='billing.Billing')),
('order_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='client_order', to='order.Order')),
],
),
]
27 changes: 27 additions & 0 deletions django/santropolFeast/billing/migrations/0002_billingcycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-08-10 18:31
from __future__ import unicode_literals

import annoying.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('billing', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='BillingCycle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('total_order', models.IntegerField()),
('detail', annoying.fields.JSONField()),
('billing_year', models.IntegerField()),
('billing_month', models.IntegerField()),
('amount_total', models.DecimalField(decimal_places=2, max_digits=6)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-08-10 19:57
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('billing', '0002_billingcycle'),
]

operations = [
migrations.RenameModel(
old_name='BillingCycle',
new_name='BillingSummary',
),
]
74 changes: 74 additions & 0 deletions django/santropolFeast/billing/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
from django.db import models
from member.models import Client
from order.models import Order
from annoying.fields import JSONField
from django.utils.translation import ugettext_lazy as _


# Create your models here.

class OrderBilling(models.Model):
order_id = models.ForeignKey(
"order.Order",
related_name='client_order'
)
billing_id = models.ForeignKey(
"billing.Billing",
related_name='client_billing'
)


class BillingManager(models.Manager):

def get_all_billing_client(self, client):
pass


class Billing(models.Model):
client = models.ForeignKey(
"member.Client",
)

total_amount = models.DecimalField(
verbose_name=_('total_amount'),
max_digits=6,
decimal_places=2
)

# Month start with january is 1
billing_month = models.IntegerField()

billing_year = models.IntegerField()

generation_date = models.DateTimeField(
verbose_name=None, auto_now=True
)

detail = JSONField()


class BillingSummary(models.Model):

total_order = models.IntegerField()
detail = JSONField()
billing_year = models.IntegerField()
billing_month = models.IntegerField()
amount_total = models.DecimalField(
max_digits=6,
decimal_places=2,
)


# get the total amount from a list of orders
def calculate_amount_total(orders):

total = 0

for order in orders:

total += order.price

return total


# get the order detail from a list of orders
def get_order_detail(self, orders):

detail = {"meal_regular": 0, "meal_large": 0}
2 changes: 2 additions & 0 deletions django/santropolFeast/billing/templating/base_billing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends "base.html" %}
{% load i18n %}
5 changes: 5 additions & 0 deletions django/santropolFeast/billing/templating/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base_billing" %}
{% load i18n %}
{% load static %}

{% block title %}{% trans'Billing' %}{% endblock %}
5 changes: 5 additions & 0 deletions django/santropolFeast/billing/templating/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base_billing" %}
{% load i18n %}
{% load static %}

{% block title %}{% trans'Billing' %}{% endblock %}
8 changes: 8 additions & 0 deletions django/santropolFeast/billing/templating/view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "base_billing" %}
{% load i18n %}
{% load static %}

{% block title %}{% trans'Billing' %}{% endblock %}
{% block content %}

{% endblock %}
37 changes: 36 additions & 1 deletion django/santropolFeast/billing/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
from django.test import TestCase
from order.factories import OrderFactory, OrderItemFactory
from billing.models import Billing, calculate_amount_total
import datetime
from member.factories import ClientFactory, RouteFactory
from order.models import OrderManager

# Create your tests here.

class TestBilling(TestCase):

@classmethod
def setUpTestData(cls):
RouteFactory.create_batch(10)

def testTotalAmount(self):
order = OrderFactory(delivery_date=datetime.datetime.today())
orders = []
orders.append(order)

total_amount = calculate_amount_total(orders)

self.assertEqual(total_amount, order.price)

def testOrderDetail(self):
pass

def testGetOrderForMonth(self):

order = OrderFactory(delivery_date=datetime.datetime.today())
OrderFactory()
month = datetime.datetime.now().strftime("%m")
year = datetime.datetime.now().strftime("%y")
orders = ClientFactory.get_orders_for_month_client(month, year)

self.assertEqual(order, orders.first)

def testGetOrderForMonthClient(self):
pass
10 changes: 10 additions & 0 deletions django/santropolFeast/billing/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf.urls import url
from billing.views import BillingList, BillingCreate, delete_billing
from django.utils.translation import ugettext_lazy as _

urlpatterns = [
url(r'^list/', BillingList.as_view(), name="list"),
url(r'^create', BillingCreate.as_view(), name="create"),
url(r'^view/(?<pk>\d+)/$', BillingView.as_view(), name="view"),
url(r'^delete/(?<pk>\d+)/$', BillingDelete.as_view(), name='delete'),
]
87 changes: 86 additions & 1 deletion django/santropolFeast/billing/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
from django.shortcuts import render
from django.views import generic
from billing.models import Billing, OrderBilling, calculate_amount_total
from django.shortcuts import login_required
from order.models import get_orders_for_month_client

# Create your views here.

class BillingList(generic.ListView):
# Display the billing list
model = Billing
template_name = "list.html"
context_object_name = "billing"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(BillingList, self).dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super(BillingList, self).get_context_data(**kwargs)

# Context variable
context['myVariableOfContext'] = 0

return context


class BillingCreate(generic.CreateView):
# View to create the billing

model = Billing
template_name = "create.html"
context_object_name = "billing"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(BillingCreate, self).dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super(BillingCreate, self).get_context_data(**kwargs)

# Context variable
context['myVariableOfContext'] = 0

return context

def create_bill(client, year, month):

orders = get_orders_for_month_client(month, year, client)

billing = Billing.objects.create(
client=client,
total_amount=calculate_amount_total(orders),
billing_month=month,
billing_year=year,
generation_date=datetime.now(),
detail=get_order_detail(orders),
)

for order in orders:
OrderBilling.objects.create(
order_id=order.id,
billing_id=billing.id
)

return billing


class BillingView(generic.DetailView):
# Display detail of billing
model = Billing
template_name = "view.html"
context_object_name = "billing"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(BillingView, self).dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super(BillingView, self).get_context_data(**kwargs)

# Context variable
context['myVariableOfContext'] = 0

return context


class BillingDelete(generic.DeleteView):
pass
22 changes: 22 additions & 0 deletions django/santropolFeast/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ def get_orders_for_date(self, delivery_date=None):
delivery_date=delivery_date,
)

def get_orders_for_month(self, month, year):
""" Return the orders for the given month """
delivery_date__month = month
delivery_date__year = year

return self.get_queryset().filter(
delivery_date=delivery_date,
status="D",
)

def get_orders_for_month_client(self, month, year, client):
"""Return the orders for the given month and client"""

delivery_date__month = month
delivery_date__year = year

return self.get_queryset().filter(
delivery_date=delivery_date,
client=client,
status="D",
)


class Order(models.Model):

Expand Down

0 comments on commit d7e8c80

Please sign in to comment.