Skip to content

Commit

Permalink
feat: download project summary document after form record
Browse files Browse the repository at this point in the history
  • Loading branch information
keenthekeen committed Apr 30, 2024
1 parent 1bff249 commit d3573b7
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 24 deletions.
35 changes: 26 additions & 9 deletions app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,23 @@ public function index(Request $request): Response {
* Show the form for creating a new resource.
*/
public function create(Request $request): Response {
return $this->edit($request, new Document([]));
$document = new Document([
'tag' => $request->input('tag'),
]);
if ($request->input('tag') == 'summary' and $request->input('project_id')) {
$document->project()->associate($request->input('project_id'));
$document->title = 'รายงานผลการดำเนินงานโครงการ'.$document->project->name;
$document->recipient = 'รองคณบดีฝ่ายกิจการนิสิต';
$document->department_id = $document->project->department_id;
}

return $this->edit($request, $document);
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse {
public function store(Request $request): \Symfony\Component\HttpFoundation\Response {
return $this->update($request, new Document());
}

Expand Down Expand Up @@ -137,7 +147,7 @@ public function edit(Request $request, Document $document): Response
* Update the specified resource in storage.
* @throws \Throwable
*/
public function update(Request $request, Document $document): RedirectResponse {
public function update(Request $request, Document $document): \Symfony\Component\HttpFoundation\Response {
$this->validate($request, [
'title' => 'required|filled|string|min:5|max:255',
'recipient' => 'required|filled|string|max:255',
Expand Down Expand Up @@ -167,6 +177,19 @@ public function update(Request $request, Document $document): RedirectResponse {
$document->number_to = $document->number + $amount - 1;
}
}

// Save project summary info
if ($request->input('tag') == 'summary' and $request->input('project_id')) {
$document->project()->associate($request->input('project_id'));
$document->project->objectives = $request->input('objectives');
$document->project->expense = $request->input('expense');
$document->project->saveOrFail();

if ($request->filled('generate_document')) {
return Inertia::location(route('projects.generateSummaryDocument', ['project' => $document->project->id]));
}
}

if ($request->hasFile('attachment')) {
$path = $request->file('attachment')->store('documents');
if ($document->attachment_path) {
Expand All @@ -183,12 +206,6 @@ public function update(Request $request, Document $document): RedirectResponse {
$document->status = Document::STATUS_APPROVED;
}
$document->saveOrFail();
if ($request->input('tag') == 'summary' and $request->input('project_id')) {
$document->project()->associate($request->input('project_id'));
$document->project->objectives = $request->input('objectives');
$document->project->expense = $request->input('expense');
$document->project->saveOrFail();
}

return redirect()
->route('documents.show', ['document' => $document->id])
Expand Down
58 changes: 51 additions & 7 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,65 @@ public function generateSummaryDocument(Request $request, Project $project) {
'organizers_name' => $participant->user->name,
'organizers_id' => $participant->user->student_id
]));
$template->cloneRowAndSetValues('expense_name', array_map(function (array $ex) {
return ['expense_name' => $ex['name'] ?? '', 'expense_source' => $ex['source'] ?? '', 'expense_amount' => number_format($ex['amount'] ?? 0, 2)];
}, $project->expense));
$template->cloneRowAndSetValues('exp_name', $project->expense ? array_map(function (array $ex) {
return [
'exp_name' => $ex['name'] ?? '',
'exp_type' => $ex['type'] ?? '',
'exp_source' => $ex['source'] ?? '',
'exp_amount' => number_format($ex['amount'] ?? 0, 2),
'exp_paid' => number_format($ex['paid'] ?? 0, 2),
];
}, $project->expense) : [
[
'exp_name' => 'ไม่ได้ใช้งบประมาณ',
'exp_type' => '',
'exp_source' => '',
'exp_amount' => '',
'exp_paid' => '',
],
]);
// Set total expense
$template->setValues([
'exp_t_amnt' => number_format(array_reduce($project->expense, function ($carry, $item) {
return $carry + ($item['amount'] ?? 0);
}, 0), 2),
'exp_t_paid' => number_format(array_reduce($project->expense, function ($carry, $item) {
return $carry + ($item['paid'] ?? 0);
}, 0), 2),
]);
$aims = explode("\n", $project->aims);
$objectives = array_map(fn($objective) => $objective['goal'], $project->objectives);
$objectiveCount = max(count($aims), count($objectives));
$objectiveCount = max(count($aims), count($project->objectives));
$objectiveSum = [];
for ($i = 0; $i < $objectiveCount; $i++) {
$objectiveSum []= [
'objectives_goal' => $objectives[$i] ?? '...',
'objectives_goal' => $project->objectives[$i]['goal'] ?? '...',
'objectives_result' => isset($project->objectives[$i]['result']) ? ($project->objectives[$i]['result'].' ('.$project->objectives[$i]['percentage'].'%)') : '...',
'aims_text' => $aims[$i] ?? '...',
];
}
$template->cloneRowAndSetValues('objectives_goal', $objectiveSum);

// Calculate average percentage
$totalPercentage = $countPercentage = 0;
foreach ($project->objectives as $objective) {
if (isset($objective['percentage'])) {
$totalPercentage += $objective['percentage'];
$countPercentage++;
}
}
$template->setValues([
'total_percent' => $countPercentage ? number_format($totalPercentage / $countPercentage, 2) : '...',
]);

// Set participant list
$template->cloneRowAndSetValues('ptcp_no', $project->participants->map(fn(ProjectParticipant $participant, int $i) => [
'ptcp_no' => $i + 1,
'ptcp_name' => $participant->user->name,
'ptcp_id' => $participant->user->student_id,
'ptcp_type' => ProjectParticipant::TYPES_OPTIONS[$participant->type] ?? $participant->type,
'ptcp_title' => $participant->title ?? '-',
]));

$tmpPath = tempnam(storage_path(), 'tmp-projectsummary-');
$template->saveAs($tmpPath);

Expand Down Expand Up @@ -557,7 +601,7 @@ public function exportParticipant(Project $project) {
return [
$i + 1,
$participant->user->name,
['organizer' => 'ผู้รับผิดชอบ', 'staff' => 'ผู้จัดกิจกรรม', 'attendee' => 'ผู้เข้าร่วม'][$participant->type] ?? $participant->type,
ProjectParticipant::TYPES_OPTIONS[$participant->type] ?? $participant->type,
$participant->title,
];
});
Expand Down
1 change: 1 addition & 0 deletions app/Models/ProjectParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ProjectParticipant extends Model {

protected $fillable = ['user_id', 'type', 'title'];
protected $visible = ['id', 'type', 'title', 'user', 'project'];
public const TYPES_OPTIONS = ['organizer' => 'ผู้รับผิดชอบ', 'staff' => 'ผู้จัดกิจกรรม', 'attendee' => 'ผู้เข้าร่วม'];

public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo {
return $this->belongsTo(User::class);
Expand Down
44 changes: 39 additions & 5 deletions resources/js/Pages/DocumentCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@
</div>
</template>
</jet-form-section>
<template v-if="!item.id">
<jet-section-border/>
<jet-form-section>
<template #title>ดาวน์โหลดแบบรายงานผลโครงการ</template>
<template #description>กรุณาดาวน์โหลดไฟล์ แล้วใช้โปรแกรม Microsoft Word แก้ไข/เติมรายละเอียดให้ครบถ้วน
(หรือใช้แบบฟอร์มที่สพจ. แจกจ่าย) แล้วจึงอัปโหลดไฟล์ที่แก้ไขแล้วในช่องถัดไป
</template>
<template #form>
<div class="col-span-6">
<jet-button type="button" @click="generateSummaryDocument">
บันทึกผลการดำเนินโครงการ และ ดาวน์โหลดแบบรายงานผลโครงการ
</jet-button>
</div>
</template>
</jet-form-section>
</template>
</div>
<jet-section-border/>
</template>
Expand All @@ -392,7 +408,7 @@
</progress>
<jet-button type="submit" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
Save
{{ item.id ? 'บันทึก' : 'บันทึก & ออกเลขเอกสาร' }}
</jet-button>
</template>
</jet-form-section>
Expand Down Expand Up @@ -425,8 +441,8 @@ import JetLabel from '@/Jetstream/Label.vue'
import JetSectionBorder from '@/Jetstream/SectionBorder.vue'
import Checkbox from "@/Jetstream/Checkbox.vue";
import AttachmentBox from "@/Components/AttachmentBox.vue";
import {ref, reactive, watch} from 'vue';
import {router, Link} from '@inertiajs/vue3'
import {reactive, ref, watch} from 'vue';
import {Link, router} from '@inertiajs/vue3'
import {debounce} from "lodash/function";
import {isNumber} from "lodash/lang";
Expand Down Expand Up @@ -493,9 +509,27 @@ const submit = function () {
? route('documents.update', {document: props.item.id})
: route('documents.store'),
form
)
);
}
}
};
const generateSummaryDocument = function () {
form.project_id = selectedProject.value ? selectedProject.value.id : null;
form.objectives = selectedProject.value.objectives;
form.expense = selectedProject.value.expense;
form.amount = 1;
if (form.objectives.filter(o => o.result && o.result.length > 0).length !== form.objectives.length) {
alert("กรุณากรอกผลการประเมินทุกตัวชี้วัด");
return;
}
if (form.expense.filter(e => e.paid || (e.paid === 0)).length !== form.expense.length) {
alert("กรุณากรอกยอดเงินที่จ่ายจริงให้ครบทุกรายการ หากไม่ได้ใช้จ่าย ให้ใส่ 0");
return;
}
router.post(route('documents.store'), {
...form,
generate_document: true,
});
};
const searchProject = debounce(function (keyword) {
keywordError.value = 'กำลังค้นหา...';
Expand Down
15 changes: 12 additions & 3 deletions resources/js/Pages/ProjectShow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@
</h3>
</div>
<div class="border-t border-gray-200 p-4 sm:px-6">
<p class="text-sm text-gray-700">เมื่อเสร็จสิ้นโครงการแล้ว ให้รายงานผลการดำเนินโครงการ และส่งเบิกค่าใช้จ่าย (ถ้ามี)
ให้เรียบร้อยโดยเร็ว</p>
<p class="my-0.5 text-gray-700">
เมื่อเสร็จสิ้นโครงการแล้ว ให้รายงานผลการดำเนินโครงการ และส่งเบิกค่าใช้จ่าย (ถ้ามี) ให้เรียบร้อยโดยเร็ว
</p>
<p class="my-0.5 text-blue-500">
ข้อมูลผลการดำเนินโครงการ มีผลต่อการพิจารณางบประมาณโครงการ/ฝ่ายครั้งถัดไป
</p>
<inertia-link :href="route('documents.create', {project_id: item.id, tag: 'summary'})"
class="inline-block items-center px-4 py-2 mt-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition">
บันทึกผลการดำเนินโครงการ และสร้างแบบรายงานผลโครงการ
</inertia-link>
&ensp;หรือ&ensp;
<a :href="route('projects.generateSummaryDocument', {project: item.id})"
class="inline-block items-center px-4 py-2 mt-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition">ดาวน์โหลดแบบรายงานผลโครงการ</a>
class="text-blue-400">ดาวน์โหลดแบบรายงานผลโครงการ</a>
</div>
</div>
<div class="bg-white shadow overflow-hidden sm:rounded-lg my-4">
Expand Down
Binary file modified storage/project_summary_template.docx
Binary file not shown.

0 comments on commit d3573b7

Please sign in to comment.