Allot.php 32 KB

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