def post(self): schema = EntrySchema() entry, errors = schema.load(request.json) if errors: return errors, 422 entry.created_by = get_jwt_identity() entry.accounting_period = datetime.now().strftime("%Y-%m") try: if not TransactionType.get(uuid=entry.tran_type): return {"msg": "The transaction type supplied doesn't exist"}, 422 if not Account.get(id=entry.credit) and not Account.get(id=entry.debit): return {"msg": "The supplied account id does not exist"}, 422 if Entry.get(reference=entry.reference): return {"msg": "The supplied reference already exists"}, 409 if Entry.get(reference=entry.cheque_number): return {"msg": "This transaction is already reversed"}, 409 if entry.tran_type == "reversal" and not Entry.get( reference=entry.cheque_number ): return {"msg": "You can only reverse an existing transaction"}, 422 if entry.tran_type == "reversal": orig = Entry.get(reference=entry.cheque_number) entry.debit = orig.credit entry.credit = orig.debit entry.amount = orig.amount entry.entity_id = orig.entity_id entry.transact() return {"msg": "entry created", "entry": schema.dump(entry).data}, 201 except Exception as e: db.session.rollback() return {"msg": e.args, "exception": e.args}, 500
def init_transaction(transaction): entry = Entry( reference=transaction.uuid, amount=transaction.amount, phone=transaction.phone, entity_id=transaction.entity_id, description=transaction.narration, tran_type=transaction.tran_type, category=transaction.category, pay_type=transaction.pay_type, ) cust_acct = Account.get(owner_id=transaction.reference) if transaction.tran_type == 'payment': entry.debit = CommissionAccount.get(code='escrow').account.id entry.credit = cust_acct.id elif transaction.tran_type == 'bill': entry.debit = cust_acct.id entry.credit = Account.get(owner_id=transaction.entity_id).id else: raise Exception("Failed to determine transaction accounts") valid, reason, status = entry.is_valid() if valid: return entry else: raise Exception(reason, status)
def init_expenditure(expenditure): entry = Entry( reference=expenditure.uuid, amount=expenditure.amount, phone=expenditure.phone, entity_id=expenditure.entity_id, description=expenditure.narration, tran_type=expenditure.category, cheque_number=expenditure.reference, category=expenditure.vendor_id, pay_type=expenditure.pay_type, ) if expenditure.pay_type == 'credit': entry.debit = CommissionAccount.get(code='credit').account.id entry.credit = CommissionAccount.get(code=expenditure.category).account.id entry.reference = entry.reference + '-credit' elif expenditure.on_credit and expenditure.pay_type != 'credit' and expenditure.credit_status in ('PAID','PARTIAL'): entry.debit = Account.get(owner_id=expenditure.pay_type).id entry.credit = CommissionAccount.get(code='credit').account.id else: entry.debit = Account.get(owner_id=expenditure.pay_type).id entry.credit = CommissionAccount.get(code=expenditure.category).account.id return entry
def init_item_log(item_log): entry = Entry( reference=item_log.uuid, amount=item_log.amount, phone='', entity_id=item_log.entity_id, description=item_log.item_id, tran_type=item_log.category, category=item_log.item_id, pay_type=item_log.pay_type, ) if item_log.pay_type == 'credit': entry.debit = CommissionAccount.get(code='credit').account.id entry.credit = Account.get(owner_id=item_log.debit).id entry.reference = entry.reference + '-credit' elif item_log.on_credit and item_log.pay_type != 'credit' and item_log.credit_status == 'PAID': entry.debit = Account.get(owner_id=item_log.pay_type).id entry.credit = CommissionAccount.get(code='credit').account.id else: entry.debit = Account.get(owner_id=item_log.pay_type).id entry.credit = Account.get(owner_id=item_log.debit).id valid, reason, status = entry.is_valid() if valid: return entry else: raise Exception(reason, status)
def init_expense(expense): entry = Entry( reference=expense.uuid, amount=expense.amount, phone='', entity_id=expense.entity_id, description=expense.narration, tran_type='expense', cheque_number=expense.reference, category=expense.item, pay_type=expense.pay_type, ) if expense.pay_type == 'credit': entry.debit = CommissionAccount.get(code='credit').account.id entry.credit = CommissionAccount.get(code='expenses').account.id entry.reference = entry.reference + '-credit' elif expense.on_credit and expense.pay_type != 'credit' and expense.credit_status in ('PAID','PARTIAL'): entry.debit = Account.get(owner_id=expense.pay_type).id entry.credit = CommissionAccount.get(code='credit').account.id else: entry.debit = Account.get(owner_id=expense.pay_type).id entry.credit = CommissionAccount.get(code='expenses').account.id valid, reason, status = entry.is_valid() if valid: return entry else: raise Exception(reason, status)
def is_valid(self): """validate the object""" if not TransactionType.get(uuid=self.tran_type): return False, {"msg": "The transaction type {0} doesn't exist".format(self.tran_type)}, 422 if not Account.get(id=self.credit) and not Account.get(id=self.debit): return False, {"msg": "The supplied account id does not exist"}, 422 if Entry.get(reference=self.reference): return False, {"msg": "The supplied reference already exists"}, 409 if Entry.get(reference=self.cheque_number): return False, {"msg": "This transaction is already reversed"}, 409 if self.tran_type == "reversal" and not Entry.get( reference=self.cheque_number ): return False, {"msg": "You can only reverse an existing transaction"}, 422 # check balance account = Account.get(id=self.debit) bal_after = int(account.balance) - int(self.amount) app.logger.info(self.amount) if account.minimum_balance is not None and float(bal_after) < float(account.minimum_balance): return False, {"msg": "Insufficient balance on {0} account {1}".format(account.name,commas(account.balance))}, 409 if self.tran_type == "reversal": orig = Entry.get(tranid=self.cheque_number) self.debit = orig.credit self.credit = orig.debit self.amount = orig.amount self.entity_id = orig.entity_id return True, self, 200
def post(self): schema = VendorSchema() vendor, errors = schema.load(request.json) if errors: return errors, 422 vendor.created_by = get_jwt_identity() try: if Vendor.get(name=vendor.name): return {"msg": "The supplied name already exists"}, 409 if Vendor.get(email=vendor.email): return {"msg": "The supplied email already exists"}, 409 account = Account( owner_id=vendor.uuid, acc_type="vendor", created_by=get_jwt_identity(), minimum_balance=0.0,group=vendor.uuid ) db.session.add(vendor) db.session.add(account) db.session.commit() return {"msg": "vendor created", "vendor": schema.dump(vendor).data}, 201 except Exception as e: return {"msg": e.args}, 500
def get_entries(self): entries = [self] charge = Tarriff.get( tran_type=self.tran_type, payment_type=self.pay_type, entity_id="ALL" ) if charge: charge_fee = get_charge_fee(charge.code, self.amount) splits = ChargeSplit.query.filter_by(code=charge.code).all() for split in splits: name = "charge-" + str(splits.index(split) + 1) fee = float(split.percentage / 100) * float(charge_fee) e = Entry( reference=self.reference, amount=fee, debit=self.debit, credit=Account.get(code=split.account_code).id, description=charge.name, tran_type="charge", phone=self.phone, pay_type=self.pay_type, utility_code=name, entity_id=self.entity_id, ) entries.append(e) if self.tran_type == 'payment': payment_entry = Entry( reference=self.reference, amount=self.amount, description=self.description, tran_type=self.tran_type, phone=self.phone, pay_type=self.pay_type, entity_id=self.entity_id ) payment_entry.debit = Account.get(owner_id=self.entity_id).id payment_entry.credit = Account.get(owner_id=self.pay_type).id entries.append(payment_entry) return entries
def sms(self, phone): try: from autoshop.commons.messaging import send_sms_async if self.tran_type == "payment": msg = ( "Hello " + Account.get(id=self.credit).name + ", your payment of UGX " + "{:,}".format(float(self.amount)) + " has been received " ) else: msg = ( "Hello " + Account.get(id=self.debit).name + ", please note that you have a bill of UGX " + "{:,}".format(float(self.amount)) + ". " ) send_sms_async(phone, msg) except Exception: pass
def post(self): schema = TransactionSchema() transaction, errors = schema.load(request.json) if errors: return errors, 422 transaction.created_by = get_jwt_identity() transaction.vendor_id = User.get(id=transaction.created_by).company_id if not Customer.get(uuid=transaction.reference): return {"msg": "This reference does not exist in our customer list"}, 422 if Transaction.get(tranid=transaction.tranid, vendor_id=transaction.vendor_id): return {"msg": "The supplied transaction already exists"}, 409 if not TransactionType.get(uuid=transaction.tran_type): return {"msg": "Unknown transaction type supplied"}, 422 if not PaymentType.get(uuid=transaction.pay_type): return {"msg": "Unknown payment type supplied"}, 422 try: if transaction.is_synchronous: transaction.processed = True transaction.status = "SUCCESS" cust_acct = Account.get(owner_id=transaction.reference) transaction.entity_id = cust_acct.group entry = Entry.init_transaction(transaction) transaction.save() entry.transact() else: # just save transaction, and update later in background service transaction.save() result_schema = TransactionViewSchema() return ( { "msg": "transaction created", "transaction": result_schema.dump(transaction).data, }, 201, ) except Exception as e: transaction.status = 'FAILED' transaction.reason = str(e.args[0]) transaction.update() return {"msg": e.args[0]}, e.args[1] if len(e.args) > 1 else 500
def post(self): try: schema = CustomerSchema() customer, errors = schema.load(request.json) if errors: return errors, 422 if not customer.created_by: customer.created_by = get_jwt_identity() app.logger.info(customer.entity_id) if not Entity.get(uuid=customer.entity_id): return {"msg": "The supplied entity id does not exist"}, 422 if not CustomerType.get(uuid=customer.type_id, entity_id=customer.entity_id): return { "msg": "The supplied customer type does not exist" }, 422 if Customer.get(phone=customer.phone, entity_id=customer.entity_id): return { "msg": "The supplied customer phone already exists" }, 409 else: customer.log("A") account = Account(owner_id=customer.uuid, acc_type="customer", created_by=get_jwt_identity(), group=customer.entity_id, minimum_balance=10000000) db.session.add(account) db.session.commit() return ( { "msg": "customer created", "customer": schema.dump(customer).data }, 201, ) except Exception as e: return {"msg": e.args}, 500
def batch(self): """Execute multiple requests, submitted as a batch. :statuscode 207: Multi status""" try: requests = json.loads(request.data) except ValueError as e: return {"msg": e}, 400 responses = [] for index, req in enumerate(requests): name = req["name"] email = req["email"] phone = req["phone"] address = req["address"] entity = Entity( name=name, email=email, phone=phone, address=address, created_by=get_jwt_identity(), ) account = Account( owner_id=entity.uuid, acc_type="entity", created_by=get_jwt_identity(), group=entity.uuid, minimum_balance=0.0, ) if Entity.get(name=name): return {"msg": "The supplied name already exists " + name}, 409 else: db.session.add(entity) db.session.add(account) db.session.commit() responses.append({"msg": "entities created"}) return json.dumps(responses), 207
def seed(): import datetime fileDir = os.path.dirname(os.path.realpath('__file__')) filename = os.path.join(fileDir, 'autoshop/sql/makes.sql') file = open(filename) execute_sql(file) entity = Entity( name='Floben Autoshop', email='*****@*****.**', phone='256770443322', address='Ntinda', created_by=1, date_created=datetime.datetime.now(), ) account = Account(owner_id=entity.uuid, acc_type='entity', created_by=1, group=entity.uuid) db.session.add(entity) db.session.add(account) ctype = CustomerType(uuid='in_fleet', name='IN FLEET', created_by=1, entity_id=entity.uuid) ctype1 = CustomerType(uuid='out_fleet', name='OUT FLEET', created_by=1, entity_id=entity.uuid) db.session.add(ctype1) db.session.add(ctype) db.session.commit()
def account_name(self): """Get the tarriff.""" return Account.get(uuid=self.account_code).name