|
11 | 11 | from app.models.ticket import Ticket
|
12 | 12 |
|
13 | 13 |
|
| 14 | +def calculated_sale_by_status(ticket_id, status): |
| 15 | + """calculated_sale_by_status""" |
| 16 | + query_ = OrderTicket.query.join(Order).join(Order.discount_code, isouter=True) |
| 17 | + order_ticket_ids: OrderTicket = query_.filter( |
| 18 | + OrderTicket.ticket_id == ticket_id, Order.status == status |
| 19 | + ).all() |
| 20 | + total_amount = 0 |
| 21 | + if order_ticket_ids: |
| 22 | + for order_ticket_id in order_ticket_ids: |
| 23 | + if order_ticket_id.price and order_ticket_id.quantity: |
| 24 | + discount_amount = 0 |
| 25 | + if order_ticket_id.order.discount_code: |
| 26 | + discount_amount = order_ticket_id.order.discount_code.value |
| 27 | + total_amount += ( |
| 28 | + order_ticket_id.price - discount_amount |
| 29 | + ) * order_ticket_id.quantity |
| 30 | + return total_amount |
| 31 | + |
| 32 | + |
14 | 33 | class OrderStatisticsTicketSchema(Schema):
|
15 | 34 | """
|
16 | 35 | Api schema
|
@@ -137,48 +156,13 @@ def orders_count(self, obj):
|
137 | 156 |
|
138 | 157 | def sales_count(self, obj):
|
139 | 158 | obj_id = obj.id
|
140 |
| - total = ( |
141 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
142 |
| - .join(Order.order_tickets) |
143 |
| - .filter(OrderTicket.ticket_id == obj_id) |
144 |
| - .scalar() |
145 |
| - ) |
146 |
| - draft = ( |
147 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
148 |
| - .join(Order.order_tickets) |
149 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'draft') |
150 |
| - .scalar() |
151 |
| - ) |
152 |
| - cancelled = ( |
153 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
154 |
| - .join(Order.order_tickets) |
155 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'cancelled') |
156 |
| - .scalar() |
157 |
| - ) |
158 |
| - pending = ( |
159 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
160 |
| - .join(Order.order_tickets) |
161 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'pending') |
162 |
| - .scalar() |
163 |
| - ) |
164 |
| - expired = ( |
165 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
166 |
| - .join(Order.order_tickets) |
167 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'expired') |
168 |
| - .scalar() |
169 |
| - ) |
170 |
| - placed = ( |
171 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
172 |
| - .join(Order.order_tickets) |
173 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'placed') |
174 |
| - .scalar() |
175 |
| - ) |
176 |
| - completed = ( |
177 |
| - db.session.query(func.sum(Order.amount.label('sum'))) |
178 |
| - .join(Order.order_tickets) |
179 |
| - .filter(OrderTicket.ticket_id == obj_id, Order.status == 'completed') |
180 |
| - .scalar() |
181 |
| - ) |
| 159 | + draft = calculated_sale_by_status(obj_id, 'draft') |
| 160 | + cancelled = calculated_sale_by_status(obj_id, 'cancelled') |
| 161 | + pending = calculated_sale_by_status(obj_id, 'pending') |
| 162 | + expired = calculated_sale_by_status(obj_id, 'expired') |
| 163 | + placed = calculated_sale_by_status(obj_id, 'placed') |
| 164 | + completed = calculated_sale_by_status(obj_id, 'completed') |
| 165 | + total = draft + cancelled + pending + expired + placed + completed |
182 | 166 | result = {
|
183 | 167 | 'total': total or 0,
|
184 | 168 | 'draft': draft or 0,
|
|
0 commit comments