old-hotel-new/app/Http/Controllers/PrintController.php

128 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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;
}
}