Allot.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. class Allot extends BaseController
  8. {
  9. public $post= "";
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $this->post=$this->request->post();
  14. }
  15. public function list(){
  16. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  17. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  18. $where =[['is_del',"=",0]];
  19. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !== "" ? trim($this->post['allot_code']) : "";
  20. if ($allot_code !== "") {
  21. // $where['allot_code'] = $allot_code;
  22. $where[]=['allot_code',"=",$allot_code];
  23. }
  24. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !== "" ? trim($this->post['wsm_out']) : "";
  25. if ($wsm_out !== "") {
  26. //$where['wsm_out'] = $wsm_out;
  27. $where[]=['wsm_out',"=",$wsm_out];
  28. }
  29. $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !== "" ? trim($this->post['wsm_in']) : "";
  30. if ($wsm_in !== "") {
  31. // $where['wsm_in'] = $wsm_in;
  32. $where[]=['wsm_in',"=",$wsm_in];
  33. }
  34. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  35. if ($start !="") {
  36. //$where = ["addtime"=>Db::raw(">= '{$start}'")];
  37. $where[]=['addtime',">=",$start];
  38. }
  39. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  40. if($end !=""){
  41. // $where = ["addtime"=>Db::raw("<= '{$end}'")];
  42. $where[]=['addtime',"<=",$end];
  43. }
  44. $out_supplierNo =isset($this->post['wsm_out_supplierNo']) &&$this->post['wsm_out_supplierNo'] !=="" ? trim
  45. ($this->post['wsm_out_supplierNo']):"";
  46. if($out_supplierNo!=""){
  47. $supplier = Db::name("supplier")->where(["code"=>$out_supplierNo])->find();
  48. if(empty($supplier)){
  49. return error_show(1004,"未找到出库供应商信息");
  50. }
  51. $wsmcode = Db::name("warehouse_info")->where([
  52. "is_del"=>0,"supplierNo"=>$out_supplierNo])->column("wsm_code");
  53. //$condtion["c.wsm_code"] = $wsmcode;
  54. $where[]=['wsm_out',"in",$wsmcode];
  55. }
  56. $in_supplierNo =isset($this->post['wsm_in_supplierNo']) &&$this->post['wsm_in_supplierNo'] !=="" ? trim
  57. ($this->post['wsm_in_supplierNo']):"";
  58. if($in_supplierNo!=""){
  59. $supplier = Db::name("supplier")->where(["code"=>$in_supplierNo])->find();
  60. if(empty($supplier)){
  61. return error_show(1004,"未找到入库供应商信息");
  62. }
  63. $wsmcode = Db::name("warehouse_info")->where([
  64. "is_del"=>0,"supplierNo"=>$in_supplierNo])->column("wsm_code");
  65. //$condtion["c.wsm_code"] = $wsmcode;
  66. $where[]=['wsm_in',"in",$wsmcode];
  67. }
  68. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  69. if($status!==""){
  70. $where[]=['status',"=",$status];
  71. }
  72. $count = Db::name('allot_stock')->where($where)->count();
  73. $total = ceil($count/$size);
  74. $page = $page >= $total ? $total : $page;
  75. $list = Db::name('allot_stock')->where($where)->order("addtime desc")->page($page,$size)->select();
  76. // var_dump(Db::name('allot_stock')->getLastSql());
  77. $data=[];
  78. foreach ($list as $value){
  79. $db = Db::name('warehouse_info')->alias('a')->join("supplier b","b.code=a.supplierNo","left")
  80. ->field("a.name,a.wsm_code,b.name rname,b.code")->where(['a.wsm_code'=>$value['wsm_out'],'a.is_del'=>0])->find();
  81. $value['name'] =isset($db['name']) ? $db['name']:"";
  82. $value['wsm_code'] =isset($db['wsm_code']) ? $db['wsm_code']:"";
  83. $value['rname'] =isset($db['rname']) ? $db['rname']:"";
  84. $value['code'] =isset($db['code']) ? $db['code']:"";
  85. $dc = Db::name('warehouse_info')->alias('a')->join("supplier b","b.code=a.supplierNo","left")
  86. ->field("a.name,a.wsm_code,b.name tname,b.code")->where(['a.wsm_code'=>$value['wsm_in'],'a.is_del'=>0])->find();
  87. $value['wsm_in_name'] =isset($dc['name']) ? $dc['name']:"";
  88. $value['wsm_in_code'] =isset($dc['wsm_code']) ? $dc['wsm_code']:"";
  89. $value['tname'] =isset($dc['tname']) ? $dc['tname']:"";
  90. $value['rcode'] =isset($dc['code']) ? $dc['code']:"";
  91. $data []=$value;
  92. }
  93. return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
  94. }
  95. public function create(){
  96. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  97. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"";
  98. if($wsm_out==""){
  99. return error_show(1002,"出库仓库编号不能为空");
  100. }
  101. $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"";
  102. if($wsm_in==""){
  103. return error_show(1002,"入库仓库不能为空");
  104. }
  105. // $good_type_code = isset($this->post['good_type_code']) && $this->post['good_type_code'] !=="" ? trim($this->post['good_type_code']) :"";
  106. // if($good_type_code==""){
  107. // return error_show(1002,"商品属性不能为空");
  108. // }
  109. $allot_code=makeNo("XK");
  110. // $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  111. // if($allot_code==""){
  112. // return error_show(1002,"调拨编号不能为空");
  113. // }
  114. $apply_id =GetUserInfo($token);
  115. if(empty($apply_id)||$apply_id['code']!=0){
  116. return error_show(1002,"申请人数据不存在");
  117. }
  118. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  119. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  120. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"" ;
  121. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  122. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  123. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) : "";
  124. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) :"0";
  125. if($status==""){
  126. return error_show(1002,"状态不能为空");
  127. }
  128. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  129. if($dain==""){
  130. return error_show(1002,"商品不能为空");
  131. }
  132. Db::startTrans();
  133. try {
  134. $data=[
  135. "allot_code"=>$allot_code,
  136. "wsm_out"=>$wsm_out,
  137. "wsm_in"=>$wsm_in,
  138. "apply_name"=>$ri,
  139. "apply_id"=>$rm,
  140. "post_name"=>$post_name,
  141. "post_code"=>$post_code,
  142. "post_fee"=>$post_fee,
  143. "remark"=>$remark,
  144. "status"=>$status,
  145. "is_del"=>0,
  146. "addtime"=>date("Y-m-d H:i:s"),
  147. "updatetime"=>date("Y-m-d H:i:s"),
  148. ];
  149. $pd=Db::name('allot_stock')->insert($data,true);
  150. $dm=[];
  151. if($pd>0) {
  152. foreach ($dain as $value) {
  153. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  154. ->join("good_stock c", "c.good_type_code = b.type_code", "left")
  155. ->join("warehouse_info v","v.wsm_code=c.wsm_code","left")
  156. ->join("supplier n","n.code=v.supplierNo","left")
  157. ->where(['c.wsm_code' => $wsm_out, 'good_type_code' => $value['good_code'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
  158. ->field("b.type_code,a.good_name,a.unit,c.wsm_code,c.usable_stock,c.good_type_code,c.wait_out_stock,c.wait_in_stock,n.name")->find();
  159. // var_dump(Db::name("good_type")->getLastSql());
  160. if (empty($st)) {
  161. Db::rollback();
  162. return error_show(1003, "商品不能为空");
  163. }
  164. if($value['allot_num']>$st['usable_stock']){
  165. return error_show(2000,"库存数量不足");
  166. }
  167. $temp = [];
  168. $temp['good_name'] = $st['good_name'];
  169. $temp['allot_code'] =$allot_code;
  170. $temp['good_type_code'] = $value['good_code'];
  171. $temp['allot_num'] = $value['allot_num'];
  172. $temp['usable_num'] = 0;
  173. $temp['error_num'] = 0;
  174. $temp['error_remark'] = "";
  175. $temp['stock_num'] = 0;
  176. $temp['error_code']="";
  177. $temp['good_num']=$st['usable_stock'];
  178. $temp['addtime'] = date("Y-m-d H:i:s");
  179. $temp['updatetime'] = date("Y-m-d H:i:s");
  180. $dm[]=$temp;
  181. }
  182. $io = Db::name('allot_info')->insertAll($dm);
  183. //var_dump(Db::name('allot_info')->getLastSql());
  184. if ($io) {
  185. DB::commit();
  186. return error_show(0, "调拨创建成功");
  187. }
  188. }
  189. Db::rollback();
  190. return error_show(1002,"调拨创建失败");
  191. }catch(\Exception $e){
  192. Db::rollback();
  193. return error_show(1005,$e->getMessage());
  194. }
  195. }
  196. public function info(){
  197. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  198. if($id==""){
  199. return error_show(1002,"参数id不能为空");
  200. }
  201. $str = Db::name('allot_stock') ->where(['id'=>$id,'is_del'=>0])->find();
  202. if(empty($str)){
  203. return error_show(1003,"未找到调拨数据");
  204. }
  205. $stv = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_out'],'is_del'=>0])->field('supplierNo,wsm_code,name')->find();
  206. if(empty($stv)){
  207. return error_show(1002,"未找到仓库数据");
  208. }
  209. $vr = Db::name('supplier')->where(['code'=>$stv['supplierNo'],'is_del'=>0])->field('name,code as rcode')->find();
  210. if(empty($vr)){
  211. return error_show(1001,"未找到供应商数据");
  212. }
  213. $st = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_in'],'is_del'=>0])->field('supplierNo,wsm_code,name')->find();
  214. if(empty($st)){
  215. return error_show(1003,"未找到仓库数据");
  216. }
  217. $var = Db::name('supplier')->where(['code'=>$st['supplierNo'],'is_del'=>0])->field('name,code')->find();
  218. if(empty($var)){
  219. return error_show(1004,"未找到供应商数据");
  220. }
  221. // var_dump(Db::name('supplier')->getLastSql());
  222. $str ['wsm_out_name']=$vr['name'];
  223. $str ['wsm_in_name']=$var['name'];
  224. $str ['ckc_name']=$stv['name'];
  225. $str ['ckr_name']=$st['name'];
  226. $str['out_code']=$vr['rcode'];
  227. $str['in_code']=$var['code'];
  228. // $str = Db::name('allot_stock')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_out","left")
  229. // ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,")
  230. // ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  231. // var_dump( Db::name('allot_stock')->getLastSql());
  232. $vmp = Db::name('allot_info')->where(['allot_code'=>$str['allot_code'],'is_del'=>0])->select();
  233. $data=[];
  234. foreach ($vmp as $k=>$value){
  235. $s =Db::name('good_stock')->where(['good_type_code'=>$value['good_type_code'],'wsm_code'=>$str['wsm_out']])->find();
  236. if($str['status']<=1) {
  237. $value['usable_num'] = $s['usable_stock'];
  238. }
  239. $value['error_msg']="";
  240. if($value['error_code']!=""){
  241. $msg =Db::name("result_info")->where(['result_code'=>$value['error_code'],"is_del"=>0])->find();
  242. $value['error_code_name']= isset($msg['result']) &&$msg['result']!="" ? $msg['result']:"";
  243. }
  244. $data[]=$value;
  245. }
  246. $str['item']=$data;
  247. if(empty($str)){
  248. return error_show(1002,"未找到调拨编号");
  249. }else{
  250. return app_show(0,"获取成功",$str);
  251. }
  252. }
  253. public function edit(){
  254. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  255. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  256. if($id===""){
  257. return error_show(1002,"参数id不能为空");
  258. }
  259. $etid = Db::name("allot_stock")->where(["id"=>$id,"is_del"=>0])->find();
  260. if(empty($etid)){
  261. return error_show(1002,"未找到数据");
  262. }
  263. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  264. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  265. $apply_id =GetUserInfo($token);
  266. if(empty($apply_id)||$apply_id['code']!=0){
  267. return error_show(1002,"申请人数据不存在");
  268. }
  269. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  270. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  271. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"" ;
  272. if($wsm_out==""){
  273. return error_show(1002,"出库仓库不能为空");
  274. }
  275. $wsm_in= isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"" ;
  276. if($wsm_in==""){
  277. return error_show(1002,"入库仓库不能为空");
  278. }
  279. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  280. // $status= isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']) :"" ;
  281. // if($status==""){
  282. // return error_show(1002,"状态不能为空");
  283. // }
  284. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  285. if($dain==""){
  286. return error_show(1002,"商品不能为空");
  287. }
  288. Db::startTrans();
  289. try{
  290. $data=[
  291. "apply_id"=>$rm,
  292. "apply_name"=>$ri,
  293. "wsm_out"=>$wsm_out,
  294. "wsm_in"=>$wsm_in,
  295. "post_fee"=>$post_fee,
  296. "post_name"=>$post_name,
  297. "post_code"=>$post_code,
  298. "updatetime"=>date('Y-m-d H:i:s'),
  299. ];
  300. $da = Db::name('allot_stock')->where(["id"=>$id,"is_del"=>0])->save($data);
  301. if($da>0) {
  302. foreach ($dain as $value) {
  303. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  304. ->join("good_stock c", "c.good_type_code = b.type_code", "left")->where(['wsm_code' => $wsm_out, 'good_type_code' => $value['good_code'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
  305. ->field("b.type_code,a.good_name,a.unit,c.wsm_code,c.usable_stock,c.good_type_code,c.wait_out_stock,c.wait_in_stock")->find();
  306. // var_dump(Db::name("good_type")->getLastSql());
  307. if (empty($st)) {
  308. Db::rollback();
  309. return error_show(1003, "商品不能为空");
  310. }
  311. if(isset($value['id'])&&$value['id']!==""){
  312. $good = Db::name('allot_info')->where(["id"=>$value['id'],"is_del"=>0])->find();
  313. if(empty($good)||$good['allot_code']!=$etid['allot_code']){
  314. Db::rollback();
  315. return error_show(2000,"商品不属于此次调拨");
  316. }
  317. }
  318. if($value['allot_num']>$st['usable_stock']){
  319. Db::rollback();
  320. return error_show(2000,"库存数量不足");
  321. }
  322. $temp = [];
  323. isset($value['id'])&&$value['id']!=="" ? $temp['id'] = $value['id']:'';
  324. $temp['good_name'] = $st['good_name'];
  325. $temp['good_type_code'] = $value['good_code'];
  326. $temp['allot_num'] = $value['allot_num'];
  327. $temp['usable_num'] = 0;
  328. $temp['error_num'] = 0;
  329. $temp['error_remark'] = "";
  330. $temp['allot_code'] = $etid['allot_code'];
  331. $temp['stock_num'] = 0;
  332. $temp['good_num']=$st['usable_stock'];
  333. $temp['error_code']="";
  334. $temp['is_del']=$value['is_del'];
  335. isset($value['id'])&&$value['id']!=="" ? '' :$temp['addtime'] = date("Y-m-d H:i:s");
  336. $temp['updatetime'] = date("Y-m-d H:i:s");
  337. $np = Db::name('allot_info')->save($temp);
  338. if ($np==false) {
  339. Db::rollback();
  340. return error_show(1003, "调拨更新失败");
  341. }
  342. }
  343. Db::commit();
  344. return error_show(0, "调拨更新成功");
  345. }
  346. Db::rollback();
  347. return error_show(1003, "调拨更新失败");
  348. }catch (\Exception $e){
  349. Db::rollback();
  350. return error_show(1005,$e->getMessage());
  351. }
  352. }
  353. public function del(){
  354. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  355. if($id===""){
  356. return error_show(1004,"参数id不能为空");
  357. }
  358. $str= Db::name('allot_stock')->where(['id'=>$id,'is_del'=>0])->find();
  359. if(empty($str)){
  360. return error_show(1002,"未找到数据");
  361. }
  362. $end = Db::name('allot_stock')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  363. if($end){
  364. return error_show(0,"删除成功");
  365. }else{
  366. return error_show(1002,"删除失败");
  367. }
  368. }
  369. public function status(){
  370. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"" ;
  371. if($id==""){
  372. return error_show(1002,"调拨编号不能为空");
  373. }
  374. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) :"";
  375. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  376. if($status===""){
  377. return error_show(1002,"审核状态不能为空");
  378. }
  379. $st = Db::name('allot_stock')->where(['id'=>$id,"is_del"=>0])->find();
  380. if(empty($st)){
  381. return error_show(1002,"调拨信息未找到");
  382. }
  383. Db::startTrans();
  384. try{
  385. $st['remark']=$remark;
  386. $st['status']=$status;
  387. $st['updatetime']= date('Y-m-d H:i:s');
  388. $sv = Db::name('allot_stock')->save($st);
  389. if($sv){
  390. if($status==2 || $status==1){
  391. $vmp = Db::name('allot_info')->where(['allot_code'=>$st['allot_code'],"is_del"=>0])->select();
  392. foreach ($vmp as $value){
  393. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_type_code'],"wsm_code"=>$st['wsm_out'],"is_del"=>0])->find();
  394. //var_dump($value['allot_num'],$stock['usable_stock']);
  395. if($value['allot_num']>$stock['usable_stock']){
  396. Db::rollback();
  397. return error_show(2000,"超出库存数量");
  398. }
  399. if($status==2){
  400. $stock['usable_stock']-=$value['allot_num'];
  401. $stock['wait_out_stock']+=$value['allot_num'];
  402. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  403. $stock['updatetime']= date('Y-m-d H:i:s');
  404. $Db = Db::name('good_stock')->update($stock);
  405. //var_dump(Db::name('good_stock')->getLastSql());
  406. if($Db==false){
  407. Db::rollback();
  408. return error_show(1002,"状态更新失败");
  409. }
  410. }
  411. }
  412. }
  413. Db::commit();
  414. return error_show(0,"状态更新成功");
  415. }
  416. Db::rollback();
  417. return error_show(1003,"状态更新失败");
  418. }catch (\Exception $e) {
  419. Db::rollback();
  420. return error_show(1005, $e->getMessage());
  421. }
  422. }
  423. /*出库方发货*/
  424. public function getont(){
  425. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  426. if(empty($allot_code)){
  427. return error_show(1002,"调拨单编号不能为空");
  428. }
  429. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  430. if($al==""){
  431. return error_show(1003,"调拨编号未找到");
  432. }
  433. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  434. if($post_name==""){
  435. return error_show(1002,"物流公司不能为空");
  436. }
  437. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  438. if($post_code==""){
  439. return error_show(1002,"物流单号不能为空");
  440. }
  441. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  442. if($post_fee===""){
  443. return error_show(1002,"物流费用不能为空");
  444. }
  445. Db::startTrans();
  446. try {
  447. $data=[
  448. "allot_code"=>$allot_code,
  449. "post_name"=>$post_name,
  450. "post_code"=>$post_code,
  451. "post_fee"=>$post_fee,
  452. "status"=>3,
  453. "is_del"=>0,
  454. "updatetime"=>date("Y-m-d H:i:s"),
  455. ];
  456. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code])->update($data);
  457. if($datainfo){
  458. $vnp = Db::name('allot_info')->where(['allot_code'=>$al['allot_code'],'is_del'=>0])->select();
  459. // var_dump(Db::name('allot_info')->getLastSql());
  460. $good_data=[];
  461. foreach ($vnp as $value){
  462. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_type_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  463. if($stock['wait_out_stock']<$value['allot_num']){
  464. Db::rollback();
  465. return error_show(2000,"超出库存数量");
  466. }
  467. $stock['wait_out_stock']-=$value['allot_num'];
  468. $stock['intra_stock']+=$value['allot_num'];
  469. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  470. $stock['updatetime']= date('Y-m-d H:i:s');
  471. $dr = Db::name('good_stock')->update($stock);
  472. if($dr==false){
  473. Db::rollback();
  474. return error_show(1002,"发货更新失败");
  475. }
  476. $good_data[]=["stock_id"=>$stock['id'],"type"=>1,'stock'=>$value['allot_num'],"stock_name"=>"intra_stock"];
  477. $good_data[]= ["stock_id"=>$stock['id'],"type"=>2,'stock'=>$value['allot_num'],"stock_name"=>"wait_out_stock"];
  478. }
  479. GoodLog::LogAdd($this->post['token'],$good_data,'allotsend');
  480. Db::commit();
  481. return error_show(0,"发货更新成功");
  482. }
  483. Db::rollback();
  484. return error_show(1003,"发货更新失败");
  485. }catch (\Exception $e) {
  486. Db::rollback();
  487. return error_show(1005, $e->getMessage());
  488. }
  489. }
  490. /*入库方验货*/
  491. public function getin(){
  492. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  493. if(empty($allot_code)){
  494. return error_show(1002,"调拨单编号不能为空");
  495. }
  496. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  497. if($al==""){
  498. return error_show(1003,"调拨编号未找到");
  499. }
  500. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  501. if($dain==""){
  502. return error_show(1002,"商品不能为空");
  503. }
  504. Db::startTrans();
  505. try {
  506. $data=[
  507. "allot_code"=>$allot_code,
  508. "is_del"=>0,
  509. "status"=>4,
  510. "updatetime"=>date("Y-m-d H:i:s"),
  511. ];
  512. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  513. if($datainfo>0) {
  514. $good_data=[];
  515. foreach ($dain as $value) {
  516. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  517. ->join("good_stock c", "c.good_type_code = b.type_code", "left")->where(['wsm_code' => $al['wsm_out'], 'good_type_code' => $value['good_code'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
  518. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock,c.good_type_code")->find();
  519. if (empty($st)) {
  520. Db::rollback();
  521. return error_show(1003, "未找到商品");
  522. }
  523. $n = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code'],'is_del'=>0])->find();
  524. if(empty($n)){
  525. Db::rollback();
  526. return error_show(10032, "未找到数据");
  527. }
  528. // if($value['allot_num']>$st['usable_stock']){
  529. // return error_show(2000,"库存数量不足");
  530. // }
  531. $temp = [];
  532. $temp['error_code']=$value['error_code'];
  533. $temp['good_num']=$st['usable_stock'];
  534. $temp['usable_num'] = $value['usable_num'];
  535. $temp['error_num'] = $value['error_num'];
  536. $temp['error_remark'] = $value['error_remark'];
  537. $temp['stock_num']=$n['allot_num'];
  538. $temp['updatetime'] = date("Y-m-d H:i:s");
  539. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code'],'is_del'=>0])->save($temp);
  540. if($np==false){
  541. Db::rollback();
  542. return error_show(1001,"数据更新失败");
  543. }
  544. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  545. if($stock['intra_stock']<$n['allot_num']){
  546. Db::rollback();
  547. return error_show(2000,"超出库存数量");
  548. }
  549. $stock['updatetime']= date('Y-m-d H:i:s');
  550. $stock['intra_stock']-=$n['allot_num'];
  551. $dr = Db::name('good_stock')->update($stock);
  552. if($dr==false){
  553. Db::rollback();
  554. return error_show(1002,"数据更新失败");
  555. }
  556. $good_data[]=["stock_id"=>$stock['id'],"type"=>2,'stock'=>$n['allot_num'],"stock_name"=>"intra_stock"];
  557. $instock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  558. if($instock==false){
  559. $instock =[];
  560. $instock['good_type_code']=$value['good_code'];
  561. $instock['wsm_code']=$al['wsm_in'];
  562. $instock['wait_in_stock']=0;
  563. $instock['wait_out_stock']=0;
  564. $instock['usable_stock']=0;
  565. $instock['intra_stock']=0;
  566. $instock['total_stock']=0;
  567. $instock['status']=1;
  568. $instock['is_del']=0;
  569. $instock['warn_stock']=0;
  570. $instock['addtime']= date('Y-m-d H:i:s');
  571. }
  572. $instock['wait_in_stock']+=$n['allot_num'];
  573. $instock['updatetime']= date('Y-m-d H:i:s');
  574. $sk = Db::name('good_stock')->save($instock);
  575. // $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  576. if ($sk==false) {
  577. Db::rollback();
  578. return error_show(1003, "入库验货更新失败");
  579. }
  580. $good_data[]=["stock_id"=>isset($instock['id'])?$instock['id']: Db::name("good_stock")->getLastInsID(),"type"=>1,'stock'=>$n['allot_num'],"stock_name"=>"wait_in_stock"];
  581. }
  582. GoodLog::LogAdd($this->post['token'],$good_data,'allotin');
  583. Db::commit();
  584. return error_show(0, "入库验货更新成功");
  585. }
  586. Db::rollback();
  587. return error_show(1002, "入库验货更新失败");
  588. }catch (\Exception $e){
  589. Db::rollback();
  590. return error_show(1005,$e->getMessage());
  591. }
  592. }
  593. /*验货审核*/
  594. public function vesio(){
  595. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  596. if(empty($allot_code)){
  597. return error_show(1002,"调拨单编号不能为空");
  598. }
  599. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  600. if($al==""){
  601. return error_show(1003,"调拨编号未找到");
  602. }
  603. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  604. if($dain==""){
  605. return error_show(1002,"商品不能为空");
  606. }
  607. Db::startTrans();
  608. try {
  609. $data=[
  610. "allot_code"=>$allot_code,
  611. "status"=>5,
  612. "is_del"=>0,
  613. "updatetime"=>date("Y-m-d H:i:s"),
  614. ];
  615. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  616. if($datainfo>0) {
  617. foreach ($dain as $value) {
  618. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  619. ->join("good_stock c", "c.good_type_code = b.type_code", "left")->where(['wsm_code' => $al['wsm_in'], 'good_type_code' => $value['good_code'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
  620. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock")->find();
  621. if (empty($st)) {
  622. Db::rollback();
  623. return error_show(1003, "商品不能为空");
  624. }
  625. $temp = [];
  626. $temp['error_code']="";
  627. $temp['good_num']=$st['usable_stock'];
  628. $temp['error_num'] = $value['error_num'];
  629. $temp['stock_num'] = $value['stock_num'];
  630. $temp['error_remark'] = $value['error_remark'];
  631. $temp['updatetime'] = date("Y-m-d H:i:s");
  632. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code']])->save($temp);
  633. if($np==false){
  634. Db::rollback();
  635. return error_show(1001,"数据更新失败");
  636. }
  637. $p = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=>$al['allot_code'],"is_del"=>0])->find();
  638. if($p==false){
  639. Db::rollback();
  640. return error_show(1002,"未找到数据");
  641. }
  642. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  643. if($stock['wait_in_stock']<$p['stock_num']){
  644. Db::rollback();
  645. return error_show(2000,"超出待入库存数量");
  646. }
  647. $stock['wait_in_stock']-=$p['stock_num'];
  648. $stock['usable_stock']+=$p['stock_num'];
  649. $stock['updatetime']=date('Y-m-d H:i:s');
  650. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  651. $dr = Db::name('good_stock')->update($stock);
  652. if ($dr==false) {
  653. Db::rollback();
  654. return error_show(1004, "验货更新失败");
  655. }
  656. $good_data[]=["stock_id"=> $stock['id'],"type"=>2,'stock'=>$p['stock_num'],"stock_name"=>"wait_in_stock"];
  657. $good_data[]=["stock_id"=> $stock['id'],"type"=>1,'stock'=>$p['stock_num'],"stock_name"=>"usable_stock"];
  658. }
  659. GoodLog::LogAdd($this->post['token'],$good_data,'allotincheck');
  660. Db::commit();
  661. return error_show(0, "验货更新成功");
  662. }
  663. Db::rollback();
  664. return error_show(1003, "验货更新失败");
  665. }catch (\Exception $e){
  666. Db::rollback();
  667. return error_show(1005,$e->getMessage());
  668. }
  669. }
  670. }