Allot.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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']])->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']) ? $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']) ? '' :$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){
  385. $vmp = Db::name('allot_info')->where(['allot_code'=>$st['allot_code']])->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. $stock['usable_stock']-=$value['allot_num'];
  394. $stock['wait_out_stock']+=$value['allot_num'];
  395. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  396. $stock['updatetime']= date('Y-m-d H:i:s');
  397. $Db = Db::name('good_stock')->update($stock);
  398. //var_dump(Db::name('good_stock')->getLastSql());
  399. if($Db==false){
  400. Db::rollback();
  401. return error_show(1002,"状态更新失败");
  402. }
  403. }
  404. }
  405. Db::commit();
  406. return error_show(0,"状态更新成功");
  407. }
  408. Db::rollback();
  409. return error_show(1003,"状态更新失败");
  410. }catch (\Exception $e) {
  411. Db::rollback();
  412. return error_show(1005, $e->getMessage());
  413. }
  414. }
  415. /*出库方发货*/
  416. public function getont(){
  417. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  418. if(empty($allot_code)){
  419. return error_show(1002,"调拨单编号不能为空");
  420. }
  421. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  422. if($al==""){
  423. return error_show(1003,"调拨编号未找到");
  424. }
  425. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  426. if($post_name==""){
  427. return error_show(1002,"物流公司不能为空");
  428. }
  429. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  430. if($post_code==""){
  431. return error_show(1002,"物流单号不能为空");
  432. }
  433. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  434. if($post_fee===""){
  435. return error_show(1002,"物流费用不能为空");
  436. }
  437. Db::startTrans();
  438. try {
  439. $data=[
  440. "allot_code"=>$allot_code,
  441. "post_name"=>$post_name,
  442. "post_code"=>$post_code,
  443. "post_fee"=>$post_fee,
  444. "status"=>3,
  445. "is_del"=>0,
  446. "updatetime"=>date("Y-m-d H:i:s"),
  447. ];
  448. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code])->update($data);
  449. if($datainfo){
  450. $vnp = Db::name('allot_info')->where(['allot_code'=>$al['allot_code']])->select();
  451. // var_dump(Db::name('allot_info')->getLastSql());
  452. foreach ($vnp as $value){
  453. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_type_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  454. if($stock['wait_out_stock']<$value['allot_num']){
  455. Db::rollback();
  456. return error_show(2000,"超出库存数量");
  457. }
  458. $stock['wait_out_stock']-=$value['allot_num'];
  459. $stock['intra_stock']+=$value['allot_num'];
  460. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  461. $stock['updatetime']= date('Y-m-d H:i:s');
  462. $dr = Db::name('good_stock')->update($stock);
  463. if($dr==false){
  464. Db::rollback();
  465. return error_show(1002,"发货更新失败");
  466. }
  467. }
  468. Db::commit();
  469. return error_show(0,"发货更新成功");
  470. }
  471. Db::rollback();
  472. return error_show(1003,"发货更新失败");
  473. }catch (\Exception $e) {
  474. Db::rollback();
  475. return error_show(1005, $e->getMessage());
  476. }
  477. }
  478. /*入库方验货*/
  479. public function getin(){
  480. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  481. if(empty($allot_code)){
  482. return error_show(1002,"调拨单编号不能为空");
  483. }
  484. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  485. if($al==""){
  486. return error_show(1003,"调拨编号未找到");
  487. }
  488. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  489. if($dain==""){
  490. return error_show(1002,"商品不能为空");
  491. }
  492. Db::startTrans();
  493. try {
  494. $data=[
  495. "allot_code"=>$allot_code,
  496. "is_del"=>0,
  497. "status"=>4,
  498. "updatetime"=>date("Y-m-d H:i:s"),
  499. ];
  500. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  501. if($datainfo>0) {
  502. foreach ($dain as $value) {
  503. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  504. ->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")
  505. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock,c.good_type_code")->find();
  506. if (empty($st)) {
  507. Db::rollback();
  508. return error_show(1003, "未找到商品");
  509. }
  510. $n = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code']])->find();
  511. if(empty($n)){
  512. Db::rollback();
  513. return error_show(10032, "未找到数据");
  514. }
  515. // if($value['allot_num']>$st['usable_stock']){
  516. // return error_show(2000,"库存数量不足");
  517. // }
  518. $temp = [];
  519. $temp['error_code']=$value['error_code'];
  520. $temp['good_num']=$st['usable_stock'];
  521. $temp['usable_num'] = $value['usable_num'];
  522. $temp['error_num'] = $value['error_num'];
  523. $temp['error_remark'] = $value['error_remark'];
  524. $temp['stock_num']=$n['allot_num'];
  525. $temp['updatetime'] = date("Y-m-d H:i:s");
  526. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code']])->save($temp);
  527. if($np==false){
  528. Db::rollback();
  529. return error_show(1001,"数据更新失败");
  530. }
  531. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  532. if($stock['intra_stock']<$n['allot_num']){
  533. Db::rollback();
  534. return error_show(2000,"超出库存数量");
  535. }
  536. $stock['updatetime']= date('Y-m-d H:i:s');
  537. $stock['intra_stock']-=$n['allot_num'];
  538. $dr = Db::name('good_stock')->update($stock);
  539. if($dr==false){
  540. Db::rollback();
  541. return error_show(1002,"数据更新失败");
  542. }
  543. $instock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  544. if($instock==false){
  545. $instock =[];
  546. $instock['good_type_code']=$value['good_code'];
  547. $instock['wsm_code']=$al['wsm_in'];
  548. $instock['wait_in_stock']=0;
  549. $instock['wait_out_stock']=0;
  550. $instock['usable_stock']=0;
  551. $instock['intra_stock']=0;
  552. $instock['total_stock']=0;
  553. $instock['status']=1;
  554. $instock['is_del']=0;
  555. $instock['warn_stock']=0;
  556. $instock['addtime']= date('Y-m-d H:i:s');
  557. }
  558. $instock['wait_in_stock']+=$n['allot_num'];
  559. $instock['updatetime']= date('Y-m-d H:i:s');
  560. $sk = Db::name('good_stock')->save($instock);
  561. // $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  562. if ($sk==false) {
  563. Db::rollback();
  564. return error_show(1003, "入库验货更新失败");
  565. }
  566. }
  567. Db::commit();
  568. return error_show(0, "入库验货更新成功");
  569. }
  570. Db::rollback();
  571. return error_show(1002, "入库验货更新失败");
  572. }catch (\Exception $e){
  573. Db::rollback();
  574. return error_show(1005,$e->getMessage());
  575. }
  576. }
  577. /*验货审核*/
  578. public function vesio(){
  579. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  580. if(empty($allot_code)){
  581. return error_show(1002,"调拨单编号不能为空");
  582. }
  583. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  584. if($al==""){
  585. return error_show(1003,"调拨编号未找到");
  586. }
  587. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  588. if($dain==""){
  589. return error_show(1002,"商品不能为空");
  590. }
  591. Db::startTrans();
  592. try {
  593. $data=[
  594. "allot_code"=>$allot_code,
  595. "status"=>5,
  596. "is_del"=>0,
  597. "updatetime"=>date("Y-m-d H:i:s"),
  598. ];
  599. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  600. if($datainfo>0) {
  601. foreach ($dain as $value) {
  602. $st = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  603. ->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")
  604. ->field("b.type_code,a.good_name,c.wsm_code,c.usable_stock")->find();
  605. if (empty($st)) {
  606. Db::rollback();
  607. return error_show(1003, "商品不能为空");
  608. }
  609. $temp = [];
  610. $temp['error_code']="";
  611. $temp['good_num']=$st['usable_stock'];
  612. $temp['error_num'] = $value['error_num'];
  613. $temp['stock_num'] = $value['stock_num'];
  614. $temp['error_remark'] = $value['error_remark'];
  615. $temp['updatetime'] = date("Y-m-d H:i:s");
  616. $np = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=> $al['allot_code']])->save($temp);
  617. if($np==false){
  618. Db::rollback();
  619. return error_show(1001,"数据更新失败");
  620. }
  621. $p = Db::name('allot_info')->where(['good_type_code'=>$value['good_code'],'allot_code'=>$al['allot_code']])->find();
  622. if($p==""){
  623. Db::rollback();
  624. return error_show(1002,"未找到数据");
  625. }
  626. $stock = Db::name("good_stock")->where(["good_type_code"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  627. if($stock['wait_in_stock']<$p['stock_num']){
  628. Db::rollback();
  629. return error_show(2000,"超出待入库存数量");
  630. }
  631. $stock['wait_in_stock']-=$p['stock_num'];
  632. $stock['usable_stock']+=$p['stock_num'];
  633. $stock['updatetime']=date('Y-m-d H:i:s');
  634. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  635. $dr = Db::name('good_stock')->update($stock);
  636. if ($dr==false) {
  637. Db::rollback();
  638. return error_show(1004, "验货更新失败");
  639. }
  640. Db::commit();
  641. return error_show(0, "验货更新成功");
  642. }
  643. }
  644. Db::rollback();
  645. return error_show(1003, "验货更新失败");
  646. }catch (\Exception $e){
  647. Db::rollback();
  648. return error_show(1005,$e->getMessage());
  649. }
  650. }
  651. }