CostAccounting.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. /**
  6. * 各月制造费用维护
  7. */
  8. class CostAccounting extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 各月制造费用维护菜单
  14. * @return void
  15. */
  16. public function getTab()
  17. {
  18. // 1. 请求方法验证
  19. if (!$this->request->isGet()) {
  20. $this->error('仅支持GET请求');
  21. }
  22. // 2. 使用静态变量避免重复创建数组
  23. static $tabs, $sist;
  24. if ($tabs === null) {
  25. $tabs = [
  26. '1、月度人工数据',
  27. '2、水电气直接费用',
  28. '3、水电气分摊费用',
  29. '4、其他待摊费用',
  30. '5、车间色度数'
  31. ];
  32. $sist = ['胶印车间', '凹丝印车间', '印后车间'];
  33. }
  34. // 3. 数据库查询优化
  35. $sql = "SELECT
  36. DATE_FORMAT(sczl_rq, '%Y%m') AS month
  37. FROM `设备_产量计酬`
  38. WHERE sczl_rq IS NOT NULL
  39. AND sczl_rq > '2025-01-01'
  40. GROUP BY DATE_FORMAT(sczl_rq, '%Y%m')
  41. ORDER BY DATE_FORMAT(sczl_rq, '%Y%m') DESC";
  42. // 执行查询
  43. $result = db()->query($sql);
  44. $months = array_column($result, 'month');
  45. // 4. 使用模板构建策略
  46. $data = [];
  47. $template = [];
  48. // 预构建模板
  49. foreach ($tabs as $tab) {
  50. $template[$tab] = ($tab === '2、水电气直接费用') ? $sist : $tab;
  51. }
  52. // 仅需单次循环赋值
  53. foreach ($months as $month) {
  54. $data[$month] = $template;
  55. }
  56. $this->success('成功', $data);
  57. }
  58. /**
  59. * 月度车间人工维护
  60. * @return void
  61. * @throws \think\db\exception\BindParamException
  62. * @throws \think\exception\PDOException
  63. */
  64. public function ArtificialAdd()
  65. {
  66. if($this->request->isPost() === false){
  67. $this->error('请求错误');
  68. }
  69. $param = Request::instance()->post();
  70. if (empty($param)) {
  71. $this->error('参数错误');
  72. }
  73. $data = [];
  74. foreach ($param as $key => $value) {
  75. $data[$key] = [
  76. 'Sys_ny' => $value['sys_ny'],
  77. '车间' => $value['sist'],
  78. '一线工资总额' => $value['number'],
  79. 'Sys_id' => $value['sys_id'],
  80. 'Sys_rq' => date('Y-m-d H:i:s', time())
  81. ];
  82. }
  83. $sql = db('成本v23_各月人工')->fetchSql(true)->insertAll($data);
  84. $res = db()->query($sql);
  85. if ($res !== false) {
  86. $this->success('新增成功');
  87. }else{
  88. $this->error('添加失败');
  89. }
  90. }
  91. /**
  92. * 月度人工维护列表
  93. * @return void
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function ArtificialAddList()
  99. {
  100. if($this->request->isGet() === false){
  101. $this->error('请求错误');
  102. }
  103. $param = $this->request->param();
  104. if (empty($param) || empty($param['Sys_ny'])) {
  105. $this->error('参数错误');
  106. }
  107. $list = db('成本v23_各月人工')
  108. ->where('Sys_ny', $param['Sys_ny'])
  109. ->field('Sys_ny, 车间, 一线工资总额, Sys_id as 创建用户, Sys_rq as 创建时间,Mod_rq as 修订时间,UniqID')
  110. ->select();
  111. $this->success('成功', $list);
  112. }
  113. /**
  114. * 月度人工维护修改
  115. * @return void
  116. * @throws \think\Exception
  117. * @throws \think\db\exception\BindParamException
  118. * @throws \think\exception\PDOException
  119. */
  120. public function ArtificialEdit()
  121. {
  122. if($this->request->isPost() === false){
  123. $this->error('请求错误');
  124. }
  125. $param = Request::instance()->post();
  126. if (empty($param)) {
  127. $this->error('参数错误');
  128. }
  129. $i = 0;
  130. foreach ($param as $value) {
  131. $sql = db('成本v23_各月人工')
  132. ->where('UniqID', $value['UniqID'])
  133. ->fetchSql(true)
  134. ->update(['一线工资总额'=>$value['number'],'Mod_rq'=>date('Y-m-d H:i:s', time())]);
  135. $res = db()->query($sql);
  136. if ($res === false) {
  137. $i++;
  138. }
  139. }
  140. if ($i === 0) {
  141. $this->success('修改成功');
  142. }else{
  143. $this->error('修改失败');
  144. }
  145. }
  146. /**
  147. * 月度人工数据右侧上方列表
  148. * @return void
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. */
  153. public function ArtificialList()
  154. {
  155. if (!$this->request->isGet()) {
  156. $this->error('仅支持GET请求');
  157. }
  158. $month = $this->request->param('month');
  159. if (empty($month)) {
  160. $this->error('month参数缺失或为空');
  161. }
  162. $list = db('成本v23_各月人工')->alias('a')
  163. ->join('设备_基本资料 b', 'a.车间 = b.使用部门')
  164. ->join('绩效工资汇总 c', 'a.Sys_ny = c.sys_ny AND c.sczl_jtbh = b.设备编号')
  165. ->join('成本_各月其他费用 d', 'a.Sys_ny = d.sys_ny','left')
  166. ->field([
  167. 'a.Sys_ny' => '年月',
  168. 'rtrim(a.车间)' => '车间',
  169. 'SUM(c.个人计件工资) + SUM(c.个人加班工资)' => '人工分摊因子总额',
  170. 'a.一线工资总额' => '车间实发工资',
  171. 'd.部门人员工资' => '部门人员工资实发',
  172. 'd.管理人员工资' => '管理人员工资实发'
  173. ])
  174. ->where('a.Sys_ny', $month)
  175. ->group('a.Sys_ny, a.车间')
  176. ->select();
  177. if (empty($list)) {
  178. $this->success('未找到匹配数据', []);
  179. }
  180. $this->success('获取成功', $list);
  181. }
  182. /**
  183. * 月度人工数据下方工单详情
  184. * @return void
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. * @throws \think\exception\DbException
  188. */
  189. public function ArtificialDetailList()
  190. {
  191. if ($this->request->isGet() === false) {
  192. $this->error('请求错误');
  193. }
  194. $param = $this->request->param();
  195. if (empty($param)) {
  196. $this->error('参数错误');
  197. }
  198. $month = $param['month'];
  199. $sist = $param['sist'];
  200. $list = db('工单_质量考核汇总')
  201. ->alias('a')
  202. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  203. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh and a.印件及工序 = c.yj_Yjno')
  204. ->join('工单_工艺资料 d', 'a.Gy0_gdbh = d.Gy0_gdbh and a.印件及工序 = d.Gy0_yjno and a.工序 = d.Gy0_gxh')
  205. ->field([
  206. 'a.sys_ny' => '年月',
  207. 'a.Gy0_gdbh' => '工单编号',
  208. 'a.印件及工序' => '印件号',
  209. 'a.工序' => '工序号',
  210. 'c.yj_yjmc' => '印件名称',
  211. 'd.Gy0_gxmc' => '工序名称',
  212. 'a.印件工序产量' => '班组车头产量',
  213. 'd.工价系数' => '工序难度系数',
  214. 'SUM(a.CjsJe)' => '计件工资',
  215. 'rtrim(b.使用部门)' => '车间名称',
  216. ])
  217. ->where('a.Sys_ny', $month)
  218. ->where('b.使用部门', $sist)
  219. ->group('a.Sys_ny, a.Gy0_gdbh,a.印件及工序,a.印件及工序')
  220. ->order('a.Gy0_gdbh')
  221. ->select();
  222. if (empty($list)) {
  223. $this->success('未找到数据');
  224. }else{
  225. $this->success('成功', $list);
  226. }
  227. }
  228. /**
  229. * 水电气直接费用右侧列表
  230. * @return void
  231. * @throws \think\db\exception\DataNotFoundException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. * @throws \think\exception\DbException
  234. */
  235. public function shuidianqiList()
  236. {
  237. if ($this->request->isGet() === false) {
  238. $this->error('请求错误');
  239. }
  240. $param = $this->request->param();
  241. if (empty($param)) {
  242. $this->error('参数错误');
  243. }
  244. $month = $param['month'];
  245. $sist = $param['sist'];
  246. $list = db('成本_各月水电气')
  247. ->field('rtrim(部门名称) as 部门名称,rtrim(设备编号) as 设备编号,rtrim(科目名称) as 设备名称,rtrim(耗电量) as 耗电量,
  248. rtrim(单位电价) as 单位电价,rtrim(耗气量) as 耗气量,rtrim(单位气价) as 单位气价,rtrim(Sys_id) as 创建用户,Sys_rq as 创建时间,UniqID,耗电量*单位电价 as 直接费用合计')
  249. ->where('Sys_ny', $month)
  250. ->where('部门名称', $sist)
  251. ->where('费用类型', '直接')
  252. ->group('Sys_ny, Sys_rq, UniqID')
  253. ->select();
  254. if (empty($list)) {
  255. $this->success('未找到数据');
  256. }else{
  257. $this->success('成功', $list);
  258. }
  259. }
  260. /**
  261. * 水电气下方工单详情
  262. * @return void
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. * @throws \think\exception\DbException
  266. */
  267. public function shuidianqiDetailList()
  268. {
  269. if ($this->request->isGet() === false) {
  270. $this->error('请求错误');
  271. }
  272. $param = $this->request->param();
  273. if (empty($param)) {
  274. $this->error('参数错误');
  275. }
  276. $month = substr($param['month'], 0, 4) . '-' . substr($param['month'], 4, 2);
  277. $machine = $param['machine'];
  278. $list = db('设备_产量计酬')
  279. ->alias('a')
  280. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh and a.sczl_yjno = b.yj_Yjno')
  281. ->field('a.sczl_gdbh as 工单编号,b.yj_yjmc as 印件名称,a.sczl_yjno as 印件号,a.sczl_gxh as 工序号,
  282. sum(a.sczl_cl)*a.sczl_ls as 班组车头产量,sum(a.sczl_设备运行工时) as 占用机时')
  283. ->where('a.sczl_rq','like', '%'.$month.'%')
  284. ->where('a.sczl_jtbh', $machine)
  285. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh')
  286. ->select();
  287. if (empty($list)) {
  288. $this->success('未找到数据');
  289. }
  290. foreach ($list as $k => $v) {
  291. $list[$k]['计件产量'] = $v['班组车头产量'];
  292. $list[$k]['水电气分摊因子'] = $v['占用机时'];
  293. $list[$k]['年月'] = $param['month'];
  294. }
  295. $this->success('成功', $list);
  296. }
  297. /**
  298. * 水电气分摊费用
  299. * @return void
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. * @throws \think\exception\DbException
  303. */
  304. public function shuidianqifentanList()
  305. {
  306. if ($this->request->isGet() === false) {
  307. $this->error('请求错误');
  308. }
  309. $param = $this->request->param();
  310. if (empty($param)) {
  311. $this->error('参数错误');
  312. }
  313. $month = $param['month'];
  314. $list = db('成本_各月水电气')
  315. ->field('rtrim(部门名称) as 部门名称,rtrim(设备编号) as 设备编号,rtrim(科目名称) as 设备名称,rtrim(耗电量) as 耗电量,
  316. rtrim(单位电价) as 单位电价,rtrim(耗气量) as 耗气量,rtrim(单位气价) as 单位气价,rtrim(Sys_id) as 创建用户,Sys_rq as 创建时间,UniqID,耗电量*单位电价 as 直接费用合计')
  317. ->where('Sys_ny', $month)
  318. ->where('费用类型', '分摊')
  319. ->group('Sys_ny, Sys_rq, UniqID')
  320. ->select();
  321. if (empty($list)) {
  322. $this->success('未找到数据');
  323. }else{
  324. $this->success('成功', $list);
  325. }
  326. }
  327. /**
  328. * 各月工单色度数
  329. * @return void
  330. * @throws \think\db\exception\BindParamException
  331. * @throws \think\db\exception\DataNotFoundException
  332. * @throws \think\db\exception\ModelNotFoundException
  333. * @throws \think\exception\DbException
  334. * @throws \think\exception\PDOException
  335. */
  336. public function ChromaticityDetailAdd()
  337. {
  338. if ($this->request->isGet() === false) {
  339. $this->error('请求错误');
  340. }
  341. $param = $this->request->param();
  342. if (empty($param)) {
  343. $this->error('参数错误');
  344. }
  345. $monthArr = db('成本v23_月度成本明细')->where(['sys_ny' => $param['month']])->select();
  346. if (empty($monthArr)) {
  347. $this->error('请先创建月度数据...');
  348. }
  349. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  350. $list = db('成本v23_月度成本明细')
  351. ->alias('a')
  352. ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号')
  353. ->join('工单_工艺资料 c','a.sczl_gdbh = c.Gy0_gdbh and a.sczl_yjno = c.Gy0_yjno and a.sczl_gxh = c.Gy0_gxh')
  354. ->field('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_jtbh,a.工序名称,sum(a.计件产量) as 产量,a.sczl_jtbh,a.sczl_ms,b.使用部门,
  355. sum(a.占用机时) as 通电工时,c.Gy0_dedh,c.Gy0_gxmc')
  356. ->where('a.sys_ny', $param['month'])
  357. ->whereIn('车间名称',$sist)
  358. ->group('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_jtbh')
  359. ->select();
  360. if (empty($list)) {
  361. $this->error('没找到生产数据');
  362. }
  363. $data = [];
  364. foreach ($list as $k => $v) {
  365. if ($v['sczl_ms'] === '0.00'){
  366. $list[$k]['sczl_ms'] = 1;
  367. }
  368. if (strpos($v['Gy0_gxmc'],'切废') !== false){
  369. $list[$k]['sczl_ms'] = 0.2;
  370. }
  371. $data[] = [
  372. '年月' => $param['month'],
  373. 'sczl_gdbh' => $v['sczl_gdbh'],
  374. 'sczl_yjno' => $v['sczl_yjno'],
  375. 'sczl_gxh' => $v['sczl_gxh'],
  376. 'sczl_jtbh' => $v['sczl_jtbh'],
  377. 'sczl_gxmc' => $v['工序名称'],
  378. 'sczl_ms' => $v['sczl_ms'],
  379. 'sczl_dedh' => $v['Gy0_dedh'],
  380. '通电时间' => $v['通电工时'],
  381. 'sczl_cl' => $v['产量'],
  382. '部门' => $v['使用部门'],
  383. 'Sys_ID' => $param['sys_id'],
  384. 'Sys_Rq' => date('Y-m-d H:i:s', time())
  385. ];
  386. }
  387. if (db('成本_各月色度数')->where('年月',$param['month'])->count() !== 0) {
  388. db('成本_各月色度数')->where('年月',$param['month'])->delete();
  389. }
  390. $sql = db('成本_各月色度数')->fetchSql(true)->insertAll($data);
  391. $res = db()->query($sql);
  392. if ($res !== false) {
  393. $this->success('成功');
  394. }else{
  395. $this->error('失败');
  396. }
  397. }
  398. /**
  399. * 各月车间色度数列表
  400. * @return void
  401. * @throws \think\db\exception\DataNotFoundException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. * @throws \think\exception\DbException
  404. */
  405. public function ChromaticityDetailList()
  406. {
  407. if ($this->request->isGet() === false) {
  408. $this->error('请求错误');
  409. }
  410. $param = $this->request->param();
  411. if (empty($param)) {
  412. $this->error('参数错误');
  413. }
  414. // 执行统计查询
  415. $stats = db('成本_各月色度数')
  416. ->field([
  417. 'rtrim(部门) AS workshop',
  418. 'SUM(sczl_cl * IF(sczl_ms=0, 1, sczl_ms)) AS total'
  419. ])
  420. ->where('部门', '<>', '') // 过滤空车间数据
  421. ->where('sczl_cl', '>', 0) // 过滤无效产量
  422. ->where('年月', $param['month'])
  423. ->group('部门')
  424. ->select();
  425. // 构造中文返回结果
  426. $result = [];
  427. foreach ($stats as $item) {
  428. $result[] = [
  429. '年月' => $param['month'],
  430. '车间' => $item['workshop'],
  431. '色度数' => number_format($item['total'], 2)
  432. ];
  433. }
  434. $this->success('成功', $result);
  435. }
  436. /**
  437. * 车间色度数详情列表
  438. * @return void
  439. * @throws \think\db\exception\DataNotFoundException
  440. * @throws \think\db\exception\ModelNotFoundException
  441. * @throws \think\exception\DbException
  442. */
  443. public function MonochromaticDetailList()
  444. {
  445. if ($this->request->isGet() === false) {
  446. $this->error('请求错误');
  447. }
  448. $param = $this->request->param();
  449. if (empty($param)) {
  450. $this->error('参数错误');
  451. }
  452. $list = db('成本_各月色度数')
  453. ->where('年月', $param['month'])
  454. ->where('部门', $param['sist'])
  455. ->order('sczl_gdbh')
  456. ->select();
  457. $this->success('成功', $list);
  458. }
  459. /**
  460. * 车间成本核算汇总
  461. * @return void
  462. * @throws \think\db\exception\DataNotFoundException
  463. * @throws \think\db\exception\ModelNotFoundException
  464. * @throws \think\exception\DbException
  465. */
  466. public function SummaryCostAccountingList()
  467. {
  468. if ($this->request->isGet() === false) {
  469. $this->error('请求错误');
  470. }
  471. $param = $this->request->param();
  472. if (empty($param)) {
  473. $this->error('参数错误');
  474. }
  475. $page = $this->request->param('page', 1);
  476. $pageSize = $this->request->param('limit', 30);
  477. $total = db('成本v23_月度成本明细')
  478. ->where('车间名称', $param['sist'])
  479. ->where('sys_ny', $param['month'])
  480. ->count();
  481. $list = db('成本v23_月度成本明细')
  482. ->where('车间名称', $param['sist'])
  483. ->where('sys_ny', $param['month'])
  484. ->order('sczl_gdbh')
  485. ->page($page, $pageSize)
  486. ->select();
  487. $data = [
  488. 'total' => $total,
  489. 'list' => $list,
  490. ];
  491. if (empty($list)) {
  492. $this->error('未找到数据');
  493. }else{
  494. $this->success('成功', $data);
  495. }
  496. }
  497. /**
  498. * 创建各月水电气分摊
  499. * @return void
  500. * @throws \think\db\exception\BindParamException
  501. * @throws \think\exception\PDOException
  502. */
  503. public function UtilitiesAdd()
  504. {
  505. if ($this->request->isPost() === false) {
  506. $this->error('请求错误');
  507. }
  508. $param = Request::instance()->post();
  509. if (empty($param)) {
  510. $this->error('参数错误');
  511. }
  512. $data = [];
  513. $currentTime = date('Y-m-d H:i:s');
  514. foreach ($param as $item) {
  515. // 验证必要字段是否存在
  516. if (empty($item['sys_ny']) || empty($item['sist']) || empty($item['科目名称'])) {
  517. $this->error('缺少必要参数');
  518. }
  519. // 确保数值字段正确处理为 decimal 类型
  520. $data[] = [
  521. 'Sys_ny' => $item['sys_ny'],
  522. '部门名称' => $item['sist'],
  523. '费用类型' => '分摊',
  524. '科目名称' => $item['科目名称'],
  525. '耗电量' => isset($item['耗电量']) ? (float)$item['耗电量'] : 0.00,
  526. '单位电价' => isset($item['单位电价']) ? (float)$item['单位电价'] : 0.00,
  527. '耗气量' => isset($item['耗气量']) ? (float)$item['耗气量'] : 0.00,
  528. '单位气价' => isset($item['单位气价']) ? (float)$item['单位气价'] : 0.00,
  529. 'Sys_id' => $item['sys_id'] ?? '',
  530. 'Sys_rq' => $currentTime
  531. ];
  532. }
  533. $result = db('成本_各月水电气')->where('Sys_ny', $data[0]['Sys_ny'])->where('费用类型','分摊')->delete();
  534. $sql = db('成本_各月水电气')->fetchSql(true)->insertAll($data);
  535. $res = db()->query($sql);
  536. if ($res !== false) {
  537. $this->success('成功');
  538. }else{
  539. $this->error('失败');
  540. }
  541. }
  542. /**
  543. * 各月水电气分摊费用列表
  544. * @return void
  545. * @throws \think\db\exception\DataNotFoundException
  546. * @throws \think\db\exception\ModelNotFoundException
  547. * @throws \think\exception\DbException
  548. */
  549. public function UtilitiesList()
  550. {
  551. if($this->request->isGet() === false){
  552. $this->error('请求错误');
  553. }
  554. $param = $this->request->param();
  555. if (empty($param)) {
  556. $this->error('参数错误');
  557. }
  558. $list = db('成本_各月水电气')
  559. ->field('rtrim(部门名称) as 部门名称,rtrim(科目名称) as 科目名称,耗电量,单位电价,耗气量,单位气价')
  560. ->where('费用类型', '分摊')
  561. ->where('Sys_ny', $param['month'])
  562. ->group('部门名称,科目名称')
  563. ->select();
  564. if (empty($list)) {
  565. $list = db('成本_各月水电气')
  566. ->field('rtrim(部门名称) as 部门名称,rtrim(科目名称) as 科目名称')
  567. ->where('费用类型', '分摊')
  568. ->group('部门名称,科目名称')
  569. ->select();
  570. }
  571. foreach ($list as $k => $v) {
  572. $list[$k]['年月'] = $param['month'];
  573. }
  574. $this->success('成功', $list);
  575. }
  576. /**
  577. * 成本汇总左侧列表
  578. * @return void
  579. */
  580. public function getSummaryTab()
  581. {
  582. if ($this->request->isGet() === false) {
  583. $this->error('请求错误');
  584. }
  585. $months = db('成本v23_月度成本明细')
  586. ->field('sys_ny AS year_month')
  587. ->group('Sys_ny')
  588. ->order('Sys_ny DESC')
  589. ->column('Sys_ny');
  590. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  591. $data = [];
  592. foreach ($months as $month) {
  593. $data[$month] = $sist;
  594. }
  595. $this->success('成功', $data);
  596. }
  597. //计算每个车间色度数
  598. private function CountSistChromaticity($month,$sist)
  599. {
  600. $data = db('成本v23_月度成本明细')
  601. ->where('sys_ny', $month)
  602. ->where('车间名称', $sist)
  603. ->group('车间名称')
  604. ->value('班组车头产量*sczl_ms as 色度数');
  605. return $data;
  606. }
  607. //计算每个机台色度数
  608. private function CountMachineChromatic($month,$sist)
  609. {
  610. $data = db('成本v23_月度成本明细')
  611. ->where('sys_ny', $month)
  612. ->where('车间名称', $sist)
  613. ->group('sczl_jtbh')
  614. ->value('班组车头产量*sczl_ms as 色度数');
  615. return $data;
  616. }
  617. //水电气分摊费用下方明细列表
  618. // public function shuidianqiMachineDetailList()
  619. // {
  620. // if ($this->request->isGet() === false) {
  621. // $this->error('请求错误');
  622. // }
  623. // $param = $this->request->param();
  624. // if (empty($param)) {
  625. // $this->error('参数错误');
  626. // }
  627. // $list = db('设备_基本资料')
  628. // ->alias('a')
  629. // ->join('成本_各月分摊系数 b', 'a.设备编号 = b.设备编号', 'LEFT')
  630. // ->field()
  631. //
  632. // }
  633. /**
  634. * 其他待摊费用
  635. * @return void
  636. * @throws \think\db\exception\BindParamException
  637. * @throws \think\exception\PDOException
  638. */
  639. public function PrepaidExpensesListEdit()
  640. {
  641. if ($this->request->isPost() === false) {
  642. $this->error('请求错误');
  643. }
  644. $param = Request::instance()->post();
  645. if (empty($param)) {
  646. $this->error('参数错误');
  647. }
  648. $data = [
  649. 'sys_ny' => $param['month'],
  650. '部门人员工资' => $param['salary1'],
  651. '管理人员工资' => $param['salary2'],
  652. '场地租金' => $param['rental'],
  653. '待摊折旧' => $param['depreciation'],
  654. '工资成本占比' => $param['proportion'],
  655. '其他' => $param['rest'],
  656. '后勤人员工资' => $param['profit'],
  657. 'sys_id' => $param['sys_id'],
  658. ];
  659. $result = db('成本_各月其他费用')->where('sys_ny', $param['month'])->count();
  660. if ($result > 0) {
  661. $data['mod_rq'] = date('Y-m-d H:i:s', time());
  662. $sql = db('成本_各月其他费用')->where('sys_ny', $param['month'])->fetchSql(true)->update($data);
  663. }else{
  664. $data['sys_rq'] = date('Y-m-d H:i:s', time());
  665. $sql = db('成本_各月其他费用')
  666. ->fetchSql(true)
  667. ->insert($data);
  668. }
  669. $res = db()->query($sql);
  670. if ($res !== false) {
  671. $this->success('成功');
  672. }else{
  673. $this->error('失败');
  674. }
  675. }
  676. /**
  677. * 其他待摊费用列表
  678. * @return void
  679. * @throws \think\db\exception\DataNotFoundException
  680. * @throws \think\db\exception\ModelNotFoundException
  681. * @throws \think\exception\DbException
  682. */
  683. public function PrepaidExpensesList()
  684. {
  685. if ($this->request->isGet() === false) {
  686. $this->error('请求错误');
  687. }
  688. $param = $this->request->param();
  689. if (empty($param)) {
  690. $this->error('参数错误');
  691. }
  692. $list = db('成本_各月其他费用')
  693. ->where('sys_ny', $param['month'])
  694. ->field('sys_ny as 年月,部门人员工资,管理人员工资,场地租金,待摊折旧,工资成本占比,其他,后勤人员工资,sys_id as 创建用户,sys_rq as 创建时间,mod_rq as 修改时间,UniqID')
  695. ->find();
  696. if (empty($list)) {
  697. $this->error('未找到数据');
  698. }else{
  699. $this->success('成功', $list);
  700. }
  701. }
  702. /**
  703. * 参考其他费用参考月份
  704. * @return void
  705. * @throws \think\db\exception\BindParamException
  706. * @throws \think\db\exception\DataNotFoundException
  707. * @throws \think\db\exception\ModelNotFoundException
  708. * @throws \think\exception\DbException
  709. * @throws \think\exception\PDOException
  710. */
  711. public function PrepaidExpensesDetailCopy()
  712. {
  713. if ($this->request->isGet() === false) {
  714. $this->error('请求错误');
  715. }
  716. $param = $this->request->param();
  717. if (empty($param)) {
  718. $this->error('参数错误');
  719. }
  720. $formMonth = $param['formMonth'];
  721. $toMonth = $param['toMonth'];
  722. $list = db('成本_各月其他费用')
  723. ->where('sys_ny', $formMonth)
  724. ->field('管理人员工资,场地租金,待摊折旧,工资成本占比,后勤人员工资')
  725. ->find();
  726. $list['sys_ny'] = $toMonth;
  727. $list['sys_id'] = $param['sys_id'];
  728. $list['sys_rq'] = date('Y-m-d H:i:s', time());
  729. $sql = db('成本_各月其他费用')
  730. ->fetchSql(true)
  731. ->insert($list);
  732. $res = db()->query($sql);
  733. if ($res !== false) {
  734. $this->success('成功');
  735. }else{
  736. $this->error('失败');
  737. }
  738. }
  739. /**
  740. * 各月水电气直接费用创建
  741. * @return void
  742. * @throws \think\db\exception\BindParamException
  743. * @throws \think\db\exception\DataNotFoundException
  744. * @throws \think\db\exception\ModelNotFoundException
  745. * @throws \think\exception\DbException
  746. * @throws \think\exception\PDOException
  747. */
  748. public function shuidianqiDetailAdd()
  749. {
  750. if ($this->request->isGet() === false) {
  751. $this->error('请求错误');
  752. }
  753. $param = $this->request->param();
  754. if (empty($param)) {
  755. $this->error('参数错误');
  756. }
  757. $month = substr($param['month'], 0, 4) . '-' . substr($param['month'], 4, 2);
  758. $sist = ['胶印车间','凹丝印车间','印后车间'];
  759. $list = db('设备_产量计酬')
  760. ->alias('a')
  761. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  762. ->where('a.sczl_rq', 'like', $month . '%')
  763. ->where('b.sys_sbID','<>','')
  764. ->where('b.使用部门','in',$sist)
  765. ->field('a.sczl_jtbh,sum(a.sczl_设备运行工时) as 通电工时,b.使用部门,rtrim(b.设备名称) as 设备名称')
  766. ->order('b.使用部门,a.sczl_jtbh')
  767. ->group('a.sczl_jtbh')
  768. ->select();
  769. $data = [];
  770. foreach ($list as $k => $v) {
  771. $data[] = [
  772. 'Sys_ny' => $param['month'],
  773. '部门名称' => $v['使用部门'],
  774. '费用类型' => '直接',
  775. '设备编号' => $v['sczl_jtbh'],
  776. '科目名称' => $v['设备名称'],
  777. '耗电量' => $v['通电工时'],
  778. '单位电价' => 0.69,
  779. 'Sys_id' => $param['sys_id'],
  780. 'Sys_rq' => date('Y-m-d H:i:s', time()),
  781. ];
  782. }
  783. $sql = db('成本_各月水电气')->fetchSql(true)->insertAll($data);
  784. $res = db()->query($sql);
  785. if ($res !== false) {
  786. $this->success('成功');
  787. }else{
  788. $this->error('失败');
  789. }
  790. }
  791. /**
  792. * 获取月份工序成本左侧菜单
  793. * @return void
  794. */
  795. public function getProcessTab()
  796. {
  797. if ($this->request->isGet() === false) {
  798. $this->error('请求错误');
  799. }
  800. $months = db('成本v23_月度成本明细')
  801. ->field('sys_ny AS year_month')
  802. ->group('Sys_ny')
  803. ->order('Sys_ny DESC')
  804. ->column('Sys_ny');
  805. $sist = ['01、切纸机组','02、胶印机组', '03、卷凹机组', '04、圆烫机组','05、圆切机组', '06、单凹机组',
  806. '07、丝印机组','08、喷码机组','09、烫金机组','10、模切机组','11、检品机组','12、覆膜机组' ];
  807. $data = [];
  808. foreach ($months as $month) {
  809. $data[$month] = $sist;
  810. }
  811. $this->success('成功', $data);
  812. }
  813. /**
  814. * 获取月份工序成本列表
  815. * @return void
  816. * @throws \think\Exception
  817. * @throws \think\db\exception\DataNotFoundException
  818. * @throws \think\db\exception\ModelNotFoundException
  819. * @throws \think\exception\DbException
  820. */
  821. public function ProcessCostAccountingList()
  822. {
  823. if ($this->request->isGet() === false) {
  824. $this->error('请求错误');
  825. }
  826. $param = $this->request->param();
  827. if (empty($param)) {
  828. $this->error('参数错误');
  829. }
  830. $page = $this->request->param('page', 1);
  831. $pageSize = $this->request->param('limit', 30);
  832. $total = db('成本v23_月度成本明细')
  833. ->alias('a')
  834. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  835. ->where('b.设备编组', $param['sist'])
  836. ->where('a.sys_ny', $param['month'])
  837. ->count();
  838. $list = db('成本v23_月度成本明细')
  839. ->alias('a')
  840. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  841. ->where('b.设备编组', $param['sist'])
  842. ->where('a.sys_ny', $param['month'])
  843. ->field('a.*')
  844. ->order('a.sczl_gdbh')
  845. ->page($page, $pageSize)
  846. ->select();
  847. $data = [
  848. 'total' => $total,
  849. 'list' => $list,
  850. ];
  851. if (empty($list)) {
  852. $this->error('未找到数据');
  853. }else{
  854. $this->success('成功', $data);
  855. }
  856. }
  857. /**
  858. * 计算完工成本明细
  859. * @return void
  860. * @throws \think\Exception
  861. * @throws \think\db\exception\BindParamException
  862. * @throws \think\db\exception\DataNotFoundException
  863. * @throws \think\db\exception\ModelNotFoundException
  864. * @throws \think\exception\DbException
  865. * @throws \think\exception\PDOException
  866. */
  867. public function CalculateCompletedCost()
  868. {
  869. if ($this->request->isGet() === false) {
  870. $this->error('请求错误');
  871. }
  872. $param = $this->request->param();
  873. if (empty($param)) {
  874. $this->error('参数错误');
  875. }
  876. $month = substr($param['month'], 0, 4) . '-' . substr($param['month'], 4, 2);
  877. $field = [
  878. 'a.jjcp_gdbh as 工单编号',
  879. 'sum(b.车间人工) as 车间人工',
  880. 'sum(b.部门人工附加) as 部门人工附加',
  881. 'sum(c.金额) as 直接材料',
  882. 'sum(b.分摊材料) as 分摊材料',
  883. 'sum(b.直接水电) as 直接水电',
  884. 'sum(b.分摊水电) as 分摊水电',
  885. 'sum(b.废气处理) as 废气处理',
  886. 'sum(b.锅炉 + b.热水锅炉) as 锅炉',
  887. 'sum(b.空压机) as 空压机',
  888. 'sum(b.真空鼓风机) as 真空鼓风机',
  889. 'sum(b.中央空调) as 中央空调',
  890. 'sum(b.分摊其他) as 分摊其他'
  891. ];
  892. $list = db('成品入仓')
  893. ->alias('a')
  894. ->join('成本v23_月度成本明细 b', 'a.jjcp_gdbh = b.sczl_gdbh')
  895. ->join('材料出库单列表 c', 'a.jjcp_gdbh = c.表体生产订单号', 'left')
  896. ->field($field)
  897. ->where('a.jjcp_sj', 'like', $month.'%')
  898. ->group('a.jjcp_gdbh')
  899. ->select();
  900. if (empty($list)) {
  901. $this->error('未找到完工工单');
  902. }
  903. // 获取所有工单编号
  904. $gdbhList = array_column($list, '工单编号');
  905. // 删除已存在的工单数据
  906. db('成本v23_完工工单车间成本汇总')
  907. ->where('工单编号', 'in', $gdbhList)
  908. ->delete();
  909. foreach ($list as $k => $v) {
  910. $list[$k]['考核直接材料'] = $v['直接材料'];
  911. $list[$k]['成本合计'] = $v['直接材料'] + $v['车间人工'] + $v['部门人工附加'] + $v['分摊材料'] + $v['直接水电'] + $v['分摊水电'] + $v['废气处理'] +
  912. $v['锅炉'] + $v['空压机'] + $v['真空鼓风机'] + $v['中央空调'] + $v['分摊其他'];
  913. $list[$k]['Sys_id'] = $param['sys_id'];
  914. $list[$k]['Sys_rq'] = date('Y-m-d H:i:s', time());
  915. }
  916. $sql = db('成本v23_完工工单车间成本汇总')->fetchSql(true)->insertAll($list);
  917. $res = db()->query($sql);
  918. if ($res !== false) {
  919. $this->success('计算成功');
  920. }else{
  921. $this->error('计算失败');
  922. }
  923. }
  924. /**
  925. * 完工工单成本左侧菜单
  926. * @return void
  927. * @throws \think\db\exception\DataNotFoundException
  928. * @throws \think\db\exception\ModelNotFoundException
  929. * @throws \think\exception\DbException
  930. */
  931. public function GetCompletionWorkOrderCostTab()
  932. {
  933. if ($this->request->isGet() === false) {
  934. $this->error('请求错误');
  935. }
  936. $list = db('成本v23_完工工单车间成本汇总')
  937. ->alias('a')
  938. ->join('工单_基本资料 b', 'a.工单编号 = b.Gd_gdbh')
  939. ->join('产品_基本资料 c', 'b.成品代号 = c.产品编号')
  940. ->where('b.成品代号', '<>', '')
  941. ->where('c.客户编号', '<>', '')
  942. ->where('c.客户名称', '<>', '')
  943. ->field([
  944. 'TRIM(c.客户名称) as 客户名称',
  945. 'TRIM(c.客户编号) as 客户编号'
  946. ])
  947. ->group('客户编号, 客户名称')
  948. ->order('客户编号')
  949. ->select();
  950. if (empty($list)) {
  951. $this->error('未找到完工数据');
  952. }
  953. $data = [];
  954. foreach ($list as $k => $v) {
  955. $data[] = $v['客户编号'] .'-->'.$v['客户名称'];
  956. }
  957. $this->success('成功', $data);
  958. }
  959. /**
  960. * 完工工单成本列表
  961. * @return void
  962. * @throws \think\db\exception\DataNotFoundException
  963. * @throws \think\db\exception\ModelNotFoundException
  964. * @throws \think\exception\DbException
  965. */
  966. public function CompletionWorkOrderCostList()
  967. {
  968. if ($this->request->isGet() === false) {
  969. $this->error('请求错误');
  970. }
  971. $param = $this->request->param();
  972. if (empty($param)) {
  973. $this->error('参数错误');
  974. }
  975. // 参数过滤和安全处理
  976. $param = array_map(function($value) {
  977. return is_string($value) ? trim($value) : $value;
  978. }, $param);
  979. $where = [];
  980. // 精确查询成品代号
  981. if (!empty($param['code'])) {
  982. $code = htmlspecialchars($param['code'], ENT_QUOTES);
  983. $where['b.成品代号'] = ['like', $code . '%'];
  984. }
  985. // 分页参数处理
  986. $page = max(1, intval($param['page'] ?? 1));
  987. $pageSize = max(1, min(100, intval($param['limit'] ?? 30))); // 限制最大100条
  988. // 搜索条件处理
  989. if (!empty($param['search'])) {
  990. $search = htmlspecialchars($param['search'], ENT_QUOTES);
  991. $where['a.工单编号|b.成品名称'] = ['like', $search . '%'];
  992. }
  993. // 获取总数用于分页(性能优化)
  994. $totalQuery = db('成本v23_完工工单车间成本汇总')
  995. ->alias('a')
  996. ->join('工单_基本资料 b', 'a.工单编号 = b.Gd_gdbh')
  997. ->where($where);
  998. $total = $totalQuery->count();
  999. // 分页查询数据
  1000. $list = [];
  1001. if ($total > 0) {
  1002. $offset = ($page - 1) * $pageSize;
  1003. $list = db('成本v23_完工工单车间成本汇总')
  1004. ->alias('a')
  1005. ->join('工单_基本资料 b', 'a.工单编号 = b.Gd_gdbh')
  1006. ->field([
  1007. 'a.*',
  1008. 'TRIM(b.成品代号) as 成品代号',
  1009. 'TRIM(b.成品名称) as 成品名称',
  1010. ])
  1011. ->where($where)
  1012. ->order('a.工单编号 DESC') // 添加排序,避免分页混乱
  1013. ->limit($offset, $pageSize)
  1014. ->select();
  1015. }
  1016. if (empty($list)) {
  1017. $this->error('未找到完工成本数据');
  1018. }
  1019. $data = [
  1020. 'total' => $total,
  1021. 'list' => $list,
  1022. ];
  1023. $this->success('成功', $data);
  1024. }
  1025. }