feat: deal group

This commit is contained in:
2024-11-08 15:58:41 +03:00
parent fced9b8101
commit 25e6cf0e7e
8 changed files with 400 additions and 57 deletions

View File

@@ -1,3 +1,4 @@
from collections import defaultdict
from io import BytesIO
from typing import List
@@ -10,11 +11,12 @@ from weasyprint import HTML, CSS
import backend.config
import constants
import models
from constants import MONTHS, ENV
from external.billing import BillingClient, CreateBillingRequestValue, CreateBillRequestSchema, CreateBillRequestItems, \
BillStatusUpdateRequest, NotificationChannel, NotifyReceivedBillRequestSchema, DeleteBillRequestSchema, \
ProductBillingDocumentPdf, ServiceBillingDocumentPdf
from models import DealBillRequest, Deal, DealProduct, DealService as DealServiceModel
from models import DealBillRequest, Deal, DealProduct, DealService as DealServiceModel, DealProductService
from schemas.billing import *
from services.base import BaseService
from services.deal import DealService
@@ -73,25 +75,54 @@ class BillingService(BaseService):
deal_service = DealService(self.session)
billing_client = BillingClient(backend.config.BILLING_API_KEY)
deal: Deal = await deal_service.get_by_id(user, request.deal_id)
basic_deal: Deal = await deal_service.get_by_id(user, request.deal_id, return_raw=True)
deals = await deal_service.get_deals_grouped(basic_deal)
billing_request_values: List[CreateBillingRequestValue] = []
for product in deal.products:
for service in product.services:
psq = defaultdict(lambda: defaultdict(lambda: 0))
sq = defaultdict(lambda: 0)
services_dict = {}
products_dict = {}
for deal in deals:
for product in deal.products:
product: DealProduct
for service in product.services:
service: DealProductService
psq[product.product_id][service.service_id] += product.quantity
products_dict[product.product_id] = product.product
services_dict[service.service_id] = service.service
for deal in deals:
for service in deal.services:
service: models.DealService
services_dict[service.service_id] = service.service
sq[service.service_id] += service.quantity
for product_id, services_ids in psq.items():
product: models.Product = products_dict[product_id]
for service_id in services_ids:
service = services_dict[service_id]
service: models.Service
quantity = psq[product_id][service_id]
billing_request_values.append(
CreateBillingRequestValue(
name=f'[{product.product.name}] - {service.service.name}',
name=f'[{product.name}] - {service.name}',
price=service.price,
amount=product.quantity
amount=quantity
)
)
for service in deal.services:
for service_id, quantity in sq.items():
service: models.Service = services_dict[service_id]
billing_request_values.append(
CreateBillingRequestValue(
name=f'{service.service.name}',
name=f'{service.name}',
price=service.price,
amount=service.quantity
amount=quantity
)
)
deal = basic_deal
create_bill_request = CreateBillRequestSchema(
listener_transaction_id=deal.id,
payer_name=deal.client.name,