Dropped Taxes table and changed script to modify ledgers.

Profit and loss fully done in new style.
This commit is contained in:
Tanshu
2012-10-18 00:07:32 +05:30
parent a9aa9bd6ae
commit 6edd04b817
4 changed files with 144 additions and 173 deletions

View File

@ -1,3 +1,5 @@
UPDATE entities_ledgers SET type = 11 WHERE type = 13;
DROP TABLE Entities_Taxes;
UPDATE entities_vouchers SET date = date || ".000000", lasteditdate = lasteditdate || ".000000", creationdate = creationdate || ".000000"; UPDATE entities_vouchers SET date = date || ".000000", lasteditdate = lasteditdate || ".000000", creationdate = creationdate || ".000000";
UPDATE entities_attendances SET date = date || ".000000", creationdate = creationdate || ".000000"; UPDATE entities_attendances SET date = date || ".000000", creationdate = creationdate || ".000000";
UPDATE entities_fingerprints SET date = date || ".000000"; UPDATE entities_fingerprints SET date = date || ".000000";

View File

@ -334,19 +334,19 @@ class LedgerType:
@classmethod @classmethod
def list(cls): def list(cls):
list = [] list = []
list.append(LedgerType(1, 'Cash', True, True, True, 20, True)) list.append(LedgerType(1, 'Cash', True, True, True, 10, True))
list.append(LedgerType(2, 'Purchase', False, True, True, 20, True)) list.append(LedgerType(2, 'Purchase', False, True, True, 20, True))
list.append(LedgerType(3, 'Sale', False, False, True, 10, True)) list.append(LedgerType(3, 'Sale', False, False, True, 10, True))
list.append(LedgerType(4, 'Assets', True, True, False, 10, True)) list.append(LedgerType(4, 'Assets', True, True, False, 20, True))
list.append(LedgerType(5, 'Capital', True, False, False, 10, True)) list.append(LedgerType(5, 'Capital', True, False, False, 70, True))
list.append(LedgerType(6, 'Debtors', True, True, True, 30, True)) list.append(LedgerType(6, 'Debtors', True, True, True, 30, True))
list.append(LedgerType(7, 'Expenses', False, True, True, 40, True)) list.append(LedgerType(7, 'Expenses', False, True, True, 40, True))
# list.append(LedgerType(8, 'Discount', False, False, True, 30, True)) # list.append(LedgerType(8, 'Discount', False, False, True, 30, True))
list.append(LedgerType(9, 'Creditors', True, False, True, 30, True)) list.append(LedgerType(9, 'Creditors', True, False, True, 60, True))
list.append(LedgerType(10, 'Salary', True, True, True, 30, False)) list.append(LedgerType(10, 'Salary', True, True, True, 40, False))
list.append(LedgerType(11, 'Liabilities', True, False, True, 30, True)) list.append(LedgerType(11, 'Liabilities', True, False, True, 50, True))
list.append(LedgerType(12, 'Revenue', False, False, True, 30, True)) list.append(LedgerType(12, 'Revenue', False, False, True, 30, True))
# list.append(LedgerType(13, 'Tax', True, False, True, 30, True)) list.append(LedgerType(13, 'Tax', True, False, True, 80, True))
# list.append(LedgerType(14, 'Total', False, False, False, 900, False)) # list.append(LedgerType(14, 'Total', False, False, False, 900, False))
# list.append(LedgerType(15, 'Net', False, False, False, 1000, False)) # list.append(LedgerType(15, 'Net', False, False, False, 1000, False))
return list return list

View File

