Resign.php 52 KB

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