def transferProcess(request): data = json.loads(request.body) ref_id = data['ref_id'] date = data['date'] new_warehouse = data['new_warehouse'] lines = data['lines'] myUsername = request.session.get('username') user = User.objects.get(username=myUsername) if user.branch.transfer.filter(ref_id=ref_id).exists(): tr = user.branch.transfer.latest('pk') listed_ref_id = tr.ref_id.split('-') listed_date = str(now.today()).split('-') current_code = int(listed_ref_id[3]) if listed_ref_id[1] == listed_date[0] and listed_ref_id[2] == listed_date[1]: current_code += 1 ref_id = 'T-{}-{}-{}'.format(listed_date[0], listed_date[1], str(current_code).zfill(4)) tr = Transfer() tr.ref_id = ref_id tr.date = date tr.warehouse = Warehouse.objects.get(pk=new_warehouse) tr.approved = False tr.created_by = User.objects.get(username=myUsername) tr.save() user.branch.transfer.add(tr) for line in lines: ti = Transfer_Item() ti.product = Product.objects.get(pk=int(line['code'])) ti.transfer = tr ti.remaining = int(line['remaining']) ti.transfer_quantity = int(line['quantity']) ti.save() sweetify.sweetalert(request, icon='success', title='Success!', persistent='Dismiss') return JsonResponse(0, safe=0)
def transfer_funds(from_wallet, to_wallet, amount, comment): if from_wallet is not None: if from_wallet.balance < amount: raise BillingException('Not enough funds') if to_wallet is None: raise BillingException('Receiver cannot be null') if comment is None or comment == '': raise BillingException('Missing comment') try: amount = int(amount) except: raise BillingException('Incorrect transfer amount') if amount < 0: raise BillingException('Cannot transfer negative funds') try: with transaction.atomic(): transfer = Transfer() transfer.amount = amount transfer.comment = comment transfer.sender = from_wallet transfer.receiver = to_wallet transfer.save() if from_wallet is not None: from_wallet.balance -= amount from_wallet.save() to_wallet.balance += amount to_wallet.save() except IntegrityError as e: raise BillingException('Transaction integrity error: {}'.format(e)) return True