def initialise_withdrawal(self, withdrawal_amount):
     from server.utils.credit_transfer import make_withdrawal_transfer
     withdrawal = make_withdrawal_transfer(
         withdrawal_amount,
         send_account=self,
         automatically_resolve_complete=False)
     return withdrawal
Esempio n. 2
0
 def initialise_withdrawal(self, withdrawal_amount, transfer_mode):
     from server.utils.credit_transfer import make_withdrawal_transfer
     withdrawal = make_withdrawal_transfer(withdrawal_amount,
                                           send_user=self,
                                           automatically_resolve_complete=False,
                                           transfer_mode=transfer_mode,
                                           token=self.token)
     return withdrawal
    def post(self):
        # Process post data
        post_data = request.get_json()
        account_ids = []
        relist_existing = True
        if post_data:
            account_ids = post_data.get('accounts', [])
            relist_existing = post_data.get('relist_existing', True)

        payout_withdrawal_limit = g.active_organisation._minimum_vendor_payout_withdrawal_wei or 0

        if not isinstance(account_ids, list):

            response_object = {
                'message': 'Accounts parameter expects a list',
            }
            return make_response(jsonify(response_object)), 400

        if account_ids:
            vendors = db.session.query(TransferAccount)\
                .filter(TransferAccount.account_type == TransferAccountType.USER)\
                .filter(TransferAccount.id.in_(account_ids))\
                .all()

            for vendor in vendors:
                if not vendor.primary_user.has_vendor_role:

                    response_object = {
                        'message':
                        f'Transfer account with id {vendor.id} not a vendor account. Please only IDs of vendor accounts',
                    }
                    return make_response(jsonify(response_object)), 400

            selected_vendor_ids = [v.id for v in vendors]
            list_difference = [
                item for item in account_ids if item not in selected_vendor_ids
            ]
            if list_difference:
                response_object = {
                    'message':
                    f'Accounts {list_difference} were requested but do not exist',
                }
                return make_response(jsonify(response_object)), 400
        else:
            vendor_users = db.session.query(User)\
                .filter(User.has_vendor_role)\
                .all()

            vendors = [v.default_transfer_account for v in vendor_users]
            vendors = filter(lambda vendor: not vendor.is_ghost, vendors)

        output = io.StringIO()
        writer = csv.writer(output)

        writer.writerow([
            'Vendor Account ID',
            'Phone',
            'ContactName',
            'Current Balance',
            'Total Sent',
            'Total Received',
            'Approved',
            'Beneficiary',
            'Vendor',
            'InvoiceDate',
            'DueDate',
            'Transfer ID',
            'UnitAmount',
            'Payment Has Been Made',
            'Bank Payment Date',
        ])
        for v in vendors:
            if relist_existing:
                withdrawals = (CreditTransfer.query.filter(
                    CreditTransfer.sender_transfer_account_id == v.id).filter(
                        CreditTransfer.transfer_status ==
                        TransferStatusEnum.PENDING).all())
            else:
                withdrawals = []

            withdrawal_amount = Decimal(v._balance_wei or 0) / Decimal(1e16)
            if withdrawal_amount > 0 and (v._balance_wei
                                          or 0) >= payout_withdrawal_limit:
                transfer = make_withdrawal_transfer(
                    withdrawal_amount,
                    token=v.token,
                    send_user=v.primary_user,
                    sender_transfer_account=v,
                    transfer_mode=TransferModeEnum.INTERNAL,
                    require_sender_approved=False,
                    automatically_resolve_complete=False,
                )

                db.session.flush()

                withdrawals.append(transfer)

            for w in withdrawals:
                writer.writerow([
                    v.id,
                    v.primary_user.phone,
                    f'{v.primary_user.first_name or ""} {v.primary_user.last_name or ""}',
                    cents_to_dollars(v.balance),
                    cents_to_dollars(v.total_sent),
                    cents_to_dollars(v.total_received),
                    v.is_approved,
                    v.primary_user.has_beneficiary_role,
                    v.primary_user.has_vendor_role,
                    datetime.today().strftime('%Y-%m-%d'),
                    (datetime.today() +
                     timedelta(days=7)).strftime('%Y-%m-%d'),
                    w.id,
                    cents_to_dollars(w.transfer_amount),
                    '',
                    '',
                ])

        # Encode the CSV such that it can be sent as a file
        bytes_output = io.BytesIO()
        bytes_output.write(output.getvalue().encode('utf-8'))
        bytes_output.seek(0)
        return send_file(bytes_output,
                         as_attachment=True,
                         attachment_filename='vendor_payout.csv',
                         mimetype='text/csv')