AuxiliaryAccountService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace App\Service;
  3. use App\Model\AuxiliaryAccount;
  4. use App\Model\AuxiliaryAccountDetails;
  5. use App\Model\CalendarDetails;
  6. use App\Model\Device;
  7. use App\Model\Employee;
  8. use App\Model\ExpenseClaims;
  9. use App\Model\ExpenseClaimsDetails;
  10. use App\Model\Fee;
  11. use App\Model\Item;
  12. use App\Model\ItemDetails;
  13. use App\Model\MonthlyDdOrder;
  14. use App\Model\MonthlyDdOrderDetails;
  15. use App\Model\MonthlyPsOrder;
  16. use App\Model\MonthlyPsOrderDetails;
  17. use App\Model\MonthlyPwOrderDetails;
  18. use App\Model\RuleSet;
  19. use App\Model\RuleSetDetails;
  20. use Carbon\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. class AuxiliaryAccountService extends Service
  23. {
  24. public function auxiliaryAccountList($data,$user){
  25. $model = $this->setCommon($data, $user);
  26. $list = $this->limit($model,'',$data);
  27. $list = $this->fillData($list);
  28. return [true, $list];
  29. }
  30. //
  31. private function setCommon($data,$user, $field = []){
  32. if(empty($field)) $field = AuxiliaryAccount::$field;
  33. $data['top_depart_id'] = $user['top_depart_id'];
  34. $model = AuxiliaryAccount::Clear($user,$data);
  35. $model = $model->where('del_time',0)
  36. ->select($field)
  37. ->orderby('month', 'desc');
  38. if(! empty($data['month'])) $model->where('month', $data['month']);
  39. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  40. return $model;
  41. }
  42. public function fillData($data){
  43. if(empty($data['data'])) return $data;
  44. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  45. foreach ($data['data'] as $key => $value){
  46. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  47. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  48. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  49. }
  50. return $data;
  51. }
  52. public function auxiliaryAccountSummary($data,$user){
  53. //传参月份
  54. if(!isset($data['month'])) return [201,'月份必传'];
  55. $month = $data['month'];
  56. $monthStart = strtotime($month);
  57. $monthEnd = strtotime("$month +1 month") - 1;
  58. //获取员工汇总月份工资
  59. list($status,$employee_month_amount) = $this->employeeMonthAmount($data,$user,$monthStart,$monthEnd);
  60. if (!$status) return [$status,$employee_month_amount];
  61. //获取设备折旧费用
  62. list($status,$device_month_amount) = $this->deviceMonthAmount($data,$user,$monthStart,$monthEnd);
  63. if (!$status) return [$status,$device_month_amount];
  64. //获取费用汇总的费用数据
  65. list($status,$fee_month_amount) = $this->feeMonthAmount($data,$user,$monthStart,$monthEnd);
  66. if (!$status) return [$status,$fee_month_amount];
  67. return [true,[
  68. "employee_month_amount" => $employee_month_amount,
  69. "device_month_amount" => $device_month_amount,
  70. "fee_month_amount" => $fee_month_amount,
  71. ]];
  72. }
  73. private function employeeMonthAmount($month,$user,$monthStart,$monthEnd){
  74. $data['top_depart_id'] = $user['top_depart_id'];
  75. $month_ps_order = MonthlyPsOrder::Clear($user,$data);
  76. $month_ps_order_id = $month_ps_order->where("month",$monthStart)->first();
  77. if(empty($month_ps_order_id)){
  78. return [false,"请补充对应月份人员月度工资信息"];
  79. }else{
  80. $month_ps_order_id = $month_ps_order_id->id;
  81. }
  82. $model = MonthlyPsOrderDetails::Clear($user,$data);
  83. //总金额
  84. $total_amount = $model->where('main_id',$month_ps_order_id)->where('del_time',0)->sum("salary");
  85. //委内
  86. $entrust_in_amount = $model->where('main_id',$month_ps_order_id)->where("entrust_type",1)->sum("salary");
  87. //委外
  88. $entrust_out_amount = $model->where('main_id',$month_ps_order_id)->where("entrust_type",2)->sum("salary");
  89. return [true,[
  90. "total_amount" => $total_amount,
  91. "entrust_in_amount" => $entrust_in_amount,
  92. "entrust_out_amount" => $entrust_out_amount,
  93. ]];
  94. }
  95. private function deviceMonthAmount($month,$user,$monthStart,$monthEnd){
  96. $data['top_depart_id'] = $user['top_depart_id'];
  97. $month_ps_order = MonthlyDdOrder::Clear($user,$data);
  98. $month_ps_order_id = $month_ps_order->where('del_time',0)->where("month",$monthStart)->first();
  99. if(empty($month_ps_order_id)){
  100. return [false,"请补充对应月份设备月度折旧信息"];
  101. }else{
  102. $month_ps_order_id = $month_ps_order_id->id;
  103. }
  104. $model = MonthlyDdOrderDetails::Clear($user,$data);
  105. //总金额
  106. $total_amount = $model->where('main_id',$month_ps_order_id)->sum("depreciation_amount");
  107. return [true,[
  108. "total_amount" => $total_amount,
  109. ]];
  110. }
  111. private function feeMonthAmount($month,$user,$monthStart,$monthEnd){
  112. $data['top_depart_id'] = $user['top_depart_id'];
  113. $month_ps_order = ExpenseClaims::Clear($user,$data);
  114. $month_ps_order_id = $month_ps_order->where('del_time',0)->where("month",$monthStart)->first();
  115. if(empty($month_ps_order_id)){
  116. return [false,"请补充对应月份费用信息"];
  117. }else{
  118. $month_ps_order_id = $month_ps_order_id->id;
  119. }
  120. $model = new ExpenseClaimsDetails();
  121. //总金额
  122. $list = $model->where('expense_claims_id',$month_ps_order_id)->select("fee_id","amount","remark","entrust_type")->get()->toArray();
  123. ///分组
  124. $fee_type_list = Fee::Clear($user,$data)->where('del_time',0)->select("title","id","parent_id")->get()->toArray();
  125. return [true,$this->groupListByRoot($list,$fee_type_list)];
  126. }
  127. /**
  128. * 将报销明细按照一级费用分类进行分组
  129. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  130. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  131. * @return array
  132. */
  133. public function groupListByRoot(array $list, array $fee_type_list)
  134. {
  135. // 1. 建立 ID 索引,方便快速查找
  136. $idMap = array_column($fee_type_list, null, 'id');
  137. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  138. $childToRoot = [];
  139. foreach ($fee_type_list as $type) {
  140. $current = $type;
  141. // 向上追溯直到 parent_id 为 0,即找到一级分类
  142. while ($current['parent_id'] != 0 && isset($idMap[$current['parent_id']])) {
  143. $current = $idMap[$current['parent_id']];
  144. }
  145. $childToRoot[$type['id']] = [
  146. 'id' => $current['id'],
  147. 'title' => $current['title']
  148. ];
  149. }
  150. // 3. 遍历明细数据进行分组
  151. $grouped = [];
  152. foreach ($list as $item) {
  153. $feeId = $item['fee_id'];
  154. // 获取该费用对应的一级分类信息
  155. $rootInfo = $childToRoot[$feeId] ?? ['id' => 0, 'title' => '其他费用'];
  156. $rootId = $rootInfo['id'];
  157. if (!isset($grouped[$rootId])) {
  158. $grouped[$rootId] = [
  159. 'first_level_id' => $rootId,
  160. 'first_level_title' => $rootInfo['title'],
  161. 'details' => []
  162. ];
  163. }
  164. $grouped[$rootId]['details'][] = $item;
  165. }
  166. // 4. 重置数组索引并返回
  167. return array_values($grouped);
  168. }
  169. public function auxiliaryAccountAdd($data,$user){
  170. list($status,$msg) = $this->auxiliaryAccountRule($data, $user);
  171. if(!$status) return [$status,$msg];
  172. //费用报销结构
  173. try {
  174. DB::beginTransaction();
  175. $model = new ExpenseClaims();
  176. $model->code = $this->generateBillNo([
  177. 'top_depart_id' => $user['top_depart_id'],
  178. 'type' => ExpenseClaims::Order_type,
  179. 'period' => date("Ym", strtotime($data['month']))
  180. ]);
  181. $model->month = $data['order_time'] ?? 0;
  182. $model->crt_id = $user['id'];
  183. $model->top_depart_id = $user['top_depart_id'];
  184. $model->save();
  185. $data['top_depart_id'] = $user['top_depart_id'];
  186. $this->saveDetail($model->id, time(), $data);
  187. DB::commit();
  188. }catch (\Exception $exception){
  189. DB::rollBack();
  190. return [false,$exception->getMessage()];
  191. }
  192. return [true, ''];
  193. }
  194. private function saveDetail($id, $time, $data){
  195. if(! empty($data['details'])){
  196. $unit = [];
  197. foreach ($data['details'] as $value){
  198. $unit[] = [
  199. 'auxiliary_account_id' => $id,
  200. 'voucher_date' => strtotime($value['voucher_date']),
  201. 'voucher_type' => $value['voucher_type'],
  202. 'voucher_no' => $value['voucher_no'],
  203. 'voucher_remark' => $value['voucher_remark'],
  204. 'voucher_amount' => $value['voucher_amount'],
  205. 'entrust_type' => $value['entrust_type'],
  206. 'type' => $value['type'],
  207. 'fee_id' => $value['fee_id']??0,
  208. 'remark' => $value['remark']??"",
  209. 'entrust1_amount' => $value['entrust1_amount']??0,
  210. 'entrust2_amount' => $value['entrust2_amount']??0,
  211. 'aggregation_amount' => $value['aggregation_amount']??00,
  212. 'total_amount' => $value['total_amount'],
  213. 'top_depart_id' => $data['top_depart_id'],
  214. 'crt_time' => $time,
  215. ];
  216. }
  217. if(!empty($unit)) AuxiliaryAccountDetails::insert($unit);
  218. }
  219. }
  220. private function auxiliaryAccountRule($data,$user){
  221. if(!isset($data['month'])) return [false,"月份必传"];
  222. $monthStr = $data['month']; // 假设是 "2026-03"
  223. $monthStart = strtotime($monthStr); // 2026-03-01 00:00:00
  224. // 获取该月最后一秒:下个月 1 号减去 1 秒
  225. $monthEnd = strtotime("$monthStr +1 month") - 1;
  226. foreach ($data['details'] as $index => $item) {
  227. if (!isset($item['voucher_date'])) {
  228. return [false, "第" . ($index + 1) . "凭证日期缺失"];
  229. }
  230. if (!isset($item['voucher_no'])) {
  231. return [false, "第" . ($index + 1) . "凭证号数缺失"];
  232. }
  233. if (!isset($item['voucher_amount'])) {
  234. return [false, "第" . ($index + 1) . "凭证记载金额缺失"];
  235. }
  236. if (!isset($item['voucher_type'])) {
  237. return [false, "第" . ($index + 1) . "凭证类型缺失"];
  238. }
  239. if (!isset($item['aggregation_amount'])) {
  240. return [false, "第" . ($index + 1) . "凭证归集金额缺失"];
  241. }
  242. if (!isset($item['voucher_remark'])) {
  243. return [false, "第" . ($index + 1) . "凭证摘要缺失"];
  244. }
  245. // 将报销日期转换为时间戳
  246. $claimTime = strtotime($item['voucher_date']);
  247. // 判断:voucher_date 必须早于 month
  248. // 如果 voucher_date 是 2026-02-28,而 month 是 2026-03-01,则校验通过
  249. if ($claimTime < $monthStart || $claimTime > $monthEnd) {
  250. return [false, "第" . ($index + 1) . "凭证日期必须早于当前结算月份(" . $data['month'] . ")"];
  251. }
  252. }
  253. return [true,""];
  254. }
  255. public function auxiliaryAccountEdit($data,$user){
  256. list($status,$msg) = $this->auxiliaryAccountRule($data, $user, false);
  257. if(!$status) return [$status,$msg];
  258. try {
  259. DB::beginTransaction();
  260. $model = AuxiliaryAccount::where('id',$data['id'])->first();
  261. //不允许修改头部
  262. // $model->save();
  263. $time = time();
  264. AuxiliaryAccountDetails::where('del_time',0)
  265. ->where('auxiliary_account_id', $model->id)
  266. ->update(['del_time' => $time]);
  267. $data['top_depart_id'] = $user['top_depart_id'];
  268. $this->saveDetail($model->id, $time, $data);
  269. DB::commit();
  270. }catch (\Exception $exception){
  271. DB::rollBack();
  272. return [false,$exception->getMessage()];
  273. }
  274. return [true, ''];
  275. }
  276. public function auxiliaryAccountDel($data){
  277. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  278. try {
  279. DB::beginTransaction();
  280. $time = time();
  281. AuxiliaryAccount::where('del_time',0)
  282. ->whereIn('id',$data['id'])
  283. ->update(['del_time' => $time]);
  284. AuxiliaryAccountDetails::where('del_time',0)
  285. ->whereIn('auxiliary_account_id', $data['id'])
  286. ->update(['del_time' => $time]);
  287. DB::commit();
  288. }catch (\Exception $exception){
  289. DB::rollBack();
  290. return [false,$exception->getMessage()];
  291. }
  292. return [true, ''];
  293. }
  294. public function auxiliaryAccountDetail($data, $user){
  295. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  296. $customer = AuxiliaryAccount::where('del_time',0)
  297. ->where('id',$data['id'])
  298. ->first();
  299. if(empty($customer)) return [false,'单据不存在或已被删除'];
  300. $customer = $customer->toArray();
  301. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  302. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  303. $details = $this->getDetail($data['id']);
  304. $customer["details"] = $details;
  305. return [true, $customer];
  306. }
  307. private function getDetail($id){
  308. $data = AuxiliaryAccountDetails::where('del_time',0)
  309. ->where('auxiliary_account_id', $id)
  310. ->select('*',DB::raw("FROM_UNIXTIME(voucher_date, '%Y-%m-%d') as voucher_date"))
  311. ->get()->toArray();
  312. return $data;
  313. }
  314. private function UtcTime($time){
  315. return Carbon::createFromTimestamp($time, 'UTC')
  316. ->toIso8601ZuluString();
  317. }
  318. }