Last active
March 26, 2023 17:42
-
-
Save saqib-mehmood-tkxel/d1645d46db77ac4f309f3d999ff64694 to your computer and use it in GitHub Desktop.
Code sample of Python/Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplyCredit(APIView): | |
permission_classes = [IsAuthenticated] | |
def post(self, request): | |
""" | |
Apply credit on bill.com | |
""" | |
try: | |
audit_log = AuditLog(user=request.user) | |
billee_id = request.POST.get('billee_id') | |
if not billee_id or billee_id == 'undefined': | |
return CustomResponse(message="Please select Billee").error() | |
if not Billee.objects.filter(id=billee_id).exists(): | |
return CustomResponse(message="Invalid Billee").error() | |
billee_credit = True | |
credit_amount = request.POST.get('credit_amount') | |
if not credit_amount: | |
return CustomResponse(message="Please enter credit amount").error() | |
amount_reason = request.POST.get('amount_reason') | |
if not amount_reason: | |
return CustomResponse(message="Please enter amount reason").error() | |
credit_amount = round(float(credit_amount), 2) | |
#Create customer and invoice in bill.com | |
flag = apply_credits_to_customers(billee.id, credit_amount, False, None) | |
# if invoice credit is already applied against billee add new amount to credit amount. | |
if flag: | |
if billee.invoice_credit: | |
invoice_credit = billee.invoice_credit | |
prev_obj = copy.copy(invoice_credit) | |
invoice_credit.credit_amount = credit_amount + float(invoice_credit.credit_amount) | |
invoice_credit.amount_reason = invoice_credit.amount_reason + ', ' + amount_reason | |
invoice_credit.save() | |
# make an entry in audit log | |
audit_log.edit(prev_obj=prev_obj, new_obj=invoice_credit) | |
else: | |
# else create new InvoiceCredit obj and add the credit amount | |
invoice_credit = InvoiceCredit.objects.create(credit_amount=credit_amount, amount_reason=amount_reason) | |
billee.invoice_credit = invoice_credit | |
billee.save() | |
# make an entry in audit log | |
audit_log.create(instance=billee.invoice_credit, extra_str=", General credit applied") | |
return CustomResponse(message="Credit Applied Successfully!").response() | |
else: | |
return CustomResponse( | |
message="Billee doesn't exist on Bill.com or there maybe some technical issue").error() | |
except: | |
traceback.print_exc() | |
return CustomResponse(message=SOMETHING_WENT_WRONG).error() | |
def get(self, request): | |
return CustomResponse(message=INVALID_METHOD).error() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment