class SeasonForm(forms.Form): from_year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), label=gettext('from_year'), initial=common.current_month().year) from_month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), label=gettext('from_month'), initial=common.current_month().month) to_year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), label=gettext('to_year'), initial=common.current_month().year) to_month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), label=gettext('to_month'), initial=common.current_month().month) def clean_from_year(self): return int(self.cleaned_data['from_year']) def clean_from_month(self): return int(self.cleaned_data['from_month']) def clean_to_year(self): return int(self.cleaned_data['to_year']) def clean_to_month(self): return int(self.cleaned_data['to_month'])
class MonthForm(forms.Form): year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), label=gettext('year'), initial=datetime.now().year) month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), label=gettext('month'), initial=common.current_month().month) def clean_year(self): return int(self.cleaned_data['year']) def clean_month(self): return int(self.cleaned_data['month'])
class LocateDemandForm(forms.Form): project = forms.ModelChoiceField(Project.objects.all(), empty_label=gettext('choose_project')) year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), initial=datetime.now().year) month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), initial=common.current_month().month) def clean_year(self): return int(self.cleaned_data['year']) def clean_month(self): return int(self.cleaned_data['month'])
def nh_season_profit(request): months = [] totals = {} if len(request.GET): form = NHBranchSeasonForm(request.GET) if form.is_valid(): nhbranch = form.cleaned_data['nhbranch'] cleaned_data = form.cleaned_data from_year, from_month, to_year, to_month = cleaned_data['from_year'], cleaned_data['from_month'], \ cleaned_data['to_year'], cleaned_data['to_month'] total_profit, total_net_income = 0, 0 nhmonths = NHMonth.objects.range( from_year, from_month, to_year, to_month).filter(nhbranch=nhbranch) for nhm in nhmonths: nhm.include_tax = False salary_expenses = 0 #collect all employee expenses for this month for salary in NHEmployeeSalary.objects.nondeleted().filter( nhbranch=nhm.nhbranch, year=nhm.year, month=nhm.month): salary_expenses += salary.check_amount or 0 #calculate commulative sales prices for this month sales_worth = 0 for nhsale in nhm.nhsales.all(): sales_worth += nhsale.price profit = nhm.total_net_income - salary_expenses month_total = { 'nhmonth': nhm, 'sales_count': nhm.nhsales.count(), 'sales_worth_no_tax': sales_worth, 'income_no_tax': nhm.total_income, 'lawyers_pay': nhm.total_lawyer_pay, 'net_income_no_tax': nhm.total_net_income, 'salary_expenses': salary_expenses, 'profit': profit } months.append(month_total) total_profit += profit total_net_income += nhm.total_net_income totals = {} #calculate relative fields, and totals for month in months: month['relative_profit'] = month['profit'] / total_profit * 100 month['relative_net_income'] = month[ 'net_income_no_tax'] / total_net_income * 100 for key in [ 'sales_worth_no_tax', 'income_no_tax', 'lawyers_pay', 'net_income_no_tax', 'salary_expenses', 'profit' ]: totals.setdefault(key, 0) totals[key] += month[key] else: form = NHBranchSeasonForm() month = current_month() from_year, from_month, to_year, to_month = month.year, month.month, month.year, month.month context = { 'months': months, 'totals': totals, 'filterForm': form, 'from_year': from_year, 'from_month': from_month, 'to_year': to_year, 'to_month': to_month } return render(request, 'Analytics/nh_season_profit.html', context)
def projects_profit(request): # load all Tax objects, order by 'date' descending taxes = list(Tax.objects.all()) def _process_demands(demands): projects = [] for p, demand_iter in itertools.groupby(demands, lambda demand: demand.project): p.sale_count, p.total_income, p.total_expense, p.profit, p.total_sales_amount = 0, 0, 0, 0, 0 p.employee_expense = {} for demand in demand_iter: # find the first tax object with date earlier then demand's date demand_date = date(demand.year, demand.month, 1) tax = next((t for t in taxes if t.date <= demand_date)) tax_val = tax.value / 100 + 1 total_amount = demand.total_amount / tax_val sales = demand.sales_list sale_count = demand.sales_count p.total_income += total_amount p.sale_count += sale_count p.total_sales_amount += sum( map( lambda sale: sale.price if sale.include_tax else sale.price / tax_val, sales)) projects.append(p) return projects def _process_salaries(salaries, projects): for s in salaries: # find the first tax object with date earlier then salary's date salary_date = date(s.year, s.month, 1) tax = next((t for t in taxes if t.date <= salary_date)) tax_val = tax.value / 100 + 1 terms = s.employee.employment_terms if not terms: continue s.calculate() for project, salary in s.project_salary().items(): fixed_salary = salary if terms.hire_type.id == HireType.SelfEmployed: fixed_salary = salary / tax_val p = projects[projects.index(project)] p.employee_expense.setdefault(s.employee, 0) p.employee_expense[s.employee] += fixed_salary p.total_expense += fixed_salary if len(request.GET): form = SeasonForm(request.GET) if form.is_valid(): cleaned_data = form.cleaned_data from_year, from_month, to_year, to_month = cleaned_data[ 'from_year'], cleaned_data['from_month'], cleaned_data[ 'to_year'], cleaned_data['to_month'] demands = Demand.objects \ .range(from_year, from_month, to_year, to_month) \ .order_by('project') set_demand_sale_fields(demands, from_year, from_month, to_year, to_month) set_demand_diff_fields(demands) salaries = EmployeeSalary.objects.nondeleted().range( from_year, from_month, to_year, to_month) projects = _process_demands(demands) _process_salaries(salaries, projects) total_income = sum([project.total_income for project in projects]) avg_relative_expense_income, avg_relative_sales_expense = 0, 0 project_count = 0 for p in projects: if p.sale_count > 0: project_count += 1 p.relative_income = total_income and ( p.total_income / total_income * 100) or 100 if p.total_expense and p.total_sales_amount: p.relative_sales_expense = float( p.total_expense) / p.total_sales_amount * 100 avg_relative_sales_expense += p.relative_sales_expense else: if p.total_expense == 0 and p.total_sales_amount == 0: p.relative_sales_expense_str = 'אפס' elif p.total_sales_amount == 0: p.relative_sales_expense_str = u'גרעון' elif p.total_expense == 0: p.relative_sales_expense_str = u'עודף' if p.total_income and p.total_expense: p.relative_expense_income = p.total_expense / p.total_income * 100 avg_relative_expense_income += p.relative_expense_income else: if p.total_income == 0 and p.total_expense == 0: p.relative_expense_income_str = u'אפס' elif p.total_income == 0: p.relative_expense_income_str = u'גרעון' elif p.total_expense == 0: p.relative_expense_income_str = u'עודף' p.profit = p.total_income - p.total_expense total_sale_count = sum( [project.sale_count for project in projects]) total_expense = sum( [project.total_expense for project in projects]) total_profit = sum([project.profit for project in projects]) if project_count: avg_relative_expense_income = avg_relative_expense_income / project_count avg_relative_sales_expense = avg_relative_sales_expense / project_count else: month = current_month() form = SeasonForm( initial={ 'from_year': month.year, 'from_month': month.month, 'to_year': month.year, 'to_month': month.month }) from_year, from_month, to_year, to_month = month.year, month.month, month.year, month.month total_income, total_expense, total_profit, avg_relative_expense_income, total_sale_count, avg_relative_sales_expense = 0, 0, 0, 0, 0, 0 projects = [] context = { 'projects': projects, 'from_year': from_year, 'from_month': from_month, 'to_year': to_year, 'to_month': to_month, 'filterForm': form, 'total_income': total_income, 'total_expense': total_expense, 'total_profit': total_profit, 'avg_relative_expense_income': avg_relative_expense_income, 'total_sale_count': total_sale_count, 'avg_relative_sales_expense': avg_relative_sales_expense } return render(request, 'Analytics/projects_profit.html', context)
class DemandPayBalanceForm(forms.Form): class DemandPayBalanceType(object): __slots__ = ('id', 'name') def __init__(self, id=None, name=None): self.id, self.name = id, name demand_pay_balance_choices = [ DemandPayBalanceType('all', gettext('all')), DemandPayBalanceType('un-paid', gettext('un-paid')), DemandPayBalanceType('mis-paid', gettext('mis-paid')), DemandPayBalanceType('partially-paid', gettext('partially-paid')), DemandPayBalanceType('fully-paid', gettext('fully-paid')), ] from_year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), label=gettext('from_year'), initial=common.current_month().year) from_month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), label=gettext('from_month'), initial=common.current_month().month) to_year = forms.ChoiceField( choices=((i, i) for i in range(datetime.now().year - 10, datetime.now().year + 10)), label=gettext('to_year'), initial=common.current_month().year) to_month = forms.ChoiceField(choices=((i, i) for i in range(1, 13)), label=gettext('to_month'), initial=common.current_month().month) all_times = forms.BooleanField(required=False, label=gettext('all_times')) project = forms.ModelChoiceField(queryset=Project.objects.all(), empty_label=gettext('all_projects'), label=gettext('project'), required=False) demand_pay_balance = forms.ChoiceField(choices=[ (x.id, x.name) for x in demand_pay_balance_choices ], label=gettext('pay_balance')) def __init__(self, *args, **kw): super(DemandPayBalanceForm, self).__init__(*args, **kw) for attr in ['from_month', 'from_year', 'to_month', 'to_year']: self.fields[attr].required = False def clean_demand_pay_balance(self): demand_pay_balance = self.cleaned_data['demand_pay_balance'] return [ dpb for dpb in DemandPayBalanceForm.demand_pay_balance_choices if dpb.id == demand_pay_balance ][0] def clean_from_year(self): return int(self.cleaned_data['from_year']) def clean_from_month(self): return int(self.cleaned_data['from_month']) def clean_to_year(self): return int(self.cleaned_data['to_year']) def clean_to_month(self): return int(self.cleaned_data['to_month'])