Allot.php 48 KB

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