JounalController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\User;
  7. use Auth;
  8. use App\Models\AccountType;
  9. use App\Models\AccountHead;
  10. use App\Models\Journal;
  11. use App\Models\PaymentHistory;
  12. use App\Models\JournalComment;
  13. use App\Models\FundRequest;
  14. class JounalController extends Controller
  15. {
  16. public function __construct(){
  17. $this->middleware('auth');
  18. }
  19. function index()
  20. {
  21. //$data['journals']=Journal::with('debit')->paginate(50);
  22. $data['journals']=\DB::select("SELECT debit_account,ahd.name,SUM(COALESCE(amount, 0)) AS debit,(SELECT COALESCE(SUM(j1.amount), 0) credit FROM journal j1 WHERE j1.ref_id = j.debit_account) credit FROM
  23. journal j JOIN acc_head ahd ON j.debit_account = ahd.id WHERE ahd.type_id IN (1, 2)
  24. GROUP BY j.debit_account order by j.id desc");
  25. $data['funds']=FundRequest::with(['acc_head','user'])->where('is_approved',0)
  26. ->orderBY('id','desc')->get();
  27. $data['approved_funds']=FundRequest::with(['acc_head','user'])->where('is_approved',1)
  28. ->orderBY('id','desc')->get();
  29. $data['title']='Journal';
  30. return view('admin.journal.index',$data);
  31. }
  32. /**
  33. * get journal details with breakdown
  34. * @param integer $id
  35. * @return view
  36. */
  37. function get_details($id)
  38. {
  39. $data['journal']=Journal::with(['debit','credit','user'])->where('debit_account',$id)->orWhere('ref_id',$id)->get();
  40. $data['title']='Journal Details';
  41. return view('admin.journal.details',$data);
  42. }
  43. function create()
  44. {
  45. $data['account_types']=AccountType::with(['acc_head'=>function($q){
  46. return $q->where('parent_head',0);
  47. }])->orderBy('name','asc')->get();
  48. $data['title']='Create Journal';
  49. return view('admin.journal.create',$data);
  50. }
  51. function store(Request $req)
  52. {
  53. $this->validate($req,[
  54. 'account_head'=>'required',
  55. 'credit_head'=>'required',
  56. 'amount'=>'required|numeric'
  57. ]);
  58. $documentUpload = $req->file('filedocument');
  59. if ( !empty( $documentUpload )) {
  60. $documentname = $documentUpload->getClientOriginalName();
  61. $path = 'public/account_expense/';
  62. $documentUpload->move($path,$documentname);
  63. $expenseDocument = $documentname;
  64. } else {
  65. $expenseDocument = '';
  66. }
  67. $journal=new Journal();
  68. $journal->debit_account = $req->get('account_head');
  69. $journal->credit_account = $req->get('credit_head');
  70. $journal->ref_id = $req->get('account_head');
  71. $journal->description = $req->get('description');
  72. $journal->amount = $req->get('amount');
  73. $journal->expense_document = $expenseDocument;
  74. $journal->is_approaved = 0;
  75. $journal->created_by = \Auth::user()->id;
  76. $journal->created_at = $req->get('date',date('Y-m-d'));
  77. $journal->save();
  78. $req->session()->flash('success','Journal created successfully !');
  79. return redirect('account/journal');
  80. }
  81. function get_acc_head($id)
  82. {
  83. $heads=AccountHead::where('id',$id)->orderBy('name','asc')->get();
  84. $str="<option value=''>Select Account Type</option>";
  85. if($heads)
  86. {
  87. foreach($heads as $hd)
  88. {
  89. $str.="<option value='{$hd->id}'>{$hd->name}</option>";
  90. }
  91. }
  92. return $str;
  93. }
  94. function change_approaval_status(Request $req)
  95. {
  96. $journal_comment=JournalComment::where('debit_account',$req->get('debit_account'))->first();
  97. if($journal_comment)
  98. {
  99. $journal_comment->debit_account=$req->get('debit_account');
  100. $journal_comment->approaval_status=$req->get('status');
  101. $journal_comment->approaved_amount=$req->get('amount');
  102. $journal_comment->remarks=$req->get('remarks');
  103. $journal_comment->approaved_by=\Auth::user()->id;
  104. $journal_comment->save();
  105. }
  106. else
  107. {
  108. $journal_comment=new JournalComment();
  109. $journal_comment->debit_account=$req->get('debit_account');
  110. $journal_comment->approaval_status=$req->get('status');
  111. $journal_comment->approaved_amount=$req->get('amount');
  112. $journal_comment->remarks=$req->get('remarks');
  113. $journal_comment->approaved_by=\Auth::user()->id;
  114. $journal_comment->save();
  115. }
  116. return response()->json(['ok'=>true]);
  117. }
  118. function payment($id)
  119. {
  120. //$data['journal_comment']=JournalComment::with('user')->where('debit_account',$id)->first();
  121. $data['fund_req']=FundRequest::where('id',$id)->first();
  122. $data['acc_head']=AccountHead::with('children')->where('type_id',1)->where('parent_head',0)->orderBy('name','asc')->get();
  123. $data['title']='Make Payment';
  124. return view('admin.journal.payment',$data);
  125. }
  126. function save_payment(Request $req)
  127. {
  128. $this->validate($req,[
  129. 'hdn_id'=>'required',
  130. 'account_head'=>'required',
  131. 'amount'=>'required|numeric'
  132. ]);
  133. $comment=$req->get('remarks');
  134. $head_id=$req->get('account_head');
  135. //Add Journal Entry
  136. $journal=new Journal();
  137. $journal->ref_id=$req->get('hdn_id');
  138. $journal->debit_account=5;
  139. $journal->credit_account=$head_id;
  140. $journal->description=$comment;
  141. $journal->amount=$req->get('amount');
  142. $journal->created_by=\Auth::user()->id;
  143. $journal->save();
  144. FundRequest::where('id',$req->get('hdn_req_id'))->delete();
  145. $req->session()->flash('success','Payment successfull !');
  146. return redirect('account/journal');
  147. }
  148. }