Allot.php 33 KB

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