@ -9,6 +9,38 @@
<button ng-click="show()">Show</button> <button ng-click="show()">Show</button>
</div> </div>
</div> </div>
<div class="control-group">
<label for="gvGridProfitLoss" class="control-label"></label>
<div class="controls">
<table id="gvGridProfitLoss" class="clean-table">
<thead>
<tr>
<th>Group</th>
<th>Name</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in info.ProfitLossBody">
<td>{{item.Group}}</td>
<td>{{item.Name}}</td>
<td class="right">{{item.SubAmount}}</td>
<td class="right">{{item.Amount}}</td>
</tr>
</tbody>
<tfoot>
<tr ng-repeat="item in info.ProfitLossFooter">
<td>{{item.Group}}</td>
<td>{{item.Name}}</td>
<td class="right">{{item.SubAmount}}</td>
<td class="right">{{item.Amount}}</td>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="control-group"> <div class="control-group">
<label for="gvGrid" class="control-label"></label> <label for="gvGrid" class="control-label"></label>
@ -16,26 +48,30 @@
<table id="gvGrid" class="clean-table"> <table id="gvGrid" class="clean-table">
<thead> <thead>
<tr> <tr>
<th>Group</th>
<th>Name</th> <th>Name</th>
<th>Expenses</th> <th>Amount</th>
<th>Incomes</th> <th>Total</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr ng-repeat="item in info.Body"> <tr ng-repeat="item in info.BalanceSheetBody">
<td>{{item.Name}}</a></td> <td>{{item.Group}}</td>
<td class="right">{{item.Expense}}</td> <td>{{item.Name}}</td>
<td class="right">{{item.Income}}</td> <td class="right">{{item.SubAmount}}</td>
<td class="right">{{item.Amount}}</td>
</tr> </tr>
</tbody> </tbody>
<tfoot> <tfoot>
<tr ng-repeat="item in info.Footer"> <tr ng-repeat="item in info.BalanceSheetFooter">
<td>{{item.Name}}</a></td> <td>{{item.Group}}</td>
<td class="right">{{item.Expense}}</td> <td>{{item.Name}}</td>
<td class="right">{{item.Income}}</td> <td class="right">{{item.SubAmount}}</td>
<td class="right">{{item.Amount}}</td>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>
</div> </div>
</div> </div>
</form> </form>

View File

