Allot.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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']==0) {
  236. $value['usable_num'] = $s['usable_stock'];
  237. }
  238. $data[]=$value;
  239. }
  240. $str['item']=$data;
  241. if(empty($str)){
  242. return error_show(1002,"未找到调拨编号");
  243. }else{
  244. return app_show(0,"获取成功",$str);
  245. }
  246. }
  247. public function edit(){
  248. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  249. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  250. if($id===""){
  251. return error_show(1002,"参数id不能为空");
  252. }
  253. $etid = Db::name("allot_stock")->where(["id"=>$id,"is_del"=>0])->find();
  254. if(empty($etid)){
  255. return error_show(1002,"未找到数据");
  256. }
  257. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  258. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  259. $apply_id =GetUserInfo($token);
  260. if(empty($apply_id)||$apply_id['code']!=0){
  261. return error_show(1002,"申请人数据不存在");
  262. }
  263. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  264. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  265. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"" ;
  266. if($wsm_out==""){
  267. return error_show(1002,"出库仓库不能为空");
  268. }
  269. $wsm_in= isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"" ;
  270. if($wsm_in==""){
  271. return error_show(1002,"入库仓库不能为空");
  272. }
  273. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  274. // $status= isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']) :"" ;
  275. // if($status==""){
  276. // return error_show(1002,"状态不能为空");
  277. // }
  278. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  279. if($dain==""){
  280. return error_show(1002,"商品不能为空");
  281. }
  282. Db::startTrans();
  283. try{
  284. $data=[
  285. "apply_id"=>$rm,
  286. "apply_name"=>$ri,
  287. "wsm_out"=>$wsm_out,
  288. "wsm_in"=>$wsm_in,
  289. "post_fee"=>$post_fee,
  290. "post_name"=>$post_name,
  291. "post_code"=>$post_code,
  292. "updatetime"=>date('Y-m-d H:i:s'),
  293. ];
  294. $da = Db::name('allot_stock')->where(["id"=>$id,"is_del"=>0])->save($data);
  295. if($da>0) {
  296. foreach ($dain as $value) {
  297. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  298. ->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")
  299. ->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();
  300. // var_dump(Db::name("good_type")->getLastSql());
  301. if (empty($st)) {
  302. Db::rollback();
  303. return error_show(1003, "商品不能为空");
  304. }
  305. if(isset($value['id'])&&$value['id']!==""){
  306. $good = Db::name('allot_info')->where(["id"=>$value['id'],"is_del"=>0])->find();
  307. if(empty($good)||$good['allot_code']!=$etid['allot_code']){
  308. Db::rollback();
  309. return error_show(2000,"商品不属于此次调拨");
  310. }
  311. }
  312. if($value['allot_num']>$st['usable_stock']){
  313. Db::rollback();
  314. return error_show(2000,"库存数量不足");
  315. }
  316. $temp = [];
  317. isset($value['id'])&&$value['id']!=="" ? $temp['id'] = $value['id']:'';
  318. $temp['good_name'] = $st['good_name'];
  319. $temp['good_type_code'] = $value['good_code'];
  320. $temp['allot_num'] = $value['allot_num'];
  321. $temp['usable_num'] = 0;
  322. $temp['error_num'] = 0;
  323. $temp['error_remark'] = "";
  324. $temp['allot_code'] = $etid['allot_code'];
  325. $temp['stock_num'] = 0;
  326. $temp['good_num']=$st['usable_stock'];
  327. $temp['error_code']="";
  328. $stmp['is_del']=$value['is_del'];
  329. isset($value['id'])&&$value['id']!=="" ? '' :$temp['addtime'] = date("Y-m-d H:i:s");
  330. $temp['updatetime'] = date("Y-m-d H:i:s");
  331. $np = Db::name('allot_info')->save($temp);
  332. if ($np==false) {
  333. Db::rollback();
  334. return error_show(1003, "调拨更新失败");
  335. }
  336. }
  337. Db::commit();
  338. return error_show(0, "调拨更新成功");
  339. }
  340. Db::rollback();
  341. return error_show(1003, "调拨更新失败");
  342. }catch (\Exception $e){
  343. Db::rollback();
  344. return error_show(1005,$e->getMessage());
  345. }
  346. }
  347. public function del(){
  348. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  349. if($id===""){
  350. return error_show(1004,"参数id不能为空");
  351. }
  352. $str= Db::name('allot_stock')->where(['id'=>$id,'is_del'=>0])->find();
  353. if(empty($str)){
  354. return error_show(1002,"未找到数据");
  355. }
  356. $end = Db::name('allot_stock')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  357. if($end){
  358. return error_show(0,"删除成功");
  359. }else{
  360. return error_show(1002,"删除失败");
  361. }
  362. }
  363. public function status(){
  364. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"" ;
  365. if($id==""){
  366. return error_show(1002,"调拨编号不能为空");
  367. }
  368. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) :"";
  369. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  370. if($status===""){
  371. return error_show(1002,"审核状态不能为空");
  372. }
  373. $st = Db::name('allot_stock')->where(['id'=>$id,"is_del"=>0])->find();
  374. if(empty($st)){
  375. return error_show(1002,"调拨信息未找到");
  376. }
  377. Db::startTrans();
  378. try{
  379. $st['remark']=$remark;
  380. $st['status']=$status;
  381. $st['updatetime']= date('Y-m-d H:i:s');
  382. $sv = Db::name('allot_stock')->save($st);
  383. if($sv){
  384. if($status==2 || $status==1){
  385. $vmp = Db::name('allot_info')->where(['allot_code'=>$st['allot_code'],"is_del"=>0])->select();
  386. foreach ($vmp as $value){
  387. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_type_code'],"wsm_code"=>$st['wsm_out'],"is_del"=>0])->find();
  388. //var_dump($value['allot_num'],$stock['usable_stock']);
  389. if($value['allot_num']>$stock['usable_stock']){
  390. Db::rollback();
  391. return error_show(2000,"超出库存数量");
  392. }
  393. if($status==2){
  394. $stock['usable_stock']-=$value['allot_num'];
  395. $stock['wait_out_stock']+=$value['allot_num'];
  396. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  397. $stock['updatetime']= date('Y-m-d H:i:s');
  398. $Db = Db::name('good_stock')->update($stock);
  399. //var_dump(Db::name('good_stock')->getLastSql());
  400. if($Db==false){
  401. Db::rollback();
  402. return error_show(1002,"状态更新失败");
  403. }
  404. }
  405. }
  406. }
  407. Db::commit();
  408. return error_show(0,"状态更新成功");
  409. }
  410. Db::rollback();
  411. return error_show(1003,"状态更新失败");
  412. }catch (\Exception $e) {
  413. Db::rollback();
  414. return error_show(1005, $e->getMessage());
  415. }
  416. }
  417. /*出库方发货*/
  418. public function getont(){
  419. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  420. if(empty($allot_code)){
  421. return error_show(1002,"调拨单编号不能为空");
  422. }
  423. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  424. if($al==""){
  425. return error_show(1003,"调拨编号未找到");
  426. }
  427. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  428. if($post_name==""){
  429. return error_show(1002,"物流公司不能为空");
  430. }
  431. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  432. if($post_code==""){
  433. return error_show(1002,"物流单号不能为空");
  434. }
  435. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  436. if($post_fee===""){
  437. return error_show(1002,"物流费用不能为空");
  438. }
  439. Db::startTrans();
  440. try {
  441. $data=[
  442. "allot_code"=>$allot_code,
  443. "post_name"=>$post_name,
  444. "post_code"=>$post_code,
  445. "post_fee"=>$post_fee,
  446. "status"=>3,
  447. "is_del"=>0,
  448. "updatetime"=>date("Y-m-d H:i:s"),
  449. ];
  450. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code])->update($data);
  451. if($datainfo){
  452. $vnp = Db::name('allot_info')->where(['allot_code'=>$al['allot_code'],'is_del'=>0])->select();
  453. // var_dump(Db::name('allot_info')->getLastSql());
  454. foreach ($vnp as $value){
  455. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_type_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  456. if($stock['wait_out_stock']<$value['allot_num']){
  457. Db::rollback();
  458. return error_show(2000,"超出库存数量");
  459. }
  460. $stock['wait_out_stock']-=$value['allot_num'];
  461. $stock['intra_stock']+=$value['allot_num'];
  462. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  463. $stock['updatetime']= date('Y-m-d H:i:s');
  464. $dr = Db::name('good_stock')->update($stock);
  465. if($dr==false){
  466. Db::rollback();
  467. return error_show(1002,"发货更新失败");
  468. }
  469. }
  470. Db::commit();
  471. return error_show(0,"发货更新成功");
  472. }
  473. Db::rollback();
  474. return error_show(1003,"发货更新失败");
  475. }catch (\Exception $e) {
  476. Db::rollback();
  477. return error_show(1005, $e->getMessage());
  478. }
  479. }
  480. /*入库方验货*/
  481. public function getin(){
  482. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  483. if(empty($allot_code)){
  484. return error_show(1002,"调拨单编号不能为空");
  485. }
  486. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  487. if($al==""){
  488. return error_show(1003,"调拨编号未找到");
  489. }
  490. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  491. if($dain==""){
  492. return error_show(1002,"商品不能为空");
  493. }
  494. Db::startTrans();
  495. try {
  496. $data=[
  497. "allot_code"=>$allot_code,
  498. "is_del"=>0,
  499. "status"=>4,
  500. "updatetime"=>date("Y-m-d H:i:s"),
  501. ];
  502. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  503. if($datainfo>0) {
  504. foreach ($dain as $value) {
  505. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  506. ->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")
  507. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock,c.good_type_code")->find();
  508. if (empty($st)) {
  509. Db::rollback();
  510. return error_show(1003, "未找到商品");
  511. }
  512. $n = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code'],'is_del'=>0])->find();
  513. if(empty($n)){
  514. Db::rollback();
  515. return error_show(10032, "未找到数据");
  516. }
  517. // if($value['allot_num']>$st['usable_stock']){
  518. // return error_show(2000,"库存数量不足");
  519. // }
  520. $temp = [];
  521. $temp['error_code']=$value['error_code'];
  522. $temp['good_num']=$st['usable_stock'];
  523. $temp['usable_num'] = $value['usable_num'];
  524. $temp['error_num'] = $value['error_num'];
  525. $temp['error_remark'] = $value['error_remark'];
  526. $temp['stock_num']=$n['allot_num'];
  527. $temp['updatetime'] = date("Y-m-d H:i:s");
  528. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code'],'is_del'=>0])->save($temp);
  529. if($np==false){
  530. Db::rollback();
  531. return error_show(1001,"数据更新失败");
  532. }
  533. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  534. if($stock['intra_stock']<$n['allot_num']){
  535. Db::rollback();
  536. return error_show(2000,"超出库存数量");
  537. }
  538. $stock['updatetime']= date('Y-m-d H:i:s');
  539. $stock['intra_stock']-=$n['allot_num'];
  540. $dr = Db::name('good_stock')->update($stock);
  541. if($dr==false){
  542. Db::rollback();
  543. return error_show(1002,"数据更新失败");
  544. }
  545. $instock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  546. if($instock==false){
  547. $instock =[];
  548. $instock['good_type_code']=$value['good_code'];
  549. $instock['wsm_code']=$al['wsm_in'];
  550. $instock['wait_in_stock']=0;
  551. $instock['wait_out_stock']=0;
  552. $instock['usable_stock']=0;
  553. $instock['intra_stock']=0;
  554. $instock['total_stock']=0;
  555. $instock['status']=1;
  556. $instock['is_del']=0;
  557. $instock['warn_stock']=0;
  558. $instock['addtime']= date('Y-m-d H:i:s');
  559. }
  560. $instock['wait_in_stock']+=$n['allot_num'];
  561. $instock['updatetime']= date('Y-m-d H:i:s');
  562. $sk = Db::name('good_stock')->save($instock);
  563. // $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  564. if ($sk==false) {
  565. Db::rollback();
  566. return error_show(1003, "入库验货更新失败");
  567. }
  568. }
  569. Db::commit();
  570. return error_show(0, "入库验货更新成功");
  571. }
  572. Db::rollback();
  573. return error_show(1002, "入库验货更新失败");
  574. }catch (\Exception $e){
  575. Db::rollback();
  576. return error_show(1005,$e->getMessage());
  577. }
  578. }
  579. /*验货审核*/
  580. public function vesio(){
  581. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  582. if(empty($allot_code)){
  583. return error_show(1002,"调拨单编号不能为空");
  584. }
  585. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  586. if($al==""){
  587. return error_show(1003,"调拨编号未找到");
  588. }
  589. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  590. if($dain==""){
  591. return error_show(1002,"商品不能为空");
  592. }
  593. Db::startTrans();
  594. try {
  595. $data=[
  596. "allot_code"=>$allot_code,
  597. "status"=>5,
  598. "is_del"=>0,
  599. "updatetime"=>date("Y-m-d H:i:s"),
  600. ];
  601. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  602. if($datainfo>0) {
  603. foreach ($dain as $value) {
  604. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  605. ->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")
  606. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock")->find();
  607. if (empty($st)) {
  608. Db::rollback();
  609. return error_show(1003, "商品不能为空");
  610. }
  611. $temp = [];
  612. $temp['error_code']="";
  613. $temp['good_num']=$st['usable_stock'];
  614. $temp['error_num'] = $value['error_num'];
  615. $temp['stock_num'] = $value['stock_num'];
  616. $temp['error_remark'] = $value['error_remark'];
  617. $temp['updatetime'] = date("Y-m-d H:i:s");
  618. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code']])->save($temp);
  619. if($np==false){
  620. Db::rollback();
  621. return error_show(1001,"数据更新失败");
  622. }
  623. $p = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=>$al['allot_code'],"is_del"=>0])->find();
  624. if($p==""){
  625. Db::rollback();
  626. return error_show(1002,"未找到数据");
  627. }
  628. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  629. if($stock['wait_in_stock']<$p['stock_num']){
  630. Db::rollback();
  631. return error_show(2000,"超出待入库存数量");
  632. }
  633. $stock['wait_in_stock']-=$p['stock_num'];
  634. $stock['usable_stock']+=$p['stock_num'];
  635. $stock['updatetime']=date('Y-m-d H:i:s');
  636. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  637. $dr = Db::name('good_stock')->update($stock);
  638. if ($dr==false) {
  639. Db::rollback();
  640. return error_show(1004, "验货更新失败");
  641. }
  642. Db::commit();
  643. return error_show(0, "验货更新成功");
  644. }
  645. }
  646. Db::rollback();
  647. return error_show(1003, "验货更新失败");
  648. }catch (\Exception $e){
  649. Db::rollback();
  650. return error_show(1005,$e->getMessage());
  651. }
  652. }
  653. }