|
@@ -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,17 +12,163 @@ 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','');
|
|
|
+ $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]);
|
|
|
+ }
|
|
|
+ $searchGoodsIds = [];
|
|
|
+ if (!empty($classify)) {
|
|
|
+ $searchGoodsIds = Goods::where('goods_classify', $classify)->pluck('goods_id')->toArray();
|
|
|
+ } elseif (!empty($categoryId)) {
|
|
|
+ $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
|
|
|
+ $searchCategoryIds = [];
|
|
|
+ if (!empty($category)) {
|
|
|
+ $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
|
|
|
+ }
|
|
|
+ $searchCategoryIds[] = $categoryId;
|
|
|
+
|
|
|
+ $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ $dataInOuts = DataInout::where('data_inout_status', 'VALID')
|
|
|
+ ->where('data_inout_classify', 'IN')
|
|
|
+ ->whereBetween('data_inout_addtimes', $days)
|
|
|
+ ->when(!empty($payType), function ($query) use ($payType) {
|
|
|
+ $query->where('data_inout_pay_type', $payType);
|
|
|
+ })->when(!empty($goodsName), function ($query) use ($goodsName) {
|
|
|
+ $query->where('join_data_inout_object_json', 'like', "'" . $goodsName . "'");
|
|
|
+ })->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) {
|
|
|
+ $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'] = $good['order_sheet_num'] + $statistics[$orderClassify]['total'];
|
|
|
+ $statistics[$orderClassify]['amount'] = $good['order_sheet_pay'] + $statistics[$orderClassify]['amount'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
|
|
|
+ $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
|
|
|
+
|
|
|
+ // 交集
|
|
|
+ if (!empty($classify) || !empty($categoryId)) {
|
|
|
+ $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
|
|
|
+
|
|
|
+ $goods = array_filter($goods, function ($item) use ($goodsIds) {
|
|
|
+ return in_array($item['goods_id'], $goodsIds);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ 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) {
|
|
|
+ $rowGoods = Goods::with([
|
|
|
+ 'category',
|
|
|
+ 'skuOne' => function ($query) use ($row) {
|
|
|
+ $query->where('goods_sku_id', $row['goods_sku_id']);
|
|
|
+ }
|
|
|
+ ])->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,
|
|
|
+ 'rows' => $rows,
|
|
|
+ 'statistics' => $statistics
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public function listOld(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', '');
|
|
|
if (!empty($days)) {
|
|
|
$days[0] = strtotime($days[0]);
|
|
|
$days[1] = strtotime($days[1]);
|
|
@@ -32,15 +181,15 @@ class GoodsSalesController
|
|
|
} 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);
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
$recordStart = ($page - 1) * 20;
|
|
@@ -49,19 +198,19 @@ class GoodsSalesController
|
|
|
if (!empty($days)) {
|
|
|
$where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
|
|
|
}
|
|
|
- if (!empty($classify)){
|
|
|
+ if (!empty($classify)) {
|
|
|
$where .= " and di.data_income_classify = '{$classify}'";
|
|
|
}
|
|
|
- if (!empty($categorySearchIds)){
|
|
|
+ if (!empty($categorySearchIds)) {
|
|
|
$where .= " and g.join_goods_category_id in({$categorySearchIds})";
|
|
|
}
|
|
|
- if (!empty($goodsName)){
|
|
|
+ if (!empty($goodsName)) {
|
|
|
$where .= " and g.goods_name like '%{$goodsName}%' ";
|
|
|
}
|
|
|
- if (!empty($premisesId)){
|
|
|
+ if (!empty($premisesId)) {
|
|
|
$where .= " and di.join_data_income_dept_id = {$premisesId}";
|
|
|
}
|
|
|
- if (!empty($payType)){
|
|
|
+ if (!empty($payType)) {
|
|
|
$where .= " and di.data_income_pay_type = '{$payType}'";
|
|
|
}
|
|
|
|
|
@@ -118,11 +267,11 @@ class GoodsSalesController
|
|
|
public function statistics(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','');
|
|
|
+ $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]);
|
|
@@ -135,34 +284,34 @@ class GoodsSalesController
|
|
|
} 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);
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
$where = "where 1";
|
|
|
if (!empty($days)) {
|
|
|
$where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
|
|
|
}
|
|
|
- if (!empty($classify)){
|
|
|
+ if (!empty($classify)) {
|
|
|
$where .= " and di.data_income_classify = '{$classify}'";
|
|
|
}
|
|
|
- if (!empty($categorySearchIds)){
|
|
|
+ if (!empty($categorySearchIds)) {
|
|
|
$where .= " and g.join_goods_category_id in({$categorySearchIds})";
|
|
|
}
|
|
|
- if (!empty($goodsName)){
|
|
|
+ if (!empty($goodsName)) {
|
|
|
$where .= " and g.goods_name like '%{$goodsName}%' ";
|
|
|
}
|
|
|
- if (!empty($premisesId)){
|
|
|
+ if (!empty($premisesId)) {
|
|
|
$where .= " and di.join_data_income_dept_id = {$premisesId}";
|
|
|
}
|
|
|
- if (!empty($payType)){
|
|
|
+ if (!empty($payType)) {
|
|
|
$where .= " and di.data_income_pay_type = '{$payType}'";
|
|
|
}
|
|
|
|