Resign.php 54 KB

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