CostAccounting.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 ChromaticityAdd()
  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. $list = db('绩效工资汇总')
  341. ->alias('a')
  342. ->join('工单_工艺资料 b','a.sczl_gdbh = b.Gy0_gdbh and a.sczl_yjno = b.Gy0_yjno and a.sczl_gxh = b.Gy0_gxh')
  343. ->join('设备_基本资料 c','a.sczl_jtbh = c.设备编号','LEFT')
  344. ->join('工单_印件资料 d','a.sczl_gdbh = d.Yj_Gdbh and a.sczl_yjno = d.yj_Yjno')
  345. ->field('a.sczl_gdbh as 工单编号,a.sczl_yjno as 印件号,a.sczl_gxh as 工序号,sum(a.班组车头产量) as 班组车头产量,b.Gy0_gxmc as 工序名称,
  346. a.sczl_ms as 墨色数,c.使用部门,b.印刷方式,b.版距,b.工价系数,a.sczl_jtbh,d.yj_yjmc as 印件名称,sum(a.车头产量占用机时) as 占用机时,a.sys_rq as 年月,
  347. a.工序难度系数,sum(a.班组换算产量) as 班组换算产量,a.千件工价')
  348. ->where('a.sys_ny', $param['month'])
  349. ->group('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_jtbh')
  350. ->select();
  351. $data = [];
  352. foreach ($list as $k => $v) {
  353. if ($v['版距'] === '0.0'){
  354. $list[$k]['版距'] = 1000;
  355. }
  356. if ($v['墨色数'] === '0.00'){
  357. $list[$k]['墨色数'] = 1;
  358. }
  359. if (strpos($v['工序名称'],'切废')){
  360. $list[$k]['墨色数'] = 0.2;
  361. }
  362. $chanliang = $v['班组车头产量']*$v['工序难度系数'] + $v['班组换算产量'];
  363. $data[] = [
  364. '车间名称' => $v['使用部门'],
  365. 'sys_ny' => $param['month'],
  366. 'sczl_gdbh' => $v['工单编号'],
  367. '印件名称' => $v['印件名称'],
  368. 'sczl_yjno' => $v['印件号'],
  369. 'sczl_gxh' => $v['工序号'],
  370. '工序名称' => $v['工序名称'],
  371. 'sczl_jtbh' => $v['sczl_jtbh'],
  372. '卷张换算系数' => $list[$k]['版距']/1000,
  373. '占用机时' => $v['占用机时'],
  374. '班组车头产量' => $v['班组车头产量'],
  375. 'sczl_ms' => $list[$k]['墨色数'],
  376. '工序难度系数' => $v['工序难度系数'],
  377. '班组换算产量' => $v['班组换算产量'],
  378. '千件工价' => $v['千件工价'],
  379. '计件产量' => $chanliang,
  380. '水电分摊因子' => $v['占用机时'],
  381. '材料分摊因子' => $chanliang,
  382. '人工分摊因子' => ($chanliang/1000)*$v['千件工价'],
  383. 'Sys_id' => $param['sys_id'],
  384. 'Sys_rq' => date('Y-m-d H:i:s', time())
  385. ];
  386. }
  387. db('成本v23_月度成本明细')->where('sys_ny', $param['month'])->delete();
  388. $sql = db('成本v23_月度成本明细')->fetchSql(true)->insertAll($data);
  389. $res = db()->query($sql);
  390. if ($res !== false) {
  391. $this->success('成功');
  392. }else{
  393. $this->error('失败');
  394. }
  395. }
  396. /**
  397. * 各月工单色度数
  398. * @return void
  399. * @throws \think\db\exception\BindParamException
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\ModelNotFoundException
  402. * @throws \think\exception\DbException
  403. * @throws \think\exception\PDOException
  404. */
  405. public function ChromaticityDetailAdd()
  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. $monthArr = db('成本v23_月度成本明细')->where(['sys_ny' => $param['month']])->select();
  415. if (empty($monthArr)) {
  416. $this->error('请先创建月度数据...');
  417. }
  418. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  419. $list = db('成本v23_月度成本明细')
  420. ->alias('a')
  421. ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号')
  422. ->join('工单_工艺资料 c','a.sczl_gdbh = c.Gy0_gdbh and a.sczl_yjno = c.Gy0_yjno and a.sczl_gxh = c.Gy0_gxh')
  423. ->field('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_jtbh,a.工序名称,sum(a.计件产量) as 产量,a.sczl_jtbh,a.sczl_ms,b.使用部门,
  424. sum(a.占用机时) as 通电工时,c.Gy0_dedh,c.Gy0_gxmc')
  425. ->where('a.sys_ny', $param['month'])
  426. ->whereIn('车间名称',$sist)
  427. ->group('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_jtbh')
  428. ->select();
  429. if (empty($list)) {
  430. $this->error('没找到生产数据');
  431. }
  432. $data = [];
  433. foreach ($list as $k => $v) {
  434. if ($v['sczl_ms'] === '0.00'){
  435. $list[$k]['sczl_ms'] = 1;
  436. }
  437. if (strpos($v['Gy0_gxmc'],'切废') !== false){
  438. $list[$k]['sczl_ms'] = 0.2;
  439. }
  440. $data[] = [
  441. '年月' => $param['month'],
  442. 'sczl_gdbh' => $v['sczl_gdbh'],
  443. 'sczl_yjno' => $v['sczl_yjno'],
  444. 'sczl_gxh' => $v['sczl_gxh'],
  445. 'sczl_jtbh' => $v['sczl_jtbh'],
  446. 'sczl_gxmc' => $v['工序名称'],
  447. 'sczl_ms' => $v['sczl_ms'],
  448. 'sczl_dedh' => $v['Gy0_dedh'],
  449. '通电时间' => $v['通电工时'],
  450. 'sczl_cl' => $v['产量'],
  451. '部门' => $v['使用部门'],
  452. 'Sys_ID' => $param['sys_id'],
  453. 'Sys_Rq' => date('Y-m-d H:i:s', time())
  454. ];
  455. }
  456. if (db('成本_各月色度数')->where('年月',$param['month'])->count() !== 0) {
  457. db('成本_各月色度数')->where('年月',$param['month'])->delete();
  458. }
  459. $sql = db('成本_各月色度数')->fetchSql(true)->insertAll($data);
  460. $res = db()->query($sql);
  461. if ($res !== false) {
  462. $this->success('成功');
  463. }else{
  464. $this->error('失败');
  465. }
  466. }
  467. /**
  468. * 各月车间色度数列表
  469. * @return void
  470. * @throws \think\db\exception\DataNotFoundException
  471. * @throws \think\db\exception\ModelNotFoundException
  472. * @throws \think\exception\DbException
  473. */
  474. public function ChromaticityDetailList()
  475. {
  476. if ($this->request->isGet() === false) {
  477. $this->error('请求错误');
  478. }
  479. $param = $this->request->param();
  480. if (empty($param)) {
  481. $this->error('参数错误');
  482. }
  483. // 执行统计查询
  484. $stats = db('成本_各月色度数')
  485. ->field([
  486. 'rtrim(部门) AS workshop',
  487. 'SUM(sczl_cl * IF(sczl_ms=0, 1, sczl_ms)) AS total'
  488. ])
  489. ->where('部门', '<>', '') // 过滤空车间数据
  490. ->where('sczl_cl', '>', 0) // 过滤无效产量
  491. ->where('年月', $param['month'])
  492. ->group('部门')
  493. ->select();
  494. // 构造中文返回结果
  495. $result = [];
  496. foreach ($stats as $item) {
  497. $result[] = [
  498. '年月' => $param['month'],
  499. '车间' => $item['workshop'],
  500. '色度数' => number_format($item['total'], 2)
  501. ];
  502. }
  503. $this->success('成功', $result);
  504. }
  505. /**
  506. * 车间色度数详情列表
  507. * @return void
  508. * @throws \think\db\exception\DataNotFoundException
  509. * @throws \think\db\exception\ModelNotFoundException
  510. * @throws \think\exception\DbException
  511. */
  512. public function MonochromaticDetailList()
  513. {
  514. if ($this->request->isGet() === false) {
  515. $this->error('请求错误');
  516. }
  517. $param = $this->request->param();
  518. if (empty($param)) {
  519. $this->error('参数错误');
  520. }
  521. $list = db('成本_各月色度数')
  522. ->where('年月', $param['month'])
  523. ->where('部门', $param['sist'])
  524. ->order('sczl_gdbh')
  525. ->select();
  526. $this->success('成功', $list);
  527. }
  528. /**
  529. * 车间成本核算汇总
  530. * @return void
  531. * @throws \think\db\exception\DataNotFoundException
  532. * @throws \think\db\exception\ModelNotFoundException
  533. * @throws \think\exception\DbException
  534. */
  535. public function SummaryCostAccountingList()
  536. {
  537. if ($this->request->isGet() === false) {
  538. $this->error('请求错误');
  539. }
  540. $param = $this->request->param();
  541. if (empty($param)) {
  542. $this->error('参数错误');
  543. }
  544. $page = $this->request->param('page', 1);
  545. $pageSize = $this->request->param('limit', 30);
  546. $total = db('成本v23_月度成本明细')
  547. ->where('车间名称', $param['sist'])
  548. ->where('sys_ny', $param['month'])
  549. ->count();
  550. $list = db('成本v23_月度成本明细')
  551. ->where('车间名称', $param['sist'])
  552. ->where('sys_ny', $param['month'])
  553. ->order('sczl_gdbh')
  554. ->page($page, $pageSize)
  555. ->select();
  556. $data = [
  557. 'total' => $total,
  558. 'list' => $list,
  559. ];
  560. if (empty($list)) {
  561. $this->error('未找到数据');
  562. }else{
  563. $this->success('成功', $data);
  564. }
  565. }
  566. /**
  567. * 创建各月水电气分摊
  568. * @return void
  569. * @throws \think\db\exception\BindParamException
  570. * @throws \think\exception\PDOException
  571. */
  572. public function UtilitiesAdd()
  573. {
  574. if ($this->request->isPost() === false) {
  575. $this->error('请求错误');
  576. }
  577. $param = Request::instance()->post();
  578. if (empty($param)) {
  579. $this->error('参数错误');
  580. }
  581. $data = [];
  582. $currentTime = date('Y-m-d H:i:s');
  583. foreach ($param as $item) {
  584. // 验证必要字段是否存在
  585. if (empty($item['sys_ny']) || empty($item['sist']) || empty($item['科目名称'])) {
  586. $this->error('缺少必要参数');
  587. }
  588. // 确保数值字段正确处理为 decimal 类型
  589. $data[] = [
  590. 'Sys_ny' => $item['sys_ny'],
  591. '部门名称' => $item['sist'],
  592. '费用类型' => '分摊',
  593. '科目名称' => $item['科目名称'],
  594. '耗电量' => isset($item['耗电量']) ? (float)$item['耗电量'] : 0.00,
  595. '单位电价' => isset($item['单位电价']) ? (float)$item['单位电价'] : 0.00,
  596. '耗气量' => isset($item['耗气量']) ? (float)$item['耗气量'] : 0.00,
  597. '单位气价' => isset($item['单位气价']) ? (float)$item['单位气价'] : 0.00,
  598. 'Sys_id' => $item['sys_id'] ?? '',
  599. 'Sys_rq' => $currentTime
  600. ];
  601. }
  602. $result = db('成本_各月水电气')->where('Sys_ny', $data[0]['Sys_ny'])->where('费用类型','分摊')->delete();
  603. $sql = db('成本_各月水电气')->fetchSql(true)->insertAll($data);
  604. $res = db()->query($sql);
  605. if ($res !== false) {
  606. $this->success('成功');
  607. }else{
  608. $this->error('失败');
  609. }
  610. }
  611. /**
  612. * 各月水电气分摊费用列表
  613. * @return void
  614. * @throws \think\db\exception\DataNotFoundException
  615. * @throws \think\db\exception\ModelNotFoundException
  616. * @throws \think\exception\DbException
  617. */
  618. public function UtilitiesList()
  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. ->field('rtrim(部门名称) as 部门名称,rtrim(科目名称) as 科目名称,耗电量,单位电价,耗气量,单位气价')
  629. ->where('费用类型', '分摊')
  630. ->where('Sys_ny', $param['month'])
  631. ->group('部门名称,科目名称')
  632. ->select();
  633. if (empty($list)) {
  634. $list = db('成本_各月水电气')
  635. ->field('rtrim(部门名称) as 部门名称,rtrim(科目名称) as 科目名称')
  636. ->where('费用类型', '分摊')
  637. ->group('部门名称,科目名称')
  638. ->select();
  639. }
  640. foreach ($list as $k => $v) {
  641. $list[$k]['年月'] = $param['month'];
  642. }
  643. $this->success('成功', $list);
  644. }
  645. /**
  646. * 成本汇总左侧列表
  647. * @return void
  648. */
  649. public function getSummaryTab()
  650. {
  651. if ($this->request->isGet() === false) {
  652. $this->error('请求错误');
  653. }
  654. $months = db('成本v23_月度成本明细')
  655. ->field('sys_ny AS year_month')
  656. ->group('Sys_ny')
  657. ->order('Sys_ny DESC')
  658. ->column('Sys_ny');
  659. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  660. $data = [];
  661. foreach ($months as $month) {
  662. $data[$month] = $sist;
  663. }
  664. $this->success('成功', $data);
  665. }
  666. //计算每个车间色度数
  667. private function CountSistChromaticity($month,$sist)
  668. {
  669. $data = db('成本v23_月度成本明细')
  670. ->where('sys_ny', $month)
  671. ->where('车间名称', $sist)
  672. ->group('车间名称')
  673. ->value('班组车头产量*sczl_ms as 色度数');
  674. return $data;
  675. }
  676. //计算每个机台色度数
  677. private function CountMachineChromatic($month,$sist)
  678. {
  679. $data = db('成本v23_月度成本明细')
  680. ->where('sys_ny', $month)
  681. ->where('车间名称', $sist)
  682. ->group('sczl_jtbh')
  683. ->value('班组车头产量*sczl_ms as 色度数');
  684. return $data;
  685. }
  686. //水电气分摊费用下方明细列表
  687. // public function shuidianqiMachineDetailList()
  688. // {
  689. // if ($this->request->isGet() === false) {
  690. // $this->error('请求错误');
  691. // }
  692. // $param = $this->request->param();
  693. // if (empty($param)) {
  694. // $this->error('参数错误');
  695. // }
  696. // $list = db('设备_基本资料')
  697. // ->alias('a')
  698. // ->join('成本_各月分摊系数 b', 'a.设备编号 = b.设备编号', 'LEFT')
  699. // ->field()
  700. //
  701. // }
  702. /**
  703. * 其他待摊费用
  704. * @return void
  705. * @throws \think\db\exception\BindParamException
  706. * @throws \think\exception\PDOException
  707. */
  708. public function PrepaidExpensesListEdit()
  709. {
  710. if ($this->request->isPost() === false) {
  711. $this->error('请求错误');
  712. }
  713. $param = Request::instance()->post();
  714. if (empty($param)) {
  715. $this->error('参数错误');
  716. }
  717. $data = [
  718. 'sys_ny' => $param['month'],
  719. '部门人员工资' => $param['salary1'],
  720. '管理人员工资' => $param['salary2'],
  721. '场地租金' => $param['rental'],
  722. '待摊折旧' => $param['depreciation'],
  723. '工资成本占比' => $param['proportion'],
  724. '其他' => $param['rest'],
  725. '后勤人员工资' => $param['profit'],
  726. 'sys_id' => $param['sys_id'],
  727. ];
  728. $result = db('成本_各月其他费用')->where('sys_ny', $param['month'])->count();
  729. if ($result > 0) {
  730. $data['mod_rq'] = date('Y-m-d H:i:s', time());
  731. $sql = db('成本_各月其他费用')->where('sys_ny', $param['month'])->fetchSql(true)->update($data);
  732. }else{
  733. $data['sys_rq'] = date('Y-m-d H:i:s', time());
  734. $sql = db('成本_各月其他费用')
  735. ->fetchSql(true)
  736. ->insert($data);
  737. }
  738. $res = db()->query($sql);
  739. if ($res !== false) {
  740. $this->success('成功');
  741. }else{
  742. $this->error('失败');
  743. }
  744. }
  745. /**
  746. * 其他待摊费用列表
  747. * @return void
  748. * @throws \think\db\exception\DataNotFoundException
  749. * @throws \think\db\exception\ModelNotFoundException
  750. * @throws \think\exception\DbException
  751. */
  752. public function PrepaidExpensesList()
  753. {
  754. if ($this->request->isGet() === false) {
  755. $this->error('请求错误');
  756. }
  757. $param = $this->request->param();
  758. if (empty($param)) {
  759. $this->error('参数错误');
  760. }
  761. $list = db('成本_各月其他费用')
  762. ->where('sys_ny', $param['month'])
  763. ->field('sys_ny as 年月,部门人员工资,管理人员工资,场地租金,待摊折旧,工资成本占比,其他,后勤人员工资,sys_id as 创建用户,sys_rq as 创建时间,mod_rq as 修改时间,UniqID')
  764. ->find();
  765. if (empty($list)) {
  766. $this->error('未找到数据');
  767. }else{
  768. $this->success('成功', $list);
  769. }
  770. }
  771. /**
  772. * 参考其他费用参考月份
  773. * @return void
  774. * @throws \think\db\exception\BindParamException
  775. * @throws \think\db\exception\DataNotFoundException
  776. * @throws \think\db\exception\ModelNotFoundException
  777. * @throws \think\exception\DbException
  778. * @throws \think\exception\PDOException
  779. */
  780. public function PrepaidExpensesDetailCopy()
  781. {
  782. if ($this->request->isGet() === false) {
  783. $this->error('请求错误');
  784. }
  785. $param = $this->request->param();
  786. if (empty($param)) {
  787. $this->error('参数错误');
  788. }
  789. $formMonth = $param['formMonth'];
  790. $toMonth = $param['toMonth'];
  791. $list = db('成本_各月其他费用')
  792. ->where('sys_ny', $formMonth)
  793. ->field('管理人员工资,场地租金,待摊折旧,工资成本占比,后勤人员工资')
  794. ->find();
  795. $list['sys_ny'] = $toMonth;
  796. $list['sys_id'] = $param['sys_id'];
  797. $list['sys_rq'] = date('Y-m-d H:i:s', time());
  798. $sql = db('成本_各月其他费用')
  799. ->fetchSql(true)
  800. ->insert($list);
  801. $res = db()->query($sql);
  802. if ($res !== false) {
  803. $this->success('成功');
  804. }else{
  805. $this->error('失败');
  806. }
  807. }
  808. /**
  809. * 各月水电气直接费用创建
  810. * @return void
  811. * @throws \think\db\exception\BindParamException
  812. * @throws \think\db\exception\DataNotFoundException
  813. * @throws \think\db\exception\ModelNotFoundException
  814. * @throws \think\exception\DbException
  815. * @throws \think\exception\PDOException
  816. */
  817. public function shuidianqiDetailAdd()
  818. {
  819. if ($this->request->isGet() === false) {
  820. $this->error('请求错误');
  821. }
  822. $param = $this->request->param();
  823. if (empty($param)) {
  824. $this->error('参数错误');
  825. }
  826. $month = substr($param['month'], 0, 4) . '-' . substr($param['month'], 4, 2);
  827. $sist = ['胶印车间','凹丝印车间','印后车间'];
  828. $list = db('设备_产量计酬')
  829. ->alias('a')
  830. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  831. ->where('a.sczl_rq', 'like', $month . '%')
  832. ->where('b.sys_sbID','<>','')
  833. ->where('b.使用部门','in',$sist)
  834. ->field('a.sczl_jtbh,sum(a.sczl_设备运行工时) as 通电工时,b.使用部门,rtrim(b.设备名称) as 设备名称')
  835. ->order('b.使用部门,a.sczl_jtbh')
  836. ->group('a.sczl_jtbh')
  837. ->select();
  838. $data = [];
  839. foreach ($list as $k => $v) {
  840. $data[] = [
  841. 'Sys_ny' => $param['month'],
  842. '部门名称' => $v['使用部门'],
  843. '费用类型' => '直接',
  844. '设备编号' => $v['sczl_jtbh'],
  845. '科目名称' => $v['设备名称'],
  846. '耗电量' => $v['通电工时'],
  847. '单位电价' => 0.69,
  848. 'Sys_id' => $param['sys_id'],
  849. 'Sys_rq' => date('Y-m-d H:i:s', time()),
  850. ];
  851. }
  852. $sql = db('成本_各月水电气')->fetchSql(true)->insertAll($data);
  853. $res = db()->query($sql);
  854. if ($res !== false) {
  855. $this->success('成功');
  856. }else{
  857. $this->error('失败');
  858. }
  859. }
  860. /**
  861. * 获取各车间人工分摊因子比例
  862. * @param string $month 年月
  863. * @return array
  864. */
  865. protected function WageRatio($month)
  866. {
  867. try {
  868. // 使用一次查询获取所有需要的数据
  869. $result = db('成本v23_月度成本明细')
  870. ->where('sys_ny', $month)
  871. ->whereNotNull('车间名称')
  872. ->field('
  873. SUM(人工分摊因子) as total,
  874. rtrim(车间名称) as workshop_name,
  875. SUM(人工分摊因子) as workshop_total
  876. ')
  877. ->group('车间名称')
  878. ->select();
  879. if (empty($result)) {
  880. return [];
  881. }
  882. // 计算总人工分摊因子
  883. $total = array_sum(array_column($result, 'workshop_total'));
  884. // 计算每个车间的比例
  885. $data = [];
  886. foreach ($result as $item) {
  887. if ($total > 0) {
  888. $data[$item['workshop_name']] = number_format($item['workshop_total'] / $total, 2);
  889. } else {
  890. $data[$item['workshop_name']] = 0;
  891. }
  892. }
  893. return $data;
  894. } catch (\Exception $e) {
  895. // 记录日志
  896. \think\facade\Log::error('WageRatio计算失败: ' . $e->getMessage());
  897. return [];
  898. }
  899. }
  900. /**
  901. * 获取指定部门各月的色度数数据
  902. * @param string $month 年月
  903. * @param string $sist 部门
  904. * @return array
  905. */
  906. protected function WageRatioMonth($month, $sist)
  907. {
  908. try {
  909. // 使用一次查询获取数据和总数
  910. $query = db('成本_各月色度数')
  911. ->where(['年月' => $month, '部门' => $sist]);
  912. // 计算总数
  913. $total = $query->value('SUM(sczl_cl * CASE
  914. WHEN sczl_ms = 0 OR sczl_ms IS NULL OR sczl_ms = \'\' THEN 1
  915. ELSE sczl_ms
  916. END)');
  917. // 获取列表数据
  918. $list = $query->field("
  919. sczl_gdbh,
  920. sczl_yjno,
  921. sczl_gxh,
  922. sczl_jtbh,
  923. sczl_cl,
  924. CASE
  925. WHEN sczl_ms = 0 OR sczl_ms IS NULL OR sczl_ms = ''
  926. THEN 1
  927. ELSE sczl_ms
  928. END as sczl_ms
  929. ")->select();
  930. return [
  931. 'total' => $total ?: 0, // 防止空值
  932. 'list' => $list ?: [], // 防止空值
  933. ];
  934. } catch (\Exception $e) {
  935. \think\facade\Log::error('WageRatioMonth查询失败: ' . $e->getMessage());
  936. return ['total' => 0, 'list' => []];
  937. }
  938. }
  939. /**
  940. * 计算工资分配
  941. * @param string $month 年月
  942. * @param string $sist 部门
  943. * @param float $ratio 比例
  944. * @param string $type 类型
  945. * @param float $amount 金额
  946. * @return array
  947. */
  948. protected function WageRatioCalculation($month, $sist, $ratio, $type, $amount)
  949. {
  950. try {
  951. $data = [];
  952. $chromaNumber = $this->WageRatioMonth($month, $sist);
  953. if (empty($chromaNumber['list']) || $chromaNumber['total'] == 0) {
  954. return [];
  955. }
  956. $money = $ratio * $amount;
  957. $name = $type === '部门人员工资' ? '车间人工' : '部门人工附加';
  958. foreach ($chromaNumber['list'] as $item) {
  959. if ($chromaNumber['total'] > 0) {
  960. $perAmount = ($item['sczl_cl'] * $item['sczl_ms'] / $chromaNumber['total']) * $money;
  961. $data[] = [
  962. 'name' => $name,
  963. 'money' => round($perAmount, 2),
  964. 'sczl_gdbh' => $item['sczl_gdbh'],
  965. 'sczl_yjno' => $item['sczl_yjno'],
  966. 'sczl_gxh' => $item['sczl_gxh'],
  967. 'sczl_jtbh' => $item['sczl_jtbh'],
  968. ];
  969. }
  970. }
  971. return $data;
  972. } catch (\Exception $e) {
  973. \think\facade\Log::error('WageRatioCalculation计算失败: ' . $e->getMessage());
  974. return [];
  975. }
  976. }
  977. /**
  978. * 工资费用分配主函数(优化版)
  979. */
  980. public function WageExpensesList()
  981. {
  982. // 参数验证
  983. if (!$this->request->isGet()) {
  984. throw new \Exception('请求错误,仅支持GET请求');
  985. }
  986. $param = $this->request->param();
  987. if (empty($param) || !isset($param['month'])) {
  988. throw new \Exception('参数错误,缺少必要参数');
  989. }
  990. $month = trim($param['month']);
  991. // 获取工资比例
  992. $wageRatio = $this->WageRatio($month);
  993. if (empty($wageRatio)) {
  994. throw new \Exception('未找到该月的人工分摊因子数据');
  995. }
  996. // 获取月度工资数据
  997. $monthWage = db('成本_各月其他费用')
  998. ->where('sys_ny', $month)
  999. ->field('部门人员工资,管理人员工资')
  1000. ->find();
  1001. if (empty($monthWage)) {
  1002. throw new \Exception('未找到该月的工资数据');
  1003. }
  1004. // 准备批量更新数据
  1005. $updateData = [];
  1006. $updateCount = 0;
  1007. // 遍历所有车间
  1008. foreach ($wageRatio as $workshopName => $ratio) {
  1009. // 计算两种类型的工资分配
  1010. $updateTypes = [
  1011. '部门人员工资' => '车间人工',
  1012. '管理人员工资' => '部门人工附加'
  1013. ];
  1014. foreach ($updateTypes as $wageType => $fieldName) {
  1015. if (!isset($monthWage[$wageType])) {
  1016. continue;
  1017. }
  1018. $calculatedData = $this->WageRatioCalculation(
  1019. $month,
  1020. $workshopName,
  1021. $ratio,
  1022. $wageType,
  1023. $monthWage[$wageType]
  1024. );
  1025. // 收集更新数据
  1026. foreach ($calculatedData as $item) {
  1027. if (!empty($item['sczl_gdbh'])) {
  1028. // 构建唯一标识作为更新条件
  1029. $conditions = [
  1030. 'sczl_gdbh' => $item['sczl_gdbh'],
  1031. 'sczl_gxh' => $item['sczl_gxh'] ?? null,
  1032. 'sczl_jtbh' => $item['sczl_jtbh'] ?? null,
  1033. 'sczl_yjno' => $item['sczl_yjno'] ?? null,
  1034. ];
  1035. // 生成唯一标识字符串
  1036. $uniqCondition = http_build_query(array_filter($conditions));
  1037. $updateData[] = [
  1038. 'condition' => $uniqCondition, // 用于去重的条件
  1039. 'conditions' => $conditions, // 实际查询条件
  1040. 'field' => $item['name'],
  1041. 'value' => $item['money'],
  1042. 'month' => $month
  1043. ];
  1044. $updateCount++;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. // 批量更新
  1050. $updatedRows = $this->batchUpdateData($updateData,$month);
  1051. // 返回成功结果
  1052. return json([
  1053. 'code' => 200,
  1054. 'msg' => '工资分配完成',
  1055. 'data' => [
  1056. 'updated_rows' => $updatedRows,
  1057. 'update_count' => $updateCount,
  1058. 'workshop_count' => count($wageRatio)
  1059. ]
  1060. ]);
  1061. }
  1062. /**
  1063. * 批量更新数据(原生SQL版本)
  1064. * @param array $updateData 更新数据
  1065. * @param string $month 月份
  1066. * @return int 更新的行数
  1067. */
  1068. private function batchUpdateData(array $updateData, string $month)
  1069. {
  1070. if (empty($updateData)) {
  1071. return 0;
  1072. }
  1073. $updatedRows = 0;
  1074. // 按字段分组
  1075. $groupedData = [];
  1076. foreach ($updateData as $item) {
  1077. $field = $item['field'];
  1078. $groupedData[$field][] = $item;
  1079. }
  1080. // 对每个字段进行批量更新
  1081. foreach ($groupedData as $field => $items) {
  1082. // 构建CASE WHEN语句
  1083. $caseWhens = [];
  1084. $gdbhList = [];
  1085. foreach ($items as $item) {
  1086. $baseCondition = "sczl_gdbh = '{$item['conditions']['sczl_gdbh']}'";
  1087. // 添加其他可选条件
  1088. $conditions = [$baseCondition];
  1089. if (!empty($item['conditions']['sczl_gxh'])) {
  1090. $conditions[] = "sczl_gxh = '{$item['conditions']['sczl_gxh']}'";
  1091. }
  1092. if (!empty($item['conditions']['sczl_jtbh'])) {
  1093. $conditions[] = "sczl_jtbh = '{$item['conditions']['sczl_jtbh']}'";
  1094. }
  1095. if (!empty($item['conditions']['sczl_yjno'])) {
  1096. $conditions[] = "sczl_yjno = '{$item['conditions']['sczl_yjno']}'";
  1097. }
  1098. $whenCondition = implode(' AND ', $conditions);
  1099. $caseWhens[] = "WHEN {$whenCondition} THEN {$item['value']}";
  1100. $gdbhList[] = $item['conditions']['sczl_gdbh'];
  1101. }
  1102. if (empty($caseWhens)) {
  1103. continue;
  1104. }
  1105. // 去重
  1106. $gdbhList = array_unique($gdbhList);
  1107. $gdbhStr = implode("','", $gdbhList);
  1108. $caseStr = implode(' ', $caseWhens);
  1109. // 构建SQL
  1110. $sql = "UPDATE 成本v23_月度成本明细
  1111. SET {$field} = CASE {$caseStr} ELSE {$field} END
  1112. WHERE sczl_gdbh IN ('{$gdbhStr}') AND sys_ny = '{$month}'";
  1113. // 执行SQL
  1114. $result = db()->execute($sql);
  1115. $updatedRows += $result;
  1116. }
  1117. return $updatedRows;
  1118. }
  1119. /**
  1120. * 获取工资分配详情(调试用)
  1121. */
  1122. public function getWageDistributionDetail($month)
  1123. {
  1124. $wageRatio = $this->WageRatio($month);
  1125. $distribution = [];
  1126. foreach ($wageRatio as $workshop => $ratio) {
  1127. $distribution[$workshop] = [
  1128. 'ratio' => $ratio,
  1129. 'data' => $this->WageRatioMonth($month, $workshop)
  1130. ];
  1131. }
  1132. return $distribution;
  1133. }
  1134. /**
  1135. * 计算工艺材料分摊
  1136. * @param $month
  1137. * @return bool
  1138. */
  1139. public function calculationChroma($month)
  1140. {
  1141. try {
  1142. // 1. 批量获取数据,减少数据库查询次数
  1143. $date = substr($month, 0, 4) . '-' . substr($month, 4, 2);
  1144. // 获取分摊材料总金额
  1145. $totalMoney = db('材料出库单列表')
  1146. ->where([
  1147. '出库日期' => ['like', $date . '%'],
  1148. '部门' => '印刷成本中心'
  1149. ])
  1150. ->whereNull('表体生产订单号')
  1151. ->field('SUM(金额) as money')
  1152. ->find();
  1153. if (!$totalMoney || $totalMoney['money'] <= 0) {
  1154. return false;
  1155. }
  1156. // 获取工单工艺数据及总色度数
  1157. $processData = db('成本v23_月度成本明细')
  1158. ->whereNotNull('车间名称')
  1159. ->where('sys_ny', $month)
  1160. ->field('Uniqid, 班组车头产量*sczl_ms as 色度数, SUM(班组车头产量*sczl_ms) OVER() as total_chroma')
  1161. ->order('Uniqid asc')
  1162. ->select();
  1163. if (empty($processData)) {
  1164. return false;
  1165. }
  1166. // 从第一条数据中获取总色度数
  1167. $totalChroma = $processData[0]['total_chroma'] ?? 0;
  1168. if ($totalChroma <= 0) {
  1169. return false; // 防止除以零
  1170. }
  1171. // 2. 批量更新数据,减少数据库操作次数
  1172. $updateData = [];
  1173. foreach ($processData as $process) {
  1174. $money = round($totalMoney['money'] * ($process['色度数'] / $totalChroma), 2);
  1175. $updateData[] = [
  1176. 'Uniqid' => $process['Uniqid'],
  1177. '分摊材料' => $money
  1178. ];
  1179. }
  1180. // 批量更新(使用fetchSql构建安全的SQL)
  1181. if (!empty($updateData)) {
  1182. return $this->batchUpdateWithFetchSql('成本v23_月度成本明细', $updateData, 'Uniqid');
  1183. }
  1184. return false;
  1185. } catch (\Exception $e) {
  1186. // 记录错误日志
  1187. \think\Log::error('工艺材料分摊计算失败: ' . $e->getMessage());
  1188. return false;
  1189. }
  1190. }
  1191. /**
  1192. * 使用fetchSql方法批量更新数据(安全处理中文字段名)
  1193. * @param string $table 表名
  1194. * @param array $data 更新数据数组
  1195. * @param string $pk 主键字段名
  1196. * @return bool
  1197. */
  1198. protected function batchUpdateWithFetchSql($table, $data, $pk = 'Uniqid')
  1199. {
  1200. if (empty($data)) {
  1201. return false;
  1202. }
  1203. try {
  1204. // 方法一:使用查询构建器配合fetchSql
  1205. $ids = array_column($data, $pk);
  1206. // 开始事务
  1207. db()->startTrans();
  1208. // 先查询现有数据
  1209. $query = db($table)
  1210. ->whereIn($pk, $ids)
  1211. ->field($pk)
  1212. ->fetchSql(true)
  1213. ->select();
  1214. $sql = $query;
  1215. // 构造批量更新SQL
  1216. $updateSql = "UPDATE `{$table}` SET ";
  1217. // 获取所有需要更新的字段(排除主键)
  1218. $fields = array_keys($data[0]);
  1219. $updateFields = array_diff($fields, [$pk]);
  1220. $cases = [];
  1221. foreach ($updateFields as $field) {
  1222. $caseSql = "`{$field}` = CASE ";
  1223. foreach ($data as $item) {
  1224. $caseSql .= "WHEN `{$pk}` = '{$item[$pk]}' THEN '{$item[$field]}' ";
  1225. }
  1226. $caseSql .= "ELSE `{$field}` END";
  1227. $cases[] = $caseSql;
  1228. }
  1229. $updateSql .= implode(', ', $cases);
  1230. $updateSql .= " WHERE `{$pk}` IN ('" . implode("','", $ids) . "')";
  1231. // 执行更新
  1232. $result = db()->execute($updateSql);
  1233. db()->commit();
  1234. return $result !== false;
  1235. } catch (\Exception $e) {
  1236. db()->rollback();
  1237. \think\Log::error('批量更新失败: ' . $e->getMessage());
  1238. // 如果批量更新失败,回退到单条更新(也使用fetchSql)
  1239. return $this->singleUpdateFallback($table, $data, $pk);
  1240. }
  1241. }
  1242. /**
  1243. * 单条更新回退方案(使用fetchSql)
  1244. */
  1245. protected function singleUpdateFallback($table, $data, $pk)
  1246. {
  1247. $success = true;
  1248. foreach ($data as $item) {
  1249. try {
  1250. // 使用fetchSql构建安全的更新语句
  1251. $updateData = [];
  1252. foreach ($item as $key => $value) {
  1253. if ($key !== $pk) {
  1254. $updateData[$key] = $value;
  1255. }
  1256. }
  1257. $sql = db($table)
  1258. ->where($pk, $item[$pk])
  1259. ->update($updateData)
  1260. ->fetchSql(true);
  1261. // 获取SQL并执行
  1262. $result = db()->execute($sql);
  1263. if ($result === false) {
  1264. $success = false;
  1265. \think\Log::error('单条更新失败 - SQL: ' . $sql);
  1266. }
  1267. } catch (\Exception $e) {
  1268. $success = false;
  1269. \think\Log::error('单条更新异常: ' . $e->getMessage());
  1270. }
  1271. }
  1272. return $success;
  1273. }
  1274. }