Resign.php 53 KB

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