128 lines
3.8 KiB
PHP
128 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\ConstFlow;
|
||
use Illuminate\Http\Request;
|
||
use App\Services\Admin\ConstFlowService;
|
||
|
||
class PrintController extends Controller
|
||
{
|
||
public function showConst(Request $request)
|
||
{
|
||
$name = '';
|
||
$time = now()->format('Y-m-d H:i:s');
|
||
$sn = '';
|
||
//默认list最少N行,最多N行;
|
||
$list = [];
|
||
$total = 0;
|
||
$bigTotal = '';
|
||
$adminName = '';
|
||
$timeZone = '';
|
||
|
||
$flow = ConstFlow::find($request->input('id', 0));
|
||
if($flow){
|
||
$rows = (new ConstFlowService())->makeFeelist($flow);
|
||
$flow->load(['oldman', 'adminuser']);
|
||
$name = $flow->oldman->name;
|
||
$time = $flow->created_at->format('Y-m-d H:i:s');
|
||
$idsn = $flow->idsn;
|
||
$ukeys = explode(',', $request->input('ukeys', ''));
|
||
$i = 0;
|
||
foreach($rows as $item){
|
||
|
||
if($i<4){
|
||
if(in_array($item['ukey'], $ukeys)){
|
||
$i++;
|
||
$list[] = $item;
|
||
}
|
||
}else{
|
||
break;
|
||
}
|
||
}
|
||
for($i; $i<4; $i++){
|
||
$list[] = [
|
||
'ukey' => '',
|
||
'name' => '',
|
||
'fee_name' => '',
|
||
'fee_value' => 0,
|
||
];
|
||
}
|
||
|
||
if(count($list) > 0){
|
||
$total = collect($list)->sum('fee_value');
|
||
if($total > 0){
|
||
$bigTotal = $this->num2chinese($total);
|
||
}else{
|
||
$bigTotal = '负'.$this->num2chinese(abs($total));
|
||
}
|
||
}
|
||
$adminName = $flow->adminuser->name;
|
||
$timeZone = substr($flow->start_at, 0, 10).'至'.substr($flow->end_at, 0, 10);
|
||
}
|
||
|
||
return view('print-const', compact('name', 'time', 'idsn', 'list', 'total', 'bigTotal', 'timeZone', 'adminName'));
|
||
}
|
||
|
||
|
||
private function num2chinese($num) {
|
||
$cnNums = array(
|
||
"", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
|
||
);
|
||
$cnIntUnits = array("", "拾", "佰", "仟");
|
||
$cnIntRadice = array("", "万", "亿", "兆");
|
||
$cnDecUnits = array("角", "分");
|
||
$cnInteger = "整";
|
||
|
||
$intStr = (string)$num;
|
||
$len = strlen($intStr);
|
||
if ($len > 15 || $num < 0) {
|
||
return false; // 超出处理范围
|
||
}
|
||
|
||
if ($num == 0) {
|
||
return "零元整";
|
||
}
|
||
|
||
$zeroCount = 0;
|
||
$outChinese = "";
|
||
for ($i = 0; $i < $len; $i++) {
|
||
$idx = $len - $i - 1;
|
||
$thisNum = (int)$intStr[$idx];
|
||
if ($thisNum == 0) {
|
||
$zeroCount++;
|
||
continue;
|
||
}
|
||
if ($zeroCount > 0) {
|
||
$outChinese = "零" . $outChinese; // 处理零
|
||
}
|
||
$zeroCount = 0;
|
||
$outChinese = $cnNums[$thisNum] . $cnIntUnits[$i % 4] . $outChinese;
|
||
if ($i % 4 == 0) {
|
||
$outChinese = $cnIntRadice[$i / 4] . $outChinese;
|
||
}
|
||
}
|
||
|
||
if ($zeroCount > 0) {
|
||
$outChinese = "零" . $outChinese; // 处理最后的零
|
||
}
|
||
|
||
$outChinese .= "元";
|
||
|
||
// 处理小数部分
|
||
if (strpos($num, '.') !== false) {
|
||
$decPart = substr($num, strpos($num, '.') + 1);
|
||
for ($i = 0, $len = strlen($decPart); $i < $len; $i++) {
|
||
$thisNum = (int)$decPart[$i];
|
||
if ($thisNum == 0) {
|
||
continue;
|
||
}
|
||
$outChinese .= $cnNums[$thisNum] . $cnDecUnits[$i];
|
||
}
|
||
} else {
|
||
$outChinese .= "整";
|
||
}
|
||
|
||
return $outChinese;
|
||
}
|
||
} |