示例#1
0
 public function accountRecordSearch($eventId, Request $request)
 {
     $concatId = trim($request->get('account_id'), '0');
     $accountId = substr($concatId, strlen($concatId) - 1);
     $accountParentId = substr($concatId, 0, strlen($concatId) - 1);
     $validator = Validator::make(['event_id' => $eventId, 'account_id' => $accountId, 'account_parent_id' => $accountParentId], ['event_id' => 'required|exists:event,id', 'account_id' => 'required|exists:account,id', 'account_parent_id' => 'required|exists:account,parent_id']);
     // check the accountParentId + accountId exists in the account table
     $checkIdexist = DB::select('select * from account where id = :account_id and parent_id = :account_parent_id', ['account_id' => $accountId, 'account_parent_id' => $accountParentId]);
     if (empty($checkIdexist)) {
         $validator->messages()->merge(new MessageBag(['科目不存在']));
     }
     // success or fail
     if (!$validator->messages()->isEmpty()) {
         $result = ['type' => 'Failed', 'content' => $validator->messages()];
         return response()->json($result);
     } else {
         // to save all data to response
         $results = [];
         // catch all trade relate to the account id
         $diarys = Diary::where('account_id', '=', $accountId)->where('account_parent_id', '=', $accountParentId)->get();
         $diarys = $diarys->sortBy(function ($diary) {
             return $diary->trade->trade_at;
         });
         $accountDirection = DB::select('select direction from account where id = :account_id and parent_id = :account_parent_id', ['account_id' => $accountId, 'account_parent_id' => $accountParentId])[0]->direction;
         foreach ($diarys as $diary) {
             // check it's the correct event_id
             if ($diary->trade->event_id == (int) $eventId) {
                 $tempArray = ['trade_at' => $diary->trade->trade_at, 'trade_name' => $diary->trade->name, 'direction' => $diary->direction, 'debit_value' => $diary->direction ? $diary->amount : 0, 'credit_value' => $diary->direction ? 0 : $diary->amount, 'trade_comment' => $diary->trade->comment, 'account_direction' => $accountDirection];
                 array_push($results, $tempArray);
             }
         }
         return response()->json(['type' => 'Success', 'content' => json_encode($results)]);
     }
 }
示例#2
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     DB::table('trade')->delete();
     DB::table('diary')->delete();
     Trade::create(['name' => '支付中研院場地租金', 'handler' => '劉彥君', 'comment' => '跟歷年一樣的金額', 'event_id' => 1, 'user_id' => 1, 'trade_at' => '2015-12-31']);
     Diary::create(['direction' => true, 'amount' => 1000, 'trade_id' => 1, 'account_id' => 1, 'account_parent_id' => 111]);
     Diary::create(['direction' => false, 'amount' => 1000, 'trade_id' => 1, 'account_id' => 7, 'account_parent_id' => 211]);
 }
示例#3
0
 public function destroy(Diary $diary)
 {
     $diary->delete();
     \Flash::success('Entry successfully deleted.');
     return redirect('diary');
 }
