文件
mashangyoujiu/Application/Retail/Controller/MgoodsController.class.php

1758 行
68 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Retail\Controller;
use Common\Business\DownFile;
use Common\Business\GoodsClassify;
use Common\Business\GoodsState;
use Common\Business\OperateMGoods;
use Common\Business\Plug;
use Common\Business\SerialNumberGoods;
use Common\Business\SkuAttribute;
use Common\Model\ImageGalleryModel;
use Common\Model\RetailDaDaCityModel;
use Common\Model\RetailGoodsClassModel;
use Common\Model\RetailMgoodsModel;
use Common\Model\SaleMGoodsModel;
use Common\Model\SkuMGoodsViewModel;
use Guzzle\Parser\ParserRegistry;
use Think\Controller;
use Common\Business\Goods;
use Common\Model\RetailGoodsBrandModel;
//[1.9.20]
/**
* 商户商品档案
* Class MgoodsController
* @package Retail\Controller
*/
class MgoodsController extends BaseController
{
public function index()
{
$this->display();
}
//获取商户平台类型 1默认系统 2微礼平台
//德育币插件 true使用 false不使用
public function getPlatformType()
{
$Plug = new Plug();
$plugData = $Plug->merPlugState([
'merchant_id' => $this->merchant_id,
'identification_arr' => ['moraleducationcoin'],
]);
$result = [
'status' => 1,
'message' => '获取成功',
'platform_type' => $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1,
'moral_education_coin' => $plugData['moraleducationcoin'],
];
$this->ajaxReturn($result);
}
/**
* 获取商品列表
*/
public function goodsList()
{
$param = array(
'platform_type' => $this->role_info['platform_type'],
'merchant_id' => $this->merchant_id,
'page' => I('post.page') ? I('post.page') : 1, //页
'limit' => I('post.limit') ? I('post.limit') : 10, //每页数量
'need_img' => true, //是否需要图片
'need_goods_num' => true, //是否需要库存
'need_count' => true, //是否需要总数量
'keyword' => I('post.keyword'),
'keyword_type' => I('post.keyword_type'),
'can_exchange_weigh' => I('post.can_exchange_weigh'),
'condition_name' => I("post.condition_name"), //排序规则关键词
'order_type' => I("post.order_type"), //排序规则
//按照商品类别搜索
'person1' => I('post.person1'),
'person2' => I('post.person2'),
'person3' => I('post.person3'),
'status' => I('post.status'),//商品状态 0上架 1下架
'issellonline' => I('post.issellonline'), //商城出售状态
'attribute_state' => I('post.attribute'), //规格 1无规格 2规格
'recovery_type' => I('post.recovery_type'), //2回收站
);
$SkuGoodsViewModel = new OperateMGoods();
$ret = $SkuGoodsViewModel->getMerGoodsList($param);
$this->ajaxReturn($ret);
}
public function goods_cate()
{
$user = session('ls_userinfo');
//根据商户查询处所有有效的一级分类
$map['pid'] = array('eq', 0);
$map['status'] = 0;
$map['merchantid'] = $user['merchantid'];
$class_name = M('retail_goods_class')->where(array('merchantid' => $user['merchantid'], 'status' => 0, 'type' => 1))->order('code asc')->select();
$this->assign('class_name', $class_name);
$this->display();
}
//异步获取数据 渲染layer表格
public function catesList()
{
//直接根据门店id查询出该门店所有分类
$user = session('ls_userinfo');
$where['merchantid'] = $user['merchantid'];
$where['pid'] = 0;
$where['status'] = 0;
//获取查询条件
if (!empty($_GET['keyword'])) {
if (is_numeric($_GET['keyword'])) {
//纯数字直接查询code编码
$where['code'] = array('like', '%' . $_GET['keyword'] . '%');
} else {
$where['class_name'] = array('like', '%' . $_GET['keyword'] . '%');
}
$this->assign('code_name', $_GET['keyword']);
}
$page = I('get.page');
$limit = I('get.limit');
$cates = M('retail_goods_class')->where($where)->page($page, $limit)->order('id asc')->select();
$cates_count = M('retail_goods_class')->where($where)->count();
//处理序号
if (!empty($cates)) {
foreach ($cates as $key => &$value) {
$value['key_num'] = $key + 1;
//统计出有效的二级分类
$num = M("retail_goods_class")->where(array('pid' => $value['id'], 'status' => 0))->count();
$cates[$key]['c_num'] = $num ? $num : 0;
}
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['count'] = $cates_count;
$rt['data'] = $cates;
$rt['post'] = $user;
$this->ajaxReturn($rt);
}
/**
* 添加/编辑类别
*/
public function cateSet()
{
$user = session('ls_userinfo');
if (IS_POST) {
$data = I('post.');
//判断是否有id传入
if (!$data['id']) {
$data['merchantid'] = $user['merchantid'];
//分类名称不能重复
$map['class_name'] = I("post.class_name");
$find = M("retail_goods_class")->where($map)->find();
if ($find) {
//存在
$res['status'] = 0;
$res['msg'] = '该分类已存在,请更换分类名';
} else {
//不存在id 创建新的数据
$data['addtime'] = time();
$create = M('retail_goods_class')->add($data);
if ($create) {
$res['status'] = 1;
$res['msg'] = '创建成功';
$res['data'] = $data;
}
}
$this->ajaxReturn($res);
} else {
$map['class_name'] = I("post.class_name");
$map['id'] = array('neq', I("post.id"));
$find = M("retail_goods_class")->where($map)->find();
if ($find) {
$res['status'] = 0;
$res['msg'] = '该分类已存在,请更换分类名';
} else {
$create = M('retail_goods_class')->where(array('id' => $data['id'], 'merchantid' => $user['merchantid']))->save($data);
if ($create) {
$res['status'] = 1;
$res['msg'] = '修改成功';
}
}
$this->ajaxReturn($res);
}
}
}
//+=======================================================================
//- 上面有二级分类
//+=======================================================================
/* 门店同步过的商品改为未同步 */
public function sync_two($goods_id)
{
$sync_two = M('retail_goods')->where(array('m_goods_id' => $goods_id, 'sync_state' => 1))->find();
if ($sync_two) {
M('retail_goods')->where(array('m_goods_id' => $goods_id))->setField('sync_state', 0);
}
}
/**
* 自动获取获取货号
*/
public function auto_number()
{
$user = session('ls_userinfo');
if (IS_POST) {
$i = 0;
while (1) {
$num = rand(1000, 99999);
$where['merchantid'] = $user['merchantid'];
$where['good_number'] = $num;
$where['status'] = array('in', '0,1');
$info = M('retail_mgoods')->where($where)->field('id')->find();
if (empty($info)) {
$res['status'] = 1;
$res['info'] = $num;
$res['msg'] = '获取成功';
$this->ajaxReturn($res);
break;
}
$i++;
if ($i > 30) {
$res['status'] = 0;
$res['msg'] = '获取失败,请重试';
$this->ajaxReturn($res);
break;
}
}
}
}
/**
* 自动获取商品类别编码
*/
public function atuoGetCode()
{
$user = session('ls_userinfo');
//根据当前门店id查询当前类别code编码最大的一条数据
$max_code = M('retail_goods_class')->field('code')->where(array('merchantid' => $user['merchantid']))->order('code desc')->limit('1')->find();
$code = $max_code['code'];
if (empty($code)) {
$new_code = '000001';
} else {
$new_code = strval($code + 1);
if ($new_code >= 10) {
$new_code = '0000' . $new_code;
} else {
$new_code = '00000' . $new_code;
}
}
$res['status'] = 1;
$res['code'] = $new_code;
$this->ajaxReturn($res);
}
/**
* 获取分类旧数据
*/
public function getCateOldInfo()
{
if (IS_POST) {
$id = $_POST['id'];
$info = M('retail_goods_class')->where(array('id' => $id))->find();
if ($info) {
$res['status'] = 1;
$res['msg'] = '获取成功';
$res['info'] = $info;
} else {
$res['status'] = 0;
$res['msg'] = '暂无数据';
}
$this->ajaxReturn($res);
}
}
/**
* 批量删除商品
*/
public function moreDelete()
{
if (IS_POST) {
$ids = $_POST['id'];
$new_ids = explode(',', trim($ids, ',')); //去除最后的逗号并转换成数组
// var_dump($new_ids);exit;
foreach ($new_ids as $key => $value) {
//循环删除
$delete = M('retail_goods')->where(array('id' => $value))->setField('status', '2');
}
if ($delete) {
$res['status'] = 1;
$res['msg'] = '删除成功';
} else {
$res['status'] = 0;
$res['msg'] = '删除失败';
}
$this->ajaxReturn($res);
}
}
/**
* 删除单个商品
*/
public function singleDelete()
{
if (IS_POST) {
$id = I('post.id');
$goodsInfo = M('retail_mgoods')->where(array('id' => $id))->find();
if ($goodsInfo) {
$delete = M('retail_mgoods')->where(array('id' => $id))->setField('status', 2);
if ($delete) {
if ($goodsInfo['attribute'] == 2) {
//规格商品规格状态修改
M('retail_sku_mgoods')->where(array('mgoods_id' => $id))->setField('state', 1);
}
//删除销售库商品
$success = $this->updateSaleGoods($id, $sku_id = '', 2);
if ($success) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
$desc = '商品档案-删除单商品'; //操作行为描述
action_log($source, $type, $request_data, $desc);
$res['status'] = 1;
$res['msg'] = '删除成功';
} else {
$res['status'] = 0;
$res['msg'] = '商品库存状态删除失败,请刷新重试';
}
} else {
$res['status'] = 0;
$res['msg'] = '删除失败';
}
} else {
$res['status'] = 0;
$res['msg'] = '未找到该商品,请刷新重试';
}
$this->ajaxReturn($res);
}
}
/**
* 批量删除商品类别
*/
public function cateDelete()
{
if (IS_POST) {
$cate = M('retail_goods_class');
$id = I("post.id");
$type = I("post.type"); //1:删除一级分类 2:删除二级分类
if (empty($id) || empty($type)) {
$res['status'] = 0;
$res['msg'] = '参数有误,请刷新后重试';
} else {
//根据分类的不同 需要进行不同的删除判断操作
if ($type == 1) {
//只需判断是否有二级分类正在使用此分类
$map['pid'] = $id;
$map['status'] = 0;
$find = M('retail_goods_class')->where($map)->find();
if (!empty($find)) {
$res['status'] = 0;
$res['msg'] = '该分类下存在二级分类,无法删除';
} else {
$delete = $cate->where(array('id' => $id))->setField('status', 1);
if ($delete) {
$res['status'] = 1;
$res['msg'] = '删除成功';
} else {
$res['status'] = 0;
$res['msg'] = '删除失败';
}
}
} elseif ($type == 2) {
$where['goods_class_id'] = $id;
$where['status'] = 0;
$find = M('retail_mgoods')->where($where)->find();
$find1 = M('retail_goods')->where($where)->find();
// echo M()->_sql();exit;
if (!empty($find) || !empty($find1)) {
$res['status'] = 0;
$res['msg'] = '删除失败,该类别下还有商品存在!';
} else {
$delete = $cate->where(array('id' => $id))->setField('status', 1);
if ($delete) {
$res['status'] = 1;
$res['msg'] = '删除成功';
} else {
$res['status'] = 0;
$res['msg'] = '删除失败';
}
}
}
}
$this->ajaxReturn($res);
}
}
//店内出售状态
public function changeStoreState()
{
$content = I('post.');
$data['operator_type'] = 1;
$data['user_type'] = 3;
$data['merchant_id'] = $this->merchant_id;
$data['goods_id'] = $content['id'];
$data['retail_state'] = $content['status'];
$GoodsStateClass = new GoodsState();
$result = $GoodsStateClass->changeStoreSale($data);
$this->ajaxReturn($result);
}
// 编辑商城出售状态
public function changeOnlineState()
{
if (IS_POST) {
$id = I('post.id');
$ist = I('post.issellonline');
$ret = M('retail_mgoods')->where(array('merchantid' => $this->merchant_id, 'id' => $id))->save(array('issellonline' => $ist));
if ($ret) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
if ($ist == 1) {
$desc = '商品档案-开启商城出售'; //操作行为描述
} elseif ($ist == 0) {
$desc = '商品档案-关闭商城出售'; //操作行为描述
}
action_log($source, $type, $request_data, $desc);
M('retail_goods')->where(array('m_goods_id' => $id))->save(array('sync_state' => 0));
$rt['status'] = 1;
$rt['msg'] = 'ok';
} else {
$rt['status'] = 2;
$rt['msg'] = 'not ok';
}
$this->ajaxReturn($rt);
}
}
// 编辑上下架状态
public function changeStatus()
{
if (IS_POST) {
$id = I('post.id');
$ist = I('post.status');
$ret = M('retail_mgoods')->where(array('merchantid' => $this->merchant_id, 'id' => $id))->save(array('status' => $ist));
if ($ret) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
if ($ist == 1) {
$desc = '商品档案-商品下架'; //操作行为描述
} elseif ($ist == 0) {
$desc = '商品档案-商品上架'; //操作行为描述
}
action_log($source, $type, $request_data, $desc);
$this->updateSaleGoods($id, $sku_id = "", $ist);
M('retail_goods')->where(array('m_goods_id' => $id))->save(array('sync_state' => 0));
$rt['status'] = 1;
$rt['msg'] = 'ok';
} else {
$rt['status'] = 2;
$rt['msg'] = 'not ok';
}
$this->ajaxReturn($rt);
}
}
// 编辑会员线上打折状态
public function changeVipOnlineState()
{
if (IS_POST) {
$id = I('post.id');
$ist = I('post.vip_online_state');
$ret = M('retail_mgoods')->where(array('merchantid' => $this->merchant_id, 'id' => $id))->save(array('vip_online_state' => $ist));
if ($ret) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
if ($ist == 1) {
$desc = '商品档案-开启线上打折'; //操作行为描述
} elseif ($ist == 0) {
$desc = '商品档案-关闭线上打折'; //操作行为描述
}
action_log($source, $type, $request_data, $desc);
M('retail_goods')->where(array('m_goods_id' => $id))->save(array('sync_state' => 0));
$rt['status'] = 1;
$rt['msg'] = 'ok';
} else {
$rt['status'] = 2;
$rt['msg'] = 'not ok';
}
$this->ajaxReturn($rt);
}
}
// 编辑会员线下打折状态
public function changeVipRetailState()
{
if (IS_POST) {
$id = I('post.id');
$ist = I('post.vip_retail_state');
$ret = M('retail_mgoods')->where(array('merchantid' => $this->merchant_id, 'id' => $id))->save(array('vip_retail_state' => $ist));
if ($ret) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
if ($ist == 1) {
$desc = '商品档案-开启线下打折'; //操作行为描述
} elseif ($ist == 0) {
$desc = '商品档案-关闭线下打折'; //操作行为描述
}
action_log($source, $type, $request_data, $desc);
M('retail_goods')->where(array('m_goods_id' => $id))->save(array('sync_state' => 0));
$rt['status'] = 1;
$rt['msg'] = 'ok';
} else {
$rt['status'] = 2;
$rt['msg'] = 'not ok';
}
$this->ajaxReturn($rt);
}
}
/**
* 规格或非规格导出
*/
public function SkuMGoodsExport()
{
ini_set('memory_limit', -1);
ini_set('max_execution_time', '60');
$data = I('post.');
$searchForm = $data['searchForm'];
unset($data['searchForm']);
$data['operator_type'] = 1;
$data['user_type'] = 3;
$data['merchant_id'] = $this->merchant_id;
$data['platform_type'] = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
$data['searchText'] = $searchForm['keyword'];
$data['keyword_type'] = $searchForm['keyword_type'];
$data['pricing_method'] = $searchForm['can_exchange_weigh'];
$data['condition_name'] = $searchForm['condition_name'];
$data['order_type'] = $searchForm['order_type'];
$data['person1'] = $searchForm['person1'];
$data['person2'] = $searchForm['person2'];
$data['person3'] = $searchForm['person3'];
$data['status'] = $searchForm['status'];
$GoodsClass = new Goods();
$result = $GoodsClass->exportGoods($data);
$this->ajaxReturn($result);
}
//下载文件
public function exportFile()
{
$attribute_state = I('get.attribute_state');
if ($attribute_state == 2) {
$file_name = '规格商品导出';
} else {
$file_name = '非规格商品导出';
}
$DownFileClass = new DownFile();
$DownFileClass->downFile([
'file_path' => I('get.file_path'),
'file_name' => $file_name,
'file_type' => '.xls'
]);
}
//导出方法
private function getExcel($expTitle, $expCellName, $expTableData, $str = "")
{
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
$fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$common = $str;
vendor("PHPExcel.PHPExcel");
ob_clean();
$objPHPExcel = new \PHPExcel();
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1'); //合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $common);
// $objPHPExcel->setActiveSheetIndex(1)->setCellValue('A1', $str);
for ($i = 0; $i < $cellNum; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
}
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), ' ' . $expTableData[$i][$expCellName[$j][0]]);
}
}
foreach ($cellName as $value) {
$objPHPExcel->getActiveSheet()->getColumnDimension($value)->setWidth(20);
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit();
}
// 批量删除
public function batch_del()
{
if (IS_POST) {
$del_data = I('post.del_data');
if (!empty($del_data)) {
$Mgoods = M('retail_mgoods');
foreach ($del_data as $key => &$val) {
$del_res = $Mgoods->where(array('id' => $val['id']))->setField('status', 2);
if (empty($del_res)) {
$res['status'] = 0;
$res['msg'] = '有商品删除失败,请重新提交!';
$this->ajaxReturn($res);
}
if ($val['attribute'] == 2) {
//规格商品状态修改
M('retail_sku_mgoods')->where(array('mgoods_id' => $val['id']))->setField('state', 1);
}
$success = $this->updateSaleGoods($val['id'], $sku_id = "", 2);
}
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
$desc = '商品档案-批量删除商品'; //操作行为描述
action_log($source, $type, $request_data, $desc);
$res['status'] = 1;
$res['msg'] = '删除成功!';
} else {
$res['status'] = 0;
$res['msg'] = '无删除数据';
}
$this->ajaxReturn($res);
}
}
/*
* 批量更换商品分类
* */
public function move_class()
{
$user = session('ls_userinfo');
//查询出该门店可用分类
$merchantid = $user['merchantid'];
$class_name = M('retail_goods_class')->where(array('merchantid' => $merchantid, 'status' => 0, 'type' => 1))->order('code asc')->select();
$this->assign('class_name', $class_name);
$this->display();
}
public function goodsMoveList()
{
$user = session('ls_userinfo');
//接收查询条件
$keyword = I('get.keyword');
if (!empty($keyword)) {
$keyword = trim($keyword, ' ');
$where['g.goods_name|g.good_number|g.goods_code'] = array('like', '%' . $keyword . '%');
}
//按照商品类别搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$where['g.goods_class_id'] = $person3;
} else if (!empty($person2)) {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $person2, 'c3.status' => 0))
->field('c3.id')->select();
$flag = 1;
} else if (!empty($person1)) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid AND c3.status=0', 'LEFT')
->where(array('c1.id' => $person1, 'c2.status' => 0))
->field('c2.id,c3.id as c3_id')->select();
$flag = 1;
}
if (!empty($class)) {
$arr = array();
foreach ($class as &$val) {
if (!in_array($val['id'], $arr)) {
$arr[] = $val['id'];
}
if (!empty($val['c3_id'])) {
if (!in_array($val['c3_id'], $arr)) {
$arr[] = $val['c3_id'];
}
}
}
$str = implode(',', $arr);
$where['g.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['g.goods_class_id'] = $person2 ? $person2 : $person1;
}
$where['g.merchantid'] = $user['merchantid'];
$where['g.status'] = array('in', '0,1');
$goods_count = M('retail_mgoods as g')->where($where)->count();
$goods = M('retail_mgoods as g')
->join('ls_retail_goods_class as c on c.id=g.goods_class_id')
->where($where)->page($_GET['page'], $_GET['limit'])
->order('g.good_number desc')
->field('g.goods_name,g.good_number,g.goods_code,g.id,c.class_name,c.id as c_id,c.pid,c.type,c.class_name')
->select();
foreach ($goods as &$val) {
if ($val['type'] == 2) {
$class1 = M('retail_goods_class')->where(array('id' => $val['pid']))->getField('class_name');
$val['class_name'] = $class1 . '/' . $val['class_name'];
} else if ($val['type'] == 3) {
$class1 = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c2.pid=c1.id')
->where(array('c2.id' => $val['pid']))
->field('c1.class_name,c2.class_name as c2_class_name')
->find();
$val['class_name'] = $class1['class_name'] . '/' . $class1['c2_class_name'] . '/' . $val['class_name'];
}
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['count'] = $goods_count;
$rt['data'] = $goods;
$this->ajaxReturn($rt);
}
//批量修改商品分类
public function moveGoodsClass()
{
$user = session('ls_userinfo');
$goodsIds = I('post.goodsIds');
$class_id = I('post.class_id');
if (empty($goodsIds) || empty($class_id)) {
$res['status'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$class = M('retail_goods_class as c')
->join('ls_retail_goods_class as c1 on c1.pid=c.id AND c1.status=0', 'LEFT')
->where(array('c.id' => $class_id, 'c.status' => 0))
->field('c.*,c1.id as c1_id')
->select();
if (empty($class)) {
$res['status'] = 0;
$res['msg'] = '分类不存在';
} else if (!empty($class['c1_id'])) {
$res['status'] = 0;
$res['msg'] = '请选择到底级分类';
} else {
$update = M('retail_mgoods')->where(array('id' => array('in', $goodsIds), 'merchantid' => $user['merchantid']))->save(array('goods_class_id' => $class_id));
if ($update) {
//供给仓分类修改
$this->moveSaleClass($goodsIds, $class_id);
}
$updateGoods = M('retail_goods')->where(array('m_goods_id' => array('in', $goodsIds), 'sync_state' => 1))->save(array('sync_state' => 0));
if ($update) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
$desc = '商品档案-批量修改分类'; //操作行为描述
action_log($source, $type, $request_data, $desc);
$res['status'] = 1;
$res['msg'] = '修改成功';
} else {
$res['status'] = 0;
$res['msg'] = '修改失败请重试';
}
}
$this->ajaxReturn($res);
}
/**
* 供给仓批量修改分类
* @param $goodsIds 商品id
* @param $class_id 分类id
*
*/
protected function moveSaleClass($goodsIds, $class_id)
{
$merchant_id = session("merchant_userinfo")['id'];
if (!empty($goodsIds) && !empty($class_id)) {
$where = array(
'merchant_id' => $merchant_id,
'goods_id' => array('in', $goodsIds),
);
$data = array(
'goods_class_id' => $class_id
);
$save = D("SaleMGoods")->updateData($where, $data);
}
return $save;
}
/* 批量修改 */
public function updateMGoods($id)
{
$ids = trim($id, ',');
$this->assign('ids', $ids);
$this->display();
}
//修改的商品信息
public function edit_getPrice()
{
$ids = I('post.ids');
$id = explode(",", $ids);
$data = array();
foreach ($id as $k => $v) {
$data[$k] = M("retail_mgoods")
->where(array('id' => $v))
->field('id,good_number,goods_name,goods_code,purch_price,retail_price,online_price,goods_num,can_exchange_weigh,shop_mem_status,shop_mem_price')
->find();
if (intval($data[$k]['goods_num']) == $data[$k]['goods_num']) { // 非kg商品
$data[$k]['goods_num'] = intval($data[$k]['goods_num']);
}
$data[$k]['shop_mem_price'] = $data[$k]['shop_mem_status'] == 1 ? $data[$k]['shop_mem_price'] : '未开启商城会员价';
}
$this->ajaxReturn(array('code' => 0, 'data' => $data));
}
//执行批量修改商品价格
public function edit_Price()
{
$user = session('ls_userinfo');
$data = I('post.list');
$good_numbereError = array();
$goods_codesError = array();
foreach ($data as $k => $v) {
//判断货号是否存在
$good_number = M('retail_mgoods')->where(array('merchantid' => $user['merchantid'], 'good_number' => $v['good_number'], 'id' => array('neq', $v['id'])))->find();
if (!empty($good_number)) {
$res['status'] = 0;
$res['msg'] = "第" . ($k + 1) . "条《货号》(" . $v['good_number'] . ")已存在,修改失败;<br/>";
$this->ajaxReturn($res);
}
//判断商品条码是否存在
if ($v['goods_code'] != '') {
$goods_code = M('retail_mgoods')->where(array('merchantid' => $user['merchantid'], 'goods_code' => $v['goods_code'], 'id' => array('neq', $v['id'])))->find();
if (!empty($goods_code)) {
$res['status'] = 0;
$res['msg'] = "第" . ($k + 1) . "条《商品条码》(" . $v['good_number'] . ")已存在,修改失败;<br/>";
$this->ajaxReturn($res);
}
}
if ($good_numbereError[$k] == '' && $goods_codesError[$k] == '') {
$id = $v['id'];
$list['good_number'] = $v['good_number'];
$list['goods_name'] = $v['goods_name'];
$list['goods_code'] = $v['goods_code'];
$list['purch_price'] = $v['purch_price'];
$list['retail_price'] = $v['retail_price'];
$list['online_price'] = $v['online_price'];
$list['goods_num'] = $v['goods_num'];
$list['can_exchange_weigh'] = $v['can_exchange_weigh'];
$list['shop_mem_price'] = $v['shop_mem_status'] == 1 ? $v['shop_mem_price'] : null;
$mgoods = M('retail_mgoods')->where(array('id' => $id))->find();
if ($mgoods['good_number'] != $list['good_number'] || $mgoods['goods_name'] != $list['goods_name'] || $mgoods['goods_code'] != $list['goods_code'] || $mgoods['purch_price'] != $list['purch_price'] || $mgoods['online_price'] != $list['online_price'] || $mgoods['retail_price'] != $list['retail_price'] || $mgoods['goods_num'] != $list['goods_num'] || $mgoods['shop_mem_price'] != $list['shop_mem_price'] || $mgoods['can_exchange_weigh'] != $list['can_exchange_weigh']) {
$edit = M("retail_mgoods")->where(array('id' => $id))->save($list);
if ($edit) {
$edit_Success[$k] = "修改成功";
$mgoods_id[$k] = $id;
} else {
$edit_Error[$k] = "第" . ($k + 1) . "条商品信息修改失败;<br/>";
}
}
}
}
if (!empty($edit_Success)) {
$edits = M('retail_goods')->where(array('mgoodsid' => array('in', $mgoods_id)))->save(array('sync_two' => 0));
}
if (empty($good_numbereError) && empty($goods_codesError) && empty($edit_Error) || !empty($edit_Success)) {
$res['status'] = 1;
$res['msg'] = "修改成功";
} elseif (!empty($good_numbereError) || !empty($goods_codesError) || !empty($edit_Error)) {
$numbereError = "";
foreach ($good_numbereError as $k => $v) {
$numbereError .= $v;
}
$codesError = "";
foreach ($goods_codesError as $k => $v) {
$codesError .= $v;
}
$priceError = "";
foreach ($edit_Error as $k => $v) {
$priceError .= $v;
}
$res['status'] = 0;
$res['msg'] = $numbereError . $codesError . $priceError;
} else {
$res['status'] = 2;
$res['msg'] = "商品信息未修改";
}
$this->ajaxReturn($res);
}
//删除轮播图
public function delMGoodsBanner()
{
$user = session('ls_userinfo');
$id = I('post.id');
$gid = I('post.gid');
if (empty($id) || empty($gid) || empty($user)) {
$res['status'] = 0;
$res['msg'] = "缺少参数";
$this->ajaxReturn($res);
}
$map['merchantid'] = $user['merhcantid'];
$map['id'] = $id;
$map['mgoods_id'] = $gid;
$banner = M('retail_mgoods_banner')->where($map)->find();
if (empty($banner)) {
$res['status'] = 0;
$res['msg'] = "轮播图有误";
} else {
$del = M('retail_mgoods_banner')->where($map)->delete();
if ($del) {
$res['status'] = 1;
$res['msg'] = "删除成功";
} else {
$res['status'] = 0;
$res['msg'] = "服务器出差了";
}
}
$this->ajaxReturn($res);
}
public function getAutoGoodsCode()
{
$merchantid = session('ls_userinfo')['merchantid'];
$code = '';
while (1) {
$code = rand(1000000000, 9999999999);
$map = array('merhcantid' => $merchantid, 'status' => array('in', '0,1'), 'goods_code' => $code);
$old1 = M('retail_mgoods')->where($map)->find();
if (!empty($old1)) {
continue;
}
$map = array('merhcant_id' => $merchantid, 'status' => array('in', '0,1'), 'goods_code' => $code);
$old2 = M('retail_goods')->where($map)->find();
if (!empty($old2)) {
continue;
}
$map1 = array('merhcantid' => $merchantid, 'state' => 0, 'sku_code' => $code);
$old3 = M('retail_sku_mgoods')->where($map1)->find();
if (!empty($old3)) {
continue;
}
$map1 = array('merhcant_id' => $merchantid, 'state' => 0, 'sku_code' => $code);
$old4 = M('retail_sku_goods')->where($map1)->find();
if (!empty($old4)) {
continue;
}
break;
}
$res['status'] = 1;
$res['msg'] = '获取成功';
$res['code'] = $code;
$this->ajaxReturn($res);
}
/* 获取批量修改的商品
* @ids 商品id
* @return Array
* 新版2019-7-15
* */
public function get_update_mgoods_list()
{
$user = I('session.ls_userinfo');
$ids = I('post.ids');
if (!$ids) {
$res['status'] = 0;
$res['msg'] = '参数缺失';
$this->ajaxReturn($res);
}
$map['id'] = array('in', $ids);
$map['merchantid'] = $user['merchantid'];
$mgoods_list = M('retail_mgoods')->field('id,goods_name,good_number,goods_code,purch_price,retail_price,online_price,store_mem_status,store_mem_price,shop_mem_status,shop_mem_price,can_exchange_weigh,attribute,plus_price,plus_mem_status')->where($map)->select();
if (!$mgoods_list) {
$res['status'] = 0;
$res['msg'] = '未查询到修改数据';
$this->ajaxReturn($res);
}
foreach ($mgoods_list as &$val) {
$where = array();
$where['merchant_id'] = $user['merchantid'];
$where['goods_id'] = $val['id'];
$where['status'] = array('in', '0,1');
if ($val['attribute'] == 2) {
$val['purch_price'] = "规格成本价";
$goods_num = D('SaleMGoods')->getSum($where, "goods_num");
} else {
$val['purch_price'] = floatval($val['purch_price']);
$goods_num = D('SaleMGoods')->getFindField($where, "goods_num");
}
$goods_num = $goods_num ? $goods_num : 0;
$val['goods_num'] = round($goods_num, 3);
$val['retail_price'] = floatval($val['retail_price']);
$val['online_price'] = floatval($val['online_price']);
$val['store_mem_price'] = floatval($val['store_mem_price']);
$val['shop_mem_price'] = floatval($val['shop_mem_price']);
$val['plus_price'] = floatval($val['plus_price']);
$val['attribute_name'] = $val['attribute'] == 1 ? '无规格' : '有规格';
}
$res['status'] = 1;
$res['mgoods_list'] = $mgoods_list;
$this->ajaxReturn($res);
}
/* 批量修改商品数据
* @goods_list Array
* @return Array
* */
public function batch_save_mgoods()
{
$merchant_id = session("merchant_userinfo")['id'];
$mgoods_list = I('post.mgoods_list');
if (!$mgoods_list) {
$res['status'] = 0;
$res['msg'] = '参数缺失';
$this->ajaxReturn($res);
}
$msg = '';
$msg_code = '';
$msg_goods_num = '';
$msg_sku_weigh = '';
$numberArr = array();
$codeArr = array();
$reg1 = "/^\+?[0-9][0-9]*$/";
$reg2 = "/^[0-9]+(.[0-9]{1,3})?$/";
foreach ($mgoods_list as $key => &$val) {
$find = M('retail_mgoods')->where(array('good_number' => $val['good_number'], 'merchantid' => $merchant_id, 'status' => array('neq', 2), 'id' => array('neq', $val['id'])))->find();
if ($find || in_array($val['good_number'], $numberArr)) {
$msg .= ($key + 1) . ',';
} else {
$numberArr[] = $val['good_number'];
}
unset($find);
if (!empty($val['goods_code'])) {
$find = M('retail_mgoods')->where(array('goods_code' => $val['goods_code'], 'merchantid' => $merchant_id, 'status' => array('neq', 2), 'id' => array('neq', $val['id'])))->find();
if ($find || in_array($val['goods_code'], $codeArr)) {
$msg_code .= ($key + 1) . ',';
} else {
$codeArr[] = $val['goods_code'];
}
unset($find);
}
if ($val['can_exchange_weigh'] == 2 && !preg_match($reg2, abs($val['goods_num']))) {
$msg_goods_num .= ($key + 1) . ',';
} else if ($val['can_exchange_weigh'] == 1 && !preg_match($reg1, abs($val['goods_num']))) {
$msg_goods_num .= ($key + 1) . ',';
}
if ($val['attribute'] == 2 && $val['can_exchange_weigh'] == 2) {
$msg_sku_weigh .= ($key + 1) . ',';
}
}
if (trim($msg, ',')) {
$res['status'] = 0;
$res['msg'] = '第' . trim($msg, ',') . '条商品货号已存在';
$this->ajaxReturn($res);
}
if (trim($msg_code, ',')) {
$res['status'] = 0;
$res['msg'] = '第' . trim($msg_code, ',') . '条商品条码已存在';
$this->ajaxReturn($res);
}
if (!empty($msg_goods_num)) {
$res['status'] = 0;
$res['msg'] = '第' . trim($msg_goods_num, ',') . '条商品库存与称重属性不符';
$this->ajaxReturn($res);
}
if (!empty($msg_sku_weigh)) {
$res['status'] = 0;
$res['msg'] = '第' . trim($msg_sku_weigh, ',') . '条商品规格与称重不能同时开启';
$this->ajaxReturn($res);
}
$result = $this->batch_update_mgoods($mgoods_list);
if ($result['status'] == 1) {
//操作日志记录
$source = 0; //请求来源: 0pc系统 1小助手
$type = 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST); //请求参数 json数据
$desc = '商品档案-批量修改商品'; //操作行为描述
action_log($source, $type, $request_data, $desc);
$res['status'] = 1;
$res['msg'] = $result['msg'];
} else {
$res['status'] = 0;
$res['msg'] = $result['msg'];
}
$this->ajaxReturn($res);
}
/* 批量修改商品数据7-12
* @goods_list Array
* @return Array
* */
protected function batch_update_mgoods($mgoods_list)
{
$record = 0;
foreach ($mgoods_list as $key => &$val) {
$find = M('retail_mgoods')->where(array('id' => $val['id']))->find();
if (!$find) {
$res['status'] = 0;
$res['msg'] = '第' . ($key + 1) . '条查询失败';
return $res;
}
if ($val['goods_name'] != $find['goods_name']) {
$up['goods_name'] = $val['goods_name'];
$content['goods_name'] = $val['goods_name'];
}
if ($val['good_number'] != $find['good_number']) {
$up['good_number'] = $val['good_number'];
$content['goods_number'] = $val['good_number'];
}
if ($val['goods_code'] != $find['goods_code']) {
$up['goods_code'] = $val['goods_code'];
$content['goods_code'] = $val['goods_code'];
}
if ($val['can_exchange_weigh'] != $find['can_exchange_weigh']) {
$up['can_exchange_weigh'] = $val['can_exchange_weigh'];
$content['pricing_method'] = $val['can_exchange_weigh'];
}
if ($val['retail_price'] != $find['retail_price']) {
$up['retail_price'] = round($val['retail_price'], 2);
}
if ($val['online_price'] != $find['online_price']) {
$up['online_price'] = round($val['online_price'], 2);
}
if ($val['store_mem_status'] != $find['store_mem_status']) {
$up['store_mem_status'] = $val['store_mem_status'];
}
if ($val['store_mem_price'] != $find['store_mem_price']) {
$up['store_mem_price'] = round($val['store_mem_price'], 2);
}
if ($val['shop_mem_status'] != $find['shop_mem_status']) {
$up['shop_mem_status'] = $val['shop_mem_status'];
}
if ($val['shop_mem_price'] != $find['shop_mem_price']) {
$up['shop_mem_price'] = round($val['shop_mem_price'], 2);
}
if ($val['plus_mem_status'] != $find['plus_mem_status']) {
$up['plus_mem_status'] = $val['plus_mem_status'];
}
if ($val['plus_price'] != $find['plus_price']) {
$up['plus_price'] = $val['plus_price'];
}
if ($content) {
$where = array();
$where['goods_id'] = $val['id'];
$where['status'] = array('in', '0,1');
D('SaleMGoods')->updateData($where, $content);
unset($where['status']);
$where['type'] = 5; //初始化入库
D('WarehousingGoods')->updateData($where, $content);
unset($content);
}
if ($up) {
$record += 1;
$save = M('retail_mgoods')->where(array('id' => $val['id']))->save($up);
if (!$save) {
$res['status'] = 0;
$res['msg'] = '第' . ($key + 1) . '条修改失败';
return $res;
}
unset($save);
unset($up);
}
$this->sync_two($find['id']);
unset($find);
}
$res['status'] = 1;
$res['msg'] = $record > 0 ? '修改成功' : '请先修改数据再提交';
return $res;
}
/*
* **************************************************库存模块***************************************
*/
public function warning()
{
$merchantid = session('ls_userinfo')['merchantid'];
$where['merchantid'] = $merchantid;
$where['status'] = 0;
$where['type'] = 1;
$class_name = M('retail_goods_class')->where($where)->order('code asc')->select();
$this->assign('class_name', $class_name);
$this->display();
}
public function warning_list()
{
$merchantid = session("ls_userinfo")['merchantid'];
$p = I('get.page');
$l = I('get.limit');
$good_number = I('get.good_number');
//按照商品类别搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
if (!empty($good_number)) {
$where['m.good_number|m.goods_name'] = $good_number;
}
$flag = 0;
if (!empty($person3)) {
$where['m.goods_class_id'] = $person3;
} else if (!empty($person2)) {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $person2, 'c3.status' => 0))
->field('c3.id')->select();
$flag = 1;
} else if (!empty($person1)) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid AND c3.status=0', 'LEFT')
->where(array('c1.id' => $person1, 'c2.status' => 0))
->field('c2.id,c3.id as c3_id')->select();
$flag = 1;
}
if (!empty($class)) {
$arr = array();
foreach ($class as &$val) {
if (!in_array($val['id'], $arr)) {
$arr[] = $val['id'];
}
if (!empty($val['c3_id'])) {
if (!in_array($val['c3_id'], $arr)) {
$arr[] = $val['c3_id'];
}
}
}
$str = implode(',', $arr);
$where['m.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['m.goods_class_id'] = $person2 ? $person2 : $person1;
}
$where['m.merchantid'] = $merchantid;
$where['m.status'] = array('in', '0,1');
$mgoods_info = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num < m.less_num')
->page($p, $l)
->order('m.status asc')
->select();
$count = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num < m.less_num')
->count();
if ($mgoods_info) {
foreach ($mgoods_info as $key => &$val) {
$val['index'] = $key + 1;
if ($val['status'] == 0) {
$val['status_name'] = '正常';
} else if ($val['status'] == 1) {
$val['status_name'] = '下架';
}
if ($val['attribute'] == 2) {
$val['average_purch_price'] = '规格价';
$val['purch_price'] = '规格价';
$val['retail_price'] = '规格价';
}
$val['cate_name'] = M('retail_goods_class')->where(array('id' => $val['goods_class_id']))->getField('class_name');
}
}
$res['status'] = 1;
$res['code'] = 0;
$res['msg'] = '';
$res['data'] = $mgoods_info;
$this->ajaxReturn($res);
}
public function warning_list_export()
{
$merchantid = session("ls_userinfo")['merchantid'];
$good_number = I('get.good_number');
//按照商品类别搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
if (!empty($good_number)) {
$where['m.good_number|m.goods_name'] = $good_number;
}
$flag = 0;
if (!empty($person3)) {
$where['m.goods_class_id'] = $person3;
} else if (!empty($person2)) {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $person2, 'c3.status' => 0))
->field('c3.id')->select();
$flag = 1;
} else if (!empty($person1)) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid AND c3.status=0', 'LEFT')
->where(array('c1.id' => $person1, 'c2.status' => 0))
->field('c2.id,c3.id as c3_id')->select();
$flag = 1;
}
if (!empty($class)) {
$arr = array();
foreach ($class as &$val) {
if (!in_array($val['id'], $arr)) {
$arr[] = $val['id'];
}
if (!empty($val['c3_id'])) {
if (!in_array($val['c3_id'], $arr)) {
$arr[] = $val['c3_id'];
}
}
}
$str = implode(',', $arr);
$where['m.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['m.goods_class_id'] = $person2 ? $person2 : $person1;
}
$where['m.merchantid'] = $merchantid;
$where['m.status'] = array('in', '0,1');
$mgoods_info = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num < m.less_num')
->order('m.status asc')
->select();
if ($mgoods_info) {
foreach ($mgoods_info as $key => &$val) {
$val['index'] = $key + 1;
if ($val['status'] == 0) {
$val['status_name'] = '正常';
} else if ($val['status'] == 1) {
$val['status_name'] = '下架';
}
if ($val['attribute'] == 2) {
$val['average_purch_price'] = '规格价';
$val['purch_price'] = '规格价';
$val['retail_price'] = '规格价';
}
$val['cate_name'] = M('retail_goods_class')->where(array('id' => $val['goods_class_id']))->getField('class_name');
}
}
$filename = '预警商品';
$arr = array(
array('good_number', '货号'),
array('goods_name', '品名'),
array('goods_code', '商品条码'),
array('average_purch_price', '成本价'),
array('purch_price', '当前进价(元)'),
array('retail_price', '零售价(元)'),
array('online_price', '商城价(元)'),
array('status_name', '状态'),
array('cate_name', '类别'),
array('goods_num', '库存'),
array('less_num', '预警值'),
);
$str = $filename . '导出时间' . date('Y-m-d', time());
$this->getExcel($filename, $arr, $mgoods_info, $str);
}
//超量
public function excess()
{
$merchantid = session('ls_userinfo')['merchantid'];
$where['merchantid'] = $merchantid;
$where['status'] = 0;
$where['type'] = 1;
$class_name = M('retail_goods_class')->where($where)->order('code asc')->select();
$this->assign('class_name', $class_name);
$this->display();
}
public function excess_list()
{
$merchantid = session("ls_userinfo")['merchantid'];
$good_number = I('get.good_number');
//按照商品类别搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
if (!empty($good_number)) {
$where['m.good_number|m.goods_name'] = $good_number;
}
$flag = 0;
if (!empty($person3)) {
$where['m.goods_class_id'] = $person3;
} else if (!empty($person2)) {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $person2, 'c3.status' => 0))
->field('c3.id')->select();
$flag = 1;
} else if (!empty($person1)) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid AND c3.status=0', 'LEFT')
->where(array('c1.id' => $person1, 'c2.status' => 0))
->field('c2.id,c3.id as c3_id')->select();
$flag = 1;
}
if (!empty($class)) {
$arr = array();
foreach ($class as &$val) {
if (!in_array($val['id'], $arr)) {
$arr[] = $val['id'];
}
if (!empty($val['c3_id'])) {
if (!in_array($val['c3_id'], $arr)) {
$arr[] = $val['c3_id'];
}
}
}
$str = implode(',', $arr);
$where['m.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['m.goods_class_id'] = $person2 ? $person2 : $person1;
}
$where['m.merchantid'] = $merchantid;
$where['m.status'] = array('in', '0,1');
$mgoods_info = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num > m.more_num')
->order('m.status asc')
->select();
$count = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num > m.more_num')
->count();
if ($mgoods_info) {
foreach ($mgoods_info as $key => &$val) {
$val['index'] = $key + 1;
if ($val['status'] == 0) {
$val['status_name'] = '正常';
} else if ($val['status'] == 1) {
$val['status_name'] = '下架';
}
if ($val['attribute'] == 2) {
$val['average_purch_price'] = '规格价';
$val['purch_price'] = '规格价';
$val['retail_price'] = '规格价';
}
$val['cate_name'] = M('retail_goods_class')->where(array('id' => $val['goods_class_id']))->getField('class_name');
}
}
$res['code'] = 0;
$res['msg'] = '';
$res['count'] = $count;
$res['data'] = $mgoods_info;
$this->ajaxReturn($res);
}
public function excess_list_export()
{
$merchantid = session("ls_userinfo")['merchantid'];
$good_number = I('get.good_number');
//按照商品类别搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
if (!empty($good_number)) {
$where['m.good_number|m.goods_name'] = $good_number;
}
$flag = 0;
if (!empty($person3)) {
$where['m.goods_class_id'] = $person3;
} else if (!empty($person2)) {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $person2, 'c3.status' => 0))
->field('c3.id')->select();
$flag = 1;
} else if (!empty($person1)) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid AND c3.status=0', 'LEFT')
->where(array('c1.id' => $person1, 'c2.status' => 0))
->field('c2.id,c3.id as c3_id')->select();
$flag = 1;
}
if (!empty($class)) {
$arr = array();
foreach ($class as &$val) {
if (!in_array($val['id'], $arr)) {
$arr[] = $val['id'];
}
if (!empty($val['c3_id'])) {
if (!in_array($val['c3_id'], $arr)) {
$arr[] = $val['c3_id'];
}
}
}
$str = implode(',', $arr);
$where['m.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['m.goods_class_id'] = $person2 ? $person2 : $person1;
}
$where['m.merchantid'] = $merchantid;
$where['m.status'] = array('in', '0,1');
$mgoods_info = M('retail_mgoods as m')
->join('ls_retail_mgoods as r on r.id=m.id', "LEFT")
->where($where)
->where('m.goods_num > m.more_num')
->order('m.status asc')
->select();
if ($mgoods_info) {
foreach ($mgoods_info as $key => &$val) {
$val['index'] = $key + 1;
if ($val['status'] == 0) {
$val['status_name'] = '正常';
} else if ($val['status'] == 1) {
$val['status_name'] = '下架';
}
if ($val['attribute'] == 2) {
$val['average_purch_price'] = '规格价';
$val['purch_price'] = '规格价';
$val['retail_price'] = '规格价';
}
$val['cate_name'] = M('retail_goods_class')->where(array('id' => $val['goods_class_id']))->getField('class_name');
}
}
$filename = '超量商品';
$arr = array(
array('index', '序号'),
array('good_number', '货号'),
array('goods_name', '品名'),
array('goods_code', '商品条码'),
array('average_purch_price', '成本价(元)'),
array('purch_price', '当前进价(元)'),
array('retail_price', '零售价(元)'),
array('online_price', '商城价(元)'),
array('status_name', '状态'),
array('cate_name', '类别'),
array('goods_num', '库存'),
array('more_num', '超量值'),
);
$str = $filename . '导出时间' . date('Y-m-d', time());
$this->getExcel($filename, $arr, $mgoods_info, $str);
}
/**
* 新增、编辑商品页面 获取商品详情
*/
public function goodsSet()
{
if (IS_POST) {
//获取商品详情
$OperateMGoodsApi = new OperateMGoods($this->merchant_id);
$data = $OperateMGoodsApi->getMGoodsDetails(array(
'goods_id' => I('post.id'),
));
$this->ajaxReturn($data);
} else {
//展示页面
$this->display();
}
}
/**
* 获取商品积分限制设置
*/
public function getEditGoodsBaseSet()
{
$OperateMGoodsApi = new OperateMGoods($this->merchant_id);
$config = $OperateMGoodsApi->getEditGoodsConfig();
$this->ajaxReturn(array(
'status' => 1,
'msg' => '获取成功',
'data' => $config,
));
}
/**
* 获取商品规格信息
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getGoodsSkuAttr()
{
$id = I('post.id');
$SkuAttribute = new SkuAttribute();
$param = array(
'merchant_id' => $this->merchant_id,
'goods_id' => $id,
);
$data = $SkuAttribute->getMerGoodsSkuInfo($param);
$this->ajaxReturn($data);
}
/**
* 保存商品新增、编辑
*/
public function doGoodsSet()
{
if (IS_POST) {
$user = session('jurisdiction_user');
$OperateGoodsApi = new OperateMGoods($this->merchant_id);
$item = array(
'merchant_id' => $this->merchant_id,
'user_type' => $user['login_type'],
'goodsInfo' => I('post.'),
'platform_type' => $this->role_info['platform_type']
);
$editGoods = $OperateGoodsApi->editMerchantGoods($item);
$this->ajaxReturn($editGoods);
}
}
/**
* 条码检测
*/
public function checkgoods()
{
if (IS_POST) {
$merchant = session('merchant_userinfo');
$code = I('post.code');
$goods = new Goods();
$result = $goods->basisCodeGetGoodsInfo($code, $merchant['partnerid']); //获取商品信息 1代表商户获取
if ($result['status'] == 1) {
$result = ['status' => 1, 'msg' => '查询成功', 'data' => $result['data']['goods']];
} else {
$result = ['status' => 0, 'msg' => '未找到该商品'];
}
$this->ajaxReturn($result);
}
}
/*
* 商户供给仓商品上架,下架,删除
* $status 0上架 1下架 2删除
*/
public function updateSaleGoods($id, $sku_id, $status)
{
$data = array();
$data['goods_id'] = $id;
if (!empty($sku_id)) {
$data['sku_id'] = $sku_id;
}
$content['status'] = $status;
$res = D('SaleMGoods')->updateData($data, $content);
if ($res['status'] == 0) {
$res['msg'] = '库存状况状态修改失败';
$this->ajaxReturn($res);
}
return $res;
}
/*商品回收站
*
*/
public function recycle()
{
$merchant_id = session("merchant_userinfo")['id'];
$class_name = M('retail_goods_class')->where(array('merchantid' => $merchant_id, 'status' => 0, 'type' => 1))->order('code asc')->select();
$this->assign('class_name', json_encode($class_name));
$this->display();
}
//简拼设置
public function simple()
{
$GoodsClassify = new GoodsClassify();
$result = $GoodsClassify->getGoodsClassTopLevel($this->merchant_id);
$this->assign('goodsClass1', json_encode($result['data']['list']));
$arr = array(
'updateGoods' => '/index.php/Retail/Mgoods/batchPinYin',
'generate' => '/index.php/Retail/Mgoods/generatePinYin',
);
$authArr = checkFunction($arr);
$this->assign('authArr', $authArr);
$this->display();
}
//简拼的商品列表
public function getSimpleGoods()
{
$data = I('post.');
$data['merchant_id'] = $this->merchant_id;
$GoodsClass = new Goods();
$result = $GoodsClass->getMerchantGoodsList($data);
$this->ajaxReturn($result);
}
//一键生成简拼
public function generatePinYin()
{
$GoodsClass = new Goods();
$result = $GoodsClass->generateSimpleSpelling([
'type' => 1,
'merchant_id' => $this->merchant_id,
'page' => I('post.page')
]);
$this->ajaxReturn($result);
}
//批量修改
public function batchPinYin()
{
$data = I('post.');
$data['type'] = 1;
$data['merchant_id'] = $this->merchant_id;
$GoodsClass = new Goods();
$result = $GoodsClass->batchSimpleSpelling($data);
$this->ajaxReturn($result);
}
/**
* 批量修改商品的一个字段
*/
public function batchOperateGoods()
{
if (IS_POST) {
$user = session('jurisdiction_user');
$OperateGoods = new OperateMGoods();
$result = $OperateGoods->batchOperateGoods([
'merchant_id' => $this->merchant_id,
'user_type' => $user['login_type'],
'operate_id' => $user['id'],
'key' => I('post.key'),
'value' => I('post.value'),
'goodsIds' => I('post.goodsIds'),
]);
$this->ajaxReturn($result);
}
}
}