123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace app\admin\model\system;
- use app\admin\model\order\StoreOrder;
- use app\admin\model\store\StoreProduct;
- use app\admin\model\store\StoreProductAttrValue;
- use app\admin\model\ump\StoreSeckill;
- use app\models\store\StoreCart;
- use crmeb\services\PHPExcelService;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\facade\Db;
- use think\session\Store;
- /**
- * 店铺仓库明细 model
- * Class User
- * @package app\admin\model\user
- */
- class SystemStoreProductStock extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_store_product_stock';
- use ModelTrait;
- public static function getStockList($where, $store_id)
- {
- $model = self::alias('s')
- ->field('s.*')
- ->join('store_product p', 's.product_id = p.id', 'right')
- ->join('store_product_attr_value pv', 's.unique = pv.unique', 'right')
- ->where('s.store_id', $store_id);
- if (isset($where['product_id']) && $where['product_id'] != '') {
- $model = $model->where('s.product_id', $where['product_id']);
- }
- if (isset($where['type']) && $where['type'] != '') {
- switch ($where['type']) {
- case 1:
- $model = $model->where('s.stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20);
- break;
- }
- }
- $count = $model->count();
- $model = $model->order('s.product_id asc,s.id desc');
- if (!(isset($where['excel']) && $where['excel'] == 1)) {
- $model = $model->page((int)$where['page'], (int)$where['limit']);
- }
- $data = $model->select()->each(function ($item) use ($store_id, $where) {
- $item['product_info'] = StoreProduct::get($item['product_id']);
- if ($item['product_info'])
- $item['product_info']['product_attr_info'] = StoreProductAttrValue::get(['unique' => $item['unique']]);
- if (!(isset($where['start_time']) && $where['start_time'] != '')) {
- $where['start_time'] = 0;
- } else {
- $where['start_time'] = strtotime($where['start_time']);
- }
- if (!(isset($where['end_time']) && $where['end_time'] != '')) {
- $where['end_time'] = 9999999999;
- } else {
- $where['end_time'] = strtotime($where['end_time']);
- }
- $deal_function = function ($type) use ($store_id, $where, $item) {
- $sum = 0;
- if (isset($where['data']) && $where['data'] != '') {
- $model = StoreOrder::getModelTime($where, new StoreOrder(), 'pay_time');
- } else {
- $model = new StoreOrder();
- }
- if (!(isset($where['data']) && $where['data'] != '')) {
- $model = $model->where('pay_time', 'between', [$where['start_time'], $where['end_time']]);
- }
- if ($type == 'wait') {
- $orders = $model
- ->where('store_id', $store_id)
- ->where('paid', 1)
- ->where('status', 0)
- ->where('is_del',0)
- ->where('is_system_del',0)
- ->where('refund_status', 0)
- ->select();
- } else if ($type == "wait_check") {
- $orders = $model
- ->where('store_id', $store_id)
- ->where('paid', 1)
- ->where('shipping_type', 2)
- ->where('status', 0)
- ->where('refund_status', 0)
- ->select();
- } else if ($type == "wait_send") {
- $orders = $model
- ->where('store_id', $store_id)
- ->where('paid', 1)
- ->where('status', 0)
- ->where('refund_status', 0)
- ->select();
- } else {
- $orders = $model->where('store_id', $store_id)
- ->where('paid', 1)
- ->where('refund_status', 0)
- ->select();
- }
- foreach ($orders as $v) {
- if (!$v['seckill_id'] && !$v['bargain_id'] && !$v['combination_id'] && !$v['integral_id'])
- $sum += StoreCart::where('is_del', 0)
- ->where('is_pay', 1)
- ->where('id', 'in', json_decode($v['cart_id'], true))
- ->where('product_id', $item['product_id'])
- ->where('product_attr_unique', $item['unique'])
- ->sum('cart_num');
- else {
- $info = StoreProductAttrValue::where(['unique' => $item['unique']])->find();
- $type = 0;
- $product_id = $item['product_id'];
- if ($v['seckill_id']) {
- $type = 1;
- $product_id = $v['seckill_id'];
- }
- if ($v['bargain_id']) {
- $type = 2;
- $product_id = $v['bargain_id'];
- }
- if ($v['combination_id']) {
- $type = 3;
- $product_id = $v['combination_id'];
- }
- if ($v['integral_id']) {
- $type = 4;
- $product_id = $v['integral_id'];
- }
- $unique = StoreProductAttrValue::where('suk', $info['suk'])->where('product_id', $product_id)->where('type', $type)->value('unique');
- $sum += StoreCart::where('is_del', 0)
- ->where('is_pay', 1)
- ->where('id', 'in', json_decode($v['cart_id'], true))
- ->where('product_id', $item['product_id'])
- ->where('product_attr_unique', $unique)
- ->sum('cart_num');
- }
- }
- return $sum;
- };
- $wait = $deal_function('wait');
- $wait_check = $deal_function('wait_check');
- $wait_send = $deal_function('wait_send');
- $sales = $deal_function('sales');
- $item['wait'] = $wait;
- $item['wait_check'] = $wait_check;
- $item['wait_send'] = $wait_send;
- $item['sales'] = $sales;
- });
- if (isset($where['excel']) && $where['excel'] == 1) {
- $export = [];
- foreach ($data as $index => $item) {
- $export[] = [
- $item['product_info']['store_name'],
- $item['product_info']['product_attr_info']['suk'],
- $item['stock'],
- $item['wait_check'],
- $item['wait_send'],
- $item['sales'],
- ];
- }
- PHPExcelService::setExcelHeader(['商品名称', '规格', '库存', '待核销', '待送货', '销量'])
- ->setExcelTile('门店库存导出', '门店库存信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
- ->setExcelContent($export)
- ->ExcelSave();
- }
- return compact('count', 'data');
- }
- public static function getWarning($store_id)
- {
- $model = self::where('store_id', $store_id);
- $model = $model->where('stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20);
- $count = $model->count();
- return $count;
- }
- }
|