post = $this->request->filter('trim')->post(); $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1"; $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10"; $where = [["a.is_del", "=", 0], ['wpo.supplierNo', '=', $this->request->user['supplierNo']], ['b.is_stock', '=', '0']]; $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : ""; if ($orderCode !== "") { $where[] = ['a.orderCode', "like", "%$orderCode%"]; } $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : ""; if ($order_type !== "") { $where[] = ['a.order_type', "=", $order_type]; } $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : ""; if ($outCode !== "") { $where[] = ['a.outCode', "like", "%$outCode%"]; } $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : ""; if ($apply_name !== "") { $where[] = ['a.apply_name', "like", "%$apply_name%"]; } $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : ""; if ($good_code !== "") { $where[] = ['b.good_code', "like", "%$good_code%"]; } $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : ""; if ($good_name !== "") { $where[] = ['b.good_name', "like", "%$good_name%"]; } $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : ""; if ($status !== "") { $where[] = ['a.status', "=", $status]; } $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : ""; if ($start != "") { $where[] = ["a.addtime", '>=', $start]; } $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : ""; if ($end != "") { $where[] = ["a.addtime", '<=', $end]; } $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : ""; if ($companyNo != "") { $where[] = ['b.customer_code', "like", "%$companyNo%"]; } $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : ""; if ($cgdNo != "") { $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%']; } // $role = $this->checkRole(); // $condition = ''; // if (!empty($role['write']) && $this->uid != "") { // // $where[]=["a.apply_id","in",$role['write']]; // $condition .= " (b.is_stock=1 and n.contactor = {$this->uid}) or (b.is_stock=0 and wpo.cgder_id = {$this->uid}) or a.apply_id in (" . implode(',', // $role['write']) . ")"; // } // if(!empty($role['platform']) ){ // $where[]=["b.platform_id","in",$role['platform']]; // } $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : ""; if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)]; //发货时间筛选 $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : ""; $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : ""; if ($start_sendtime != "" && $end_sendtime != "") { $where[] = ["a.sendtime", 'between', [$start_sendtime . " 00:00:00", $end_sendtime . " 223:59:59"]]; $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',) } $count = Db::name('order_out') ->alias('a') ->join("sale b", "b.orderCode=a.orderCode", "left") ->join("customer_info v", "v.companyNo=b.customer_code", "left") ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left") ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0") ->leftJoin("order_send os", "os.outCode=a.outCode") ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo") ->where($where) // ->where($condition) ->count(); $total = ceil($count / $size); $page = $page >= $total ? $total : $page; $list = Db::name('order_out') ->alias('a') ->join("sale b", "b.orderCode=a.orderCode", "left") ->join("customer_info v", "v.companyNo=b.customer_code", "left") ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left") ->leftJoin("order_send os", "os.outCode=a.outCode") ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo") ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,u.itemid") ->where($where) // ->where($condition) ->order("addtime desc") ->page($page, $size) ->select() ->toArray(); $all_createrid = array_column($list,'apply_id'); $item = get_company_name_by_uid($all_createrid); $data = []; foreach ($list as $value) { $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code") ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find(); $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find(); if ($addr) { $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : ''; if (is_string($addinfo) && $addinfo != '') { $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => '']; list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']); } $addr['addr_cn'] = GetAddr(json_encode($addinfo)); } if ($value['order_type'] == 3) { $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find(); } else { $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find(); } $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : ""; $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : ""; $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : ""; $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'] . $addr['addr'] : ""; $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : ""; $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : ""; $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : []; $value['company_name'] = $item[$value['apply_id']]??''; $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime']; $data[] = $value; } return json_show(0, "获取成功", ['list' => $data, 'count' => $count]); } //发货单详情 public function saleOutInfo() { $this->post = $this->request->filter('trim')->post(); $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : ""; if ($outCode == "") { return json_show(1002, "参数outcode不能为空"); } $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find(); if (empty($codeinfo)) { return json_show(1002, "未找到出库数据"); } $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find(); if ($item['order_type'] == 3) { $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find(); if ($goodinfo == false) { return json_show(1004, "未找到商品数据"); } } else { $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left') ->where(['a.skuCode' => $item['skuCode']])->find(); if ($goodinfo == false) { return json_show(1002, "未找到商品数据"); } } $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : []; $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find(); if ($addr) { $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : ''; if (is_string($addinfo) && $addinfo != '') { $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => '']; list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']); } $addr['addr_cn'] = GetAddr(json_encode($addinfo)); } $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : ""; $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : ""; $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : ""; $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : ""; $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : ""; $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : ""; $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : ""; $codeinfo['addr'] = $addr['addr_cn'] ?? ''; $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : ""; $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : ""; $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : ""; $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : ""; $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime']; $codeinfo['can'] = $int; $codeinfo['order_source'] = $item['order_source']; $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find(); $codeinfo['order_return'] = $orderReturn; return json_show(0, "获取成功", $codeinfo); } //库管发货 public function outSend() { $this->post = $this->request->filter('trim')->post(); $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : ""; if ($outCode == "") { return json_show(1004, "参数outCode不能为空"); } $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find(); if ($outinfo == false) { return json_show(1004, "发货数据未找到"); } $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find(); if (empty($einfo)) { return json_show(1002, "未找到销售订单数据"); } $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find(); if ($is_reurn != false) { return json_show(1002, "销售单存在退货未处理完成"); } $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : ""; if ($post_name == "") { return json_show(1004, "参数post_name不能为空"); } $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : ""; if ($post_code == "") { return json_show(1004, "参数post_code不能为空"); } $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : ""; $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : ""; $outinfo['post_name'] = $post_name; $outinfo['post_code'] = $post_code; $outinfo['post_fee'] = $post_fee; $outinfo['sendtime'] = date("Y-m-d H:i:s"); $old_outinfo_status = $outinfo['status']; $outinfo['status'] = 2; $outinfo['updatetime'] = date("Y-m-d H:i:s"); $outinfo['remark'] = $remark; if ($einfo['wsend_num'] < $outinfo['send_num']) { return json_show(1004, "订单未发货数量不足"); } Db::startTrans(); try { $up = Db::name("order_out")->save($outinfo); // $user_info = GetUserInfo($this->post['token']); $uid = $this->request->user['uid']; $uname = $this->request->user['nickname']; if ($up) { //修改状态,添加待办 ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [ "order_code" => $outinfo['outCode'],//出库单号 "status" => $old_outinfo_status,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "edit"//新建create,编辑edit,更改状态status ], "CKD", $outinfo['status'], $outinfo); ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [ "order_type" => 'CKD', "order_code" => $outinfo['outCode'],//出库单号 "order_id" => $outinfo['id'], "order_status" => $outinfo['status'], "before_status" => $old_outinfo_status, 'wait_id' => $outinfo['apply_id'], 'wait_name' => $outinfo['apply_name'], 'holder_id' => $outinfo['apply_id'], ]); $orderstatus = $einfo['status']; $einfo['send_num'] += $outinfo['send_num']; $einfo['wsend_num'] -= $outinfo['send_num']; $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1); $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2); $einfo['updatetime'] = date("Y-m-d H:i:s"); $saleup = Db::name("sale")->save($einfo); if ($saleup == false) { Db::rollback(); return json_show(1002, "销售单状态更新失败"); } // } //修改状态,添加待办 ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [ "order_code" => $einfo['orderCode'],//出库单号 "status" => $orderstatus,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "edit"//新建create,编辑edit,更改状态status ], "XSQRD", $einfo['status'], $einfo); ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [ "order_type" => 'XSQRD', "order_code" => $einfo['orderCode'],//出库单号 "order_id" => $einfo['id'], "order_status" => $einfo['status'], "before_status" => $orderstatus, 'holder_id' => $einfo['apply_id'], 'person_id' => Db::name('supplier')->where(['code' => $this->request->user['supplierNo'], 'is_del' => 0])->value('personid'), ]); $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find(); if ($stokc == false) { Db::rollback(); return json_show(1002, "未找到库存数据"); } else { if ($outinfo['send_num'] > $stokc['wait_out_stock']) { Db::rollback(); return json_show(1002, "超出库存数量"); } $stokc['wait_out_stock'] -= $outinfo['send_num']; $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock']; // $stokc['intra_stock']+=$outinfo['send_num']; $stokc['updatetime'] = date("Y-m-d H:i:s"); } $stoc = Db::name("good_stock")->save($stokc); if ($stoc == false) { Db::rollback(); return json_show(1002, "库存更新失败"); } if($einfo["order_type"]!=1){ $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty(); if(empty($outsend)){ Db::rollback(); return error_show(1002, "未找到关联采购单"); } $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0]) ->findOrEmpty(); $bn_code =makeNo("BN"); $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num'],0); if($bnup==false){ Db::rollback(); return error_show(1004,"库存bn库存数更新失败"); } } $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"]; GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD"); Cache::store("redis")->handler()->lPush("SENDOUT", $outCode); Db::commit(); } else { Db::rollback(); return json_show(1004, "发货失败"); } //如果是有赞订单的话,将发货信息推到有赞 //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起 if ($einfo['order_source'] == 5) { $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]); $res = json_decode($res, true); if ($res['code'] != 0) return json_show(0, '发货成功,' . $res['message']); } return json_show(0, "发货成功"); } catch (\Exception $e) { Db::rollback(); return json_show(1004, $e->getMessage()); } } //库管批量发货 public function outSendBatchByImport() { $param = $this->request->only(['list'], 'post', 'trim'); if (empty($param['list'])) return json_show(1005, 'list参数不能为空'); //只允许处理所属供应商下的采购单关联的发货单 $temp = Db::name('order_out') ->alias('a') ->leftJoin('order_num b', 'b.orderCode=a.orderCode') ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo') ->where([ ['a.is_del', '=', 0], ['a.outCode', 'in', array_column($param['list'], 'outCode')], ['c.supplierNo', '<>', $this->request->user['supplierNo']] ])->column('a.outCode'); if (!empty($temp)) return json_show(1003, '不允许处理其他供应商的单子', $temp); // $user = GetUserInfo($param['token']); $createrid = $this->request->user['uid']; $creater = $this->request->user['nickname']; //所有发货单信息 $order_out_infos = Db::name("order_out") ->where('is_del', 0) ->whereIn('outCode', array_column($param['list'], 'outCode')) ->whereIn('status', [0, 1]) ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode'); //处理数据 Db::startTrans(); try { $good_data = []; $temp_out_codes = []; foreach ($param['list'] as $value) { if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货'); //每次循环都需要重新查询对应数据,为了数据及时更新 $sale_infos = Db::name("sale") ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode']) ->where('is_del', 0) ->column('id,send_num,wsend_num,status,good_code,order_type,order_source,apply_id', 'orderCode'); if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到'); //判断发货单是否重复 if (isset($temp_out_codes[$value['outCode']])) { Db::rollback(); return json_show(1005, '下列发货申请单号重复', [$value]); } else $temp_out_codes[$value['outCode']] = $value['outCode']; //【发货单】 //查询已有发货单信息及状态 //更新发货单 $up = Db::name("order_out") ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0]) ->update([ 'post_name' => isset($value['post_name']) ? $value['post_name'] : '', 'post_code' => $value['post_code'], 'post_fee' => $value['post_fee'], 'sendtime' => date('Y-m-d H:i:s'), 'status' => 2, 'updatetime' => date('Y-m-d H:i:s'), 'remark' => isset($value['remark']) ? $value['remark'] : '', ]); if ($up) { //修改状态,添加待办 ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [ "order_code" => $value['outCode'],//出库单号 "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "edit"//新建create,编辑edit,更改状态status ], "CKD", 2, $order_out_infos[$value['outCode']]); ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [ "order_type" => 'CKD', "order_code" => $value['outCode'],//出库单号 "order_id" => $order_out_infos[$value['outCode']]['id'], "order_status" => 2, "before_status" => $order_out_infos[$value['outCode']]['status'], 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'], 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'], 'holder_id' => $order_out_infos[$value['outCode']]['apply_id'], 'person_id' => Db::name('supplier')->where(['code' => $this->request->user['supplierNo'], 'is_del' => 0])->value('personid'), ]); //【销售单】 $einfo = []; $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num']; $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num']; $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1); $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2); $einfo['updatetime'] = date("Y-m-d H:i:s"); $saleup = Db::name("sale") ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id']) ->update($einfo); if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败'); //修改状态,添加待办 ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [ "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号 "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "edit"//新建create,编辑edit,更改状态status ], "XSQRD", $einfo['status'], $einfo); ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [ "order_type" => 'XSQRD', "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号 "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'], "order_status" => $einfo['status'], "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'], 'holder_id' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['apply_id'], 'person_id' => Db::name('supplier')->where(['code' => $this->request->user['supplierNo'], 'is_del' => 0])->value('personid'), ]); //【库存】 $stock = Db::name("good_stock") ->field('id,wait_out_stock,usable_stock,total_stock,updatetime') ->where([ "is_del" => 0, 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'], "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'], ])->find(); if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据'); else { if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量'); $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num']; $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock']; $stock['updatetime'] = date("Y-m-d H:i:s"); $stock_rs = Db::name("good_stock") ->where('id', $stock['id']) ->update($stock); if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败'); if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){ $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty(); if(empty($outsend)){ Db::rollback(); return error_show(1002, "未找到关联采购单"); } $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0]) ->findOrEmpty(); $bn_code =makeNo("BN"); $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num'],0); if($bnup==false){ Db::rollback(); return error_show(1004,"库存bn库存数更新失败"); } } $good_data[] = [ 'good_log_code' => $value['outCode'], 'stock_id' => $stock['id'], 'type' => 2, 'stock' => $order_out_infos[$value['outCode']]['send_num'], 'stock_name' => 'wait_out_stock' ]; } } else throw new Exception($value['outCode'] . '发货失败'); Cache::store("redis")->handler()->lPush("SENDOUT", $value['outCode']); } if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD"); Db::commit(); return json_show(0, '发货成功'); } catch (Exception $exception) { Db::rollback(); return json_show(1004, $exception->getMessage()); } } //咨询商品详情 public function goodzxinfo() { $this->post = $this->request->filter('trim')->post(); $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : ""; if ($orderCode == "") { return json_show(1003, "参数spuCode不能为空"); } $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find(); if (empty($order)) { return json_show(1003, "订单数据未找到"); } $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find(); if ($order['brand_id'] != 0) { $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find(); $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : ""; } else { $order["brand_name"] = ""; $order["brand_id"] = ""; } $order['specinfo'] = json_decode($order['specinfo'], true); $order['unit'] = isset($unit['unit']) ? $unit['unit'] : ''; $order['cat_info'] = made($order['cat_id'], []); $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : ""; if ($order['is_gold_price'] == 1) { $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find(); } $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find(); $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : ""; $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find(); $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : ""; $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find(); $order['company'] = isset($company['company']) ? $company['company'] : ""; $order["gold_price"] = isset($price['price']) ? $price['price'] : 0; $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0; return json_show(0, "获取成功", $order); } //发货申请单导出 public function exportSaleOut() { $outCodes = $this->request->filter('trim')->post('outCodes'); if (empty($outCodes)) return json_show(1004, '要导出的发货申请单编号不能为空'); $i = 1; $send_type = [1 => '直接发货', 2 => '延时发货']; $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货']; $list = Db::name("order_out") ->alias('a') ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,b.good_code as 产品编号,b.good_name as 产品名称,"" as 规格,"" as 单位,wi.name as 供应商名称,s.code as 供应商编号,po.nake_fee as 裸价,a.post_fee as 物流费,b.send_type as 发货方式,po.good_price as 采购单价,b.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,a.send_num 发货数量,oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类') ->whereIn('a.outCode', $outCodes) ->order("a.addtime desc") ->withAttr('序号', function () use (&$i) { return $i++; })->withAttr('采购单状态', function ($val) use ($status) { return isset($status[$val]) ? $status[$val] : ''; })->withAttr('发货方式', function ($val) use ($send_type) { return isset($send_type[$val]) ? $send_type[$val] : ''; })->where("a.is_del", "=", 0) ->leftJoin("sale b", "b.orderCode=a.orderCode") ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0") ->leftJoin("warehouse_info wi", "wi.wsm_code=a.wsm_code AND wi.is_del=0") ->leftJoin("supplier s", "s.code=wi.supplierNo AND s.is_del=0") ->leftJoin('order_send os', 'os.outCode=a.outCode ') ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0") ->select() ->toArray(); foreach ($list as &$value) { if ($value['order_type'] == 3) { $temp = Db::name("good_zixun") ->where(["spuCode" => $value['产品编号'], "is_del" => 0]) ->field('id,specinfo,good_unit') ->find(); $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0; $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : []; $speclist = []; foreach ($specinfo as $val) { $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name']; } } else { $good_unit = Db::name('good_basic') ->where(['spuCode' => $value['产品编号']]) ->value('good_unit', 0); $spec = Db::name("good_spec") ->field('id,spec_id,spec_value_id') ->where(["spuCode" => $value['产品编号'], "is_del" => 0]) ->select() ->toArray(); $speclist = []; if (!empty($spec)) { foreach ($spec as $val) { $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', ''); } } } $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);; $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : ''; $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name')); $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字) $addinfo = $value['addr_code'] != '' ? json_decode($value['addr_code'], true) ?? $value['addr_code'] : ''; if (is_string($addinfo) && $addinfo != '') { $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => '']; list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $value['addr_code']); } $addr_cn = GetAddr(json_encode($addinfo)); $value['收货地址'] = $addr_cn . $value['收货地址']; unset($value['order_type']); unset($value['addr_code']); } if (empty($list)) $list[] = '没有可供导出的数据'; $headerArr = array_keys($list[0]); excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list); } //发货单详情 public function outInfo() { $outCode = $this->request->filter('trim')->post('outCode'); //isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : ""; if ($outCode == "") { return json_show(1002, "参数outcode不能为空"); } $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find(); if (empty($codeinfo)) { return json_show(1002, "未找到出库数据"); } $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find(); if ($item['order_type'] == 3) { $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find(); if ($goodinfo == false) { return json_show(1004, "未找到商品数据"); } } else { $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left') ->where(['a.skuCode' => $item['skuCode']])->find(); if ($goodinfo == false) { return json_show(1002, "未找到商品数据"); } } $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : []; $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find(); if ($addr) { $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : ''; if (is_string($addinfo) && $addinfo != '') { $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => '']; list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']); } $addr['addr_cn'] = GetAddr(json_encode($addinfo)); } $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : ""; $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : ""; $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : ""; $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : ""; $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : ""; $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : ""; $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : ""; $codeinfo['addr'] = $addr['addr_cn'] ?? ''; $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : ""; $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : ""; $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : ""; $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : ""; $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime']; $codeinfo['can'] = $int; $codeinfo['order_source'] = $item['order_source']; $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find(); $codeinfo['order_return'] = $orderReturn; return json_show(0, "获取成功", $codeinfo); } //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo public function getGoodZxInfoByNotZixun() { $param = $this->request->only(['spuCode'], 'post', 'trim'); $val = Validate::rule(['spuCode' => 'require']); if (!$val->check($param)) return json_show(1004, $val->getError()); $order = Db::name('consult_bids') ->field(true) ->where(['spuCode' => $param['spuCode'], 'is_del' => 0]) ->find(); if (empty($order)) return json_show(1003, "数据未找到"); // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):""; // if($orderCode==""){ // return json_show(1003,"参数spuCode不能为空"); // } // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find(); // if(empty($order)){ // return json_show(1003,"订单数据未找到"); // } $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find(); if ($order['brand_id'] != 0) { $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find(); $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : ""; } else { $order["brand_name"] = ""; $order["brand_id"] = ""; } $order['specinfo'] = json_decode($order['specinfo'], true); $order['unit'] = isset($unit['unit']) ? $unit['unit'] : ''; $order['cat_info'] = made($order['cat_id'], []); $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : ""; if ($order['is_gold_price'] == 1) { $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find(); } $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find(); $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : ""; // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find(); // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:""; $order["gold_price"] = isset($price['price']) ? $price['price'] : 0; $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0; return json_show(0, "获取成功", $order); } }