159 lines
4.7 KiB
Python
159 lines
4.7 KiB
Python
import graphene
|
|
from graphene import Field, Boolean, List, String
|
|
|
|
from backend.core.commands import (
|
|
CreateInvoiceCommand,
|
|
SendInvoiceCommand,
|
|
MarkInvoicePaidCommand,
|
|
CancelInvoiceCommand
|
|
)
|
|
from backend.core.repositories import InvoiceRepository, CustomerRepository
|
|
from backend.graphql_api.types import InvoiceType
|
|
from backend.graphql_api.inputs import InvoiceCreateInput
|
|
|
|
|
|
class CreateInvoiceMutation(graphene.Mutation):
|
|
"""Mutation to create a new invoice."""
|
|
class Arguments:
|
|
input = InvoiceCreateInput(required=True)
|
|
|
|
invoice = Field(InvoiceType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, input):
|
|
try:
|
|
invoice_repo = InvoiceRepository()
|
|
customer_repo = CustomerRepository()
|
|
command = CreateInvoiceCommand(
|
|
invoice_repo=invoice_repo,
|
|
customer_repo=customer_repo,
|
|
customer_id=input.customer_id,
|
|
invoice_date=input.date,
|
|
account_ids=input.account_ids,
|
|
project_ids=input.project_ids,
|
|
total_amount=input.total_amount
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'invoice': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'invoice': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while creating the invoice"
|
|
}
|
|
|
|
|
|
class SendInvoiceMutation(graphene.Mutation):
|
|
"""Mutation to send an invoice."""
|
|
class Arguments:
|
|
id = graphene.ID(required=True)
|
|
|
|
invoice = Field(InvoiceType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, id):
|
|
try:
|
|
invoice_repo = InvoiceRepository()
|
|
command = SendInvoiceCommand(
|
|
invoice_repo=invoice_repo,
|
|
invoice_id=id
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'invoice': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'invoice': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while sending the invoice"
|
|
}
|
|
|
|
|
|
class MarkInvoicePaidMutation(graphene.Mutation):
|
|
"""Mutation to mark an invoice as paid."""
|
|
class Arguments:
|
|
id = graphene.ID(required=True)
|
|
payment_type = graphene.String(required=True)
|
|
date_paid = graphene.String()
|
|
|
|
invoice = Field(InvoiceType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, id, payment_type, date_paid=None):
|
|
try:
|
|
invoice_repo = InvoiceRepository()
|
|
command = MarkInvoicePaidCommand(
|
|
invoice_repo=invoice_repo,
|
|
invoice_id=id,
|
|
payment_type=payment_type,
|
|
date_paid=date_paid
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'invoice': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'invoice': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while marking the invoice as paid"
|
|
}
|
|
|
|
|
|
class CancelInvoiceMutation(graphene.Mutation):
|
|
"""Mutation to cancel an invoice."""
|
|
class Arguments:
|
|
id = graphene.ID(required=True)
|
|
|
|
invoice = Field(InvoiceType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, id):
|
|
try:
|
|
invoice_repo = InvoiceRepository()
|
|
command = CancelInvoiceCommand(
|
|
invoice_repo=invoice_repo,
|
|
invoice_id=id
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'invoice': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'invoice': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while cancelling the invoice"
|
|
}
|