Resign.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. <?php
  2. /**
  3. * 备库申请
  4. */
  5. namespace app\admin\controller;
  6. use app\admin\model\ActionLog;
  7. use app\admin\model\GoodLog;
  8. use app\admin\model\ProcessOrder;
  9. use think\facade\Cache;
  10. use think\facade\Config;
  11. use think\facade\Db;
  12. use think\App;
  13. //备货
  14. class Resign extends Base
  15. {
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. }
  20. public function list(){
  21. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  22. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  23. $where =[['p.is_del',"=",0]];
  24. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
  25. if($bkcode!=""){
  26. //$where['p.bk_code'] = $bk_code;
  27. $where[]=['p.bk_code',"like","%{$bkcode}%"];
  28. }
  29. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code']):"";
  30. if($good_code!=""){
  31. // $where['p.good_code'] = $good_code;
  32. $where[]=['p.spuCode',"like","%{$good_code}%"];
  33. }
  34. $good_type_code = isset($this->post['good_type_code']) && $this->post['good_type_code']!="" ? trim($this->post['good_type_code']):"";
  35. if($good_type_code!=""){
  36. // $where['p.good_type_code'] = $good_type_code;
  37. $where[]=['p.good_type_code',"like","%{$good_type_code}%"];
  38. }
  39. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  40. if($wsm_code!=""){
  41. //$where['p.wsm_code'] = $wsm_code;
  42. $where[]=['p.wsm_code',"=",$wsm_code];
  43. }
  44. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name']):"";
  45. if($good_name!=""){
  46. // $where['p.good_name'] = Db::raw(" like '%{$good_name}%'");
  47. $where[]=['p.good_name',"like","%{$good_name}%"];
  48. }
  49. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']):"";
  50. if($apply_name!=""){
  51. // $where['p.apply_name'] =Db::Raw("like '%{$apply_name}%'");
  52. $where[]=['p.apply_name',"like","%{$apply_name}%"];
  53. }
  54. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  55. if($status!==""){
  56. // $where['p.status'] = $status;
  57. $where[]=['p.status',"=",$status];
  58. }
  59. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  60. if($start!==""){
  61. //$where['p.addtime'] = Db::Raw(">= '{$start}'");
  62. $where[]=['p.addtime',">=",$start];
  63. }
  64. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  65. if($end!==""){
  66. // $where['p.addtime'] = Db::Raw("<= '{$end}'");
  67. $where[]=['p.addtime',"<=",$end];
  68. }
  69. $role=$this->checkRole();
  70. $condition='';
  71. if(!empty($role['write']) && $this->uid!=""){
  72. // $where[]=["a.apply_id","in",$role['write']];
  73. $condition .="p.cgderid = {$this->uid} or p.apply_id in (".implode(',',$role['write']).")";
  74. }
  75. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  76. if ($company_name !== "") $where[] = ["p.apply_id", 'in', get_company_item_user_by_name($company_name)];
  77. // if(!empty($role['platform']) ){
  78. // $where[]=["p.platform_id","in",$role['platform']];
  79. // }
  80. $count=Db::name("purchease")->alias('p')->leftJoin("depart_user u", "u.uid=p.apply_id AND u.is_del=0")->where($where)->where($condition)->count();
  81. $total = ceil($count/$size);
  82. $page = $page >= $total ? $total : $page;
  83. $list = Db::name("purchease")
  84. ->alias('p')
  85. ->field('p.*,u.itemid')
  86. ->leftJoin("depart_user u", "u.uid=p.apply_id AND u.is_del=0")
  87. ->where($where)
  88. ->where($condition)
  89. ->page($page,$size)
  90. ->order("p.addtime desc")
  91. ->cursor();
  92. $data=[];
  93. foreach ($list as $value){
  94. $value['wsm_name']="";
  95. $value['wsm_supplier']="";
  96. $value['wsm_supplierNo']="";
  97. if($value['wsm_code']!=""){
  98. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  99. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  100. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  101. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  102. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  103. }
  104. $value['file_name'] ='';
  105. if($value['file_url']){
  106. $file= basename($value['file_url']);
  107. $value['file_name'] =substr($file,0,strripos($file,"_"));
  108. }
  109. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  110. $data[]=$value;
  111. }
  112. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  113. }
  114. public function add(){
  115. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  116. if($token==""){
  117. return error_show(101,'token不能为空');
  118. }
  119. $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  120. if($good_code==""){
  121. return error_show(1004,"参数good_code不能为空");
  122. }
  123. $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  124. if($good_num===""){
  125. return error_show(1004,"参数good_code不能为空");
  126. }
  127. $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  128. if($file_url===""){
  129. return error_show(1004,"参数file_url不能为空");
  130. }
  131. $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  132. if($mark==""){
  133. return error_show(1004,"参数remark不能为空");
  134. }
  135. $good =Db::name("good_basic")->where([["spuCode","=",$good_code],["is_del","=",0]])->find();
  136. if($good==false){
  137. return error_show(1004,"未找到商品数据");
  138. }
  139. $supplier=Db::name("supplier")->where("code","=",$good['supplierNo'])->find();
  140. if($supplier==false) return error_show(1004,"未找到供应商数据");
  141. // $bk= Db::name("purchease")->where([["spuCode","=",$good_code],["status","<>",2],["is_del","=",0]])->find();
  142. // if($bk!=false) return error_show(1004,"存在未完成备库单数据");
  143. // $cgd =Db::name("purchease_order")->where([["spuCode","=",$good_code],["status","in",[0,1]],["is_del","=",0]])->find();
  144. // if($cgd!=false) return error_show(1004,"存在未完成采购单数据");
  145. //查验仓库库存数量
  146. $stock =Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")->where
  147. (["spuCode"=>$good_code, "a.is_del"=>0,"a.status"=>1,"b.companyNo"=>$good['companyNo']])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  148. // if(isset($stock['usable_stock'])&& $stock['usable_stock']!=0){
  149. // return error_show(1003,"库存数量为 {$stock['usable_stock']} 无法备库");
  150. // }
  151. //校验数量
  152. $goodnake = Db::name("good_nake")
  153. ->field('id,min_num')
  154. ->where([["spuCode", "=", $good_code], ["is_del", "=", 0]])
  155. ->order("min_num asc")
  156. ->find();
  157. if (empty($goodnake)) return error_show(1004, '未找到商品成本数据');
  158. $minnum = $goodnake['min_num'];
  159. if($good['is_stock']==1 && $good['stock_moq']>0){
  160. $minnum =$good['stock_moq'];
  161. }
  162. if ($minnum > $good_num) return error_show(1004, '该商品最低采购数量为' . $minnum);
  163. $lastime = isset($this->post['lastime'])&&$this->post['lastime']!="" ? $this->post['lastime']:"";
  164. if($lastime===""){
  165. return error_show(1004,"参数lastime不能为空");
  166. }
  167. $data = GetUserInfo($token);
  168. if((!empty($data) && $data['code']!=0) ||empty($data) ){
  169. return error_show($data['code'],$data['message']);
  170. }
  171. $userinfo = $data['data'];
  172. $bk_code=makeNo("BK");
  173. $info = [
  174. "spuCode"=>$good['spuCode'],
  175. "good_name"=>$good['good_name'],
  176. "good_num"=>$good_num,
  177. "wsm_code"=>'',
  178. "companyNo"=>$good['companyNo'],
  179. "supplierNo"=>$good['supplierNo'],
  180. "file_url"=>$file_url,
  181. "lasttime"=>$lastime,
  182. "status"=>1,
  183. "remark"=>$mark,
  184. "cgder"=>$good['creater'],
  185. "cgderid"=>$good['createrid'],
  186. "addtime"=>date("Y-m-d H:i:s"),
  187. "updatetime"=>date("Y-m-d H:i:s"),
  188. "apply_id"=>$userinfo['id'],
  189. "apply_name"=>$userinfo['nickname'],
  190. "bk_code"=>$bk_code
  191. ];
  192. $in = Db::name("purchease")->insert($info,true);
  193. if($in>0){
  194. $order=["order_type"=>'BHD',"order_code"=>$bk_code,"order_id"=>$in,"order_status"=>1,"before_status"=>1, 'holder_id' => $info['cgderid']];
  195. ProcessOrder::AddProcess($token,$order);
  196. $ste = ["order_code"=>$bk_code,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  197. ActionLog::logAdd($token,$ste,"BHD",1,$info);
  198. //台账记录
  199. Db::name('standing_book')
  200. ->insert([
  201. 'standBookNo' => makeNo("IO"),
  202. 'bk_code' => $bk_code,
  203. 'purchease_id' => $in,
  204. 'addtime' => date('Y-m-d H:i:s'),
  205. 'updatetime' => date('Y-m-d H:i:s')
  206. ]);
  207. return app_show(0,"新建成功",['bk_code'=>$bk_code]);
  208. }else{
  209. return error_show(1005,"新建失败");
  210. }
  211. }
  212. /**
  213. * @return \think\response\Json|void
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. */
  218. public function copy(){
  219. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  220. if($id==""){
  221. return error_show(1001,'id不能为空');
  222. }
  223. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  224. if(empty($info)){
  225. return error_show(1002,'未找到数据');
  226. }
  227. $data = GetUserInfo($this->post['token']);
  228. if((!empty($data) && $data['code']!=0) ||empty($data) ){
  229. return error_show($data['code'],$data['message']);
  230. }
  231. //查验仓库库存数量
  232. // $stock =Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")->where
  233. // (["spuCode"=>$info['spuCode'], "a.is_del"=>0,"a.status"=>1,"b.companyNo"=>$info['companyNo']])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  234. // if(isset($stock['usable_stock'])&& $stock['usable_stock']!=0){
  235. // return error_show(1003,"库存数量为 {$stock['usable_stock']} 无法备库");
  236. // }
  237. $userinfo = $data['data'];
  238. $bk_code=makeNo("BK");
  239. $info['bk_code']=$bk_code;
  240. $info['status']=1;
  241. $info['addtime']=date("Y-m-d H:i:s");
  242. $info['updatetime']=date("Y-m-d H:i:s");
  243. $info['apply_id']=$userinfo['id'];
  244. $info['apply_name']=$userinfo['nickname'];
  245. unset($info['id']);
  246. $in = Db::name("purchease")->insert($info,true);
  247. if($in>0){
  248. $order=["order_type"=>'BHD',"order_code"=>$bk_code,"order_id"=>$in,"order_status"=>1,"before_status"=>1, 'holder_id' => $info['apply_id']];
  249. ProcessOrder::AddProcess($this->post['token'],$order);
  250. $ste = ["order_code"=>$bk_code,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  251. ActionLog::logAdd($this->post['token'],$ste,"BHD",1,$info);
  252. return app_show(0,"新建成功",['bk_code'=>$bk_code]);
  253. }else{
  254. return error_show(1005,"新建失败");
  255. }
  256. }
  257. /**
  258. * @return \think\response\Json|void
  259. * @throws \think\db\exception\DataNotFoundException
  260. * @throws \think\db\exception\DbException
  261. * @throws \think\db\exception\ModelNotFoundException
  262. */
  263. public function info(){
  264. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  265. if($id==""){
  266. return error_show(1001,'id不能为空');
  267. }
  268. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  269. if(empty($info)){
  270. return error_show(1002,'未找到数据');
  271. }
  272. $good =Db::name("good_basic")->where([["spuCode","=", $info['spuCode']],["is_del","=",0]])->find();
  273. if($good==false){
  274. return error_show(1004,"未找到商品数据");
  275. }
  276. $int= isset($good['cat_id']) && $good['cat_id'] !=0 ? made($good['cat_id']):[];
  277. $metal = \think\facade\Config::get("noble");
  278. $info['noble_metal']= isset($metal[$good['noble_metal']]) && $good['noble_metal'] !=''?$metal[$good['noble_metal']]:'';
  279. $info['noble_id']= isset($good['noble_metal']) && $good['noble_metal'] !='' ? $good['noble_metal']:"";
  280. $info['wsm_name']="";
  281. $info['file_name'] ='';
  282. if($info['file_url']){
  283. $file= basename($info['file_url']);
  284. $info['file_name'] =substr($file,0,strripos($file,"_"));
  285. }
  286. if($info['wsm_code']!=""){
  287. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  288. ->where(["a.wsm_code"=>$info['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  289. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  290. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  291. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  292. }
  293. $supplier =Db::name("supplier")->where(["code"=>$info['supplierNo']])->find();
  294. $info['supplier_name'] = isset($supplier['name'])?$supplier['name']:"";
  295. $company =Db::name("business")->where(["companyNo"=>$info['companyNo']])->find();
  296. $info['company_name'] = isset($company['company'])?$company['company']:"";
  297. $info['can']=$int;
  298. return app_show(0,"获取成功",$info);
  299. }
  300. public function edit(){
  301. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  302. if($token==""){
  303. return error_show(101,'token不能为空');
  304. }
  305. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  306. if($id==""){
  307. return error_show(1001,'id不能为空');
  308. }
  309. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  310. if(empty($info)){
  311. return error_show(1002,'未找到数据');
  312. }
  313. $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  314. if($customer==""){
  315. return error_show(1004,"参数customer_code不能为空");
  316. }
  317. $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  318. if($supplier==""){
  319. return error_show(1004,"参数supplier_code不能为空");
  320. }
  321. $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  322. if($good_code==""){
  323. return error_show(1004,"参数good_code不能为空");
  324. }
  325. $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  326. if($good_num===""){
  327. return error_show(1004,"参数good_code不能为空");
  328. }
  329. $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  330. if($file_url===""){
  331. return error_show(1004,"参数file_url不能为空");
  332. }
  333. $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  334. if($mark==""){
  335. return error_show(1004,"参数remark不能为空");
  336. }
  337. $good =Db::name("good_basic")->where([["spuCode","=", $good_code],["is_del","=",0]])->find();
  338. if($good==false){
  339. return error_show(1004,"未找到商品数据");
  340. }
  341. $data = GetUserInfo($token);
  342. if((!empty($data) && $data['code']!=0) ||empty($data) ){
  343. return error_show($data['code'],$data['message']);
  344. }
  345. $userinfo = $data['data'];
  346. $up = [
  347. "spuCode"=>$good_code,
  348. "good_name"=>$good['good_name'],
  349. "good_num"=>$good_num,
  350. // "skuCode"=>$good['skuCode'],
  351. "companyNo"=>$customer,
  352. "supplierNo"=>$supplier,
  353. "file_url"=>$file_url,
  354. "remark"=>$mark,
  355. "updatetime"=>date("Y-m-d H:i:s"),
  356. ];
  357. $in = Db::name("purchease")->where($info)->save($up);
  358. if($in){
  359. $order=["order_type"=>'BHD',"order_code"=>$info['bk_code'],"order_id"=>$info['id'],"order_status"=>$info['status'],"before_status"=>1, 'holder_id' => $info['apply_id']];
  360. ProcessOrder::AddProcess($this->post['token'],$order);
  361. $ste = ["order_code"=>$info['bk_code'],"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"];
  362. ActionLog::logAdd($this->post['token'],$ste,"BHD",$info['status'],$up);
  363. return app_show(0,"更新成功",['bk_code'=>$info['bk_code']]);
  364. }else{
  365. return error_show(1005,"更新失败");
  366. }
  367. }
  368. public function delete(){
  369. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  370. if($id==""){
  371. return error_show(1001,'id不能为空');
  372. }
  373. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  374. if(empty($info)){
  375. return error_show(1002,'未找到数据');
  376. }
  377. $info['is_del']=1;
  378. $info['updatetime']=date("Y-m-d H:i:s");
  379. $up = Db::name("purchease")->update($info);
  380. if($up){
  381. $order=["order_type"=>'BHD',"order_code"=>$info['bk_code'],"order_id"=>$info['id'],"order_status"=>$info['status']];
  382. ProcessOrder::workdel($order);
  383. $ste = ["order_code"=>$info['bk_code'],"status"=>$info['status'],"action_remark"=>'',"action_type"=>"delete"];
  384. ActionLog::logAdd($this->post['token'],$ste,"BHD",$info['status'],$info);
  385. }
  386. return $up ? app_show(0,"删除成功"):error_show(1005,"删除失败");
  387. }
  388. public function status(){
  389. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  390. if($id==""){
  391. return error_show(1001,'id不能为空');
  392. }
  393. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  394. if(empty($info)){
  395. return error_show(1002,'未找到数据');
  396. }
  397. $status = isset($this->post['status'])&&$this->post['status']!=='' ? intval($this->post['status']) : "";
  398. if($status===""){
  399. return error_show(1001,'status不能为空');
  400. }
  401. $temp = $info['status'];
  402. $info['status']= $status;
  403. $info['updatetime']=date("Y-m-d H:i:s");
  404. $up = Db::name("purchease")->update($info);
  405. if($up){
  406. $order = ["order_type" => 'BHD', "order_code" => $info['bk_code'], "order_id" => $info['id'], "order_status" => $status, "before_status" => $temp, 'holder_id' => $info['apply_id']];
  407. ProcessOrder::AddProcess($this->post['token'],$order);
  408. $ste = ["order_code"=>$info['bk_code'],"status"=> $temp,"action_remark"=>'',"action_type"=>"status"];
  409. ActionLog::logAdd($this->post['token'],$ste,"BHD",$status,$info);
  410. return app_show(0,"更新成功",['bk_code'=>$info['bk_code']]);
  411. }else{
  412. return error_show(1005,"更新失败");
  413. }
  414. }
  415. public function addFeed(){
  416. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  417. if($token==""){
  418. return error_show(101,'token不能为空');
  419. }
  420. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  421. if($id==""){
  422. return error_show(1001,'id不能为空');
  423. }
  424. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  425. if(empty($info)){
  426. return error_show(1002,'未找到备库数据');
  427. }
  428. if($info['status']!=1){
  429. return error_show(1002,'备库数据状态有误');
  430. }
  431. $num = isset($this->post['num']) && $this->post['num']!=="" ? intval($this->post['num']):"";
  432. if($num==''){
  433. return error_show(1002,'参数num 不能为空或零');
  434. }
  435. $expecttime = isset($this->post['expecttime'])&&$this->post['expecttime']!=''?$this->post['expecttime'] :"";
  436. if($expecttime==''){
  437. return error_show(1002,'参数expecttime 不能为空');
  438. }
  439. $wsm_type = isset($this->post['wsm_type'])&&$this->post['wsm_type']!="" ? intval($this->post['wsm_type']):"";
  440. if($wsm_type==""){
  441. return error_show(1002,'参数wsm_type 不能为空');
  442. }
  443. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  444. if($wsm_code==""){
  445. return error_show(1002,'参数wsm_code 不能为空');
  446. }
  447. $wsminfo = Db::name("warehouse_info")->where(['wsm_code'=>$wsm_code,"wsm_type"=>$wsm_type,"is_del"=>0])->find();
  448. if(empty($wsminfo)){
  449. return error_show(1002,'未找到仓库数据');
  450. }
  451. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!="" ? trim($this->post['supplierNo']):"";
  452. if($supplierNo==""){
  453. return error_show(1002,'参数supplierNo 不能为空');
  454. }
  455. $supplie = Db::name("supplier")->where(['code'=>$supplierNo,"is_del"=>0])->find();
  456. if(empty($supplie)){
  457. return error_show(1002,'未找到供应商');
  458. }
  459. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=="" ? $this->post['good_price']:"";
  460. if($good_price===""){
  461. return error_show(1002,'参数good_price 不能为空');
  462. }
  463. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=="" ? $this->post['pakge_fee']:0;
  464. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=="" ? $this->post['cert_fee']:0;
  465. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=="" ? $this->post['open_fee']:0;
  466. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=="" ? $this->post['delivery_fee']:0;
  467. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=="" ? $this->post['mark_fee']:0;
  468. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=="" ? $this->post['teach_fee']:0;
  469. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=="" ? $this->post['nake_fee']:0;
  470. $demo_fee = isset($this->post['demo_fee'])&&$this->post['demo_fee']!=="" ? $this->post['demo_fee']:0;
  471. $weight = isset($this->post['weight'])&&$this->post['weight']!=="" ? $this->post['weight']:0;
  472. $diff_weight =isset($this->post['diff_weight'])&&$this->post['diff_weight']!=="" ? $this->post['diff_weight']:0;
  473. $diff_price =isset($this->post['diff_price'])&&$this->post['diff_price']!=="" ? $this->post['diff_price']:0;
  474. $remark =isset($this->post['remark'])&&$this->post['remark']!=="" ? $this->post['remark']:'';
  475. $data = GetUserInfo($token);
  476. if((!empty($data) && $data['code']!=0) ||empty($data) ){
  477. return error_show($data['code'],$data['message']);
  478. }
  479. $userinfo = $data['data'];
  480. $data=[
  481. "bkcode"=>$info['bk_code'],
  482. "warehouse"=>$wsm_code,
  483. "wsm_type"=>$wsm_type,
  484. "num"=>$num,
  485. 'expecttime'=>$expecttime,
  486. 'supplier'=>isset($supplie) && $supplie['code']!=''? $supplie['code']:'',
  487. 'good_price'=>$good_price,
  488. 'pakge_fee'=>$pakge_fee,
  489. 'cert_fee'=>$cert_fee,
  490. 'open_fee'=>$open_fee,
  491. 'mark_fee'=>$mark_fee,
  492. 'teach_fee'=>$teach_fee,
  493. 'nake_fee'=>$nake_fee,
  494. 'demo_fee'=>$demo_fee,
  495. 'delivery_fee'=>$delivery_fee,
  496. 'weight'=>$weight,
  497. 'remark'=>$remark,
  498. 'diff_weight'=>$diff_weight,
  499. 'diff_price'=>$diff_price,
  500. 'purchaser'=>$userinfo['nickname'],
  501. 'purchaser_id'=>$userinfo['id'],
  502. "addtime"=>date("Y-m-d H:i:s"),
  503. "updatetime"=>date("Y-m-d H:i:s")
  504. ];
  505. Db::startTrans();
  506. try{
  507. $in = Db::name('purchease_feedback')->insert($data,true);
  508. if($in>0){
  509. $order=["order_type"=>'FKD',"order_code"=>$info['bk_code'],"order_id"=>$in, "order_status"=>0,"before_status"=>1];
  510. ProcessOrder::AddProcess($this->post['token'],$order);
  511. $ste = ["order_code"=>$info['bk_code'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  512. ActionLog::logAdd($this->post['token'],$ste,"FKD",0,$data);
  513. $temp=$info['status'];
  514. $info['status']=2;
  515. $info['wsm_code']=$wsm_code;
  516. $info['addtime']=date("Y-m-d H:i:s");
  517. $ood=Db::name('purchease')->save($info);
  518. if($ood){
  519. $order=["order_type"=>'BHD',"order_code"=>$info['bk_code'],"order_id"=>$info['id'], "order_status"=>$info['status'],"before_status"=>$temp, 'holder_id' => $info['apply_id']];
  520. ProcessOrder::AddProcess($this->post['token'],$order);
  521. $ste = ["order_code"=>$info['bk_code'],"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
  522. ActionLog::logAdd($this->post['token'],$ste,"BHD",$info['status'],$info);
  523. Db::commit();
  524. return app_show(0,"新建成功");
  525. }
  526. }
  527. Db::rollback();
  528. return error_show(1005,"新建失败");
  529. }catch (\Exception $e){
  530. Db::rollback();
  531. return error_show(1005,"新建失败");
  532. }
  533. // return $in ? app_show(0,"新建成功"):error_show(1005,"新建失败");
  534. }
  535. public function delFeed(){
  536. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  537. if($id==""){
  538. return error_show(1001,'id不能为空');
  539. }
  540. $feed = Db::name("purchease_feedback")->where(['id'=>$id,'is_del'=>0])->find();
  541. if(empty($feed)){
  542. return error_show(1004,'未找到数据');
  543. }
  544. if($feed['status']==1){
  545. return error_show(1004,'反馈数据已采用无法删除');
  546. }
  547. $feed['is_del']= 1;
  548. $feed['updatetime']=date("Y-m-d H:i:s");
  549. $up = Db::name("purchease_feedback")->update($feed);
  550. if($up){
  551. $order=["order_type"=>'FKD',"order_code"=>$feed['bk_code'],"order_id"=>$feed['id'], "order_status"=>0];
  552. ProcessOrder::workdel($order);
  553. $ste = ["order_code"=>$feed['bk_code'],"status"=>$feed['status'],"action_remark"=>'',"action_type"=>"delete"];
  554. ActionLog::logAdd($this->post['token'],$ste,"FKD",$feed['status'],$feed);
  555. }
  556. return $up ? app_show(0,"删除成功"):error_show(1005,"删除失败");
  557. }
  558. public function infoFeed(){
  559. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  560. if($id==""){
  561. return error_show(1001,'id不能为空');
  562. }
  563. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  564. if(empty($info)){
  565. return error_show(1002,'未找到备库数据');
  566. }
  567. $feed = Db::name("purchease_feedback")->where(['bkcode'=>$info['bk_code'],'is_del'=>0])->find();
  568. if(empty($feed)){
  569. return error_show(1004,'未找到反馈数据');
  570. }
  571. $feed['wsm_name']="";
  572. if($feed['warehouse']!=""){
  573. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  574. ->where(["a.wsm_code"=>$feed['warehouse']])->field("a.name as wsm_name,b.name,b.code")->find();
  575. $feed['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  576. $feed['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  577. $feed['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  578. }
  579. if($feed['supplier']!=""){
  580. $supplie = Db::name("supplier")->where(['code'=>$feed['supplier'],"is_del"=>0])->find();
  581. $feed['supplier_name'] =isset($supplie['name']) ? $supplie['name']:"";
  582. }
  583. return app_show(0,"获取成功",$feed);
  584. }
  585. public function changeFeed(){
  586. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  587. if($id==""){
  588. return error_show(1001,'id不能为空');
  589. }
  590. $feed = Db::name("purchease")->where(['id'=>$id,'is_del'=>0])->find();
  591. if(empty($feed)){
  592. return error_show(1004,'未找到数据');
  593. }
  594. $status = isset($this->post['status'])&& $this->post['status']!=='' ? intval($this->post['status']) : "";
  595. if($status==""){
  596. return error_show(1001,'参数status 不能为空');
  597. }
  598. $remark = isset($this->post['remark'])&& $this->post['remark']!=='' ? trim($this->post['remark']) : "";
  599. Db::startTrans();
  600. try{
  601. $tem =$feed['status'];
  602. $feed['remark']= $remark;
  603. $feed['status']= $status;
  604. $feed['updatetime']=date("Y-m-d H:i:s");
  605. $up = Db::name("purchease")->update($feed);
  606. if($up){
  607. $order=["order_type"=>'BHD',"order_code"=>$feed['bk_code'],"order_id"=>$feed['id'], "order_status"=>$feed['status'],"before_status"=>$tem, 'holder_id' => $feed['apply_id']];
  608. ProcessOrder::AddProcess($this->post['token'],$order);
  609. $ste = ["order_code"=>$feed['bk_code'],"status"=>$tem,"action_remark"=>'',"action_type"=>"edit"];
  610. ActionLog::logAdd($this->post['token'],$ste,"BHD",$feed['status'],$feed);
  611. if($status==3){
  612. // $ware = Db::name("warehouse_addr")->where(["wsm_code"=>$feed['warehouse'],"is_del"=>0])->find();
  613. $purse = Db::name("purchease_feedback")->where(['bkcode'=>$feed['bk_code'],"is_del"=>0])->find();
  614. if(empty($purse)){
  615. Db::rollback();
  616. return error_show(1001,'未找到备库反馈数据');
  617. }
  618. $pe = $purse['status'];
  619. $purse['status']=1;
  620. $purse['addtime']=date("Y-m-d H:i:s");
  621. $up = Db::name("purchease_feedback")->save($purse);
  622. if($up==false){
  623. Db::rollback();
  624. return error_show(1001,'备库反馈数据状态修改失败');
  625. }
  626. $order=["order_type"=>'FKD',"order_code"=>$feed['bk_code'],"order_id"=>$purse['id'],"order_status"=>$purse['status'],"before_status"=>$pe];
  627. ProcessOrder::AddProcess($this->post['token'],$order);
  628. $ste = ["order_code"=>$feed['bk_code'],"status"=>$pe,"action_remark"=>'',"action_type"=>"status"];
  629. ActionLog::logAdd($this->post['token'],$ste,"FKD",$feed['status'],$purse);
  630. $supplier = Db::name("supplier")->where(["code"=>$purse['supplier']])->find();
  631. $inw = makeNo("CG");
  632. $goodinfo=Db::name('good_basic')
  633. ->field('id,createrid,creater')
  634. ->where(['is_del'=>0,'spuCode'=>$feed['spuCode']])
  635. ->findOrEmpty();
  636. $data=[
  637. "bkcode"=>$purse['bkcode'],
  638. 'cgdNo'=>$inw,
  639. "wsm_code"=>$purse['warehouse'],
  640. "good_code"=>$feed['good_code'],
  641. "good_name"=>$feed['good_name'],
  642. "good_type_code"=>$feed['good_type_code'],
  643. "good_num"=>$feed['good_num'],
  644. "send_num"=>0,
  645. "wsend_num"=>$feed['good_num'],
  646. 'good_price'=>$purse['good_price'],
  647. "total_fee"=>$purse['good_price'],
  648. 'pakge_fee'=>$purse['pakge_fee'],
  649. 'cert_fee'=>$purse['cert_fee'],
  650. 'open_fee'=>$purse['open_fee'],
  651. 'mark_fee'=>$purse['mark_fee'],
  652. 'teach_fee'=>$purse['teach_fee'],
  653. 'nake_fee'=>$purse['nake_fee'],
  654. 'demo_fee'=>$purse['demo_fee'],
  655. 'delivery_fee'=>$purse['delivery_fee'],
  656. 'weight'=>$purse['weight'],
  657. 'diff_weight'=>$purse['diff_weight'],
  658. 'diff_fee'=>$purse['diff_price'],
  659. 'cgder_id'=>$purse['purchaser_id'],
  660. 'cgder'=>$purse['purchaser'],
  661. "remark"=>$purse['remark'],
  662. "status"=>0,
  663. 'supplierNo'=>$purse['supplier'],
  664. 'supplier_name'=>isset($supplier['name']) ? $supplier['name']:"",
  665. 'lasttime'=>$purse['expecttime'],
  666. "addtime"=>date("Y-m-d H:i:s"),
  667. "updatetime"=>date("Y-m-d H:i:s"),
  668. 'good_createrid' => $goodinfo['createrid'],
  669. 'good_creater' => $goodinfo['creater'],//商品的创建人
  670. ];
  671. $in= Db::name("purchease_order")->insert($data,true);
  672. if($in>0){
  673. $order=["order_type"=>'CGD',"order_code"=>$data['cgdNo'],"order_id"=>$in, "order_status"=>$data['status'],"before_status"=>0];
  674. ProcessOrder::AddProcess($this->post['token'],$order);
  675. $ste = ["order_code"=>$inw,"status"=>$data['status'],"action_remark"=>'',"action_type"=>"create"];
  676. ActionLog::logAdd($this->post['token'],$ste,"CGD",$data['status'],$data);
  677. //将采购单数据塞入到队列中
  678. $push_data = json_encode([
  679. 'supplierNo' => $data['supplierNo'],
  680. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  681. 'data' => [
  682. 'cgdNo' => $data['cgdNo'],
  683. 'spuCode' => $feed['spuCode'],
  684. 'good_name' => $data['good_name'],
  685. 'good_num' => $data['good_num'],
  686. 'good_price' => $data['good_price'],
  687. 'total_fee' => $data['total_fee'],
  688. 'weight' => $data['weight'],
  689. 'addtime' => $data['addtime'],
  690. ],
  691. ], JSON_UNESCAPED_UNICODE);
  692. Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  693. Db::commit();
  694. return app_show(0,"更新成功",['cgdNo'=>$inw]);
  695. }
  696. }else{
  697. if($status==4){
  698. $purse = Db::name("purchease_feedback")->where(['bkcode'=>$feed['bk_code'],"is_del"=>0])->find();
  699. if(empty($purse)){
  700. Db::rollback();
  701. return error_show(1001,'未找到备库反馈数据');
  702. }
  703. $pe=$purse['status'];
  704. $purse['status']=2;
  705. $purse['addtime']=date("Y-m-d H:i:s");
  706. $up = Db::name("purchease_feedback")->save($purse);
  707. if($up==false){
  708. Db::rollback();
  709. return error_show(1001,'备库反馈数据状态修改失败');
  710. }
  711. $order=["order_type"=>'FKD',"order_code"=>$feed['bk_code'],"order_id"=>$purse['id'],"order_status"=>$purse['status'],"before_status"=>1];
  712. ProcessOrder::AddProcess($this->post['token'],$order);
  713. $ste = ["order_code"=>$feed['bk_code'],"status"=>$pe,"action_remark"=>'',"action_type"=>"status"];
  714. ActionLog::logAdd($this->post['token'],$ste,"FKD",$feed['status'],$purse);
  715. }
  716. Db::commit();
  717. return app_show(0,"更新成功");
  718. }
  719. }
  720. Db::rollback();
  721. return error_show(1004,"更新失败");
  722. }catch (\Exception $e){
  723. Db::rollback();
  724. return error_show(1005,$e->getMessage());
  725. }
  726. }
  727. public function addwsm(){
  728. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  729. if($id==""){
  730. return error_show(1001,'id不能为空');
  731. }
  732. $info = Db::name("purchease")->where(["id"=>$id,"is_del"=>0])->find();
  733. if($info==false){
  734. return error_show(1002,'未找到备库数据');
  735. }
  736. $wsm= isset($this->post['wsm_code'])&&$this->post['wsm_code']!="" ? trim($this->post['wsm_code']) : "";
  737. if($wsm==""){
  738. return error_show(1004,"参数wsm_code不能为空");
  739. }
  740. $wsminfo = Db::name("warehouse_info")->where(["wsm_code"=>$wsm,"is_del"=>0])->find();
  741. if($wsminfo==false){
  742. return error_show(1004,"未找到仓库数据");
  743. }
  744. $info['wsm_code']=$wsm;
  745. $old_info_status = $info['status'];
  746. $info['status']=1;
  747. $info['updatetime']=date("y-m-d H;i:s");
  748. $up =Db::name("purchease")->save($info);
  749. if($up){
  750. //修改状态,添加待办
  751. ActionLog::logAdd($this->post['token'], [
  752. "order_code" => $info['bk_code'],//销售单code
  753. "status" => $old_info_status,//这里的status是之前的值
  754. "action_remark" => '',//备注
  755. "action_type" => "edit"//新建create,编辑edit,更改状态status
  756. ], "BHD", $info['status'], $info);
  757. ProcessOrder::AddProcess($this->post['token'], [
  758. "order_type" => 'BHD',
  759. "order_code" => $info['bk_code'],//销售单code
  760. "order_id" => $info['id'],
  761. "order_status" => $info['status'],
  762. "before_status"=>$old_info_status,
  763. 'holder_id' => $info['apply_id']
  764. ]);
  765. return app_show(0,"备库单仓库选择成功");
  766. }else{
  767. return error_show(1004,"备库单仓库选择失败");
  768. }
  769. }
  770. //备库单生成采购单
  771. public function createCgd(){
  772. $bk_code=isset($this->post['bk_code'])&&$this->post['bk_code']!=="" ? trim($this->post['bk_code']):"";
  773. if($bk_code===""){
  774. return error_show(1004,"参数bk_code不能为空");
  775. }
  776. $info = Db::name("purchease")->where(["bk_code"=>$bk_code,"is_del"=>0])->find();
  777. if($info==false){
  778. return error_show(1002,'未找到备库数据');
  779. }
  780. $wsm_code =isset($this->post['wsm_code'])&&$this->post['wsm_code']!=="" ? trim($this->post['wsm_code']):'';
  781. if($wsm_code==""){
  782. return error_show(1002,'备库仓库不能为空');
  783. }
  784. $wsminfo = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  785. if($wsminfo==false){
  786. return error_show(1002,'备库仓库未找到');
  787. }
  788. $good =Db::name("good_basic")->where([["spuCode","=",$info['spuCode']],["is_del","=",0]])->find();
  789. if($good==false){
  790. return error_show(1004,"未找到商品数据");
  791. }
  792. $supplier=Db::name("supplier")->where(["code"=>$info['supplierNo'],"is_del"=>0])->find();
  793. if($supplier==false){
  794. return error_show(1004,"未找到供应商数据");
  795. }
  796. $goodnake =Db::name("good_nake")->where([["spuCode","=",$info['spuCode']],["is_del","=",0],["min_num","<=",
  797. $info['good_num']]])->order("min_num desc")->find();
  798. if($goodnake==false){
  799. return error_show(1004,"未找到成本数据");
  800. }
  801. $top_cat = made($good['cat_id']);
  802. $top_cat_id = isset($top_cat[0]['id'])?$top_cat[0]['id']:"";
  803. if($good['is_gold_price']==1 && $top_cat_id==6){
  804. $gold = Db::name("gold_price1")->where(["type"=>$good['noble_metal'],"is_del"=>0,"status"=>1])->order("addtime desc")
  805. ->find();
  806. // $weight = $good['noble_weight']*$info['good_num'];
  807. $sale_price = round($good['demo_fee'] / $info['good_num'] + $good['open_fee'] / $info['good_num'] + $good['noble_weight'] * $gold["price"] + $goodnake['cost_fee'] * $good['noble_weight'] + $goodnake['package_fee'] + $goodnake['mark_fee'] + $goodnake['cert_fee'] +$goodnake['nake_fee'] + $goodnake['delivery_fee'],2);
  808. $total_price = round($info['good_num']*$sale_price,2);
  809. }else{
  810. $sale_price = $goodnake['nake_total'];
  811. $total_price = round($info['good_num']*$sale_price,2);
  812. }
  813. $cgdCode = makeNo("CG");
  814. $cg =["cgdNo"=>$cgdCode,
  815. "bkcode"=>$bk_code,
  816. "wsm_code"=>$wsm_code,
  817. "cgder"=>$good['creater'],
  818. "cgder_id"=>$good['createrid'],
  819. "spuCode"=>$info['spuCode'],
  820. "good_name"=>$info['good_name'],
  821. "good_num"=>$info['good_num'],
  822. "good_price"=>$sale_price,
  823. "total_fee"=>$total_price,
  824. "pakge_fee"=>isset($goodnake['package_fee'])? $goodnake['package_fee']:0,
  825. "cert_fee"=>isset($goodnake['cert_fee'])? $goodnake['cert_fee']:0,
  826. "open_fee"=>$good['open_fee'],
  827. "delivery_fee"=>isset($goodnake['delivery_fee'])? $goodnake['delivery_fee']:0,
  828. "mark_fee"=>isset($goodnake['mark_fee'])? $goodnake['mark_fee']:0,
  829. "nake_fee"=>isset($goodnake['nake_fee'])? $goodnake['nake_fee']:0,
  830. "teach_fee"=>isset($goodnake['cost_fee'])? $goodnake['cost_fee']:0,
  831. "demo_fee"=>$good['demo_fee'],
  832. "diff_weight"=>"0",
  833. "diff_fee"=>"0",
  834. "gold_price"=>"0",
  835. "supplierNo"=>$info['supplierNo'],
  836. "supplier_name"=>$supplier['name'],
  837. "companyNo"=>$info['companyNo'],
  838. "send_status"=>1,
  839. "send_num"=>"0",
  840. "wsend_num"=>$info['good_num'],
  841. "weight"=>$good['noble_weight'],
  842. "remark"=>"",
  843. "status"=>0,//0表示初始化
  844. "lasttime"=>$info['lasttime'],
  845. "is_del"=>0,
  846. "order_type" => 1,//1备库
  847. "order_source" => 0,//0备库下单
  848. "good_type"=>1,
  849. "addtime"=>date("Y-m-d H:i:s"),
  850. "updatetime"=>date("Y-m-d H:i:s"),
  851. 'good_createrid' => $good['createrid'],
  852. 'good_creater' => $good['creater'],//商品创建人
  853. ];
  854. Db::startTrans();
  855. try{
  856. $up =Db::name("purchease_order")->insert($cg,true);
  857. if($up>0){
  858. $good = Db::name("good_stock")->where(["wsm_code" =>$wsm_code, "spuCode" => $info['spuCode'], "is_del" => 0])->find();
  859. if ($good == false) {
  860. $good = [
  861. "spuCode" => $info['spuCode'],
  862. "wsm_code" => $wsm_code,
  863. "usable_stock" => 0,
  864. "wait_out_stock" => 0,
  865. "wait_in_stock" => 0,
  866. "total_stock" => 0,
  867. "addtime" => date("Y-m-d H:i:s"),
  868. "updatetime" => date("Y-m-d H:i:s"),
  869. ];
  870. $order = ["order_code" => $info['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  871. } else {
  872. $order = ["order_code" => $info['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  873. }
  874. $good['wait_in_stock'] += $info['good_num'];
  875. $good['updatetime'] = date("Y-m-d H:i:s");
  876. $upd = Db::name("good_stock")->save($good);
  877. $good_data[] = ['good_log_code' => $cgdCode,"stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1,
  878. 'stock' => $info['good_num'], "stock_name" => "wait_in_stock"];
  879. if ($upd) {
  880. // 商品变动日志表,good_log_code字段存储采购单号
  881. $order = ["order_code" => $cgdCode, "status" => 0, "action_remark" => '', "action_type" => "create"];
  882. GoodLog::LogAdd($this->post['token'], $good_data, 'CGD');
  883. ActionLog::logAdd($this->post['token'], $order, "CGD", 0, $good);
  884. $process = ["order_code" => $cgdCode, "order_id" => $up, "order_status" => $cg['status'], "order_type" => 'CGD',"before_status"=>0,'holder_id'=>Db::name('supplier')->where(['code' => $cg['supplierNo'], 'is_del' => 0])->value('personid', 0)];
  885. ProcessOrder::AddProcess($this->post['token'], $process);
  886. $info['wsm_code'] = $wsm_code;
  887. $old_info_status = $info['status'];
  888. $info['status'] = 2;
  889. $info['updatetime'] = date("Y-m-d H:i:s");
  890. $bas = Db::name("purchease")->save($info);
  891. if ($bas) {
  892. //修改状态,添加待办
  893. ActionLog::logAdd($this->post['token'], [
  894. "order_code" => $info['bk_code'],//销售单code
  895. "status" => $old_info_status,//这里的status是之前的值
  896. "action_remark" => '',//备注
  897. "action_type" => "edit"//新建create,编辑edit,更改状态status
  898. ], "BHD", $info['status'], $info);
  899. ProcessOrder::AddProcess($this->post['token'], [
  900. "order_type" => 'BHD',
  901. "order_code" => $info['bk_code'],//销售单code
  902. "order_id" => $info['id'],
  903. "order_status" => $info['status'],
  904. "before_status"=> $old_info_status,
  905. 'holder_id' => $info['apply_id']
  906. ]);
  907. //维护台账记录
  908. Db::name('standing_book')->where('bk_code', $bk_code)->update([
  909. 'spuCode' => $cg['spuCode'],
  910. 'order_type' => $cg['order_type'],
  911. 'order_source' => $cg['order_source'],
  912. 'supplierNo' => $cg['supplierNo'],
  913. 'companyNo' => $cg['companyNo'],
  914. 'cgdNo' => $cg['cgdNo'],
  915. 'updatetime' => date('Y-m-d H:i:s'),
  916. ]);
  917. //将采购单数据塞入到队列中
  918. $push_data = json_encode([
  919. 'supplierNo' => $cg['supplierNo'],
  920. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  921. 'data' => [
  922. 'cgdNo' => $cg['cgdNo'],
  923. 'spuCode' => $cg['spuCode'],
  924. 'good_name' => $cg['good_name'],
  925. 'good_num' => $cg['good_num'],
  926. 'good_price' => $cg['good_price'],
  927. 'total_fee' => $cg['total_fee'],
  928. 'weight' => $cg['weight'],
  929. 'addtime' => $cg['addtime'],
  930. ],
  931. ], JSON_UNESCAPED_UNICODE);
  932. Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  933. Db::commit();
  934. return app_show(0, "采购单新建成功", ["cgdNo" => $cgdCode]);
  935. }
  936. }
  937. // }
  938. }
  939. Db::rollback();
  940. return error_show(1004,"采购单新建失败");
  941. }catch (\Exception $e){
  942. Db::rollback();
  943. return error_show(1004,$e->getMessage());
  944. }
  945. }
  946. }