FinishedProductWarehousing.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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. $warnings = [];
  413. // 1. 末板超节损(与同步独立,失败不阻断后续)
  414. if ($this->isEndBoard($data['jjcp_smb'])) {
  415. try {
  416. $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
  417. } catch (\Exception $e) {
  418. $warnings[] = '超节损计算失败:' . $e->getMessage();
  419. }
  420. }
  421. // 2. 中间表同步(与超节损独立;失败/未同步均写入日志 status_msg)
  422. $syncMsg = $this->syncFinishStockOrLogSkip($data, $data['UniqId'], '新增');
  423. if ($syncMsg !== null) {
  424. $warnings[] = $syncMsg;
  425. }
  426. $result = ['UniqId' => $data['UniqId']];
  427. if (!empty($warnings)) {
  428. $result['warnings'] = $warnings;
  429. }
  430. $this->success(empty($warnings) ? '成功' : '成品入仓已保存,但部分后续处理失败', $result);
  431. }
  432. /**
  433. * 查询成品入仓同步操作日志
  434. * @ApiMethod (GET)
  435. * @param int|string $UniqId 成品入仓 UniqId
  436. */
  437. public function getSyncLog()
  438. {
  439. if (!$this->request->isGet()) {
  440. $this->error('请求方式错误');
  441. }
  442. $uniqId = $this->request->param('UniqId');
  443. if ($uniqId === null || $uniqId === '') {
  444. $this->error('参数错误', 'UniqId');
  445. }
  446. $list = db('工单_成品入仓同步操作日志')
  447. ->where('UniqId', (int)$uniqId)
  448. ->field('id,Gdbh,wlbm,yjmc,Yjdh,Yjno,sys_id,sys_rq,type,status,status_msg,UniqId')
  449. ->order('id desc')
  450. ->select();
  451. $this->success('成功', $list ?: []);
  452. }
  453. /**
  454. * 修改
  455. * 前端传:UniqId、数量(jjcp_sl)、smb(jjcp_smb)、sys_id(Sys_id)、时间(jjcp_sj)
  456. * @ApiMethod (POST)
  457. */
  458. public function edit()
  459. {
  460. if (!$this->request->isPost()) {
  461. $this->error('请求方式错误');
  462. }
  463. $req = $this->request->param();
  464. $uniqId = $req['UniqId'] ?? ($req['UniaId'] ?? null);
  465. if ($uniqId === null || trim((string)$uniqId) === '') {
  466. $this->error('参数错误', 'UniqId', 100);
  467. }
  468. $uniqId = (int)$uniqId;
  469. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  470. if (empty($old)) {
  471. $this->error('未找到成品入仓记录');
  472. }
  473. $hasQty = array_key_exists('jjcp_sl', $req) || array_key_exists('数量', $req);
  474. $hasSmb = array_key_exists('jjcp_smb', $req) || array_key_exists('smb', $req);
  475. $hasSj = array_key_exists('jjcp_sj', $req);
  476. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  477. if (!$hasQty && !$hasSmb && !$hasSj && ($sysId === null || $sysId === '')) {
  478. $this->error('参数错误', '', 111);
  479. }
  480. $data = ['Mod_rq' => date('Y-m-d H:i:s')];
  481. if ($sysId !== null && $sysId !== '') {
  482. $data['Sys_id'] = $sysId;
  483. }
  484. if ($hasSj) {
  485. $data['jjcp_sj'] = $req['jjcp_sj'];
  486. }
  487. $oldQty = (float)$old['jjcp_sl'];
  488. $newQty = $oldQty;
  489. $qtyChanged = false;
  490. if ($hasQty) {
  491. $newQty = (float)($req['jjcp_sl'] ?? $req['数量']);
  492. $data['jjcp_sl'] = $newQty;
  493. $qtyChanged = abs($newQty - $oldQty) > 0.00001;
  494. }
  495. $oldSmb = (string)$old['jjcp_smb'];
  496. $newSmb = $oldSmb;
  497. $smbChanged = false;
  498. if ($hasSmb) {
  499. $newSmb = (string)($req['jjcp_smb'] ?? $req['smb']);
  500. $data['jjcp_smb'] = $newSmb;
  501. $smbChanged = trim($newSmb) !== trim($oldSmb);
  502. }
  503. $sjChanged = false;
  504. if ($hasSj) {
  505. $sjChanged = trim((string)$req['jjcp_sj']) !== trim((string)$old['jjcp_sj']);
  506. }
  507. $sysIdChanged = false;
  508. if ($sysId !== null && $sysId !== '') {
  509. $oldSys = trim((string)($old['Sys_id'] ?? ''));
  510. $sysIdChanged = trim((string)$sysId) !== $oldSys;
  511. }
  512. $needsMiddleSync = $hasQty || $hasSmb || $hasSj || $sysIdChanged;
  513. // 改为末板时,校验同工单同印件是否已有其他末板记录
  514. if ($smbChanged && $this->isEndBoard($newSmb)) {
  515. $existingEndBoard = $this->findExistingEndBoard($old['jjcp_gdbh'], $old['jjcp_yjno']);
  516. if ($existingEndBoard && (int)$existingEndBoard['UniqId'] !== $uniqId) {
  517. $this->error(
  518. $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
  519. );
  520. }
  521. }
  522. $mongodb = Db::connect(config('database.mongodb'));
  523. db()->startTrans();
  524. try {
  525. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->update($data);
  526. $bool = db()->query($sql);
  527. $mongodb->name('finished_products')
  528. ->where('UniqId', $uniqId)
  529. ->update($data);
  530. db()->commit();
  531. } catch (\Exception $e) {
  532. db()->rollback();
  533. $this->error($e->getMessage());
  534. }
  535. if ($bool === false) {
  536. $this->error('失败');
  537. }
  538. $warnings = [];
  539. // 1. 改为末板时先做超节损(与同步独立)
  540. if ($smbChanged && $this->isEndBoard($newSmb)) {
  541. try {
  542. $this->handleEndBoardSuperLoss($old['jjcp_gdbh'], $old['jjcp_yjno']);
  543. } catch (\Exception $e) {
  544. $warnings[] = '超节损计算失败:' . $e->getMessage();
  545. }
  546. }
  547. // 2. 同步中间表:先「新增」负数量冲销原数据,再「新增」写入修改后数据(不用「修改」类型)
  548. if ($needsMiddleSync) {
  549. $syncMsgs = $this->syncFinishStockAfterEditOrLogSkip($old, $uniqId, $sysId);
  550. if (!empty($syncMsgs)) {
  551. $warnings = array_merge($warnings, $syncMsgs);
  552. }
  553. }
  554. if (!empty($warnings)) {
  555. $this->success('成品入仓已保存,但部分后续处理失败', [
  556. 'UniqId' => $uniqId,
  557. 'warnings' => $warnings,
  558. ]);
  559. }
  560. $this->success('成功');
  561. }
  562. /**
  563. * 删除
  564. * 前端传:UniqId、sys_id(Sys_id)
  565. * @ApiMethod (POST)
  566. */
  567. public function del()
  568. {
  569. if (!$this->request->isPost()) {
  570. $this->error('请求方式错误');
  571. }
  572. $req = $this->request->param();
  573. if (!(isset($req['UniqId']) && trim($req['UniqId']) != '')) {
  574. $this->error('参数错误', 'UniqId', 100);
  575. }
  576. $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
  577. if ($sysId === null || trim((string)$sysId) === '') {
  578. $this->error('参数错误', 'sys_id', 101);
  579. }
  580. $uniqId = (int)$req['UniqId'];
  581. $old = db('成品入仓')->where('UniqId', $uniqId)->find();
  582. if (empty($old)) {
  583. $this->error('未找到成品入仓记录');
  584. }
  585. // 满足同步条件才回冲;不满足或失败都写日志。同步失败不再阻断删除
  586. $syncMsg = $this->syncFinishStockOrLogSkip(
  587. $old,
  588. $uniqId,
  589. '新增',
  590. -1 * (float)$old['jjcp_sl'],
  591. $sysId
  592. );
  593. $mongodb = Db::connect(config('database.mongodb'));
  594. db()->startTrans();
  595. try {
  596. $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->delete();
  597. $bool = db()->query($sql);
  598. $mongodb->name('finished_products')
  599. ->where('UniqId', $uniqId)
  600. ->delete();
  601. db()->commit();
  602. } catch (\Exception $e) {
  603. db()->rollback();
  604. $this->error($e->getMessage());
  605. }
  606. if ($bool === false) {
  607. $this->error('失败');
  608. }
  609. if ($syncMsg !== null) {
  610. $this->success('删除成功,但同步处理异常', [
  611. 'UniqId' => $uniqId,
  612. 'warnings' => [$syncMsg],
  613. ]);
  614. }
  615. $this->success('成功');
  616. }
  617. /**
  618. * excel导出
  619. * @ApiMethod (POST)
  620. * @param string date
  621. * @param string sys_id
  622. * @param array fields
  623. */
  624. public function export(){
  625. if(!$this->request->isGet()){
  626. $this->error('请求方式错误');
  627. }
  628. $req = $this->request->param();
  629. if (!isset($req['date'])) $this->error('参数缺失');
  630. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  631. $this->error('参数错误','fields',100);
  632. }
  633. if(strpos($req['date'],'-')){
  634. $where = ['c.Sys_rq'=>['like',$req['date'].'%']];
  635. }else if(empty($req['date'])){
  636. $where = [];
  637. }else{
  638. $where = ['c.jjcp_gdbh'=>$req['date']];
  639. }
  640. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  641. $where['c.Sys_id'] = ['LIKE',$req['sys_id'].'%'];
  642. }
  643. $arr = [
  644. '入仓类型' =>['入仓类型'],
  645. '仓库编号' =>['仓库编号'],
  646. '仓库名称' =>['仓库名称'],
  647. 'jjcp_num' =>['入仓单号'],
  648. 'jjcp_gdbh' =>['工单编号'],
  649. 'jjcp_yjno' =>['印件号'],
  650. 'jjcp_sl' =>['入仓数量'],
  651. 'jjcp_sls' =>['累计入仓数量'],
  652. '订单数量' =>['订单数量'],
  653. '完成率' =>['完成率'],
  654. 'jjcp_dw' =>['单位'],
  655. 'jjcp_sj' =>['入仓时间'],
  656. 'jjcp_smb' =>['首末板'],
  657. 'jjcp_cpdh' =>['印件代号'],
  658. 'jjcp_cpmc' =>['印件名称'],
  659. '成品编码' =>['产品编号'],
  660. '成品名称' =>['产品名称'],
  661. '客户料号' =>['客户料号'],
  662. 'jjcp_desc' =>['备注'],
  663. '客户编号' =>['客户编号'],
  664. 'Gd_客户名称' =>['客户名称'],
  665. '最近入仓日期' =>['最近入仓日期'],
  666. '机型备注' =>['机型备注'],
  667. ];
  668. $data[0] = [];
  669. foreach ($req['fields'] as $k=>$v){
  670. if(array_key_exists($v,$arr)){
  671. array_push($data[0],$arr[$v][0]);
  672. }
  673. }
  674. $num = db('成品入仓')->alias('c')
  675. ->where($where)
  676. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  677. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  678. ->count();
  679. $rows = [];
  680. for($i=1;$i<=ceil($num/10000);$i++){
  681. $p = $i == ceil($num/10000) ? $num%10000 : 10000;
  682. $r = db('成品入仓')->alias('c')
  683. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  684. rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
  685. c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
  686. rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  687. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
  688. rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
  689. rtrim(c.机型备注) as 机型备注')
  690. ->where($where)
  691. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  692. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  693. ->order('c.UniqId desc')
  694. ->limit($i,$p)
  695. ->select();
  696. $rows = array_merge($rows,$r);
  697. }
  698. foreach ($rows as $key=>$value) {
  699. $row = db('成品入仓')
  700. ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
  701. ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
  702. ->order('Sys_rq desc')
  703. ->select();
  704. $subArray = [];
  705. foreach ($arr as $k=>$v){
  706. if($k=='jjcp_sls'){
  707. $subArray[$k] = 0;
  708. foreach ($row as $n) {
  709. $subArray[$k] += $n['jjcp_sl'];
  710. }
  711. }else if($k=='完成率'){
  712. if ($value['订单数量']!=0){
  713. $subArray[$k] = 0;
  714. foreach ($row as $n) {
  715. $subArray[$k] += $n['jjcp_sl'];
  716. }
  717. $subArray[$k] = number_format($subArray[$k]/$value['订单数量']/100,2);
  718. }else{
  719. $subArray[$k] = '';
  720. }
  721. }else if($k=='最近入仓日期'){
  722. $subArray[$k] = $row[0]['Sys_rq'];
  723. }else{
  724. $subArray[$k] = $rows[$key][$k];
  725. }
  726. }
  727. array_push($data,$subArray);
  728. }
  729. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  730. }
  731. /**
  732. * 月度A
  733. * @ApiMethod (POST)
  734. * @param string start_time
  735. * @param string end_time
  736. * @param string file_name
  737. * @param array fields
  738. */
  739. public function monthExportA(){
  740. if(!$this->request->isGet()){
  741. $this->error('请求方式错误');
  742. }
  743. $req = $this->request->param();
  744. $arr = [
  745. 'start_time', 'end_time', 'file_name'
  746. ];
  747. foreach ($arr as $key => $value){
  748. if (!isset($req[$value])){
  749. $this->error('参数错误',$value,$key+1);
  750. }
  751. }
  752. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  753. $this->error('参数错误','fields',100);
  754. }
  755. $arr = [
  756. '入仓类型' =>['入仓类型'],
  757. '仓库编号' =>['仓库编号'],
  758. '仓库名称' =>['仓库名称'],
  759. 'jjcp_gdbh' =>['工单编号'],
  760. 'jjcp_cpdh' =>['入仓存货编号'],
  761. 'jjcp_cpmc' =>['入仓存货名称'],
  762. 'jjcp_sl' =>['入仓数量'],
  763. 'jjcp_dw' =>['单位'],
  764. 'jjcp_sj' =>['入仓时间'],
  765. 'jjcp_smb' =>['首末板'],
  766. 'jjcp_num' =>['单据编号'],
  767. '订单编号' =>['订单编号'],
  768. ];
  769. $data[0] = [];
  770. foreach ($req['fields'] as $k=>$v){
  771. if(array_key_exists($v,$arr)){
  772. array_push($data[0],$arr[$v][0]);
  773. }
  774. }
  775. //客户编号
  776. $rows = db('成品入仓')->alias('c')
  777. ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
  778. c.jjcp_gdbh, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  779. c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw, c.jjcp_sj,
  780. rtrim(c.jjcp_smb) as jjcp_smb,
  781. rtrim(c.jjcp_num) as jjcp_num, rtrim(c.订单编号) as 订单编号
  782. ')
  783. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  784. ->order('c.jjcp_sj asc')
  785. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  786. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  787. ->select();
  788. foreach ($rows as $key=>$value) {
  789. $subArray = [];
  790. foreach ($arr as $k=>$v){
  791. $subArray[$k] = $rows[$key][$k];
  792. }
  793. array_push($data,$subArray);
  794. }
  795. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  796. }
  797. /**
  798. * 月度B
  799. * @ApiMethod (POST)
  800. * @param string start_time
  801. * @param string end_time
  802. * @param string file_name
  803. * @param array fields
  804. */
  805. public function monthExportB(){
  806. if(!$this->request->isGet()){
  807. $this->error('请求方式错误');
  808. }
  809. $req = $this->request->param();
  810. $arr = [
  811. 'start_time', 'end_time', 'file_name'
  812. ];
  813. foreach ($arr as $key => $value){
  814. if (!isset($req[$value])){
  815. $this->error('参数错误',$value,$key+1);
  816. }
  817. }
  818. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  819. $this->error('参数错误','fields',100);
  820. }
  821. $arr = [
  822. '入仓类型' =>['入仓类型'],
  823. '客户编号' =>['客户编号'],
  824. '客户名称' =>['客户名称'],
  825. '产品类别' =>['产品类别'],
  826. 'jjcp_gdbh' =>['工单编号'],
  827. '成品编码' =>['成品编码'],
  828. '成品名称' =>['成品名称'],
  829. 'jjcp_sls' =>['入仓数量'],
  830. '大箱产量' =>['大箱产量'],
  831. '产品单价' =>['万张单价'],
  832. '产值万元' =>['产值万元'],
  833. ];
  834. $data[0] = [];
  835. foreach ($req['fields'] as $k=>$v){
  836. if(array_key_exists($v,$arr)){
  837. array_push($data[0],$arr[$v][0]);
  838. }
  839. }
  840. $rows1 = db('成品入仓')->alias('c')
  841. ->field('rtrim(c.入仓类型) as 入仓类型, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(b.客户名称) as 客户名称,
  842. rtrim(b.产品类别) as 产品类别, c.jjcp_gdbh,
  843. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称
  844. ')
  845. ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  846. ->order('客户编号 asc,b.产品类别 desc,c.成品编码 asc,c.jjcp_gdbh asc')
  847. ->join('产品_基本资料 b','b.产品编号=c.jjcp_cpdh')
  848. ->group('c.jjcp_gdbh')
  849. ->select();
  850. $rows2 = db('成品入仓')
  851. ->field('rtrim(入仓类型) as 入仓类型, rtrim(成品编码) as 客户编号,
  852. rtrim(成品编码) as 客户名称, rtrim(成品编码) as 产品类别,
  853. jjcp_gdbh,rtrim(成品编码) as 成品编码, rtrim(成品名称) as 成品名称
  854. ')
  855. ->where('jjcp_sj','between time',[$req['start_time'], $req['end_time']])
  856. ->where('成品编码','')
  857. ->order('jjcp_gdbh asc')
  858. ->group('jjcp_gdbh')
  859. ->select();
  860. $rows = array_merge($rows2,$rows1);
  861. $num = db('工单_基本资料')->count();
  862. $arr1 = db('工单_基本资料')->limit($num)
  863. ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, 产品单价 as 万张单价');
  864. $brr = array_column($rows,'jjcp_gdbh');
  865. $brr = db('成品入仓')
  866. ->where('jjcp_gdbh', 'in', $brr)
  867. ->group('jjcp_gdbh')
  868. ->column('jjcp_gdbh, sum(jjcp_sl)');
  869. foreach ($rows as $key=>$value) {
  870. $subArray = [];
  871. foreach ($arr as $k=>$v){
  872. if($k=='jjcp_sls'){
  873. $subArray[$k] = $brr[$value['jjcp_gdbh']];
  874. }else if ($k=='大箱产量'){
  875. if($value['产品类别']=='条盒'){
  876. $subArray[$k] = $brr[$value['jjcp_gdbh']]/250;
  877. }else if($value['产品类别']=='小盒'){
  878. $subArray[$k] = $brr[$value['jjcp_gdbh']]/2500;
  879. }else{
  880. $subArray[$k] = 0;
  881. }
  882. }else if ($k=='产品单价'){
  883. if($value['产品类别']!=''){
  884. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  885. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']],4));
  886. }
  887. }else{
  888. $subArray[$k] = 0;
  889. }
  890. }else if ($k=='产值万元'){
  891. if($value['产品类别']!=''){
  892. if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
  893. $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']]*$brr[$value['jjcp_gdbh']]/100000000,2));
  894. }
  895. }else{
  896. $subArray[$k] = 0;
  897. }
  898. }else{
  899. $subArray[$k] = $rows[$key][$k];
  900. }
  901. }
  902. array_push($data,$subArray);
  903. }
  904. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  905. }
  906. /**
  907. * 各日统计列表
  908. * @ApiMethod (GET)
  909. * @param string $date 时间
  910. * @param string $sys_id 用户
  911. */
  912. public function getDayList()
  913. {
  914. //get请求
  915. if(!$this->request->isGet()){
  916. $this->error('请求方式错误');
  917. }
  918. $req = $this->request->param();
  919. $page = 1;
  920. $limit = 15;
  921. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  922. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  923. $where = [
  924. 'c.jjcp_smb'=>['<>',''],
  925. ];
  926. if (isset($req['key_word']) && !empty($req['key_word'])){
  927. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  928. }
  929. //客户编号
  930. $rows = db('成品入仓')->alias('c')
  931. ->field('LEFT(c.jjcp_sj, 10) as jjcp_sj, c.jjcp_gdbh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
  932. rtrim(c.jjcp_cpdh) as jjcp_cpdh, g.实际投料')
  933. ->where($where)
  934. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  935. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  936. ->order('c.jjcp_sj desc')
  937. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  938. ->page($page,$limit)
  939. ->select();
  940. $total = db('成品入仓')->alias('c')
  941. ->where($where)
  942. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  943. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  944. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  945. ->count();
  946. $brr = db('成品入仓')
  947. ->group('jjcp_gdbh,jjcp_cpdh')
  948. ->column('CONCAT(jjcp_gdbh, "-", rtrim(jjcp_cpdh)), sum(jjcp_sl)');
  949. foreach ($rows as $key=>$value) {
  950. $rows[$key]['实际投料'] = (string)floatval($value['实际投料']*10000);
  951. if(array_key_exists($value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'],$brr)){
  952. $rows[$key]['入仓数量'] = $brr[$value['jjcp_gdbh'].'-'.$value['jjcp_cpdh']];
  953. if($rows[$key]['实际投料']!=0){
  954. $rows[$key]['投入产出率'] = number_format($rows[$key]['入仓数量']/$rows[$key]['实际投料']*100,2);
  955. }else{
  956. $rows[$key]['投入产出率'] = '';
  957. }
  958. }else{
  959. $rows[$key]['入仓数量'] = '';
  960. $rows[$key]['投入产出率'] = '';
  961. }
  962. }
  963. $data = [
  964. 'total' => $total,
  965. 'rows' => $rows,
  966. ];
  967. $this->success('成功',$data);
  968. }
  969. /**
  970. * 各日统计列表excel导出
  971. * @ApiMethod (POST)
  972. * @param string file_name
  973. * @param array fields
  974. */
  975. public function dayExport()
  976. {
  977. //get请求
  978. if(!$this->request->isPost()){
  979. $this->error('请求方式错误');
  980. }
  981. $req = $this->request->param();
  982. if (!isset($req['file_name'])){
  983. $this->error('参数错误','file_name');
  984. }
  985. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  986. $this->error('参数错误','fields',100);
  987. }
  988. if (isset($req['key_word']) && !empty($req['key_word'])){
  989. $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
  990. }
  991. $arr = [
  992. 'jjcp_gdbh' =>['jjcp_gdbh'],
  993. 'jjcp_yjno' =>['jjcp_yjno'],
  994. 'jjcp_cpmc' =>['印件名称'],
  995. '成品编码' =>['成品编码'],
  996. '成品名称' =>['成品名称'],
  997. '实际投料' =>['实际投料'],
  998. 'jjcp_sls' =>['入仓数量'],
  999. 'jjcp_sj' =>['完工日期'],
  1000. ];
  1001. $data[0] = [];
  1002. foreach ($req['fields'] as $k=>$v){
  1003. if(array_key_exists($v,$arr)){
  1004. array_push($data[0],$arr[$v][0]);
  1005. }
  1006. }
  1007. $where = [
  1008. 'c.jjcp_smb'=>['<>','']
  1009. ];
  1010. //客户编号
  1011. $rows = db('成品入仓')->alias('c')
  1012. ->field('c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.jjcp_cpmc) as jjcp_cpmc, rtrim(c.jjcp_cpdh) as jjcp_cpdh,
  1013. rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称, sum(jjcp_sl) as jjcp_sls,
  1014. g.实际投料, LEFT(c.jjcp_sj, 10) as jjcp_sj
  1015. ')
  1016. ->where($where)
  1017. ->where('g.Gd_cpdh=c.jjcp_cpdh')
  1018. ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
  1019. ->order('c.jjcp_sj desc')
  1020. ->group('c.jjcp_gdbh, c.jjcp_cpdh')
  1021. ->select();
  1022. foreach ($rows as $key=>$value) {
  1023. $subArray = [];
  1024. foreach ($arr as $k=>$v){
  1025. if($k=='实际投料'){
  1026. $subArray[$k] = (string)floatval($value['实际投料']*10000);
  1027. }else{
  1028. $subArray[$k] = $rows[$key][$k];
  1029. }
  1030. }
  1031. array_push($data,$subArray);
  1032. }
  1033. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1034. }
  1035. /**
  1036. * 入仓末板数据统计列表
  1037. * @ApiMethod (GET)
  1038. * @param string $date 时间
  1039. * @param string $sys_id 用户
  1040. */
  1041. public function mbList()
  1042. {
  1043. //get请求
  1044. if(!$this->request->isGet()){
  1045. $this->error('请求方式错误');
  1046. }
  1047. $req = $this->request->param();
  1048. // $page = 1;
  1049. // $limit = 15;
  1050. // if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  1051. // if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  1052. $date = date('Y-m-d',strtotime('-3 month'));
  1053. $arr1 = db('db_包装产量预报')
  1054. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1055. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1056. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1057. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1058. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1059. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1060. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1061. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1062. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1063. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1064. ')
  1065. ->where('sczl_rq','>=',$date)
  1066. ->select();
  1067. $data1 = [];
  1068. foreach ($arr1 as $k=>$v){
  1069. for ($i=1;$i<=6;$i++){
  1070. if (!empty($v['sczl_gdbh'.$i])){
  1071. $v['sczl_gdbh'.$i] = strtoupper($v['sczl_gdbh'.$i]);
  1072. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1073. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1074. }else{
  1075. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1076. }
  1077. }
  1078. }
  1079. }
  1080. krsort($data1);
  1081. //成品入仓中jjcp_smb不为空的工单
  1082. $rows2 = db('成品入仓')
  1083. ->where('jjcp_sj','>=',$date)
  1084. // ->where('jjcp_smb','<>','末 板')
  1085. ->group('jjcp_gdbh, jjcp_cpdh')
  1086. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, max(jjcp_smb) as jjcp_smb,
  1087. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj,UniqId");
  1088. $data = [];
  1089. foreach($data1 as $key=>$value){
  1090. $subArray = [];
  1091. $gdbh = strtoupper(explode('-',$key)[0]);
  1092. $yjno = explode('-',$key)[1];
  1093. $res = db('工单_印件资料')
  1094. ->where('Yj_Gdbh',$gdbh)
  1095. ->where('yj_Yjno',$yjno)
  1096. ->field('Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, (yj_平张投料 * yj_ks * yj_ls) as 投料')
  1097. ->find();
  1098. if(!empty($res)){
  1099. $subArray['Yj_Gdbh'] = $res['Yj_Gdbh'];
  1100. $subArray['yj_Yjno'] = $res['yj_Yjno'];
  1101. $subArray['yj_yjmc'] = trim($res['yj_yjmc']);
  1102. $subArray['yj_成品数量'] = (string)floatval($res['yj_成品数量']*10000);
  1103. $subArray['yj_实际投料'] = (string)floatval($res['投料']);
  1104. }else{
  1105. $subArray['Yj_Gdbh'] = '';
  1106. $subArray['yj_Yjno'] = '';
  1107. $subArray['yj_yjmc'] = '';
  1108. $subArray['yj_成品数量'] = '';
  1109. $subArray['yj_实际投料'] = '';
  1110. }
  1111. if(array_key_exists($key,$rows2)){
  1112. $subArray['jjcp_smb'] = $rows2[$key]['jjcp_smb'];
  1113. $subArray['jjcp_sls'] = $rows2[$key]['jjcp_sls'];
  1114. $subArray['jjcp_sj'] = $rows2[$key]['jjcp_sj'];
  1115. }else{
  1116. $subArray['jjcp_smb'] = '';
  1117. $subArray['jjcp_sls'] = '';
  1118. $subArray['jjcp_sj'] = '';
  1119. }
  1120. $subArray['sczl_cls'] = (string)floatval($value);
  1121. array_push($data,$subArray);
  1122. }
  1123. $data = [
  1124. 'total' => count($data),
  1125. 'rows' => $data,
  1126. ];
  1127. $this->success('成功',$data);
  1128. }
  1129. /**
  1130. * 入仓末板数据统计列表excel导出
  1131. * @ApiMethod (POST)
  1132. * @param string file_name
  1133. * @param array fields
  1134. */
  1135. public function mbExport()
  1136. {
  1137. //get请求
  1138. if(!$this->request->isPost()){
  1139. $this->error('请求方式错误');
  1140. }
  1141. $req = $this->request->param();
  1142. if (!isset($req['file_name'])){
  1143. $this->error('参数错误','file_name');
  1144. }
  1145. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  1146. $this->error('参数错误','fields',100);
  1147. }
  1148. $arr = [
  1149. 'Yj_Gdbh' =>['工单编号'],
  1150. 'yj_Yjno' =>['印件号'],
  1151. 'yj_yjmc' =>['印件名称'],
  1152. 'yj_成品数量' =>['计划交货数量'],
  1153. 'yj_实际投料' =>['印件投料'],
  1154. 'sczl_cls' =>['包装产量'],
  1155. 'jjcp_sj' =>['最近入仓时间'],
  1156. 'jjcp_sls' =>['入仓产量'],
  1157. 'status' =>['完工'],
  1158. '缺数' =>['缺数'],
  1159. ];
  1160. $data[0] = [];
  1161. foreach ($req['fields'] as $k=>$v){
  1162. if(array_key_exists($v,$arr)){
  1163. array_push($data[0],$arr[$v][0]);
  1164. }
  1165. }
  1166. $date = date('Y-m-d',strtotime('-3 month'));
  1167. $arr1 = db('db_包装产量预报')
  1168. ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
  1169. cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
  1170. cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
  1171. cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
  1172. (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
  1173. (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
  1174. (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
  1175. (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
  1176. (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
  1177. (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
  1178. ')
  1179. ->where('sczl_rq','>=',$date)
  1180. ->select();
  1181. $data1 = [];
  1182. foreach ($arr1 as $k=>$v){
  1183. for ($i=1;$i<=6;$i++){
  1184. if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
  1185. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
  1186. }else{
  1187. $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
  1188. }
  1189. }
  1190. }
  1191. krsort($data1);
  1192. //成品入仓中jjcp_smb不为空的工单
  1193. $rows1 = db('成品入仓')
  1194. ->where('jjcp_smb','<>','')
  1195. ->order('jjcp_gdbh desc')
  1196. ->column("CONCAT(jjcp_gdbh, '-', jjcp_yjno) AS gdyj, UniqId");
  1197. $rows2 = db('成品入仓')
  1198. ->where('jjcp_sj','>=',$date)
  1199. ->group('jjcp_gdbh, jjcp_cpdh')
  1200. ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, jjcp_smb,
  1201. sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj");
  1202. $rows3 = db('工单_印件资料')->cache(true,86400)
  1203. ->group('Yj_Gdbh, yj_Yjno')
  1204. ->column("CONCAT( Yj_Gdbh, '-', yj_Yjno) AS gdyj,
  1205. Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, yj_实际投料");
  1206. foreach($data1 as $key=>$value){
  1207. if(!array_key_exists($key,$rows1)){
  1208. $subArray = [];
  1209. foreach ($arr as $k=>$v){
  1210. if($k=='yj_成品数量' || $k=='yj_实际投料'){
  1211. if(array_key_exists($key,$rows3)){
  1212. $subArray[$k] = (string)floatval($rows3[$key][$k]*10000);
  1213. }else{
  1214. $subArray[$k] = '';
  1215. }
  1216. }else if($k=='Yj_Gdbh' || $k=='yj_Yjno'){
  1217. if(array_key_exists($key,$rows3)){
  1218. $subArray[$k] = $rows3[$key][$k];
  1219. }else{
  1220. $subArray[$k] = '';
  1221. }
  1222. }else if($k=='yj_yjmc'){
  1223. if(array_key_exists($key,$rows3)){
  1224. $subArray[$k] = trim($rows3[$key][$k]);
  1225. }else{
  1226. $subArray[$k] = '';
  1227. }
  1228. }else if($k=='sczl_cls'){
  1229. $subArray[$k] = (string)floatval($value);
  1230. }else if($k=='status'){
  1231. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1232. $subArray[$k] = '完工';
  1233. }else{
  1234. $subArray[$k] = '';
  1235. }
  1236. }else if($k=='缺数'){
  1237. if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
  1238. $subArray[$k]['缺数'] = (int)$rows3[$key]['yj_成品数量']-(int)$value;
  1239. }else{
  1240. $subArray[$k] = '';
  1241. }
  1242. }else{
  1243. if(array_key_exists($key,$rows2)){
  1244. $subArray[$k] = $rows2[$key][$k];
  1245. $subArray[$k] = $rows2[$key][$k];
  1246. $subArray[$k] = $rows2[$key][$k];
  1247. }else{
  1248. $subArray[$k] = '';
  1249. $subArray[$k] = '';
  1250. $subArray[$k] = '';
  1251. }
  1252. }
  1253. }
  1254. array_push($data,$subArray);
  1255. }
  1256. }
  1257. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  1258. }
  1259. /**
  1260. * 构建新增入仓数据
  1261. * @param array $req
  1262. * @return array
  1263. */
  1264. protected function buildAddData(array $req)
  1265. {
  1266. $fields = [
  1267. 'Sys_id',
  1268. '入仓类型', '仓库编号', '仓库名称',
  1269. 'jjcp_num',
  1270. 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
  1271. 'jjcp_cpdh', 'jjcp_cpmc',
  1272. '成品编码', '成品名称',
  1273. '客户料号',
  1274. 'jjcp_smb', 'jjcp_sj',
  1275. 'jjcp_sl', 'jjcp_dw',
  1276. '机型备注',
  1277. 'jjcp_desc',
  1278. ];
  1279. $data = [];
  1280. foreach ($fields as $index => $field) {
  1281. if (!isset($req[$field])) {
  1282. $this->error('参数错误', $field, $index + 1);
  1283. }
  1284. $data[$field] = $req[$field];
  1285. }
  1286. $data['Sys_rq'] = date('Y-m-d H:i:s');
  1287. return $data;
  1288. }
  1289. /**
  1290. * 校验印件产品名称
  1291. * @param string $gdbh
  1292. * @param string $yjno
  1293. * @param string $cpmc
  1294. */
  1295. protected function validateProductName($gdbh, $yjno, $cpmc)
  1296. {
  1297. $name = db('工单_印件资料')
  1298. ->where('Yj_Gdbh', $gdbh)
  1299. ->where('yj_Yjno', $yjno)
  1300. ->value('yj_yjmc');
  1301. if ($name !== $cpmc) {
  1302. $this->error('产品名称错误');
  1303. }
  1304. }
  1305. /**
  1306. * 查询工单印件是否已存在末版记录
  1307. * @param string $gdbh
  1308. * @param string $yjno
  1309. * @return array|null
  1310. */
  1311. protected function findExistingEndBoard($gdbh, $yjno)
  1312. {
  1313. return db('成品入仓')
  1314. ->where('jjcp_gdbh', $gdbh)
  1315. ->where('jjcp_yjno', $yjno)
  1316. ->where('jjcp_smb', '<>', '')
  1317. ->find();
  1318. }
  1319. /**
  1320. * 校验工单印件是否已完全领料
  1321. * @param string $gdbh
  1322. * @param string $yjno
  1323. */
  1324. protected function validateMaterialReceived($gdbh, $yjno)
  1325. {
  1326. $materialRecord = db('工单_印件资料')
  1327. ->alias('a')
  1328. ->join('物料_收发记录 b', 'a.Yj_Gdbh=b.st_gdbh and a.yj_cpdh=b.cpdh')
  1329. ->where('a.Yj_Gdbh', $gdbh)
  1330. ->where('a.yj_Yjno', $yjno)
  1331. ->group('b.st_wlbh')
  1332. ->order('b.st_wlbh asc')
  1333. ->column('b.st_wlbh');
  1334. $bomRecord = db('工单_印件资料')
  1335. ->alias('a')
  1336. ->join('工单_bom资料 b', 'a.Yj_Gdbh=b.BOM_工单编号 and a.yj_Yjno=b.BOM_工单行号 and a.yj_cpdh=b.BOM_产品编号')
  1337. ->where('a.Yj_Gdbh', $gdbh)
  1338. ->where('a.yj_Yjno', $yjno)
  1339. ->where('b.BOM_实际用量', '<>', 0)
  1340. ->group('b.BOM_物料编码')
  1341. ->order('b.BOM_物料编码 asc')
  1342. ->column('b.BOM_物料编码');
  1343. foreach ($bomRecord as $materialCode) {
  1344. if (!in_array($materialCode, $materialRecord)) {
  1345. $this->error('工单' . $gdbh . '印件' . $yjno . '物料' . $materialCode . '未领料');
  1346. }
  1347. }
  1348. }
  1349. /**
  1350. * 生成成品入仓 UniqId
  1351. * @return int
  1352. */
  1353. protected function generateFinishStockUniqId()
  1354. {
  1355. $uniqId = db('成品入仓')->max('UniqId');
  1356. return $uniqId < 10000000 ? 10000000 : $uniqId + 1;
  1357. }
  1358. /**
  1359. * 保存成品入仓数据到 MySQL 和 MongoDB
  1360. * @param array $data
  1361. * @param mixed $mongodb
  1362. */
  1363. protected function saveFinishStock(array $data, $mongodb)
  1364. {
  1365. db()->startTrans();
  1366. try {
  1367. $sql = db('成品入仓')->fetchSql(true)->insert($data);
  1368. $inserted = db()->query($sql);
  1369. if ($inserted === false) {
  1370. throw new \Exception('MySQL 保存失败');
  1371. }
  1372. $mongodb->name('finished_products')->insert($data);
  1373. db()->commit();
  1374. } catch (\Exception $e) {
  1375. db()->rollback();
  1376. $this->error($e->getMessage());
  1377. }
  1378. }
  1379. /**
  1380. * 判断是否为末版(仅判断首字符是否为「末」)
  1381. * @param string $smb
  1382. * @return bool
  1383. */
  1384. protected function isEndBoard($smb)
  1385. {
  1386. $smb = preg_replace('/^[\s\x{3000}]+|[\s\x{3000}]+$/u', '', (string)$smb);
  1387. if ($smb === '') {
  1388. return false;
  1389. }
  1390. return mb_substr($smb, 0, 1, 'UTF-8') === '末';
  1391. }
  1392. /**
  1393. * 是否需要同步成品入仓中间表
  1394. * @param string $gdbh
  1395. * @return bool
  1396. */
  1397. protected function shouldSyncFinishStockMiddle($gdbh)
  1398. {
  1399. return $this->explainFinishStockSyncGate($gdbh)['ok'];
  1400. }
  1401. /**
  1402. * 同步门槛判定及跳过原因(便于写入同步日志 status_msg)
  1403. * @param string $gdbh
  1404. * @return array{ok:bool,reason:string}
  1405. */
  1406. protected function explainFinishStockSyncGate($gdbh)
  1407. {
  1408. $gdbh = strtoupper(trim((string)$gdbh));
  1409. if ($gdbh === '' || !isset($gdbh[0]) || $gdbh[0] !== 'Y') {
  1410. return ['ok' => false, 'reason' => '不满足同步条件:工单编号不以Y开头或为空[' . $gdbh . ']'];
  1411. }
  1412. // if ((int)substr($gdbh, 1) < 2608001) {
  1413. // return ['ok' => false, 'reason' => '不满足同步条件:工单编号小于Y2608001[' . $gdbh . ']'];
  1414. // }
  1415. try {
  1416. $orderDate = db('工单_基本资料')->where('Gd_gdbh', $gdbh)->value('接单日期');
  1417. } catch (\Exception $e) {
  1418. return ['ok' => false, 'reason' => '查询工单_基本资料接单日期失败:' . $e->getMessage()];
  1419. }
  1420. if (empty($orderDate)) {
  1421. return ['ok' => false, 'reason' => '不满足同步条件:工单_基本资料无接单日期[' . $gdbh . ']'];
  1422. }
  1423. // if (strtotime(trim((string)$orderDate)) < strtotime('2026-08-01')) {
  1424. // return ['ok' => false, 'reason' => '不满足同步条件:接单日期[' . trim((string)$orderDate) . ']早于2026-08-01'];
  1425. // }
  1426. try {
  1427. // db3:正式中间库,仅查询 U8_06工单资料是否存在该工单
  1428. $middleDb = Db::connect(config('database.db3'));
  1429. $exists = $middleDb->name('U8_06工单资料')->where('工单编号', $gdbh)->count();
  1430. } catch (\Exception $e) {
  1431. return ['ok' => false, 'reason' => '查询正式库U8_06工单资料失败:' . $e->getMessage()];
  1432. }
  1433. if ($exists <= 0) {
  1434. return ['ok' => false, 'reason' => '不满足同步条件:正式库U8_06工单资料不存在工单[' . $gdbh . ']'];
  1435. }
  1436. return ['ok' => true, 'reason' => ''];
  1437. }
  1438. /**
  1439. * 按判定结果执行同步,或写入「未同步」原因到日志表
  1440. * @param array $record 成品入仓记录(至少含业务字段)
  1441. * @param int|string $uniqId
  1442. * @param string $insertType
  1443. * @param float|int|null $iQtyOverride
  1444. * @param string|null $usernameOverride
  1445. * @return string|null 失败或未同步时的提示文案;成功返回 null
  1446. */
  1447. protected function syncFinishStockOrLogSkip(array $record, $uniqId, $insertType, $iQtyOverride = null, $usernameOverride = null)
  1448. {
  1449. $gdbh = isset($record['jjcp_gdbh']) ? $record['jjcp_gdbh'] : '';
  1450. $gate = $this->explainFinishStockSyncGate($gdbh);
  1451. $synchronization = new \app\api\controller\Synchronization();
  1452. if (!$gate['ok']) {
  1453. $synchronization->logFinishStockSync($record, $uniqId, $insertType, '未同步', $gate['reason']);
  1454. // 条件不满足仅记日志,不作为接口告警
  1455. return null;
  1456. }
  1457. try {
  1458. $synchronization->syncFinishStockMiddle($uniqId, $insertType, $iQtyOverride, $usernameOverride);
  1459. return null;
  1460. } catch (\Exception $e) {
  1461. return $e->getMessage();
  1462. }
  1463. }
  1464. /**
  1465. * 修改后同步中间表:① 新增 + 原数量负数(修改前快照) ② 新增 + 修改后正数量
  1466. * @param array $old 修改前成品入仓记录
  1467. * @param int $uniqId
  1468. * @param string|null $sysId
  1469. * @return array 失败提示列表,空表示无告警
  1470. */
  1471. protected function syncFinishStockAfterEditOrLogSkip(array $old, $uniqId, $sysId = null)
  1472. {
  1473. $warnings = [];
  1474. $gdbh = $old['jjcp_gdbh'] ?? '';
  1475. $gate = $this->explainFinishStockSyncGate($gdbh);
  1476. $synchronization = new \app\api\controller\Synchronization();
  1477. $operator = ($sysId !== null && trim((string)$sysId) !== '') ? $sysId : ($old['Sys_id'] ?? '');
  1478. if (!$gate['ok']) {
  1479. $synchronization->logFinishStockSync($old, $uniqId, '新增', '未同步', $gate['reason']);
  1480. return $warnings;
  1481. }
  1482. $oldQty = (float)$old['jjcp_sl'];
  1483. if (abs($oldQty) < 0.00001) {
  1484. $synchronization->logFinishStockSync($old, $uniqId, '新增', '未同步', '原数量为0,跳过冲销同步');
  1485. return $warnings;
  1486. }
  1487. try {
  1488. $synchronization->syncFinishStockMiddle(
  1489. $uniqId,
  1490. '新增',
  1491. -1 * $oldQty,
  1492. $operator,
  1493. $old
  1494. );
  1495. } catch (\Exception $e) {
  1496. $warnings[] = '修改冲销同步失败:' . $e->getMessage();
  1497. return $warnings;
  1498. }
  1499. try {
  1500. $synchronization->syncFinishStockMiddle($uniqId, '新增', null, $operator);
  1501. } catch (\Exception $e) {
  1502. $warnings[] = '修改重传同步失败:' . $e->getMessage();
  1503. }
  1504. return $warnings;
  1505. }
  1506. /**
  1507. * 末版入仓后进入超节损
  1508. * @param string $gdbh
  1509. * @param string $yjno
  1510. */
  1511. protected function handleEndBoardSuperLoss($gdbh, $yjno)
  1512. {
  1513. $orderLoss = new OrderSuperLoss();
  1514. $result = [];
  1515. try {
  1516. $result = $orderLoss->OneOrderSuperLoss($gdbh, $yjno);
  1517. } catch (DataNotFoundException $e) {
  1518. } catch (ModelNotFoundException $e) {
  1519. } catch (DbException $e) {
  1520. }
  1521. if (empty($result)) {
  1522. return;
  1523. }
  1524. db('工单_质量考核汇总')
  1525. ->where('Gy0_gdbh', $gdbh)
  1526. ->where('印件及工序', $yjno)
  1527. ->delete();
  1528. $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
  1529. $inserted = db()->query($sql);
  1530. if ($inserted === false) {
  1531. throw new \Exception('超节损数据保存失败');
  1532. }
  1533. }
  1534. }