OrderSuperLoss.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use \think\Request;
  6. use \think\Db;
  7. /**
  8. * 工单超节损核算接口
  9. */
  10. class OrderSuperLoss extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 首页
  16. *
  17. */
  18. public function index()
  19. {
  20. $this->success('请求成功');
  21. }
  22. /**
  23. * 获取左侧菜单栏
  24. * @ApiMethod GET
  25. */
  26. public function getTab()
  27. {
  28. if (Request::instance()->isGet() == false) {
  29. $this->error('非法请求');
  30. }
  31. $is_hav_cache = Cache::get('OrderSuperLoss/getTab');
  32. if ($is_hav_cache === false){
  33. $sql = "SELECT DISTINCT(Gd_gdbh),`年月`,rtrim(`客户编号`) as 客户编号,rtrim(`客户名称`) as 客户名称 FROM `rec_月度废品汇总`
  34. WHERE STR_TO_DATE(`年月`, '%Y%m') >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) order by 年月 desc,客户编号 asc";
  35. $data = Db::query($sql);
  36. $outputArray = [];
  37. foreach ($data as $item) {
  38. $yearMonth = $item['年月'];
  39. if (!isset($outputArray[$yearMonth])) {
  40. // If key doesn't exist in output array, initialize it
  41. $outputArray[$yearMonth] = [];
  42. }
  43. // Check if the customer already exists in the current yearMonth array
  44. $existingCustomerIndex = null;
  45. foreach ($outputArray[$yearMonth] as $index => $customer) {
  46. if ($customer['客户编号'] === $item['客户编号']) {
  47. $existingCustomerIndex = $index;
  48. break;
  49. }
  50. }
  51. if ($existingCustomerIndex !== null) {
  52. // If customer exists, increment the total
  53. $outputArray[$yearMonth][$existingCustomerIndex]['total']++;
  54. } else {
  55. // If customer doesn't exist, add a new entry
  56. $outputArray[$yearMonth][] = [
  57. '客户名称' => $item['客户名称'],
  58. '客户编号' => $item['客户编号'],
  59. 'total' => 1,
  60. ];
  61. }
  62. }
  63. // 遍历每个年月的数组
  64. $list = [];
  65. foreach ($outputArray as $yearMonth => $orders) {
  66. $totalOrders = 0;
  67. // 遍历每个订单,累加订单数量
  68. foreach ($orders as $order) {
  69. $totalOrders += $order['total'];
  70. }
  71. // 输出每个年月的订单数量
  72. $list[$yearMonth.'-'.$totalOrders] = $orders;
  73. }
  74. Cache::set('OrderSuperLoss/getTab',$list,86400);
  75. }else{
  76. $list = Cache::get('OrderSuperLoss/getTab');
  77. }
  78. $this->success('请求成功',$list);
  79. }
  80. /**
  81. * 获取超节损工单列表
  82. * @ApiMethod GET
  83. * @params string date
  84. * @params string code
  85. * 报废定额=(基础损耗+损耗率*计划产量)*损耗系数*计损色数(特定工序会有,如果无就不乘)
  86. * 允损比例=报废定额/计划产量
  87. * 目标合格率 = 100%-超节损工单中显示工序的允损比例之和
  88. *
  89. */
  90. public function getList(){
  91. if (Request::instance()->isGet() == false) {
  92. $this->error('非法请求');
  93. }
  94. $params = Request::instance()->param();
  95. $where = [];
  96. if (!empty($params['code'])) {
  97. if (!empty($params['search'])) {
  98. $this->error('参数错误');
  99. }
  100. $where['a.客户编号' ] = $params['code'];
  101. }
  102. if (!empty($params['date'])) {
  103. if (!empty($params['search'])) {
  104. $this->error('参数错误');
  105. }
  106. $where['a.年月' ] = $params['date'];
  107. }
  108. if (!empty($params['search'])) {
  109. if (!empty($params['date']) || !empty($params['code'])) {
  110. $this->error('参数错误');
  111. }
  112. $where['a.产品名称'] = array('like','%'.$params['search'].'%');
  113. }
  114. $where['b.行号' ] = 1;
  115. $limit = $params['limit'];
  116. if (empty($limit)){
  117. $limit = 15;
  118. }
  119. $pages = $params['page'];
  120. if (empty($pages)){
  121. $pages = 1;
  122. }
  123. $data = Db::table('rec_月度废品汇总')->alias('a')
  124. ->join('工单_基本资料 b', 'a.Gd_gdbh = b.Gd_gdbh','left')
  125. ->join('工单_印件资料 c', 'a.Gd_gdbh = c.Yj_Gdbh','left')
  126. ->where($where)
  127. ->field('a.Gd_gdbh, SUM(a.废品数量) AS 废品合计, a.年月, rtrim(a.Gd_cpmc) as Gd_cpmc, a.Gd_cpdh, a.实际投料, b.计量单位, c.yj_Yjno, c.yj_ls')
  128. ->group('a.Gd_gdbh')
  129. ->order('a.Gd_cpdh asc,a.年月 desc')
  130. ->fetchSql(true)
  131. ->page($pages)
  132. ->limit($limit)
  133. ->select();
  134. foreach ($data as $key => $value){
  135. //查出成品数量及日期
  136. $cp_sql = "SELECT SUM(jjcp_sl) as cp_sl,MAX(jjcp_sj) as jjcp_sj FROM `成品入仓` WHERE jjcp_gdbh = '{$value['Gd_gdbh']}' GROUP BY jjcp_gdbh";
  137. $cp_data = Db::query($cp_sql);
  138. $data[$key]['warehousing_num'] = $cp_data[0]['cp_sl'];
  139. $data[$key]['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  140. //查出进入超节损的工序,有上报产量的工序就进入超节损
  141. $gxh_sql = "SELECT sczl_gxh FROM
  142. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$value['Gd_gdbh']}'
  143. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$value['Gd_gdbh']}') AS combined_result";
  144. $gxh_arr = Db::query($gxh_sql);
  145. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  146. $gy_data =db('工单_工艺资料')->where('Gy0_gdbh',$value['Gd_gdbh'])->where('Gy0_gxh','in',$gxh_values)->field('Gy0_计划接货数,Gy0_计划损耗')->select();
  147. $arr = [];
  148. $plan_loss = [];//工单计划损耗
  149. foreach ($gy_data as $k=>$v){
  150. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  151. $arr[$k] = floor($rate * 10000) /10000;
  152. $plan_loss[$k] = $v['Gy0_计划损耗'];
  153. }
  154. $target_rate = (1-array_sum($arr))*100;
  155. $data[$key]['target_rate'] =$target_rate.'%'; //目标合格率
  156. $real_rate = $cp_data[0]['cp_sl'] / ($value['实际投料'] * 10000) *100;
  157. $data[$key]['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  158. //奖惩系数及金额
  159. $data[$key]['reward_rate'] = '';
  160. $data[$key]['reward_money'] = '';
  161. //制程废品
  162. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$value['Gd_gdbh'])->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  163. $data[$key]['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $value['yj_ls'];//制程废品
  164. $data[$key]['废品合计'] = $data[$key]['zcfp'] + $value['废品合计'];//废品合计
  165. $data[$key]['intangible_loss'] = $value['实际投料'] *10000 - $cp_data[0]['cp_sl'] - $data[$key]['废品合计'];//工单无形损
  166. //材料废
  167. $waste_l =db('rec_月度废品汇总')->where('Gd_gdbh',$value['Gd_gdbh'])->where('废品类别','like','%L%')->field('SUM(废品数量) as 废品数量')->select();
  168. $data[$key]['material_waste'] = $waste_l[0]['废品数量'];
  169. //零头处理
  170. $waste_w = db('rec_月度废品汇总')->where('Gd_gdbh',$value['Gd_gdbh'])->where('废品类别','like','%M%')->field('SUM(废品数量) as 废品数量')->select();
  171. $data[$key]['minor_processing'] = $waste_w[0]['废品数量'];
  172. //外发废
  173. $out_sql = "SELECT fp_sl1,fp_sl2,fp_sl3,fp_sl4,fp_sl5,fp_sl6,fp_sl7,fp_sl8,fp_sl9,fp_sl10,fp_sl11,fp_sl12,fp_sl13,
  174. fp_lb1,fp_lb2,fp_lb3,fp_lb4,fp_lb5,fp_lb6,fp_lb7,fp_lb8,fp_lb9,fp_lb10,fp_lb11,fp_lb12,fp_lb13,
  175. fp_bh1,fp_bh2,fp_bh3,fp_bh4,fp_bh5,fp_bh6,fp_bh7,fp_bh8,fp_bh9,fp_bh10,fp_bh11,fp_bh12,fp_bh13,
  176. fp_gxmc1,fp_gxmc2,fp_gxmc3,fp_gxmc4,fp_gxmc5,fp_gxmc6,fp_gxmc7,fp_gxmc8,fp_gxmc9,fp_gxmc10,fp_gxmc11,fp_gxmc12,fp_gxmc13
  177. FROM db_qczl WHERE qczl_gdbh = '{$value['Gd_gdbh']}'";
  178. $waste_out = Db::query($out_sql);
  179. $list = [];
  180. $quality = [];
  181. $j = 0;
  182. $m = 0;
  183. foreach ($waste_out as $entry) {
  184. for ($i = 1; $i <= 13; $i++) {
  185. $labelKey = "fp_lb" . $i;
  186. $bhKey = "fp_bh" . $i;
  187. $gxmcKey = "fp_gxmc" . $i;
  188. $slKey = "fp_sl" . $i;
  189. if (!empty($entry[$labelKey])) {
  190. if (substr($entry[$gxmcKey],0,2) == '99'){
  191. $list[$j]= $entry[$slKey];
  192. $j++;
  193. }
  194. }
  195. if (!empty($entry[$bhKey])) {
  196. if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  197. $quality[$m]= $entry[$slKey];
  198. $m++;
  199. }
  200. }
  201. }
  202. }
  203. $data[$key]['waste_out'] = array_sum($list);//外发废
  204. $data[$key]['waste_share'] = '';//分摊废
  205. $data[$key]['plan_loss'] = array_sum($plan_loss);//工单计划损耗
  206. $data[$key]['waste_quality'] = array_sum($quality); //质检废
  207. }
  208. $this->success('请求成功',$data);
  209. }
  210. /**
  211. * 获取工单超节损工艺
  212. * @ApiMethod GET
  213. * @params string order
  214. */
  215. public function getOrderSuperLossGy(){
  216. if (Request::instance()->isGet() == false) {
  217. $this->error('非法请求');
  218. }
  219. $params = Request::instance()->param();
  220. if (!isset($params['order']) || empty($params['order'])) {
  221. $this->error('参数错误');
  222. }
  223. $sql = "SELECT a.Gy0_gdbh,a.Gy0_yjno,a.Gy0_gxh,rtrim(a.Gy0_gxmc) as Gy0_gxmc,rtrim(a.Add_gxmc) as Add_gxmc,a.Gy0_ls,a.Gy0_Rate0,a.Gy0_Rate1,a.损耗系数,a.Gy0_ms,
  224. a.Gy0_计划接货数,a.Gy0_计划损耗,sum(b.sczl_cl) as total_cl,sum(b.sczl_zcfp) as total_fp,sum(c.sczl_cl) as cl,SUM(c.sczl_fp) as fp FROM `工单_工艺资料` a
  225. LEFT JOIN `设备_产量计酬` b ON a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh
  226. LEFT JOIN `db_sczl` c ON a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh
  227. WHERE a.Gy0_gdbh = '{$params['order']}' GROUP BY a.Gy0_gxh";
  228. $data = Db::query($sql);
  229. foreach ($data as $key=>$value){
  230. if (!empty($value['total_cl'])){
  231. $data[$key]['total_cl'] = $value['total_cl'] *$value['Gy0_ls'];
  232. }else{
  233. $data[$key]['total_cl'] =$value['cl'] *$value['Gy0_ls'];
  234. }
  235. if (!empty($value['total_fp'])){
  236. $data[$key]['total_fp'] = $value['total_fp'] *$value['Gy0_ls'];
  237. }else{
  238. $data[$key]['total_fp'] = $value['fp'] *$value['Gy0_ls'];
  239. }
  240. unset($data[$key]['cl']);
  241. unset($data[$key]['fp']);
  242. }
  243. $this->success('请求成功',$data);
  244. }
  245. /**
  246. * 工单超节损统计
  247. * @ApiMethod GET
  248. * @params string order
  249. */
  250. public function getOrderSuperLossCount(){
  251. if (Request::instance()->isGet() == false) {
  252. $this->error('非法请求');
  253. }
  254. $params = Request::instance()->param();
  255. if (!isset($params['order']) || empty($params['order'])) {
  256. $this->error('参数错误');
  257. }
  258. $order = $params['order'];
  259. $field = 'Gd_gdbh,rtrim(成品代号) as 成品代号,rtrim(成品名称) as 成品名称,rtrim(销售订单号) as 销售订单号,订单数量,实际投料';
  260. $data =db('工单_基本资料')->where('Gd_Gdbh',$order)->where('行号',1)->field($field)->find();
  261. //查出成品数量及日期
  262. $cp_sql = "SELECT SUM(jjcp_sl) as cp_sl,MAX(jjcp_sj) as jjcp_sj FROM `成品入仓` WHERE jjcp_gdbh = '{$order}' GROUP BY jjcp_gdbh";
  263. $cp_data = Db::query($cp_sql);
  264. $data['warehousing_num'] = $cp_data[0]['cp_sl'];
  265. $data['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  266. //查出进入超节损的工序,有上报产量的工序就进入超节损
  267. $gxh_sql = "SELECT sczl_gxh FROM
  268. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$order}'
  269. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$order}') AS combined_result";
  270. $gxh_arr = Db::query($gxh_sql);
  271. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  272. $gy_data =db('工单_工艺资料')
  273. ->alias('a')
  274. ->field([
  275. 'a.Gy0_yjno', 'a.Gy0_gxh', 'RTRIM(a.Gy0_gxmc) as Gy0_gxmc','RTRIM(a.Add_gxmc) as Add_gxmc', 'a.Gy0_ls', 'a.Gy0_计划接货数',
  276. 'a.Gy0_计划损耗', 'a.超节损承担比例','SUM(b.sczl_zcfp) as total_fp','SUM(b.sczl_cl) as total_cl','SUM(c.sczl_cl) as cl','SUM(c.sczl_fp) as fp',
  277. ])
  278. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh','left')
  279. ->join('db_sczl c', 'a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh','left')
  280. ->where([
  281. 'a.Gy0_gdbh' => $order,
  282. 'a.Gy0_gxh' => ['in', $gxh_values]
  283. ])
  284. ->group('a.Gy0_gxh')
  285. ->select();
  286. $arr = [];
  287. $plan_loss = [];//工单计划损耗
  288. foreach ($gy_data as $k=>$v){
  289. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  290. $arr[$k] = floor($rate * 10000) /10000;
  291. $plan_loss[$k] = $v['Gy0_计划损耗'];
  292. }
  293. $target_rate = (1-array_sum($arr))*100;
  294. $data['target_rate'] =$target_rate.'%'; //目标合格率
  295. $real_rate = $cp_data[0]['cp_sl'] / ($data['实际投料'] * 10000) *100;
  296. $data['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  297. //制程废品
  298. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$order)->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  299. //联数
  300. $ls =db('工单_印件资料')->where('Yj_Gdbh',$order)->value('yj_ls');
  301. //制程废品*ls
  302. $data['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $ls;
  303. //废品数量
  304. $waste =db('rec_月度废品汇总')->where('Gd_gdbh',$order)->field('SUM(废品数量) as 废品合计')->select();
  305. //废品合计
  306. $waste_total = $data['zcfp'] + $waste[0]['废品合计'];
  307. //工单无形损
  308. $data['intangible_loss'] = $data['实际投料'] *10000 - $cp_data[0]['cp_sl'] - $waste_total;
  309. //材料废
  310. $waste_l =db('rec_月度废品汇总')->where('Gd_gdbh',$order)->where('废品类别','like','%L%')->field('SUM(废品数量) as 废品数量')->select();
  311. $data['material_waste'] = $waste_l[0]['废品数量'];
  312. //零头处理
  313. $waste_w = db('rec_月度废品汇总')->where('Gd_gdbh',$order)->where('废品类别','like','%M%')->field('SUM(废品数量) as 废品数量')->select();
  314. $data['minor_processing'] = $waste_w[0]['废品数量'];
  315. //外发废
  316. $out_sql = "SELECT fp_sl1,fp_sl2,fp_sl3,fp_sl4,fp_sl5,fp_sl6,fp_sl7,fp_sl8,fp_sl9,fp_sl10,fp_sl11,fp_sl12,fp_sl13,
  317. fp_lb1,fp_lb2,fp_lb3,fp_lb4,fp_lb5,fp_lb6,fp_lb7,fp_lb8,fp_lb9,fp_lb10,fp_lb11,fp_lb12,fp_lb13,
  318. fp_bh1,fp_bh2,fp_bh3,fp_bh4,fp_bh5,fp_bh6,fp_bh7,fp_bh8,fp_bh9,fp_bh10,fp_bh11,fp_bh12,fp_bh13,
  319. fp_gxmc1,fp_gxmc2,fp_gxmc3,fp_gxmc4,fp_gxmc5,fp_gxmc6,fp_gxmc7,fp_gxmc8,fp_gxmc9,fp_gxmc10,fp_gxmc11,fp_gxmc12,fp_gxmc13
  320. FROM db_qczl WHERE qczl_gdbh = '{$order}'";
  321. $waste_out = Db::query($out_sql);
  322. $list = [];
  323. $quality = [];
  324. $j = 0;
  325. $m = 0;
  326. foreach ($waste_out as $entry) {
  327. for ($i = 1; $i <= 13; $i++) {
  328. $labelKey = "fp_lb" . $i;
  329. $bhKey = "fp_bh" . $i;
  330. $gxmcKey = "fp_gxmc" . $i;
  331. $slKey = "fp_sl" . $i;
  332. if (!empty($entry[$labelKey])) {
  333. if (substr($entry[$gxmcKey],0,2) == '99'){
  334. $list[$j]= $entry[$slKey];
  335. $j++;
  336. }
  337. }
  338. if (!empty($entry[$bhKey])) {
  339. if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  340. $quality[$m]= $entry[$slKey];
  341. $m++;
  342. }
  343. }
  344. }
  345. }
  346. $data['waste_out'] = array_sum($list);//外发废
  347. //质检废
  348. $data['waste_quality'] = array_sum($quality);
  349. $plan_total =db('工单_工艺资料')->where(['Gy0_gdbh' => $order, 'Gy0_gxh' => ['in', $gxh_values]])->value('SUM(Gy0_计划损耗)');
  350. // halt($plan_total);
  351. //按工序打印
  352. if ($params['type'] == 1){
  353. $total = [];
  354. $total['plan_loss'] = 0;
  355. $total['total_fp'] = 0;
  356. $total['waste_quality'] = 0;
  357. $total['waste_intangible'] = 0;
  358. $total['total_waste'] = 0;
  359. $total['loss'] = 0;
  360. foreach ($gy_data as $k=>$item){
  361. $gy_data[$k]['waste_quality'] = 0;
  362. $gy_data[$k]['total_fp'] = $item['total_fp'] * $item['Gy0_ls'];
  363. $gy_data[$k]['total_cl'] = $item['total_cl'] * $item['Gy0_ls'];
  364. $gy_data[$k]['intangible_loss'] = round($item['Gy0_计划损耗'] / $plan_total * $data['intangible_loss']);
  365. foreach ($waste_out as $entry) {
  366. for ($i = 1; $i <= 13; $i++) {
  367. $gxmcKey = "fp_gxmc" . $i;
  368. $slKey = "fp_sl" . $i;
  369. if ((int)substr($entry[$gxmcKey],0,2) == $item['Gy0_gxh']){
  370. $gy_data[$k]['waste_quality'] += $entry[$slKey];
  371. }
  372. }
  373. }
  374. $gy_data[$k]['total_waste'] = $gy_data[$k]['waste_quality'] + $gy_data[$k]['intangible_loss'] + $gy_data[$k]['total_fp'];
  375. $gy_data[$k]['loss'] = $item['Gy0_计划损耗'] - $gy_data[$k]['total_waste'];
  376. $gy_data[$k]['loss_rate'] = number_format($gy_data[$k]['loss'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  377. $gy_data[$k]['target_loss_rate'] = number_format($item['Gy0_计划损耗'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  378. $gy_data[$k]['actual_loss_rate'] = number_format($gy_data[$k]['total_waste'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  379. $gy_data[$k]['超节损承担比例'] = number_format($item['超节损承担比例'],4) * 100 .'%';
  380. $total['plan_loss'] += $gy_data[$k]['Gy0_计划损耗'];
  381. $total['total_fp'] += $gy_data[$k]['total_fp'];
  382. $total['waste_quality'] += $gy_data[$k]['waste_quality'];
  383. $total['waste_intangible'] += $gy_data[$k]['intangible_loss'];
  384. $total['total_waste'] += $gy_data[$k]['total_waste'];
  385. $total['loss'] += $gy_data[$k]['loss'];
  386. }
  387. $data['gy_data'] = $gy_data;
  388. $data['total'] = $total;
  389. $this->success('请求成功',$data);
  390. }else{ //按班组打印
  391. }
  392. }
  393. /**
  394. * 获取工单工艺
  395. * @ApiMethod GET
  396. * @params string order
  397. */
  398. public function getOrderGy(){
  399. if (Request::instance()->isGet() == false) {
  400. $this->error('非法请求');
  401. }
  402. $params = Request::instance()->param();
  403. if (!isset($params['order']) || empty($params['order'])) {
  404. $this->error('参数错误');
  405. }
  406. $order = $params['order'];
  407. $field = 'Gy0_yjno,Gy0_gxh,rtrim(Gy0_gxmc) as Gy0_gxmc,rtrim(Add_gxmc) as Add_gxmc,Gy0_rate0,Gy0_rate1,损耗系数,无形损承担比例 as loss_one,超节损承担比例 as loss_two,超节损核算单价 as loss_thr,UniqId';
  408. $data =db('工单_工艺资料')->where('Gy0_gdbh',$order)->field($field)->select();
  409. $this->success('请求成功',$data);
  410. }
  411. /**
  412. * 更新工单工艺
  413. * @ApiMethod POST
  414. * @params array data
  415. */
  416. public function updateOrderGy(){
  417. if (Request::instance()->isPost() == false) {
  418. $this->error('非法请求');
  419. }
  420. $params = Request::instance()->request();
  421. if (!isset($params) || !isset($params[0]['UniqId'])){
  422. $this->error('参数不能为空');
  423. }
  424. $i = 0;
  425. foreach ($params as $key=>$value){
  426. $data = [];
  427. $data['无形损承担比例'] = $value['loss_one'];
  428. $data['超节损承担比例'] = $value['loss_two'];
  429. $data['超节损核算单价'] = $value['loss_thr'];
  430. $sql =db('工单_工艺资料')->where('UniqId',$value['UniqId'])->fetchSql(true)->update($data);
  431. $res = Db::query($sql);
  432. if ($res !== false){
  433. $i++;
  434. }
  435. }
  436. if ($i !== 0){
  437. $this->success('更新成功');
  438. }else{
  439. $this->error('更新失败');
  440. }
  441. }
  442. /**
  443. * 获取工单印件考核资料
  444. * @ApiMethod GET
  445. * @params string order
  446. */
  447. public function getOrderYj(){
  448. if (Request::instance()->isGet() == false) {
  449. $this->error('非法请求');
  450. }
  451. $params = Request::instance()->request();
  452. if (!isset($params['order']) || empty($params['order'])) {
  453. $this->error('参数错误');
  454. }
  455. $data =db('工单_印件资料')->where('Yj_gdbh',$params['order'])->field('Yj_gdbh,rtrim(yj_Yjdh) as yj_Yjdh,yj_Yjno,rtrim(yj_yjmc) as yj_yjmc,质量考核')->find();
  456. $this->success('请求成功',$data);
  457. }
  458. /**
  459. * 更新工单印件考核资料
  460. * @ApiMethod POST
  461. * @params array data
  462. */
  463. public function updateOrderYj(){
  464. if (Request::instance()->isPost() == false) {
  465. $this->error('非法请求');
  466. }
  467. $params = Request::instance()->request();
  468. if (!isset($params['order'])){
  469. $this->error('参数不能为空');
  470. }
  471. if (!isset($params['yj_yjno'])){
  472. $this->error('参数不能为空');
  473. }
  474. if (!isset($params['examine'])){
  475. $this->error('参数不能为空');
  476. }
  477. $where['Yj_gdbh'] = $params['order'];
  478. $where['yj_Yjno'] = $params['yj_yjno'];
  479. $res =db('工单_印件资料')->where($where)->setField('质量考核',$params['examine']);
  480. if ($res !== false){
  481. $this->success('更新成功');
  482. }else{
  483. $this->error('更新失败');
  484. }
  485. }
  486. /**
  487. * 获取修正工单实际投料列表
  488. * @ApiMethod GET
  489. * @params string year
  490. * @params string month
  491. */
  492. public function getOrderFeedList(){
  493. if (Request::instance()->isGet() == false) {
  494. $this->error('非法请求');
  495. }
  496. $params = Request::instance()->request();
  497. if (empty($params['year']) || empty($params['month'])) {
  498. $this->error('参数错误');
  499. }
  500. $search = $params['year'].'.'.$params['month'];
  501. $field = "a.Gd_gdbh,rtrim(c.yj_yjdh) as yj_yjdh,c.yj_Yjno, c.yj_ks,c.yj_ls,rtrim(c.yj_zzdh) as yj_zzdh,
  502. rtrim(b.BOM_物料名称) as BOM_物料名称,rtrim(c.yj_tlgg) as yj_tlgg,rtrim(b.BOM_投料单位) as BOM_投料单位,a.订单数量,c.yj_平张投料, b.BOM_实际用量,a.实际投料,a.投料确认,a.UniqId";
  503. $data =db('工单_基本资料')->alias('a')
  504. ->join('工单_印件资料 c','a.Gd_gdbh = c.Yj_Gdbh','left')
  505. ->join('工单_bom资料 b','c.Yj_Gdbh = b.BOM_工单编号 AND c.yj_zzdh = b.BOM_物料编码','left')
  506. ->where('a.投料确认','like','%'.$search.'%')
  507. ->field($field)
  508. ->group('a.Gd_gdbh')
  509. ->order('b.BOM_投料单位,a.UniqId asc')
  510. ->select();
  511. if (empty($data)){
  512. $this->success('请求成功');
  513. }
  514. foreach ($data as $key=>$value){
  515. $len = stripos($value['yj_tlgg'],'/');
  516. if ($len){
  517. $name = substr($value['yj_tlgg'],0,$len);
  518. $len_two = stripos($value['yj_tlgg'],'(');
  519. if ($len_two){
  520. $name = substr($name,0,$len_two);
  521. }
  522. $data[$key]['yj_tlgg'] = $name;
  523. }
  524. $data[$key]['rate'] = '';
  525. }
  526. $this->success('请求成功',$data);
  527. }
  528. /**
  529. * 更新工单实际投料
  530. * @ApiMethod POST
  531. * @params int UniqId
  532. * @params string number
  533. */
  534. public function updateOrderFeed(){
  535. if (Request::instance()->isPost() == false) {
  536. $this->error('非法请求');
  537. }
  538. $params = Request::instance()->post();
  539. if (!isset($params[0]['UniqId']) || empty($params[0]['UniqId'])){
  540. $this->error('参数错误');
  541. }
  542. $i = 0;
  543. foreach ($params as $key=>$value){
  544. if (!empty($value['number'])){
  545. $info = db('工单_基本资料')->where('UniqId',$value['UniqId'])->value('Gd_gdbh');
  546. $res_sql = db('工单_基本资料')->where('UniqId',$value['UniqId'])->fetchSql(true)->setField('实际投料',$value['number']);
  547. $res = Db::query($res_sql);
  548. $data['yj_实际投料'] = $value['number'];
  549. $yj_sql =db('工单_印件资料')->where('Yj_Gdbh',$info)->fetchSql(true)->update($data);
  550. $yjRes = Db::query($yj_sql);
  551. if (!$res && !$yjRes){
  552. $i++;
  553. }
  554. }
  555. }
  556. if ($i != 0){
  557. $this->success('更新成功');
  558. }else{
  559. $this->error('更新失败');
  560. }
  561. }
  562. /**
  563. * 工单工序产量统计
  564. * @ApiMethod GET
  565. * @params string order
  566. */
  567. public function getOrderProcessCount(){
  568. if (Request::instance()->isGet() == false) {
  569. $this->error('非法请求');
  570. }
  571. $params = Request::instance()->request();
  572. if (empty($params['order']) || empty($params['order'])) {
  573. $this->error('参数错误');
  574. }
  575. $order = $params['order'];
  576. $sql = "SELECT a.Gy0_yjno,a.Gy0_gxh,rtrim(a.Gy0_gxmc) as Gy0_gxmc,rtrim(a.Add_gxmc) as Add_gxmc,a.Gy0_计划接货数, a.Gy0_ls,a.Gy0_sbbh,a.PD_WG,SUM(b.sczl_cl) as sczl_cl,
  577. SUM(b.sczl_zcfp) as sczl_zcfp,SUM(b.sczl_来料少数) as 来料异常,COUNT(DISTINCT b.sczl_num) as process_num,b.sczl_Pgcl,SUM(c.sczl_cl) as cl,b.sczl_rq
  578. FROM `工单_工艺资料` a
  579. LEFT JOIN `设备_产量计酬` b ON a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh
  580. LEFT JOIN db_sczl c ON a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh
  581. WHERE a.Gy0_gdbh = '{$order}' GROUP BY a.Gy0_gxh";
  582. $data = Db::query($sql);
  583. //手检数据
  584. $handData =db('db_手工检验')->where('sczl_gdbh',$order)->field('sum(sczl_cl) as cl,rtrim(sczl_yjgx) as sczl_gxh')->select();
  585. //包装及成品防护数据
  586. $sql = "SELECT sczl_gdbh1,sczl_gdbh2,sczl_gdbh3,sczl_gdbh4,sczl_gdbh5,sczl_gdbh6,rtrim(sczl_gxmc1) as sczl_gxmc1,rtrim(sczl_gxmc2) as sczl_gxmc2,
  587. rtrim(sczl_gxmc3) as sczl_gxmc3,rtrim(sczl_gxmc4) as sczl_gxmc4,rtrim(sczl_gxmc5) as sczl_gxmc5,rtrim(sczl_gxmc6) as sczl_gxmc6,
  588. sczl_cl1,sczl_cl2,sczl_cl3,sczl_cl4,sczl_cl5,sczl_cl6,sczl_PgCl1,sczl_PgCl2,sczl_PgCl3,sczl_PgCl4,sczl_PgCl5,sczl_PgCl6
  589. FROM `mesdb`.`db_包装计件` WHERE `sczl_gdbh1` LIKE '%{$order}%' OR `sczl_gdbh2` LIKE '%{$order}%'
  590. OR `sczl_gdbh3` LIKE '%{$order}%' OR `sczl_gdbh4` LIKE '%{$order}%' OR `sczl_gdbh5` LIKE '%{$order}%' OR `sczl_gdbh6` LIKE '%{$order}%'";
  591. $package = Db::query($sql);
  592. $list = [];
  593. $j = 0;
  594. foreach ($package as $value){
  595. for ($i = 1; $i <= 6; $i++) {
  596. $gdbhlKey = "sczl_gdbh" . $i;
  597. $gxmcKey = "sczl_gxmc" . $i;
  598. $clKey = "sczl_cl" . $i;
  599. $pgclKey = "sczl_PgCl" . $i;
  600. if ($value[$gdbhlKey] == '2311743' && $value[$clKey] > 0) {
  601. $list[$j]['sczl_gdbh'] = $value[$gdbhlKey];
  602. $list[$j]['sczl_gxmc'] = $value[$gxmcKey];
  603. $list[$j]['sczl_cl'] = $value[$clKey];
  604. $list[$j]['sczl_PgCl'] = $value[$pgclKey];
  605. $j++;
  606. }
  607. }
  608. }
  609. // 创建一个关联数组,用于存储相同 sczl_gxmc 的值和对应的乘积之和
  610. $sumArray = [];
  611. foreach ($list as $item) {
  612. $key = $item['sczl_gxmc'];
  613. $cl = floatval($item['sczl_cl']);
  614. $pgCl = intval($item['sczl_PgCl']);
  615. $product = $cl * $pgCl;
  616. if (!isset($sumArray[$key])) {
  617. $sumArray[$key] = 0;
  618. }
  619. $sumArray[$key] += $product;
  620. }
  621. $handGxh = substr($handData[0]['sczl_gxh'],-2);
  622. foreach ($data as $key=>$item){
  623. $data[$key]['折算车头产量'] = round($item['Gy0_计划接货数']/$item['Gy0_ls']);
  624. $data[$key]['制程废品率'] = '';
  625. if ($item['sczl_zcfp'] > 0){
  626. $data[$key]['制程废品率'] = number_format($item['sczl_zcfp']/$item['sczl_cl']*100,4).'%';
  627. }
  628. if ($item['cl'] > 0){
  629. $data[$key]['sczl_cl'] = $item['cl'];
  630. }
  631. if ($item['sczl_Pgcl'] > 0){
  632. $data[$key]['sczl_cl'] = $item['sczl_cl'] * $item['sczl_Pgcl'];
  633. }
  634. if ($item['Gy0_gxh'] == (int)$handGxh){
  635. $data[$key]['sczl_cl'] = $handData[0]['cl'];
  636. }
  637. foreach ($sumArray as $k=>$v){
  638. if ($item['Gy0_gxmc'] == $k){
  639. $data[$key]['sczl_cl'] = $v;
  640. }
  641. }
  642. if (substr($item['PD_WG'],0,4) == '1900'){
  643. $data[$key]['PD_WG'] = '计划';
  644. }else{
  645. $data[$key]['PD_WG'] = '完工';
  646. }
  647. }
  648. $this->success('请求成功',$data);
  649. }
  650. }