Skip to content

Commit

Permalink
Merge pull request #6 from moltob/hotfix/4.3.1
Browse files Browse the repository at this point in the history
sales fix
  • Loading branch information
moltob authored Sep 14, 2017
2 parents b046ac7 + eb016d0 commit 4ad66ae
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 113 deletions.
22 changes: 13 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## Unreleased

## [4.3.0] - 2017-09-08
## 4.3.1 - 2017-09-14
### Added

- Display of changelog.

### Fixed

- Annual income now exact sum of all invoices.
- Drilldown to articles sold in given year now shows article names from invoice, not the originally selected article (from master data).

## 4.3.0 - 2017-09-08
### Added
- Excel export of invoice lists.
- TODO

### Changed
- TODO

### Removed

[Unreleased]: https://github.com/moltob/pybizwiz/compare/4.3.0...HEAD
[4.3.0]: https://github.com/moltob/pybizwiz/compare/4.3.0...v4.2.0
5 changes: 5 additions & 0 deletions bizwiz/common/templates/common/changelog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "common/base.html" %}

{% block content %}
{{ changelog | safe }}
{% endblock content %}
23 changes: 1 addition & 22 deletions bizwiz/common/templates/common/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ <h1>{% trans 'Welcome to Bizwiz 4' %}</h1>
aspiring photographer.
{% endblocktrans %}
</p>
<h2>{% trans 'Background' %}</h2>
<p>
{% blocktrans %}
With the official start of the photography business at
http://www.bpfotografie.de/, Bizwiz started in early 2007 as a Windows
forms application based on the .NET framework. The first version used in production
was Bizwiz 2 and was written in C# and used MSSQL as a server backend.
{% endblocktrans %}
</p>
<p>
{% blocktrans %}
After production use until end of 2016, Bizwiz was fully rewritten as a Django web
application, utilizing Python as well as the typical web languages. Bizwiz is still
a server-rendered (classic) web application, as it is truly data-driven and the
classic render-edit-post cycle applies very well. In addition, learning the classic
Django framework was also an objective of the project. Today, Bizwiz is running
within a Docker container on virtually any platform.
{% endblocktrans %}
</p>
<h2>{% trans 'Resources' %}</h2>
<p>
{% blocktrans %}
Expand All @@ -40,8 +21,6 @@ <h2>{% trans 'Resources' %}</h2>
<a href="https://hub.docker.com/r/mpagel/bizwiz">https://hub.docker.com/r/mpagel/bizwiz</a>.
{% endblocktrans %}
</p>

</div>
<p class="small text-right">Version: {{ version }}</p>

<p class="small text-right">Version: <a href="{% url 'common:changelog' %}">{{ version }}</a></p>
{% endblock content %}
1 change: 1 addition & 0 deletions bizwiz/common/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

urlpatterns = [
url(r'^$', views.Welcome.as_view(), name='index'),
url(r'^changelog$', views.Changelog.as_view(), name='changelog'),
url(r'^session-filter/$', views.SessionFilter.as_view(), name='session-filter'),
]
31 changes: 30 additions & 1 deletion bizwiz/common/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import logging
import os

from django import conf
from django.views import generic
import markdown

from bizwiz.common.session_filter import set_session_filter
from bizwiz.version import BIZWIZ_VERSION

_logger = logging.getLogger(__name__)


class Welcome(generic.TemplateView):
template_name = 'common/welcome.html'

def get_context_data(self, **kwargs):
return super().get_context_data(version=BIZWIZ_VERSION, **kwargs)
return super().get_context_data(
version=BIZWIZ_VERSION,
**kwargs
)


class Changelog(generic.TemplateView):
template_name = 'common/changelog.html'

def get_context_data(self, **kwargs):

# read an render changelog markdown:
try:
with open(os.path.join(conf.settings.BASE_DIR, 'CHANGELOG.md')) as changelogfile:
changelog = markdown.markdown(changelogfile.read())
except FileNotFoundError:
_logger.warning('Changelog not found.')
changelog = ''

return super().get_context_data(
changelog=changelog,
**kwargs
)


class SizedColumnsMixin:
Expand Down
4 changes: 2 additions & 2 deletions bizwiz/invoices/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Meta:
class InvoicedCustomerForm(forms.ModelForm):
class Meta:
model = InvoicedCustomer
exclude = ('original_customer', 'invoice')
exclude = ('invoice', )