示例#4
0
 public function editEventDiary(Request $request, $eventId)
 {
     //first validate
     $validatorTotal = Validator::make(['event_id' => $eventId, 'trade_id' => $request->get('trade_id'), 'trade_at' => $request->get('trade_at'), 'name' => $request->get('name'), 'handler' => $request->get('handler'), 'comment' => $request->get('comment'), 'debit_array' => $request->get('debit_array'), 'credit_array' => $request->get('credit_array')], ['event_id' => 'required|exists:event,id', 'trade_id' => 'required|exists:trade,id', 'trade_at' => 'required|date', 'name' => 'required|max:95', 'handler' => 'max:15', 'comment' => 'string', 'debit_array' => 'required|json', 'credit_array' => 'required|json']);
     //decode string to json
     $debitDictionary = json_decode($request->get('debit_array'));
     $creditDictionary = json_decode($request->get('credit_array'));
     //to check the balance
     $debitTotal = 0;
     $creditTotal = 0;
     //validate debit account
     foreach ($debitDictionary as $debit) {
         $debitConcatId = trim($debit->account, "0");
         $debitAccountId = substr($debitConcatId, strlen($debitConcatId) - 1);
         $debitAccountParentId = substr($debitConcatId, 0, strlen($debitConcatId) - 1);
         $validator = Validator::make(['account_id' => $debitAccountId, 'account_parent_id' => $debitAccountParentId, 'amount' => $debit->amount], ['account_id' => 'required|exists:account,id', 'account_parent_id' => 'required|exists:account,parent_id', 'amount' => 'required|numeric|integer|min:1']);
         //merge the error message
         if ($validator->fails()) {
             $validatorTotal->messages()->merge($validator->messages());
         }
         $debitTotal = $debitTotal + $debit->amount;
     }
     //validate credit account
     foreach ($creditDictionary as $credit) {
         $creditConcatId = trim($credit->account, "0");
         $creditAccountId = substr($creditConcatId, strlen($creditConcatId) - 1);
         $creditaAccountParentId = substr($creditConcatId, 0, strlen($creditConcatId) - 1);
         $validator = Validator::make(['account_id' => $creditAccountId, 'account_parent_id' => $creditaAccountParentId, 'amount' => $credit->amount], ['account_id' => 'required|exists:account,id', 'account_parent_id' => 'required|exists:account,parent_id', 'amount' => 'required|numeric|integer|min:1']);
         //merge the error message
         if ($validator->fails()) {
             $validatorTotal->messages()->merge($validator->messages());
         }
         $creditTotal = $creditTotal + $credit->amount;
     }
     // check balace
     if ($debitTotal !== $creditTotal) {
         $validatorTotal->messages()->merge(new MessageBag(['借貸不平衡']));
     }
     // check date
     if (date("Y-m-d", strtotime($request->get('trade_at'))) > date("Y-m-d")) {
         $validatorTotal->messages()->merge(new MessageBag(['交易尚未發生']));
     }
     // check the upload file
     $files = $request->file('diary_attached_files');
     if ($request->hasFile('diary_attached_files')) {
         foreach ($files as $file) {
             if (!$file->isValid()) {
                 $validatorTotal->messages()->merge(new MessageBag(['上傳收據檔案失敗']));
             } else {
                 // check the file name
                 if (!in_array($file->getClientOriginalExtension(), ['jpeg', 'png', 'jpg', 'pdf'])) {
                     $validatorTotal->messages()->merge(new MessageBag(['上傳收據檔案檔名不符']));
                 }
                 // check the file size
                 if ($file->getMaxFilesize() < $file->getClientSize()) {
                     $validatorTotal->messages()->merge(new MessageBag(['上傳收據檔案過大']));
                 }
             }
         }
     }
     //!!!end validate!!!
     if (!$validatorTotal->messages()->isEmpty()) {
         return redirect()->route('event::diary', ['eventId' => $eventId, 'page' => $request->get('current_page')])->with('errors' . $request->get('trade_id'), $validatorTotal->messages());
     } else {
         $transaction = DB::transaction(function () use($request, $eventId, $debitDictionary, $creditDictionary, $files) {
             //edit the trade
             Trade::find($request->get('trade_id'))->update(['name' => $request->get('name'), 'handler' => $request->get('handler'), 'comment' => $request->get('comment'), 'event_id' => $eventId, 'trade_at' => date("Y-m-d", strtotime($request->get('trade_at'))), 'user_id' => Session::get('user')->id]);
             $diarys = Trade::find($request->get('trade_id'))->diary;
             foreach ($diarys as $diary) {
                 $diary->delete();
             }
             foreach ($debitDictionary as $debit) {
                 $debitConcatId = trim($debit->account, "0");
                 $debitAccountId = substr($debitConcatId, strlen($debitConcatId) - 1);
                 $debitAccountParentId = substr($debitConcatId, 0, strlen($debitConcatId) - 1);
                 Diary::create(['direction' => 1, 'amount' => $debit->amount, 'trade_id' => $request->get('trade_id'), 'account_id' => $debitAccountId, 'account_parent_id' => $debitAccountParentId]);
             }
             foreach ($creditDictionary as $credit) {
                 $creditConcatId = trim($credit->account, "0");
                 $creditAccountId = substr($creditConcatId, strlen($creditConcatId) - 1);
                 $creditaAccountParentId = substr($creditConcatId, 0, strlen($creditConcatId) - 1);
                 Diary::create(['direction' => 0, 'amount' => $credit->amount, 'trade_id' => $request->get('trade_id'), 'account_id' => $creditAccountId, 'account_parent_id' => $creditaAccountParentId]);
             }
             // move the file to the storage/app/user-upload
             if ($request->hasFile('diary_attached_files')) {
                 foreach ($files as $key => $file) {
                     // create the file's storage path & name
                     $filePath = join(DIRECTORY_SEPARATOR, ['app', 'diary', $eventId, $request->get('trade_id')]);
                     $useableId = DB::select('select max(file_number) + 1 as useable_id from diary_attached_files where event_id = :eid and trade_id = :tid', ['eid' => $eventId, 'tid' => $request->get('trade_id')])[0]->useable_id;
                     if ($useableId === null) {
                         $useableId = 1;
                     }
                     $fileName = join('-', [$eventId, $request->get('trade_id'), $useableId, '.' . $file->getClientOriginalExtension()]);
                     // move the file
                     $file->move(storage_path($filePath), $fileName);
                     DiaryAttachedFiles::create(['event_id' => $eventId, 'trade_id' => $request->get('trade_id'), 'file_number' => $useableId, 'file_name' => $fileName, 'uploader' => Session::get('user')->id]);
                 }
             }
         });
     }
     Cache::forget('tradeList-' . $eventId . '-' . $request->get('current_page'));
     if (is_null($transaction)) {
         Session::flash('toast_message', ['type' => 'success', 'content' => '成功編輯交易「' . $request->get('name') . '」']);
         return redirect()->route('event::diary', ['eventId' => $eventId, 'page' => $request->get('current_page')]);
     } else {
         Session::flash('toast_message', ['type' => 'error', 'content' => '編輯交易「' . $request->get('name') . '」失敗']);
         return redirect()->route('event::diary', ['eventId' => $eventId, 'page' => $request->get('current_page')]);
     }
 }