Quellcode durchsuchen

Register discounts on the order line

master
Lars van de Kerkhof vor 2 Jahren
Ursprung
Commit
e6fb1897a4
2 geänderte Dateien mit 52 neuen und 19 gelöschten Zeilen
  1. 51
    19
      src/oscar/apps/basket/abstract_models.py
  2. 1
    0
      src/oscar/apps/basket/utils.py

+ 51
- 19
src/oscar/apps/basket/abstract_models.py Datei anzeigen

727
     def __init__(self, *args, **kwargs):
727
     def __init__(self, *args, **kwargs):
728
         super().__init__(*args, **kwargs)
728
         super().__init__(*args, **kwargs)
729
         # Instance variables used to persist discount information
729
         # Instance variables used to persist discount information
730
-        self._discount_excl_tax = D("0.00")
731
-        self._discount_incl_tax = D("0.00")
730
+        self.discounts = []
731
+        # self._discount_excl_tax = D("0.00")
732
+        # self._discount_incl_tax = D("0.00")
732
         self.consumer = LineOfferConsumer(self)
733
         self.consumer = LineOfferConsumer(self)
733
 
734
 
734
     class Meta:
735
     class Meta:
764
         """
765
         """
765
         Remove any discounts from this line.
766
         Remove any discounts from this line.
766
         """
767
         """
767
-        self._discount_excl_tax = D("0.00")
768
-        self._discount_incl_tax = D("0.00")
768
+        self.discounts = []
769
+        # self._discount_excl_tax = D("0.00")
770
+        # self._discount_incl_tax = D("0.00")
769
         self.consumer = LineOfferConsumer(self)
771
         self.consumer = LineOfferConsumer(self)
770
 
772
 
771
     def discount(self, discount_value, affected_quantity, incl_tax=True, offer=None):
773
     def discount(self, discount_value, affected_quantity, incl_tax=True, offer=None):
778
                     "Attempting to discount the tax-inclusive price of a line "
780
                     "Attempting to discount the tax-inclusive price of a line "
779
                     "when tax-exclusive discounts are already applied"
781
                     "when tax-exclusive discounts are already applied"
780
                 )
782
                 )
781
-            self._discount_incl_tax += discount_value
782
         else:
783
         else:
783
             if self._discount_incl_tax > 0:
784
             if self._discount_incl_tax > 0:
784
                 raise RuntimeError(
785
                 raise RuntimeError(
785
                     "Attempting to discount the tax-exclusive price of a line "
786
                     "Attempting to discount the tax-exclusive price of a line "
786
                     "when tax-inclusive discounts are already applied"
787
                     "when tax-inclusive discounts are already applied"
787
                 )
788
                 )
788
-            self._discount_excl_tax += discount_value
789
+        self.discounts.append((discount_value, affected_quantity, incl_tax, offer))
789
         self.consume(affected_quantity, offer=offer)
790
         self.consume(affected_quantity, offer=offer)
790
 
791
 
791
     def consume(self, quantity, offer=None):
792
     def consume(self, quantity, offer=None):
870
     # Properties
871
     # Properties
871
     # ==========
872
     # ==========
872
 
873
 
874
+    @property
875
+    def _discount_incl_tax(self):
876
+        return sum([discount[0] for discount in self.discounts if discount[2]], 0)
877
+
878
+    @_discount_incl_tax.setter
879
+    def _discount_incl_tax(self, value):
880
+        raise Exception("_discount_incl_tax kan je niet setten")
881
+
882
+    @property
883
+    def _discount_excl_tax(self):
884
+        return sum([discount[0] for discount in self.discounts if not discount[2]], 0)
885
+
886
+    @_discount_excl_tax.setter
887
+    def _discount_excl_tax(self, value):
888
+        raise Exception("_discount_excl_tax kan je niet setten")
889
+
873
     @property
890
     @property
874
     def has_discount(self):
891
     def has_discount(self):
875
         return bool(self.consumer.consumed())
892
         return bool(self.consumer.consumed())
884
 
901
 
885
     @property
902
     @property
886
     def discount_value(self):
903
     def discount_value(self):
904
+        return sum([discount[0] for discount in self.discounts], 0)
887
         # Only one of the incl- and excl- discounts should be non-zero
