Skip to content

Commit

Permalink
[Issue #174] started to add a few more factories for testing in the a…
Browse files Browse the repository at this point in the history
…rea of product and customer groups. Added a new entity "Estimation of ressource consumption" removed the employee assignment to task instead
  • Loading branch information
scaphilo committed Aug 31, 2018
1 parent 694f70a commit b10ef4e
Show file tree
Hide file tree
Showing 19 changed files with 433 additions and 155 deletions.
10 changes: 8 additions & 2 deletions koalixcrm/crm/documents/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def calculate_document_price(document, pricing_date):

if positions.exists():
for position in positions:
price += Calculations.calculate_position_price(position, pricing_date, contact_for_price_calculation, document.currency)
price += Calculations.calculate_position_price(position,
pricing_date,
contact_for_price_calculation,
document.currency)
tax += Calculations.calculate_position_tax(position, document.currency)

if calculate_with_document_discount:
Expand Down Expand Up @@ -64,7 +67,10 @@ def calculate_position_price(position, pricing_date, contact, currency):
Can trow Product.NoPriceFound when Product Price could not be found"""

if not position.overwrite_product_price:
position.position_price_per_unit = position.product.get_price(pricing_date, position.unit, contact, currency)
position.position_price_per_unit = position.product.get_price(pricing_date,
position.unit,
contact,
currency)
if isinstance(position.discount, Decimal):
position.last_calculated_price = int(position.position_price_per_unit * position.quantity * (
1 - position.discount / 100) / currency.rounding) * currency.rounding
Expand Down
6 changes: 6 additions & 0 deletions koalixcrm/crm/factories/factory_customer_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ class Meta:
name = factory.Sequence(lambda n: "Customer Group #%s" % n)


class AdvancedCustomerGroupFactory(factory.django.DjangoModelFactory):
class Meta:
model = CustomerGroup
django_get_or_create = ('name',)

name = factory.Sequence(lambda n: "Customer Group #%s" % n)
16 changes: 16 additions & 0 deletions koalixcrm/crm/factories/factory_customer_group_transformk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

import factory
from koalixcrm.crm.product.customer_group_transform import CustomerGroupTransform
from koalixcrm.crm.factories.factory_product import StandardProductFactory
from koalixcrm.crm.factories.factory_customer_group import AdvancedCustomerGroupFactory
from koalixcrm.crm.factories.factory_customer_group import StandardCustomerGroupFactory


class StandardCustomerGroupTransformFactory(factory.django.DjangoModelFactory):
class Meta:
model = CustomerGroupTransform
from_customer_group = factory.SubFactory(AdvancedCustomerGroupFactory)
to_customer_group = factory.SubFactory(StandardCustomerGroupFactory)
product = factory.SubFactory(StandardProductFactory)
factor = "10"
29 changes: 29 additions & 0 deletions koalixcrm/crm/factories/factory_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

import factory
from koalixcrm.crm.models import Project
from koalixcrm.crm.factories.factory_unit import StandardUnitFactory
from koalixcrm.djangoUserExtension.factories.factory_template_set import StandardTemplateSetFactory
from koalixcrm.crm.factories.factory_user import StaffUserFactory


class StandardProductFactory(factory.django.DjangoModelFactory):
class Meta:
model = Project
django_get_or_create = ('project_name',)

description = models.TextField(verbose_name=_("Description"),
null=True,
blank=True)
title = "This is a test Product"
product_number = "123456"
default_unit = factory.SubFactory(StandardUnitFactory)
date_of_creation = make_date_utc(datetime.datetime(2018, 6, 15, 00))
last_modification = make_date_utc(datetime.datetime(2018, 6, 15, 00))
last_modified_by = factory.SubFactory(StaffUserFactory)
tax = factory.SubFactory(StandardUnitFactory)
accounting_product_categorie = models.ForeignKey('accounting.ProductCategorie',
verbose_name=_("Accounting Product Categorie"),
null=True,
blank="True")

4 changes: 3 additions & 1 deletion koalixcrm/crm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
from koalixcrm.crm.product.currency import *
from koalixcrm.crm.product.price import *
from koalixcrm.crm.product.product import *
from koalixcrm.crm.product.customer_group_transform import *
from koalixcrm.crm.product.unit_transform import *
from koalixcrm.crm.product.tax import *
from koalixcrm.crm.product.unit import *

from koalixcrm.crm.reporting.employee_assignment_to_task import *
from koalixcrm.crm.reporting.estimation_of_resource_consumption import *
from koalixcrm.crm.reporting.generic_task_link import *
from koalixcrm.crm.reporting.task import *
from koalixcrm.crm.reporting.task_link_type import *
Expand Down
51 changes: 51 additions & 0 deletions koalixcrm/crm/product/customer_group_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext as _


class CustomerGroupTransform(models.Model):
from_customer_group = models.ForeignKey('CustomerGroup',
verbose_name=_("From Unit"),
related_name="db_reltransfromfromcustomergroup",
blank=False,
null=False)
to_customer_group = models.ForeignKey('CustomerGroup',
verbose_name=_("To Unit"),
related_name="db_reltransfromtocustomergroup",
blank=False,
null=False)
product = models.ForeignKey('Product',
verbose_name=_("Product"),
blank=False,
null=False)
factor = models.IntegerField(verbose_name=_("Factor between From and To Customer Group"),
blank=True,
null=True)

def transform(self, customer_group):
"""The transform function verifies whether the provided argument customer_group
is corresponding with the "from_customer_group" variable of the CustomerGroupTransform class
When this is ok, the function returns the "to_customer_group". When the provided customer_group
argument is not corresponding, the function returns a "None"
Args:
customer_group: CustomerGroup object
Returns:
CustomerGroup object or None
Raises:
No exceptions planned"""
if self.from_customer_group == customer_group:
return self.to_customer_group
else:
return None

def __str__(self):
return "From " + self.from_customer_group.name + " to " + self.to_customer_group.name

class Meta:
app_label = "crm"
verbose_name = _('Customer Group Price Transform')
verbose_name_plural = _('Customer Group Price Transforms')
78 changes: 37 additions & 41 deletions koalixcrm/crm/product/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,49 @@
from koalixcrm.crm.contact.customer_group import CustomerGroup


class CustomerGroupTransform(models.Model):
from_customer_group = models.ForeignKey('CustomerGroup', verbose_name=_("From Unit"),
related_name="db_reltransfromfromcustomergroup")
to_customer_group = models.ForeignKey('CustomerGroup', verbose_name=_("To Unit"),
related_name="db_reltransfromtocustomergroup")
product = models.ForeignKey('Product', verbose_name=_("Product"))
factor = models.IntegerField(verbose_name=_("Factor between From and To Customer Group"), blank=True, null=True)

def transform(self, customer_group):
if (self.from_customer_group == customer_group):
return self.to_customer_group
else:
return unit

def __str__(self):
return "From " + self.from_customer_group.name + " to " + self.to_customer_group.name

class Meta:
app_label = "crm"
verbose_name = _('Customer Group Price Transfrom')
verbose_name_plural = _('Customer Group Price Transfroms')


class Price(models.Model):
product = models.ForeignKey("Product", verbose_name=_("Product"))
unit = models.ForeignKey(Unit, blank=False, verbose_name=_("Unit"))
currency = models.ForeignKey(Currency, blank=False, null=False, verbose_name=('Currency'))
customer_group = models.ForeignKey(CustomerGroup, blank=True, null=True, verbose_name=_("Customer Group"))
price = models.DecimalField(max_digits=17, decimal_places=2, verbose_name=_("Price Per Unit"))
valid_from = models.DateField(verbose_name=_("Valid from"), blank=True, null=True)
valid_until = models.DateField(verbose_name=_("Valid until"), blank=True, null=True)
product = models.ForeignKey("Product",
verbose_name=_("Product"))
unit = models.ForeignKey(Unit,
blank=False,
verbose_name=_("Unit"))
currency = models.ForeignKey(Currency,
verbose_name='Currency',
blank=False,
null=False)
customer_group = models.ForeignKey(CustomerGroup,
verbose_name=_("Customer Group"),
blank=True,
null=True)
price = models.DecimalField(max_digits=17,
decimal_places=2, verbose_name=_("Price Per Unit"))
valid_from = models.DateField(verbose_name=_("Valid from"),
blank=True,
null=True)
valid_until = models.DateField(verbose_name=_("Valid until"),
blank=True,
null=True)

def is_valid_from_criteria_fulfilled(self, date):
if self.valid_from == None:
return True;
if not self.valid_from:
return True
elif (self.valid_from - date).days <= 0:
return True;
return True
else:
return False;
return False

def is_valid_until_criteria_fulfilled(self, date):
if self.valid_until == None:
if not self.valid_until:
return True
elif (date - self.valid_until).days <= 0:
return True
else:
return False

def is_customer_group_criteria_fulfilled(self, customerGroup):
if self.customer_group == None:
def is_customer_group_criteria_fulfilled(self, customer_group):
if not self.customer_group:
return True
elif self.customer_group == customerGroup:
elif self.customer_group == customer_group:
return True
else:
return False
Expand All @@ -76,7 +67,7 @@ def is_unit_criteria_fulfilled(self, unit):
else:
return False

def matchesDateUnitCustomerGroupCurrency(self, date, unit, customer_group, currency):
def matches_date_unit_customer_group_currency(self, date, unit, customer_group, currency):
if (self.is_unit_criteria_fulfilled(unit) &
self.is_currency_criteria_fulfilled(currency) &
self.is_customer_group_criteria_fulfilled(customer_group) &
Expand All @@ -98,7 +89,12 @@ class ProductPrice(admin.TabularInline):
classes = ['collapse']
fieldsets = (
('', {
'fields': ('price', 'currency', 'unit', 'valid_from', 'valid_until', 'customer_group')
'fields': ('price',
'currency',
'unit',
'valid_from',
'valid_until',
'customer_group')
}),
)
allow_add = True
allow_add = True
50 changes: 37 additions & 13 deletions koalixcrm/crm/product/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,38 @@ def get_price(self, date, unit, customer, currency):
customer_group_transforms = koalixcrm.crm.product.price.CustomerGroupTransform.objects.filter(product=self.id)
valid_prices = list()
for price in list(prices):
if price.direct_fits(date,
unit,
customerGroup,
currency):
valid_prices.append(price.price)
elif price.fits_trough_customer_group_transform():
valid_prices.append(price.price)
elif price.fits_through_currency_transform():
price.transform()
elif price.fits_through_price_and_group_transform():

for customerGroup in CustomerGroup.objects.filter(customer=customer):
if price.matchesDateUnitCustomerGroupCurrency(date, unit, customerGroup, currency):
valid_prices.append(price.price)
if price.matches_date_unit_customer_group_currency(date,
unit,
customerGroup,
currency):
else:
for customerGroupTransform in customer_group_transforms:
if price.matchesDateUnitCustomerGroupCurrency(date,
unit,
customerGroupTransform.transform(customerGroup),
currency):
if price.matches_date_unit_customer_group_currency(date,
unit,
customerGroupTransform.transform(
customerGroup),
currency):
valid_prices.append(price.price * customerGroup.factor);
else:
for unitTransform in list(unit_transforms):
if price.matchesDateUnitCustomerGroupCurrency(date,
unitTransform.transfrom(unit).transform(
unitTransform),
customerGroupTransform.transform(
customerGroup), currency):
if price.matches_date_unit_customer_group_currency(date,
unitTransform.transfrom(unit).transform(
unitTransform),
customerGroupTransform.transform(
customerGroup),
currency):
valid_prices.append(
price.price * customerGroupTransform.factor * unitTransform.factor);
if len(valid_prices) > 0:
Expand Down Expand Up @@ -96,10 +111,19 @@ def __str__(self):


class OptionProduct(admin.ModelAdmin):
list_display = ('product_number', 'title', 'default_unit', 'tax', 'accounting_product_categorie')
list_display = ('product_number',
'title',
'default_unit',
'tax',
'accounting_product_categorie')
list_display_links = ('product_number',)
fieldsets = (
(_('Basics'), {
'fields': ('product_number', 'title', 'description', 'default_unit', 'tax', 'accounting_product_categorie')
'fields': ('product_number',
'title',
'description',
'default_unit',
'tax',
'accounting_product_categorie')
}),)
inlines = [ProductPrice, ProductUnitTransform]
43 changes: 9 additions & 34 deletions koalixcrm/crm/product/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,14 @@ class Meta:


class OptionUnit(admin.ModelAdmin):
list_display = ('id', 'description', 'short_name', 'is_a_fraction_of', 'fraction_factor_to_next_higher_unit')
fieldsets = (('', {'fields': ('description', 'short_name', 'is_a_fraction_of', 'fraction_factor_to_next_higher_unit')}),)
list_display = ('id',
'description',
'short_name',
'is_a_fraction_of',
'fraction_factor_to_next_higher_unit')
fieldsets = (('', {'fields': ('description',
'short_name',
'is_a_fraction_of',
'fraction_factor_to_next_higher_unit')}),)
allow_add = True


class UnitTransform(models.Model):
from_unit = models.ForeignKey('Unit', verbose_name=_("From Unit"), related_name="db_reltransfromfromunit")
to_unit = models.ForeignKey('Unit', verbose_name=_("To Unit"), related_name="db_reltransfromtounit")
product = models.ForeignKey('Product', verbose_name=_("Product"))
factor = models.IntegerField(verbose_name=_("Factor between From and To Unit"), blank=True, null=True)

def transform(self, unit):
if (self.from_unit == unit):
return self.to_unit
else:
return unit

def __str__(self):
return "From " + self.from_unit.short_name + " to " + self.to_unit.short_name

class Meta:
app_label = "crm"
verbose_name = _('Unit Transfrom')
verbose_name_plural = _('Unit Transfroms')


class ProductUnitTransform(admin.TabularInline):
model = UnitTransform
extra = 1
classes = ['collapse']
fieldsets = (
('', {
'fields': ('from_unit', 'to_unit', 'factor',)
}),
)
allow_add = True
Loading

0 comments on commit b10ef4e

Please sign in to comment.