Allot.php 39 KB

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