FinishedProductWarehousing.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\controller\OrderSuperLoss;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\exception\DbException;
  8. use think\Db;
  9. /**
  10. * 成品入仓维护接口
  11. */
  12. class FinishedProductWarehousing extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页
  18. *成品入仓测试
  19. */
  20. public function index()
  21. {
  22. $this->success('请求成功');
  23. }
  24. /**
  25. * 侧边栏
  26. * @ApiMethod (GET)
  27. */
  28. public function getTab()
  29. {
  30. //get请求
  31. if(!$this->request->isGet()){
  32. $this->error('请求方式错误');
  33. }
  34. $rows = db('成品入仓')
  35. ->field('LEFT(Sys_rq, 10) as date, COUNT(*) as counts')
  36. ->group('date')
  37. ->order('UniqId desc')
  38. ->limit(50)
  39. ->select();
  40. $num = db('成品入仓')
  41. ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
  42. ->count();
  43. $arr = db('成品入仓')
  44. ->field('LEFT(Sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
  45. ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
  46. ->group('date, sys_id')
  47. ->limit($num)
  48. ->select();
  49. foreach($rows as $key=>$value){
  50. $rows[$key]['sys'] = [];
  51. foreach($arr as $k=>$v){
  52. if($value['date'] == $v['date']){
  53. unset($v['date']);
  54. array_push($rows[$key]['sys'],$v);
  55. unset($arr[$k]);
  56. }
  57. }
  58. $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
  59. }
  60. $this->success('成功',$rows);
  61. }
  62. /**
  63. * 侧边栏
  64. * @ApiMethod (GET)
  65. */
  66. public function getTabByGdbh()
  67. {
  68. //get请求
  69. if(!$this->request->isGet()){
  70. $this->error('请求方式错误');
  71. }
  72. $num = db('成品入仓')->count();
  73. $rows = db('成品入仓')
  74. ->field('jjcp_gdbh, rtrim(jjcp_cpdh) as jjcp_cpdh, LEFT(Sys_rq, 10) as Sys_rq')
  75. ->order('Sys_rq desc')
  76. ->limit($num)
  77. ->select();
  78. $num = db('工单_基本资料')->count();
  79. $arr = db('工单_基本资料')
  80. ->limit($num)
  81. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, CONCAT(rtrim(Gd_khdh), " 【", rtrim(Gd_khmc)) AS kh');
  82. $data = [];
  83. foreach ($rows as $value) {
  84. $key = $value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'];
  85. if (isset($arr[$key]) && !isset($data[$arr[$key]])) {
  86. $data[$arr[$key]] = $value['Sys_rq'];
  87. }
  88. }
  89. // 对键进行升序排序
  90. uksort($data, function($a, $b) use ($data) {
  91. if ($data[$a] == $data[$b]) {
  92. return ($a > $b) ? 1 : -1; // 在值相同的情况下,根据键进行升序排列
  93. }
  94. return ($data[$a] < $data[$b]) ? 1 : -1;
  95. });
  96. $this->success('成功',$data);
  97. }
  98. /**
  99. * 列表
  100. * @ApiMethod (GET)
  101. * @param string $date 时间
  102. * @param string $sys_id 用户
  103. */
  104. public function getList()
  105. {
  106. // 验证请求方式
  107. if (!$this->request->isGet()) {
  108. $this->error('请求方式错误');
  109. }
  110. $req = $this->request->param();
  111. if (!isset($req['date'])) {
  112. $this->error('参数缺失');
  113. }
  114. // 设置分页参数,默认值
  115. $page = isset($req['page']) && !empty($req['page']) ? intval($req['page']) : 1;
  116. $limit = isset($req['limit']) && !empty($req['limit']) ? intval($req['limit']) : 20;
  117. // 构建查询条件
  118. if (strpos($req['date'], '-')) {
  119. $where = ['c.Sys_rq' => ['like', $req['date'] . '%']];
  120. $option = ['Sys_rq' => ['like', $req['date'] . '%']];
  121. } else {
  122. $where = [];
  123. $option = [];
  124. }
  125. if (isset($req['sys_id']) && !empty($req['sys_id'])) {
  126. $where['c.Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
  127. $option['Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
  128. }
  129. // 获取总数
  130. $total = db('成品入仓')->where($option)->count();
  131. // 查询数据(带分页)
  132. $rows = db('成品入仓')->alias('c')
  133. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  134. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  135. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  136. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  137. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  138. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  139. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  140. ->where($where)
  141. ->join('工单_基本资料 g', 'g.Gd_gdbh=c.jjcp_gdbh AND g.行号 = c.jjcp_yjno AND g.Gd_cpdh=c.jjcp_cpdh')
  142. ->order('c.UniqId desc')
  143. ->limit(($page - 1) * $limit, $limit)
  144. ->select();
  145. // 处理数据
  146. foreach ($rows as $key => $value) {
  147. $row = db('成品入仓')
  148. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  149. ->where(['jjcp_gdbh' => $value['jjcp_gdbh'], 'jjcp_cpdh' => $value['jjcp_cpdh']])
  150. ->order('Sys_rq desc')
  151. ->select();
  152. $rows[$key]['jjcp_sls'] = 0;
  153. foreach ($row as $k => $v) {
  154. $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
  155. }
  156. if ($value['订单数量'] != 0) {
  157. $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls'] / $value['订单数量'] / 100, 2);
  158. } else {
  159. $rows[$key]['完成率'] = '';
  160. }
  161. $rows[$key]['订单数量'] = floatval($value['订单数量'] * 10000);
  162. $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
  163. $rows[$key]['Mod_rq'] = $value['Mod_rq'] == '1900-01-01 00:00:00' ? '' : $value['Mod_rq'];
  164. }
  165. $data = [
  166. 'total' => $total,
  167. 'rows' => $rows,
  168. ];
  169. $this->success('成功', $data);
  170. }
  171. /**
  172. * 定位
  173. * @ApiMethod (GET)
  174. * @param string $gdbh 工单编号
  175. * @param string $cpmc 产品名称
  176. * @param string $page 页码
  177. * @param string $limit 数量
  178. */
  179. public function locate()
  180. {
  181. //get请求
  182. if(!$this->request->isGet()){
  183. $this->error('请求方式错误');
  184. }
  185. $req = $this->request->param();
  186. $page = 1;
  187. $limit = 15;
  188. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  189. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  190. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  191. $where = ['c.jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
  192. $option = ['jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
  193. //客户编号
  194. $rows = db('成品入仓')->alias('c')
  195. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  196. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  197. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  198. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
  199. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  200. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  201. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  202. ->where($where)
  203. ->where('y.yj_Yjno=c.jjcp_yjno')
  204. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  205. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  206. ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
  207. ->order('c.UniqId desc')
  208. ->page($page,$limit)
  209. ->select();
  210. $total = db('成品入仓')->where($option)->count();
  211. }else{
  212. if (isset($req['cpmc']) && !empty($req['cpmc'])){
  213. //查询工单表
  214. $gd = db('工单_基本资料')
  215. ->where('Gd_cpmc', 'LIKE', '%'.$req['cpmc'].'%')
  216. ->column('Gd_gdbh');
  217. $where = ['c.jjcp_gdbh'=>['in', $gd]];
  218. $option = ['jjcp_gdbh'=>['in', $gd]];
  219. //客户编号
  220. $rows = db('成品入仓')->alias('c')
  221. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  222. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  223. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  224. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
  225. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  226. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  227. rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  228. ->where($where)
  229. ->where('y.yj_Yjno=c.jjcp_yjno')
  230. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  231. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  232. ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
  233. ->order('c.UniqId desc')
  234. ->page($page,$limit)
  235. ->select();
  236. $total = db('成品入仓')->where($option)->count();
  237. }else{
  238. $this->error('参数错误');
  239. }
  240. }
  241. foreach ($rows as $key=>$value) {
  242. $row = db('成品入仓')
  243. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  244. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  245. ->order('Sys_rq desc')
  246. ->select();
  247. $rows[$key]['jjcp_sls'] = 0;
  248. foreach ($row as $k=>$v) {
  249. $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
  250. }
  251. if ($value['订单数量']!=0){
  252. $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls']/$value['订单数量']/100,2);
  253. }else{
  254. $rows[$key]['完成率'] = '';
  255. }
  256. $rows[$key]['订单数量'] = floatval($value['订单数量']*10000);
  257. $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
  258. $rows[$key]['Mod_rq'] = $value['Mod_rq']=='1900-01-01 00:00:00' ? '' :$value['Mod_rq'];
  259. }
  260. $data = [
  261. 'total' => $total,
  262. 'rows' => $rows,
  263. ];
  264. $this->success('成功',$data);
  265. }
  266. /**
  267. * 获取信息
  268. * @ApiMethod (GET)
  269. * @param string $UniqId UniqId
  270. */
  271. public function getInfo()
  272. {
  273. //get请求
  274. if(!$this->request->isGet()){
  275. $this->error('请求方式错误');
  276. }
  277. $req = $this->request->param();
  278. if (isset($req['UniqId']) && !empty($req['UniqId'])){
  279. $UniqId = $req['UniqId'];
  280. }else{
  281. $this->error('参数错误');
  282. }
  283. //客户编号
  284. $rows = db('成品入仓')->alias('c')
  285. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  286. rtrim(c.jjcp_num) as jjcp_num,
  287. c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.订单编号) as 订单编号,
  288. rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  289. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  290. rtrim(c.客户料号) as 客户料号,
  291. rtrim(c.jjcp_smb) as jjcp_smb, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  292. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw,
  293. g.订单数量*10000 as 订单数量, rtrim(c.机型备注) as 机型备注, rtrim(c.jjcp_desc) as jjcp_desc,
  294. rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
  295. ->where('c.UniqId',$UniqId)
  296. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  297. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  298. ->find();
  299. $rows['jjcp_sls'] = db('成品入仓')
  300. ->where(['jjcp_gdbh'=>$rows['jjcp_gdbh'], 'jjcp_cpdh'=>$rows['jjcp_cpdh']])
  301. ->sum('jjcp_sl');
  302. $this->success('成功',$rows);
  303. }
  304. /**
  305. * 获取仓库信息
  306. * @ApiMethod (GET)
  307. * @param string keyword 关键词
  308. */
  309. public function getCk()
  310. {
  311. //get请求
  312. if(!$this->request->isGet()){
  313. $this->error('请求方式错误');
  314. }
  315. $req = $this->request->param();
  316. $key_word = '';
  317. if (isset($req['key_word']) && !empty($req['key_word'])){
  318. $key_word = $req['key_word'];
  319. }
  320. $rs = db('物料_仓库信息')
  321. ->field('rtrim(编号) as 编号, rtrim(名称) as 名称')
  322. ->where('编号|名称','like','%'.$key_word.'%')
  323. ->where('名称','in',['面材仓库','主要产成品'])
  324. ->select();
  325. $this->success('成功',$rs);
  326. }
  327. /**
  328. * 查询印件工序及产品名称
  329. * @ApiMethod (GET)
  330. * @param string $gdbh 工单编号
  331. */
  332. public function getGd()
  333. {
  334. //get请求
  335. if(!$this->request->isGet()){
  336. $this->error('请求方式错误');
  337. }
  338. $req = $this->request->param();
  339. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  340. $gdbh = $req['gdbh'];
  341. }else{
  342. $this->error('参数错误');
  343. }
  344. $rows = db('工单_基本资料')->alias('g')
  345. ->field('g.Gd_gdbh, j.yj_yjno, rtrim(g.销售订单号) as 销售订单号, rtrim(j.yj_Yjdh) as yj_Yjdh, rtrim(j.yj_yjmc) as yj_yjmc, rtrim(g.成品代号) as 成品代号, rtrim(g.成品名称) as 成品名称, rtrim(g.客户料号) as 客户料号')
  346. ->where(['g.Gd_gdbh'=>$gdbh])
  347. ->join(['工单_印件资料'=>'j'],'g.Gd_cpdh=j.yj_yjdh and g.Gd_gdbh=j.Yj_Gdbh')
  348. ->group('j.yj_yjno')
  349. ->order('j.yj_yjno asc')
  350. ->select();
  351. foreach ($rows as $key=>$value){
  352. $rows[$key]['gdyj'] = $value['Gd_gdbh'].' -'.sprintf("%02d", $value['yj_yjno']).'-'.$value['yj_yjmc'];
  353. }
  354. $this->success('成功',$rows);
  355. }
  356. /**
  357. * 查询工单数量
  358. * @ApiMethod (GET)
  359. * @param string $gdbh 工单编号
  360. * @param string $cpdh 产品代号
  361. * @param string $rcsl 入仓数量
  362. */
  363. public function getGdsl()
  364. {
  365. //get请求
  366. if(!$this->request->isGet()){
  367. $this->error('请求方式错误');
  368. }
  369. $req = $this->request->param();
  370. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  371. $gdbh = $req['gdbh'];
  372. }else{
  373. $this->error('参数错误');
  374. }
  375. if (isset($req['cpdh']) && !empty($req['cpdh'])){
  376. $cpdh = $req['cpdh'];
  377. }else{
  378. $this->error('参数错误');
  379. }
  380. $rows = db('工单_基本资料')->alias('g')
  381. ->field('订单数量*10000 as 订单数量, gd_statu')
  382. ->where(['Gd_gdbh'=>$gdbh, 'Gd_cpdh'=>$cpdh])
  383. ->find();
  384. if (!$rows) $this->error('未查询到记录');
  385. $rows['jjcp_sls'] = db('成品入仓')
  386. ->where(['jjcp_gdbh'=>$gdbh, 'jjcp_cpdh'=>$cpdh])
  387. ->sum('jjcp_sl');
  388. $this->success('成功',$rows);
  389. }
  390. /**
  391. * 新增
  392. * @ApiMethod (POST)
  393. */
  394. public function add()
  395. {
  396. if (!$this->request->isPost()) {
  397. $this->error('请求方式错误');
  398. }
  399. $req = $this->request->param();
  400. $data = $this->buildAddData($req);
  401. $this->validateProductName($data['jjcp_gdbh'], $data['jjcp_yjno'], $data['jjcp_cpmc']);
  402. $existingEndBoard = $this->findExistingEndBoard($data['jjcp_gdbh'], $data['jjcp_yjno']);
  403. if ($existingEndBoard) {
  404. $this->error(
  405. $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
  406. );
  407. }
  408. $this->validateMaterialReceived($data['jjcp_gdbh'], $data['jjcp_yjno']);
  409. $data['UniqId'] = $this->generateFinishStockUniqId();
  410. $mongodb = Db::connect(config('database.mongodb'));
  411. $this->saveFinishStock($data, $mongodb);
  412. try {
  413. $synchronization = new \app\api\controller\Synchronization();
  414. $synchronization->syncFinishStockMiddle($data['UniqId'], '新增');
  415. } catch (\Exception $e) {
  416. $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $data['UniqId']]);
  417. }
  418. if ($this->isEndBoard($data['jjcp_smb'])) {
  419. $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
  420. }
  421. $this->success('成功', ['UniqId' => $data['UniqId']]);
  422. }
  423. /**
  424. * 查询成品入仓同步操作日志
  425. * @ApiMethod (GET)
  426. * @param int|string $UniqId 成品入仓 UniqId
  427. */
  428. public function getSyncLog()
  429. {
  430. if (!$this->request->isGet()) {
  431. $this->error('请求方式错误');
  432. }
  433. $uniqId = $this->request->param('UniqId');
  434. if ($uniqId === null || $uniqId === '') {
  435. $this->error('参数错误', 'UniqId');
  436. }
  437. $list = db('工单_成品入仓同步操作日志')
  438. ->where('UniqId', (int)$uniqId)
  439. ->field('id,Gdbh,wlbm,yjmc,Yjdh,Yjno,sys_id,sys_rq,type,status,status_msg,UniqId')
  440. ->order('id desc')
  441. ->select();
  442. $this->success('成功', $list ?: []);
  443. }
  444. /**
  445. * 修改
  446. * @ApiMethod (POST)
  447. */
  448. public function edit()
  449. {
  450. if(!$this->request->isPost()){
  451. $this->error('请求方式错误');
  452. }
  453. //连接mongodb
  454. $mongodb = Db::connect(config('database.mongodb'));
  455. $req = $this->request->param();
  456. $arr = [
  457. '入仓类型', '仓库编号', '仓库名称',
  458. 'jjcp_num',
  459. 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
  460. 'jjcp_cpdh', 'jjcp_cpmc',
  461. '成品编码', '成品名称',
  462. '客户料号',
  463. 'jjcp_smb', 'jjcp_sj',
  464. 'jjcp_sl', 'jjcp_dw',
  465. '机型备注',
  466. 'jjcp_desc'
  467. ];
  468. $data = [];
  469. foreach ($arr as $key => $value){
  470. if (!isset($req[$value])){
  471. continue;
  472. }
  473. $data[$value] = $req[$value];
  474. }
  475. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  476. $this->error('参数错误','UniqId',100);
  477. }
  478. if (count($data)==0){
  479. $this->error('参数错误','',111);
  480. }
  481. $data['Mod_rq'] = date('Y-m-d H:i:s');
  482. //判断当前工单是否存在已提交模版
  483. if ($data['jjcp_smb'] === '末 板'){
  484. $existingRecord = db('成品入仓')
  485. ->where('jjcp_gdbh', $data['jjcp_gdbh'])
  486. ->where('jjcp_yjno', $data['jjcp_yjno'])
  487. ->where('jjcp_smb', '<>', '') // 判断 jjcp_smb 不为空
  488. ->find();
  489. if ($existingRecord && $existingRecord['UniqId'] !== (int)$req['UniqId']) {
  490. $this->error($existingRecord['jjcp_gdbh'].'工单 -- 印件'.$existingRecord['jjcp_yjno'].'已经进入末版,无法继续操作');
  491. }
  492. };
  493. //开启事务
  494. db()->startTrans();
  495. try{
  496. $sql = db('成品入仓')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
  497. $bool = db()->query($sql);
  498. //同步更新monodb数据
  499. $res = $mongodb->name('finished_products')
  500. ->where('UniqId', (int)$req['UniqId']) // 使用实际的UniqId值
  501. ->update($data);
  502. // 提交事务
  503. db()->commit();
  504. } catch (\Exception $e) {
  505. // 回滚事务
  506. db()->rollback();
  507. $this->error($e->getMessage());
  508. }
  509. if($bool===false) $this->error('失败');
  510. try {
  511. $synchronization = new \app\api\controller\Synchronization();
  512. $synchronization->syncFinishStockMiddle($req['UniqId'], '修改');
  513. } catch (\Exception $e) {
  514. $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $req['UniqId']]);
  515. }
  516. $oederLoss = new \app\api\controller\OrderSuperLoss();
  517. if (isset($data['jjcp_smb']) && strpos($data['jjcp_smb'],'末') !== false){
  518. try {
  519. $result = $oederLoss->OneOrderSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
  520. } catch (DataNotFoundException $e) {
  521. } catch (ModelNotFoundException $e) {
  522. } catch (DbException $e) {
  523. }
  524. db('工单_质量考核汇总')
  525. ->where('Gy0_gdbh',$data['jjcp_gdbh'])
  526. ->where('印件及工序',$data['jjcp_yjno'])
  527. ->delete();
  528. $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
  529. $res = db()->query($sql);
  530. if($res===false) $this->error('失败');
  531. }
  532. $this->success('成功');
  533. }
  534. /**
  535. * 修改
  536. * @ApiMethod (POST)
  537. * @param string 'UniqId'
  538. */
  539. public function del()
  540. {
  541. if(!$this->request->isPost()){
  542. $this->error('请求方式错误');
  543. }
  544. //连接mongodb
  545. $mongodb = Db::connect(config('database.mongodb'));
  546. $req = $this->request->param();
  547. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  548. $this->error('参数错误','UniqId',100);
  549. }
  550. //开启事务
  551. db()->startTrans();
  552. try{
  553. $bool = db('成品入仓')->where('UniqId',$req['UniqId'])->delete();
  554. //同时删除mongdb数据
  555. //同步更新monodb数据
  556. $res = $mongodb->name('finished_products')
  557. ->where('UniqId', (int)$req['UniqId']) // 使用实际的UniqId值
  558. ->delete();
  559. // 提交事务
  560. db()->commit();
  561. } catch (\Exception $e) {
  562. // 回滚事务
  563. db()->rollback();
  564. $this->error($e->getMessage());
  565. }
  566. if($bool===false) $this->error('失败');
  567. $this->success('成功');
  568. }
  569. /**
  570. * excel导出
  571. * @ApiMethod (POST)
  572. * @param string date
  573. * @param string sys_id
  574. * @param array fields
  575. */
  576. public function export(){
  577. if(!$this->request->isGet()){
  578. $this->error('请求方式错误');
  579. }
  580. $req = $this->request->param();
  581. if (!isset($req['date'])) $this->error('参数缺失');
  582. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  583. $this->error('参数错误','fields',100);
  584. }
  585. if(strpos($req['date'],'-')){
  586. $where = ['c.Sys_rq'=>['like',$req['date'].'%']];
  587. }else if(empty($req['date'])){
  588. $where = [];
  589. }else{
  590. $where = ['c.jjcp_gdbh'=>$req['date']];
  591. }
  592. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  593. $where['c.Sys_id'] = ['LIKE',$req['sys_id'].'%'];
  594. }
  595. $arr = [
  596. '入仓类型' =>['入仓类型'],
  597. '仓库编号' =>['仓库编号'],
  598. '仓库名称' =>['仓库名称'],
  599. 'jjcp_num' =>['入仓单号'],
  600. 'jjcp_gdbh' =>['工单编号'],
  601. 'jjcp_yjno' =>['印件号'],
  602. 'jjcp_sl' =>['入仓数量'],
  603. 'jjcp_sls' =>['累计入仓数量'],
  604. '订单数量' =>['订单数量'],
  605. '完成率' =>['完成率'],
  606. 'jjcp_dw' =>['单位'],
  607. 'jjcp_sj' =>['入仓时间'],
  608. 'jjcp_smb' =>['首末板'],
  609. 'jjcp_cpdh' =>['印件代号'],
  610. 'jjcp_cpmc' =>['印件名称'],
  611. '成品编码' =>['产品编号'],
  612. '成品名称' =>['产品名称'],
  613. '客户料号' =>['客户料号'],
  614. 'jjcp_desc' =>['备注'],
  615. '客户编号' =>['客户编号'],
  616. 'Gd_客户名称' =>['客户名称'],
  617. '最近入仓日期' =>['最近入仓日期'],
  618. '机型备注' =>['机型备注'],
  619. ];
  620. $data[0] = [];
  621. foreach ($req['fields'] as $k=>$v){
  622. if(array_key_exists($v,$arr)){
  623. array_push($data[0],$arr[$v][0]);
  624. }
  625. }
  626. $num = db('成品入仓')->alias('c')
  627. ->where($where)
  628. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  629. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  630. ->count();
  631. $rows = [];
  632. for($i=1;$i<=ceil($num/10000);$i++){
  633. $p = $i == ceil($num/10000) ? $num%10000 : 10000;
  634. $r = db('成品入仓')->alias('c')
  635. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  636. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  637. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  638. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  639. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  640. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  641. rtrim(c.机型备注) as 机型备注')
  642. ->where($where)
  643. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  644. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  645. ->order('c.UniqId desc')
  646. ->limit($i,$p)
  647. ->select();
  648. $rows = array_merge($rows,$r);
  649. }
  650. foreach ($rows as $key=>$value) {
  651. $row = db('成品入仓')
  652. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  653. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  654. ->order('Sys_rq desc')
  655. ->select();
  656. $subArray = [];
  657. foreach ($arr as $k=>$v){
  658. if($k=='jjcp_sls'){
  659. $subArray[$k] = 0;
  660. foreach ($row as $n) {
  661. $subArray[$k] += $n['jjcp_sl'];
  662. }
  663. }else if($k=='完成率'){
  664. if ($value['订单数量']!=0){
  665. $subArray[$k] = 0;
  666. foreach ($row as $n) {
  667. $subArray[$k] += $n['jjcp_sl'];
  668. }
  669. $subArray[$k] = number_format($subArray[$k]/$value['订单数量']/100,2);
  670. }else{
  671. $subArray[$k] = '';
  672. }
  673. }else if($k=='最近入仓日期'){
  674. $subArray[$k] = $row[0]['Sys_rq'];
  675. }else{
  676. $subArray[$k] = $rows[$key][$k];
  677. }
  678. }
  679. array_push($data,$subArray);
  680. }
  681. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  682. }
  683. /**
  684. * 月度A
  685. * @ApiMethod (POST)
  686. * @param string start_time
  687. * @param string end_time
  688. * @param string file_name
  689. * @param array fields
  690. */
  691. public function monthExportA(){
  692. if(!$this->request->isGet()){
  693. $this->error('请求方式错误');
  694. }
  695. $req = $this->request->param();
  696. $arr = [
  697. 'start_time', 'end_time', 'file_name'
  698. ];
  699. foreach ($arr as $key => $value){
  700. if (!isset($req[$value])){
  701. $this->error('参数错误',$value,$key+1);
  702. }
  703. }
  704. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  705. $this->error('参数错误','fields',100);
  706. }
  707. $arr = [
  708. '入仓类型' =>['入仓类型'],
  709. '仓库编号' =>['仓库编号'],
  710. '仓库名称' =>['仓库名称'],
  711. 'jjcp_gdbh' =>['工单编号'],
  712. 'jjcp_cpdh' =>['入仓存货编号'],
  713. 'jjcp_cpmc' =>['入仓存货名称'],
  714. 'jjcp_sl' =>['入仓数量'],
  715. 'jjcp_dw' =>['单位'],
  716. 'jjcp_sj' =>['入仓时间'],
  717. 'jjcp_smb' =>['首末板'],
  718. 'jjcp_num' =>['单据编号'],
  719. '订单编号' =>['订单编号'],
  720. ];
  721. $data[0] = [];
  722. foreach ($req['fields'] as $k=>$v){
  723. if(array_key_exists($v,$arr)){
  724. array_push($data[0],$arr[$v][0]);
  725. }
  726. }
  727. //客户编号
  728. $rows = db('成品入仓')->alias('c')
  729. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  730. c.jjcp_gdbh, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  731. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw, c.jjcp_sj,
  732. rtrim(c.jjcp_smb) as jjcp_smb,
  733. rtrim(c.jjcp_num) as jjcp_num, rtrim(c.订单编号) as 订单编号
  734. ')
  735. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  736. ->order('c.jjcp_sj asc')
  737. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  738. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  739. ->select();
  740. foreach ($rows as $key=>$value) {
  741. $subArray = [];
  742. foreach ($arr as $k=>$v){
  743. $subArray[$k] = $rows[$key][$k];
  744. }
  745. array_push($data,$subArray);
  746. }
  747. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  748. }
  749. /**
  750. * 月度B
  751. * @ApiMethod (POST)
  752. * @param string start_time
  753. * @param string end_time
  754. * @param string file_name
  755. * @param array fields
  756. */
  757. public function monthExportB(){
  758. if(!$this->request->isGet()){
  759. $this->error('请求方式错误');
  760. }
  761. $req = $this->request->param();
  762. $arr = [
  763. 'start_time', 'end_time', 'file_name'
  764. ];
  765. foreach ($arr as $key => $value){
  766. if (!isset($req[$value])){
  767. $this->error('参数错误',$value,$key+1);
  768. }
  769. }
  770. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  771. $this->error('参数错误','fields',100);
  772. }
  773. $arr = [
  774. '入仓类型' =>['入仓类型'],
  775. '客户编号' =>['客户编号'],
  776. '客户名称' =>['客户名称'],
  777. '产品类别' =>['产品类别'],
  778. 'jjcp_gdbh' =>['工单编号'],
  779. '成品编码' =>['成品编码'],
  780. '成品名称' =>['成品名称'],
  781. 'jjcp_sls' =>['入仓数量'],
  782. '大箱产量' =>['大箱产量'],
  783. '产品单价' =>['万张单价'],
  784. '产值万元' =>['产值万元'],
  785. ];
  786. $data[0] = [];
  787. foreach ($req['fields'] as $k=>$v){
  788. if(array_key_exists($v,$arr)){
  789. array_push($data[0],$arr[$v][0]);
  790. }
  791. }
  792. $rows1 = db('成品入仓')->alias('c')
  793. ->field('rtrim(c.入仓类型) as 入仓类型, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(b.客户名称) as 客户名称,
  794. rtrim(b.产品类别) as 产品类别, c.jjcp_gdbh,
  795. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称
  796. ')
  797. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  798. ->order('客户编号 asc,b.产品类别 desc,c.成品编码 asc,c.jjcp_gdbh asc')
  799. ->join('产品_基本资料 b','b.产品编号=c.jjcp_cpdh')
  800. ->group('c.jjcp_gdbh')
  801. ->select();
  802. $rows2 = db('成品入仓')
  803. ->field('rtrim(入仓类型) as 入仓类型, rtrim(成品编码) as 客户编号,
  804. rtrim(成品编码) as 客户名称, rtrim(成品编码) as 产品类别,
  805. jjcp_gdbh,rtrim(成品编码) as 成品编码, rtrim(成品名称) as 成品名称
  806. ')
  807. ->where('jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  808. ->where('成品编码','')
  809. ->order('jjcp_gdbh asc')
  810. ->group('jjcp_gdbh')
  811. ->select();
  812. $rows = array_merge($rows2,$rows1);
  813. $num = db('工单_基本资料')->count();
  814. $arr1 = db('工单_基本资料')->limit($num)
  815. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, 产品单价 as 万张单价');
  816. $brr = array_column($rows,'jjcp_gdbh');
  817. $brr = db('成品入仓')
  818. ->where('jjcp_gdbh', 'in', $brr)
  819. ->group('jjcp_gdbh')
  820. ->column('jjcp_gdbh, sum(jjcp_sl)');
  821. foreach ($rows as $key=>$value) {
  822. $subArray = [];
  823. foreach ($arr as $k=>$v){
  824. if($k=='jjcp_sls'){
  825. $subArray[$k] = $brr[$value['jjcp_gdbh']];
  826. }else if ($k=='大箱产量'){
  827. if($value['产品类别']=='条盒'){
  828. $subArray[$k] = $brr[$value['jjcp_gdbh']]/250;
  829. }else if($value['产品类别']=='小盒'){
  830. $subArray[$k] = $brr[$value['jjcp_gdbh']]/2500;
  831. }else{
  832. $subArray[$k] = 0;
  833. }
  834. }else if ($k=='产品单价'){
  835. if($value['产品类别']!=''){
  836. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  837. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']],4));
  838. }
  839. }else{
  840. $subArray[$k] = 0;
  841. }
  842. }else if ($k=='产值万元'){
  843. if($value['产品类别']!=''){
  844. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  845. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']]*$brr[$value['jjcp_gdbh']]/100000000,2));
  846. }
  847. }else{
  848. $subArray[$k] = 0;
  849. }
  850. }else{
  851. $subArray[$k] = $rows[$key][$k];
  852. }
  853. }
  854. array_push($data,$subArray);
  855. }
  856. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  857. }
  858. /**
  859. * 各日统计列表
  860. * @ApiMethod (GET)
  861. * @param string $date 时间
  862. * @param string $sys_id 用户
  863. */
  864. public function getDayList()
  865. {
  866. //get请求
  867. if(!$this->request->isGet()){
  868. $this->error('请求方式错误');
  869. }
  870. $req = $this->request->param();
  871. $page = 1;
  872. $limit = 15;
  873. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  874. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  875. $where = [
  876. 'c.jjcp_smb'=>['<>',''],
  877. ];
  878. if (isset($req['key_word']) && !empty($req['key_word'])){
  879. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  880. }
  881. //客户编号
  882. $rows = db('成品入仓')->alias('c')
  883. ->field('LEFT(c.jjcp_sj, 10) as jjcp_sj, c.jjcp_gdbh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  884. rtrim(c.jjcp_cpdh) as jjcp_cpdh, g.实际投料')
  885. ->where($where)
  886. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  887. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  888. ->order('c.jjcp_sj desc')
  889. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  890. ->page($page,$limit)
  891. ->select();
  892. $total = db('成品入仓')->alias('c')
  893. ->where($where)
  894. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  895. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  896. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  897. ->count();
  898. $brr = db('成品入仓')
  899. ->group('jjcp_gdbh,jjcp_cpdh')
  900. ->column('CONCAT(jjcp_gdbh, "-", rtrim(jjcp_cpdh)), sum(jjcp_sl)');
  901. foreach ($rows as $key=>$value) {
  902. $rows[$key]['实际投料'] = (string)floatval($value['实际投料']*10000);
  903. if(array_key_exists($value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'],$brr)){
  904. $rows[$key]['入仓数量'] = $brr[$value['jjcp_gdbh'].'-'.$value['jjcp_cpdh']];
  905. if($rows[$key]['实际投料']!=0){
  906. $rows[$key]['投入产出率'] = number_format($rows[$key]['入仓数量']/$rows[$key]['实际投料']*100,2);
  907. }else{
  908. $rows[$key]['投入产出率'] = '';
  909. }
  910. }else{
  911. $rows[$key]['入仓数量'] = '';
  912. $rows[$key]['投入产出率'] = '';
  913. }
  914. }
  915. $data = [
  916. 'total' => $total,
  917. 'rows' => $rows,
  918. ];
  919. $this->success('成功',$data);
  920. }
  921. /**
  922. * 各日统计列表excel导出
  923. * @ApiMethod (POST)
  924. * @param string file_name
  925. * @param array fields
  926. */
  927. public function dayExport()
  928. {
  929. //get请求
  930. if(!$this->request->isPost()){
  931. $this->error('请求方式错误');
  932. }
  933. $req = $this->request->param();
  934. if (!isset($req['file_name'])){
  935. $this->error('参数错误','file_name');
  936. }
  937. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  938. $this->error('参数错误','fields',100);
  939. }
  940. if (isset($req['key_word']) && !empty($req['key_word'])){
  941. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  942. }
  943. $arr = [
  944. 'jjcp_gdbh' =>['jjcp_gdbh'],
  945. 'jjcp_yjno' =>['jjcp_yjno'],
  946. 'jjcp_cpmc' =>['印件名称'],
  947. '成品编码' =>['成品编码'],
  948. '成品名称' =>['成品名称'],
  949. '实际投料' =>['实际投料'],
  950. 'jjcp_sls' =>['入仓数量'],
  951. 'jjcp_sj' =>['完工日期'],
  952. ];
  953. $data[0] = [];
  954. foreach ($req['fields'] as $k=>$v){
  955. if(array_key_exists($v,$arr)){
  956. array_push($data[0],$arr[$v][0]);
  957. }
  958. }
  959. $where = [
  960. 'c.jjcp_smb'=>['<>','']
  961. ];
  962. //客户编号
  963. $rows = db('成品入仓')->alias('c')
  964. ->field('c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.jjcp_cpmc) as jjcp_cpmc, rtrim(c.jjcp_cpdh) as jjcp_cpdh,
  965. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称, sum(jjcp_sl) as jjcp_sls,
  966. g.实际投料, LEFT(c.jjcp_sj, 10) as jjcp_sj
  967. ')
  968. ->where($where)
  969. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  970. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  971. ->order('c.jjcp_sj desc')
  972. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  973. ->select();
  974. foreach ($rows as $key=>$value) {
  975. $subArray = [];
  976. foreach ($arr as $k=>$v){
  977. if($k=='实际投料'){
  978. $subArray[$k] = (string)floatval($value['实际投料']*10000);
  979. }else{
  980. $subArray[$k] = $rows[$key][$k];
  981. }
  982. }
  983. array_push($data,$subArray);
  984. }
  985. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  986. }
  987. /**
  988. * 入仓末板数据统计列表
  989. * @ApiMethod (GET)
  990. * @param string $date 时间
  991. * @param string $sys_id 用户
  992. */
  993. public function mbList()
  994. {
  995. //get请求
  996. if(!$this->request->isGet()){
  997. $this->error('请求方式错误');
  998. }
  999. $req = $this->request->param();
  1000. // $page = 1;
  1001. // $limit = 15;
  1002. // if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  1003. // if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  1004. $date = date('Y-m-d',strtotime('-3 month'));
  1005. $arr1 = db('db_包装产量预报')
  1006. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1007. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1008. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1009. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1010. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1011. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1012. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1013. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1014. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1015. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1016. ')
  1017. ->where('sczl_rq','>=',$date)
  1018. ->select();
  1019. $data1 = [];
  1020. foreach ($arr1 as $k=>$v){
  1021. for ($i=1;$i<=6;$i++){
  1022. if (!empty($v['sczl_gdbh'.$i])){
  1023. $v['sczl_gdbh'.$i] = strtoupper($v['sczl_gdbh'.$i]);
  1024. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1025. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1026. }else{
  1027. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1028. }
  1029. }
  1030. }
  1031. }
  1032. krsort($data1);
  1033. //成品入仓中jjcp_smb不为空的工单
  1034. $rows2 = db('成品入仓')
  1035. ->where('jjcp_sj','>=',$date)
  1036. ->group('jjcp_gdbh, jjcp_cpdh')
  1037. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, max(jjcp_smb) as jjcp_smb,
  1038. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj,UniqId");
  1039. $data = [];
  1040. foreach($data1 as $key=>$value){
  1041. $subArray = [];
  1042. $gdbh = strtoupper(explode('-',$key)[0]);
  1043. $yjno = explode('-',$key)[1];
  1044. $res = db('工单_印件资料')
  1045. ->where('Yj_Gdbh',$gdbh)
  1046. ->where('yj_Yjno',$yjno)
  1047. ->field('Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, (yj_平张投料 * yj_ks * yj_ls) as 投料')
  1048. ->find();
  1049. if(!empty($res)){
  1050. $subArray['Yj_Gdbh'] = $res['Yj_Gdbh'];
  1051. $subArray['yj_Yjno'] = $res['yj_Yjno'];
  1052. $subArray['yj_yjmc'] = trim($res['yj_yjmc']);
  1053. $subArray['yj_成品数量'] = (string)floatval($res['yj_成品数量']*10000);
  1054. $subArray['yj_实际投料'] = (string)floatval($res['投料']);
  1055. }else{
  1056. $subArray['Yj_Gdbh'] = '';
  1057. $subArray['yj_Yjno'] = '';
  1058. $subArray['yj_yjmc'] = '';
  1059. $subArray['yj_成品数量'] = '';
  1060. $subArray['yj_实际投料'] = '';
  1061. }
  1062. if(array_key_exists($key,$rows2)){
  1063. $subArray['jjcp_smb'] = $rows2[$key]['jjcp_smb'];
  1064. $subArray['jjcp_sls'] = $rows2[$key]['jjcp_sls'];
  1065. $subArray['jjcp_sj'] = $rows2[$key]['jjcp_sj'];
  1066. }else{
  1067. $subArray['jjcp_smb'] = '';
  1068. $subArray['jjcp_sls'] = '';
  1069. $subArray['jjcp_sj'] = '';
  1070. }
  1071. $subArray['sczl_cls'] = (string)floatval($value);
  1072. array_push($data,$subArray);
  1073. }
  1074. $data = [
  1075. 'total' => count($data),
  1076. 'rows' => $data,
  1077. ];
  1078. $this->success('成功',$data);
  1079. }
  1080. /**
  1081. * 入仓末板数据统计列表excel导出
  1082. * @ApiMethod (POST)
  1083. * @param string file_name
  1084. * @param array fields
  1085. */
  1086. public function mbExport()
  1087. {
  1088. //get请求
  1089. if(!$this->request->isPost()){
  1090. $this->error('请求方式错误');
  1091. }
  1092. $req = $this->request->param();
  1093. if (!isset($req['file_name'])){
  1094. $this->error('参数错误','file_name');
  1095. }
  1096. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  1097. $this->error('参数错误','fields',100);
  1098. }
  1099. $arr = [
  1100. 'Yj_Gdbh' =>['工单编号'],
  1101. 'yj_Yjno' =>['印件号'],
  1102. 'yj_yjmc' =>['印件名称'],
  1103. 'yj_成品数量' =>['计划交货数量'],
  1104. 'yj_实际投料' =>['印件投料'],
  1105. 'sczl_cls' =>['包装产量'],
  1106. 'jjcp_sj' =>['最近入仓时间'],
  1107. 'jjcp_sls' =>['入仓产量'],
  1108. 'status' =>['完工'],
  1109. '缺数' =>['缺数'],
  1110. ];
  1111. $data[0] = [];
  1112. foreach ($req['fields'] as $k=>$v){
  1113. if(array_key_exists($v,$arr)){
  1114. array_push($data[0],$arr[$v][0]);
  1115. }
  1116. }
  1117. $date = date('Y-m-d',strtotime('-3 month'));
  1118. $arr1 = db('db_包装产量预报')
  1119. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1120. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1121. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1122. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1123. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1124. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1125. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1126. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1127. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1128. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1129. ')
  1130. ->where('sczl_rq','>=',$date)
  1131. ->select();
  1132. $data1 = [];
  1133. foreach ($arr1 as $k=>$v){
  1134. for ($i=1;$i<=6;$i++){
  1135. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1136. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1137. }else{
  1138. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1139. }
  1140. }
  1141. }
  1142. krsort($data1);
  1143. //成品入仓中jjcp_smb不为空的工单
  1144. $rows1 = db('成品入仓')
  1145. ->where('jjcp_smb','<>','')
  1146. ->order('jjcp_gdbh desc')
  1147. ->column("CONCAT(jjcp_gdbh, '-', jjcp_yjno) AS gdyj, UniqId");
  1148. $rows2 = db('成品入仓')
  1149. ->where('jjcp_sj','>=',$date)
  1150. ->group('jjcp_gdbh, jjcp_cpdh')
  1151. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, jjcp_smb,
  1152. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj");
  1153. $rows3 = db('工单_印件资料')->cache(true,86400)
  1154. ->group('Yj_Gdbh, yj_Yjno')
  1155. ->column("CONCAT( Yj_Gdbh, '-', yj_Yjno) AS gdyj,
  1156. Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, yj_实际投料");
  1157. foreach($data1 as $key=>$value){
  1158. if(!array_key_exists($key,$rows1)){
  1159. $subArray = [];
  1160. foreach ($arr as $k=>$v){
  1161. if($k=='yj_成品数量' || $k=='yj_实际投料'){
  1162. if(array_key_exists($key,$rows3)){
  1163. $subArray[$k] = (string)floatval($rows3[$key][$k]*10000);
  1164. }else{
  1165. $subArray[$k] = '';
  1166. }
  1167. }else if($k=='Yj_Gdbh' || $k=='yj_Yjno'){
  1168. if(array_key_exists($key,$rows3)){
  1169. $subArray[$k] = $rows3[$key][$k];
  1170. }else{
  1171. $subArray[$k] = '';
  1172. }
  1173. }else if($k=='yj_yjmc'){
  1174. if(array_key_exists($key,$rows3)){
  1175. $subArray[$k] = trim($rows3[$key][$k]);
  1176. }else{
  1177. $subArray[$k] = '';
  1178. }
  1179. }else if($k=='sczl_cls'){
  1180. $subArray[$k] = (string)floatval($value);
  1181. }else if($k=='status'){
  1182. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1183. $subArray[$k] = '完工';
  1184. }else{
  1185. $subArray[$k] = '';
  1186. }
  1187. }else if($k=='缺数'){
  1188. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1189. $subArray[$k]['缺数'] = (int)$rows3[$key]['yj_成品数量']-(int)$value;
  1190. }else{
  1191. $subArray[$k] = '';
  1192. }
  1193. }else{
  1194. if(array_key_exists($key,$rows2)){
  1195. $subArray[$k] = $rows2[$key][$k];
  1196. $subArray[$k] = $rows2[$key][$k];
  1197. $subArray[$k] = $rows2[$key][$k];
  1198. }else{
  1199. $subArray[$k] = '';
  1200. $subArray[$k] = '';
  1201. $subArray[$k] = '';
  1202. }
  1203. }
  1204. }
  1205. array_push($data,$subArray);
  1206. }
  1207. }
  1208. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1209. }
  1210. /**
  1211. * 构建新增入仓数据
  1212. * @param array $req
  1213. * @return array
  1214. */
  1215. protected function buildAddData(array $req)
  1216. {
  1217. $fields = [
  1218. 'Sys_id',
  1219. '入仓类型', '仓库编号', '仓库名称',
  1220. 'jjcp_num',
  1221. 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
  1222. 'jjcp_cpdh', 'jjcp_cpmc',
  1223. '成品编码', '成品名称',
  1224. '客户料号',
  1225. 'jjcp_smb', 'jjcp_sj',
  1226. 'jjcp_sl', 'jjcp_dw',
  1227. '机型备注',
  1228. 'jjcp_desc',
  1229. ];
  1230. $data = [];
  1231. foreach ($fields as $index => $field) {
  1232. if (!isset($req[$field])) {
  1233. $this->error('参数错误', $field, $index + 1);
  1234. }
  1235. $data[$field] = $req[$field];
  1236. }
  1237. $data['Sys_rq'] = date('Y-m-d H:i:s');
  1238. return $data;
  1239. }
  1240. /**
  1241. * 校验印件产品名称
  1242. * @param string $gdbh
  1243. * @param string $yjno
  1244. * @param string $cpmc
  1245. */
  1246. protected function validateProductName($gdbh, $yjno, $cpmc)
  1247. {
  1248. $name = db('工单_印件资料')
  1249. ->where('Yj_Gdbh', $gdbh)
  1250. ->where('yj_Yjno', $yjno)
  1251. ->value('yj_yjmc');
  1252. if ($name !== $cpmc) {
  1253. $this->error('产品名称错误');
  1254. }
  1255. }
  1256. /**
  1257. * 查询工单印件是否已存在末版记录
  1258. * @param string $gdbh
  1259. * @param string $yjno
  1260. * @return array|null
  1261. */
  1262. protected function findExistingEndBoard($gdbh, $yjno)
  1263. {
  1264. return db('成品入仓')
  1265. ->where('jjcp_gdbh', $gdbh)
  1266. ->where('jjcp_yjno', $yjno)
  1267. ->where('jjcp_smb', '<>', '')
  1268. ->find();
  1269. }
  1270. /**
  1271. * 校验工单印件是否已完全领料
  1272. * @param string $gdbh
  1273. * @param string $yjno
  1274. */
  1275. protected function validateMaterialReceived($gdbh, $yjno)
  1276. {
  1277. $materialRecord = db('工单_印件资料')
  1278. ->alias('a')
  1279. ->join('物料_收发记录 b', 'a.Yj_Gdbh=b.st_gdbh and a.yj_cpdh=b.cpdh')
  1280. ->where('a.Yj_Gdbh', $gdbh)
  1281. ->where('a.yj_Yjno', $yjno)
  1282. ->group('b.st_wlbh')
  1283. ->order('b.st_wlbh asc')
  1284. ->column('b.st_wlbh');
  1285. $bomRecord = db('工单_印件资料')
  1286. ->alias('a')
  1287. ->join('工单_bom资料 b', 'a.Yj_Gdbh=b.BOM_工单编号 and a.yj_Yjno=b.BOM_工单行号 and a.yj_cpdh=b.BOM_产品编号')
  1288. ->where('a.Yj_Gdbh', $gdbh)
  1289. ->where('a.yj_Yjno', $yjno)
  1290. ->where('b.BOM_实际用量', '<>', 0)
  1291. ->group('b.BOM_物料编码')
  1292. ->order('b.BOM_物料编码 asc')
  1293. ->column('b.BOM_物料编码');
  1294. foreach ($bomRecord as $materialCode) {
  1295. if (!in_array($materialCode, $materialRecord)) {
  1296. $this->error('工单' . $gdbh . '印件' . $yjno . '物料' . $materialCode . '未领料');
  1297. }
  1298. }
  1299. }
  1300. /**
  1301. * 生成成品入仓 UniqId
  1302. * @return int
  1303. */
  1304. protected function generateFinishStockUniqId()
  1305. {
  1306. $uniqId = db('成品入仓')->max('UniqId');
  1307. return $uniqId < 10000000 ? 10000000 : $uniqId + 1;
  1308. }
  1309. /**
  1310. * 保存成品入仓数据到 MySQL 和 MongoDB
  1311. * @param array $data
  1312. * @param mixed $mongodb
  1313. */
  1314. protected function saveFinishStock(array $data, $mongodb)
  1315. {
  1316. db()->startTrans();
  1317. try {
  1318. $sql = db('成品入仓')->fetchSql(true)->insert($data);
  1319. $inserted = db()->query($sql);
  1320. if ($inserted === false) {
  1321. throw new \Exception('MySQL 保存失败');
  1322. }
  1323. $mongodb->name('finished_products')->insert($data);
  1324. db()->commit();
  1325. } catch (\Exception $e) {
  1326. db()->rollback();
  1327. $this->error($e->getMessage());
  1328. }
  1329. }
  1330. /**
  1331. * 判断是否为末版
  1332. * @param string $smb
  1333. * @return bool
  1334. */
  1335. protected function isEndBoard($smb)
  1336. {
  1337. return strpos((string)$smb, '末') !== false;
  1338. }
  1339. /**
  1340. * 末版入仓后进入超节损
  1341. * @param string $gdbh
  1342. * @param string $yjno
  1343. */
  1344. protected function handleEndBoardSuperLoss($gdbh, $yjno)
  1345. {
  1346. $orderLoss = new OrderSuperLoss();
  1347. $result = [];
  1348. try {
  1349. $result = $orderLoss->OneOrderSuperLoss($gdbh, $yjno);
  1350. } catch (DataNotFoundException $e) {
  1351. } catch (ModelNotFoundException $e) {
  1352. } catch (DbException $e) {
  1353. }
  1354. if (empty($result)) {
  1355. return;
  1356. }
  1357. db('工单_质量考核汇总')
  1358. ->where('Gy0_gdbh', $gdbh)
  1359. ->where('印件及工序', $yjno)
  1360. ->delete();
  1361. $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
  1362. $inserted = db()->query($sql);
  1363. if ($inserted === false) {
  1364. $this->error('超节损数据保存失败');
  1365. }
  1366. }
  1367. }