helper = helper.FormHelper()
helper.form_tag = False
Expand Down Expand Up @@ -253,7 +253,7 @@ class BaseInvoicedArticleFormset(forms.BaseInlineFormSet):
min_num=1,
validate_min=True,
extra=0,
exclude=('original_article', 'invoice')
exclude=('invoice', )
)


Expand Down
23 changes: 23 additions & 0 deletions bizwiz/invoices/migrations/0014_auto_20170913_1944.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-13 17:44
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('invoices', '0013_auto_20170903_1541'),
]

operations = [
migrations.RemoveField(
model_name='invoicedarticle',
name='original_article',
),
migrations.RemoveField(
model_name='invoicedcustomer',
name='original_customer',
),
]
20 changes: 2 additions & 18 deletions bizwiz/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ class InvoicedArticle(ArticleBase):
default=ItemKind.ARTICLE,
blank=True,
)
original_article = models.ForeignKey(
Article,
on_delete=models.SET_NULL,
verbose_name=_("Original article"),
blank=True,
null=True,
related_name='invoiced_articles',
)
amount = models.PositiveSmallIntegerField(_("Amount"))
invoice = models.ForeignKey(
Invoice,
Expand All @@ -100,7 +92,7 @@ class Meta:

@classmethod
def create(cls, invoice, article, amount):
invoiced_article = InvoicedArticle(invoice=invoice, original_article=article, amount=amount)
invoiced_article = InvoicedArticle(invoice=invoice, amount=amount)
if article:
copy_field_data(ArticleBase, article, invoiced_article)
return invoiced_article
Expand All @@ -115,14 +107,6 @@ def total(self):


class InvoicedCustomer(CustomerBase):
original_customer = models.ForeignKey(
Customer,
on_delete=models.SET_NULL,
verbose_name=_("Original customer"),
blank=True,
null=True,
related_name='invoiced_customer',
)
invoice = models.OneToOneField(
Invoice,
verbose_name=_("Invoice"),
Expand All @@ -136,6 +120,6 @@ class Meta:

@classmethod
def create(cls, invoice, customer):
invoiced_customer = InvoicedCustomer(invoice=invoice, original_customer=customer)
invoiced_customer = InvoicedCustomer(invoice=invoice)
copy_field_data(CustomerBase, customer, invoiced_customer)
return invoiced_customer
7 changes: 0 additions & 7 deletions bizwiz/invoices/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,8 @@ def create_invoice(*, customer, invoiced_articles=None, project=None, rebates=No
_logger.info('Creating new invoice {} for customer {}.'.format(invoice.number, customer))

if invoiced_articles:
# get articles from database by names:
names = {a.name for a in invoiced_articles}
articles = Article.objects.filter(name__in=names)
article_by_name = {a.name: a for a in articles}

for invoiced_article in invoiced_articles:
original_article = article_by_name.get(invoiced_article.name)
invoiced_article.invoice = invoice
invoiced_article.original_article = original_article
invoiced_article.save()
_logger.debug(' {}'.format(invoiced_article))

Expand Down
33 changes: 17 additions & 16 deletions bizwiz/invoices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,13 @@ def forms_valid(self, invoice_form, invoiced_article_formset):
rebates = invoice_form.cleaned_data['rebates']

with transaction.atomic():
invoiced_articles = invoiced_article_formset.save(commit=False)
invoice = services.create_invoice(
project=project,
invoiced_articles=invoiced_articles,
customer=original_customer,
rebates=rebates,
)
invoiced_article_formset.instance = invoice
invoiced_article_formset.save()
services.refresh_rebates(invoice)

return self.form_valid(invoice_form)

Expand Down Expand Up @@ -330,15 +329,22 @@ class Sales(mixins.LoginRequiredMixin, SizedColumnsMixin, tables.SingleTableView
"""Sales per year."""
table_class = SalesTable
column_widths = ('10%', '20%', '30%', '40%',)

queryset = Invoice.objects \
.exclude(date_paid=None) \
.annotate(year_paid=functions.ExtractYear('date_paid')) \
.values('year_paid') \
.filter(invoiced_articles__kind=ItemKind.ARTICLE) \
.annotate(num_invoices=models.Count('id', distinct=True),
num_articles=models.Sum('invoiced_articles__amount'),
total=models.Sum(
.annotate(total=models.Sum(
models.F('invoiced_articles__price') * models.F('invoiced_articles__amount')
)) \
.annotate(num_invoices=models.Count('id', distinct=True),
num_articles=models.Sum(
models.Case(
models.When(invoiced_articles__kind=ItemKind.ARTICLE,
invoiced_articles__price__gt=0,
then='invoiced_articles__amount'),
default=0
)
))
template_name = 'invoices/sales_list.html'

Expand All @@ -353,12 +359,7 @@ def get_context_data(self, **kwargs):


class ArticleSalesTable(tables.Table):
name = tables.LinkColumn(
'articles:update',
args=[tables.utils.A('original_article__pk')],
verbose_name=_("Invoice text"),
accessor='original_article__name'
)
name = tables.Column(_("Invoice text"))
amount = tables.Column(_("Ordered"), accessor='year_amount', attrs=COLUMN_RIGHT_ALIGNED)
total = tables.Column(_('Total value'), attrs=COLUMN_RIGHT_ALIGNED)

Expand All @@ -376,10 +377,10 @@ class ArticleSales(mixins.LoginRequiredMixin, SizedColumnsMixin, tables.SingleTa

def get_queryset(self):
return InvoicedArticle.objects \
.filter(kind=ItemKind.ARTICLE, original_article__isnull=False) \
.filter(kind=ItemKind.ARTICLE) \
.filter(price__gt=0) \
.filter(invoice__date_paid__year=self.kwargs['year']) \
.values('original_article__pk', 'original_article__name') \
.values('name') \
.annotate(year_amount=models.Sum('amount'),
total=models.Sum(models.F('price') * models.F('amount'))) \
.order_by('-year_amount')
Expand All @@ -390,7 +391,7 @@ def get_context_data(self, **kwargs):
# prepare chart data by separating x and y axis:
queryset = context['object_list']
article_amounts = [a['year_amount'] for a in queryset]
article_names = [a['original_article__name'] for a in queryset]
article_names = [a['name'] for a in queryset]

# clear names of articles with minor contribution:
total_amount = sum(article_amounts)
Expand Down
1 change: 1 addition & 0 deletions requirements-prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ django-crispy-forms==1.6.1
django-jquery-js==3.1.1
django-tables2==1.10.0
gunicorn==19.7.1
Markdown==2.6.9
olefile==0.44
Pillow==4.2.1
py==1.4.34
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pytest-django
# PDF generation
reportlab

# markdown to html (e.g. for changelog display)
markdown

# serve static files simply via gunicorn
whitenoise

Expand Down
2 changes: 0 additions & 2 deletions test/bizwiz/invoices/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def test__invoiced_customer__create():
assert c2.company_name == 'COMPANY_NAME'
assert c2.zip_code == 'ZIP'
assert c2.city == 'CITY'
assert c2.original_customer == c1


def test__invoiced_article__create():
Expand All @@ -48,7 +47,6 @@ def test__invoiced_article__create():
assert a2.name == 'NAME'
assert a2.price == 1.2
assert a2.amount == 1
assert a2.original_article == a1
assert a2.kind == ItemKind.ARTICLE


Expand Down
13 changes: 0 additions & 13 deletions test/bizwiz/invoices/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def test__create_invoice__global(customer, posted_articles):
invoice = create_invoice(customer=customer, invoiced_articles=posted_articles)

assert not invoice.project
assert invoice.invoiced_customer.original_customer == customer
invoiced_articles = list(invoice.invoiced_articles.all())
assert len(invoiced_articles) == 2
assert invoiced_articles[0].name == 'A1'
Expand Down Expand Up @@ -123,18 +122,6 @@ def test__create_invoice__number_unique(customer, posted_articles):
assert invoice.number not in {'123', '45'}


@pytest.mark.django_db
def test__create_invoice__original_article(customer, posted_articles):
article = Article(name='A1', price=5)
article.save()

invoice = create_invoice(customer=customer, invoiced_articles=posted_articles)

invoiced_articles = invoice.invoiced_articles.all()
assert invoiced_articles[0].original_article == article
assert invoiced_articles[1].original_article is None


@pytest.fixture
def invoice_with_rebates(customer, posted_articles):
rebate1 = Rebate(name='NAME1', kind=RebateKind.PERCENTAGE, value=10, auto_apply=False)
Expand Down
Loading

0 comments on commit 4ad66ae

Please sign in to comment.