public function addMarketOrder(Request $request) { $metaData = MetaData::where('meta_key', 'server_status')->first(); if ($metaData['meta_value'] == 0) { $errorData = array('status' => 'fail', 'message' => 'Sorry! Server is closed. Please try later.', 'code' => '422'); return Response::json($errorData, 200); } try { $productType = Product::find($request['productTypeId']); if ($productType->type == 0 && $productType->lot_size * $request['lot'] < 25000) { $returnData = array('status' => 'fail', 'message' => 'Sorry! Invalid requist. Please try retail symbol.', 'code' => '422'); return Response::json($returnData, 200); } if ($productType->type == 1 && $productType->lot_size * $request['lot'] >= 25000) { $returnData = array('status' => 'fail', 'message' => 'Sorry! Invalid requist. Please try wholesale symbol.', 'code' => '422'); return Response::json($returnData, 200); } $stock = Stock::where('productTypeId', $request['productTypeId'])->where('branchId', $request['branchId'])->first(); $branch = Branch::find($request['branchId']); $stock->quantity = AddProduct::where('stockId', $stock->id)->where('added', 1)->sum('quantity') - AddProduct::where('stockId', $stock->id)->where('added', 0)->sum('quantity'); if ($stock->quantity < $productType->lot_size * $request['lot']) { $returnData = array('status' => 'fail', 'message' => 'Sorry! Insufficient stock. Please try low quantity.', 'code' => '422'); return Response::json($returnData, 200); } else { $login = Login::where('remember_token', '=', $request->header('token'))->where('status', '=', '1')->where('login_from', '=', $request->ip())->first(); $clientStock = new ClientStock(); $clientStock->type = 1; $clientStock->price = 4800; $clientStock->memberId = $login->member_id; $clientStock->stockId = $stock->id; $clientStock->amount = $productType->lot_size * $request['lot']; $clientStock->status = 0; $clientStock->commission = $productType->commision * $request['lot']; $clientStock->margin = $productType->margin * $request['lot']; $clientStock->delivery_charge = $productType->lot_size * $request['lot'] / 10 * $branch->delivery_charge; $clientStock->cost = $clientStock->amount / 10 * $clientStock->price; $clientStock->remaining_cost = $clientStock->cost - $clientStock->margin; $date = strtotime("+7 day"); $clientStock->delivery_date = date('Y-m-d', $date); $clientStock->ticket = $this->ticket_generate(); $clientStock->save(); $account = new Account(); $account->memberId = $login->member_id; $account->addedBy = $login->member_id; $account->type = 0; $account->amount = $clientStock->margin + $clientStock->commission; $account->ticket = $clientStock->ticket; $account->save(); $returnData = array('status' => 'ok', 'code' => '200', 'clientStock' => $clientStock, 'account' => $account); return Response::json($returnData, 200); } } catch (\Exception $e) { return $e->getMessage(); } }