@ -1,10 +1,10 @@
import datetime import datetime
from sqlalchemy.sql.expression import func from sqlalchemy.sql.expression import func, desc
from pyramid.view import view_config from pyramid.view import view_config
from brewman.models import DBSession from brewman.models import DBSession
from brewman.models.master import CostCenter, Ledger, LedgerType from brewman.models.master import CostCenter, LedgerType, LedgerBase
from brewman.models.voucher import Voucher, Journal, VoucherType, Inventory from brewman.models.voucher import Voucher, Journal, VoucherType, Inventory
from brewman.views.services.session import services_session_period_start, services_session_period_finish from brewman.views.services.session import services_session_period_start, services_session_period_finish
@ -17,210 +17,143 @@ def get_html(request):
@view_config(request_method='GET', route_name='profit_loss', renderer='json', xhr=True) @view_config(request_method='GET', route_name='profit_loss', renderer='json', xhr=True)
def get_profit_loss(request): def get_profit_loss(request):
print('profit loss')
start_date = request.GET.get('StartDate', None) start_date = request.GET.get('StartDate', None)
finish_date = request.GET.get('FinishDate', None) finish_date = request.GET.get('FinishDate', None)
if start_date and finish_date: if start_date and finish_date:
report = {'StartDate': start_date, 'FinishDate': finish_date, 'Body': [], 'Footer': []} report = {'StartDate': start_date, 'FinishDate': finish_date}
start_date = datetime.datetime.strptime(start_date, '%d-%b-%Y') start_date = datetime.datetime.strptime(start_date, '%d-%b-%Y')
finish_date = datetime.datetime.strptime(finish_date, '%d-%b-%Y') finish_date = datetime.datetime.strptime(finish_date, '%d-%b-%Y')
net_profit, opening_stock, closing_stock = build_profit_loss(request, report, start_date, finish_date)
report['Body'].append([{}, {}, {}]) net_profit, opening_stock, closing_stock,\
report['Footer'].append([{}, {}, {}]) report['ProfitLossBody'], report['ProfitLossFooter'] = build_profit_loss(request, start_date, finish_date)
build_balance_sheet(request, report, net_profit, opening_stock, closing_stock, start_date, finish_date) report['BalanceSheetBody'], report['BalanceSheetFooter'] = build_balance_sheet(request, net_profit,
opening_stock, closing_stock, start_date, finish_date)
return report return report
else: else:
return {'StartDate': services_session_period_start(request), return {'StartDate': services_session_period_start(request),
'FinishDate': services_session_period_finish(request), 'Body': [], 'Footer': {}} 'FinishDate': services_session_period_finish(request), 'Body': [], 'Footer': {}}
def build_profit_loss(request, report, start_date, finish_date): def build_profit_loss(request, start_date, finish_date):
report = []
groups = dict()
type_list = [] type_list = []
for item in LedgerType.list(): for item in LedgerType.list():
if not item.balance_sheet: if not item.balance_sheet:
type_list.append(item.id) type_list.append(item.id)
query = DBSession.query(Ledger, func.sum(Journal.amount * Journal.debit))\ amount_sum = func.sum(Journal.amount * Journal.debit)
query = DBSession.query(LedgerBase, amount_sum)\
.join(Journal.voucher).join(Journal.ledger)\ .join(Journal.voucher).join(Journal.ledger)\
.filter(Voucher.date >= start_date)\ .filter(Voucher.date >= start_date)\
.filter(Voucher.date <= finish_date)\ .filter(Voucher.date <= finish_date)\
.filter(Voucher.type != VoucherType.by_name('Issue').id)\ .filter(Voucher.type != VoucherType.by_name('Issue').id)\
.filter(Ledger.type.in_(type_list))\ .filter(LedgerBase.type.in_(type_list))\
.group_by(Ledger)\ .group_by(LedgerBase).order_by(LedgerBase.type).order_by(desc(func.abs(amount_sum))).all()
.order_by(Ledger.type)\
.all()
# Get opening / closing stock # Get opening / closing stock
opening_stock, closing_stock = get_stocks(start_date, finish_date) opening_stock, closing_stock = get_stocks(start_date, finish_date)
total_debit = opening_stock opening_stock = 0 if opening_stock is None else opening_stock
total_credit = closing_stock closing_stock = 0 if closing_stock is None else closing_stock
report['Body'].append({'Name': 'Opening / Closing Stock', 'Class': 'sub_heading unposted', total_amount = (opening_stock - closing_stock) * -1
'Expense': "\u20B9 {0:,.2f}".format(total_debit), report.append(
'Income': "\u20B9 {0:,.2f}".format(total_credit)}) {'Name': 'Opening Stock', 'SubAmount': "\u20B9 {0:,.2f}".format(opening_stock * -1), 'Order': 20.0001})
report.append({'Name': 'Closing Stock', 'SubAmount': "\u20B9 {0:,.2f}".format(closing_stock), 'Order': 29})
last_type = LedgerType(id=0, name="") purchase_group = LedgerType.by_id(2)
type_debit = 0 groups[purchase_group.id] = {'Group': purchase_group.name, 'Amount': total_amount, 'Order': purchase_group.order}
type_credit = 0 counter = 0
for ledger, amount in query: for ledger, amount in query:
# Add Subtotals # Add Items
ledger_type = LedgerType.by_id(ledger.type) ledger_type = LedgerType.by_id(ledger.type)
if last_type.id != ledger_type.id: amount = amount * -1
if last_type.id != 0: total_amount += amount
if last_type.debit:
type_debit = "\u20B9 {0:,.2f}".format(type_debit)
type_credit = ""
else:
type_credit = "\u20B9 {0:,.2f}".format(type_credit)
type_debit = ""
report['Body'].append({'Name': last_type.name, 'Class': 'sub_heading unposted', 'Expense': type_debit,
'Income': type_credit})
type_debit = 0
type_credit = 0
last_type = ledger_type
# Add Items
if ledger_type.debit:
debit = "\u20B9 {0:,.2f}".format(amount)
credit = ""
type_debit += amount
total_debit += amount
else:
credit = "\u20B9 {0:,.2f}".format(amount * -1)
debit = ""
type_credit += (amount * -1)
total_credit += (amount * -1)
if amount != 0: if amount != 0:
report['Body'].append({'Name': ledger.name, 'Expense': debit, 'Income': credit}) counter += .001
# Add Last Subtotal report.append(
if last_type.debit: {'Name': ledger.name, 'SubAmount': "\u20B9 {0:,.2f}".format(amount),
type_debit = "\u20B9 {0:,.2f}".format(type_debit) 'Order': ledger_type.order + counter})
type_credit = "" if ledger_type.id in groups:
else: groups[ledger_type.id]['Amount'] += amount
type_credit = "\u20B9 {0:,.2f}".format(type_credit) else:
type_debit = "" groups[ledger_type.id] = {'Group': ledger_type.name, 'Amount': amount, 'Order': ledger_type.order}
report['Body'].append( # Add Subtotals
{'Name': last_type.name, 'Class': 'sub_heading unposted', 'Expense': type_debit, 'Income': type_credit}) for item in groups.values():
item['Amount'] = "\u20B9 {0:,.2f}".format(item['Amount'])
report.append(item)
# Add Totals
report['Footer'].append({'Name': 'Total', 'Expense': type_debit, 'Income': type_credit})
footer = []
# Add Net # Add Net
net_profit = total_credit - total_debit if total_amount > 0:
if total_debit > total_credit: footer.append({'Name': 'Net Profit', 'Amount': "\u20B9 {0:,.2f}".format(total_amount), 'Order': 100})
total_credit = "\u20B9 {0:,.2f}".format(total_debit - total_credit)
total_debit = ""
net_name = "Net Loss"
else: else:
total_debit = "\u20B9 {0:,.2f}".format(total_credit - total_debit) footer.append({'Name': 'Net Loss', 'Amount': "\u20B9 {0:,.2f}".format(total_amount), 'Order': 100})
total_credit = ""
net_name = "Net Profit"
report['Footer'].append({'Name': net_name, 'Expense': total_debit, 'Income': total_credit}) return total_amount, opening_stock, closing_stock, sorted(report, key=lambda d: d['Order']), footer
return net_profit, opening_stock, closing_stock
def build_balance_sheet(request, report, net_profit, opening_stock, closing_stock, start_date, finish_date): def build_balance_sheet(request, net_profit, opening_stock, closing_stock, start_date, finish_date):
report = []
groups = dict()
# Add Net Profit / Loss # Add Net Profit / Loss
if net_profit < 0: net_profit *= -1
debit = "\u20B9 {0:,.2f}".format(net_profit * -1) total_amount = net_profit
credit = "" report.append({'Name': 'Net Loss' if net_profit >= 0 else 'Net Profit',
name = "Net Loss" 'SubAmount': "\u20B9 {0:,.2f}".format(net_profit), 'Order': 79})
total_debit = net_profit * -1
total_credit = 0
else:
credit = "\u20B9 {0:,.2f}".format(net_profit)
debit = ""
name = "Net Profit"
total_credit = net_profit
total_debit = 0
report['Body'].append({'Name': name, 'Liability': credit, 'Asset': debit})
# Add Accumulated Profit / Loss # Add Accumulated Profit / Loss
accumulated_profit = get_accumulated_profit(opening_stock, start_date, finish_date) accumulated_profit = get_accumulated_profit(opening_stock, start_date)
if accumulated_profit < 0: report.append({'Name': 'Accumulated Loss' if accumulated_profit >= 0 else 'Accumulated Profit',
debit = "\u20B9 {0:,.2f}".format(accumulated_profit * -1) 'SubAmount': "\u20B9 {0:,.2f}".format(accumulated_profit), 'Order': 78})
credit = "" total_amount += accumulated_profit
name = "Accumulated Loss"
total_debit += accumulated_profit * -1
else:
credit = "\u20B9 {0:,.2f}".format(accumulated_profit)
debit = ""
name = "Accumulated Profit"
total_credit += accumulated_profit
report['Body'].append({'Name': name, 'Liability': credit, 'Asset': debit}) capital_group = LedgerType.by_id(5)
groups[capital_group.id] = {'Group': capital_group.name, 'Amount': total_amount, 'Order': capital_group.order}
# Add Closing Stock total_amount += closing_stock
if closing_stock > 0: report.append({'Name': 'Closing Stock', 'SubAmount': "\u20B9 {0:,.2f}".format(closing_stock), 'Order': 20.001})
total_debit += closing_stock
report['Body'].append({'Name': 'Closing Stock', 'Asset': "\u20B9 {0:,.2f}".format(closing_stock)}) asset_group = LedgerType.by_id(4)
groups[asset_group.id] = {'Group': asset_group.name, 'Amount': closing_stock, 'Order': asset_group.order}
type_list = [] type_list = []
for item in LedgerType.list(): for item in LedgerType.list():
if item.balance_sheet: if item.balance_sheet:
type_list.append(item.id) type_list.append(item.id)
query = DBSession.query(Ledger, func.sum(Journal.amount * Journal.debit))\ amount_sum = func.sum(Journal.amount * Journal.debit)
query = DBSession.query(LedgerBase, amount_sum)\
.join(Journal.voucher).join(Journal.ledger)\ .join(Journal.voucher).join(Journal.ledger)\
.filter(Voucher.date <= finish_date)\ .filter(Voucher.date <= finish_date)\
.filter(Voucher.type != VoucherType.by_name('Issue').id)\ .filter(Voucher.type != VoucherType.by_name('Issue').id)\
.filter(Ledger.type.in_(type_list))\ .filter(LedgerBase.type.in_(type_list))\
.group_by(Ledger)\ .group_by(LedgerBase).order_by(LedgerBase.type).order_by(desc(func.abs(amount_sum))).all()
.order_by(Ledger.type)\
.all()
last_type = LedgerType(id=0, name="") counter = 0
type_debit = 0 sss = 0
type_credit = 0
for ledger, amount in query: for ledger, amount in query:
# Add Subtotals # Add Items
ledger_type = LedgerType.by_id(ledger.type) ledger_type = LedgerType.by_id(ledger.type)
if last_type.id != ledger_type.id: sss += amount
if last_type.id != 0: total_amount += amount
if last_type.debit:
type_debit = "\u20B9 {0:,.2f}".format(type_debit)
type_credit = ""
else:
type_credit = "\u20B9 {0:,.2f}".format(type_credit)
type_debit = ""
report['Body'].append({'Name': last_type.name, 'Liability': type_credit, 'Asset': type_debit})
type_debit = 0
type_credit = 0
last_type = ledger_type
# Add Items
if ledger_type.debit:
debit = "\u20B9 {0:,.2f}".format(amount)
credit = ""
type_debit += amount
total_debit += amount
else:
credit = "\u20B9 {0:,.2f}".format(amount * -1)
debit = ""
type_credit += (amount * -1)
total_credit += (amount * -1)
if amount != 0: if amount != 0:
report['Body'].append({'Name': ledger.name, 'Liability': credit, 'Asset': debit}) counter += .001
report.append(
{'Name': ledger.name, 'SubAmount': "\u20B9 {0:,.2f}".format(amount),
'Order': ledger_type.order + counter})
if ledger_type.id in groups:
groups[ledger_type.id]['Amount'] += amount
else:
groups[ledger_type.id] = {'Group': ledger_type.name, 'Amount': amount, 'Order': ledger_type.order}
# Add Last Subtotal # Add Subtotals
if last_type.debit: for item in groups.values():
type_debit = "\u20B9 {0:,.2f}".format(type_debit) item['Amount'] = "\u20B9 {0:,.2f}".format(item['Amount'])
type_credit = "" report.append(item)
else:
type_credit = "\u20B9 {0:,.2f}".format(type_credit)
type_debit = ""
report['Body'].append( footer = [{'Name': 'Total', 'Amount': "\u20B9 {0:,.2f}".format(total_amount), 'Order': 100}]
{'Name': last_type.name, 'Liability': type_credit, 'Asset': type_debit, 'class': 'sub_heading unposted'})
report['Footer'].append({'Name': 'Total', 'Liability': "\u20B9 {0:,.2f}".format(total_credit), return sorted(report, key=lambda d: d['Order']), footer
'Asset': "\u20B9 {0:,.2f}".format(total_debit)})
def get_stocks(startDate, finishDate): def get_stocks(startDate, finishDate):
@ -239,7 +172,7 @@ def get_stocks(startDate, finishDate):
return opening_stock, closing_stock return opening_stock, closing_stock
def get_accumulated_profit(opening_stock, startDate, finishDate): def get_accumulated_profit(opening_stock, start_date):
type_list = [] type_list = []
for item in LedgerType.list(): for item in LedgerType.list():
if not item.balance_sheet: if not item.balance_sheet:
@ -247,10 +180,10 @@ def get_accumulated_profit(opening_stock, startDate, finishDate):
accumulated_profit = DBSession.query(func.sum(Journal.amount * Journal.debit))\ accumulated_profit = DBSession.query(func.sum(Journal.amount * Journal.debit))\
.join(Journal.voucher).join(Journal.ledger)\ .join(Journal.voucher).join(Journal.ledger)\
.filter(Voucher.date < startDate)\ .filter(Voucher.date < start_date)\
.filter(Voucher.type != VoucherType.by_name('Issue').id)\ .filter(Voucher.type != VoucherType.by_name('Issue').id)\
.filter(Ledger.type.in_(type_list))\ .filter(LedgerBase.type.in_(type_list))\
.scalar() .scalar()
if accumulated_profit is None: if accumulated_profit is None:
accumulated_profit = 0 accumulated_profit = 0
return (accumulated_profit * -1) + opening_stock return accumulated_profit - opening_stock