替卡方式

This commit is contained in:
邓皓元 2019-04-18 09:21:39 +08:00
parent a35fd9569a
commit 0df7e107ec

View File

@ -255,9 +255,8 @@ class OrderService extends Service
}
}
/**
* 转销售方式
if (count($news)) {
// 用转销售方式创建
if ($attributes['use_type'] === 1 && count($news)) {
// 创建订单
$newOrderData = array_except($attributes, ['selected', 'sign']);
$newOrderData['id'] = ++$maxId;
@ -316,18 +315,31 @@ class OrderService extends Service
$attributes['counts'] = array_sum(array_pluck($selectedNews, 'counts'));
$attributes['selected'] = array_values($selectedNews);
$news = [];
}
*/
/** 用原有卡替换方式 */
if (count($news)) {
$replacement = DB::table(DB::raw("({$subQuery->toSql()}) as sub"))
// 替卡方式
if (count($news) || count($extras)) {
$attributes['package_id'] = $attributes['package_id'] ?? $product->package_id;
$having = "MAX(service_end_at) < '%s'";
$having = sprintf($having, Carbon::now()->subMonths(6)->endOfMonth());
$subQuery = OrderCardPartition::select(['sim', DB::raw('MAX(service_end_at) as service_end_at')])
->whereIn('type', [0, 1])
->where('company_id', $attributes['company_id'])
->where('package_id', $attributes['package_id'])
->where('original_sim', 0)
->groupBy('sim');
$replacementQuery = DB::table(DB::raw("({$subQuery->toSql()}) as sub"))
->mergeBindings($subQuery->getQuery())
->select(['sim'])
->orderBy('service_end_at')
->where('service_end_at', '<', Carbon::now()->subMonths(6)->endOfMonth())
->limit(count($news))
->get()->pluck('sim')->toArray();
->where('service_end_at', '<', Carbon::now()->subMonths(6)->endOfMonth());
if (count($news)) {
$replacement = $replacementQuery->limit(count($news))->get()->pluck('sim')->toArray();
if (count($replacement) !== count($news)) {
throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作');
@ -354,25 +366,7 @@ class OrderService extends Service
}
if (count($extras)) {
$attributes['package_id'] = $attributes['package_id'] ?? $product->package_id;
$having = "MAX(service_end_at) < '%s'";
$having = sprintf($having, Carbon::now()->subMonths(6)->endOfMonth());
$subQuery = OrderCardPartition::select(['sim', DB::raw('MAX(service_end_at) as service_end_at')])
->whereIn('type', [0, 1])
->where('company_id', $attributes['company_id'])
->where('package_id', $attributes['package_id'])
->where('original_sim', 0)
->groupBy('sim');
$replacement = DB::table(DB::raw("({$subQuery->toSql()}) as sub"))
->mergeBindings($subQuery->getQuery())
->select(['sim'])
->orderBy('service_end_at')
->where('service_end_at', '<', Carbon::now()->subMonths(6)->endOfMonth())
->limit(count($extras))
->get()->pluck('sim')->toArray();
$replacement = $replacementQuery->limit(count($extras))->get()->pluck('sim')->toArray();
if (count($replacement) !== count($extras)) {
throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作');
@ -401,6 +395,7 @@ class OrderService extends Service
DB::statement($sql);
}
}
}
if ($attributes['counts'] !== 0) {
$attributes['id'] = ++$maxId;