Add TaxRule selection in OrderPositionChange (#1700)

Co-authored-by: Raphael Michel <mail@raphaelmichel.de>
This commit is contained in:
Martin Gross
2020-06-30 11:13:33 +02:00
committed by GitHub
parent 626e332886
commit 5f50aa95eb
12 changed files with 176 additions and 8 deletions

View File

@@ -1800,7 +1800,10 @@ class OrderFee(models.Model):
self.fee_type, self.value
)
def _calculate_tax(self):
def _calculate_tax(self, tax_rule=None):
if tax_rule:
self.tax_rule = tax_rule
try:
ia = self.order.invoice_address
except InvoiceAddress.DoesNotExist:
@@ -1956,8 +1959,8 @@ class OrderPosition(AbstractPosition):
self.item.id, self.variation.id if self.variation else 0, self.order_id
)
def _calculate_tax(self):
self.tax_rule = self.item.tax_rule
def _calculate_tax(self, tax_rule=None):
self.tax_rule = tax_rule or self.item.tax_rule
try:
ia = self.order.invoice_address
except InvoiceAddress.DoesNotExist:

View File

@@ -1160,6 +1160,7 @@ class OrderChangeManager:
SubeventOperation = namedtuple('SubeventOperation', ('position', 'subevent'))
SeatOperation = namedtuple('SubeventOperation', ('position', 'seat'))
PriceOperation = namedtuple('PriceOperation', ('position', 'price'))
TaxRuleOperation = namedtuple('TaxRuleOperation', ('position', 'tax_rule'))
CancelOperation = namedtuple('CancelOperation', ('position',))
AddOperation = namedtuple('AddOperation', ('item', 'variation', 'price', 'addon_to', 'subevent', 'seat'))
SplitOperation = namedtuple('SplitOperation', ('position',))
@@ -1272,6 +1273,10 @@ class OrderChangeManager:
self._operations.append(self.PriceOperation(position, price))
def change_tax_rule(self, position_or_fee, tax_rule: TaxRule):
self._operations.append(self.TaxRuleOperation(position_or_fee, tax_rule))
self._invoice_dirty = True
def recalculate_taxes(self):
positions = self.order.positions.select_related('item', 'item__tax_rule')
ia = self._invoice_address
@@ -1594,6 +1599,24 @@ class OrderChangeManager:
op.position.price = op.price.gross
op.position._calculate_tax()
op.position.save()
elif isinstance(op, self.TaxRuleOperation):
if isinstance(op.position, OrderPosition):
self.order.log_action('pretix.event.order.changed.tax_rule', user=self.user, auth=self.auth, data={
'position': op.position.pk,
'positionid': op.position.positionid,
'addon_to': op.position.addon_to_id,
'old_taxrule': op.position.tax_rule.pk,
'new_taxrule': op.tax_rule.pk
})
elif isinstance(op.position, OrderFee):
self.order.log_action('pretix.event.order.changed.tax_rule', user=self.user, auth=self.auth, data={
'fee': op.position.pk,
'fee_type': op.position.fee_type,
'old_taxrule': op.position.tax_rule.pk,
'new_taxrule': op.tax_rule.pk
})
op.position._calculate_tax(op.tax_rule)
op.position.save()
elif isinstance(op, self.CancelFeeOperation):
self.order.log_action('pretix.event.order.changed.cancelfee', user=self.user, auth=self.auth, data={
'fee': op.fee.pk,

View File

@@ -13,7 +13,7 @@ def get_price(item: Item, variation: ItemVariation = None,
subevent: SubEvent = None, custom_price_is_net: bool = False,
addon_to: AbstractPosition = None, invoice_address: InvoiceAddress = None,
force_custom_price: bool = False, bundled_sum: Decimal = Decimal('0.00'),
max_discount: Decimal = None) -> TaxedPrice:
max_discount: Decimal = None, tax_rule=None) -> TaxedPrice:
if addon_to:
try:
iao = addon_to.item.addons.get(addon_category_id=item.category_id)
@@ -35,7 +35,9 @@ def get_price(item: Item, variation: ItemVariation = None,
if voucher:
price = voucher.calculate_price(price, max_discount=max_discount)
if item.tax_rule:
if tax_rule is not None:
tax_rule = tax_rule
elif item.tax_rule:
tax_rule = item.tax_rule
else:
tax_rule = TaxRule(