Fakultas Ilmu Komputer UI

Commit 653c86f6 authored by Reka Paska Enda's avatar Reka Paska Enda
Browse files

Reka/14/mengubah perhitungan keuntungan

parent 6b28a98e
......@@ -237,6 +237,7 @@ class Migration(migrations.Migration):
('quantity', models.PositiveIntegerField(verbose_name='quantity')),
('product', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='transaction_items', to='api.Product', verbose_name='product')),
('transaction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transaction_items', to='api.Transaction', verbose_name='transaction')),
('profit', models.DecimalField(blank=True, decimal_places=2, default=Decimal('0'), max_digits=12, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.01'))], verbose_name='total profit')),
],
options={
'verbose_name': 'transaction item',
......
......@@ -394,6 +394,16 @@ class TransactionItem(db_models.Model):
validators=[validators.MinValueValidator(decimal.Decimal('0.01'))],
verbose_name=_('product price')
)
profit= db_models.DecimalField(
blank=True,
null=True,
default=decimal.Decimal('0'),
decimal_places=2,
max_digits=12,
validators=[validators.MinValueValidator(decimal.Decimal('0.01'))],
verbose_name=_('total profit')
)
hampers_price = db_models.DecimalField(
default=decimal.Decimal('0.00'),
decimal_places=2,
......
......@@ -51,6 +51,7 @@ TRANSACTION_DATA = {
}
TRANSACTION_ITEM_DATA = {
'profit' : 1000,
'quantity': 1,
}
......
......@@ -1474,6 +1474,14 @@ class TransactionItemTest(rest_framework_test.APITestCase):
))
self.assertEqual(str(transaction_item), self.product.name)
def test_transaction_item_model_profit(self):
transaction_item = models.TransactionItem.objects.create(**dict(
seeds.TRANSACTION_ITEM_DATA,
transaction=self.transaction,
product=self.product
))
self.assertEqual(transaction_item.profit, 1000)
class ProgramTest(rest_framework_test.APITestCase):
def setUp(self):
......
......@@ -120,7 +120,7 @@ class CartCheckout(rest_framework_views.APIView):
try:
with db_transaction.atomic():
product.stock -= cart_item.quantity
product.total_profit += cart_item.quantity*product.profit
profit = cart_item.quantity*product.profit
product.save()
except db_utils.IntegrityError:
is_success = False
......@@ -128,6 +128,7 @@ class CartCheckout(rest_framework_views.APIView):
transaction=transaction,
product=product,
quantity=cart_item.quantity,
profit=profit,
hampers_price=product.hampers_price,
hampers_messages=cart_item.hampers_messages
)
......
......@@ -54,4 +54,12 @@ class TransactionDetail(generics.RetrieveUpdateAPIView):
transaction_items = instance.transaction_items.all()
api_utils.return_transaction_items_to_product_stock(
transaction_items)
if serializer.validated_data['transaction_status'] == '005':
transaction_items = instance.transaction_items.all()
for transaction_item in transaction_items:
product = transaction_item.product
product.total_profit += transaction_item.profit
product.save()
return super().update(request, *args, **kwargs) # pylint: disable=no-member
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment