Permalink
Browse files

add money view in import gms script, fix permission to support projec…

…t without program, add budget page for project without program, now super user can do anything
  • Loading branch information...
1 parent ceca303 commit 0ca5fa43aac6b8118f448fb57bd421adefa0d148 @panuta committed Nov 16, 2011
View
3 README
@@ -1 +1,4 @@
This is a restart-from-scratch version of ThaiHealthSMS.
+
+TODO:
+- Check schedule page
View
@@ -1,7 +1,8 @@
from django.db import models
class BudgetSchedule(models.Model):
- program = models.ForeignKey('domain.Program')
+ program = models.ForeignKey('domain.Program', null=True)
+ project = models.ForeignKey('domain.Project', null=True)
grant_budget = models.IntegerField(default=0)
claim_budget = models.IntegerField(default=0)
schedule_on = models.DateField()
View
@@ -7,6 +7,7 @@
url(r'^master_plan/(?P<master_plan_ref_no>\d+)/budget/$', 'view_master_plan_budget', name='view_master_plan_budget'),
url(r'^program/(?P<program_id>\d+)/budget/$', 'view_program_budget', name='view_program_budget'),
+ url(r'^project/(?P<project_id>\d+)/budget/$', 'view_project_budget', name='view_project_budget'),
url(r'^budget/(?P<schedule_id>\d+)/$', 'view_budget_overview', name='view_budget_overview'),
url(r'^budget/(?P<schedule_id>\d+)/reference/edit/$', 'view_budget_overview_edit_reference', name='view_budget_overview_edit_reference'),
View
@@ -166,14 +166,26 @@ def view_program_budget(request, program_id):
return render_page_response(request, 'budget', 'page_program/program_budget.html', {'program':program, 'schedules':schedules})
#
+# PROJECT #######################################################################
+#
+
+@login_required
+def view_project_budget(request, project_id):
+ project = get_object_or_404(Project, pk=project_id)
+ schedules = BudgetSchedule.objects.filter(project=project).order_by('-schedule_on')
+
+ return render_page_response(request, 'budget', 'page_program/project_budget.html', {'project':project, 'schedules':schedules})
+
+#
# BUDGET SCHEDULE #######################################################################
#
@login_required
def view_budget_overview(request, schedule_id):
schedule = get_object_or_404(BudgetSchedule, pk=schedule_id)
-
- if permission.access_obj(request.user, 'program budget remark edit', schedule.program):
+
+ # if permission.access_obj(request.user, 'program budget remark edit', schedule.program):
+ if (schedule.project.plan and has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), schedule.project.plan.master_plan)) or (not schedule.project.plan and permission.access_obj(request.user, 'program budget remark edit', schedule.program)):
if request.method == 'POST':
form = ModifyBudgetRemarkForm(request.POST)
if form.is_valid():
View
@@ -475,11 +475,13 @@ def view_project_overview(request, project_id):
@login_required
def view_project_edit_project(request, project_id):
project = get_object_or_404(Project, pk=project_id)
- program = project.program
-
- if not permission.access_obj(request.user, 'program project edit', program):
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
return access_denied(request)
+ if project.program and not permission.access_obj(request.user, 'program project edit', program):
+ return access_denied(request)
+
if request.method == 'POST':
form = ProjectModifyForm(request.POST)
if form.is_valid():
@@ -496,8 +498,8 @@ def view_project_edit_project(request, project_id):
return redirect('view_project_overview', (project.id))
else:
- if program:
- form = ProjectModifyForm(initial={'program_id':program.id, 'project_id':project.id, 'ref_no':project.ref_no, 'contract_no':project.contract_no, 'name':project.name, 'description':project.description, 'start_date':project.start_date, 'end_date':project.end_date})
+ if project.program:
+ form = ProjectModifyForm(initial={'program_id':project.program.id, 'project_id':project.id, 'ref_no':project.ref_no, 'contract_no':project.contract_no, 'name':project.name, 'description':project.description, 'start_date':project.start_date, 'end_date':project.end_date})
else:
form = ProjectModifyForm(initial={'program_id':'', 'project_id':project.id, 'ref_no':project.ref_no, 'contract_no':project.contract_no, 'name':project.name, 'description':project.description, 'start_date':project.start_date, 'end_date':project.end_date})
@@ -507,18 +509,23 @@ def view_project_edit_project(request, project_id):
@login_required
def view_project_delete_project(request, project_id):
project = get_object_or_404(Project, pk=project_id)
- program = project.program
-
- if not permission.access_obj(request.user, 'program project delete', program):
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
return access_denied(request)
+ if project.program and not permission.access_obj(request.user, 'program project delete', program):
+ return access_denied(request)
+
if not Activity.objects.filter(project=project).count():
project.delete()
messages.success(request, u'ลบโครงการเรียบร้อย')
else:
messages.error(request, 'ไม่สามารถลบโครงการที่มีกิจกรรมอยู่ได้')
- return redirect('view_program_projects', (program.id))
+ if project.plan:
+ return redirect('view_master_plan_programs', (project.plan.master_plan.id))
+ else:
+ return redirect('view_program_projects', (program.id))
@login_required
def view_project_activities(request, project_id):
@@ -529,8 +536,11 @@ def view_project_activities(request, project_id):
@login_required
def view_project_add_activity(request, project_id):
project = get_object_or_404(Project, pk=project_id)
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
+ return access_denied(request)
- if not permission.access_obj(request.user, 'program activity add', project.program):
+ if project.program and not permission.access_obj(request.user, 'program activity add', program):
return access_denied(request)
if request.method == 'POST':
@@ -559,8 +569,11 @@ def view_project_add_activity(request, project_id):
def view_project_edit_activity(request, activity_id):
activity = get_object_or_404(Activity, pk=activity_id)
project = activity.project
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
+ return access_denied(request)
- if not permission.access_obj(request.user, 'program activity edit', project.program):
+ if project.program and not permission.access_obj(request.user, 'program activity edit', project.program):
return access_denied(request)
if request.method == 'POST':
@@ -638,8 +651,11 @@ def view_activity_overview(request, activity_id):
def view_activity_edit_activity(request, activity_id):
activity = get_object_or_404(Activity, pk=activity_id)
project = activity.project
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
+ return access_denied(request)
- if not permission.access_obj(request.user, 'program activity edit', project.program):
+ if project.program and not permission.access_obj(request.user, 'program activity edit', project.program):
return access_denied(request)
if request.method == 'POST':
@@ -666,8 +682,11 @@ def view_activity_edit_activity(request, activity_id):
def view_activity_delete_activity(request, activity_id):
activity = get_object_or_404(Activity, pk=activity_id)
project = activity.project
+
+ if project.plan and not permission.has_role_with_obj(request.user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), project.plan.master_plan):
+ return access_denied(request)
- if not permission.access_obj(request.user, 'program activity delete', project.program):
+ if project.program and not permission.access_obj(request.user, 'program activity delete', project.program):
return access_denied(request)
activity.delete()
View
@@ -12,6 +12,7 @@ def access_obj(user, permissions, obj, at_least_one_permission=True):
else:
permissions = list(permissions)
+ """
if user.is_superuser:
checked_permission_count = 0
for permission in permissions:
@@ -25,6 +26,8 @@ def access_obj(user, permissions, obj, at_least_one_permission=True):
return checked_permission_count > 0
else:
return len(permissions) == checked_permission_count
+ """
+ if user.is_superuser: return True
else:
passed_permissions = []
@@ -101,7 +104,7 @@ def has_role_with_obj(user, roles, obj):
else:
roles = list(roles)
- if user.is_superuser: return False
+ if user.is_superuser: return True
has_access = False
for responsibility in UserRoleResponsibility.objects.filter(user=user.get_profile()):
@@ -122,7 +125,7 @@ def has_role_with_obj(user, roles, obj):
else:
if obj in responsibility.programs.all():
has_access = True
-
+
return has_access
def who_program_manager(program):
@@ -73,11 +73,18 @@ def display_project_edit_header(user, project):
@register.simple_tag
def display_activity_header(user, activity):
- header_html = unicode('<div class="supertitle"><a href="%s">แผนงาน %s</a> &#187; <a href="%s">โครงการ %s - %s</a></div><h1>กิจกรรม %s</h1>', 'utf-8') % (reverse('view_program_overview', args=[activity.project.program.id]), activity.project.program.ref_no, reverse('view_project_overview', args=[activity.project.id]), activity.project.ref_no, activity.project.name, activity.name)
-
- if permission.access_obj(user, 'program activity edit', activity.project.program):
- header_html = header_html + unicode('<div class="subtitle"><img src="%s/images/icons/edit.png" class="icon"/> <a href="%s">แก้ไขกิจกรรม</a></div>', 'utf-8') % (settings.MEDIA_URL, reverse('view_activity_edit_activity', args=[activity.id]))
+ if activity.project.plan:
+ header_html = unicode('<div class="supertitle"><a href="%s">โครงการ %s - %s</a></div><h1>กิจกรรม %s</h1>', 'utf-8') % (reverse('view_project_overview', args=[activity.project.id]), activity.project.ref_no, activity.project.name, activity.name)
+
+ if permission.has_role_with_obj(user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), activity.project.plan.master_plan):
+ header_html = header_html + unicode('<div class="subtitle"><img src="%s/images/icons/edit.png" class="icon"/> <a href="%s">แก้ไขกิจกรรม</a></div>', 'utf-8') % (settings.MEDIA_URL, reverse('view_activity_edit_activity', args=[activity.id]))
+
+ else:
+ header_html = unicode('<div class="supertitle"><a href="%s">แผนงาน %s</a> &#187; <a href="%s">โครงการ %s - %s</a></div><h1>กิจกรรม %s</h1>', 'utf-8') % (reverse('view_program_overview', args=[activity.project.program.id]), activity.project.program.ref_no, reverse('view_project_overview', args=[activity.project.id]), activity.project.ref_no, activity.project.name, activity.name)
+ if permission.access_obj(user, 'program activity edit', activity.project.program):
+ header_html = header_html + unicode('<div class="subtitle"><img src="%s/images/icons/edit.png" class="icon"/> <a href="%s">แก้ไขกิจกรรม</a></div>', 'utf-8') % (settings.MEDIA_URL, reverse('view_activity_edit_activity', args=[activity.id]))
+
return header_html
@register.simple_tag
@@ -94,7 +101,23 @@ def display_kpi_header(user, schedule):
@register.simple_tag
def display_budget_header(user, schedule):
- return unicode('<div class="supertitle"><a href="%s">แผนงาน %s - %s</a></div><h1>งวดเบิกจ่ายวันที่ %s</h1>', 'utf-8') % (reverse('view_program_overview', args=[schedule.program.id]), schedule.program.ref_no, schedule.program.name, utilities.format_full_date(schedule.schedule_on))
+ if schedule.program:
+ return unicode('<div class="supertitle"><a href="%s">แผนงาน %s - %s</a></div><h1>งวดเบิกจ่ายวันที่ %s</h1>', 'utf-8') % (reverse('view_program_overview', args=[schedule.program.id]), schedule.program.ref_no, schedule.program.name, utilities.format_full_date(schedule.schedule_on))
+
+ elif schedule.project:
+ if schedule.project.plan:
+ header_html = unicode('<div class="supertitle"><a href="%s">แผน %s - %s</a></div><h1>โครงการ (%s) %s</h1>', 'utf-8') % (reverse('view_master_plan_overview', args=[schedule.project.plan.master_plan.id]), schedule.project.plan.master_plan.ref_no, schedule.project.plan.master_plan.name, schedule.project.ref_no, schedule.project.name)
+
+ if permission.has_role_with_obj(user, ('sector_manager', 'sector_manager_assistant', 'sector_specialist'), schedule.project.plan.master_plan):
+ header_html = header_html + unicode('<div class="subtitle"><img src="%s/images/icons/edit.png" class="icon"/> <a href="%s">แก้ไขโครงการ</a></div>', 'utf-8') % (settings.MEDIA_URL, reverse('view_project_edit_project', args=[schedule.project.id]))
+
+ else:
+ header_html = unicode('<div class="supertitle"><a href="%s">แผนงาน %s - %s</a></div><h1>โครงการ (%s) %s</h1>', 'utf-8') % (reverse('view_program_overview', args=[schedule.project.program.id]), schedule.project.program.ref_no, schedule.project.program.name, schedule.project.ref_no, schedule.project.name)
+
+ if permission.access_obj(user, 'program project edit', schedule.project.program):
+ header_html = header_html + unicode('<div class="subtitle"><img src="%s/images/icons/edit.png" class="icon"/> <a href="%s">แก้ไขโครงการ</a></div>', 'utf-8') % (settings.MEDIA_URL, reverse('view_project_edit_project', args=[schedule.project.id]))
+
+ return ''
#
# TAB
@@ -193,6 +216,10 @@ def tabs_for_project(page, user, project):
if page == 'overview': html = html + '<li class="selected"><a href="%s">ภาพรวม</a></li>' % reverse('view_project_overview', args=[project.id])
else: html = html + '<li><a href="%s">ภาพรวม</a></li>' % reverse('view_project_overview', args=[project.id])
+
+ if project.plan:
+ if page == 'budget': html = html + '<li class="selected"><a href="%s">แผนการเงิน</a></li>' % reverse('view_project_budget', args=[project.id])
+ else: html = html + '<li><a href="%s">แผนการเงิน</a></li>' % reverse('view_project_budget', args=[project.id])
if page == 'activities': html = html + '<li class="selected"><a href="%s">กิจกรรม</a></li>' % reverse('view_project_activities', args=[project.id])
else: html = html + '<li><a href="%s">กิจกรรม</a></li>' % reverse('view_project_activities', args=[project.id])
View
@@ -2,6 +2,7 @@
SMS_PLAN_VIEW_URL = 'http://61.90.139.134/gms/api/?view=plan&format=json&page=0&pbyear=55'
SMS_CONTRACT_VIEW_URL = 'http://61.90.139.134/gms/api/?view=contract&format=json&page=0'
+SMS_CONTRACT_MONEY_URL = 'http://61.90.139.134/gms/api/?view=money&format=json&page=0'
from django.core.management import setup_environ
import settings
@@ -15,6 +16,7 @@
from accounts.models import UserAccount
from domain.models import *
+from budget.models import *
def convert_to_date(str):
(year_str, month_str, date_str) = str.split(' ')[0].split('-')
@@ -33,16 +35,19 @@ def find_project(raw_project_list, project_code):
logfile = open('import_gms-%02d-%02d-%02d.log' % (startdate.year, startdate.month, startdate.day), 'w')
logfile.write('Import operation started on %02d/%02d/%02d %02d:%02d\n' % (startdate.day, startdate.month, startdate.year, startdate.hour, startdate.minute))
-# Retrieve plan list in JSON format from GMS
+# Retrieve data JSON format from GMS
import_url = urllib.urlopen(SMS_PLAN_VIEW_URL)
raw_plan_list = simplejson.loads(import_url.read())
-# Retrieve project list in JSON format from GMS
import_url = urllib.urlopen(SMS_CONTRACT_VIEW_URL)
raw_project_list = simplejson.loads(import_url.read())
+#import_url = urllib.urlopen(SMS_CONTRACT_MONEY_URL)
+#raw_money_list = simplejson.loads(import_url.read())
+
stat_plan_created = 0
stat_project_created = 0
+stat_money_created = 0
# Insert plans and projects to SMS
for raw_plan in raw_plan_list:
@@ -58,14 +63,11 @@ def find_project(raw_project_list, project_code):
except Plan.DoesNotExist:
plan = Plan.objects.create(master_plan=master_plan, ref_no=raw_plan['BudgetPlan'], name=raw_plan['BudgetPlanName'])
stat_plan_created = stat_plan_created + 1
- else:
- plan.name = raw_plan['BudgetPlanName']
- plan.save()
raw_project = find_project(raw_project_list, raw_plan['ProjectCode'])
if not raw_project:
- logfile.write('ERROR: Project not found [ProjectCode="%s"]\n' % raw_plan['ProjectCode'])
+ logfile.write('ERROR: Project not found in [ProjectCode="%s"]\n' % raw_plan['ProjectCode'])
else:
try:
project = Project.objects.get(ref_no=raw_plan['ProjectCode'])
@@ -78,17 +80,44 @@ def find_project(raw_project_list, project_code):
start_date=convert_to_date(raw_project['DateStart']),
end_date=convert_to_date(raw_project['DateFinish']),
created_by=admin_user,
- )
+ )
stat_project_created = stat_project_created + 1
else:
- # Update existing record, in case there's a change
+ # Update project name and date, if imported data is different than in SMS database
project.name = raw_project['ProjectThai']
start_date=convert_to_date(raw_project['DateStart'])
end_date=convert_to_date(raw_project['DateFinish'])
+"""
+for raw_money in raw_money_list:
+ try:
+ project = Project.objects.get(ref_no=raw_money['ProjectCode'])
+ except Project.DoesNotExist:
+ logfile.write('ERROR: Project not found in SMS_CONTRACT_MONEY_URL [ProjectCode="%s"]\n' % raw_plan['ProjectCode'])
+
+ try:
+ budget_schedule = BudgetSchedule.objects.get(project=project, schedule_on=convert_to_date(raw_money['DateDue']))
+ except BudgetSchedule.DoesNotExist:
+ budget_schedule = BudgetSchedule.objects.create(
+ project=project,
+ schedule_on=convert_to_date(raw_money['DateDue']),
+ grant_budget=int(raw_money['MoneyOperate']),
+ )
+
+ revision = BudgetScheduleRevision.objects.create(
+ schedule=budget_schedule,
+ grant_budget=budget_schedule.grant_budget,
+ claim_budget=budget_schedule.claim_budget,
+ schedule_on=budget_schedule.schedule_on,
+ revised_by=admin_user
+ )
+"""
+
+
+
enddate = datetime.datetime.today()
-logfile.write('Summary: Created %d plans' % stat_plan_created)
-logfile.write('Summary: Created %d projects' % stat_project_created)
+logfile.write('Summary: Created %d plans\n' % stat_plan_created)
+logfile.write('Summary: Created %d projects\n' % stat_project_created)
logfile.write('Import operation ended on %02d/%02d/%02d %02d:%02d\n' % (startdate.day, startdate.month, startdate.year, startdate.hour, startdate.minute))
logfile.close()
Oops, something went wrong.

0 comments on commit 0ca5fa4

Please sign in to comment.