|
@@ -2,6 +2,9 @@
|
|
|
|
|
|
namespace app\admin\controller\finance;
|
|
|
|
|
|
+use app\admin\service\goods\GoodsSkuService;
|
|
|
+use app\model\DataInout;
|
|
|
+use app\model\Goods;
|
|
|
use app\model\OrderSheet;
|
|
|
use app\model\SysCategory;
|
|
|
use support\Db;
|
|
@@ -9,185 +12,229 @@ use support\Request;
|
|
|
|
|
|
class GoodsSalesController
|
|
|
{
|
|
|
-
|
|
|
public function list(Request $request)
|
|
|
{
|
|
|
$page = $request->get('page', 1);
|
|
|
$pageSize = $request->get('pageSize', 20);
|
|
|
- $days = $request->get('days', []);
|
|
|
- $classify = $request->get('classify','');
|
|
|
- $categoryId = $request->get('category_id','');
|
|
|
- $goodsName = $request->get('goods_name','');
|
|
|
- $premisesId = $request->get('premises_id','');
|
|
|
- $payType = $request->get('pay_type','');
|
|
|
+ $days = $request->get('days', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
|
|
|
+ $classify = $request->get('classify', '');
|
|
|
+ $categoryId = $request->get('category_id', '');
|
|
|
+ $goodsName = $request->get('goods_name', '');
|
|
|
+ $payType = $request->get('pay_type', '');
|
|
|
if (!empty($days)) {
|
|
|
$days[0] = strtotime($days[0]);
|
|
|
$days[1] = strtotime($days[1]);
|
|
|
- if (date('m', $days[0]) != date('m', $days[1])) {
|
|
|
- return json_fail('暂不支持跨月查询');
|
|
|
+ }
|
|
|
+ $searchGoodsIds = [];
|
|
|
+ if (!empty($classify)) {
|
|
|
+ if ($classify == 'SERVICE') {
|
|
|
+ $classify = ['SERVICE', 'CHNMED', 'CHNNCD'];
|
|
|
+ } else {
|
|
|
+ $classify = [$classify];
|
|
|
+ }
|
|
|
+ $searchGoodsIds = Goods::whereIn('goods_classify', $classify)->pluck('goods_id')->toArray();
|
|
|
+ }
|
|
|
+ if (!empty($categoryId)) {
|
|
|
+ $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
|
|
|
+ if (empty($categorySuperPath)) {
|
|
|
+ $categorySuperPath = '#' . $categoryId . '#';
|
|
|
+ }
|
|
|
+ $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
|
|
|
+ $searchCategoryIds[] = $categoryId;
|
|
|
+ // 按产品类型筛出来的ID
|
|
|
+ $oldSearchGoodsIds = $searchGoodsIds;
|
|
|
+
|
|
|
+ $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
|
|
|
+ if (!empty($oldSearchGoodsIds)) {
|
|
|
+ $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($goodsName)) {
|
|
|
+ // 按产品类型和分类筛出来的ID
|
|
|
+ $oldSearchGoodsIds = $searchGoodsIds;
|
|
|
+ $searchGoodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck("goods_id")->toArray();
|
|
|
+ if (!empty($oldSearchGoodsIds)) {
|
|
|
+ $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
|
|
|
}
|
|
|
-
|
|
|
- $month = date('Ym', $days[0]);
|
|
|
- $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
|
|
|
- } else {
|
|
|
- $month = date('Ym');
|
|
|
}
|
|
|
|
|
|
- $categorySearchIds = '';
|
|
|
- if (!empty($categoryId)){
|
|
|
- $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
|
|
|
- $categorySearchPath .= '#'. $categoryId.'#';
|
|
|
-
|
|
|
- $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
|
|
|
- $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
|
|
|
- $categorySearchIds = implode(',',$categorySearchIds);
|
|
|
+ $dataInOuts = DataInout::where('data_inout_status', 'VALID')
|
|
|
+ ->where('data_inout_classify', 'IN')
|
|
|
+ ->when(!empty($days), function ($query) use ($days) {
|
|
|
+ $query->whereBetween('data_inout_addtimes', $days);
|
|
|
+ })->when(!empty($payType), function ($query) use ($payType) {
|
|
|
+ $query->where('data_inout_pay_type', $payType);
|
|
|
+ })->select('join_data_inout_object_json')
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $goods = [];
|
|
|
+ $goodsIds = [];
|
|
|
+ $statistics = [
|
|
|
+ 'total' => 0,
|
|
|
+ 'amount' => 0,
|
|
|
+ 'goods' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'service' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'chnmed' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'chnncd' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'package' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'meals' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'dishes' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'vip' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'recharge' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'combine' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'partner' => ['total' => 0, 'amount' => 0],
|
|
|
+ 'referrer' => ['total' => 0, 'amount' => 0],
|
|
|
+ ];
|
|
|
+ foreach ($dataInOuts as $dataInOut) {
|
|
|
+ if (empty($dataInOut['join_data_inout_object_json'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
|
|
|
+ if (empty($intoObjectJson['order'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ foreach ($intoObjectJson['order'] as $order) {
|
|
|
+ if (empty($order['goods'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ foreach ($order['goods'] as $good) {
|
|
|
+ if ((!empty($classify) || !empty($categoryId) || !empty($goodsName)) && !in_array($good['goods_id'], $searchGoodsIds)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $goodKey = $good['goods_id'];
|
|
|
+ if (!empty($good['goods_sku_id'])) {
|
|
|
+ $goodKey .= '_' . $good['goods_sku_id'];
|
|
|
+ }
|
|
|
+ if (!key_exists($goodKey, $goods)) {
|
|
|
+ $goodsIds[] = $good['goods_id'];
|
|
|
+ $goods[$goodKey] = [
|
|
|
+ 'goods_id' => $good['goods_id'],
|
|
|
+ 'goods_name' => $good['goods_name'],
|
|
|
+ 'goods_sku_id' => $good['goods_sku_id'] ?? '',
|
|
|
+ 'order_sheet_num' => floatval($good['order_sheet_num']),
|
|
|
+ 'order_sheet_pay' => floatval($good['order_sheet_pay'])
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
|
|
|
+ $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $statistics['total'] = round($good['order_sheet_num'] + $statistics['total'], 2);
|
|
|
+ $statistics['amount'] = round($good['order_sheet_pay'] + $statistics['amount'], 2);
|
|
|
+
|
|
|
+ $orderClassify = strtolower($order['classify']);
|
|
|
+ $statistics[$orderClassify]['total'] = round($good['order_sheet_num'] + $statistics[$orderClassify]['total'], 2);
|
|
|
+ $statistics[$orderClassify]['amount'] = round($good['order_sheet_pay'] + $statistics[$orderClassify]['amount'], 2);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
|
|
|
+ $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
|
|
|
|
|
|
- $recordStart = ($page - 1) * 20;
|
|
|
+ // 交集
|
|
|
+ if (!empty($classify) || !empty($categoryId)) {
|
|
|
+ $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
|
|
|
|
|
|
- $where = "where 1";
|
|
|
- if (!empty($days)) {
|
|
|
- $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
|
|
|
- }
|
|
|
- if (!empty($classify)){
|
|
|
- $where .= " and di.data_income_classify = '{$classify}'";
|
|
|
- }
|
|
|
- if (!empty($categorySearchIds)){
|
|
|
- $where .= " and g.join_goods_category_id in({$categorySearchIds})";
|
|
|
- }
|
|
|
- if (!empty($goodsName)){
|
|
|
- $where .= " and g.goods_name like '%{$goodsName}%' ";
|
|
|
- }
|
|
|
- if (!empty($premisesId)){
|
|
|
- $where .= " and di.join_data_income_dept_id = {$premisesId}";
|
|
|
- }
|
|
|
- if (!empty($payType)){
|
|
|
- $where .= " and di.data_income_pay_type = '{$payType}'";
|
|
|
+ $goods = array_filter($goods, function ($item) use ($goodsIds) {
|
|
|
+ return in_array($item['goods_id'], $goodsIds);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- $totalSql = "
|
|
|
- select count(*) as total from (
|
|
|
- select
|
|
|
- data_income_classify
|
|
|
- from app_data_income_{$month} as di
|
|
|
- left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
|
|
|
- {$where}
|
|
|
- group by JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_id'), JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_sku_id'),data_income_classify
|
|
|
- ) COUNT
|
|
|
- ";
|
|
|
- $total = Db::select($totalSql);
|
|
|
-
|
|
|
- $sql = "select
|
|
|
- g.join_goods_category_id,di.data_income_classify,g.goods_id,g.goods_name,g.goods_classify, gs.goods_sku_specs_json,gs.goods_sku_id,gs.goods_sku_sales_price,MAX(di.data_income_addtimes) as addtimes,
|
|
|
- SUM(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(di.data_income_amount) as amount,COUNT(*) as count
|
|
|
- from app_data_income_{$month} as di
|
|
|
- left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = CAST(g.goods_id AS CHAR)
|
|
|
- left join app_goods_sku as gs on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_sku_id') = gs.goods_sku_id
|
|
|
- {$where}
|
|
|
- group by g.goods_id,gs.goods_sku_id,di.data_income_classify
|
|
|
- order by addtimes desc
|
|
|
- limit " . $recordStart . ',' . $pageSize;
|
|
|
- $rows = Db::select($sql);
|
|
|
- $rows = json_decode(json_encode($rows), true);
|
|
|
-
|
|
|
- $categoryIds = array_unique(array_filter(array_column($rows, 'join_goods_category_id')));
|
|
|
- $categorys = SysCategory::whereIn('category_id', $categoryIds)->pluck('category_name', 'category_id')->toArray();
|
|
|
+ // 排序
|
|
|
+ usort($goods, function ($a, $b) {
|
|
|
+ return $b['order_sheet_num'] <=> $a['order_sheet_num'];
|
|
|
+ });
|
|
|
+ // 分页
|
|
|
+
|
|
|
+ $total = count($goods);
|
|
|
+ $start = ($page - 1) * $pageSize;
|
|
|
+ $rows = array_slice($goods, $start, $pageSize);
|
|
|
foreach ($rows as &$row) {
|
|
|
- if (!empty($row['join_goods_category_id']) && isset($categorys[$row['join_goods_category_id']])) {
|
|
|
- $row['category_name'] = $categorys[$row['join_goods_category_id']];
|
|
|
- }
|
|
|
- if (!empty($row['goods_sku_specs_json'])) {
|
|
|
- $specsJson = json_decode($row['goods_sku_specs_json'], true);
|
|
|
- $skuTitle = '';
|
|
|
- foreach ($specsJson as $k => $value) {
|
|
|
- $value = is_array($value) ? $value[0] : $value;
|
|
|
- $skuTitle .= $k . ':' . $value . ';';
|
|
|
+ $rowGoods = Goods::with([
|
|
|
+ 'category',
|
|
|
+ 'skuOne' => function ($query) use ($row) {
|
|
|
+ $query->where('goods_sku_id', $row['goods_sku_id']);
|
|
|
}
|
|
|
- $row['sku'] = rtrim($skuTitle, ';');
|
|
|
+ ])->where('goods_id', $row['goods_id'])->first();
|
|
|
+ $row['goods_classify'] = !empty($rowGoods->goods_classify) ? $rowGoods->goods_classify : '';
|
|
|
+ $row['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '';
|
|
|
+ $row['category'] = !empty($rowGoods->category) ? $rowGoods->category : '';
|
|
|
+ if (!empty($rowGoods->skuOne)) {
|
|
|
+ $row['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
|
|
|
+ $row['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
|
|
|
}
|
|
|
+
|
|
|
+ $row['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
return json_success('', [
|
|
|
'page' => $page,
|
|
|
'pageSize' => $pageSize,
|
|
|
- 'total' => $total[0]->total,
|
|
|
- 'rows' => $rows
|
|
|
+ 'total' => $total,
|
|
|
+ 'rows' => $rows,
|
|
|
+ 'statistics' => $statistics
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- public function statistics(Request $request)
|
|
|
+ public function info(Request $request)
|
|
|
{
|
|
|
- $days = $request->get('days', []);
|
|
|
- $classify = $request->get('classify','');
|
|
|
- $categoryId = $request->get('category_id','');
|
|
|
- $goodsName = $request->get('goods_name','');
|
|
|
- $premisesId = $request->get('premises_id','');
|
|
|
- $payType = $request->get('pay_type','');
|
|
|
- if (!empty($days)) {
|
|
|
- $days[0] = strtotime($days[0]);
|
|
|
- $days[1] = strtotime($days[1]);
|
|
|
- if (date('m', $days[0]) != date('m', $days[1])) {
|
|
|
- return json_fail('暂不支持跨月查询');
|
|
|
+ $goodsId = $request->get('goods_id');
|
|
|
+ $skuId = $request->get('sku_id');
|
|
|
+ if (!$goodsId) {
|
|
|
+ return json_fail("参数异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ $goods = Goods::with([
|
|
|
+ 'sku' => function ($query) use ($skuId) {
|
|
|
+ $query->where('goods_sku_id', $skuId);
|
|
|
+ },
|
|
|
+ 'running' => function ($query) {
|
|
|
+ $query->select('join_running_goods_id', 'goods_running_storage', 'goods_running_sale');
|
|
|
+ },
|
|
|
+ 'detail' => function ($query) {
|
|
|
+ $query->select('join_detail_goods_id', 'goods_detail_slider_json');
|
|
|
+ },
|
|
|
+ 'user' => function ($query) {
|
|
|
+ $query->select('user_id', 'user_name');
|
|
|
+ },
|
|
|
+ 'updateUser' => function ($query) {
|
|
|
+ $query->select('user_id', 'user_name');
|
|
|
+ }
|
|
|
+ ])->where('goods_id', $goodsId)
|
|
|
+ ->first();
|
|
|
+ $goods = $goods ?? [];
|
|
|
+ if (!empty($goods['goods_cover'])) {
|
|
|
+ $goods['goods_cover'] = getenv('STORAGE_DOMAIN') . $goods['goods_cover'];
|
|
|
+ }
|
|
|
+ $sliderArr = [];
|
|
|
+ if (!empty($goods['detail']) && !empty($goods['detail']['goods_detail_slider_json'])) {
|
|
|
+ $sliderJson = json_decode($goods['detail']['goods_detail_slider_json'], true);
|
|
|
+ $sliders = explode(',', $sliderJson['slider']);
|
|
|
+ foreach ($sliders as $slider) {
|
|
|
+ $sliderArr[] = getenv('STORAGE_DOMAIN') . $slider;
|
|
|
}
|
|
|
-
|
|
|
- $month = date('Ym', $days[0]);
|
|
|
- $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
|
|
|
- } else {
|
|
|
- $month = date('Ym');
|
|
|
- }
|
|
|
-
|
|
|
- $categorySearchIds = '';
|
|
|
- if (!empty($categoryId)){
|
|
|
- $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
|
|
|
- $categorySearchPath .= '#'. $categoryId.'#';
|
|
|
-
|
|
|
- $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
|
|
|
- $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
|
|
|
- $categorySearchIds = implode(',',$categorySearchIds);
|
|
|
}
|
|
|
+ $goods['slider'] = $sliderArr;
|
|
|
|
|
|
- $where = "where 1";
|
|
|
- if (!empty($days)) {
|
|
|
- $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
|
|
|
- }
|
|
|
- if (!empty($classify)){
|
|
|
- $where .= " and di.data_income_classify = '{$classify}'";
|
|
|
- }
|
|
|
- if (!empty($categorySearchIds)){
|
|
|
- $where .= " and g.join_goods_category_id in({$categorySearchIds})";
|
|
|
+ if (!empty($goods['user'])) {
|
|
|
+ $goods['creator_username'] = $goods['user']['user_name'];
|
|
|
}
|
|
|
- if (!empty($goodsName)){
|
|
|
- $where .= " and g.goods_name like '%{$goodsName}%' ";
|
|
|
+ if (!empty($goods['updateUser'])) {
|
|
|
+ $goods['updator_username'] = $goods['updateUser']['user_name'];
|
|
|
}
|
|
|
- if (!empty($premisesId)){
|
|
|
- $where .= " and di.join_data_income_dept_id = {$premisesId}";
|
|
|
- }
|
|
|
- if (!empty($payType)){
|
|
|
- $where .= " and di.data_income_pay_type = '{$payType}'";
|
|
|
- }
|
|
|
-
|
|
|
- $statisticsSql = "
|
|
|
- select
|
|
|
- SUM(JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(data_income_amount) as amount,COUNT(*) as count
|
|
|
- from app_data_income_{$month} as di
|
|
|
- left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
|
|
|
- {$where}
|
|
|
- ";
|
|
|
- $statistics = [];
|
|
|
- $category = ['all', 'entity', 'service', 'package'];
|
|
|
- foreach ($category as $item) {
|
|
|
- $execSql = $statisticsSql;
|
|
|
- if ($item == 'entity') {
|
|
|
- $execSql .= " and data_income_classify in('GOODS')";
|
|
|
- } elseif ($item == 'service') {
|
|
|
- $execSql .= " and data_income_classify in('MEALS','CHNMED')";
|
|
|
- } elseif ($item == 'package') {
|
|
|
- $execSql .= " and data_income_classify = 'PACKAGE'";
|
|
|
+ if (!empty($goods['sku'])) {
|
|
|
+ foreach ($goods['sku'] as &$sku) {
|
|
|
+ if (!empty($sku['goods_sku_storage_json']) && !in_array($goods['goods_classify'], ['MEALS', 'PACKAGE'])) {
|
|
|
+ $storageJson = json_decode($sku['goods_sku_storage_json'], true);
|
|
|
+ $sku['storage'] = $storageJson['storage'];
|
|
|
+ } else {
|
|
|
+ $sku['storage'] = !empty($goods['running']) ? $goods['running']['goods_running_storage'] : 0;
|
|
|
+ }
|
|
|
}
|
|
|
- $res = Db::select($execSql . ' limit 1');
|
|
|
- $statistics[$item] = $res[0];
|
|
|
}
|
|
|
|
|
|
- return json_success('', $statistics);
|
|
|
+ return json_success('success', $goods);
|
|
|
}
|
|
|
}
|