Skip to content

Commit

Permalink
[Issue #174] fixed several bugs which have been caused by the change …
Browse files Browse the repository at this point in the history
…in the models
  • Loading branch information
scaphilo committed Oct 3, 2018
1 parent b1b8fd2 commit 4e72dd2
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 198 deletions.
2 changes: 1 addition & 1 deletion koalixcrm/crm/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from koalixcrm.crm.contact.customer import Customer, OptionCustomer
from koalixcrm.crm.contact.supplier import Supplier, OptionSupplier
from koalixcrm.crm.contact.customer_group import CustomerGroup, OptionCustomerGroup
from koalixcrm.crm.contact.customerbillingcycle import CustomerBillingCycle, OptionCustomerBillingCycle
from koalixcrm.crm.contact.customer_billing_cycle import CustomerBillingCycle, OptionCustomerBillingCycle
from koalixcrm.crm.contact.person import Person
from koalixcrm.crm.contact.contact import OptionPerson, CallForContact, VisitForContact
from koalixcrm.crm.contact.call import OptionCall, OptionVisit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
verbose_name_plural = _('Customer Billing Cycle')

def __str__(self):
return self.id.__str__ + ' ' + self.name
return self.id.__str__() + ' ' + self.name


class OptionCustomerBillingCycle(admin.ModelAdmin):
Expand Down
12 changes: 6 additions & 6 deletions koalixcrm/crm/documents/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +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_type.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 All @@ -96,10 +96,10 @@ def calculate_position_tax(position, currency):
Raises:
Can trow Product.NoPriceFound when Product Price could not be found"""
if isinstance(position.discount, Decimal):
position.last_calculated_tax = int(position.product.get_tax_rate() / 100 * position.position_price_per_unit * position.quantity * (
position.last_calculated_tax = int(position.product_type.get_tax_rate() / 100 * position.position_price_per_unit * position.quantity * (
1 - position.discount / 100) / currency.rounding) * currency.rounding
else:
position.last_calculated_tax = int(position.product.get_tax_rate() / 100 * position.position_price_per_unit * position.quantity /
position.last_calculated_tax = int(position.product_type.get_tax_rate() / 100 * position.position_price_per_unit * position.quantity /
currency.rounding) * currency.rounding
position.save()
return position.last_calculated_tax
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create_from_reference(self, calling_model):
self.attach_text_paragraphs()

def __str__(self):
return _("Delivery Note") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Delivery Note") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def register_payment_in_accounting(self, request, amount, payment_account):
booking.save()

def __str__(self):
return _("Invoice") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Invoice") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/payment_reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def create_from_reference(self, calling_model):
self.staff = calling_model.staff

def __str__(self):
return _("Payment Reminder") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Payment Reminder") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/purchase_confirmation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create_from_reference(self, calling_model):
self.attach_text_paragraphs()

def __str__(self):
return _("Purchase Confirmation") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Purchase Confirmation") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_from_reference(self, calling_model):
self.staff = calling_model.staff

def __str__(self):
return _("Purchase Order") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Purchase Order") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/documents/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_from_reference(self, calling_model):
self.attach_text_paragraphs()

def __str__(self):
return _("Quote") + ": " + str(self.id) + " " + _("from Contract") + ": " + str(self.contract.id)
return _("Quote") + ": " + self.id.__str__() + " " + _("from Contract") + ": " + self.contract.id.__str__()

class Meta:
app_label = "crm"
Expand Down
7 changes: 4 additions & 3 deletions koalixcrm/crm/documents/sales_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Meta:
verbose_name_plural = _('Text Paragraphs In Sales Documents')

def __str__(self):
return self.id.__str__
return self.id.__str__()


class SalesDocument(models.Model):
Expand Down Expand Up @@ -78,8 +78,9 @@ class SalesDocument(models.Model):
blank=False, null=False)
date_of_creation = models.DateTimeField(verbose_name=_("Created at"),
auto_now_add=True)
custom_date_field = models.DateTimeField(verbose_name=_("Custom Date/Time"),
blank=True, null=True)
custom_date_field = models.DateField(verbose_name=_("Custom Date"),
blank=True,
null=True)
last_modification = models.DateTimeField(verbose_name=_("Last modified"),
auto_now=True)
last_modified_by = models.ForeignKey('auth.User', limit_choices_to={'is_staff': True},
Expand Down
64 changes: 46 additions & 18 deletions koalixcrm/crm/documents/sales_document_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,45 @@
class Position(models.Model):
position_number = models.PositiveIntegerField(verbose_name=_("Position Number"),
validators=[MinValueValidator(1)])
quantity = models.DecimalField(verbose_name=_("Quantity"), decimal_places=3, max_digits=10)
description = models.TextField(verbose_name=_("Description"), blank=True, null=True)
discount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name=_("Discount"), blank=True, null=True)
product = models.ForeignKey("Product", verbose_name=_("Product"), blank=True, null=True)
unit = models.ForeignKey("Unit", verbose_name=_("Unit"), blank=True, null=True)
sent_on = models.DateField(verbose_name=_("Shipment on"), blank=True, null=True)
quantity = models.DecimalField(verbose_name=_("Quantity"),
decimal_places=3,
max_digits=10)
description = models.TextField(verbose_name=_("Description"),
blank=True, null=True)
discount = models.DecimalField(max_digits=5, decimal_places=2,
verbose_name=_("Discount"),
blank=True,
null=True)
product_type = models.ForeignKey("ProductType",
verbose_name=_("Product"),
blank=True,
null=True)
unit = models.ForeignKey("Unit", verbose_name=_("Unit"),
blank=True,
null=True)
sent_on = models.DateField(verbose_name=_("Shipment on"),
blank=True,
null=True)
overwrite_product_price = models.BooleanField(verbose_name=_('Overwrite Product Price'))
position_price_per_unit = models.DecimalField(verbose_name=_("Price Per Unit"), max_digits=17, decimal_places=2,
blank=True, null=True)
last_pricing_date = models.DateField(verbose_name=_("Last Pricing Date"), blank=True, null=True)
last_calculated_price = models.DecimalField(max_digits=17, decimal_places=2, verbose_name=_("Last Calculated Price"),
position_price_per_unit = models.DecimalField(verbose_name=_("Price Per Unit"),
max_digits=17,
decimal_places=2,
blank=True, null=True)
last_pricing_date = models.DateField(verbose_name=_("Last Pricing Date"),
blank=True,
null=True)
last_calculated_price = models.DecimalField(max_digits=17,
decimal_places=2,
verbose_name=_("Last Calculated Price"),
blank=True, null=True)
last_calculated_tax = models.DecimalField(max_digits=17, decimal_places=2, verbose_name=_("Last Calculated Tax"),
blank=True, null=True)
last_calculated_tax = models.DecimalField(max_digits=17,
decimal_places=2,
verbose_name=_("Last Calculated Tax"),
blank=True,
null=True)

def __str__(self):
return _("Position") + ": " + str(self.id)
return _("Position") + ": " + self.id.__str__()

class Meta:
app_label = "crm"
Expand All @@ -49,7 +71,7 @@ def add_positions(position_class, object_to_create_pdf):
objects = list(position_class.objects.filter(sales_document=object_to_create_pdf.id))
for position in list(position_class.objects.filter(sales_document=object_to_create_pdf.id)):
objects += list(Position.objects.filter(id=position.id))
objects += list(ProductType.objects.filter(id=position.product.id))
objects += list(ProductType.objects.filter(id=position.product_type.id))
objects += list(Unit.objects.filter(id=position.unit.id))
return objects

Expand All @@ -58,12 +80,11 @@ def create_position(self, calling_model, attach_to_model):
links itself to the attach_to_model, this function is usually
used within the create invoice, quote, reminder,... functions"""

self.product = calling_model.product
self.product_type = calling_model.product_type
self.position_number = calling_model.position_number
self.quantity = calling_model.quantity
self.description = calling_model.description
self.discount = calling_model.discount
self.product = calling_model.product
self.unit = calling_model.unit
self.sent_on = calling_model.sent_on
self.overwrite_product_price = calling_model.overwrite_product_price
Expand All @@ -85,8 +106,15 @@ class SalesDocumentInlinePosition(admin.TabularInline):
fieldsets = (
('', {
'fields': (
'position_number', 'quantity', 'unit', 'product', 'description', 'discount', 'overwrite_product_price',
'position_price_per_unit', 'sent_on')
'position_number',
'quantity',
'unit',
'product_type',
'description',
'discount',
'overwrite_product_price',
'position_price_per_unit',
'sent_on')
}),
)
allow_add = True
Expand Down
2 changes: 1 addition & 1 deletion koalixcrm/crm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from koalixcrm.crm.contact.customer_group import *
from koalixcrm.crm.contact.customer import *
from koalixcrm.crm.contact.postal_address import *
from koalixcrm.crm.contact.customerbillingcycle import *
from koalixcrm.crm.contact.customer_billing_cycle import *
from koalixcrm.crm.contact.email_address import *
from koalixcrm.crm.contact.phone_address import *
from koalixcrm.crm.contact.supplier import *
Expand Down
6 changes: 3 additions & 3 deletions koalixcrm/crm/product/customer_group_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class CustomerGroupTransform(models.Model):
blank=False,
null=False)
product_type = models.ForeignKey('ProductType',
verbose_name=_("Product Type"),
blank=False,
null=False)
verbose_name=_("Product Type"),
blank=False,
null=False)
factor = models.IntegerField(verbose_name=_("Factor between From and To Customer Group"),
blank=True,
null=True)
Expand Down
51 changes: 28 additions & 23 deletions koalixcrm/crm/product/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ def __str__(self):
return str(self.id) + " " +str(self.price) + " " + str(self.currency.short_name)

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

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

