|
|
@@ -6,6 +6,12 @@ use App\Model\DDEmployee;
|
|
|
use App\Model\Field;
|
|
|
use App\Model\FieldData;
|
|
|
use App\Model\Inventory;
|
|
|
+use App\Model\PoPodetails;
|
|
|
+use App\Model\PoPomain;
|
|
|
+use App\Model\PuAppVouch;
|
|
|
+use App\Model\PuAppVouchs;
|
|
|
+use App\Model\RdRecord01;
|
|
|
+use App\Model\RdRecords01;
|
|
|
use App\Model\Record;
|
|
|
use App\Model\U8State;
|
|
|
use App\Model\Vendor;
|
|
|
@@ -201,6 +207,7 @@ class U8XkyServerService extends Service
|
|
|
|
|
|
public function inventoryCommon($data, $user, $field = []){
|
|
|
$type = U8State::type_four;
|
|
|
+ $qx = $user['qx'];
|
|
|
|
|
|
$model = Inventory::from('inventory as i')
|
|
|
->leftJoin('u8_state as s', function ($join) use ($type, $user) {
|
|
|
@@ -211,6 +218,9 @@ class U8XkyServerService extends Service
|
|
|
})
|
|
|
->where('i.del_time', 0)
|
|
|
->where('i.login_type', $user['login_type'])
|
|
|
+ ->when(empty($qx), function ($query) use($user){
|
|
|
+ return $query->where('v.crt_id', $user['userid']);
|
|
|
+ })
|
|
|
->select('i.*', DB::raw('IFNULL(s.state, -1) as state'),'s.result')
|
|
|
->orderby('i.id', 'desc');
|
|
|
|
|
|
@@ -429,6 +439,7 @@ class U8XkyServerService extends Service
|
|
|
|
|
|
public function vendorCommon($data, $user, $field = []){
|
|
|
$type = U8State::type_five;
|
|
|
+ $qx = $user['qx'];
|
|
|
|
|
|
$model = Vendor::from('vendor as v')
|
|
|
->leftJoin('u8_state as s', function ($join) use ($type, $user) {
|
|
|
@@ -438,7 +449,9 @@ class U8XkyServerService extends Service
|
|
|
->where('s.del_time', '=', 0);
|
|
|
})
|
|
|
->where('v.del_time', 0)
|
|
|
-// ->where('v.crt_id', $user['userid'])
|
|
|
+ ->when(empty($qx), function ($query) use($user){
|
|
|
+ return $query->where('v.crt_id', $user['userid']);
|
|
|
+ })
|
|
|
->select('v.*', DB::raw('IFNULL(s.state, -1) as state'),'s.result')
|
|
|
->orderby('v.id', 'desc');
|
|
|
|
|
|
@@ -525,20 +538,6 @@ class U8XkyServerService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
- public function filedCommon1($data, $user, $field = []){
|
|
|
- $model = FieldData::select([
|
|
|
- 'userid',
|
|
|
- 'type',
|
|
|
- // 使用 GROUP_CONCAT 将 title 合并,并起别名为 titles
|
|
|
- DB::raw('GROUP_CONCAT(title SEPARATOR ",") as titles')
|
|
|
- ])
|
|
|
- ->where('login_type', $user['login_type'])
|
|
|
- ->groupBy('userid', 'type')
|
|
|
- ->orderBy('type', 'desc'); // 或者根据你的需求排序
|
|
|
-
|
|
|
- return $model;
|
|
|
- }
|
|
|
-
|
|
|
public function getField($data, $user){
|
|
|
if(empty($data['type'])) return [false, 'type类型不能为空'];
|
|
|
$type = $data['type'];
|
|
|
@@ -746,300 +745,1016 @@ class U8XkyServerService extends Service
|
|
|
|
|
|
//新增供应商到本地-----------------------------------------------
|
|
|
|
|
|
- //U8 存货库存列表
|
|
|
- public function stockList($data, $user)
|
|
|
- {
|
|
|
+ //新增采购请购单到本地--------------------------------------------
|
|
|
+
|
|
|
+ public function purchaseRequisitionAdd($data, $user){
|
|
|
+ list($status, $msg) = $this->purchaseRequisitionRule($data, $user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
try {
|
|
|
- $field_list = [];
|
|
|
- $employee = DDEmployee::where('userid', $user['userid'])->where('login_type', $user['login_type'])->first();
|
|
|
- $qx = $employee['qx'] ?? 0;
|
|
|
- if(empty($qx)) {
|
|
|
- $field_list = FieldData::where('userid', $user['userid'])
|
|
|
- ->where('login_type', $user['login_type'])
|
|
|
- ->where('type', FieldData::STATE_ZERO)
|
|
|
- ->pluck('key')
|
|
|
- ->all();
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = new PuAppVouch();
|
|
|
+ $model->order_number = $this->generateBillNo([
|
|
|
+ 'type' => PuAppVouch::Order_type,
|
|
|
+ 'login_type' => $user['login_type'],
|
|
|
+ 'period' => date("Ym")
|
|
|
+ ]);
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType']; // 业务类型前端写死
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->login_type = $user['login_type'];
|
|
|
+ $model->crt_id = $user['userid'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $this->saveDetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function saveDetail($id, $data){
|
|
|
+ if(! empty($data['details'])){
|
|
|
+ $unit = [];
|
|
|
+ foreach ($data['details'] as $value){
|
|
|
+ $unit[] = [
|
|
|
+ 'main_id' => $id,
|
|
|
+ 'cInvCode' => $value['cInvCode'],
|
|
|
+ 'quantity' => $value['quantity'],
|
|
|
+ 'price' => $value['price'] ?? 0,
|
|
|
+ 'dRequirDate' => $value['dRequirDate'],
|
|
|
+ 'dArriveDate' => $value['dArriveDate'],
|
|
|
+ 'rate' => $value['rate'],
|
|
|
+ 'name' => $value['name'],
|
|
|
+ 'size' => $value['size'] ?? '',
|
|
|
+ 'unit' => $value['unit'] ?? '',
|
|
|
+ ];
|
|
|
}
|
|
|
+ if(! empty($unit)) PuAppVouchs::insert($unit);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getDetail($id){
|
|
|
+ $data = PuAppVouchs::where('del_time',0)
|
|
|
+ ->where('main_id', $id)
|
|
|
+ ->get()->toArray();
|
|
|
+
|
|
|
+ $detail = [
|
|
|
+ 'details' => $data,
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $detail;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseRequisitionEdit($data, $user){
|
|
|
+ list($status, $msg) = $this->purchaseRequisitionRule($data, $user, false);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = PuAppVouch::where('id', $data['id'])->first();
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType'];
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ PuAppVouchs::where('del_time',0)
|
|
|
+ ->where('main_id', $data['id'])
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+
|
|
|
+ $this->saveDetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, "编辑请购单失败: " . $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseRequisitionRule(&$data, $user, $is_add = true){
|
|
|
+ if(! $is_add && empty($data['id'])) return [false, 'ID不能为空'];
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+
|
|
|
+ if(empty($data['order_time'])) return [false, '单据日期不能为空'];
|
|
|
+ $orderTime = trim($data['order_time']);
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $orderTime) || strtotime($orderTime) === false) {
|
|
|
+ return [false, '单据日期格式不正确,必须为 YYYY-MM-DD 格式(如:2026-04-04)'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($data['details'])) return [false, '表体不能为空'];
|
|
|
+ foreach ($data['details'] as $key => $value){
|
|
|
+ $line = "第 " . ($key + 1) . "行";
|
|
|
+ if(empty($value['cInvCode'])) return [false, $line . '存货编码不能为空'];
|
|
|
+ if(empty($value['name'])) return [false, $line . '存货名称不能为空'];
|
|
|
+ if(! is_numeric($value['quantity']) || floatval($value['quantity']) <= 0.0) return [false, $line . '存货数量不正常'];
|
|
|
+ if(! isset($value['rate']) || ! is_numeric($value['rate'])) return [false, $line . '税率为空或非数字'];
|
|
|
+
|
|
|
+ if(empty($value['dRequirDate'])) return [false, $line . '需求日期不能为空'];
|
|
|
+ if(empty($value['dArriveDate'])) return [false, $line . '建议订货日期不能为空'];
|
|
|
|
|
|
- // 1. 构建基础查询:关联现存量表和存货档案表
|
|
|
- $query = DB::connection('sqlsrv')
|
|
|
- ->table('CurrentStock as S')
|
|
|
- ->select([
|
|
|
- 'S.cWhCode', // 仓库编码
|
|
|
- 'W.cWhName', // 仓库名称
|
|
|
- 'S.cInvCode', // 存货编码
|
|
|
- 'I.cInvName', // 存货名称
|
|
|
- 'I.cInvStd', // 规格型号
|
|
|
- 'I.cInvCCode', // 分类编码
|
|
|
-
|
|
|
- // --- 数量字段对齐你的结构 ---
|
|
|
- 'S.iQuantity', // 结存数量 (账面现存量)
|
|
|
- 'S.fAvaQuantity', // 可用数量
|
|
|
- 'S.fOutQuantity', // 待发货数量 (待出)
|
|
|
- 'S.fInQuantity', // 待入库数量 (待入)
|
|
|
- 'S.fStopQuantity', // 冻结数量
|
|
|
-
|
|
|
- // --- 批次与日期 ---
|
|
|
- 'S.cBatch', // 批号
|
|
|
- 'S.dMdate', // 生产日期
|
|
|
- 'S.dVDate', // 失效日期
|
|
|
- ])
|
|
|
- ->join('Inventory as I', 'S.cInvCode', '=', 'I.cInvCode')
|
|
|
- ->leftJoin('Warehouse as W', 'S.cWhCode', '=', 'W.cWhCode')
|
|
|
- ->leftJoin('InventoryClass as IC', 'I.cInvCCode', '=', 'IC.cInvCCode');
|
|
|
-
|
|
|
- // 2. 过滤条件:存货名称 (支持模糊查询)
|
|
|
- if (!empty($data['material_title'])) {
|
|
|
- $query->where('I.cInvName', 'like', '%' . $data['material_title'] . '%');
|
|
|
+ $dRequirDate = trim($value['dRequirDate']);
|
|
|
+ $dArriveDate = trim($value['dArriveDate']);
|
|
|
+
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dRequirDate) || strtotime($dRequirDate) === false) {
|
|
|
+ return [false, $line . "需求日期格式不正确,必须为 YYYY-MM-DD"];
|
|
|
+ }
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dArriveDate) || strtotime($dArriveDate) === false) {
|
|
|
+ return [false, $line . "建议订货日期格式不正确,必须为 YYYY-MM-DD"];
|
|
|
}
|
|
|
|
|
|
- // 2. 过滤条件:存货编码 (支持模糊查询)
|
|
|
- if (!empty($data['material_code'])) {
|
|
|
- $query->where('S.cInvCode', 'like', '%' . $data['material_code'] . '%');
|
|
|
+ $timeOrder = strtotime($orderTime);
|
|
|
+ $timeRequir = strtotime($dRequirDate);
|
|
|
+ $timeArrive = strtotime($dArriveDate);
|
|
|
+
|
|
|
+ if ($timeArrive < $timeOrder) {
|
|
|
+ return [false, $line . "建议订货日期({$dArriveDate})不能早于单据日期({$orderTime})"];
|
|
|
+ }
|
|
|
+ if ($timeRequir < $timeArrive) {
|
|
|
+ return [false, $line . "需求日期({$dRequirDate})不能早于建议订货日期({$dArriveDate})"];
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! $is_add){
|
|
|
+ $order = PuAppVouch::where('id', $data['id'])->where('del_time',0)->first();
|
|
|
+ if(empty($order)) return [false, '请购单信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_two, $user, $order['order_number']);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseRequisitionCommon($data, $user, $field = []){
|
|
|
+ $type = U8State::type_two;
|
|
|
+ $qx = $user['qx'];
|
|
|
|
|
|
- // 3. 过滤条件:存货分类 (支持左匹配,即选大类查出所有子类)
|
|
|
- if (!empty($data['category_code'])) {
|
|
|
- // U8 分类是级次结构,用 like '01%' 可以查出 01 开头的所有子类
|
|
|
- $query->where('I.cInvCCode', 'like', $data['category_code'] . '%');
|
|
|
+ $model = PuAppVouch::from('pu_appvouch as v')
|
|
|
+ ->leftJoin('u8_state as s', function ($join) use ($type, $user) {
|
|
|
+ $join->on('v.order_number', '=', 's.order_number')
|
|
|
+ ->where('s.type', '=', $type)
|
|
|
+ ->where('s.login_type', '=', $user['login_type'])
|
|
|
+ ->where('s.del_time', '=', 0);
|
|
|
+ })
|
|
|
+ ->where('v.del_time', 0)
|
|
|
+ ->when(empty($qx), function ($query) use($user){
|
|
|
+ return $query->where('v.crt_id', $user['userid']);
|
|
|
+ })
|
|
|
+ ->select('v.*', DB::raw('IFNULL(s.state, -1) as state'),'s.result')
|
|
|
+ ->orderby('v.id', 'desc');
|
|
|
+
|
|
|
+ if (isset($data['state']) && $data['state'] !== '') {
|
|
|
+ $state_filter = $data['state'];
|
|
|
+ if ($state_filter == -1) {
|
|
|
+ // 未发起审批:u8_states 表里没记录,s.id 就会是 NULL
|
|
|
+ $model->whereNull('s.id');
|
|
|
+ } else {
|
|
|
+ // 0:待审核 1:通过 2:驳回
|
|
|
+ $model->where('s.state', $state_filter);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 6. 排序
|
|
|
- $query->orderBy('S.cInvCode', 'asc')->orderBy('S.cWhCode', 'asc');
|
|
|
+ if(! empty($data['code'])) $model->where('v.code', 'like', '%' . $data['code'] . '%');
|
|
|
+ if(! empty($data['id'])) $model->where('v.id', $data['id']);
|
|
|
+ if(! empty($data['order_number'])) $model->where('v.order_number', 'like', '%' . $data['order_number'] . '%');
|
|
|
+ if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+ $model->where('v.crt_time', '>=', $return[0]);
|
|
|
+ $model->where('v.crt_time', '<=', $return[1]);
|
|
|
+ }
|
|
|
|
|
|
- // 7. 调用你定义的分页方法
|
|
|
- $columns = ['*'];
|
|
|
- $result = $this->limit($query, $columns, $data);
|
|
|
+ return $model;
|
|
|
+ }
|
|
|
|
|
|
- // 注意这里的 &$item,加了 & 符号才能直接修改原数组里的内容
|
|
|
- foreach ($result['data'] as &$item) {
|
|
|
- $numFields = ['iQuantity', 'fAvaQuantity', 'fOutQuantity', 'fInQuantity', 'fStopQuantity'];
|
|
|
+ public function purchaseRequisitionList($data, $user){
|
|
|
+ $model = $this->purchaseRequisitionCommon($data, $user);
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillPurchaseRequisitionData($list,$user);
|
|
|
|
|
|
- foreach ($numFields as $field) {
|
|
|
- if (isset($item->$field)) {
|
|
|
- $item->$field = (float)$item->$field;
|
|
|
- }
|
|
|
- }
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
|
|
|
- $item->dMdate = $item->dMdate ? date('Y-m-d', strtotime($item->dMdate)) : '';
|
|
|
- $item->dVDate = $item->dVDate ? date('Y-m-d', strtotime($item->dVDate)) : '';
|
|
|
+ public function fillPurchaseRequisitionData($data, $user){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
|
|
|
- // 脱敏处理
|
|
|
- if (!empty($field_list)) {
|
|
|
- foreach ($field_list as $blackField) {
|
|
|
- if (isset($item->$blackField)) {
|
|
|
- $item->$blackField = '*****';
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- unset($item); // 销毁引用
|
|
|
+ $e_map = DDEmployee::whereIn('userid', array_unique(array_column($data['data'], 'crt_id')))
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->pluck('name', 'userid')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['crt_name'] = $e_map[$value['crt_id']] ?? "";
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['state_title'] = U8State::state_name[$value['state']];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseRequisitionMyDetail($data, $user){
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+ $order_number = $data['order_number'] ?? "";
|
|
|
+ if(empty($id) && empty($order_number)) return [false,'请选择数据'];
|
|
|
+
|
|
|
+ $customer = PuAppVouch::where('del_time',0)
|
|
|
+ ->when(! empty($id), function ($query) use($id){
|
|
|
+ return $query->where('id', $id);
|
|
|
+ })
|
|
|
+ ->when(! empty($order_number), function ($query) use($order_number){
|
|
|
+ return $query->where('id', $order_number);
|
|
|
+ })
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false,'采购请购单不存在或已被删除'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ $customer['crt_name'] = DDEmployee::where('id',$customer['crt_id'])->value('name');
|
|
|
+ $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
+
|
|
|
+ $details = $this->getDetail($data['id']);
|
|
|
+ $customer = array_merge($customer, $details);
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseRequisitionDel($data, $user){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ $order = PuAppVouch::where('id', $data['id'])->where('del_time',0)->first();
|
|
|
+ if(empty($order)) return [false, '请购单信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_two, $user, $order['order_number']);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
|
|
|
- return [true, $result];
|
|
|
+ PuAppVouch::where('del_time',0)
|
|
|
+ ->where('id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ PuAppVouchs::where('del_time',0)
|
|
|
+ ->where('main_id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增采购订单到本地--------------------------------------------
|
|
|
|
|
|
+ public function purchaseOrderAdd($data, $user){
|
|
|
+ list($status, $msg) = $this->purchaseOrderRule($data, $user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = new PoPomain();
|
|
|
+ $model->order_number = $this->generateBillNo([
|
|
|
+ 'type' => PoPomain::Order_type,
|
|
|
+ 'login_type' => $user['login_type'],
|
|
|
+ 'period' => date("Ym")
|
|
|
+ ]);
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType'];
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->cVenCode = $data['cVenCode'];
|
|
|
+ $model->cVenCodeTitle = $data['cVenCodeTitle'];
|
|
|
+ $model->cappcode = !empty($data['cappcode']) ? (array)$data['cappcode'] : [];
|
|
|
+ $model->nflat = $data['nflat'] ?? 1;
|
|
|
+ $model->cexch_name = $data['cexch_name'] ?? '人民币';
|
|
|
+ $model->iTaxRate = $data['iTaxRate'] ?? 13;
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->login_type = $user['login_type'];
|
|
|
+ $model->crt_id = $user['userid'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $this->saveODetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
} catch (\Throwable $exception) {
|
|
|
- return [false, "查询库存失败: " . $exception->getMessage()];
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
}
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
}
|
|
|
|
|
|
- //U8 供应商列表
|
|
|
- public function vendorU8List($data, $user)
|
|
|
- {
|
|
|
- // 1. 构建基础查询
|
|
|
- $query = DB::connection('sqlsrv')
|
|
|
- ->table('Vendor as V')
|
|
|
- ->select([
|
|
|
- 'V.cVenCode', // 供应商编码
|
|
|
- 'V.cVenName', // 供应商名称
|
|
|
- 'V.cVenAbbName', // 供应商简称
|
|
|
- 'V.cVCCode', // 分类编码
|
|
|
- 'VC.cVCName', // 分类名称 (来自 VendorClass)
|
|
|
- 'V.cVenAddress', // 地址
|
|
|
- 'V.cVenPhone', // 电话
|
|
|
- 'V.dVenDevDate', // 发展日期
|
|
|
- 'V.cCreatePerson', // 创建人
|
|
|
- 'V.bVenTax', // 是否计税
|
|
|
- 'V.iId' // 内部ID
|
|
|
- ])
|
|
|
- // 关联供应商分类表获取分类名称
|
|
|
- ->leftJoin('VendorClass as VC', 'V.cVCCode', '=', 'VC.cVCCode');
|
|
|
-
|
|
|
- // 2. 增加搜索逻辑 (可选)
|
|
|
- if (!empty($data['keyword'])) {
|
|
|
- $keyword = $data['keyword'];
|
|
|
- $query->where(function($q) use ($keyword) {
|
|
|
- $q->where('V.cVenCode', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('V.cVenName', 'like', "%{$keyword}%")
|
|
|
- ->orWhere('V.cVenAbbName', 'like', "%{$keyword}%");
|
|
|
- });
|
|
|
+ private function saveODetail($id, $data){
|
|
|
+ if(! empty($data['details'])){
|
|
|
+ $unit = [];
|
|
|
+ foreach ($data['details'] as $value){
|
|
|
+ $unit[] = [
|
|
|
+ 'main_id' => $id,
|
|
|
+ 'iAppIds' => $value['iAppIds'] ?? 0,
|
|
|
+ 'cInvCode' => $value['cInvCode'],
|
|
|
+ 'quantity' => $value['quantity'],
|
|
|
+ 'price' => $value['price'],
|
|
|
+ 'rate' => $value['rate'],
|
|
|
+ 'dArriveDate' => $value['dArriveDate'],
|
|
|
+ 'name' => $value['name'],
|
|
|
+ 'size' => $value['size'] ?? '',
|
|
|
+ 'unit' => $value['unit'] ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ if(! empty($unit)) PoPodetails::insert($unit);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 3. 排序 (默认按编码排序)
|
|
|
- $query->orderBy('V.cVenCode', 'ASC');
|
|
|
+ private function getODetail($id){
|
|
|
+ $data = PoPodetails::where('del_time',0)
|
|
|
+ ->where('main_id', $id)
|
|
|
+ ->get()->toArray();
|
|
|
|
|
|
- // 4. 调用你定义的分页方法
|
|
|
- // 注意:limit 方法内部会执行 paginate 并将结果填充到 $data
|
|
|
- $columns = ['*']; // select 已经在上面定义过了,这里传 * 即可
|
|
|
- $result = $this->limit($query, $columns, $data);
|
|
|
+ $detail = [
|
|
|
+ 'details' => $data,
|
|
|
+ ];
|
|
|
|
|
|
- return [true, $result];
|
|
|
+ return $detail;
|
|
|
}
|
|
|
|
|
|
- // U8 供应商分类树结构
|
|
|
- public function vendorClassTree($data, $user)
|
|
|
- {
|
|
|
+ public function purchaseOrderEdit($data, $user){
|
|
|
+ list($status, $msg) = $this->purchaseOrderRule($data, $user, false);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
try {
|
|
|
- // 1. 获取所有供应商分类 (表名: VendorClass)
|
|
|
- $classes = DB::connection('sqlsrv')
|
|
|
- ->table('VendorClass')
|
|
|
- ->select('cVCCode', 'cVCName', 'iVCGrade', 'bVCEnd') // 编码、名称、级次
|
|
|
- ->orderBy('cVCCode', 'asc')
|
|
|
- ->get();
|
|
|
-
|
|
|
- if ($classes->isEmpty()) {
|
|
|
- return [true, []];
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = PoPomain::where('id', $data['id'])->first();
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType'];
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->cVenCode = $data['cVenCode'];
|
|
|
+ $model->cVenCodeTitle = $data['cVenCodeTitle'];
|
|
|
+ $model->cappcode = !empty($data['cappcode']) ? (array)$data['cappcode'] : [];
|
|
|
+ $model->iTaxRate = $data['iTaxRate'] ?? 13;
|
|
|
+ $model->nflat = $data['nflat'] ?? 1;
|
|
|
+ $model->cexch_name = $data['cexch_name'] ?? '人民币';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ PoPodetails::where('del_time',0)
|
|
|
+ ->where('main_id', $data['id'])
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+
|
|
|
+ $this->saveODetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, "编辑采购订单失败: " . $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderRule(&$data, $user, $is_add = true)
|
|
|
+ {
|
|
|
+ if(! $is_add && empty($data['id'])) return [false, 'ID不能为空'];
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+
|
|
|
+ if(empty($data['order_time'])) return [false, '单据日期不能为空'];
|
|
|
+ $orderTime = trim($data['order_time']);
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $orderTime) || strtotime($orderTime) === false) {
|
|
|
+ return [false, '单据日期格式不正确,必须为 YYYY-MM-DD 格式(如:2026-04-04)'];
|
|
|
+ }
|
|
|
+ if(empty($data['cVenCode']) || empty($data['cVenCodeTitle'])) return [false, '供应商信息不能为空'];
|
|
|
+
|
|
|
+ // ==================== 核心修改:支持 JSON 字符串单号强校验 ====================
|
|
|
+ $u8DetailsMap = [];
|
|
|
+ $isRefApp = !empty($data['cappcode']);
|
|
|
+ if($isRefApp && ! is_array($data['cappcode'])) return [false, '请购单号非数组结构'];
|
|
|
+
|
|
|
+ if ($isRefApp) {
|
|
|
+ $service = new U8ServerService($user);
|
|
|
+ $error = $service->getError();
|
|
|
+ if(! empty($error)) return [false, $error];
|
|
|
+
|
|
|
+ // 1. 规整单号为干净的数组
|
|
|
+ $cappcodes = $data['cappcode'];
|
|
|
+ $cappcodes = array_filter(array_map('trim',$cappcodes));
|
|
|
+
|
|
|
+ // 【优化】如果数组被过滤后变空了,说明没有真正的参照单号,将 $isRefApp 标记为 false 即可,不直接报错
|
|
|
+ if (empty($cappcodes)) {
|
|
|
+ $isRefApp = false;
|
|
|
+ } else {
|
|
|
+ // 2. 调用用友接口,获取最新的可用明细
|
|
|
+ list($status, $u8Details) = $service->getRequisitionDetailsByCode($cappcodes);
|
|
|
+ if(! $status) return [false, $u8Details];
|
|
|
+
|
|
|
+ foreach ($u8Details as $u8Item) {
|
|
|
+ $u8DetailsMap[$u8Item['detail_id']] = $u8Item;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 统计本地已预占的数量
|
|
|
+ $localOccupiedQuery = DB::table('po_pomain as m')
|
|
|
+ ->join('po_podetails as d', 'd.main_id', '=', 'm.id')
|
|
|
+ ->where('m.del_time', 0)
|
|
|
+ ->where('d.del_time', 0)
|
|
|
+ ->where('m.code', ''); // 还没同步成功
|
|
|
+
|
|
|
+ // 如果是编辑操作,排除自身
|
|
|
+ if (!$is_add) {
|
|
|
+ $localOccupiedQuery->where('m.id', '!=', $id);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 JSON_CONTAINS 匹配本地主表中的 JSON 字段
|
|
|
+ $localOccupiedQuery->where(function ($query) use ($cappcodes) {
|
|
|
+ foreach ($cappcodes as $index => $code) {
|
|
|
+ if ($index === 0) {
|
|
|
+ $query->whereRaw('JSON_CONTAINS(m.cappcode, ?)', ['"' . $code . '"']);
|
|
|
+ } else {
|
|
|
+ $query->orWhereRaw('JSON_CONTAINS(m.cappcode, ?)', ['"' . $code . '"']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 聚合得出被预占的总数量
|
|
|
+ $localOccupiedRows = $localOccupiedQuery->select(
|
|
|
+ 'd.iAppIds',
|
|
|
+ DB::raw('SUM(d.quantity) as total_qty')
|
|
|
+ )
|
|
|
+ ->groupBy('d.iAppIds')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $localOccupiedMap = [];
|
|
|
+ foreach ($localOccupiedRows as $row) {
|
|
|
+ $localOccupiedMap[$row->iAppIds] = (float)$row->total_qty;
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
+ // ====================================================================
|
|
|
|
|
|
- // 2. 格式化数据,以编码为 Key
|
|
|
- $classList = [];
|
|
|
- foreach ($classes as $item) {
|
|
|
- $classList[$item->cVCCode] = [
|
|
|
- 'label' => $item->cVCName,
|
|
|
- 'value' => $item->cVCCode, // 前端通常需要 value 字段
|
|
|
- 'code' => $item->cVCCode,
|
|
|
- 'grade' => $item->iVCGrade,
|
|
|
- 'is_end' => $item->bVCEnd,
|
|
|
- 'children' => []
|
|
|
- ];
|
|
|
+ if(empty($data['details'])) return [false, '表体不能为空'];
|
|
|
+ foreach ($data['details'] as $key => $value){
|
|
|
+ $line = "第 " . ($key + 1) . "行";
|
|
|
+ if(empty($value['cInvCode'])) return [false, $line . '存货编码不能为空'];
|
|
|
+ if(empty($value['name'])) return [false, $line . '存货名称不能为空'];
|
|
|
+
|
|
|
+ $currentQty = floatval($value['quantity'] ?? 0);
|
|
|
+ if(! is_numeric($value['quantity']) || $currentQty <= 0.0) return [false, $line . '存货数量不正常'];
|
|
|
+ if(! isset($value['rate']) || ! is_numeric($value['rate'])) return [false, $line . '税率为空或非数字'];
|
|
|
+
|
|
|
+ if(empty($value['dArriveDate'])) return [false, $line . '建议订货日期不能为空'];
|
|
|
+
|
|
|
+ $dArriveDate = trim($value['dArriveDate']);
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dArriveDate) || strtotime($dArriveDate) === false) {
|
|
|
+ return [false, $line . "计划到货日期格式不正确,必须为 YYYY-MM-DD"];
|
|
|
+ }
|
|
|
+
|
|
|
+ $timeOrder = strtotime($orderTime);
|
|
|
+ $timeArrive = strtotime($dArriveDate);
|
|
|
+ if ($timeArrive < $timeOrder) {
|
|
|
+ return [false, $line . "计划到货日期({$dArriveDate})不能早于单据日期({$orderTime})"];
|
|
|
}
|
|
|
|
|
|
- // 3. 构建引用树
|
|
|
- $tree = [];
|
|
|
- foreach ($classList as $code => &$node) {
|
|
|
- // 获取父级编码
|
|
|
- $parentCode = $this->getParentCode($code);
|
|
|
+ // ==================== 逐行精确匹配校验数量(混合单据核心修改) ====================
|
|
|
+ // 【关键改动】:不仅表头是参照,且当前行有具体的请购单明细ID(iAppIds > 0),才进行用友和本地库存校验
|
|
|
+ $iAppIds = isset($value['iAppIds']) ? (int)$value['iAppIds'] : 0;
|
|
|
|
|
|
- if ($parentCode === null || !isset($classList[$parentCode])) {
|
|
|
- // 顶级节点
|
|
|
- $tree[] = &$node;
|
|
|
- } else {
|
|
|
- // 挂载到父节点
|
|
|
- $classList[$parentCode]['children'][] = &$node;
|
|
|
+ if ($isRefApp && $iAppIds > 0) {
|
|
|
+
|
|
|
+ if (!isset($u8DetailsMap[$iAppIds])) {
|
|
|
+ return [false, $line . "对应的请购单明细在用友系统已无剩余可用量或该请购单号输入有误,请刷新数据!"];
|
|
|
+ }
|
|
|
+
|
|
|
+ $u8RefCode = $u8DetailsMap[$iAppIds]['cappcode'];
|
|
|
+ $u8AvailableQty = (float)$u8DetailsMap[$iAppIds]['available_qty'];
|
|
|
+ $localOccupiedQty = $localOccupiedMap[$iAppIds] ?? 0.0;
|
|
|
+
|
|
|
+ $realAvailableQty = $u8AvailableQty - $localOccupiedQty;
|
|
|
+
|
|
|
+ if ($currentQty > $realAvailableQty) {
|
|
|
+ if ($localOccupiedQty > 0) {
|
|
|
+ return [false, $line . "【请购单:{$u8RefCode}】可下单数量不足!用友剩余: {$u8AvailableQty},本地在途预占: {$localOccupiedQty},实际可用仅剩: {$realAvailableQty},当前输入: {$currentQty}"];
|
|
|
+ } else {
|
|
|
+ return [false, $line . "【请购单:{$u8RefCode}】可下单数量不足!用友系统最大可用量为: {$u8AvailableQty},当前输入: {$currentQty}"];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ // ====================================================================
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! $is_add){
|
|
|
+ $order = PoPomain::where('id', $data['id'])->where('del_time',0)->first();
|
|
|
+ if(empty($order)) return [false, '采购单信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_one, $user, $order['order_number']);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderCommon($data, $user, $field = []){
|
|
|
+ $type = U8State::type_one;
|
|
|
+ $qx = $user['qx'];
|
|
|
+
|
|
|
+ $model = PoPomain::from('po_pomain as v')
|
|
|
+ ->leftJoin('u8_state as s', function ($join) use ($type, $user) {
|
|
|
+ $join->on('v.order_number', '=', 's.order_number')
|
|
|
+ ->where('s.type', '=', $type)
|
|
|
+ ->where('s.login_type', '=', $user['login_type'])
|
|
|
+ ->where('s.del_time', '=', 0);
|
|
|
+ })
|
|
|
+ ->where('v.del_time', 0)
|
|
|
+ ->when(empty($qx), function ($query) use($user){
|
|
|
+ return $query->where('v.crt_id', $user['userid']);
|
|
|
+ })
|
|
|
+ ->select('v.*', DB::raw('IFNULL(s.state, -1) as state'),'s.result')
|
|
|
+ ->orderby('v.id', 'desc');
|
|
|
+
|
|
|
+ if (isset($data['state']) && $data['state'] !== '') {
|
|
|
+ $state_filter = $data['state'];
|
|
|
+ if ($state_filter == -1) {
|
|
|
+ // 未发起审批:u8_states 表里没记录,s.id 就会是 NULL
|
|
|
+ $model->whereNull('s.id');
|
|
|
+ } else {
|
|
|
+ // 0:待审核 1:通过 2:驳回
|
|
|
+ $model->where('s.state', $state_filter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['code'])) $model->where('v.code', 'like', '%' . $data['code'] . '%');
|
|
|
+ if(! empty($data['id'])) $model->where('v.id', $data['id']);
|
|
|
+ if(! empty($data['order_number'])) $model->where('v.order_number', 'like', '%' . $data['order_number'] . '%');
|
|
|
+ if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+ $model->where('v.crt_time', '>=', $return[0]);
|
|
|
+ $model->where('v.crt_time', '<=', $return[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $model;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderList($data, $user){
|
|
|
+ $model = $this->purchaseOrderCommon($data, $user);
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillPurchaseOrderData($list,$user);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillPurchaseOrderData($data, $user){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $e_map = DDEmployee::whereIn('userid', array_unique(array_column($data['data'], 'crt_id')))
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->pluck('name', 'userid')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['crt_name'] = $e_map[$value['crt_id']] ?? "";
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['state_title'] = U8State::state_name[$value['state']];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderMyDetail($data, $user){
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+ $order_number = $data['order_number'] ?? "";
|
|
|
+ if(empty($id) && empty($order_number)) return [false,'请选择数据'];
|
|
|
+
|
|
|
+ $customer = PoPomain::where('del_time',0)
|
|
|
+ ->when(! empty($id), function ($query) use($id){
|
|
|
+ return $query->where('id', $id);
|
|
|
+ })
|
|
|
+ ->when(! empty($order_number), function ($query) use($order_number){
|
|
|
+ return $query->where('id', $order_number);
|
|
|
+ })
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false,'采购订单不存在或已被删除'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ $customer['crt_name'] = DDEmployee::where('id',$customer['crt_id'])->value('name');
|
|
|
+ $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
+
|
|
|
+ $details = $this->getODetail($data['id']);
|
|
|
+ $customer = array_merge($customer, $details);
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderDel($data, $user){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ $order = PoPomain::where('id', $data['id'])->where('del_time',0)->first();
|
|
|
+ if(empty($order)) return [false, '采购订单信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_one, $user, $order['order_number']);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ PoPomain::where('del_time',0)
|
|
|
+ ->where('id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ PoPodetails::where('del_time',0)
|
|
|
+ ->where('main_id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增采购入库单到本地
|
|
|
|
|
|
- return [true, $tree];
|
|
|
+ public function purchaseOrderInAdd($data, $user)
|
|
|
+ {
|
|
|
+ list($status, $msg) = $this->purchaseOrderInRule($data, $user);
|
|
|
+ if (!$status) return [false, $msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
|
+ $model = new RdRecord01();
|
|
|
+ $model->order_number = $this->generateBillNo([
|
|
|
+ 'type' => RdRecord01::Order_type,
|
|
|
+ 'login_type' => $user['login_type'],
|
|
|
+ 'period' => date("Ym")
|
|
|
+ ]);
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType'] ?? '普通采购';
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->cVenCode = $data['cVenCode'];
|
|
|
+ $model->cVenCodeTitle = $data['cVenCodeTitle'];
|
|
|
+ // 存储采购订单号数组为 JSON
|
|
|
+ $model->cOrderCode = !empty($data['cOrderCode']) ? (array)$data['cOrderCode'] : [];
|
|
|
+ $model->warehouseCode = $data['warehouseCode'];
|
|
|
+ $model->warehouseCodeTitle = $data['warehouseCodeTitle'];
|
|
|
+ $model->cRdCode = $data['cRdCode'];
|
|
|
+ $model->cRdCodeTitle = $data['cRdCodeTitle'];
|
|
|
+ $model->bredvouch = isset($data['bredvouch']) ? (int)$data['bredvouch'] : 0; // 0蓝 1红
|
|
|
+ $model->iTaxRate = $data['iTaxRate'] ?? 13;
|
|
|
+ $model->nflat = $data['nflat'] ?? 1;
|
|
|
+ $model->cexch_name = $data['cexch_name'] ?? '人民币';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->login_type = $user['login_type'];
|
|
|
+ $model->crt_id = $user['userid'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $this->saveOIDetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
} catch (\Throwable $exception) {
|
|
|
- return [false, "获取供应商分类树失败: " . $exception->getMessage()];
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, "新增采购入库单失败: " . $exception->getMessage()];
|
|
|
}
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
}
|
|
|
|
|
|
- //U8 存货分类树结构
|
|
|
- public function inventoryClassTree($data, $user)
|
|
|
+ public function purchaseOrderInEdit($data, $user)
|
|
|
{
|
|
|
+ list($status, $msg) = $this->purchaseOrderInRule($data, $user, false);
|
|
|
+ if (!$status) return [false, $msg];
|
|
|
+
|
|
|
try {
|
|
|
- // 1. 从数据库获取所有存货分类
|
|
|
- $classes = DB::connection('sqlsrv')
|
|
|
- ->table('InventoryClass')
|
|
|
- ->select('cInvCCode', 'cInvCName', 'iInvCGrade', 'bInvCEnd')
|
|
|
- ->orderBy('cInvCCode', 'asc')
|
|
|
- ->get();
|
|
|
-
|
|
|
- if ($classes->isEmpty()) return [true, []];
|
|
|
-
|
|
|
- // 2. 将集合转换为数组并以编码作为 Key,方便查找
|
|
|
- $classList = [];
|
|
|
- foreach ($classes as $item) {
|
|
|
- $classList[$item->cInvCCode] = [
|
|
|
- 'label' => $item->cInvCName,
|
|
|
- 'code' => $item->cInvCCode,
|
|
|
- 'grade' => $item->iInvCGrade,
|
|
|
- 'is_end' => $item->bInvCEnd,
|
|
|
- 'children' => []
|
|
|
- ];
|
|
|
- }
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = RdRecord01::where('id', $data['id'])->first();
|
|
|
+ if (empty($model)) return [false, '单据不存在'];
|
|
|
+
|
|
|
+ $model->order_time = $data['order_time'];
|
|
|
+ $model->cBusType = $data['cBusType'] ?? '普通采购';
|
|
|
+ $model->cPTCode = $data['cPTCode'];
|
|
|
+ $model->cPTCodeTitle = $data['cPTCodeTitle'];
|
|
|
+ $model->cVenCode = $data['cVenCode'];
|
|
|
+ $model->cVenCodeTitle = $data['cVenCodeTitle'];
|
|
|
+ $model->cOrderCode = !empty($data['cOrderCode']) ? (array)$data['cOrderCode'] : [];
|
|
|
+ $model->warehouseCode = $data['warehouseCode'];
|
|
|
+ $model->warehouseCodeTitle = $data['warehouseCodeTitle'];
|
|
|
+ $model->cRdCode = $data['cRdCode'];
|
|
|
+ $model->cRdCodeTitle = $data['cRdCodeTitle'];
|
|
|
+ $model->iTaxRate = $data['iTaxRate'] ?? 13;
|
|
|
+ $model->nflat = $data['nflat'] ?? 1;
|
|
|
+ $model->cexch_name = $data['cexch_name'] ?? '人民币';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ // 软删除老明细
|
|
|
+ RdRecords01::where('del_time', 0)
|
|
|
+ ->where('main_id', $data['id'])
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+
|
|
|
+ $this->saveOIDetail($model->id, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, "编辑采购入库单失败: " . $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderInRule(&$data, $user, $is_add = true)
|
|
|
+ {
|
|
|
+ if (!$is_add && empty($data['id'])) return [false, 'ID不能为空'];
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+
|
|
|
+ if (empty($data['order_time'])) return [false, '单据日期不能为空'];
|
|
|
+ $orderTime = trim($data['order_time']);
|
|
|
+ if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $orderTime) || strtotime($orderTime) === false) {
|
|
|
+ return [false, '单据日期格式不正确,必须为 YYYY-MM-DD 格式'];
|
|
|
+ }
|
|
|
+ if (empty($data['cVenCode']) || empty($data['cVenCodeTitle'])) return [false, '供应商信息不能为空'];
|
|
|
+ if (empty($data['warehouseCode']) || empty($data['warehouseCodeTitle'])) return [false, '仓库信息不能为空'];
|
|
|
+ if (empty($data['cRdCode']) || empty($data['cRdCodeTitle'])) return [false, '收发类别不能为空'];
|
|
|
+
|
|
|
+ if(! isset($data['bredvouch'])) return [false, '蓝单|红单标识不存在'];
|
|
|
+ $bredvouch = $data['bredvouch']; // 0蓝单 1红单
|
|
|
+
|
|
|
+ // ==================== 核心逻辑:精准追溯用友采购订单及本地预占 ====================
|
|
|
+ if(empty($data['cOrderCode']) || !is_array($data['cOrderCode'])) return [false, '采购订单不能为空或类型错误'];
|
|
|
+
|
|
|
+ $u8PoDetailsMap = [];
|
|
|
+
|
|
|
+ $service = new U8ServerService($user);
|
|
|
+ $error = $service->getError();
|
|
|
+ if (!empty($error)) return [false, $error];
|
|
|
+
|
|
|
+ $cOrderCodes = array_filter(array_map('trim', $data['cOrderCode']));
|
|
|
+ if (empty($cOrderCodes)) return [false, '参照生成的采购订单号不能为空'];
|
|
|
+
|
|
|
+ // 1. 调用用友接口,获取最新采购订单的明细流(包含未入库量、已入库量、原订单量)
|
|
|
+ list($status, $u8PoDetails) = $service->getPurchaseOrderDetailsByCode($cOrderCodes);
|
|
|
+ if (!$status) return [false, $u8PoDetails];
|
|
|
+
|
|
|
+ foreach ($u8PoDetails as $u8Item) {
|
|
|
+ $u8PoDetailsMap[$u8Item['detail_id']] = $u8Item;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 统计本地已预占/已入库(code为空,未完全同步到用友)的入库数量
|
|
|
+ $localOccupiedQuery = DB::table('rd_record01 as m')
|
|
|
+ ->join('rd_records01 as d', 'd.main_id', '=', 'm.id')
|
|
|
+ ->where('m.del_time', 0)
|
|
|
+ ->where('d.del_time', 0)
|
|
|
+ ->where('m.code', ''); // 排除用友已落单的数据,防止重复扣减
|
|
|
|
|
|
- // 3. 构建树形结构
|
|
|
- $tree = [];
|
|
|
- foreach ($classList as $code => &$node) {
|
|
|
- // 获取当前分类的级次 (U8 逻辑通常根据编码长度判断父级)
|
|
|
- // 比如 0101 的父级是 01
|
|
|
- $parentCode = $this->getParentCode($code);
|
|
|
+ if (!$is_add) {
|
|
|
+ $localOccupiedQuery->where('m.id', '!=', $id);
|
|
|
+ }
|
|
|
|
|
|
- if ($parentCode === null || !isset($classList[$parentCode])) {
|
|
|
- // 如果没有父级编码,或者父级编码不在列表里,说明是顶级分类
|
|
|
- $tree[] = &$node;
|
|
|
+ // 使用 JSON_CONTAINS 圈定本次涉及的订单号在本地的预占
|
|
|
+ $localOccupiedQuery->where(function ($query) use ($cOrderCodes) {
|
|
|
+ foreach ($cOrderCodes as $index => $code) {
|
|
|
+ if ($index === 0) {
|
|
|
+ $query->whereRaw('JSON_CONTAINS(m.cOrderCode, ?)', ['"' . $code . '"']);
|
|
|
} else {
|
|
|
- // 将当前节点引用到父节点的 children 数组中
|
|
|
- $classList[$parentCode]['children'][] = &$node;
|
|
|
+ $query->orWhereRaw('JSON_CONTAINS(m.cOrderCode, ?)', ['"' . $code . '"']);
|
|
|
}
|
|
|
}
|
|
|
+ });
|
|
|
|
|
|
- return [true, $tree];
|
|
|
+ $localOccupiedRows = $localOccupiedQuery->select(
|
|
|
+ 'd.iPOsID',
|
|
|
+ DB::raw('SUM(d.quantity) as total_qty') // 蓝单为正,红单为负,直接聚合
|
|
|
+ )
|
|
|
+ ->groupBy('d.iPOsID')
|
|
|
+ ->get();
|
|
|
|
|
|
- } catch (\Throwable $exception) {
|
|
|
- return [false, "获取分类树失败: " . $exception->getMessage()];
|
|
|
+ $localOccupiedMap = [];
|
|
|
+ foreach ($localOccupiedRows as $row) {
|
|
|
+ $localOccupiedMap[$row->iPOsID] = (float)$row->total_qty;
|
|
|
+ }
|
|
|
+ // ====================================================================
|
|
|
+
|
|
|
+ if (empty($data['details'])) return [false, '表体明细不能为空'];
|
|
|
+ foreach ($data['details'] as $key => $value) {
|
|
|
+ $line = "第 " . ($key + 1) . "行";
|
|
|
+ if (empty($value['cInvCode'])) return [false, $line . '存货编码不能为空'];
|
|
|
+ if (empty($value['name'])) return [false, $line . '存货名称不能为空'];
|
|
|
+
|
|
|
+ $currentQty = floatval($value['quantity'] ?? 0);
|
|
|
+ if ($bredvouch === 0 && $currentQty <= 0) {
|
|
|
+ return [false, $line . '正数入库单(蓝单)存货数量必须大于0'];
|
|
|
+ }
|
|
|
+ if ($bredvouch === 1 && $currentQty >= 0) {
|
|
|
+ return [false, $line . '负数入库单(红单/退货)存货数量必须小于0'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($value['iPOsID'])) return [false, '采购订单子表ID不能为空'];
|
|
|
+
|
|
|
+ // ==================== 混合单据逐行精细校验数量 ====================
|
|
|
+ $iPOsID = $value['iPOsID'];
|
|
|
+
|
|
|
+ if (!isset($u8PoDetailsMap[$iPOsID])) {
|
|
|
+ return [false, $line . "对应的采购订单明细在用友系统不可用或已被关闭,请核对!"];
|
|
|
+ }
|
|
|
+
|
|
|
+ $poCode = $u8PoDetailsMap[$iPOsID]['order_number']; // 来源采购订单号
|
|
|
+ $poQty = (float)$u8PoDetailsMap[$iPOsID]['po_qty']; // 订单总执行量
|
|
|
+ $u8ReceivedQty = (float)$u8PoDetailsMap[$iPOsID]['received_qty']; // 用友端已入库累计
|
|
|
+ $localOccupiedQty = $localOccupiedMap[$iPOsID] ?? 0; // 本地中转在途入库累计
|
|
|
+
|
|
|
+ if ($bredvouch === 0) {
|
|
|
+ // 【蓝单场景】:控制不能超过未入库余量
|
|
|
+ // 可入库余量 = 订单总数 - 用友已入库 - 本地在途入库
|
|
|
+ $availableQty = $poQty - $u8ReceivedQty - $localOccupiedQty;
|
|
|
+ if ($currentQty > $availableQty) {
|
|
|
+ return [false, $line . "【订单:{$poCode}】超过最大可入库量!用友订单待入库: " . ($poQty - $u8ReceivedQty) . ",本地在途预占: {$localOccupiedQty},实际可用余量: {$availableQty},当前输入: {$currentQty}"];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 【红单退货场景】:控制退货绝对值不能大于总共已经成功入库的数量
|
|
|
+ // 总入库资产量 = 用友已确认入库 + 本地在途蓝单数量(若有本地在途红单则会冲减)
|
|
|
+ $totalInStockQty = $u8ReceivedQty + $localOccupiedQty;
|
|
|
+ if (abs($currentQty) > $totalInStockQty) {
|
|
|
+ return [false, $line . "【订单:{$poCode}】退货数量超标!该产品系统累计总入库资产仅为: {$totalInStockQty},本次尝试退货: " . abs($currentQty)];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$is_add) {
|
|
|
+ $order = RdRecord01::where('id', $data['id'])->where('del_time', 0)->first();
|
|
|
+ if (empty($order)) return [false, '单据信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ if($order['bredvouch'] != $bredvouch) return [false, '编辑不允许修改红蓝单标识'];
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_three, $user, $order['order_number']);
|
|
|
+ if (!$status) return [false, $msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function saveOIDetail($id, $data)
|
|
|
+ {
|
|
|
+ if (!empty($data['details'])) {
|
|
|
+ $unit = [];
|
|
|
+ foreach ($data['details'] as $value) {
|
|
|
+ $unit[] = [
|
|
|
+ 'main_id' => $id,
|
|
|
+ 'iPOsID' => $value['iPOsID'] ?? 0, // 核心外键:对应用友采购订单明细AutoID
|
|
|
+ 'cInvCode' => $value['cInvCode'],
|
|
|
+ 'quantity' => $value['quantity'],
|
|
|
+ 'price' => $value['price'] ?? 0,
|
|
|
+ 'rate' => $value['rate'] ?? 0,
|
|
|
+ 'name' => $value['name'],
|
|
|
+ 'size' => $value['size'] ?? '',
|
|
|
+ 'unit' => $value['unit'] ?? '',
|
|
|
+ 'cBatch' => $value['cBatch'] ?? '',
|
|
|
+ 'dMadeDate' => $value['dMadeDate'] ?? '',
|
|
|
+ 'dVDate' => $value['dVDate'] ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ if (!empty($unit)) RdRecords01::insert($unit);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 辅助函数:根据 U8 编码规则获取父级编码
|
|
|
- * U8 的级次通常存储在 GradeDef 表,但通用逻辑是截取末尾
|
|
|
- */
|
|
|
- private function getParentCode($code)
|
|
|
+ private function getOIDetail($id)
|
|
|
{
|
|
|
- $len = strlen($code);
|
|
|
- if ($len <= 2) return null; // 假设第一级是2位,小于等于2位则无父级
|
|
|
+ $data = RdRecords01::where('del_time', 0)
|
|
|
+ ->where('main_id', $id)
|
|
|
+ ->get()->toArray();
|
|
|
|
|
|
- // 这里假设级次是 2-2-2-2 (最常见配置)
|
|
|
- // 实际生产中,如果级次不固定,建议查询 GradeDef 表
|
|
|
- return substr($code, 0, $len - 2);
|
|
|
+ return ['details' => $data];
|
|
|
}
|
|
|
|
|
|
- //U8 计量单位组(带默认主计量单位)
|
|
|
- public function getUnitGroups($data, $user)
|
|
|
+ public function purchaseOrderInCommon($data, $user, $field = [])
|
|
|
{
|
|
|
- $list = DB::connection('sqlsrv')
|
|
|
- ->select("
|
|
|
- SELECT
|
|
|
- G.cGroupCode,
|
|
|
- G.cGroupName,
|
|
|
- G.iGroupType,
|
|
|
- U.cComUnitCode,
|
|
|
- U.cComUnitName,
|
|
|
- U.iNumber
|
|
|
- FROM ComputationGroup AS G
|
|
|
- OUTER APPLY (
|
|
|
- SELECT TOP 1 cComUnitCode, cComUnitName, iNumber
|
|
|
- FROM ComputationUnit
|
|
|
- WHERE cGroupCode = G.cGroupCode
|
|
|
- ORDER BY
|
|
|
- bMainUnit DESC, -- 1. 优先主计量
|
|
|
- iNumber ASC, -- 2. 序号最小 (若 NULL 会排在最前)
|
|
|
- cComUnitCode ASC -- 3. 编码最小
|
|
|
- ) AS U
|
|
|
- ");
|
|
|
+ $type = U8State::type_three;
|
|
|
+ $qx = $user['qx'];
|
|
|
|
|
|
- return [true, $list];
|
|
|
+ $model = RdRecord01::from('rd_record01 as v')
|
|
|
+ ->leftJoin('u8_state as s', function ($join) use ($type, $user) {
|
|
|
+ $join->on('v.order_number', '=', 's.order_number')
|
|
|
+ ->where('s.type', '=', $type)
|
|
|
+ ->where('s.login_type', '=', $user['login_type'])
|
|
|
+ ->where('s.del_time', '=', 0);
|
|
|
+ })
|
|
|
+ ->where('v.del_time', 0)
|
|
|
+ ->when(empty($qx), function ($query) use ($user) {
|
|
|
+ return $query->where('v.crt_id', $user['userid']);
|
|
|
+ })
|
|
|
+ ->select('v.*', DB::raw('IFNULL(s.state, -1) as state'), 's.result')
|
|
|
+ ->orderby('v.id', 'desc');
|
|
|
+
|
|
|
+ if (isset($data['state']) && $data['state'] !== '') {
|
|
|
+ $state_filter = $data['state'];
|
|
|
+ if ($state_filter == -1) {
|
|
|
+ $model->whereNull('s.id');
|
|
|
+ } else {
|
|
|
+ $model->where('s.state', $state_filter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['code'])) $model->where('v.code', 'like', '%' . $data['code'] . '%');
|
|
|
+ if (!empty($data['id'])) $model->where('v.id', $data['id']);
|
|
|
+ if (!empty($data['order_number'])) $model->where('v.order_number', 'like', '%' . $data['order_number'] . '%');
|
|
|
+ if (!empty($data['crt_time'][0]) && !empty($data['crt_time'][1])) {
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+ $model->where('v.crt_time', '>=', $return[0]);
|
|
|
+ $model->where('v.crt_time', '<=', $return[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $model;
|
|
|
}
|
|
|
|
|
|
- //U8 计量单位档案
|
|
|
- public function getComputationUnitList($data, $user)
|
|
|
+ public function purchaseOrderInList($data, $user)
|
|
|
{
|
|
|
- $list = DB::connection('sqlsrv')
|
|
|
- ->table('ComputationUnit as U')
|
|
|
- ->select(
|
|
|
- 'U.cComUnitCode', // 单位编码
|
|
|
- 'U.cComUnitName', // 单位名称
|
|
|
- 'U.cGroupCode', // 所属组编码
|
|
|
- 'U.bMainUnit', // 是否主单位
|
|
|
- 'U.iNumber' // 排序序号
|
|
|
- )
|
|
|
- ->orderBy('U.cGroupCode', 'ASC')
|
|
|
- ->orderBy('U.iNumber', 'ASC') // 按照你要求的 iNumber 排序
|
|
|
- ->get();
|
|
|
+ $model = $this->purchaseOrderInCommon($data, $user);
|
|
|
+ $list = $this->limit($model, '', $data);
|
|
|
+ $list = $this->fillPurchaseOrderInData($list, $user);
|
|
|
|
|
|
return [true, $list];
|
|
|
}
|
|
|
+
|
|
|
+ public function fillPurchaseOrderInData($data, $user)
|
|
|
+ {
|
|
|
+ if (empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $e_map = DDEmployee::whereIn('userid', array_unique(array_column($data['data'], 'crt_id')))
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->pluck('name', 'userid')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($data['data'] as $key => $value) {
|
|
|
+ $data['data'][$key]['crt_name'] = $e_map[$value['crt_id']] ?? "";
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s', $value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['state_title'] = U8State::state_name[$value['state']] ?? '';
|
|
|
+ $data['data'][$key]['bredvouch_title'] = RdRecord01::$bredvouch_title[$value['bredvouch']] ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderInMyDetail($data, $user)
|
|
|
+ {
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+ $order_number = $data['order_number'] ?? "";
|
|
|
+ if (empty($id) && empty($order_number)) return [false, '请选择数据'];
|
|
|
+
|
|
|
+ $customer = RdRecord01::where('del_time', 0)
|
|
|
+ ->when(!empty($id), function ($query) use ($id) {
|
|
|
+ return $query->where('id', $id);
|
|
|
+ })
|
|
|
+ ->when(!empty($order_number), function ($query) use ($order_number) {
|
|
|
+ return $query->where('order_number', $order_number);
|
|
|
+ })
|
|
|
+ ->where('login_type', $user['login_type'])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (empty($customer)) return [false, '采购入库单不存在或已被删除'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ $customer['crt_name'] = DDEmployee::where('userid', $customer['crt_id'])->value('name') ?: '';
|
|
|
+ $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s", $customer['crt_time']) : '';
|
|
|
+
|
|
|
+ $details = $this->getOIDetail($customer['id']);
|
|
|
+ $customer = array_merge($customer, $details);
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function purchaseOrderInDel($data, $user)
|
|
|
+ {
|
|
|
+ if (empty($data['id'])) return [false, '请选择数据!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ $order = RdRecord01::where('id', $data['id'])->where('del_time', 0)->first();
|
|
|
+ if (empty($order)) return [false, '单据信息不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+
|
|
|
+ list($status, $msg) = $this->checkState(U8State::type_three, $user, $order['order_number']);
|
|
|
+ if (!$status) return [false, $msg];
|
|
|
+
|
|
|
+ RdRecord01::where('del_time', 0)->where('id', $data['id'])->update(['del_time' => $time]);
|
|
|
+ RdRecords01::where('del_time', 0)->where('main_id', $data['id'])->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
}
|