采购单处理
This commit is contained in:
@@ -23,16 +23,63 @@ class PurchaseOrdersController extends BaseController
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
// protected function indexFormat($data)
|
||||
// {
|
||||
// if (isset($data['created_at']))
|
||||
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
// if (isset($data['updated_at']))
|
||||
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
// return $data;
|
||||
// }
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
if (isset($data['records']) && count($data['records']) > 0) {
|
||||
foreach ($data['records'] as &$record) {
|
||||
$result = [
|
||||
'id' => $record['id'],
|
||||
'po_no' => $record['po_no'],
|
||||
'pi_no' => $record['pi_no'],
|
||||
'buyer_id' => $record['buyer_id'],
|
||||
'buyer_name' => $record['buyer_name'],
|
||||
'contract_file' => $record['contract_file'],
|
||||
'contract_file_url' => $record['contract_file'] ? (new \app\common\service\OssService)->generatePrivateUrl($data['contract_file']) : '',
|
||||
'status' => $record['status'],
|
||||
'remark' => $record['remark'],
|
||||
'created_at' => date('Y-m-d H:i:s', strtotime($record['created_at'])),
|
||||
'updated_at' => date('Y-m-d H:i:s', strtotime($record['updated_at'])),
|
||||
];
|
||||
$record = $result;
|
||||
}
|
||||
unset($record);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
protected function showFormat($data)
|
||||
{
|
||||
$result = [
|
||||
'id' => $data['id'],
|
||||
'po_no' => $data['po_no'],
|
||||
'pi_no' => $data['pi_no'],
|
||||
'buyer_id' => $data['buyer_id'],
|
||||
'buyer_name' => $data['buyer_name'],
|
||||
'contract_file' => $data['contract_file'],
|
||||
'contract_file_url' => $data['contract_file'] ? (new \app\common\service\OssService)->generatePrivateUrl($data['contract_file']) : '',
|
||||
'status' => $data['status'],
|
||||
'remark' => $data['remark']
|
||||
];
|
||||
|
||||
if (isset($data['items']) && count($data['items']) > 0) {
|
||||
foreach ($data['items'] as $record) {
|
||||
$result['stock_order'][] = [
|
||||
'id' => $record['stockOrder']['id'],
|
||||
'order_no' => $record['stockOrder']['order_no'],
|
||||
'salesperson_snapshot' => $record['stockOrder']['salesperson_snapshot'],
|
||||
'biz_date' => date('Y-m-d', $record['stockOrder']['biz_date']),
|
||||
'stock_order_items' => [
|
||||
'id' => $record['stockOrderItems']['id'],
|
||||
'model_snapshot' => $record['stockOrderItems']['model_snapshot'],
|
||||
'unique_id' => $record['stockOrderItems']['unique_id'],
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 前置查询处理
|
||||
*
|
||||
|
||||
@@ -3,9 +3,14 @@
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class PurchaseOrderItems extends Model
|
||||
{
|
||||
|
||||
use SoftDeletes;
|
||||
|
||||
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
@@ -46,4 +51,21 @@ class PurchaseOrderItems extends Model
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
|
||||
public function purchaseOrder()
|
||||
{
|
||||
return $this->belongsTo(PurchaseOrders::class, 'purchase_order_id', 'id');
|
||||
}
|
||||
|
||||
|
||||
public function stockOrder()
|
||||
{
|
||||
return $this->belongsTo(StockOrders::class, 'stock_id', 'id');
|
||||
}
|
||||
|
||||
public function stockOrderItems()
|
||||
{
|
||||
return $this->belongsTo(StockOrderItems::class, 'stock_items_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,13 @@
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class PurchaseOrders extends Model
|
||||
{
|
||||
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
@@ -40,10 +44,21 @@ class PurchaseOrders extends Model
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'po_no', 'pi_no', 'buyer_id', 'contract_file', 'status', 'remark', 'created_at', 'updated_at'];
|
||||
protected $fillable = ['id', 'po_no', 'pi_no', 'buyer_id', 'buyer_name', 'contract_file', 'status', 'remark', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
|
||||
public function items()
|
||||
{
|
||||
return $this->hasMany(PurchaseOrderItems::class, 'purchase_order_id', 'id');
|
||||
}
|
||||
|
||||
public function buyer()
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'buyer_id');
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,11 @@ namespace app\common\service;
|
||||
use app\common\validate\PurchaseOrdersValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\PurchaseOrders;
|
||||
use support\Request;
|
||||
use app\common\logic\PurchaseOrdersLogic;
|
||||
use app\common\model\PurchaseOrderItems;
|
||||
use app\common\model\User;
|
||||
use support\Db;
|
||||
use support\Request;
|
||||
|
||||
class PurchaseOrdersService
|
||||
{
|
||||
@@ -52,33 +54,165 @@ class PurchaseOrdersService
|
||||
'order_items' => $this->getLogic()->getOrderItemsListByOrderNo($data['order_id']),
|
||||
default => $data,
|
||||
};
|
||||
return $result;
|
||||
return $this->postProcess('PreData', $result->toArray());
|
||||
}
|
||||
|
||||
public function afterServicePreData(array $data)
|
||||
{
|
||||
foreach ($data as $key => &$item) {
|
||||
if (isset($item['biz_date'])) {
|
||||
$item['biz_date'] = date('Y-m-d', $item['biz_date']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function beforeServiceStore(array $data, Request $request)
|
||||
{
|
||||
$data['po_no'] = generateStockNo('CG');
|
||||
$data['buyer_id'] = $request->user->uid;
|
||||
$data['buyer_name'] = User::where('id', $request->user->uid)->value('name');
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function serviceStore(Request $request, array $data = [], string $scene = 'store')
|
||||
{
|
||||
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
||||
$dl = $this->getDataLimitFromRequest($request);
|
||||
// 创建采购单
|
||||
$created = $this->logicCreate($data, $dl);
|
||||
foreach ($data['stock_order'] as $order) {
|
||||
foreach ($order['stock_items'] as $item) {
|
||||
// 创建采购单明细
|
||||
PurchaseOrderItems::create([
|
||||
'purchase_order_id' => $created->id,
|
||||
'stock_id' => $order['stock_orders_id'],
|
||||
'stock_items_id' => $item['stock_order_items_id'],
|
||||
'stock_items_uqid' => $item['unique_id'],
|
||||
]);
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request, $data, $scene) {
|
||||
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
||||
$dl = $this->getDataLimitFromRequest($request);
|
||||
// 创建采购单
|
||||
$created = $this->logicCreate($data, $dl);
|
||||
if (isset($data['stock_order'])) {
|
||||
foreach ($data['stock_order'] as $order) {
|
||||
foreach ($order['stock_items'] as $item) {
|
||||
// 创建采购单明细
|
||||
PurchaseOrderItems::create([
|
||||
'purchase_order_id' => $created->id,
|
||||
'stock_id' => $order['stock_orders_id'],
|
||||
'stock_items_id' => $item['stock_order_items_id'],
|
||||
'stock_items_uqid' => $item['unique_id'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->postProcess(ucfirst($scene), $created);
|
||||
return $this->postProcess(ucfirst($scene), $created);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新采购单
|
||||
*
|
||||
* @param Request $request
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function serviceUpdate(Request $request, array $data = [], string $scene = 'update')
|
||||
{
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request, $data, $scene) {
|
||||
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
||||
|
||||
// 构建查询条件
|
||||
$config = $this->buildQueryConfig($request, $scene);
|
||||
|
||||
// 更新主表信息
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
|
||||
// 获取更新后的数据
|
||||
$order = $this->logicShow($config);
|
||||
|
||||
// 更新采购单明细
|
||||
if (isset($data['stock_order'])) {
|
||||
// 先删除原有的明细
|
||||
PurchaseOrderItems::where('purchase_order_id', $order->id)->delete();
|
||||
|
||||
// 重新创建明细
|
||||
foreach ($data['stock_order'] as $orderData) {
|
||||
foreach ($orderData['stock_items'] as $item) {
|
||||
PurchaseOrderItems::create([
|
||||
'purchase_order_id' => $order->id,
|
||||
'stock_id' => $orderData['stock_orders_id'],
|
||||
'stock_items_id' => $item['stock_order_items_id'],
|
||||
'stock_items_uqid' => $item['unique_id'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->postProcess(ucfirst($scene), $order);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询配置
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
||||
{
|
||||
$p = $request->all();
|
||||
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
|
||||
switch ($scene) {
|
||||
// case 'index':
|
||||
// // $config['with']['buyer'] = ['name', 'avatar'];
|
||||
// break;
|
||||
case 'update':
|
||||
$config['where']['id'] = $p['id'] ?? 0;
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
$config['with'] = [
|
||||
'items', 'items.stockOrder', 'items.stockOrderItems'
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除采购订单
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function serviceDestroy(Request $request)
|
||||
{
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request) {
|
||||
// 构建查询条件
|
||||
$config = $this->buildQueryConfig($request, 'update');
|
||||
|
||||
// 删除采购单明细
|
||||
$order = $this->logicShow($config);
|
||||
if ($order && $order->items) {
|
||||
PurchaseOrderItems::where('purchase_order_id', $order->id)->delete();
|
||||
}
|
||||
|
||||
// 删除采购单主表
|
||||
$deleted = $this->logicDestroy($config);
|
||||
|
||||
return $this->postProcess('Destroy', $deleted);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// protected function afterServiceShow($item)
|
||||
// {
|
||||
|
||||
// $item['items'] = $item->items->map(function ($item) {
|
||||
// $item['stock_order'] = $item->stockOrderItems;
|
||||
// return $item;
|
||||
// });
|
||||
// var_dump($item->toArray());
|
||||
// return $item;
|
||||
|
||||
// }
|
||||
}
|
||||
@@ -192,7 +192,11 @@ class CrudUtil
|
||||
*/
|
||||
public static function findOneBy(Builder $q): Model
|
||||
{
|
||||
return $q->first();
|
||||
$model = $q->first();
|
||||
if (!$model) {
|
||||
throw new BusinessException("记录不存在", 404);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user