Files
liche/admin/controllers/items/goods/Goods.php
T
2022-05-26 14:54:33 +08:00

1433 lines
67 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:商品
* Created on: 2021/7/14 15:03
* Created by: dengbw
*/
class Goods extends HD_Controller
{
private $statusAry = array(1 => '未分配', 2 => '已分配', 0 => '下架');
public function __construct()
{
parent::__construct();
$this->load->model('items/items_model', 'mdItems');
$this->load->model('items/items_relate_model');
$this->load->model('auto/auto_brand_model', 'mdAutoBrand');
$this->load->model('auto/auto_series_model', 'mdAutoSeries');
$this->load->model('auto/auto_attr_model', 'mdAutoAttr');
$this->load->model("biz/biz_model", 'mdBiz');
$this->load->model("sys/sys_addr_model", 'addr_model');
$this->load->model('area_model', 'mdArea');
$this->load->model('sys/sys_company_model');
}
public function index()
{
$this->lists();
}
public function lists()
{
$params = $this->input->get();
$page = $params['page'] = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] = $params['size'] ? intval($params['size']) : 20;
$where = array('id<>1' => null, 'status>' => 0);
$where_1 = $where_2 = $where_3 = array('m.id<>1' => null);
$autoList = array();
$search_type = 0;//0不连表,1联表筛选精品,2联表筛选销售,3联表筛选精品和销售
if ($params['in_time']) {
$in_time = explode(' ~ ', $params['in_time']);
if ($in_time[0]) {
$where['in_time >='] = $in_time[0] . ' 00:00:00';
$where_1['in_time >='] = $in_time[0] . ' 00:00:00';
$where_2['m.in_time >='] = $in_time[0] . ' 00:00:00';
$where_3['m.in_time >='] = $in_time[0] . ' 00:00:00';
}
if ($in_time[1]) {
$where['in_time <='] = $in_time[1] . ' 23:59:59';
$where_1['in_time <='] = $in_time[1] . ' 23:59:59';
$where_2['m.in_time <='] = $in_time[1] . ' 23:59:59';
$where_3['m.in_time <='] = $in_time[1] . ' 23:59:59';
}
}
if ($params['brand_id']) {
$where['brand_id'] = $params['brand_id'];
$where_1['brand_id'] = $params['brand_id'];
$where_2['m.brand_id'] = $params['brand_id'];
$where_3['m.brand_id'] = $params['brand_id'];
$autoList[2] = $this->mdAutoSeries->select(array('status' => 1, 'brand_id' => $params['brand_id']), 'id desc', 0, 0, 'id,name');
}
if ($params['s_id']) {
$where['s_id'] = $params['s_id'];
$where_1['s_id'] = $params['s_id'];
$where_2['m.s_id'] = $params['s_id'];
$where_3['m.s_id'] = $params['s_id'];
$autoList[3] = $this->mdAutoAttr->select(array('type' => 1, 's_id' => $params['s_id']), 'id desc', 0, 0, 'id,title as name');
}
if ($params['v_id']) {
$where['v_id'] = $params['v_id'];
$where_1['v_id'] = $params['v_id'];
$where_2['m.v_id'] = $params['v_id'];
$where_3['m.v_id'] = $params['v_id'];
}
//车身颜色
if ($params['cor_id']) {
$where['cor_id'] = $params['cor_id'];
$where_1['cor_id'] = $params['cor_id'];
$where_2['m.cor_id'] = $params['cor_id'];
$where_3['m.cor_id'] = $params['cor_id'];
} else {
$params['cor_id'] = '';
}
//地址筛选
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
$where_1['biz_id'] = $params['biz_id'];
$where_2['m.biz_id'] = $params['biz_id'];
$where_3['m.biz_id'] = $params['biz_id'];
}
if ($params['biz_id'] == -1 && $params['addr_id']) {
$where['addr_id'] = $params['addr_id'];
$where_1['addr_id'] = $params['addr_id'];
$where_2['m.addr_id'] = $params['addr_id'];
$where_3['m.addr_id'] = $params['addr_id'];
} else if ($params['city_id']) {
$where_biz['status'] = 1;
$params['city_id'] && $where_biz['city_id'] = $params['city_id'];
$params['county_id'] && $where_biz['county_id'] = $params['county_id'];
$res_biz = $this->mdBiz->select($where_biz, 'id desc', 0, 0, 'id');
$res_addr = $this->addr_model->select($where_biz, 'id desc', 0, 0, 'id');
if ($res_biz && $res_addr) {
$str_ids = implode(',', array_column($res_biz, 'id'));
$str_addr_ids = implode(',', array_column($res_addr, 'id'));
$where["(biz_id in ({$str_ids}) or addr_id in ({$str_addr_ids}))"] = null;
$where_1["(biz_id in ({$str_ids}) or addr_id in ({$str_addr_ids}))"] = null;
$where_2["(m.biz_id in ({$str_ids}) or m.addr_id in ({$str_addr_ids}))"] = null;
$where_3["(m.biz_id in ({$str_ids}) or m.addr_id in ({$str_addr_ids}))"] = null;
} else if ($res_biz) {
$str_ids = implode(',', array_column($res_biz, 'id'));
$where["biz_id in ({$str_ids})"] = null;
$where_1["biz_id in ({$str_ids})"] = null;
$where_2["m.biz_id in ({$str_ids})"] = null;
$where_3["m.biz_id in ({$str_ids})"] = null;
} else if ($res_addr) {
$str_addr_ids = implode(',', array_column($res_addr, 'id'));
$where["addr_id in ({$str_addr_ids})"] = null;
$where_1["addr_id in ({$str_addr_ids})"] = null;
$where_2["m.addr_id in ({$str_addr_ids})"] = null;
$where_3["m.addr_id in ({$str_addr_ids})"] = null;
} else {
$where['biz_id'] = -2;
$where_1['biz_id'] = -2;
$where_2['m.biz_id'] = -2;
$where_3['m.biz_id'] = -2;
}
} else {
$params['city_id'] = '';
$params['county_id'] = '';
$params['addr_id'] = '';
$params['biz_id'] = '';
}
if (strlen($params['status']) > 0) {
$where['status'] = $params['status'];
$where_1['m.status'] = $params['status'];
$where_2['m.status'] = $params['status'];
$where_3['m.status'] = $params['status'];
} else {
$params['status'] = '';
}
if (1 == $params['sold']) {//已售
$where["bill_time<>'0000-00-00 00:00:00'"] = null;
$where_1["m.bill_time<>'0000-00-00 00:00:00'"] = null;
$where_2["m.bill_time<>'0000-00-00 00:00:00'"] = null;
$where_3["m.bill_time<>'0000-00-00 00:00:00'"] = null;
$view = '/items/goods/lists_sold';
$_title = '已售车辆';
} else {
$where["bill_time"] = '0000-00-00 00:00:00';
$where_1["m.bill_time"] = '0000-00-00 00:00:00';
$where_2["m.bill_time"] = '0000-00-00 00:00:00';
$where_3["m.bill_time"] = '0000-00-00 00:00:00';
$view = '/items/goods/lists';
$_title = '库存车辆';
}
if ($params['vin']) {
$where["vin like '%{$params['vin']}%'"] = null;
$where_1["m.vin like '%{$params['vin']}%'"] = null;
$where_2["m.vin like '%{$params['vin']}%'"] = null;
$where_3["m.vin like '%{$params['vin']}%'"] = null;
}
//精品筛选
if ($params['fine_ids']) {
$fine_num = substr_count($params['fine_ids'], ',') + 1;
$where_1["j.type_id in ({$params['fine_ids']})"] = null;
$where_1["having"] = "fine_num={$fine_num}";
$where_2["j2.type_id in ({$params['fine_ids']})"] = null;
$where_2["having"] = "fine_num={$fine_num}";
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else {
$params['fine_ids'] = '';
}
//是否选入精品
if (strlen($params['is_fine']) > 0) {
if (1 == $params['is_fine']) {
$where_1["j.type_id>0"] = null;
$where_1["having"] = "fine_num>0";
$where_3["j2.type_id>0"] = null;
$where_3["having"] = "fine_num>0";
} else {
$where_1["j.type_id is null"] = null;
$where_3["j2.type_id is null"] = null;
}
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else {
$params['is_fine'] = '';
}
//销售员筛选
if ($params['admin_id']) {
$search_type = 0 == $search_type || 2 == $search_type ? 2 : 3;
$where_2['j.admin_id'] = $params['admin_id'];
$where_3['j.admin_id'] = $params['admin_id'];
} else {
$params['city_id_admin'] = '';
$params['county_id_admin'] = '';
$params['biz_id_admin'] = '';
$params['admin_id'] = '';
}
$order = 'id';
if ('fine_num' == $params['order']) {//精品标签数排序
$order = $params['order'];
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else if (in_array($params['order'], array('admin_id', 'mobile', 'uname'))) {//销售员ID、订单用户电话、订单用户名称
$order = $params['order'];
$search_type = 0 == $search_type || 2 == $search_type ? 2 : 3;
} else if ('sold_time' == $params['order']) {//售卖时间排序
$order = 'bill_time';
} else if ($params['order']) {
$order = $params['order'];
} else {
$params['order'] = $order;
}
$sort = 'desc';
if ($params['sort']) {
$sort = $params['sort'];
} else {
$params['sort'] = $sort;
}
$autoList[1] = $this->mdAutoBrand->select(array('status' => 1), 'id desc', 0, 0, 'id,name');
switch ($search_type) {
case 1://精品筛选
$count = $this->mdItems->count_fine($where_1);
break;
case 2://订单筛选
$count = $this->mdItems->count_order($where_2);
break;
case 3://精品+订单筛选
$count = $this->mdItems->count_order_fine($where_3);
break;
default:
$count = $this->mdItems->count($where);
}
$lists = array();
$fine_ids = array();
if ($count) {
switch ($search_type) {
case 1://精品筛选
$fileds = 'id,brand_id,s_id,vin,v_id,cor_id,incor_id,in_time,bill_time,biz_id,addr_id,count(type_id) as fine_num,company_id';
$orderby = "{$order} {$sort}";
$rows = $this->mdItems->select_fine($where_1, $orderby, $page, $size, $fileds);
break;
case 2://订单筛选
$fileds = 'm.id, m.brand_id, m.s_id, m.vin, m.v_id, m.cor_id, m.incor_id, m.in_time, m.bill_time, m.biz_id, m.addr_id,m.company_id';
if (in_array($order, array('admin_id', 'mobile'))) {
$orderby = "j.{$order} {$sort}";
} else if ('uname' == $order) {
$orderby = "j.name {$sort}";
} else {
$orderby = "{$order} {$sort}";
}
$rows = $this->mdItems->select_order($where_2, $orderby, $page, $size, $fileds);
break;
case 3://精品+订单筛选
$fileds = 'm.id, m.brand_id, m.s_id, m.vin, m.v_id, m.cor_id, m.incor_id, m.in_time, m.bill_time, m.biz_id,m.addr_id, count(j2.type_id) as fine_num,m.company_id';
if (in_array($order, array('admin_id', 'mobile'))) {
$orderby = "j.{$order} {$sort}";
} else if ('uname' == $order) {
$orderby = "j.name {$sort}";
} else {
$orderby = "m.{$order} {$sort}";
}
$rows = $this->mdItems->select_order_fine($where_3, $orderby, $page, $size, $fileds);
break;
default:
$fileds = 'id,brand_id,s_id,vin,v_id,cor_id,incor_id,in_time,bill_time,biz_id,addr_id,company_id';
$orderby = "{$order} {$sort}";
$rows = $this->mdItems->select($where, $orderby, $page, $size, $fileds);
}
$items_info = $this->items_info($rows);
$map_item_fine = array();
$map_order = array();
if ($items_info['ids']) {
//获取精品加装
$str_ids = implode(',', $items_info['ids']);
$where = array("item_id in ({$str_ids})" => null, 'type' => 1, 'status' => 1);
$rows_fine = $this->items_relate_model->select($where, '', 0, 0, 'item_id, type_id');
$map_item_fine = array();//按item_id分组所属精品ID
foreach ($rows_fine as $v) {
!in_array($v['type_id'], $fine_ids) && $fine_ids[] = $v['type_id'];
$map_item_fine[$v['item_id']][] = $v['type_id'];
}
//已售商品订单信息
$this->load->model('receiver/order/receiver_orders_model', 'orders_model');
$str_ids = implode(',', $items_info['ids']);
$where = array("item_id in ({$str_ids})" => null);
$rows_order = $this->orders_model->select($where, 'id desc', 0, 0, 'item_id, admin_id, name, mobile');
$admin_ids = array();
foreach ($rows_order as $v) {
$map_order[$v['item_id']] = array('admin_id' => $v['admin_id'], 'uname' => $v['name'], 'mobile' => $v['mobile']);
$v['admin_id'] && !in_array($v['admin_id'], $admin_ids) && $admin_ids[] = $v['admin_id'];
}
if ($admin_ids) {
//销售员列表
$this->load->model('app/licheb/app_licheb_users_model', 'licheb_users_model');
$str_ids = implode(',', $admin_ids);
$where = array("id in({$str_ids})" => null);
$select = 'id, uname, biz_id';
$rows_admin = $this->licheb_users_model->select($where, 'id desc', 0, 0, $select);
if ($rows_admin) {
$biz_ids = array();
$map_admin = array();
$map_biz_name = array();
foreach ($rows_admin as $v) {
$map_admin[$v['id']] = $v;
!in_array($v['biz_id'], $biz_ids) && $biz_ids[] = $v['biz_id'];
}
if ($biz_ids) {//店铺列表
$str_ids = implode(',', $biz_ids);
$where = array("id in({$str_ids})" => null);
$map_biz_name = $this->mdBiz->map('id', 'biz_name', $where);
}
foreach ($map_order as $k => $v) {
$admin_id = $v['admin_id'];
$admin = $map_admin[$admin_id];
if (!$admin) {
continue;
}
$biz_name = $map_biz_name[$admin['biz_id']];
$admin_name = "{$biz_name} {$admin['uname']}";
$map_order[$k] = array(
'admin_id' => $admin_id,
'biz_id' => $admin['biz_id'],
'admin_name' => $admin_name,
'uname' => $v['uname'],
'mobile' => $v['mobile']
);
}
}
}
}
foreach ($rows as $k => $v) {
$order = $map_order[$v['id']];
$address = '';
if ($v['biz_id'] > 0) {
$address = $items_info['map_biz'][$v['biz_id']];
$re_biz = $this->mdBiz->get(array('id' => $v['biz_id']));
if ($re_biz['county_id']) {
$re_area = $this->mdArea->get(array('county_id' => $re_biz['county_id']));
$re_area && $address = "{$re_area['city_name']} {$re_area['county_name']} {$address}";
} else if ($re_biz['city_id']) {
$re_area = $this->mdArea->get(array('city_id' => $re_biz['city_id']));
$re_area && $address = "{$re_area['city_name']} {$address}";
}
} else if ($v['biz_id'] == -1 && $v['addr_id']) {
$address = $items_info['map_addr'][$v['addr_id']];
}
$brand_name = $items_info['map_brand'][$v['brand_id']];
$s_name = $items_info['map_sery'][$v['s_id']];
$v_name = $items_info['map_vid'][$v['v_id']];
$cor_name = $items_info['map_corid'][$v['cor_id']];
$incor_name = $items_info['map_incorid'][$v['incor_id']];
$lists[] = array(
'id' => $v['id'],
'title' => "{$brand_name}-{$s_name}-{$v_name}-{$cor_name}",
'brand_name' => $brand_name,
's_name' => $s_name,
'v_name' => $v_name,
'cor_name' => $cor_name,
'incor_name' => $incor_name,
'vin' => $v['vin'],
'address' => $address,
'in_time' => '0000-00-00 00:00:00' == $v['in_time'] ? '' : substr($v['in_time'], 0, 10),
'sold_time' => '0000-00-00 00:00:00' == $v['bill_time'] ? '' : substr($v['bill_time'], 0, 10),
'fine_ids' => $map_item_fine[$v['id']] ? $map_item_fine[$v['id']] : array(),
'company_name' => $items_info['map_company'][$v['company_id']] ? $items_info['map_company'][$v['company_id']] : '',
'admin_name' => $order ? $order['admin_name'] : '',
'uname' => $order ? $order['uname'] : '',
'mobile' => $order ? (SUPER_ADMIN == $this->role ? $order['mobile'] : substr_replace($order['mobile'], '*****', 0, 5)) : '',
);
}
}
$statusAry = $this->statusAry;
unset($statusAry[0]);
$this->data['lists'] = $lists;
$this->data['fine_ids'] = $fine_ids;
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
$this->data['params'] = $params;
$this->data['autoList'] = $autoList;
$this->data['statusAry'] = $statusAry;
$this->data['_title'] = $_title;
return $this->show_view($view, true);
}
public function get()
{
$autoList = array();
$id = intval($this->input->get('id'));
$info['company_id'] = $info['brand_id'] = $info['s_id'] = $info['v_id'] = $info['cor_id'] = $info['incor_id'] = 0;
$info['if_pack'] = $info['city_id'] = $info['county_id'] = $info['biz_id'] = $info['addr_id'] = '';
$info['status'] = 1;
$autoList[2] = $autoList[3] = $autoList[4] = $autoList[5] = array();
$comList = $this->sys_company_model->select(['status' => 1], '', '', '', 'id,short');
if ($id > 0) {
$re = $this->mdItems->get(array('id' => $id));
if (!$re || empty($re)) {
return $this->show_json(SYS_CODE_FAIL, '商品不存在!');
}
$info = $re;
$info['pro_time'] = $re['pro_time'] != '0000-00-00 00:00:00' ? $re['pro_time'] : '';
$info['in_time'] = $re['in_time'] != '0000-00-00 00:00:00' ? $re['in_time'] : '';
$info['out_time'] = $re['out_time'] != '0000-00-00 00:00:00' ? $re['out_time'] : '';
$info['bill_time'] = $re['bill_time'] != '0000-00-00 00:00:00' ? $re['bill_time'] : '';
if ($re['brand_id']) {
$autoList[2] = $this->mdAutoSeries->select(array('status' => 1, 'brand_id' => $re['brand_id']), 'id desc', 0, 0, 'id,name');
}
if ($re['s_id']) {
$autoList[3] = $this->mdAutoAttr->select(array('type' => 1, 's_id' => $re['s_id']), 'id desc', 0, 0, 'id,title as name');
$autoList[4] = $this->mdAutoAttr->select(array('type' => 0, 's_id' => $re['s_id']), 'id desc', 0, 0, 'id,title as name');
$autoList[5] = $this->mdAutoAttr->select(array('type' => 2, 's_id' => $re['s_id']), 'id desc', 0, 0, 'id,title as name');
}
$info['city_id'] = '';
$info['county_id'] = '';
$info['biz_id'] = $info['biz_id'] ? $info['biz_id'] : '';
if ($info['biz_id']) {
if ($info['biz_id'] > 0) {
$row_biz = $this->mdBiz->get(array('id' => $info['biz_id']));
$info['city_id'] = $row_biz['city_id'];
$info['county_id'] = $row_biz['county_id'];
} else if ($info['addr_id']) {
$row_addr = $this->addr_model->get(array('id' => $info['addr_id']));
$info['city_id'] = $row_addr['city_id'];
$info['county_id'] = $row_addr['county_id'];
}
}
//获取精品加装
$where = array('item_id' => $id, 'type' => 1, 'status' => 1);
$rows_fine = $this->items_relate_model->select($where, '', 0, 0, 'type_id');
$fine_ids = $rows_fine ? array_column($rows_fine, 'type_id') : array();
$info['fine_ids'] = $fine_ids;
$_title = '编辑商品';
$edit_url = '/items/goods/goods/edit';
} else {
$info['fine_ids'] = array();
$info['in_time'] = date('Y-m-d H:i:s');
$_title = '新增商品';
$edit_url = '/items/goods/goods/add';
}
$autoList[1] = $this->mdAutoBrand->select(array('status' => 1), 'id desc', 0, 0, 'id,name');
$showInfo['autoList'] = $autoList;
$showInfo['comList'] = $comList;
$showInfo['statusAry'] = $this->statusAry;
$this->data['info'] = $info;
$this->data['showInfo'] = $showInfo;
$this->data['edit_url'] = $edit_url;
$this->data['_title'] = $_title;
return $this->show_view('/items/goods/edit', true);
}
public function get_info()
{
$id = intval($this->input->get('id'));
if (!$id) {
return $this->show_json(SYS_CODE_FAIL, '非法参数!');
}
$re = $this->mdItems->get(array('id' => $id));
if (!$re || empty($re)) {
return $this->show_json(SYS_CODE_FAIL, '商品不存在!');
}
$info = $re;
$re_b = $this->mdAutoBrand->get(array('id' => $re['brand_id']), 'name');
$re_s = $this->mdAutoSeries->get(array('id' => $re['s_id']), 'name');
$re_v = $this->mdAutoAttr->get(array('id' => $re['v_id']), 'title');
$re_cor = $this->mdAutoAttr->get(array('id' => $re['cor_id']), 'title');
$re_incor = $this->mdAutoAttr->get(array('id' => $re['incor_id']), 'title');
$info['brand_name'] = $re_b ? $re_b['name'] : '';
$info['s_name'] = $re_s ? $re_s['name'] : '';
$info['v_name'] = $re_v ? $re_v['title'] : '';
$info['cor_name'] = $re_cor ? $re_cor['title'] : '';
$info['incor_name'] = $re_incor ? $re_incor['title'] : '';
$info['if_pack_name'] = $re['if_pack'];
$info['status_name'] = $this->statusAry[$re['status']];
$info['ori_price'] = $re['ori_price'] . '元';
$info['dis_price'] = $re['dis_price'] . '元';
$info['bill_price'] = $re['bill_price'] . '元';
$info['buy_price'] = $re['buy_price'] . '元';
$info['discount'] = $re['discount'] ? $re['discount'] . '%' : '100%';
$info['pro_time'] = $re['pro_time'] != '0000-00-00 00:00:00' ? $re['pro_time'] : '';
$info['in_time'] = $re['in_time'] != '0000-00-00 00:00:00' ? $re['in_time'] : '';
$info['out_time'] = $re['out_time'] != '0000-00-00 00:00:00' ? $re['out_time'] : '';
$info['bill_time'] = $re['bill_time'] != '0000-00-00 00:00:00' ? $re['bill_time'] : '';
$address = '';
if ($info['biz_id'] > 0) {
$re_biz = $this->mdBiz->get(array('id' => $info['biz_id']));
$re_biz && $address = $re_biz['biz_name'];
if ($re_biz['county_id']) {
$re_area = $this->mdArea->get(array('county_id' => $re_biz['county_id']));
$re_area && $address = "{$re_area['city_name']} {$re_area['county_name']} {$address}";
} else if ($re_biz['city_id']) {
$re_area = $this->mdArea->get(array('city_id' => $re_biz['city_id']));
$re_area && $address = "{$re_area['city_name']} {$address}";
}
} else if ($info['biz_id'] == -1 && $info['addr_id']) {
$row_addr = $this->addr_model->get(array('id' => $info['addr_id']));
$row_addr && $address = "{$row_addr['city_name']} {$row_addr['county_name']} 其它 {$row_addr['title']}";
}
$info['address'] = $address;
//获取精品加装
$where = array('item_id' => $id, 'type' => 1, 'status' => 1);
$rows_fine = $this->items_relate_model->select($where, '', 0, 0, 'type_id');
$fine_ids = $rows_fine ? array_column($rows_fine, 'type_id') : array();
$info['fine_ids'] = $fine_ids;
$this->data['info'] = $info;
return $this->show_view('/items/goods/get_info');
}
private function checkInfo($info)
{
$msg = '';
$code = SYS_CODE_FAIL;
if (!$info) {
$msg = '非法参数';
} else if (!$info['brand_id']) {
$msg = '请选择品牌';
} else if (!$info['s_id']) {
$msg = '请选择车系';
} else if (!$info['v_id']) {
$msg = '请选择车型';
} else if (!$info['cor_id']) {
$msg = '请选择车身颜色';
} else if (!$info['incor_id']) {
$msg = '请选择内饰颜色';
} else if (!$info['biz_id'] && !$info['addr_id']) {
$msg = '请选择门店或地址';
} else if (!$info['in_time']) {
$msg = '请选择入库时间';
} else if (!$info['company_id']) {
$msg = '请选择公司归属';
} else {
$code = SYS_CODE_SUCCESS;
}
return array('code' => $code, 'msg' => $msg);
}
//添加单条数据
public function add()
{
$info = $this->input->post('info');
$checkInfo = $this->checkInfo($info);
if (!$checkInfo['code']) {
return $this->show_json($checkInfo['code'], $checkInfo['msg']);
}
if ($info['vin']) {
//车架号不重复
$where = array('vin' => $info['vin']);
$count = $this->mdItems->count($where);
if ($count > 0) {
return $this->show_json(SYS_CODE_FAIL, 'vin码已存在');
}
}
$addData['brand_id'] = $info['brand_id'];
$addData['s_id'] = $info['s_id'];
$addData['v_id'] = $info['v_id'];
$addData['cor_id'] = $info['cor_id'];
$addData['incor_id'] = $info['incor_id'];
$addData['if_pack'] = $info['if_pack'];
$addData['status'] = $info['status'];
$addData['company_id'] = $info['company_id'];
$addData['address'] = $info['address'] ? $info['address'] : '';
$addData['vin'] = $info['vin'] ? $info['vin'] : '';
$addData['stdard_num'] = $info['stdard_num'] ? $info['stdard_num'] : '';
$addData['engine_num'] = $info['engine_num'] ? $info['engine_num'] : '';
$addData['ori_price'] = $info['ori_price'] ? $info['ori_price'] : 0.00;
$addData['discount'] = $info['discount'] ? $info['discount'] : 0;
$addData['dis_price'] = $info['dis_price'] ? $info['dis_price'] : 0.00;
$addData['bill_num'] = $info['bill_num'] ? $info['bill_num'] : '';
$addData['bill_price'] = $info['bill_price'] ? $info['bill_price'] : 0.00;
$addData['buy_price'] = $info['buy_price'] ? $info['buy_price'] : 0.00;
$addData['pro_time'] = $info['pro_time'] ? $info['pro_time'] : '0000-00-00 00:00:00';
$addData['in_time'] = $info['in_time'] ? $info['in_time'] : date('Y-m-d H:i:s');
$addData['out_time'] = $info['out_time'] ? $info['out_time'] : '0000-00-00 00:00:00';
$addData['bill_time'] = $info['bill_time'] ? $info['bill_time'] : '0000-00-00 00:00:00';
$addData['c_time'] = time();
//存货地址
$biz_id = $addr_id = 0;
if ($info['biz_id']) {
$biz_id = $info['biz_id'];
$addr_id = $info['biz_id'] == -1 ? $info['addr_id'] : 0;
}
$addData['biz_id'] = $biz_id;
$addData['addr_id'] = $addr_id;
$id = $this->mdItems->add($addData);
if (!$id) {
return $this->show_json(SYS_CODE_FAIL, '添加失败!');
}
//保存精品
if ($info['fine_ids']) {
$adds = array();
foreach ($info['fine_ids'] as $fine_id) {
$adds[] = array(
'item_id' => $id,
'type' => 1,
'type_id' => $fine_id,
'status' => 1
);
}
$this->items_relate_model->add_batch($adds);
}
//添加入库日志
$this->load->library('entity/items_entity');
$params = array('item_id' => $id, 'type' => 1, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $addData['in_time'], 'biz_id' => $addData['biz_id'], 'addr_id' => $addData['addr_id']);
$this->items_entity->add_log($params);
$this->data['status'] = 1;
$url = $info['bill_time'] ? '/items/goods/goods?sold=1' : "/items/goods/goods";
return $this->show_json(SYS_CODE_SUCCESS, '添加成功!', $url);
}
/**
* 导入
* @return bool
*/
public function add_excel()
{
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
$res = $this->upload();
if (!$res['code']) {
return $this->show_json(0, $res['message']);
}
$file = $res['path'];
if ($res['file_ext'] == '.xls') {
$reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
} elseif ($res['file_ext'] == '.xlsx') {
$reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
}
$PHPExcel = $reader->load($file); // 文档名称
$objWorksheet = $PHPExcel->getActiveSheet();
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
if ($rowCnt > 10000) {
@unlink($file);
return $this->show_json(0, '数据大于一万请拆分多个表格导入');
}
$j = 0;
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
$vin = $objWorksheet->getCell('F' . $_row)->getValue(); //vin码
if ($vin) {//有vin时验证
$re_i = $this->mdItems->get(array('vin' => $vin));
} else {
$re_i = '';
}
if (!$re_i) {
$brand_id = $objWorksheet->getCell('A' . $_row)->getValue(); //品牌id
$re_b = $this->mdAutoBrand->get(array('name' => $brand_id));
$brand_id = $re_b ? $re_b['id'] : 0;
if (!$brand_id && !$vin) {
continue;
}
$s_id = $objWorksheet->getCell('B' . $_row)->getValue(); //车系id
$v_id = $objWorksheet->getCell('C' . $_row)->getValue(); //车型id
$cor_id = $objWorksheet->getCell('D' . $_row)->getValue(); //车身颜色id
$incor_id = $objWorksheet->getCell('E' . $_row)->getValue(); //内饰颜色id
$addr_name = $objWorksheet->getCell('G' . $_row)->getValue(); //存放地
$company_name = $objWorksheet->getCell('H' . $_row)->getValue(); //归属公司
$re_s = $this->mdAutoSeries->get(array('brand_id' => $brand_id, 'name' => $s_id));
$s_id = $re_s ? $re_s['id'] : 0;
$re_a1 = $this->mdAutoAttr->get(array('s_id' => $s_id, 'type' => 1, 'title' => $v_id));
$v_id = $re_a1 ? $re_a1['id'] : 0;
$re_a0 = $this->mdAutoAttr->get(array('s_id' => $s_id, 'type' => 0, 'title' => $cor_id));
$cor_id = $re_a0 ? $re_a0['id'] : 0;
$re_a2 = $this->mdAutoAttr->get(array('s_id' => $s_id, 'type' => 2, 'title' => $incor_id));
$incor_id = $re_a2 ? $re_a2['id'] : 0;
$biz_id = $addr_id = $company_id = 0;
if ($addr_name) {
if (strstr($addr_name, '其它_')) {//其它地址
$addr_name = str_replace("其它_", "", $addr_name);
$re_addr = $this->addr_model->get(['title' => $addr_name]);
if ($re_addr) {
$addr_id = $re_addr['id'];
$biz_id = -1;
}
} else {//门店地址
$re_biz = $this->mdBiz->get(['biz_name' => $addr_name]);
$re_biz && $biz_id = $re_biz['id'];
}
}
if ($company_name) {
$re_company = $this->sys_company_model->get(array('short' => $company_name));
$re_company && $company_id = $re_company['id'];
}
$addData = array(
'brand_id' => $brand_id,
's_id' => $s_id,
'v_id' => $v_id,
'cor_id' => $cor_id,
'incor_id' => $incor_id,
'vin' => $vin ? $vin : '',
'biz_id' => $biz_id,
'addr_id' => $addr_id,
'company_id' => $company_id,
'in_time' => date('Y-m-d H:i:s'),
'c_time' => time()
);
$id = $this->mdItems->add($addData);
if ($id) {
$j++;
}
}
}
@unlink($file);
return $this->show_json(SYS_CODE_SUCCESS, '导入完成,导入总条数为:' . $j);
}
//编辑单条数据
public function edit()
{
$info = $this->input->post('info');
$checkInfo = $this->checkInfo($info);
if (!$checkInfo['code']) {
return $this->show_json($checkInfo['code'], $checkInfo['msg']);
}
if ($info['vin']) {
//车架号不重复
$where = array('vin' => $info['vin'], "id<>{$info['id']}" => null);
$count = $this->mdItems->count($where);
if ($count > 0) {
return $this->show_json(SYS_CODE_FAIL, 'vin码已存在');
}
}
$re = $this->mdItems->get(['id' => $info['id']]);
$editData['brand_id'] = $info['brand_id'];
$editData['s_id'] = $info['s_id'];
$editData['v_id'] = $info['v_id'];
$editData['cor_id'] = $info['cor_id'];
$editData['incor_id'] = $info['incor_id'];
$editData['if_pack'] = $info['if_pack'];
$editData['status'] = $info['status'];
$editData['address'] = $info['address'];
$editData['vin'] = $info['vin'];
$editData['stdard_num'] = $info['stdard_num'];
$editData['engine_num'] = $info['engine_num'];
$editData['ori_price'] = $info['ori_price'];
$editData['discount'] = $info['discount'];
$editData['dis_price'] = $info['dis_price'];
$editData['bill_num'] = $info['bill_num'];
$editData['bill_price'] = $info['bill_price'];
$editData['buy_price'] = $info['buy_price'];
$editData['pro_time'] = $info['pro_time'];
$editData['in_time'] = $info['in_time'];
$editData['out_time'] = $info['out_time'];
$editData['bill_time'] = $info['bill_time'];
$editData['company_id'] = $info['company_id'];
//存货地址
$biz_id = $addr_id = 0;
if ($info['biz_id']) {
$biz_id = $info['biz_id'];
$addr_id = $info['biz_id'] == -1 ? $info['addr_id'] : 0;
}
$editData['biz_id'] = $biz_id;
$editData['addr_id'] = $addr_id;
$ret = $this->mdItems->update($editData, array('id' => $info['id']));
if (!$ret) {
return $this->show_json(SYS_CODE_FAIL, '修改失败!');
}
//保存精品
$where = array('item_id' => $info['id'], 'type' => 1);
//删除旧数据
$this->items_relate_model->update(array('status' => -1), $where);
$map_relate = $this->items_relate_model->map('type_id', 'status', $where);
if ($info['fine_ids']) {
$adds = array();
foreach ($info['fine_ids'] as $fine_id) {
if (!$map_relate[$fine_id]) {
//新增
$adds[] = array(
'item_id' => $info['id'],
'type' => 1,
'type_id' => $fine_id,
'status' => 1
);
} else {
//启用旧数据
$where = array('item_id' => $info['id'], 'type' => 1, 'type_id' => $fine_id);
$this->items_relate_model->update(array('status' => 1), $where);
}
}
$adds && $this->items_relate_model->add_batch($adds);
}
//添加日志
$this->load->library('entity/items_entity');
if ($re['in_time'] != $info['in_time']) {//入库时间不同修改日志
$params = array('item_id' => $info['id'], 'type' => 1, 'com_time' => $info['in_time']);
$this->items_entity->add_log($params);
}
if ($info['bill_time'] && $re['bill_time'] != $info['bill_time']) {//开票时间不同修改日志
$params = array('item_id' => $info['id'], 'type' => 3, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $info['bill_time'], 'biz_id' => $info['biz_id'], 'addr_id' => $info['addr_id']);
$this->items_entity->add_log($params);
}
$com_time = date('Y-m-d H:i:s');
if ($re['biz_id'] > 0 && $biz_id > 0) {//门店间变化
if ($re['biz_id'] != $biz_id) {
$params = array('item_id' => $info['id'], 'type' => 2, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $com_time, 'biz_id' => $re['biz_id'], 'biz_id_to' => $biz_id);
$this->items_entity->add_log($params);
}
} else if ($re['biz_id'] > 0 && $biz_id == -1) {//门店转地址变化
if ($addr_id) {
$params = array('item_id' => $info['id'], 'type' => 2, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $com_time, 'biz_id' => $re['biz_id'], 'addr_id_to' => $addr_id);
$this->items_entity->add_log($params);
}
} else if ($re['biz_id'] == -1 && $biz_id == -1) {//地址间变化
if ($re['addr_id'] != $addr_id) {
$params = array('item_id' => $info['id'], 'type' => 2, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $com_time, 'biz_id' => -1, 'addr_id' => $re['addr_id'], 'addr_id_to' => $addr_id);
$this->items_entity->add_log($params);
}
} else if ($re['biz_id'] == -1 && $biz_id > 0) {//地址转门店变化
$params = array('item_id' => $info['id'], 'type' => 2, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $com_time, 'biz_id' => -1, 'addr_id' => $re['addr_id'], 'biz_id_to' => $biz_id);
$this->items_entity->add_log($params);
}
$this->data['status'] = 2;
$url = $info['bill_time'] ? '/items/goods/goods?sold=1' : "/items/goods/goods";
return $this->show_json(SYS_CODE_SUCCESS, '修改成功!', $url);
}
//删除单条数据
public function del()
{
}
//批量操作(默认修改状态)
public function batch()
{
}
//导出数据列表
public function export()
{
$params = $this->input->get();
$where = $where_1 = $where_2 = $where_3 = $autoList = array();
$search_type = 0;//0不连表,1联表筛选精品,2联表筛选销售,3联表筛选精品和销售
if ($params['brand_id']) {
$where['brand_id'] = $params['brand_id'];
$where_1['brand_id'] = $params['brand_id'];
$where_2['m.brand_id'] = $params['brand_id'];
$where_3['m.brand_id'] = $params['brand_id'];
$autoList[2] = $this->mdAutoSeries->select(array('status' => 1, 'brand_id' => $params['brand_id']), 'id desc', 0, 0, 'id,name');
}
if ($params['s_id']) {
$where['s_id'] = $params['s_id'];
$where_1['s_id'] = $params['s_id'];
$where_2['m.s_id'] = $params['s_id'];
$where_3['m.s_id'] = $params['s_id'];
$autoList[3] = $this->mdAutoAttr->select(array('type' => 1, 's_id' => $params['s_id']), 'id desc', 0, 0, 'id,title as name');
}
if ($params['v_id']) {
$where['v_id'] = $params['v_id'];
$where_1['v_id'] = $params['v_id'];
$where_2['m.v_id'] = $params['v_id'];
$where_3['m.v_id'] = $params['v_id'];
}
//车身颜色
if ($params['cor_id']) {
$where['cor_id'] = $params['cor_id'];
$where_1['cor_id'] = $params['cor_id'];
$where_2['m.cor_id'] = $params['cor_id'];
$where_3['m.cor_id'] = $params['cor_id'];
} else {
$params['cor_id'] = '';
}
//地址筛选
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
$where_1['biz_id'] = $params['biz_id'];
$where_2['m.biz_id'] = $params['biz_id'];
$where_3['m.biz_id'] = $params['biz_id'];
}
if ($params['biz_id'] == -1 && $params['addr_id']) {
$where['addr_id'] = $params['addr_id'];
$where_1['addr_id'] = $params['addr_id'];
$where_2['m.addr_id'] = $params['addr_id'];
$where_3['m.addr_id'] = $params['addr_id'];
} else if ($params['city_id']) {
$where_biz['status'] = 1;
$params['city_id'] && $where_biz['city_id'] = $params['city_id'];
$params['county_id'] && $where_biz['county_id'] = $params['county_id'];
$res_biz = $this->mdBiz->select($where_biz, 'id desc', 0, 0, 'id');
if ($res_biz) {
$str_ids = implode(',', array_column($res_biz, 'id'));
$where["biz_id in ({$str_ids})"] = null;
$where_1["biz_id in ({$str_ids})"] = null;
$where_2["m.biz_id in ({$str_ids})"] = null;
$where_3["m.biz_id in ({$str_ids})"] = null;
} else {
$where['biz_id'] = -2;
$where_1['biz_id'] = -2;
$where_2['m.biz_id'] = -2;
$where_3['m.biz_id'] = -2;
}
} else {
$params['city_id'] = '';
$params['county_id'] = '';
$params['addr_id'] = '';
$params['biz_id'] = '';
}
if (strlen($params['status']) > 0) {
$where['status'] = $params['status'];
$where_1['m.status'] = $params['status'];
$where_2['m.status'] = $params['status'];
$where_3['m.status'] = $params['status'];
} else {
$params['status'] = '';
$where['status >'] = 0;
$where_1['m.status >'] = 0;
$where_2['m.status >'] = 0;
$where_3['m.status >'] = 0;
}
if (1 == $params['sold']) {//已售
$where["bill_time<>'0000-00-00 00:00:00'"] = null;
$where_1["m.bill_time<>'0000-00-00 00:00:00'"] = null;
$where_2["m.bill_time<>'0000-00-00 00:00:00'"] = null;
$where_3["m.bill_time<>'0000-00-00 00:00:00'"] = null;
} else {
$where["bill_time"] = '0000-00-00 00:00:00';
$where_1["m.bill_time"] = '0000-00-00 00:00:00';
$where_2["m.bill_time"] = '0000-00-00 00:00:00';
$where_3["m.bill_time"] = '0000-00-00 00:00:00';
}
if ($params['vin']) {
$where["vin like '%{$params['vin']}%'"] = null;
$where_1["vin like '%{$params['vin']}%'"] = null;
$where_2["m.vin like '%{$params['vin']}%'"] = null;
$where_3["m.vin like '%{$params['vin']}%'"] = null;
}
//精品筛选
if ($params['fine_ids']) {
$fine_num = substr_count($params['fine_ids'], ',') + 1;
$where_1["type_id in ({$params['fine_ids']})"] = null;
$where_1["having"] = "fine_num={$fine_num}";
$where_2["j2.type_id in ({$params['fine_ids']})"] = null;
$where_2["having"] = "fine_num={$fine_num}";
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else {
$params['fine_ids'] = '';
}
//是否选入精品
if (strlen($params['is_fine']) > 0) {
if (1 == $params['is_fine']) {
$where_1["j.type_id>0"] = null;
$where_1["having"] = "fine_num>0";
$where_3["j2.type_id>0"] = null;
$where_3["having"] = "fine_num>0";
} else {
$where_1["j.type_id is null"] = null;
$where_3["j2.type_id is null"] = null;
}
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else {
$params['is_fine'] = '';
}
//销售员筛选
if ($params['admin_id']) {
$search_type = 0 == $search_type || 2 == $search_type ? 2 : 3;
$where_2['j.admin_id'] = $params['admin_id'];
$where_3['j.admin_id'] = $params['admin_id'];
} else {
$params['city_id_admin'] = '';
$params['county_id_admin'] = '';
$params['biz_id_admin'] = '';
$params['admin_id'] = '';
}
$order = 'id';
if ('fine_num' == $params['order']) {//精品标签数排序
$order = $params['order'];
$search_type = 0 == $search_type || 1 == $search_type ? 1 : 3;
} else if ('admin_id' == $params['order']) {//销售员ID排序
$order = $params['order'];
$search_type = 0 == $search_type || 2 == $search_type ? 2 : 3;
} else if ('sold_time' == $params['order']) {//售卖时间排序
$order = 'bill_time';
} else if ($params['order']) {
$order = $params['order'];
} else {
$params['order'] = $order;
}
$sort = 'desc';
if ($params['sort']) {
$sort = $params['sort'];
} else {
$params['sort'] = $sort;
}
$autoList[1] = $this->mdAutoBrand->select(array('status' => 1), 'id desc', 0, 0, 'id,name');
switch ($search_type) {
case 1://精品筛选
$count = $this->mdItems->count_fine($where_1);
break;
case 2://订单筛选
$count = $this->mdItems->count_order($where_2);
break;
case 3://精品+订单筛选
$count = $this->mdItems->count_order_fine($where_3);
break;
default:
$count = $this->mdItems->count($where);
}
if ($count > 10000) {
return $this->show_json(SYS_CODE_FAIL, '数据量超过1w导出太为难我了');
} else if (0 == $count) {
return $this->show_json(SYS_CODE_SUCCESS, '当前没有数据');
}
$fileName = "车辆明细";
$indexs = array(
'id' => 'ID',
'brand_name' => '品牌',
's_name' => '车系',
'v_name' => '车型',
'cor_name' => '车身颜色',
'incor_name' => '内饰颜色',
'ori_price' => '指导价',
'sale_price' => '售价',
'bill_price' => '开票金额',
'buy_price' => '采购成本',
'dis_price' => '折扣价',
'discount' => '折扣百分比',
'city_name' => '城市名',
'county_name' => '行政区',
'address' => '存放地址',
'vin' => '车架号',
'engine_num' => '发动机号',
'stdard_num' => '合格证号',
'bill_num' => '开票号',
'auto_fine' => '精品加装',
'company_name' => '公司归属',
'status' => '状态',
'in_time' => '入库时间',
'out_time' => '出库时间',
'bill_time' => '开票时间',
);
$lists = array();
$fine_ids = array();
if ($count) {
switch ($search_type) {
case 1://精品筛选
$fileds = 'm.*,count(type_id) as fine_num';
$orderby = "{$order} {$sort}";
$rows = $this->mdItems->select_fine($where_1, $orderby, 0, 0, $fileds);
break;
case 2://订单筛选
$fileds = 'm.*';
$orderby = "{$order} {$sort}";
$rows = $this->mdItems->select_order($where_2, $orderby, 0, 0, $fileds);
break;
case 3://精品+订单筛选
$fileds = 'm.*, count(j2.type_id) as fine_num';
if (in_array($order, array('admin_id'))) {
$orderby = "{$order} {$sort}";
} else {
$orderby = "m.{$order} {$sort}";
}
$rows = $this->mdItems->select_order_fine($where_3, $orderby, 0, 0, $fileds);
break;
default:
$fileds = '*';
$orderby = "{$order} {$sort}";
$rows = $this->mdItems->select($where, $orderby, 0, 0, $fileds);
}
$lists[] = $indexs;
$company_ids = $brand_ids = $s_ids = $attr_vids = $attr_corids = $attr_incorids = $biz_ids = $addr_ids = array();
$ids = array();
foreach ($rows as $v) {
$ids[] = $v['id'];
$v['brand_id'] && !in_array($v['brand_id'], $brand_ids) && $brand_ids[] = $v['brand_id'];
$v['biz_id'] && !in_array($v['biz_id'], $biz_ids) && $biz_ids[] = $v['biz_id'];
$v['addr_id'] && !in_array($v['addr_id'], $addr_ids) && $addr_ids[] = $v['addr_id'];
$v['s_id'] && !in_array($v['s_id'], $s_ids) && $s_ids[] = $v['s_id'];
$v['v_id'] && !in_array($v['v_id'], $attr_vids) && $attr_vids[] = $v['v_id'];
$v['cor_id'] && !in_array($v['cor_id'], $attr_corids) && $attr_corids[] = $v['cor_id'];
$v['incor_id'] && !in_array($v['incor_id'], $attr_incorids) && $attr_incorids[] = $v['incor_id'];
$v['company_id'] && !in_array($v['company_id'], $company_ids) && $company_ids[] = $v['company_id'];
}
$map_company = $map_brand = $map_sery = $map_vid = $map_corid = $map_incorid = $map_biz = $map_addr = array();
if ($brand_ids) {
$str_ids = implode(',', $brand_ids);
$where = array("id in ({$str_ids})" => null);
$map_brand = $this->mdAutoBrand->map('id', 'name', $where);
}
if ($s_ids) {
$str_ids = implode(',', $s_ids);
$where = array("id in ({$str_ids})" => null);
$map_sery = $this->mdAutoSeries->map('id', 'name', $where);
}
if ($attr_vids) {
$str_ids = implode(',', $attr_vids);
$where = array("id in ({$str_ids})" => null);
$map_vid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($attr_corids) {
$str_ids = implode(',', $attr_corids);
$where = array("id in ({$str_ids})" => null);
$map_corid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($attr_incorids) {
$str_ids = implode(',', $attr_incorids);
$where = array("id in ({$str_ids})" => null);
$map_incorid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($biz_ids) {
$str_ids = implode(',', $biz_ids);
$where = array("id in ({$str_ids})" => null);
$map_biz = $this->mdBiz->map('id', 'biz_name', $where);
}
if ($addr_ids) {
$str_ids = implode(',', $addr_ids);
$where = array("id in ({$str_ids})" => null);
$orderby = 'id desc';
$select = 'id, title, city_name, county_name';
$map_addr = $this->addr_model->map('id', '*', $where, $orderby, 0, 0, $select);
}
if ($company_ids) {
$str_ids = implode(',', $company_ids);
$where = array("id in ({$str_ids})" => null);
$map_company = $this->sys_company_model->map('id', 'short', $where);
}
//获取精品加装
$str_ids = implode(',', $ids);
$where = array("item_id in ({$str_ids})" => null, 'type' => 1, 'status' => 1);
$rows_fine = $this->items_relate_model->select($where, '', 0, 0, 'item_id, type_id');
$map_item_fine = array();//按item_id分组所属精品ID
foreach ($rows_fine as $v) {
!in_array($v['type_id'], $fine_ids) && $fine_ids[] = $v['type_id'];
$map_item_fine[$v['item_id']][] = $v['type_id'];
}
//获取精品ID和名称映射
$map_fine = array();
if ($fine_ids) {
$this->load->model('auto/auto_fine_model');
$str_ids = implode(',', $fine_ids);
$where = array("id in ({$str_ids})" => null);
$map_fine = $this->auto_fine_model->map('id', 'title', $where);
}
foreach ($rows as $v) {
//获取并组装精品信息
$fine_ids = $map_item_fine[$v['id']];
$auto_fine = '';
if ($fine_ids) {
foreach ($fine_ids as $fine_id) {
$auto_fine = $auto_fine ? "{$auto_fine} {$map_fine[$fine_id]}" : $map_fine[$fine_id];
}
}
$addr = array();
if ($v['biz_id'] > 0) {
$re_biz = $this->mdBiz->get(array('id' => $v['biz_id']));
if ($re_biz['county_id']) {
$re_area = $this->mdArea->get(array('county_id' => $re_biz['county_id']));
if ($re_area) {
$addr = array('city_name' => $re_area['city_name'], 'county_name' => $re_area['county_name']
, 'title' => $map_biz[$v['biz_id']]);
}
} else if ($re_biz['city_id']) {
$re_area = $this->mdArea->get(array('city_id' => $re_biz['city_id']));
if ($re_area) {
$addr = array('city_name' => $re_area['city_name'], 'title' => $map_biz[$v['biz_id']]);
}
}
} else if ($v['biz_id'] == -1 && $v['addr_id']) {
$addr = $map_addr[$v['addr_id']];
}
$lists[] = array(
'id' => $v['id'],
'brand_name' => $map_brand[$v['brand_id']],
's_name' => $map_sery[$v['s_id']],
'v_name' => $map_vid[$v['v_id']],
'cor_name' => $map_corid[$v['cor_id']],
'incor_name' => $map_incorid[$v['incor_id']],
'ori_price' => $v['ori_price'],
'sale_price' => $v['sale_price'],
'bill_price' => $v['bill_price'],
'buy_price' => $v['buy_price'],
'dis_price' => $v['dis_price'],
'discount' => $v['discount'],
'city_name' => $addr ? $addr['city_name'] : '',
'county_name' => $addr ? $addr['county_name'] : '',
'address' => $addr ? $addr['title'] : '',
'vin' => $v['vin'],
'engine_num' => $v['engine_num'],
'stdard_num' => $v['stdard_num'],
'bill_num' => $v['bill_num'],
'auto_fine' => $auto_fine,
'company_name' => $map_company[$v['company_id']],
'status' => $this->statusAry[$v['status']],
'in_time' => '0000-00-00 00:00:00' == $v['in_time'] ? '' : $v['in_time'],
'out_time' => '0000-00-00 00:00:00' == $v['out_time'] ? '' : $v['out_time'],
'bill_time' => '0000-00-00 00:00:00' == $v['bill_time'] ? '' : $v['bill_time'],
);
}
}
$this->load->library('excel');
$this->excel->out_csv($lists, $indexs, $fileName . "_" . date('YmdHis'));
return 1;
}
public function export_overdue()
{
$this->load->model('items/items_oplogs_model', 'mdItemsOplogs');
$fileName = '超期车辆';
$where = ['status<>' => 0, 'bill_time' => '0000-00-00 00:00:00'];
$res = $this->mdItems->select($where, 'id asc', 1, 5000, 'brand_id,biz_id,addr_id,s_id,v_id,cor_id,incor_id,company_id,id,vin,in_time,status');
$data = $list = [];
foreach ($res as $key => $value) {
$in_time = $value['in_time'];
if ($value['biz_id'] > 0) {
$where_logs = ["item_id" => $value['id'], 'type' => 2, 'biz_id_to' => $value['biz_id']];
} else {
$where_logs = ["item_id" => $value['id'], 'type' => 2, 'addr_id_to' => $value['addr_id']];
}
$re_logs = $this->mdItemsOplogs->max('com_time', $where_logs);
$re_logs['com_time'] && $in_time = $re_logs['com_time'];
if ($in_time && $in_time != '0000-00-00 00:00:00') {
$days = round((time() - strtotime($in_time)) / 3600 / 24);
if ($days > 30) {
$value['in_time'] = $in_time;
$value['days'] = $days;
$list[] = $value;
}
}
}
if ($list) {
$items_info = $this->items_info($list);
foreach ($list as $key => $value) {
$address = '';
if ($value['biz_id'] > 0) {
$address = $items_info['map_biz'][$value['biz_id']];
$re_biz = $this->mdBiz->get(array('id' => $value['biz_id']));
if ($re_biz['county_id']) {
$re_area = $this->mdArea->get(array('county_id' => $re_biz['county_id']));
$re_area && $address = "{$re_area['city_name']} {$re_area['county_name']} {$address}";
} else if ($re_biz['city_id']) {
$re_area = $this->mdArea->get(array('city_id' => $re_biz['city_id']));
$re_area && $address = "{$re_area['city_name']} {$address}";
}
} else if ($value['biz_id'] == -1 && $value['addr_id']) {
$address = $items_info['map_addr'][$value['addr_id']];
}
$title = "{$items_info['map_brand'][$value['brand_id']]}-{$items_info['map_sery'][$value['s_id']]}-{$items_info['map_vid'][$value['v_id']]}-{$items_info['map_corid'][$value['cor_id']]}";
$temp['address'] = $address;
$temp['title'] = $title;
$temp['vin'] = $value['vin'];
$temp['in_time'] = $value['in_time'];
$temp['days'] = $value['days'];
$temp['status_name'] = $value['status'] == 1 ? '未分配' : '已分配';
$data[] = $temp;
}
}
$indexs = [
'address' => '门店',
'title' => '车辆',
'vin' => '车架号',
'in_time' => '入库日期',
'days' => '库存天数',
"status_name" => "状态",
];
array_unshift($data, $indexs);
$this->load->library('excel');
$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
}
/**
* Notes:获取商品信息
* Created on: 2022/3/3 16:35
* Created by: dengbw
* @param $list
* @return array
*/
private function items_info($list)
{
$company_ids = $brand_ids = $s_ids = $attr_vids = $attr_corids = $attr_incorids = $biz_ids = $addr_ids = $ids = [];
foreach ($list as $v) {
$ids[] = $v['id'];
$v['brand_id'] && !in_array($v['brand_id'], $brand_ids) && $brand_ids[] = $v['brand_id'];
$v['biz_id'] && !in_array($v['biz_id'], $biz_ids) && $biz_ids[] = $v['biz_id'];
$v['addr_id'] && !in_array($v['addr_id'], $addr_ids) && $addr_ids[] = $v['addr_id'];
$v['s_id'] && !in_array($v['s_id'], $s_ids) && $s_ids[] = $v['s_id'];
$v['v_id'] && !in_array($v['v_id'], $attr_vids) && $attr_vids[] = $v['v_id'];
$v['cor_id'] && !in_array($v['cor_id'], $attr_corids) && $attr_corids[] = $v['cor_id'];
$v['incor_id'] && !in_array($v['incor_id'], $attr_incorids) && $attr_incorids[] = $v['incor_id'];
$v['company_id'] && !in_array($v['company_id'], $company_ids) && $company_ids[] = $v['company_id'];
}
$map_company = $map_brand = $map_sery = $map_vid = $map_corid = $map_incorid = $map_biz = $map_addr = array();
if ($brand_ids) {
$str_ids = implode(',', $brand_ids);
$where = array("id in ({$str_ids})" => null);
$map_brand = $this->mdAutoBrand->map('id', 'name', $where);
}
if ($s_ids) {
$str_ids = implode(',', $s_ids);
$where = array("id in ({$str_ids})" => null);
$map_sery = $this->mdAutoSeries->map('id', 'name', $where);
}
if ($attr_vids) {
$str_ids = implode(',', $attr_vids);
$where = array("id in ({$str_ids})" => null);
$map_vid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($attr_corids) {
$str_ids = implode(',', $attr_corids);
$where = array("id in ({$str_ids})" => null);
$map_corid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($attr_incorids) {
$str_ids = implode(',', $attr_incorids);
$where = array("id in ({$str_ids})" => null);
$map_incorid = $this->mdAutoAttr->map('id', 'title', $where);
}
if ($biz_ids) {
$str_ids = implode(',', $biz_ids);
$where = array("id in ({$str_ids})" => null);
$map_biz = $this->mdBiz->map('id', 'biz_name', $where);
}
if ($addr_ids) {
$str_ids = implode(',', $addr_ids);
$where = array("id in ({$str_ids})" => null);
$orderby = 'id desc';
$select = 'id, title, city_name, county_name';
$rows_addr = $this->addr_model->select($where, $orderby, 0, 0, $select);
foreach ($rows_addr as $v) {
$map_addr[$v['id']] = "{$v['city_name']} {$v['county_name']} 其它 {$v['title']}";
}
}
if ($company_ids) {
$str_ids = implode(',', $company_ids);
$where = array("id in ({$str_ids})" => null);
$map_company = $this->sys_company_model->map('id', 'short', $where);
}
$date = ['map_brand' => $map_brand, 'map_sery' => $map_sery, 'map_vid' => $map_vid, 'map_corid' => $map_corid, 'map_incorid' => $map_incorid
, 'map_biz' => $map_biz, 'map_addr' => $map_addr, 'map_company' => $map_company, 'ids' => $ids];
return $date;
}
/**
* Notes:上传excel
* Created on: 2021/7/14 15:06
* Created by: dengbw
* @return array
*/
private function upload()
{
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/';
$config['allowed_types'] = '*';//xls|xlsx
$config['max_size'] = 5 * 1024;
$config['file_name'] = 'items_' . time() . rand(1, 99999);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', ''));
} else {
$data = $this->upload->data();
return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']);
}
}
function json_info()
{
$vin = trim($this->input->post('vin'));
$item_id = intval($this->input->post('item_id'));
if ($vin) {
$re = $this->mdItems->get(array('vin' => $vin));
} else {
$re = $this->mdItems->get(array('id' => $item_id));
}
if (!$re || empty($re)) {
return $this->show_json(SYS_CODE_FAIL, '车辆不存在!');
}
$re_b = $this->mdAutoBrand->get(array('id' => $re['brand_id']), 'name');
$re_s = $this->mdAutoSeries->get(array('id' => $re['s_id']), 'name');
$re_v = $this->mdAutoAttr->get(array('id' => $re['v_id']), 'title');
$re_cor = $this->mdAutoAttr->get(array('id' => $re['cor_id']), 'title');
$title = '';
$re_b['name'] && $title = $re_b['name'];
$re_s['name'] && $title = $title ? $title . '-' . $re_s['name'] : $re_s['name'];
$re_v['title'] && $title = $title ? $title . '-' . $re_v['title'] : $re_v['title'];
$info['id'] = $re['id'];
$info['title'] = $title;
$info['vin'] = $re['vin'];
$info['cor'] = $re_cor['title'] ? $re_cor['title'] : '';
$address = '';
if ($re['biz_id'] > 0) {
$re_biz = $this->mdBiz->get(array('id' => $re['biz_id']));
$re_biz && $address = $re_biz['biz_name'];
if ($re_biz['county_id']) {
$re_area = $this->mdArea->get(array('county_id' => $re_biz['county_id']));
$re_area && $address = "{$re_area['city_name']} {$re_area['county_name']} {$address}";
} else if ($re_biz['city_id']) {
$re_area = $this->mdArea->get(array('city_id' => $re_biz['city_id']));
$re_area && $address = "{$re_area['city_name']} {$address}";
}
} else if ($re['biz_id'] == -1 && $re['addr_id']) {
$row_addr = $this->addr_model->get(array('id' => $re['addr_id']));
$row_addr && $address = "{$row_addr['city_name']} {$row_addr['county_name']} 其它 {$row_addr['title']}";
}
$info['address'] = $address;
$this->data = $info;
return $this->show_json(SYS_CODE_SUCCESS);
}
}