Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
PPL Sosial
pilar
pilar-backend
Commits
653c86f6
Commit
653c86f6
authored
Jun 06, 2021
by
Reka Paska Enda
Browse files
Reka/14/mengubah perhitungan keuntungan
parent
6b28a98e
Changes
6
Hide whitespace changes
Inline
Side-by-side
api/migrations/0001_initial.py
View file @
653c86f6
...
...
@@ -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'
,
...
...
api/models.py
View file @
653c86f6
...
...
@@ -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
,
...
...
api/seeds.py
View file @
653c86f6
...
...
@@ -51,6 +51,7 @@ TRANSACTION_DATA = {
}
TRANSACTION_ITEM_DATA
=
{
'profit'
:
1000
,
'quantity'
:
1
,
}
...
...
api/tests.py
View file @
653c86f6
...
...
@@ -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
):
...
...
api/views/cart.py
View file @
653c86f6
...
...
@@ -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
)
...
...
api/views/transaction.py
View file @
653c86f6
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment