def customers_full(request): offset = int(request.GET.get('year_offset', 0)) primary = request.user.account.company cutoff = primary.account.current_cutoff_date(offset) customers = TradeAccount.objects.filter(supplier=primary) customer_ids = customers.values_list('id', flat=True) profits = YearData.objects.filter(label=YearData.PROFITS, year=cutoff.year, account_type=TradeAccount.content_type(), account_id__in=customer_ids) sales = YearData.objects.filter(label=YearData.SALES, year=cutoff.year, account_type=TradeAccount.content_type(), account_id__in=customer_ids) accounts = {} for s in sales: if s.total > 0: account = accounts.get(s.account_id, {}) account['sales'] = s.total account['name'] = s.account.customer.name accounts[s.account_id] = account for p in profits: if p.total > 0: account = accounts.get(p.account_id, {}) account['profit'] = p.total account['name'] = p.account.customer.name accounts[p.account_id] = account accounts = sorted(accounts.values(), key=lambda k: k.get('profit', 0), reverse=True) return render_to_response('task/management/customers_full.html', dict(accounts=accounts, year=cutoff.year), context_instance=RequestContext(request))
def update_customer_data(self, data, label, year): # delete orphans year_data = YearData.objects.filter(account_type=TradeAccount.content_type(), label=label, year=year) has_data = set() for d in year_data: if d.account == None: d.delete() else: has_data.add(d.account.id) transaction.commit() # ensure all accounts have year data all_items = set(TradeAccount.objects.filter(supplier=self.primary).values_list('id', flat=True)) no_data = all_items - has_data for account_id in no_data: YearData.objects.create(label=label, year=year, account_type=TradeAccount.content_type(), account_id=account_id) transaction.commit() # update year_data = YearData.objects.filter(account_type=TradeAccount.content_type(), label=label, year=year) for d in year_data: if d.account.supplier == self.primary: for month in range(1, 13): key = (d.account.customer.id, month) d.set(month, data.get(key, 0)) d.save() transaction.commit()
def receivables_full(request): primary = request.user.account.company account_ids = TradeAccount.objects.filter( supplier=primary, debt__gt=0).order_by('-debt').values_list('id', flat=True) age_map = { 'a120': TradeAccount.RECEIVABLES_120, 'a90': TradeAccount.RECEIVABLES_090, 'a60': TradeAccount.RECEIVABLES_060, 'a30': TradeAccount.RECEIVABLES_030 } customers = {} for age_key, age in age_map.items(): data = AccountData.objects.filter( label=age, date=datetime.min, account_type=TradeAccount.content_type(), account_id__in=account_ids) for d in data: customer = customers.get(d.account_id, {}) customer['name'] = d.account.customer.name customer['total'] = d.account.debt customer[age_key] = d.value customers[d.account_id] = customer #customers = customers.values() customers = sorted(customers.values(), key=itemgetter('total'), reverse=True) return render_to_response('task/accounting/receivables_full.html', dict(customers=customers, today=date.today()), context_instance=RequestContext(request))
def suppliers(request): offset = int(request.GET.get('year_offset', 0)) primary = request.user.account.company cutoff = primary.account.current_cutoff_date(offset) supplier_ids = TradeAccount.objects.filter(customer=primary).values_list('id', flat=True) data = YearData.objects.filter(label=YearData.PURCHASES, year=cutoff.year, account_type=TradeAccount.content_type().id, account_id__in=supplier_ids).order_by('-total') return paginate(request, data, 'task/management/suppliers.html')
def receivables_per_customer(request): primary = request.user.account.company age = request.GET.get('age', '120') age_map = { '120': TradeAccount.RECEIVABLES_120, '90': TradeAccount.RECEIVABLES_090, '60': TradeAccount.RECEIVABLES_060, '30': TradeAccount.RECEIVABLES_030 } account_ids = TradeAccount.objects.filter(supplier=primary).values_list( 'id', flat=True) data = AccountData.objects.filter( label=age_map.get(age, TradeAccount.RECEIVABLES_120), date=datetime.min, account_type=TradeAccount.content_type(), account_id__in=account_ids).order_by('-value') return paginate(request, data, 'task/accounting/receivables_customer.html', max_limit=50)