CostAccounting.php 38 KB

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