/** * 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]); }
public function finish() { $legacyNum = Input::get('id'); $sellerId = Session::get(MateMiddleware::$VERIFY); $legacy = Legacy::find($legacyNum); if ($legacy->owner->id != $sellerId) { return 'invalid'; } DB::beginTransaction(); Trade::create(['id' => $legacy->id, 'buyer' => Input::get('buyer'), 'seller' => $sellerId, 'description' => $legacy->des, 'img' => $legacy->img]); $legacy->delete(); DB::commit(); return 'success'; }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(TradeRequest $request) { Trade::create($request->all()); return redirect('trades'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $input = Request::all(); Trade::create($input); return redirect('admin/trade'); }
public function addEventDiary(Request $request, $eventId) { // first validate $validatorTotal = Validator::make(['event_id' => $eventId, '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_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()) { $request->flash(); return redirect()->route('event::diary', ['eventId' => $eventId, 'page' => $request->get('current_page')])->with('errors', $validatorTotal->messages()); } else { $transaction = DB::transaction(function () use($request, $eventId, $debitDictionary, $creditDictionary, $files) { //create the trade $trade = Trade::create(['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]); 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' => $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' => $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, $trade->id]); $fileName = join('-', [$eventId, $trade->id, $key + 1, '.' . $file->getClientOriginalExtension()]); // move the file $file->move(storage_path($filePath), $fileName); DiaryAttachedFiles::create(['event_id' => $eventId, 'trade_id' => $trade->id, 'file_number' => $key + 1, 'file_name' => $fileName, 'uploader' => Session::get('user')->id]); } } }); } Cache::forget('tradeList-' . $eventId . '-' . $request->get('total_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('total_page')]); } else { Session::flash('toast_message', ['type' => 'error', 'content' => '新增交易「' . $request->get('name') . '」失敗']); return redirect()->route('event::diary', ['eventId' => $eventId, 'page' => $request->get('current_page')]); } }