Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Setup with Virtualenv
You can run the Ra demo locally

#### Dependencies
* Python 3.4, 3.5 or 3.6
* Python 3.4, 3.5 , 3.6 or 3.10
* [Virtualenv](https://virtualenv.pypa.io/en/stable/installation/)
* [PostgresSql](https://www.postgresql.org/download/)
* [VirtualenvWrapper](https://virtualenvwrapper.readthedocs.io/en/latest/install.html) (optional)
Expand Down
3 changes: 3 additions & 0 deletions myproject/myproject/environ.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DATABASE_NAME='test'
DATABASE_USER='admin'
DATABASE_PASSWORD='admin'
8 changes: 4 additions & 4 deletions myproject/myproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'jazzmin',
'crequest',
'crispy_forms',
'reversion',
Expand Down Expand Up @@ -134,9 +134,9 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': f'{env("DATABASE_NAME")}',
'USER': f'{env("DATABASE_USER")}',
'PASSWORD': f'{env("DATABASE_PASSWORD")}',
'NAME': f'{env("test")}',
'USER': f'{env("admin")}',
'PASSWORD': f'{env("admin")}',
'HOST': '',
'PORT': '5432',
}
Expand Down
34 changes: 18 additions & 16 deletions myproject/sales/admin.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
from django import forms
from ra.admin.admin import ra_admin_site, RaAdmin, RaMovementAdmin
from .models import Client, Product, SimpleSales
from .models import Client, Product, Expense, ExpenseTransaction, SalesLineTransaction, SalesTransaction
from ra.admin.admin import ra_admin_site, EntityAdmin, TransactionAdmin, TransactionItemAdmin


class ClientAdmin(RaAdmin):
fields = ('slug', 'title', 'notes', 'address', 'email', 'telephone')
view_template = 'sales/admin/client_view.html'
class ExpenseAdmin(EntityAdmin):
pass


class ProductAdmin(EntityAdmin):
pass


class ProductAdmin(RaAdmin):
class ClientAdmin(EntityAdmin):
pass


class SalesOrderAdmin(RaMovementAdmin):
fields = ['slug', 'doc_date', 'client', ('product', 'price', 'quantity', 'value')]
add_form_template = change_form_template = 'sales/admin/salesorder_changeform.html'
class SalesLineAdmin(TransactionItemAdmin):
fields = ('product', 'price', 'quantity', 'value')
model = SalesLineTransaction


def formfield_for_dbfield(self, db_field, request, **kwargs):
formfield = super().formfield_for_dbfield(db_field, request, **kwargs)
if db_field.name == 'value':
formfield.widget = forms.TextInput(attrs={'readonly': 'readonly'})
return formfield
class SalesOrderAdmin(TransactionAdmin):
inlines = [SalesLineAdmin]
fields = ['slug', 'doc_date', 'client', ]
copy_to_formset = ['client']


ra_admin_site.register(Client, ClientAdmin)
ra_admin_site.register(Product, ProductAdmin)
ra_admin_site.register(SimpleSales, SalesOrderAdmin)
ra_admin_site.register(Expense, ExpenseAdmin)
ra_admin_site.register(SalesTransaction, SalesOrderAdmin)
1 change: 1 addition & 0 deletions myproject/sales/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class SalesConfig(AppConfig):

name = 'sales'

def ready(self):
Expand Down
42 changes: 27 additions & 15 deletions myproject/sales/models.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
from django.db import models

from django.db import models
from ra.base.models import BaseInfo, BasePersonInfo, BaseMovementInfo, QuanValueMovementItem
from ra.base.models import EntityModel, TransactionModel, TransactionItemModel, QuantitativeTransactionItemModel
from ra.base.registry import register_doc_type
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _


class Product(BaseInfo):
class Product(EntityModel):
class Meta:
verbose_name = _('Product')
verbose_name_plural = _('Products')


class Client(BasePersonInfo):
class Client(EntityModel):
class Meta:
verbose_name = _('Client')
verbose_name_plural = _('Clients')


class SimpleSales(QuanValueMovementItem):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
class Expense(EntityModel):
class Meta:
verbose_name = _('Expense')
verbose_name_plural = _('Expenses')


class ExpenseTransaction(TransactionItemModel):
expense = models.ForeignKey(Expense, on_delete=models.CASCADE)

@classmethod
def get_doc_type(cls):
return 'sales'
class Meta:
verbose_name = _('Expense Transaction')
verbose_name_plural = _('Expense Transactions')


class SalesTransaction(TransactionModel):
client = models.ForeignKey(Client, on_delete=models.CASCADE)

class Meta:
verbose_name = _('Sale')
verbose_name_plural = _('Sales')


sales = {'name': 'sales', 'plus_list': ['Client'], 'minus_list': ['Product'], }
register_doc_type(sales)
class SalesLineTransaction(QuantitativeTransactionItemModel):
sales_transaction = models.ForeignKey(SalesTransaction, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, on_delete=models.CASCADE)

class Meta:
verbose_name = _('Sale Transaction Line')
verbose_name_plural = _('Sale Transaction Lines')
24 changes: 12 additions & 12 deletions myproject/sales/reports.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from django.utils.translation import ugettext_lazy as _
from ra.reporting.decorators import register_report_view
from django.utils.translation import gettext_lazy as _
from ra.reporting.registry import register_report_view
from ra.reporting.views import ReportView
from .models import Client, SimpleSales, Product
from .models import Client, SalesLineTransaction, Product


@register_report_view
class ClientTotalBalance(ReportView):
report_title = _('Clients Balances')

base_model = Client
report_model = SimpleSales
report_model = SalesLineTransaction

form_settings = {'group_by': 'client',
'group_columns': ['slug', 'title', '__balance__']}
Expand Down Expand Up @@ -40,10 +40,10 @@ class ProductTotalSales(ReportView):
base_model = Product

# What model hold the data that we want to compute.
report_model = SimpleSales
report_model = SalesLineTransaction

# The meat and potato of the report.
# We group the records in SimpleSales by Client ,
# We group the records in SalesLineTransaction by Client ,
# And we display the columns `slug` and `title` (relative to the `base_model` defined above)
# the magic field `__balance__` computes the balance (of the base model)
form_settings = {'group_by': 'product',
Expand All @@ -54,7 +54,7 @@ class ClientList(ReportView):
report_title = _('Our Clients')

base_model = Client
report_model = SimpleSales
report_model = SalesLineTransaction

# will not appear on the reports menu
hidden = True
Expand All @@ -73,7 +73,7 @@ class ProductClientSales(ReportView):
report_title = _('Client Sales for each product')

base_model = Client
report_model = SimpleSales
report_model = SalesLineTransaction

must_exist_filter = 'client_id'
header_report = ClientList
Expand All @@ -88,7 +88,7 @@ class ProductClientSales(ReportView):
class ClientDetailedStatement(ReportView):
report_title = _('client Statement')
base_model = Client
report_model = SimpleSales
report_model = SalesLineTransaction

must_exist_filter = 'client_id'
header_report = ClientList
Expand All @@ -103,7 +103,7 @@ class ProductSalesMonthly(ReportView):
report_title = _('Product Sales Monthly')

base_model = Product
report_model = SimpleSales
report_model = SalesLineTransaction

form_settings = {
'group_by': 'product',
Expand Down Expand Up @@ -172,7 +172,7 @@ class ClientSalesMonthlySeries(ReportView):
report_title = _('Client Sales Monthly')

base_model = Client
report_model = SimpleSales
report_model = SalesLineTransaction

form_settings = {
'group_by': 'client',
Expand All @@ -186,7 +186,7 @@ class ClientSalesMonthlySeries(ReportView):
@register_report_view
class ProductClientSalesMatrix(ReportView):
base_model = Product
report_model = SimpleSales
report_model = SalesLineTransaction
report_title = _('Product Client sales Cross-tab')

form_settings = {
Expand Down
43 changes: 27 additions & 16 deletions myproject/sales/templates/sales/admin/salesorder_changeform.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
{% extends RA_THEME|add:'/change_form.html' %}
{% extends 'ra/change_form.html' %}

{% block admin_change_form_document_ready %}
<script>
$(document).ready(function () {
const $quantity = $('[name*=quantity]');
const $price = $('[name*=price]');
{% block extrajs %}
{{ block.super }}
<script>
django.jQuery(document).ready(function () {
const allQuantity = $('[name*=quantity]');
const allPrice = $('[name*=price]');

function calculateTotal(e) {
let quantity = smartParseFloat($quantity.val());
let price = smartParseFloat($price.val());
$('[name*=value]').val(quantity * price)
}
function calculateTotal(e) {
let holder = $(e.target).parents('.dynamic-saleslinetransaction_set');
let $quantity = holder.find('[name*=quantity]');
let $price = holder.find('[name*=price]');
let quantity = $.ra.smartParseFloat($quantity.val());
let price = $.ra.smartParseFloat($price.val());
holder.find('[name*=value]').val(quantity * price)
}

$quantity.on('change', calculateTotal);
$price.on('change', calculateTotal);
})
</script>
{% endblock %}
allQuantity.on('change', calculateTotal);
allPrice.on('change', calculateTotal);

// The newly created rows
// ref: https://docs.djangoproject.com/en/2.2/ref/contrib/admin/javascript/
django.jQuery(document).on('formset:added', function (event, $row, formsetName) {
$row.find('[name*=quantity]').on('change', calculateTotal)
$row.find('[name*=price]').on('change', calculateTotal)
});
})
</script>
{% endblock %}
24 changes: 7 additions & 17 deletions myproject/sales/templates/sales/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
{% extends 'ra/admin/base_site.html' %}
{% load ra_admin_tags %}
{% extends 'ra/base_site.html' %}
{% load ra_admin_tags ra_tags %}

{% block content %}
{% get_report base_model='client' report_slug='clienttotalbalance' as client_balances %}
<div data-report-widget
data-report-url="{{ client_balances.get_url }}">

<div data-report-table></div>
</div>

<div data-report-widget
data-report-url="{{ client_balances.get_url }}">

<canvas data-report-chart height="50" data-report-default-chart="bar_chart"></canvas>
<div data-report-table></div>
</div>

{% endblock %}
<div class="col-sm-12">
{% get_report base_model='product' report_slug='ProductSalesMonthly' as ProductSalesMonthly %}
{% get_html_panel ProductSalesMonthly %}
</div>
{% endblock %}