Laravel
Line bot
public function handleMessageText($string)
{
// 在此METHOD處理文字訊息
// 轉小寫
$string = strtolower($string);
// 判斷該文字在stocks資料庫中存在與否
// !!!!!!!!!!!!!這個邏輯有同代碼多筆的問題存在!!!!待解決
// 目前先這樣測試功能
$contents = [];
$tradeFeeRate = 0.001425;
$tradeTaxRate = 0.003;
$tradeDiscount = 0.28;
if ($string === 'stocks') {
$allStocks = Stocks::get();
foreach ($allStocks as $stock) {
// 未含交易手續費
$sum = $stock->purchase_price * $stock->amount;
// 含交易手續費
$sumAfterFee = floor($sum * (1 + $tradeFeeRate * $tradeDiscount));
// 取得現在股價
$nowStockPrice = round($this->getStockPrice($stock->stock_code), 2);
// $nowStockPrice = 20;
// 計算現在市價
$marketValue = $stock->amount * $nowStockPrice;
// 含交易手續費&交易稅
$marketValueSoldPrice = floor($marketValue * (1 - ($tradeFeeRate * $tradeDiscount) - ($tradeTaxRate)));
// 與購買時的差額
$profit = number_format($marketValueSoldPrice - $sumAfterFee);
// 賺:紅色 虧:綠色
$profitColor = $profit >= 0 ? "#ff0000" : "#00ff00";
// $totalCost += $sumAfterFee;
// // 單項明細
// 加手續費後取得金額
$sumAfterFee = number_format($sumAfterFee);
// 持有天數
$obtainDays = Carbon::parse($stock->purchase_date)->diffInDays(now());
$contents[] =
[
"type" => "text",
"text" => "{$stock->stock_code}",
"weight" => "bold",
"size" => "3xl",
"align" => "center"
];
$contents[] =
[
"type" => "box",
"layout" => "vertical",
"margin" => "lg",
"spacing" => "sm",
"contents" => [
[
"type" => "box",
"layout" => "baseline",
"spacing" => "xs",
"contents" => [
[
"type" => "text",
"text" => "持有天數",
"color" => "#aaaaaa",
"size" => "sm",
"flex" => 2,
"offsetEnd" => "none"
],
[
"type" => "text",
"text" => "{$obtainDays}",
"wrap" => true,
"color" => "#0000ff",
"size" => "xxl",
"flex" => 5,
"align" => "end"
]
]
],
[
"type" => "box",
"layout" => "baseline",
"spacing" => "sm",
"contents" => [
[
"type" => "text",
"text" => "現時盈虧",
"color" => "#aaaaaa",
"size" => "sm",
"flex" => 2
],
[
"type" => "text",
"text" => "{$profit}",
"wrap" => true,
"color" => $profitColor,
"size" => "xxl",
"flex" => 5,
"align" => "end"
]
]
]
]
];
$contents[] =
[
"type" => "separator"
];
}
} else {
$stockData = Stocks::where('stock_code', '=', $string)->first();
if ($stockData !== null) {
// 未含交易手續費
$sum = $stockData->purchase_price * $stockData->amount;
// 含交易手續費
$sumAfterFee = floor($sum * (1 + $tradeFeeRate * $tradeDiscount));
// 取得現在股價
$nowStockPrice = round($this->getStockPrice($stockData->stock_code), 2);
// $nowStockPrice = 20;
// 計算現在市價
$marketValue = $stockData->amount * $nowStockPrice;
// 含交易手續費&交易稅
$marketValueSoldPrice = floor($marketValue * (1 - ($tradeFeeRate * $tradeDiscount) - ($tradeTaxRate)));
// 與購買時的差額
$profit = number_format($marketValueSoldPrice - $sumAfterFee);
// 賺:紅色 虧:綠色
$profitColor = $profit >= 0 ? "#ff0000" : "#00ff00";
// 持有天數
$obtainDays = Carbon::parse($stockData->purchase_date)->diffInDays(now());
$contents[] =
[
"type" => "text",
"text" => "{$stockData->stock_code}",
"weight" => "bold",
"size" => "3xl",
"align" => "center"
];
$contents[] =
[
"type" => "box",
"layout" => "vertical",
"margin" => "lg",
"spacing" => "sm",
"contents" => [
[
"type" => "box",
"layout" => "baseline",
"spacing" => "xs",
"contents" => [
[
"type" => "text",
"text" => "持有天數",
"color" => "#aaaaaa",
"size" => "sm",
"flex" => 2,
"offsetEnd" => "none"
],
[
"type" => "text",
"text" => "{$obtainDays}",
"wrap" => true,
"color" => "#0000ff",
"size" => "xxl",
"flex" => 5,
"align" => "end"
]
]
],
[
"type" => "box",
"layout" => "baseline",
"spacing" => "sm",
"contents" => [
[
"type" => "text",
"text" => "現時盈虧",
"color" => "#aaaaaa",
"size" => "sm",
"flex" => 2
],
[
"type" => "text",
"text" => "{$profit}",
"wrap" => true,
"color" => $profitColor,
"size" => "xxl",
"flex" => 5,
"align" => "end"
]
]
]
]
];
} else {
$contents[] =
[
"type" => "text",
"text" => "無資料",
"weight" => "bold",
"size" => "3xl",
"align" => "center"
];
}
}
return $contents;
}
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up