Allot.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\DataGroup as DataGroupModel;
  5. use app\admin\model\GoodLog;
  6. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  7. use app\admin\model\ProcessWait;
  8. use app\BaseController;
  9. use think\App;
  10. use think\facade\Db;
  11. //调拨 allot_info(从表)和allot_stock(主表)
  12. //调拨之后,修改库存,采购单的仓库编号是否也需要修改
  13. class Allot extends Base
  14. {
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. }
  19. public function list()
  20. {
  21. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  22. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  23. $where = [['a.is_del', "=", 0]];
  24. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !== "" ? trim($this->post['allot_code']) : "";
  25. if ($allot_code !== "") {
  26. // $where['a.allot_code'] = $allot_code;
  27. $where[] = ['a.allot_code', "=", $allot_code];
  28. }
  29. // $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !== "" ? trim($this->post['wsm_out']) : "";
  30. // if ($wsm_out !== "") {
  31. //$where['a.wsm_out'] = $wsm_out;
  32. // $where[]=['a.wsm_out',"=",$wsm_out];
  33. // }
  34. // $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !== "" ? trim($this->post['wsm_in']) : "";
  35. // if ($wsm_in !== "") {
  36. // $where['a.wsm_in'] = $wsm_in;
  37. // $where[]=['a.wsm_in',"=",$wsm_in];
  38. // }
  39. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  40. if ($start != "") {
  41. //$where = ["a.addtime"=>Db::raw(">= '{$start}'")];
  42. $where[] = ['a.addtime', ">=", $start];
  43. }
  44. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  45. if ($end != "") {
  46. // $where = ["a.addtime"=>Db::raw("<= '{$end}'")];
  47. $where[] = ['a.addtime', "<=", $end];
  48. }
  49. // $out_supplierNo =isset($this->post['wsm_out_supplierNo']) &&$this->post['wsm_out_supplierNo'] !=="" ? trim($this->post['wsm_out_supplierNo']):"";
  50. // if($out_supplierNo!=""){
  51. // $supplier = Db::name("supplier")->where(["code"=>$out_supplierNo])->find();
  52. // if(empty($supplier)){
  53. // return error_show(1004,"未找到出库供应商信息");
  54. // }
  55. // $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"wsm_type"=>1,"supplierNo"=>$out_supplierNo])->column("wsm_code");
  56. //$condtion["a.c.wsm_code"] = $wsmcode;
  57. // $where[]=['a.wsm_out',"in",$wsmcode];
  58. // }
  59. // $out_companyNo = isset($this->post['out_companyNo']) && $this->post['out_companyNo'] !== "" ? trim($this->post['out_companyNo']) : "";
  60. // if ($out_companyNo !== "") {
  61. // $where[]=["a.out_companyNo","=",$out_companyNo];
  62. // }
  63. // $in_companyNo = isset($this->post['in_companyNo']) && $this->post['in_companyNo'] !== "" ? trim($this->post['in_companyNo']) : "";
  64. // if ($in_companyNo !== "") {
  65. // $where[]=["a.in_companyNo","=",$in_companyNo];
  66. // }
  67. // $in_supplierNo =isset($this->post['wsm_in_supplierNo']) &&$this->post['wsm_in_supplierNo'] !=="" ? trim($this->post['wsm_in_supplierNo']):"";
  68. // if($in_supplierNo!=""){
  69. // $supplier = Db::name("supplier")->where(["code"=>$in_supplierNo])->find();
  70. // if(empty($supplier)){
  71. // return error_show(1004,"未找到入库供应商信息");
  72. // }
  73. // $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"wsm_type"=>1,"supplierNo"=>$in_supplierNo])->column("wsm_code");
  74. //$condtion["c.wsm_code"] = $wsmcode;
  75. // $where[]=['a.wsm_in',"in",$wsmcode];
  76. // }
  77. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  78. if ($status !== "") {
  79. $where[] = ['a.status', "=", $status];
  80. }
  81. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  82. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  83. // $role = $this->checkDataShare();
  84. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['a.apply_id', 'in', $role[DataGroupModel::$type_全部]];
  85. $role = $this->checkDataShare();
  86. // $hand = resign_hand_user($this->uid,0);
  87. // if (!empty($role[DataGroupModel::$type_全部])) {
  88. // $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  89. // $where[] = ['a.apply_id', 'in',$arr];
  90. // }
  91. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
  92. //if($relaComNo!="") $where[]=['a.in_companyNo|a.out_companyNo','=', $relaComNo];
  93. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] != "" ? trim($this->post['wsm_code']) : "";
  94. $type = isset($this->post['type']) && $this->post['type'] != "" ? intval($this->post['type']) : "";
  95. switch ($type) {
  96. case 1:
  97. //入库
  98. if ($relaComNo !== '') $where[] = ['a.in_companyNo', '=', $relaComNo];
  99. if ($wsm_code !== '') $where[] = ['a.wsm_in', '=', $wsm_code];
  100. break;
  101. case 2:
  102. //出库
  103. if ($relaComNo !== '') $where[] = ['a.out_companyNo', '=', $relaComNo];
  104. if ($wsm_code !== '') $where[] = ['a.wsm_out', '=', $wsm_code];
  105. break;
  106. case 3:
  107. //入库和出库
  108. if ($relaComNo !== '') $where[] = ['a.in_companyNo|a.out_companyNo', '=', $relaComNo];
  109. if ($wsm_code !== '') $where[] = ['a.wsm_in|a.wsm_out', '=', $wsm_code];
  110. break;
  111. }
  112. $count = Db::name('allot_stock')
  113. ->alias('a')
  114. // ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  115. ->where($where)
  116. ->count();
  117. $total = ceil($count / $size);
  118. $page = $page >= $total ? $total : $page;
  119. $list = Db::name('allot_stock')
  120. ->alias('a')
  121. ->where($where)
  122. ->order("a.addtime desc")
  123. ->page($page, $size)
  124. ->select()
  125. ->toArray();
  126. // var_dump(Db::name('allot_stock')->getLastSql());
  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. $data = [];
  131. foreach ($list as $value) {
  132. $db = Db::name('warehouse_info')
  133. ->alias('a')
  134. // ->join("supplier b","b.code=a.supplierNo","left")
  135. // ->join("business bus","bus.companyNo=a.companyNo","left")
  136. // ->field("a.name,a.wsm_code,b.name rname,b.code,bus.company")
  137. ->field("a.name,a.wsm_code,a.supplierNo,a.companyNo")
  138. ->where(['a.wsm_code' => $value['wsm_out'], 'a.is_del' => 0])
  139. ->find();
  140. $value['name'] = isset($db['name']) ? $db['name'] : "";
  141. $value['wsm_code'] = isset($db['wsm_code']) ? $db['wsm_code'] : "";
  142. $dc = Db::name('warehouse_info')
  143. ->alias('a')
  144. // ->join("business bus","bus.companyNo=a.companyNo","left")
  145. // ->join("supplier b","b.code=a.supplierNo","left")
  146. // ->field("a.name,a.wsm_code,b.name tname,b.code,bus.company")
  147. ->field("a.name,a.wsm_code,a.supplierNo,a.companyNo")
  148. ->where(['a.wsm_code' => $value['wsm_in'], 'a.is_del' => 0])
  149. ->find();
  150. $names = $userCommon->handle('getCodeAndName', ['code' => [$db['supplierNo'], $db['companyNo'], $dc['supplierNo'], $dc['companyNo']]]);
  151. $value['rname'] = $names['data'][$db['supplierNo']] ?? '';//isset($db['rname']) ? $db['rname'] : "";
  152. $value['code'] = $db['supplierNo'];
  153. $value['out_company_name'] = $names['data'][$db['companyNo']] ?? '';//isset($db['company']) ? $db['company'] : "";
  154. $value['wsm_in_name'] = isset($dc['name']) ? $dc['name'] : "";
  155. $value['wsm_in_code'] = isset($dc['wsm_code']) ? $dc['wsm_code'] : "";
  156. $value['tname'] = $names['data'][$dc['supplierNo']] ?? '';//isset($dc['tname']) ? $dc['tname']:"";
  157. $value['rcode'] = isset($dc['supplierNo']) ? $dc['supplierNo'] : "";
  158. $value['in_company_name'] = $names['data'][$dc['companyNo']] ?? '';//isset($dc['company']) ? $dc['company']:"";
  159. $value['company_name'] = $item[$value['apply_id']] ?? '';
  160. //是否具有编辑权限
  161. $value['is_allow_update'] = 0;
  162. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  163. $data [] = $value;
  164. }
  165. return app_show(0, "获取成功", ['count' => $count, 'list' => $data]);
  166. }
  167. public function create(){
  168. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  169. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"";
  170. if($wsm_out==""){
  171. return error_show(1002,"出库仓库编号不能为空");
  172. }
  173. $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"";
  174. if($wsm_in==""){
  175. return error_show(1002,"入库仓库不能为空");
  176. }
  177. //根据出入库的仓库编码,查出对应的公司、业务公司
  178. $warehouse_temp_info = Db::name('warehouse_info')
  179. ->where(['is_del' => 0, 'status' => 1])
  180. ->whereIn('wsm_code', [$wsm_out, $wsm_in])
  181. ->column('supplierNo,companyNo', 'wsm_code');//supplierNo所属供货商,companyNo业务企业
  182. $allot_code=makeNo("XK");
  183. $apply_id =GetUserInfo($token);
  184. if(empty($apply_id)||$apply_id['code']!=0){
  185. return error_show(1002,"申请人数据不存在");
  186. }
  187. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  188. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  189. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"" ;
  190. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  191. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  192. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) : "";
  193. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) :"0";
  194. if($status==""){
  195. return error_show(1002,"状态不能为空");
  196. }
  197. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  198. if($dain==""){
  199. return error_show(1002,"商品不能为空");
  200. }
  201. Db::startTrans();
  202. try {
  203. $data=[
  204. "allot_code"=>$allot_code,
  205. "wsm_out"=>$wsm_out,
  206. "wsm_in"=>$wsm_in,
  207. "out_companyNo"=>$warehouse_temp_info[$wsm_out]['companyNo'],
  208. "in_companyNo"=>$warehouse_temp_info[$wsm_in]['companyNo'],
  209. "apply_name"=>$ri,
  210. "apply_id"=>$rm,
  211. "post_name"=>$post_name,
  212. "post_code"=>$post_code,
  213. "post_fee"=>$post_fee,
  214. "remark"=>$remark,
  215. "status"=>$status,
  216. "is_del"=>0,
  217. "addtime"=>date("Y-m-d H:i:s"),
  218. "updatetime"=>date("Y-m-d H:i:s"),
  219. ];
  220. $pd=Db::name('allot_stock')->insert($data,true);
  221. $dm=[];
  222. if($pd>0) {
  223. foreach ($dain as $value) {
  224. $st = Db::name("good_stock_info")->alias("a")
  225. ->leftJoin("good_stock d" ,"d.id=a.stockid")
  226. ->leftJoin("good b" ,"d.spuCode=b.spuCode")
  227. ->where(['wsm_code' => $wsm_out, 'a.bnCode' => $value['bnCode'], 'd.is_del' => 0])
  228. ->field("d.spuCode,wsm_code,d.usable_stock,d.wait_out_stock,d.wait_in_stock,b.good_name,a.total_num,a.balance_num,a.origin_price")
  229. ->find();
  230. if (empty($st)) {
  231. Db::rollback();
  232. return error_show(1003, "商品不能为空");
  233. }
  234. if($value['allot_num']>$st['balance_num']){
  235. Db::rollback();
  236. return error_show(2000,"库存数量不足");
  237. }
  238. $temp = [];
  239. $temp['good_name'] = $st['good_name'];
  240. $temp['allot_code'] =$allot_code;
  241. $temp['bnCode'] =$value['bnCode'];
  242. $temp['good_type_code'] = $st['spuCode'];
  243. $temp['origin_price'] = $st['origin_price'];
  244. $temp['allot_num'] = $value['allot_num'];
  245. $temp['usable_num'] =$st['balance_num'];
  246. $temp['error_num'] = 0;
  247. $temp['error_remark'] = "";
  248. $temp['stock_num'] = 0;
  249. $temp['error_code']="";
  250. $temp['good_num']=$st['balance_num'];
  251. $temp['addtime'] = date("Y-m-d H:i:s");
  252. $temp['updatetime'] = date("Y-m-d H:i:s");
  253. $dm[]=$temp;
  254. }
  255. $io = Db::name('allot_info')->insertAll($dm);
  256. if ($io) {
  257. $order = ["order_code"=>$allot_code,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
  258. ActionLog::logAdd($this->post['token'],$order,"DBD",$status,$this->post);
  259. $process = ["order_code" => $allot_code, "order_id" => $pd, "order_status" => $status, "order_type" => 'DBD', "before_status" => 0, 'holder_id' => $data['apply_id']];
  260. ProcessOrder::AddProcess($token,$process);
  261. Db::commit();
  262. return error_show(0, "调拨创建成功");
  263. }
  264. }
  265. Db::rollback();
  266. return error_show(1002,"调拨创建失败");
  267. }catch(\Exception $e){
  268. Db::rollback();
  269. return error_show(1005,$e->getMessage());
  270. }
  271. }
  272. public function info(){
  273. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  274. if($id==""){
  275. return error_show(1002,"参数id不能为空");
  276. }
  277. $str = Db::name('allot_stock') ->where(['id'=>$id,'is_del'=>0])->find();
  278. if(empty($str)){
  279. return error_show(1003,"未找到调拨数据");
  280. }
  281. $stv = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_out'],'is_del'=>0])->field('companyNo,wsm_code,name')->find();
  282. if(empty($stv)){
  283. return error_show(1002,"未找到仓库数据");
  284. }
  285. $st = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_in'],'is_del'=>0])->field('companyNo,wsm_code,name')->find();
  286. if(empty($st)){
  287. return error_show(1003,"未找到仓库数据");
  288. }
  289. $user = \app\admin\common\User::getIns();
  290. $supplie=$user->handle("getCodeAndName",["code"=>[$stv['companyNo'],$st['companyNo']]]);
  291. if($supplie['code']!=0 || empty($supplie['data'])) return error_show(1001,"未找到供应商数据");
  292. $vr = ["name"=>$supplie['data'][$stv['companyNo']],"code"=>$stv['companyNo']];
  293. $var = ["name"=>$supplie['data'][$st['companyNo']],"code"=>$st['companyNo']];
  294. // $vr = Db::name('supplier')->where(['code'=>$stv['supplierNo'],'is_del'=>0])->field('name,code as rcode')->find();
  295. // if(empty($vr)){
  296. // return error_show(1001,"未找到供应商数据");
  297. // }
  298. // $var = Db::name('supplier')->where(['code'=>$st['supplierNo'],'is_del'=>0])->field('name,code')->find();
  299. // if(empty($var)){
  300. // return error_show(1004,"未找到供应商数据");
  301. // }
  302. // var_dump(Db::name('supplier')->getLastSql());
  303. $str ['wsm_out_name']=$vr['name'];
  304. $str ['wsm_in_name']=$var['name'];
  305. $str ['ckc_name']=$stv['name'];
  306. $str ['ckr_name']=$st['name'];
  307. $str['out_code']=$vr['code'];
  308. $str['in_code']=$var['code'];
  309. // $str = Db::name('allot_stock')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_out","left")
  310. // ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,")
  311. // ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  312. // var_dump( Db::name('allot_stock')->getLastSql());
  313. $vmp = Db::name('allot_info')->where(['allot_code'=>$str['allot_code'],'is_del'=>0])->select();
  314. $data=[];
  315. foreach ($vmp as $k=>$value){
  316. $s = Db::name("good_stock_info")->alias("a")
  317. ->leftJoin("good_stock d" ,"d.id=a.stockid")
  318. ->where(['wsm_code' => $str['wsm_out'], 'a.bnCode' => $value['bnCode'], 'd.is_del' => 0])
  319. ->field("a.balance_num,a.origin_price")
  320. ->find();
  321. if($str['status']<=1) {
  322. $value['usable_num'] = $s['balance_num']??0;
  323. }
  324. $value['error_msg']="";
  325. $value['good_code']=$value['spuCode']=$value['good_type_code'];
  326. if($value['error_code']!=""){
  327. $msg =Db::name("result_info")->where(['result_code'=>$value['error_code'],"is_del"=>0])->find();
  328. $value['error_msg']=$value['error_code_name']= isset($msg['result']) &&$msg['result']!="" ? $msg['result']:"";
  329. }
  330. $data[]=$value;
  331. }
  332. $str['item']=$data;
  333. if(empty($str)){
  334. return error_show(1002,"未找到调拨编号");
  335. }else{
  336. return app_show(0,"获取成功",$str);
  337. }
  338. }
  339. public function edit(){
  340. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  341. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  342. if($id===""){
  343. return error_show(1002,"参数id不能为空");
  344. }
  345. $etid = Db::name("allot_stock")->where(["id"=>$id,"is_del"=>0])->find();
  346. if(empty($etid)){
  347. return error_show(1002,"未找到数据");
  348. }
  349. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  350. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  351. $apply_id =GetUserInfo($token);
  352. if(empty($apply_id)||$apply_id['code']!=0){
  353. return error_show(1002,"申请人数据不存在");
  354. }
  355. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  356. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  357. $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"" ;
  358. if($wsm_out==""){
  359. return error_show(1002,"出库仓库不能为空");
  360. }
  361. $wsm_in= isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"" ;
  362. if($wsm_in==""){
  363. return error_show(1002,"入库仓库不能为空");
  364. }
  365. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  366. // $status= isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']) :"" ;
  367. // if($status==""){
  368. // return error_show(1002,"状态不能为空");
  369. // }
  370. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  371. if($dain==""){
  372. return error_show(1002,"商品不能为空");
  373. }
  374. Db::startTrans();
  375. try{
  376. $data=[
  377. "apply_id"=>$rm,
  378. "apply_name"=>$ri,
  379. "wsm_out"=>$wsm_out,
  380. "wsm_in"=>$wsm_in,
  381. "post_fee"=>$post_fee,
  382. "post_name"=>$post_name,
  383. "post_code"=>$post_code,
  384. "updatetime"=>date('Y-m-d H:i:s'),
  385. ];
  386. $da = Db::name('allot_stock')->where(["id"=>$id,"is_del"=>0])->save($data);
  387. if($da!=false) {
  388. foreach ($dain as $value) {
  389. $st = Db::name("good_stock_info")->alias("a")
  390. ->leftJoin("good_stock d" ,"d.id=a.stockid")
  391. ->leftJoin("good b" ,"d.spuCode=b.spuCode")
  392. ->where(['wsm_code' => $wsm_out, 'a.bnCode' => $value['bnCode'], 'd.is_del' => 0])
  393. ->field("d.spuCode,wsm_code,d.usable_stock,d.wait_out_stock,d.wait_in_stock,b.good_name,a.total_num,a.balance_num,a.origin_price")
  394. ->find();
  395. if (empty($st)) {
  396. Db::rollback();
  397. return error_show(1003, "商品不能为空");
  398. }
  399. if(isset($value['id'])&&$value['id']!==""){
  400. $good = Db::name('allot_info')->where(["id"=>$value['id'],"is_del"=>0])->find();
  401. if(empty($good)||$good['allot_code']!=$etid['allot_code']){
  402. Db::rollback();
  403. return error_show(2000,"商品不属于此次调拨");
  404. }
  405. }
  406. if($value['allot_num']>$st['balance_num']){
  407. Db::rollback();
  408. return error_show(2000,"批次库存数量不足");
  409. }
  410. $temp = [];
  411. isset($value['id'])&&$value['id']!=="" ? $temp['id'] = $value['id']:'';
  412. $temp['good_name'] = $st['good_name'];
  413. $temp['good_type_code'] = $st['spuCode'];
  414. $temp['bnCode'] =$value['bnCode'];
  415. $temp['allot_num'] = $value['allot_num'];
  416. $temp['usable_num'] =$st['balance_num'];
  417. $temp['origin_price'] = $st['origin_price'];
  418. $temp['error_num'] = 0;
  419. $temp['error_remark'] = "";
  420. $temp['allot_code'] = $etid['allot_code'];
  421. $temp['stock_num'] = 0;
  422. $temp['good_num']=$st['balance_num'];
  423. $temp['error_code']="";
  424. $temp['is_del']=$value['is_del'];
  425. isset($value['id'])&&$value['id']!=="" ? '' :$temp['addtime'] = date("Y-m-d H:i:s");
  426. $temp['updatetime'] = date("Y-m-d H:i:s");
  427. $np = Db::name('allot_info')->save($temp);
  428. if ($np==false) {
  429. Db::rollback();
  430. return error_show(1003, "调拨更新失败");
  431. }
  432. }
  433. $order = ["order_code"=>$etid['allot_code'],"status"=>$etid['status'],"action_remark"=>'',"action_type"=>"edit"];
  434. ActionLog::logAdd($this->post['token'],$order,"DBD",$etid['status'],$this->post);
  435. Db::commit();
  436. return error_show(0, "调拨更新成功");
  437. }
  438. Db::rollback();
  439. return error_show(1003, "调拨更新失败");
  440. }catch (\Exception $e){
  441. Db::rollback();
  442. return error_show(1005,$e->getMessage());
  443. }
  444. }
  445. public function del(){
  446. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  447. if($id===""){
  448. return error_show(1004,"参数id不能为空");
  449. }
  450. $str= Db::name('allot_stock')->where(['id'=>$id,'is_del'=>0])->find();
  451. if(empty($str)){
  452. return error_show(1002,"未找到数据");
  453. }
  454. $end = Db::name('allot_stock')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  455. if($end){
  456. $order = ["order_code"=>$str['allot_code'],"status"=>$str['status'],"action_remark"=>'',"action_type"=>"del"];
  457. ActionLog::logAdd($this->post['token'],$order,"DBD",$str['status'],$this->post);
  458. $process=["order_code"=>$str['allot_code'],"order_status"=>$str['status'],"order_type"=>'DBD'];
  459. ProcessOrder::workdel($process);
  460. //将对应的调拨单流程的数据都删掉(没有相关的中断节点,所以这里直接操作process_wait表)
  461. Db::name('process_wait')
  462. ->where([
  463. 'order_type' => 'DBD',
  464. 'order_code' => $str['allot_code'],
  465. 'order_id' => $id,
  466. 'status' => ProcessWait::$status_wait,
  467. ])->update(['status' => ProcessWait::$status_interrupt]);
  468. return error_show(0,"删除成功");
  469. }else{
  470. return error_show(1002,"删除失败");
  471. }
  472. }
  473. // 0 待发起流程 1 待入库方同意 2 待出库方发货 3待入库方验货 4待验货审核 5 调拨完成 6 调拨驳回
  474. public function status(){
  475. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"" ;
  476. if($id==""){
  477. return error_show(1002,"调拨编号不能为空");
  478. }
  479. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) :"";
  480. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  481. if($status===""){
  482. return error_show(1002,"审核状态不能为空");
  483. }
  484. $st = Db::name('allot_stock')->where(['id'=>$id,"is_del"=>0])->find();
  485. if(empty($st)){
  486. return error_show(1002,"调拨信息未找到");
  487. }
  488. Db::startTrans();
  489. try{
  490. $sr= $st['status'];
  491. $st['remark']=$remark;
  492. $st['status']=$status;
  493. $st['updatetime']= date('Y-m-d H:i:s');
  494. $sv = Db::name('allot_stock')->save($st);
  495. if($sv){
  496. if($status==2 || $status==1){
  497. $good_data=[];
  498. $vmp = Db::name('allot_info')->where(['allot_code'=>$st['allot_code'],"is_del"=>0])->select();
  499. foreach ($vmp as $value){
  500. $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_type_code'],"wsm_code"=>$st['wsm_out'],"is_del"=>0])->find();
  501. if($value['allot_num']>$stock['usable_stock']){
  502. Db::rollback();
  503. return error_show(2000,"超出库存数量");
  504. }
  505. $stockinfo = Db::name("good_stock_info")->where(["bnCode"=>$value['bnCode'],"stockid"=>$stock['id']])->find();
  506. if($value['allot_num']>$stockinfo['balance_num']){
  507. Db::rollback();
  508. return error_show(2000,"超出bn库存数量");
  509. }
  510. if($status==2){
  511. $stock['usable_stock']-=$value['allot_num'];
  512. $stock['wait_out_stock']+=$value['allot_num'];
  513. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  514. $stock['updatetime']= date('Y-m-d H:i:s');
  515. $Db = Db::name('good_stock')->update($stock);
  516. //var_dump(Db::name('good_stock')->getLastSql());
  517. if($Db==false){
  518. Db::rollback();
  519. return error_show(1002,"状态更新失败");
  520. }
  521. $stoup =GoodStockInfo::bnStock($stock['id'],$value['bnCode'],$value['allot_num'],1);
  522. if($stoup==false){
  523. Db::rollback();
  524. return error_show(1002,"bn库存数修改失败");
  525. }
  526. //商品变动日志表,good_log_code字段存储调拨单号
  527. $good_data[] = ['good_log_code' => $st['allot_code'], "stock_id" => $stock['id'], "type" => 1, 'stock' =>$value['allot_num'], "stock_name" => "wait_out_stock"];
  528. $good_data[] = ['good_log_code' => $st['allot_code'], "stock_id" => $stock['id'], "type" => 2, 'stock' => $value['allot_num'], "stock_name" => "usable_stock"];
  529. }
  530. }
  531. GoodLog::LogAdd($this->post['token'],$good_data,'DBD');
  532. }
  533. $order = ["order_code"=>$st['allot_code'],"status"=>$sr,"action_remark"=>'',"action_type"=>"status"];
  534. ActionLog::logAdd($this->post['token'],$order,"DBD",$status,$this->post);
  535. $process=["order_code"=>$st['allot_code'],"order_id"=>$st['id'],"order_status"=>$st['status'],"order_type"=>'DBD',"before_status"=>$sr, 'holder_id' => $st['apply_id']];
  536. ProcessOrder::AddProcess($this->post['token'],$process);
  537. Db::commit();
  538. return error_show(0,"状态更新成功");
  539. }
  540. Db::rollback();
  541. return error_show(1003,"状态更新失败");
  542. }catch (\Exception $e) {
  543. Db::rollback();
  544. return error_show(1005, $e->getMessage());
  545. }
  546. }
  547. /*出库方发货*/
  548. public function getont(){
  549. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  550. if(empty($allot_code)){
  551. return error_show(1002,"调拨单编号不能为空");
  552. }
  553. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  554. if($al==""){
  555. return error_show(1003,"调拨编号未找到");
  556. }
  557. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  558. if($post_name==""){
  559. return error_show(1002,"物流公司不能为空");
  560. }
  561. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  562. if($post_code==""){
  563. return error_show(1002,"物流单号不能为空");
  564. }
  565. $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
  566. if($post_fee===""){
  567. return error_show(1002,"物流费用不能为空");
  568. }
  569. Db::startTrans();
  570. try {
  571. $data=[
  572. "allot_code"=>$allot_code,
  573. "post_name"=>$post_name,
  574. "post_code"=>$post_code,
  575. "post_fee"=>$post_fee,
  576. "status"=>3,
  577. "is_del"=>0,
  578. "updatetime"=>date("Y-m-d H:i:s"),
  579. ];
  580. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code])->update($data);
  581. if($datainfo){
  582. $vnp = Db::name('allot_info')->where(['allot_code'=>$al['allot_code'],'is_del'=>0])->select();
  583. $good_data=[];
  584. if(!empty($vnp)){
  585. foreach ($vnp as $value){
  586. $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_type_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
  587. if($stock['wait_out_stock']<$value['allot_num']){
  588. Db::rollback();
  589. return error_show(2000,"超出库存数量");
  590. }
  591. $stock['wait_out_stock']-=$value['allot_num'];
  592. $stock['intra_stock']+=$value['allot_num'];
  593. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  594. $stock['updatetime']= date('Y-m-d H:i:s');
  595. $dr = Db::name('good_stock')->update($stock);
  596. if($dr==false){
  597. Db::rollback();
  598. return error_show(1002,"发货更新失败");
  599. }
  600. //商品变动日志表,good_log_code字段存储调拨单号
  601. $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 1, 'stock' => $value['allot_num'], "stock_name" => "intra_stock"];
  602. $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 2, 'stock' => $value['allot_num'], "stock_name" => "wait_out_stock"];
  603. }
  604. GoodLog::LogAdd($this->post['token'],$good_data,'DBD');
  605. }
  606. $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"edit"];
  607. ActionLog::logAdd($this->post['token'],$order,"DBD",3,$this->post);
  608. $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>3,"order_type"=>'DBD',"before_status"=>$al['status'], 'holder_id' => $al['apply_id']];
  609. ProcessOrder::AddProcess($this->post['token'],$process);
  610. Db::commit();
  611. return error_show(0,"发货更新成功");
  612. }
  613. Db::rollback();
  614. return error_show(1003,"发货更新失败");
  615. }catch (\Exception $e) {
  616. Db::rollback();
  617. return error_show(1005, $e->getMessage());
  618. }
  619. }
  620. /*入库方验货*/
  621. public function getin(){
  622. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  623. if(empty($allot_code)){
  624. return error_show(1002,"调拨单编号不能为空");
  625. }
  626. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  627. if($al==""){
  628. return error_show(1003,"调拨编号未找到");
  629. }
  630. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  631. if($dain==""){
  632. return error_show(1002,"商品不能为空");
  633. }
  634. Db::startTrans();
  635. try {
  636. $data=[
  637. "allot_code"=>$allot_code,
  638. "is_del"=>0,
  639. "status"=>4,
  640. "updatetime"=>date("Y-m-d H:i:s"),
  641. ];
  642. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  643. if($datainfo>0) {
  644. $good_data=[];
  645. foreach ($dain as $value) {
  646. // $st = Db::name("good_stock")->alias("a")->leftJoin("good b" ,"a.spuCode=b.spuCode")
  647. // ->where(['wsm_code' => $al['wsm_out'], 'a.spuCode' => $value['good_code'], 'a.is_del' => 0])
  648. // ->field("a.spuCode,wsm_code,usable_stock,wait_out_stock,wait_in_stock,b.good_name")->find();
  649. // if ($st==false) {
  650. // Db::rollback();
  651. // return error_show(1003, "未找到商品");
  652. // }
  653. $n = Db::name('allot_info')->where(['id'=>$value['id'],'allot_code'=>$al['allot_code'],'is_del'=>0])->find();
  654. if($n==false){
  655. Db::rollback();
  656. return error_show(10032, "未找到盘点信息数据");
  657. }
  658. $temp = [];
  659. $temp['error_code']=$value['error_code'];
  660. $temp['error_num'] = $value['error_num'];
  661. $temp['error_remark'] = $value['error_remark'];
  662. $temp['stock_num']=$n['allot_num']-$value['error_num'];
  663. $temp['updatetime'] = date("Y-m-d H:i:s");
  664. $np = Db::name('allot_info')->where(['id'=>$value['id'],'allot_code'=> $al['allot_code'],'is_del'=>0])->save($temp);
  665. if($np==false){
  666. Db::rollback();
  667. return error_show(1001,"数据更新失败");
  668. }
  669. $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->lock(true)->find();
  670. if($stock['intra_stock']<$n['allot_num']){
  671. Db::rollback();
  672. return error_show(2000,"超出库存数量");
  673. }
  674. $stock['updatetime']= date('Y-m-d H:i:s');
  675. $stock['intra_stock']-=$n['allot_num'];
  676. $dr = Db::name('good_stock')->update($stock);
  677. if($dr==false){
  678. Db::rollback();
  679. return error_show(1002,"数据更新失败");
  680. }
  681. //商品变动日志表,good_log_code字段存储调拨单号
  682. $good_data[]=['good_log_code'=>$allot_code,"stock_id"=>$stock['id'],"type"=>2,'stock'=>$n['allot_num'],"stock_name"=>"intra_stock"];
  683. $instock = Db::name("good_stock")->where(["spuCode"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
  684. if($instock==false){
  685. $instock =[];
  686. $instock['spuCode']=$value['good_code'];
  687. $instock['wsm_code']=$al['wsm_in'];
  688. $instock['wait_in_stock']=0;
  689. $instock['wait_out_stock']=0;
  690. $instock['usable_stock']=0;
  691. $instock['intra_stock']=0;
  692. $instock['total_stock']=0;
  693. $instock['status']=1;
  694. $instock['is_del']=0;
  695. $instock['warn_stock']=0;
  696. $instock['addtime']= date('Y-m-d H:i:s');
  697. }
  698. $instock['wait_in_stock']+=$n['allot_num']-$value['error_num'];
  699. $instock['updatetime']= date('Y-m-d H:i:s');
  700. $sk = Db::name('good_stock')->save($instock);
  701. if ($sk==false) {
  702. Db::rollback();
  703. return error_show(1003, "入库验货更新失败");
  704. }
  705. //商品变动日志表,good_log_code字段存储调拨单号
  706. $good_data[]=['good_log_code'=>$allot_code,"stock_id"=>isset($instock['id'])?$instock['id']: Db::name("good_stock")->getLastInsID(),"type"=>1,'stock'=>$n['allot_num']-$value['error_num'],"stock_name"=>"wait_in_stock"];
  707. }
  708. GoodLog::LogAdd($this->post['token'],$good_data,'DBD');
  709. $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"edit"];
  710. ActionLog::logAdd($this->post['token'],$order,"DBD",4,$this->post);
  711. $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>4,"order_type"=>'DBD',"before_status"=>$al['status'], 'holder_id' => $al['apply_id']];
  712. ProcessOrder::AddProcess($this->post['token'],$process);
  713. Db::commit();
  714. return error_show(0, "入库验货更新成功");
  715. }
  716. Db::rollback();
  717. return error_show(1002, "入库验货更新失败");
  718. }catch (\Exception $e){
  719. Db::rollback();
  720. return error_show(1005,$e->getMessage());
  721. }
  722. }
  723. /*验货审核*/
  724. public function vesio(){
  725. $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
  726. if(empty($allot_code)){
  727. return error_show(1002,"调拨单编号不能为空");
  728. }
  729. $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
  730. if($al==""){
  731. return error_show(1003,"调拨编号未找到");
  732. }
  733. $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  734. if($dain==""){
  735. return error_show(1002,"商品不能为空");
  736. }
  737. Db::startTrans();
  738. try {
  739. $data=[
  740. "allot_code"=>$allot_code,
  741. "status"=>5,
  742. "is_del"=>0,
  743. "updatetime"=>date("Y-m-d H:i:s"),
  744. ];
  745. $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
  746. if($datainfo>0) {
  747. $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"edit"];
  748. ActionLog::logAdd($this->post['token'],$order,"DBD",5,$this->post);
  749. foreach ($dain as $value) {
  750. // $st = Db::name("good_stock")->alias("a")->leftJoin("good b" ,"a.spuCode=b.spuCode")
  751. // ->where(['wsm_code' => $al['wsm_out'], 'a.spuCode' => $value['good_code'], 'a.is_del' => 0])
  752. // ->field("a.spuCode,wsm_code,usable_stock,wait_out_stock,wait_in_stock,b.good_name")->find();
  753. $p = Db::name('allot_info')->where(['id'=>$value['id'],'allot_code'=>$al['allot_code'],"is_del"=>0])->find();
  754. if($p==false){
  755. Db::rollback();
  756. return error_show(1002,"未找到盘点数据");
  757. }
  758. $temp = [];
  759. $temp['error_num'] = $value['error_num']+$p['stock_num']-$value['stock_num'];
  760. $temp['stock_num'] = $value['stock_num'];
  761. $temp['error_remark'] = $value['error_remark'];
  762. $temp['updatetime'] = date("Y-m-d H:i:s");
  763. $np = Db::name('allot_info')->where(['id'=>$value['id'],'allot_code'=> $al['allot_code']])->save($temp);
  764. if($np==false){
  765. Db::rollback();
  766. return error_show(1001,"数据更新失败");
  767. }
  768. $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_code'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->lock(true)->find();
  769. if($stock['wait_in_stock']<$p['stock_num']){
  770. Db::rollback();
  771. return error_show(2000,"超出待入库存数量");
  772. }
  773. $stock['wait_in_stock']-=$p['stock_num'];
  774. $stock['usable_stock']+=$p['stock_num'];
  775. $stock['updatetime']=date('Y-m-d H:i:s');
  776. $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
  777. $dr = Db::name('good_stock')->update($stock);
  778. if ($dr==false) {
  779. Db::rollback();
  780. return error_show(1004, "验货更新失败");
  781. }
  782. $stoup =GoodStockInfo::bnStock($stock['id'],$p['bnCode'],$p['stock_num'],0,$p['origin_price']);
  783. if($stoup==false){
  784. Db::rollback();
  785. return error_show(1002,"bn库存数修改失败");
  786. }
  787. //商品变动日志表,good_log_code字段存储调拨单号
  788. $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 2, 'stock' => $p['stock_num'], "stock_name" => "wait_in_stock"];
  789. $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 1, 'stock' => $p['stock_num'], "stock_name" => "usable_stock"];
  790. }
  791. GoodLog::LogAdd($this->post['token'],$good_data,'DBD');
  792. $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>5,"order_type"=>'DBD',"before_status"=>$al['status'], 'holder_id' => $al['apply_id']];
  793. ProcessOrder::AddProcess($this->post['token'],$process);
  794. Db::commit();
  795. return error_show(0, "验货更新成功");
  796. }
  797. Db::rollback();
  798. return error_show(1003, "验货更新失败");
  799. }catch (\Exception $e){
  800. Db::rollback();
  801. return error_show(1005,$e->getMessage());
  802. }
  803. }
  804. public function goodlist()
  805. {
  806. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : 1;
  807. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : 10;
  808. $condtion = [['b.is_del', "=", 0], ['c.is_del', "=", 0],["d.balance_num",">=",0]];
  809. $wsmcode = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  810. if ($wsmcode !== "") {
  811. //return error_show(1002,"仓库code不能为空");
  812. $condtion[] = ['c.wsm_code', "=", $wsmcode];
  813. // $typecode= Db::name("good_stock")->where(["wsm_code"=>$wsmcode,'is_del'=>0])->column("good_type_code");
  814. }
  815. $goodcode = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  816. if ($goodcode != "") {
  817. // $condtion['a.good_code'] = Db::raw("like '%{$goodcode}%'");
  818. $condtion[] = ['b.spuCode', "like", "%{$goodcode}%"];
  819. }
  820. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  821. if ($good_name != "") {
  822. // $condtion['a.good_name'] = Db::raw("like '%{$good_name}%'");
  823. $condtion[] = ['b.good_name', "like", "%{$good_name}%"];
  824. }
  825. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  826. if ($supplierNo != "") {
  827. // $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  828. // if (empty($supplier)) {
  829. // return error_show(1004, "未找到供应商信息");
  830. // }
  831. $wsmcode = Db::name("warehouse_info")->where(["is_del" => 0, "supplierNo" => $supplierNo])->column("wsm_code");
  832. $condtion[] = ['c.wsm_code', "in", $wsmcode];
  833. }
  834. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  835. if ($companyNo !== "") {
  836. $wsmcode = Db::name("warehouse_info")->where(['companyNo' => $companyNo, "is_del" => 0])->column("wsm_code");
  837. $condition[] = ["c.wsm_code", "in", $wsmcode];
  838. }
  839. $stock_low = isset($this->post['stock_low']) && $this->post['stock_low'] !== "" ? intval($this->post['stock_low']) : "";
  840. if ($stock_low != "") {
  841. $condtion[] = ['d.balance_num', ">=", $stock_low];
  842. }
  843. $stock_up = isset($this->post['stock_up']) && $this->post['stock_up'] !== "" ? trim($this->post['stock_up']) : "";
  844. if ($stock_up != "") {
  845. $condtion[] = ['d.balance_num', "<=", $stock_up];
  846. }
  847. $cat_id = isset($this->post['cat_id']) && $this->post['cat_id'] !== "" ? intval($this->post['cat_id']) : "";
  848. if ($cat_id != "") {
  849. $at = Db::name('cat')->where(['id' => $cat_id])->find();
  850. if (empty($at)) {
  851. return error_show(1004, "未找到分类信息");
  852. }
  853. $cat = manger([$at['id']], $at['level']);
  854. $condtion[] = ['b.cat_id', "in", $cat];
  855. }
  856. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  857. if ($company_name !== "") $condtion[] = ["b.createrid", 'in', get_company_item_user_by_name($company_name)];
  858. $count = Db::name("good")
  859. ->alias("b")
  860. ->join("good_stock c", "c.spuCode=b.spuCode", "left")
  861. ->join("good_stock_info d", "c.id=d.stockid", "left")
  862. ->where($condtion)
  863. ->count();
  864. $page >= ceil($count / $size) ? $page = ceil($count / $size) : "";
  865. $list = Db::name("good")
  866. ->alias("b")
  867. ->leftJoin("good_stock c", "c.spuCode=b.spuCode")
  868. ->join("good_stock_info d", "c.id=d.stockid", "left")
  869. ->where($condtion)
  870. ->field("b.*,c.id as stock_id,c.usable_stock,c.wait_in_stock,c.wait_out_stock,c.wsm_code,0 uid,'待处理wf' nickname,d.bnCode,d.balance_num,d.origin_price")
  871. ->page($page, $size)
  872. ->select()
  873. ->toArray();
  874. $all_createrid = array_column($list,'createrid');
  875. $item = get_company_name_by_uid($all_createrid);
  876. $data = [];
  877. if ($list) {
  878. //品牌信息
  879. $brands = Db::name('brand')->whereIn('id', array_column($list, 'brand_id'))->where('is_del', 0)->column('brand_name', 'id');
  880. $units = Db::name('unit')->whereIn('id', array_column($list, 'good_unit'))->where('is_del', 0)->column('unit', 'id');
  881. // $business = Db::name('business')->whereIn('companyNo', array_column($list, 'companyNo'))->where('is_del', 0)->column('company', 'companyNo');
  882. $userCommon=\app\admin\common\User::getIns();
  883. $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list, 'supplierNo'),array_column($list, 'companyNo'))]);
  884. // $business = $names['data'];
  885. $supplier = Db::connect('mysql_sys')
  886. ->name('supplier')
  887. ->where(['code'=>array_column($list,'supplierNo')])
  888. ->column('name,person','code');
  889. foreach ($list as $key => $value) {
  890. // $supplier = Db::name("supplier")->where(["code" => $value['supplierNo']])->find();
  891. $value['supplier_name'] = $supplier['name'] ?? '';//$names['data'][$value['supplierNo']]??'';//isset($supplier['name']) ? $supplier['name'] : "";
  892. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")->where(["a.wsm_code" => $value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  893. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  894. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  895. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  896. $value['can'] = isset($value['cat_id']) && $value['cat_id'] != 0 ? made($value['cat_id']) : [];
  897. $value['brand_name'] = isset($brands[$value['brand_id']]) ? $brands[$value['brand_id']] : '';
  898. $value['unit'] = isset($units[$value['good_unit']]) ? $units[$value['good_unit']] : '';
  899. $value['companyName'] = $names['data'][$value['companyNo']]??'';//isset($names['data'][$value['companyNo']]) ? $business[$value['companyNo']] : '';
  900. //规格信息
  901. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  902. $speclist = [];
  903. if (!empty($spec)) {
  904. foreach ($spec as $val) {
  905. $temp = [];
  906. $temp['spec_id'] = $val['spec_id'];
  907. $temp['spec_value_id'] = $val['spec_value_id'];
  908. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  909. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  910. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  911. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  912. $speclist[] = $temp;
  913. }
  914. }
  915. $value['specinfo'] = $speclist;
  916. $value['cgder'] = $supplier['person'] ?? '';//isset($supplier['person']) ? $supplier['person'] : '';
  917. $value['company_name'] = $item[$value['createrid']]??'';
  918. $data[] = $value;
  919. }
  920. }
  921. return app_show(0, "获取成功", ["list" => $data, "count" => $count]);
  922. }
  923. }