905
         # Only one of the incl- and excl- discounts should be non-zero
888
-        return max(self._discount_incl_tax, self._discount_excl_tax)
906
+        # return max(self._discount_incl_tax, self._discount_excl_tax)
889
 
907
 
890
     # pylint: disable=W0201
908
     # pylint: disable=W0201
891
     @property
909
     @property
928
 
946
 
929
     @property
947
     @property
930
     def line_price_excl_tax_incl_discounts(self):
948
     def line_price_excl_tax_incl_discounts(self):
931
-        if self._discount_excl_tax and self.line_price_excl_tax is not None:
932
-            return max(0, self.line_price_excl_tax - self._discount_excl_tax)
933
-        if self._discount_incl_tax and self.line_price_incl_tax is not None:
934
-            # This is a tricky situation.  We know the discount as calculated
935
-            # against tax inclusive prices but we need to guess how much of the
936
-            # discount applies to tax-exclusive prices.  We do this by
937
-            # assuming a linear tax and scaling down the original discount.
949
+        if self.line_price_excl_tax is None:
950
+            return None
951
+
952
+        excl_tax_discounts = sum(
953
+            [discount[0] for discount in self.discounts if not discount[2]], 0
954
+        )
955
+        incl_tax_discounts = sum(
956
+            [discount[0] for discount in self.discounts if discount[2]], 0
957
+        )
958
+
959
+        if excl_tax_discounts and self.line_price_excl_tax is not None:
960
+            return max(0, self.line_price_excl_tax - excl_tax_discounts)
961
+
962
+        if incl_tax_discounts and self.line_price_incl_tax is not None:
938
             return max(
963
             return max(
939
                 0,
964
                 0,
940
                 self.line_price_excl_tax
965
                 self.line_price_excl_tax
941
-                - round_half_up(self._tax_ratio * self._discount_incl_tax),
966
+                - round_half_up(self._tax_ratio * incl_tax_discounts),
942
             )
967
             )
968
+
943
         return self.line_price_excl_tax
969
         return self.line_price_excl_tax
944
 
970
 
945
     @property
971
     @property
946
     def line_price_incl_tax_incl_discounts(self):
972
     def line_price_incl_tax_incl_discounts(self):
973
+        excl_tax_discounts = sum(
974
+            [discount[0] for discount in self.discounts if not discount[2]], 0
975
+        )
976
+        incl_tax_discounts = sum(
977
+            [discount[0] for discount in self.discounts if discount[2]], 0
978
+        )
979
+
947
         # We use whichever discount value is set.  If the discount value was
980
         # We use whichever discount value is set.  If the discount value was
948
         # calculated against the tax-exclusive prices, then the line price
981
         # calculated against the tax-exclusive prices, then the line price
949
         # including tax
982
         # including tax
950
-        if self.line_price_incl_tax is not None and self._discount_incl_tax:
983
+        if self.line_price_incl_tax is not None and incl_tax_discounts:
951
             return max(0, self.line_price_incl_tax - self._discount_incl_tax)
984
             return max(0, self.line_price_incl_tax - self._discount_incl_tax)
952
-        elif self.line_price_excl_tax is not None and self._discount_excl_tax:
985
+        elif self.line_price_excl_tax is not None and excl_tax_discounts:
953
             return max(
986
             return max(
954
                 0,
987
                 0,
955
                 round_half_up(
988
                 round_half_up(
956
-                    (self.line_price_excl_tax - self._discount_excl_tax)
957
-                    / self._tax_ratio
989
+                    (self.line_price_excl_tax - excl_tax_discounts) / self._tax_ratio
958
                 ),
990
                 ),
959
             )
991
             )
960
 
992
 

+ 1
- 0
src/oscar/apps/basket/utils.py Datei anzeigen

115
         basket line is consumed for *any* offer, else only for the
115
         basket line is consumed for *any* offer, else only for the
116
         specified offer.
116
         specified offer.
117
         """
117
         """
118
+        # raise Exception("viggo is henk")
118
         if offer:
119
         if offer:
119
             self._cache(offer)
120
             self._cache(offer)
120
             available = self.available(offer)
121
             available = self.available(offer)

Laden…
Abbrechen
Speichern