def is_customer_group_criteria_fulfilled(self, customer_group):
if not self.customer_group:
if self.customer_group is None:
return True
elif self.customer_group == customer_group:
return True
Expand All @@ -77,35 +77,35 @@ def is_date_in_range(self, date):
else:
return False

def get_currency_transform_factor(self, currency, product):
def get_currency_transform_factor(self, currency, product_type):
"""check currency conditions and factor"""
if self.currency == currency:
currency_factor = 1
else:
currency_transform = CurrencyTransform.objects.get(from_currency=self.currency,
to_currency=currency,
product=product)
product_type=product_type)
if currency_transform:
currency_transform.get_transfrom_factor()
else:
currency_factor = 0
return currency_factor

def get_unit_transform_factor(self, unit, product):
def get_unit_transform_factor(self, unit, product_type):
"""check unit conditions and factor"""
if self.unit == unit:
unit_factor = 1
else:
unit_transform = UnitTransform.objects.get(from_unit=self.unit,
to_unit=unit,
product=product)
product_type=product_type)
if unit_transform:
unit_transform.get_transfrom_factor()
else:
unit_factor = 0
return unit_factor

def get_customer_group_transform_factor(self, customer, product):
def get_customer_group_transform_factor(self, customer, product_type):
"""The function searches through all customer_groups in which the customer is member of
from these customer_groups, the function returns the customer_group with the perfect match
or it returns the factor with the lowest transform factor
Expand All @@ -119,21 +119,26 @@ def get_customer_group_transform_factor(self, customer, product):
Raises:
No exceptions planned"""
customer_groups = CustomerGroup.objects.filter(customer=customer)
customer_group_factor = 0
for customer_group in customer_groups:
if self.customer_group == customer_group:
customer_group_factor = 1
"""Stop for loop when a perfect match is found"""
break
else:
customer_group = CustomerGroupTransform.objects.get(from_customer_group=self.customer_group,
to_customer_group=customer_group,
product=product)
if customer_group:
if customer_group_factor > customer_group.get_transfrom_factor():
customer_group_factor = customer_group.get_transfrom_factor()
return customer_group_factor
if self.customer_group is None:
return 1
if customer is not None:
customer_groups = CustomerGroup.objects.filter(customer=customer)
customer_group_factor = 0
if customer_groups is not None:
for customer_group in customer_groups:
if self.customer_group == customer_group:
customer_group_factor = 1
"""Stop for loop when a perfect match is found"""
break
else:
customer_group = CustomerGroupTransform.objects.get(from_customer_group=self.customer_group,
to_customer_group=customer_group,
product_type=product_type)
if customer_group:
if customer_group_factor > customer_group.get_transfrom_factor():
customer_group_factor = customer_group.get_transfrom_factor()
return customer_group_factor
return 1

def matches_date_unit_customer_group_currency(self, date, unit, customer_group, currency):
if (self.is_unit_criteria_fulfilled(unit) &
Expand Down
Loading

0 comments on commit 4e72dd2

Please sign in to comment.