OrderSuperLoss.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. $is_have_cache = Cache::get('OrderSuperLoss/getList'.$params['date']);
  124. if ($is_have_cache === false){
  125. $data = db('rec_月度废品汇总')->alias('a')
  126. ->join('工单_基本资料 b', 'a.Gd_gdbh = b.Gd_gdbh','left')
  127. ->join('工单_印件资料 c', 'a.Gd_gdbh = c.Yj_Gdbh','left')
  128. ->where($where)
  129. ->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')
  130. ->group('a.Gd_gdbh')
  131. ->order('a.Gd_cpdh asc,a.年月 desc')
  132. ->page($pages)
  133. ->limit($limit)
  134. ->select();
  135. $total = db('rec_月度废品汇总')->alias('a')
  136. ->join('工单_基本资料 b', 'a.Gd_gdbh = b.Gd_gdbh','left')
  137. ->join('工单_印件资料 c', 'a.Gd_gdbh = c.Yj_Gdbh','left')
  138. ->where($where)
  139. ->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')
  140. ->group('a.Gd_gdbh')
  141. ->count();
  142. foreach ($data as $key => $value){
  143. //查出成品数量及日期
  144. $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";
  145. $cp_data = Db::query($cp_sql);
  146. $data[$key]['warehousing_num'] = $cp_data[0]['cp_sl'];
  147. $data[$key]['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  148. //查出进入超节损的工序,有上报产量的工序就进入超节损
  149. $gxh_sql = "SELECT sczl_gxh FROM
  150. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$value['Gd_gdbh']}'
  151. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$value['Gd_gdbh']}') AS combined_result";
  152. $gxh_arr = Db::query($gxh_sql);
  153. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  154. $gy_data =db('工单_工艺资料')->where('Gy0_gdbh',$value['Gd_gdbh'])->where('Gy0_gxh','in',$gxh_values)->field('Gy0_计划接货数,Gy0_计划损耗')->select();
  155. $arr = [];
  156. $plan_loss = [];//工单计划损耗
  157. foreach ($gy_data as $k=>$v){
  158. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  159. $arr[$k] = floor($rate * 10000) /10000;
  160. $plan_loss[$k] = $v['Gy0_计划损耗'];
  161. }
  162. $target_rate = (1-array_sum($arr))*100;
  163. $data[$key]['target_rate'] =$target_rate.'%'; //目标合格率
  164. $real_rate = $cp_data[0]['cp_sl'] / ($value['实际投料'] * 10000) *100;
  165. $data[$key]['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  166. //奖惩系数及金额
  167. $data[$key]['reward_rate'] = '';
  168. $data[$key]['reward_money'] = '';
  169. //制程废品
  170. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$value['Gd_gdbh'])->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  171. $data[$key]['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $value['yj_ls'];//制程废品
  172. $data[$key]['废品合计'] = $data[$key]['zcfp'] + $value['废品合计'];//废品合计
  173. $data[$key]['intangible_loss'] = $value['实际投料'] *10000 - $cp_data[0]['cp_sl'] - $data[$key]['废品合计'];//工单无形损
  174. //材料废
  175. $waste_l =db('rec_月度废品汇总')->where('Gd_gdbh',$value['Gd_gdbh'])->where('废品类别','like','%L%')->field('SUM(废品数量) as 废品数量')->select();
  176. $data[$key]['material_waste'] = $waste_l[0]['废品数量'];
  177. //零头处理
  178. $waste_w = db('rec_月度废品汇总')->where('Gd_gdbh',$value['Gd_gdbh'])->where('废品类别','like','%M%')->field('SUM(废品数量) as 废品数量')->select();
  179. $data[$key]['minor_processing'] = $waste_w[0]['废品数量'];
  180. //外发废
  181. $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,
  182. 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,
  183. 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,
  184. 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
  185. FROM db_qczl WHERE qczl_gdbh = '{$value['Gd_gdbh']}'";
  186. $waste_out = Db::query($out_sql);
  187. $list = [];
  188. $quality = [];
  189. $j = 0;
  190. $m = 0;
  191. foreach ($waste_out as $entry) {
  192. for ($i = 1; $i <= 13; $i++) {
  193. $labelKey = "fp_lb" . $i;
  194. $bhKey = "fp_bh" . $i;
  195. $gxmcKey = "fp_gxmc" . $i;
  196. $slKey = "fp_sl" . $i;
  197. if (!empty($entry[$labelKey])) {
  198. if (substr($entry[$gxmcKey],0,2) == '99'){
  199. $list[$j]= $entry[$slKey];
  200. $j++;
  201. }
  202. }
  203. if (!empty($entry[$bhKey])) {
  204. if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  205. $quality[$m]= $entry[$slKey];
  206. $m++;
  207. }
  208. }
  209. }
  210. }
  211. $data[$key]['waste_out'] = array_sum($list);//外发废
  212. $data[$key]['waste_share'] = '';//分摊废
  213. $data[$key]['plan_loss'] = array_sum($plan_loss);//工单计划损耗
  214. $data[$key]['waste_quality'] = array_sum($quality); //质检废
  215. }
  216. $res = [
  217. 'data'=>$data,
  218. 'total' => $total
  219. ];
  220. Cache::set('OrderSuperLoss/getList'.$params['date'],$res,86400);
  221. }else{
  222. $res = Cache::get('OrderSuperLoss/getList'.$params['date']);
  223. }
  224. $this->success('请求成功',$res);
  225. }
  226. /**
  227. * 获取工单超节损工艺
  228. * @ApiMethod GET
  229. * @params string order
  230. */
  231. public function getOrderSuperLossGy(){
  232. if (Request::instance()->isGet() == false) {
  233. $this->error('非法请求');
  234. }
  235. $params = Request::instance()->param();
  236. if (!isset($params['order']) || empty($params['order'])) {
  237. $this->error('参数错误');
  238. }
  239. $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,
  240. 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
  241. LEFT JOIN `设备_产量计酬` b ON a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh
  242. LEFT JOIN `db_sczl` c ON a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh
  243. WHERE a.Gy0_gdbh = '{$params['order']}' GROUP BY a.Gy0_gxh";
  244. $data = Db::query($sql);
  245. foreach ($data as $key=>$value){
  246. if (!empty($value['total_cl'])){
  247. $data[$key]['total_cl'] = $value['total_cl'] *$value['Gy0_ls'];
  248. }else{
  249. $data[$key]['total_cl'] =$value['cl'] *$value['Gy0_ls'];
  250. }
  251. if (!empty($value['total_fp'])){
  252. $data[$key]['total_fp'] = $value['total_fp'] *$value['Gy0_ls'];
  253. }else{
  254. $data[$key]['total_fp'] = $value['fp'] *$value['Gy0_ls'];
  255. }
  256. unset($data[$key]['cl']);
  257. unset($data[$key]['fp']);
  258. }
  259. $this->success('请求成功',$data);
  260. }
  261. /**
  262. * 工单超节损统计
  263. * @ApiMethod GET
  264. * @params string order
  265. */
  266. public function getOrderSuperLossCount(){
  267. if (Request::instance()->isGet() == false) {
  268. $this->error('非法请求');
  269. }
  270. $params = Request::instance()->param();
  271. if (!isset($params['order']) || empty($params['order'])) {
  272. $this->error('参数错误');
  273. }
  274. $order = $params['order'];
  275. $field = 'Gd_gdbh,rtrim(成品代号) as 成品代号,rtrim(成品名称) as 成品名称,rtrim(销售订单号) as 销售订单号,订单数量,实际投料';
  276. $data =db('工单_基本资料')->where('Gd_Gdbh',$order)->where('行号',1)->field($field)->find();
  277. //查出成品数量及日期
  278. $cp_sql = "SELECT SUM(jjcp_sl) as cp_sl,MAX(jjcp_sj) as jjcp_sj FROM `成品入仓` WHERE jjcp_gdbh = '{$order}' GROUP BY jjcp_gdbh";
  279. $cp_data = Db::query($cp_sql);
  280. $data['warehousing_num'] = $cp_data[0]['cp_sl'];
  281. $data['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  282. //查出进入超节损的工序,有上报产量的工序就进入超节损
  283. $gxh_sql = "SELECT sczl_gxh FROM
  284. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$order}'
  285. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$order}') AS combined_result";
  286. $gxh_arr = Db::query($gxh_sql);
  287. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  288. $gy_data =db('工单_工艺资料')
  289. ->alias('a')
  290. ->field([
  291. '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_计划接货数',
  292. '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',
  293. ])
  294. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh','left')
  295. ->join('db_sczl c', 'a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh','left')
  296. ->where([
  297. 'a.Gy0_gdbh' => $order,
  298. 'a.Gy0_gxh' => ['in', $gxh_values]
  299. ])
  300. ->group('a.Gy0_gxh')
  301. ->select();
  302. $arr = [];
  303. $plan_loss = [];//工单计划损耗
  304. foreach ($gy_data as $k=>$v){
  305. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  306. $arr[$k] = floor($rate * 10000) /10000;
  307. $plan_loss[$k] = $v['Gy0_计划损耗'];
  308. }
  309. $target_rate = (1-array_sum($arr))*100;
  310. $data['target_rate'] =$target_rate.'%'; //目标合格率
  311. $real_rate = $cp_data[0]['cp_sl'] / ($data['实际投料'] * 10000) *100;
  312. $data['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  313. //制程废品
  314. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$order)->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  315. //联数
  316. $ls =db('工单_印件资料')->where('Yj_Gdbh',$order)->value('yj_ls');
  317. //制程废品*ls
  318. $data['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $ls;
  319. //废品数量
  320. $waste =db('rec_月度废品汇总')->where('Gd_gdbh',$order)->field('SUM(废品数量) as 废品合计')->select();
  321. //废品合计
  322. $waste_total = $data['zcfp'] + $waste[0]['废品合计'];
  323. //工单无形损
  324. $data['intangible_loss'] = $data['实际投料'] *10000 - $cp_data[0]['cp_sl'] - $waste_total;
  325. //材料废
  326. $waste_l =db('rec_月度废品汇总')->where('Gd_gdbh',$order)->where('废品类别','like','%L%')->field('SUM(废品数量) as 废品数量')->select();
  327. $data['material_waste'] = $waste_l[0]['废品数量'];
  328. //零头处理
  329. $waste_w = db('rec_月度废品汇总')->where('Gd_gdbh',$order)->where('废品类别','like','%M%')->field('SUM(废品数量) as 废品数量')->select();
  330. $data['minor_processing'] = $waste_w[0]['废品数量'];
  331. //外发废
  332. $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,
  333. 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,
  334. 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,
  335. 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
  336. FROM db_qczl WHERE qczl_gdbh = '{$order}'";
  337. $waste_out = Db::query($out_sql);
  338. $list = [];
  339. $quality = [];
  340. $j = 0;
  341. $m = 0;
  342. foreach ($waste_out as $entry) {
  343. for ($i = 1; $i <= 13; $i++) {
  344. $labelKey = "fp_lb" . $i;
  345. $bhKey = "fp_bh" . $i;
  346. $gxmcKey = "fp_gxmc" . $i;
  347. $slKey = "fp_sl" . $i;
  348. if (!empty($entry[$labelKey])) {
  349. if (substr($entry[$gxmcKey],0,2) == '99'){
  350. $list[$j]= $entry[$slKey];
  351. $j++;
  352. }
  353. }
  354. if (!empty($entry[$bhKey])) {
  355. if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  356. $quality[$m]= $entry[$slKey];
  357. $m++;
  358. }
  359. }
  360. }
  361. }
  362. $data['waste_out'] = array_sum($list);//外发废
  363. //质检废
  364. $data['waste_quality'] = array_sum($quality);
  365. $plan_total =db('工单_工艺资料')->where(['Gy0_gdbh' => $order, 'Gy0_gxh' => ['in', $gxh_values]])->value('SUM(Gy0_计划损耗)');
  366. // halt($plan_total);
  367. //按工序打印
  368. if ($params['type'] == 1){
  369. $total = [];
  370. $total['plan_loss'] = 0;
  371. $total['total_fp'] = 0;
  372. $total['waste_quality'] = 0;
  373. $total['waste_intangible'] = 0;
  374. $total['total_waste'] = 0;
  375. $total['loss'] = 0;
  376. foreach ($gy_data as $k=>$item){
  377. $gy_data[$k]['waste_quality'] = 0;
  378. $gy_data[$k]['total_fp'] = $item['total_fp'] * $item['Gy0_ls'];
  379. $gy_data[$k]['total_cl'] = $item['total_cl'] * $item['Gy0_ls'];
  380. $gy_data[$k]['intangible_loss'] = round($item['Gy0_计划损耗'] / $plan_total * $data['intangible_loss']);
  381. foreach ($waste_out as $entry) {
  382. for ($i = 1; $i <= 13; $i++) {
  383. $gxmcKey = "fp_gxmc" . $i;
  384. $slKey = "fp_sl" . $i;
  385. if ((int)substr($entry[$gxmcKey],0,2) == $item['Gy0_gxh']){
  386. $gy_data[$k]['waste_quality'] += $entry[$slKey];
  387. }
  388. }
  389. }
  390. $gy_data[$k]['total_waste'] = $gy_data[$k]['waste_quality'] + $gy_data[$k]['intangible_loss'] + $gy_data[$k]['total_fp'];
  391. $gy_data[$k]['loss'] = $item['Gy0_计划损耗'] - $gy_data[$k]['total_waste'];
  392. $gy_data[$k]['loss_rate'] = number_format($gy_data[$k]['loss'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  393. $gy_data[$k]['target_loss_rate'] = number_format($item['Gy0_计划损耗'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  394. $gy_data[$k]['actual_loss_rate'] = number_format($gy_data[$k]['total_waste'] / $item['Gy0_计划接货数'],4) * 100 .'%';
  395. $gy_data[$k]['超节损承担比例'] = number_format($item['超节损承担比例'],4) * 100 .'%';
  396. $total['plan_loss'] += $gy_data[$k]['Gy0_计划损耗'];
  397. $total['total_fp'] += $gy_data[$k]['total_fp'];
  398. $total['waste_quality'] += $gy_data[$k]['waste_quality'];
  399. $total['waste_intangible'] += $gy_data[$k]['intangible_loss'];
  400. $total['total_waste'] += $gy_data[$k]['total_waste'];
  401. $total['loss'] += $gy_data[$k]['loss'];
  402. }
  403. $data['gy_data'] = $gy_data;
  404. $data['total'] = $total;
  405. $this->success('请求成功',$data);
  406. }else{ //按班组打印
  407. }
  408. }
  409. /**
  410. * 获取工单工艺
  411. * @ApiMethod GET
  412. * @params string order
  413. */
  414. public function getOrderGy(){
  415. if (Request::instance()->isGet() == false) {
  416. $this->error('非法请求');
  417. }
  418. $params = Request::instance()->param();
  419. if (!isset($params['order']) || empty($params['order'])) {
  420. $this->error('参数错误');
  421. }
  422. $order = $params['order'];
  423. $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';
  424. $data =db('工单_工艺资料')->where('Gy0_gdbh',$order)->field($field)->select();
  425. $this->success('请求成功',$data);
  426. }
  427. /**
  428. * 更新工单工艺
  429. * @ApiMethod POST
  430. * @params array data
  431. */
  432. public function updateOrderGy(){
  433. if (Request::instance()->isPost() == false) {
  434. $this->error('非法请求');
  435. }
  436. $params = Request::instance()->request();
  437. if (!isset($params) || !isset($params[0]['UniqId'])){
  438. $this->error('参数不能为空');
  439. }
  440. $i = 0;
  441. foreach ($params as $key=>$value){
  442. $data = [];
  443. $data['无形损承担比例'] = $value['loss_one'];
  444. $data['超节损承担比例'] = $value['loss_two'];
  445. $data['超节损核算单价'] = $value['loss_thr'];
  446. $sql =db('工单_工艺资料')->where('UniqId',$value['UniqId'])->fetchSql(true)->update($data);
  447. $res = Db::query($sql);
  448. if ($res !== false){
  449. $i++;
  450. }
  451. }
  452. if ($i !== 0){
  453. $this->success('更新成功');
  454. }else{
  455. $this->error('更新失败');
  456. }
  457. }
  458. /**
  459. * 获取工单印件考核资料
  460. * @ApiMethod GET
  461. * @params string order
  462. */
  463. public function getOrderYj(){
  464. if (Request::instance()->isGet() == false) {
  465. $this->error('非法请求');
  466. }
  467. $params = Request::instance()->request();
  468. if (!isset($params['order']) || empty($params['order'])) {
  469. $this->error('参数错误');
  470. }
  471. $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();
  472. $this->success('请求成功',$data);
  473. }
  474. /**
  475. * 更新工单印件考核资料
  476. * @ApiMethod POST
  477. * @params array data
  478. */
  479. public function updateOrderYj(){
  480. if (Request::instance()->isPost() == false) {
  481. $this->error('非法请求');
  482. }
  483. $params = Request::instance()->request();
  484. if (!isset($params['order'])){
  485. $this->error('参数不能为空');
  486. }
  487. if (!isset($params['yj_yjno'])){
  488. $this->error('参数不能为空');
  489. }
  490. if (!isset($params['examine'])){
  491. $this->error('参数不能为空');
  492. }
  493. $where['Yj_gdbh'] = $params['order'];
  494. $where['yj_Yjno'] = $params['yj_yjno'];
  495. $res =db('工单_印件资料')->where($where)->setField('质量考核',$params['examine']);
  496. if ($res !== false){
  497. $this->success('更新成功');
  498. }else{
  499. $this->error('更新失败');
  500. }
  501. }
  502. /**
  503. * 获取修正工单实际投料列表
  504. * @ApiMethod GET
  505. * @params string year
  506. * @params string month
  507. */
  508. public function getOrderFeedList(){
  509. if (Request::instance()->isGet() == false) {
  510. $this->error('非法请求');
  511. }
  512. $params = Request::instance()->request();
  513. if (empty($params['year']) || empty($params['month'])) {
  514. $this->error('参数错误');
  515. }
  516. $search = $params['year'].'.'.$params['month'];
  517. $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,
  518. 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";
  519. $data =db('工单_基本资料')->alias('a')
  520. ->join('工单_印件资料 c','a.Gd_gdbh = c.Yj_Gdbh','left')
  521. ->join('工单_bom资料 b','c.Yj_Gdbh = b.BOM_工单编号 AND c.yj_zzdh = b.BOM_物料编码','left')
  522. ->where('a.投料确认','like','%'.$search.'%')
  523. ->field($field)
  524. ->group('a.Gd_gdbh')
  525. ->order('b.BOM_投料单位,a.UniqId asc')
  526. ->select();
  527. if (empty($data)){
  528. $this->success('请求成功');
  529. }
  530. foreach ($data as $key=>$value){
  531. $len = stripos($value['yj_tlgg'],'/');
  532. if ($len){
  533. $name = substr($value['yj_tlgg'],0,$len);
  534. $len_two = stripos($value['yj_tlgg'],'(');
  535. if ($len_two){
  536. $name = substr($name,0,$len_two);
  537. }
  538. $data[$key]['yj_tlgg'] = $name;
  539. }
  540. $data[$key]['rate'] = '';
  541. }
  542. $this->success('请求成功',$data);
  543. }
  544. /**
  545. * 更新工单实际投料
  546. * @ApiMethod POST
  547. * @params int UniqId
  548. * @params string number
  549. */
  550. public function updateOrderFeed(){
  551. if (Request::instance()->isPost() == false) {
  552. $this->error('非法请求');
  553. }
  554. $params = Request::instance()->post();
  555. if (!isset($params[0]['UniqId']) || empty($params[0]['UniqId'])){
  556. $this->error('参数错误');
  557. }
  558. $i = 0;
  559. foreach ($params as $key=>$value){
  560. if (!empty($value['number'])){
  561. $info = db('工单_基本资料')->where('UniqId',$value['UniqId'])->value('Gd_gdbh');
  562. $res_sql = db('工单_基本资料')->where('UniqId',$value['UniqId'])->fetchSql(true)->setField('实际投料',$value['number']);
  563. $res = Db::query($res_sql);
  564. $data['yj_实际投料'] = $value['number'];
  565. $yj_sql =db('工单_印件资料')->where('Yj_Gdbh',$info)->fetchSql(true)->update($data);
  566. $yjRes = Db::query($yj_sql);
  567. if (!$res && !$yjRes){
  568. $i++;
  569. }
  570. }
  571. }
  572. if ($i != 0){
  573. $this->success('更新成功');
  574. }else{
  575. $this->error('更新失败');
  576. }
  577. }
  578. /**
  579. * 工单工序产量统计
  580. * @ApiMethod GET
  581. * @params string order
  582. */
  583. public function getOrderProcessCount(){
  584. if (Request::instance()->isGet() == false) {
  585. $this->error('非法请求');
  586. }
  587. $params = Request::instance()->request();
  588. if (empty($params['order']) || empty($params['order'])) {
  589. $this->error('参数错误');
  590. }
  591. $order = $params['order'];
  592. $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,
  593. 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
  594. FROM `工单_工艺资料` a
  595. LEFT JOIN `设备_产量计酬` b ON a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_gxh = b.sczl_gxh
  596. LEFT JOIN db_sczl c ON a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_gxh = c.sczl_gxh
  597. WHERE a.Gy0_gdbh = '{$order}' GROUP BY a.Gy0_gxh";
  598. $data = Db::query($sql);
  599. //手检数据
  600. $handData =db('db_手工检验')->where('sczl_gdbh',$order)->field('sum(sczl_cl) as cl,rtrim(sczl_yjgx) as sczl_gxh')->select();
  601. //包装及成品防护数据
  602. $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,
  603. rtrim(sczl_gxmc3) as sczl_gxmc3,rtrim(sczl_gxmc4) as sczl_gxmc4,rtrim(sczl_gxmc5) as sczl_gxmc5,rtrim(sczl_gxmc6) as sczl_gxmc6,
  604. sczl_cl1,sczl_cl2,sczl_cl3,sczl_cl4,sczl_cl5,sczl_cl6,sczl_PgCl1,sczl_PgCl2,sczl_PgCl3,sczl_PgCl4,sczl_PgCl5,sczl_PgCl6
  605. FROM `mesdb`.`db_包装计件` WHERE `sczl_gdbh1` LIKE '%{$order}%' OR `sczl_gdbh2` LIKE '%{$order}%'
  606. OR `sczl_gdbh3` LIKE '%{$order}%' OR `sczl_gdbh4` LIKE '%{$order}%' OR `sczl_gdbh5` LIKE '%{$order}%' OR `sczl_gdbh6` LIKE '%{$order}%'";
  607. $package = Db::query($sql);
  608. $list = [];
  609. $j = 0;
  610. foreach ($package as $value){
  611. for ($i = 1; $i <= 6; $i++) {
  612. $gdbhlKey = "sczl_gdbh" . $i;
  613. $gxmcKey = "sczl_gxmc" . $i;
  614. $clKey = "sczl_cl" . $i;
  615. $pgclKey = "sczl_PgCl" . $i;
  616. if ($value[$gdbhlKey] == '2311743' && $value[$clKey] > 0) {
  617. $list[$j]['sczl_gdbh'] = $value[$gdbhlKey];
  618. $list[$j]['sczl_gxmc'] = $value[$gxmcKey];
  619. $list[$j]['sczl_cl'] = $value[$clKey];
  620. $list[$j]['sczl_PgCl'] = $value[$pgclKey];
  621. $j++;
  622. }
  623. }
  624. }
  625. // 创建一个关联数组,用于存储相同 sczl_gxmc 的值和对应的乘积之和
  626. $sumArray = [];
  627. foreach ($list as $item) {
  628. $key = $item['sczl_gxmc'];
  629. $cl = floatval($item['sczl_cl']);
  630. $pgCl = intval($item['sczl_PgCl']);
  631. $product = $cl * $pgCl;
  632. if (!isset($sumArray[$key])) {
  633. $sumArray[$key] = 0;
  634. }
  635. $sumArray[$key] += $product;
  636. }
  637. $handGxh = substr($handData[0]['sczl_gxh'],-2);
  638. foreach ($data as $key=>$item){
  639. $data[$key]['折算车头产量'] = round($item['Gy0_计划接货数']/$item['Gy0_ls']);
  640. $data[$key]['制程废品率'] = '';
  641. if ($item['sczl_zcfp'] > 0){
  642. $data[$key]['制程废品率'] = number_format($item['sczl_zcfp']/$item['sczl_cl']*100,4).'%';
  643. }
  644. if ($item['cl'] > 0){
  645. $data[$key]['sczl_cl'] = $item['cl'];
  646. }
  647. if ($item['sczl_Pgcl'] > 0){
  648. $data[$key]['sczl_cl'] = $item['sczl_cl'] * $item['sczl_Pgcl'];
  649. }
  650. if ($item['Gy0_gxh'] == (int)$handGxh){
  651. $data[$key]['sczl_cl'] = $handData[0]['cl'];
  652. }
  653. foreach ($sumArray as $k=>$v){
  654. if ($item['Gy0_gxmc'] == $k){
  655. $data[$key]['sczl_cl'] = $v;
  656. }
  657. }
  658. if (substr($item['PD_WG'],0,4) == '1900'){
  659. $data[$key]['PD_WG'] = '计划';
  660. }else{
  661. $data[$key]['PD_WG'] = '完工';
  662. }
  663. }
  664. $this->success('请求成功',$data);
  665. }
  666. }