Files
liche/admin/controllers/items/Inventory.php
T
2023-02-07 11:18:15 +08:00

330 lines
16 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:车辆盘点
* Created on: 2022/2/18 16:43
* Created by: dengbw
*/
class Inventory extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('items/Items_inventory_model', 'mdInventory');
$this->load->model('items/Items_inventory_log_model', 'mdInventoryLog');
$this->load->model('items/items_oplogs_model', 'mdItemsOplogs');
$this->load->model('items/items_model', 'mdItems');
$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');
}
public function index()
{
return $this->lists();
}
public function lists()
{
$params = $this->input->get();
$params['page'] = $params['page'] ? intval($params['page']) : 1;
$params['size'] = $params['size'] ? intval($params['size']) : 20;
$lists = $where = [];
if ($params['time']) {
$time = explode(' ~ ', $params['time']);
$time[0] && $where["c_time>="] = strtotime($time[0] . ' 00:00:00');
$time[1] && $where["c_time<="] = strtotime($time[1] . ' 23:59:59');
}
$total = $this->mdInventory->count($where);
if ($total) {
$res = $this->mdInventory->select($where, "c_time desc", $params['page'], $params['size']);
foreach ($res as $key => $value) {
$setValue = [];
$setValue['id'] = $value['id'];
$jsondata = $value['jsondata'] ? json_decode($value['jsondata'], true) : array();
$city_name = $jsondata['city_name'] ? '_'.$jsondata['city_name'] : '';
$setValue['title'] = '盘点时间_' . date('Y-m-d', $value['c_time']).$city_name;
$lists[] = $setValue;
}
}
$this->data['pager'] = array('count' => ceil($total / $params['size']), 'curr' => $params['page'], 'totle' => $total);
$this->data['lists'] = $lists;
$this->data['params'] = $params;
$this->data['_title'] = "车辆盘点";
$this->show_view('items/inventory/lists', true);
}
public function lists_log()
{
$params = $this->input->get();
$params['page'] = $params['page'] ? intval($params['page']) : 1;
$params['size'] = $params['size'] ? intval($params['size']) : 20;
if (!$params['inve_id']) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$re = $this->mdInventory->get(array('id' => $params['inve_id']));
if (!$re || empty($re)) {
return $this->show_json(SYS_CODE_FAIL, '车辆盘点不存在!');
}
$lists = [];
$where['lc_items_inventory_log.inve_id'] = $params['inve_id'];
if (strlen($params['status'])) {
$where['lc_items_inventory_log.status'] = $params['status'];
} else {
$where['lc_items_inventory_log.status in(0,1)'] = null;
$params['status'] = '-99';
}
if ($params['biz_id'] > 0) {
$where['lc_items_inventory_log.biz_id'] = $params['biz_id'];
} else if ($params['biz_id'] == -1 && $params['addr_id']) {
$where['lc_items_inventory_log.addr_id'] = $params['addr_id'];
} else if ($params['city_id']) {
$where_biz['status'] = 1;
$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["(lc_items_inventory_log.biz_id in ({$str_ids}) or lc_items_inventory_log.addr_id in ({$str_addr_ids}))"] = null;
} else if ($res_biz) {
$str_ids = implode(',', array_column($res_biz, 'id'));
$where["lc_items_inventory_log.biz_id in ({$str_ids})"] = null;
} else if ($res_addr) {
$str_addr_ids = implode(',', array_column($res_addr, 'id'));
$where["lc_items_inventory_log.addr_id in ({$str_addr_ids})"] = null;
} else {
$where['lc_items_inventory_log.biz_id'] = -2;
}
} else {
$params['city_id'] = '';
$params['county_id'] = '';
$params['addr_id'] = '';
$params['biz_id'] = '';
}
$params['brand_id'] && $where['lc_items.brand_id'] = $params['brand_id'];
$params['s_id'] && $where['lc_items.s_id'] = $params['s_id'];
$params['v_id'] && $where['lc_items.v_id'] = $params['v_id'];
$params['cor_id'] && $where['lc_items.cor_id'] = $params['cor_id'];
if ($params['item_status']) {
if ($params['item_status'] == 3) {
$where["lc_items.bill_time<>"] = '0000-00-00 00:00:00';
} else {
$where["lc_items.status"] = $params['item_status'];
}
}
if ($params['vin']) {
$where["lc_items.vin like '%{$params['vin']}%'"] = null;
}
$autoList[1] = $this->mdAutoBrand->select(array('status' => 1), 'id desc', 0, 0, 'id,name');
if ($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']) {
$autoList[3] = $this->mdAutoAttr->select(array('status' => 1, 'type' => 1, 's_id' => $params['s_id']), 'id desc', 0, 0, 'id,title as name');
}
$statusAry = $this->mdInventoryLog->statusAry();
$carImgAry = $this->mdInventoryLog->carImgAry();
$total = $this->db->select('lc_items_inventory_log.id')
->join('lc_items', 'lc_items.id = lc_items_inventory_log.item_id', 'left')
->where($where)
->count_all_results('lc_items_inventory_log');
if ($total) {
$offset = ($params['page'] - 1) * $params['size'];
$limit = $params['size'];
$this->db->from('lc_items_inventory_log');
$this->db->join('lc_items', 'lc_items.id = lc_items_inventory_log.item_id', 'left');
$this->db->select('lc_items_inventory_log.item_id,lc_items_inventory_log.biz_id,lc_items_inventory_log.addr_id
,lc_items_inventory_log.status,lc_items_inventory_log.jsondata
,lc_items.status as itemStatus,lc_items.bill_time,lc_items.brand_id,lc_items.s_id,lc_items.v_id,lc_items.cor_id,lc_items.vin,lc_items.in_time,lc_items.in_time');
$this->db->where($where);
$this->db->order_by('lc_items_inventory_log.id Desc');
$this->db->limit($limit, $offset);
$res = $this->db->get()->result_array();
$brand_ids = $s_ids = $attr_vids = $attr_corids = $biz_ids = $addr_ids = array();
foreach ($res as $v) {
$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'];
}
$map_brand = $map_sery = $map_vid = $map_corid = $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 ($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']}";
}
}
$row_id = 0;
foreach ($res as $key => $v) {
$address = $mileage = $itemStatus_name = $car_img = $where_logs = '';
if ($v['biz_id'] > 0) {
$address = $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}";
}
$where_logs = ["item_id" => $v['item_id'], 'type' => 2, 'biz_id_to' => $v['biz_id']];
} else if ($v['biz_id'] == -1 && $v['addr_id']) {
$address = $map_addr[$v['addr_id']];
$where_logs = ["item_id" => $v['item_id'], 'type' => 2, 'addr_id_to' => $v['addr_id']];
}
$title = "{$map_brand[$v['brand_id']]}-{$map_sery[$v['s_id']]}-{$map_vid[$v['v_id']]}-{$map_corid[$v['cor_id']]}";
if ($v['bill_time'] != '0000-00-00 00:00:00') {
$itemStatus_name = '已销售';
} else if ($v['itemStatus'] == 1) {
$itemStatus_name = '未分配';
} else if ($v['itemStatus'] == 2) {
$itemStatus_name = '已分配';
}
$jsondata = $v['jsondata'] ? json_decode($v['jsondata'], true) : [];
$jsondata['mileage'] && $mileage = $jsondata['mileage'];
/*if ($jsondata['car_img']) {
foreach ($carImgAry as $key2 => $value2) {
$src = $jsondata['car_img'][$key2];
$src && $car_img[] = ['title' => $value2, 'src' => build_qiniu_image_url($src)];
}
}*/
$car_img_layer = $car_img = array();
if ($jsondata['car_img']) {
$car_img_layer['title'] = '';
$car_img_layer['id'] = $v['item_id'];
$car_img_layer['start'] = 0;
$data = array();
$pid = 0;
foreach ($carImgAry as $key2 => $value2) {
$src = $jsondata['car_img'][$key2];
$src && $src = build_qiniu_image_url($src);
$src && $data[] = ['alert' => $value2, 'pid' => $pid, 'src' => $src, 'thumb' => $src];
$src && $car_img[] = ['title' => $value2, 'pid' => $pid, 'src' => $src];
$src && $pid += 1;
}
$data && $car_img_layer['data'] = $data;
}
$in_time = $v['in_time'];
if ($where_logs) {
$re_logs = $this->mdItemsOplogs->max('com_time', $where_logs);
$re_logs['com_time'] && $in_time = $re_logs['com_time'];
}
$lists[] = ['row_id' => $row_id, 'item_id' => $v['item_id'], 'title' => $title, 'vin' => $v['vin'], 'address' => $address,
'itemStatus_name' => $itemStatus_name, 'status_name' => $statusAry[$v['status']], 'mileage' => $mileage, 'car_img' => $car_img, 'car_img_layer'=>$car_img_layer,
'in_time' => '0000-00-00 00:00:00' == $in_time ? '' : substr($in_time, 0, 10)];
$row_id += 1;
}
}
$this->data['pager'] = array('count' => ceil($total / $params['size']), 'curr' => $params['page'], 'totle' => $total);
$this->data['lists'] = $lists;
$this->data['params'] = $params;
$this->data['autoList'] = $autoList;
$this->data['showInfo'] = ['statusAry' => $statusAry, 'itemStatusAry' => [1 => '未分配', 2 => '已分配', 3 => '已销售']];
$this->data['_title'] = '盘点(' . date('Y-m-d', $re['c_time']) . ")详情";
$this->show_view('items/inventory/lists_log', true);
}
public function get()
{
}
public function add()
{
$params = $this->input->post();
$city_id = $params['city_id'] ? intval($params['city_id']) : 0;
$force = $params['force'] ? intval($params['force']) : 0;
# 如果当前(该月)有相同的盘点任务,再弹窗确认是否继续发起
$starttime = mktime(0,0,0, date('m'),1, date('Y'));
$endtime = mktime(23,59,59, date('m'),date('t'), date('Y'));
$where = array('city_id' => $city_id, "c_time >= {$starttime}" => null, "c_time <= {$endtime}" => null);
$res = $this->mdInventory->count($where);
if (!$force && $res){
return $this->show_json(-1, '本月已有此盘点,确认要重新发起吗?');
}
$c_time = time();
#$inve_id = $this->mdInventory->add(['c_time' => $c_time]);
$jsondata = json_encode(array('city_name'=> $params['city_id'] && $params['city_name'] ? $params['city_name'] : ''));
$inve_id = $this->mdInventory->add(['c_time' => $c_time, 'city_id' => $city_id, 'jsondata' => $jsondata]);
if (!$inve_id) {
return $this->show_json(SYS_CODE_FAIL, '发起盘点失败');
}
$where = array('status>' => 0, 'bill_time' => '0000-00-00 00:00:00');
if ($city_id){
$where_biz['city_id'] = $params['city_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');
$str_ids = $res_biz ? implode(',', array_column($res_biz, 'id')) : '-2';
$str_addr_ids = $res_addr ? implode(',', array_column($res_addr, 'id')) : '-2';
$where["(biz_id in ({$str_ids}) or addr_id in ({$str_addr_ids}))"] = null;
}
$res = $this->mdItems->select($where, 'id asc', 0, 0, 'id,biz_id,addr_id');
$addDate = [];
foreach ($res as $key => $value) {
$addDate[] = ['inve_id' => $inve_id, 'item_id' => $value['id'], 'biz_id' => $value['biz_id'], 'addr_id' => $value['addr_id'], 'c_time' => $c_time];
}
$counts = count($addDate);
if ($counts) {
$ret = $this->mdInventoryLog->add_batch($addDate);
if (!$ret) {
return $this->show_json(SYS_CODE_FAIL, '发起盘点失败');
}
}
return $this->show_json(SYS_CODE_SUCCESS, '发起盘点成功');
}
public function edit()
{
}
public function del()
{
}
public function batch()
{
}
public function export()
{
}
}