1455 行
69 KiB
PHP
可执行文件
1455 行
69 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Retail\Controller;
|
||
|
||
use Common\Business\SerialNumberGoods;
|
||
use Common\Model\RetailGuideModel;
|
||
use Common\Model\RetailSellModel;
|
||
use Common\Model\RetailVipModel;
|
||
use Common\Order\BillOrder;
|
||
use Think\Controller;
|
||
|
||
//[1.9.20]
|
||
|
||
class ReportTableController extends BaseController
|
||
{
|
||
/**
|
||
* 门店商品库存统计(加载模板)
|
||
*/
|
||
public function stocks()
|
||
{
|
||
// 操作权限
|
||
$arr = array(
|
||
'stock_export' => '/index.php/Retail/ReportTable/stock_export',
|
||
'get_goods_list' => '/index.php/Retail/ReportTable/get_goods_list',
|
||
);
|
||
$authArr = checkFunction($arr);
|
||
$this->assign('authArr', $authArr);
|
||
$this->display();
|
||
}
|
||
|
||
/**
|
||
* 获取分类 品牌 供货商(公共方法)
|
||
* @return array $res
|
||
*/
|
||
public function get_seek_list()
|
||
{
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
// 商品分类
|
||
$class_data = M('retail_goods_class')->field('id,class_name')->where(array('pid' => 0, 'status' => 0, 'merchantid' => $user['merchantid'], 'type' => 1))->order('id asc')->select();
|
||
if ($class_data) {
|
||
foreach ($class_data as &$val) {
|
||
$children1 = M('retail_goods_class')->field('id,class_name')->where(array('pid' => $val['id'], 'status' => 0, 'merchantid' => $user['merchantid'], 'type' => 2))->order('id asc')->select();
|
||
if ($children1) {
|
||
foreach ($children1 as &$value) {
|
||
$children2 = M('retail_goods_class')->field('id,class_name')->where(array('pid' => $value['id'], 'status' => 0, 'merchantid' => $user['merchantid'], 'type' => 3))->order('id asc')->select();
|
||
$value['children'] = $children2;
|
||
}
|
||
}
|
||
$val['children'] = $children1;
|
||
}
|
||
}
|
||
// 供货商
|
||
// $res['supplier'] = M('retail_supplier')->field('id,supplier_name')->where(array('merchantid' => $user['merchantid'], 'status' => 1))->select();
|
||
// 品牌
|
||
$res['goods_brand'] = M('retail_goods_brand')->field('id,name')->where(array('merchantid' => $user['merchantid'], 'state' => 0))->select();
|
||
// 导购员
|
||
$res['guide'] = M('retail_guide')->field('id,guidename')->where(array('merchantid' => $user['merchantid'], 'storeid' => $storeid, 'status' => 1))->select();
|
||
$res['class_data'] = $class_data;
|
||
$this->ajaxReturn($res);
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取普通商品库存展示
|
||
* @param int $page 分页
|
||
* @param int $limit 每页数量
|
||
* @param array $seek_data 搜索条件
|
||
* @return array $res
|
||
*/
|
||
public function get_goods_list()
|
||
{
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
// 获取session
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
// 接收数据
|
||
$page = (int)I('post.page');
|
||
$limit = (int)I('post.limit');
|
||
$seek_data = I('post.seek_data');
|
||
// 分类搜索
|
||
$class_id = array_pop($seek_data['class_ids']);
|
||
if ($class_id) {
|
||
$class1 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class_id))->find();
|
||
if ($class1['pid'] == 0) { // 一级分类
|
||
$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' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id,c3.id as c3_id')
|
||
->select();
|
||
if ($class) {
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
if ($val['c3_id']) {
|
||
if (!in_array($val['c3_id'], $arr)) {
|
||
$arr[] = $val['c3_id'];
|
||
}
|
||
}
|
||
}
|
||
$map['g.goods_class_id'] = array('in', $arr);
|
||
} else {
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) { // 二级分类
|
||
$class = M('retail_goods_class as c1')
|
||
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
|
||
->where(array('c1.id' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id')
|
||
->select();
|
||
if ($class) {
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
}
|
||
$map['g.goods_class_id'] = array('in', $arr);
|
||
} else {
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
} else { // 三级分类
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
}
|
||
}
|
||
// 商品名称搜索
|
||
if (trim($seek_data['goods_name'], '')) {
|
||
$map['g.goods_name'] = array('like', '%' . $seek_data['goods_name'] . '%');
|
||
}
|
||
// 商品货号、条码搜索
|
||
if (trim($seek_data['keyword'], '')) {
|
||
$map['g.goods_number|g.goods_code'] = array('like', '%' . $seek_data['keyword'] . '%');
|
||
}
|
||
// // 供货商搜索
|
||
// if ($seek_data['supplier_id']) {
|
||
// $map['g.supplier_id'] = $seek_data['supplier_id'];
|
||
// }
|
||
// 品牌搜索
|
||
if ($seek_data['goods_brand_id']) {
|
||
$map['g.goods_brand_id'] = $seek_data['goods_brand_id'];
|
||
}
|
||
// 称重类型搜索
|
||
if ($seek_data['pricing_method']) {
|
||
$map['g.pricing_method'] = $seek_data['pricing_method'];
|
||
}
|
||
$map['g.store_id'] = $storeid;
|
||
$map['g.merchant_id'] = $user['merchantid'];
|
||
$map['g.status'] = array('in', "0,1");
|
||
$map['g.attribute_state'] = 1;
|
||
$goods_list = M('retail_goods as g')
|
||
->join('ls_retail_store_sale_goods as a on a.goods_id=g.id')
|
||
->field('g.id as goods_id,g.goods_class_id,g.goods_name,g.goods_number,g.goods_code,g.purchase_price,g.retail_price,online_price,g.company,g.pricing_method,g.source_type,g.shop_mem_status,g.shop_mem_price,g.store_mem_status,g.store_mem_price,g.goods_brand_id,g.supplier_id,g.serial_number_state,a.goods_num')
|
||
->where($map)
|
||
->page($page, $limit)
|
||
->order('g.id desc')
|
||
->select();
|
||
$total = M('retail_goods as g')
|
||
->join('ls_retail_store_sale_goods as a on a.goods_id=g.id')
|
||
->where($map)
|
||
->count();
|
||
foreach ($goods_list as &$val) {
|
||
// 获取商品分类
|
||
$class1 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $val['goods_class_id']))->find();
|
||
if ($class1['pid'] == 0) {
|
||
$val['class_name'] = $class1['class_name'];
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
} else {
|
||
$class3 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class2['pid']))->find();
|
||
$val['class_name'] = '/' . $class3['class_name'] . '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
}
|
||
}
|
||
// 处理货号
|
||
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '未设置';
|
||
// 进价合计
|
||
$val['all_purchase'] = floatval(round($val['goods_num'] * $val['purchase_price'], 2));
|
||
// 处理进价
|
||
$val['purchase_price'] = floatval($val['purchase_price']);
|
||
// 处理库存(加上单位)
|
||
$val['goods_num'] = $val['goods_num'] ? floatval($val['goods_num']) : 0;
|
||
$val['stock'] = floatval($val['goods_num']);
|
||
// 处理店内价
|
||
$val['retail_price'] = floatval($val['retail_price']);
|
||
// 处理商城价
|
||
$val['online_price'] = floatval($val['online_price']);
|
||
// 店内售出合计
|
||
$val['sell_retail_total'] = floatval(round($val['retail_price'] * $val['goods_num'], 2));
|
||
// 商城售出合计
|
||
$val['sell_online_total'] = floatval(round($val['online_price'] * $val['goods_num'], 2));
|
||
// 店内会员价
|
||
$val['store_mem_price'] = $val['store_mem_status'] == 1 && $val['store_mem_price'] > 0 ? $val['store_mem_price'] : '关闭';
|
||
// 商城会员价
|
||
$val['shop_mem_price'] = $val['shop_mem_status'] == 1 && $val['shop_mem_price'] > 0 ? $val['shop_mem_price'] : '关闭';
|
||
// 称重类型
|
||
$val['exchange_weigh_type'] = $val['pricing_method'] == 1 ? '非称重' : '称重';
|
||
// 供货商
|
||
if ($val['supplier_id']) {
|
||
$supplier_name = M('retail_supplier')->where(array('id' => $val['supplier_id'], 'merchantid' => $user['merchantid'], 'status' => 1))->getField('supplier_name');
|
||
$val['supplier_name'] = $supplier_name ? $supplier_name : '无';
|
||
} else {
|
||
$val['supplier_name'] = '无';
|
||
}
|
||
// 商品品牌
|
||
if ($val['goods_brand_id']) {
|
||
$goods_brand_name = M('retail_goods_brand')->where(array('id' => $val['goods_brand_id'], 'merchantid' => $user['merchantid'], 'state' => 0))->getField('name');
|
||
$val['goods_brand_name'] = $goods_brand_name ? $goods_brand_name : '无';
|
||
} else {
|
||
$val['goods_brand_name'] = '无';
|
||
}
|
||
// 商品来源
|
||
switch ($val['source_type']) {
|
||
case 1:
|
||
$val['source_name'] = '店长添加';
|
||
break;
|
||
case 2:
|
||
$val['source_name'] = '同步商户';
|
||
break;
|
||
case 3:
|
||
$val['source_name'] = '收银员添加';
|
||
break;
|
||
default:
|
||
$val['source_name'] = '未知来源';
|
||
}
|
||
// 判断权限
|
||
if ($show_purch_price && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['all_purchase'] = '***';
|
||
}
|
||
}
|
||
$res = $this->goods_statistics($map);
|
||
$res['status'] = $goods_list ? 1 : 0;
|
||
$res['goods_list'] = $goods_list;
|
||
$res['total'] = $total ? (int)$total : 0;
|
||
$this->ajaxReturn($res);
|
||
}
|
||
|
||
/**
|
||
* 统计普通商品数据
|
||
* @param array $map 搜索条件
|
||
* @return array $res
|
||
*/
|
||
protected function goods_statistics($map)
|
||
{
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$goods = M('retail_goods as g')
|
||
->join('ls_retail_store_sale_goods as a on a.goods_id=g.id')
|
||
->field('g.purchase_price,g.retail_price,g.online_price,g.pricing_method,a.goods_num')
|
||
->where($map)
|
||
->order('g.id desc')
|
||
->select();
|
||
$weigh_stock = 0; // 称重总库存
|
||
$not_weigh_stock = 0; // 非承重总库存
|
||
$purchase_price = 0; // 总进价
|
||
$retail_price_total = 0; // 店内售出合计
|
||
$online_price_total = 0; // 商城售出合计
|
||
foreach ($goods as &$val) {
|
||
if ($val['pricing_method'] == 1) { // 称重
|
||
$not_weigh_stock += $val['goods_num'];
|
||
} else { // 非称重
|
||
$weigh_stock += $val['goods_num'];
|
||
}
|
||
$purchase_price += $val['goods_num'] * $val['purchase_price'];
|
||
$retail_price_total += $val['goods_num'] * $val['retail_price'];
|
||
$online_price_total += $val['goods_num'] * $val['online_price'];
|
||
}
|
||
if (!$show_purch_price || $show_purch_price == 1) {
|
||
$res['purchase_price'] = $purchase_price ? floatval(round($purchase_price, 2)) : 0;
|
||
} else {
|
||
$res['purchase_price'] = '***';
|
||
}
|
||
$res['weigh_stock'] = $weigh_stock ? floatval($weigh_stock) : 0;
|
||
$res['not_weigh_stock'] = $not_weigh_stock ? floatval($not_weigh_stock) : 0;
|
||
$res['retail_price_total'] = $retail_price_total ? floatval(round($retail_price_total, 2)) : 0;
|
||
$res['online_price_total'] = $online_price_total ? floatval(round($online_price_total, 2)) : 0;
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 获取规格商品库存展示
|
||
* @param int $page 分页
|
||
* @param int $limit 每页数量
|
||
* @param array $seek_data 搜索条件
|
||
* @return array $res
|
||
*/
|
||
public function get_sku_list()
|
||
{
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
$page = (int)I('post.page');
|
||
$limit = (int)I('post.limit');
|
||
$seek_data = I('post.seek_data');
|
||
// 分类搜索
|
||
$class_id = array_pop($seek_data['class_ids']);
|
||
if ($class_id) {
|
||
$class1 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class_id))->find();
|
||
if ($class1['pid'] == 0) { // 一级分类
|
||
$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' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id,c3.id as c3_id')
|
||
->select();
|
||
if ($class) { // 一级分类有子分类
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
if ($val['c3_id']) {
|
||
if (!in_array($val['c3_id'], $arr)) {
|
||
$arr[] = $val['c3_id'];
|
||
}
|
||
}
|
||
}
|
||
$map['goods_class_id'] = array('in', $arr);
|
||
} else { // 一级分类无子分类
|
||
$map['goods_class_id'] = $class_id;
|
||
}
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) { // 二级分类
|
||
$class = M('retail_goods_class as c1')
|
||
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
|
||
->where(array('c1.id' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id')
|
||
->select();
|
||
if ($class) {
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
}
|
||
$map['goods_class_id'] = array('in', $arr);
|
||
} else {
|
||
$map['goods_class_id'] = $class_id;
|
||
}
|
||
} else { // 三级分类
|
||
$map['goods_class_id'] = $class_id;
|
||
}
|
||
}
|
||
}
|
||
// 商品名称搜索
|
||
if (trim($seek_data['goods_name'], '')) {
|
||
$map['goods_name'] = array('like', '%' . $seek_data['goods_name'] . '%');
|
||
}
|
||
// 商品货号、条码搜索
|
||
if (trim($seek_data['keyword'], '')) {
|
||
$map['goods_number|goods_code'] = array('like', '%' . $seek_data['keyword'] . '%');
|
||
}
|
||
// 供货商搜索
|
||
if ($seek_data['supplier_id']) {
|
||
$map['supplier_id'] = $seek_data['supplier_id'];
|
||
}
|
||
// 品牌搜索
|
||
if ($seek_data['goods_brand_id']) {
|
||
$map['goods_brand_id'] = $seek_data['goods_brand_id'];
|
||
}
|
||
// 称重类型搜索
|
||
if ($seek_data['pricing_method']) {
|
||
$map['pricing_method'] = $seek_data['pricing_method'];
|
||
}
|
||
$map['store_id'] = $storeid;
|
||
$map['merchant_id'] = $user['merchantid'];
|
||
$map['status'] = array('in', "0,1");
|
||
$map['attribute_state'] = 2;
|
||
$goods_ids = M('retail_goods')->where($map)->getField('id', true);
|
||
if (!$goods_ids) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无商品';
|
||
$this->ajaxReturn($res);
|
||
}
|
||
$where['sk.goods_id'] = array('in', $goods_ids);
|
||
$where['sk.store_id'] = $storeid;
|
||
$where['sk.status'] = 1;
|
||
$where['sk.state'] = 0;
|
||
$sku_list = M('retail_sku_goods as sk')
|
||
->join('ls_retail_store_sale_goods as a on a.sku_id=sk.id')
|
||
->join('ls_retail_goods as g on g.id=a.goods_id')
|
||
->field('sk.goods_id,sk.purchase_price,sk.sku_code as goods_code,sk.retail_price,sk.online_price,sk.sku_id_list,sk.shop_mem_price,sk.store_mem_price,g.goods_class_id,g.goods_name,g.goods_number,g.company,g.pricing_method,g.source_type,g.shop_mem_status,g.store_mem_status,g.goods_brand_id,g.supplier_id,a.goods_num')
|
||
->where($where)
|
||
->page($page, $limit)
|
||
->order('sk.id desc')
|
||
->select();
|
||
$total = M('retail_sku_goods as sk')
|
||
->join('ls_retail_store_sale_goods as a on a.sku_id=sk.id')
|
||
->join('ls_retail_goods as g on g.id=a.goods_id')
|
||
->where($where)->count();
|
||
$RSLModel = D('RetailSkuList');
|
||
foreach ($sku_list as &$val) {
|
||
// 获取商品分类
|
||
$class1 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $val['goods_class_id']))->find();
|
||
if ($class1['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class1['class_name'];
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
} else {
|
||
$class3 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class2['pid']))->find();
|
||
$val['class_name'] = '/' . $class3['class_name'] . '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
}
|
||
}
|
||
// 处理规格名称
|
||
$sku_name = $RSLModel->getFindField(array('id' => array('in', $val['sku_id_list'])), 'name', true);
|
||
$val['goods_name'] = $val['goods_name'] . '(' . implode(',', $sku_name) . ')';
|
||
// 处理货号
|
||
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '未设置';
|
||
// 进价合计
|
||
$val['all_purchase'] = round($val['goods_num'] * $val['purchase_price'], 2);
|
||
// 处理进价
|
||
$val['purchase_price'] = floatval($val['purchase_price']);
|
||
// 处理库存(加上单位)
|
||
$val['goods_num'] = $val['goods_num'] ? floatval($val['goods_num']) : 0;
|
||
$val['stock'] = $val['goods_num'] . $val['company'];
|
||
// 处理店内售价
|
||
$val['retail_price'] = floatval($val['retail_price']);
|
||
// 处理商城售价
|
||
$val['online_price'] = floatval($val['online_price']);
|
||
// 店内售出合计
|
||
$val['sell_retail_total'] = floatval(round($val['retail_price'] * $val['goods_num'], 2));
|
||
// 商城售出合计
|
||
$val['sell_online_total'] = floatval(round($val['online_price'] * $val['goods_num'], 2));
|
||
// 店内会员价
|
||
$val['store_mem_price'] = $val['store_mem_status'] == 1 && $val['store_mem_price'] > 0 ? floatval($val['store_mem_price']) : ' 关闭 ';
|
||
// 商城会员价
|
||
$val['shop_mem_price'] = $val['shop_mem_status'] == 1 && $val['shop_mem_price'] > 0 ? floatval($val['shop_mem_price']) : ' 关闭 ';
|
||
// 商品称重类型
|
||
$val['exchange_weigh_type'] = $val['pricing_method'] == 1 ? '非称重' : '称重';
|
||
// 供货商
|
||
if ($val['supplier_id']) {
|
||
$supplier_name = M('retail_supplier')->where(array('id' => $val['supplier_id'], 'merchantid' => $user['merchantid'], 'status' => 1))->getField('supplier_name');
|
||
$val['supplier_name'] = $supplier_name ? $supplier_name : '无';
|
||
} else {
|
||
$val['supplier_name'] = '无';
|
||
}
|
||
// 商品品牌
|
||
if ($val['goods_brand_id']) {
|
||
$goods_brand_name = M('retail_goods_brand')->where(array('id' => $val['goods_brand_id'], 'merchantid' => $user['merchantid'], 'state' => 0))->getField('name');
|
||
$val['goods_brand_name'] = $goods_brand_name ? $goods_brand_name : '无';
|
||
} else {
|
||
$val['goods_brand_name'] = '无';
|
||
}
|
||
// 商品来源
|
||
switch ($val['source_type']) {
|
||
case 1:
|
||
$val['source_name'] = '店长添加';
|
||
break;
|
||
case 2:
|
||
$val['source_name'] = '同步商户';
|
||
break;
|
||
case 3:
|
||
$val['source_name'] = '收银员添加';
|
||
break;
|
||
default:
|
||
$val['source_name'] = '未知来源';
|
||
}
|
||
/* 判断权限 */
|
||
if ($show_purch_price && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['all_purchase'] = '***';
|
||
}
|
||
}
|
||
$res = $this->sku_statistics($where);
|
||
$res['status'] = $sku_list ? 1 : 0;
|
||
$res['goods_list'] = $sku_list ? $sku_list : array();
|
||
$res['total'] = $total ? (int)$total : 0;
|
||
$this->ajaxReturn($res);
|
||
}
|
||
|
||
/**
|
||
* 统计规格商品数据
|
||
* @param array $map 搜索条件
|
||
* @return array $res
|
||
*/
|
||
protected function sku_statistics($where)
|
||
{
|
||
/* 查看进价权限 */
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$sku = M('retail_sku_goods as sk')
|
||
->join('ls_retail_store_sale_goods as a on a.sku_id=sk.id')
|
||
->field('sk.purchase_price,sk.retail_price,sk.online_price,a.pricing_method,a.goods_num')
|
||
->where($where)
|
||
->select();
|
||
$weigh_stock = 0; // 称重总库存
|
||
$not_weigh_stock = 0; // 非承重总库存
|
||
$purchase_price = 0; // 总进价
|
||
$retail_price_total = 0; // 店内售出合计
|
||
$online_price_total = 0; // 商城售出合计
|
||
foreach ($sku as &$val) {
|
||
if ($val['pricing_method'] == 1) { // 非称重
|
||
$not_weigh_stock += $val['goods_num'];
|
||
} else { // 称重
|
||
$weigh_stock += $val['goods_num'];
|
||
}
|
||
$purchase_price += $val['goods_num'] * $val['purchase_price'];
|
||
$retail_price_total += $val['goods_num'] * $val['retail_price'];
|
||
$online_price_total += $val['goods_num'] * $val['online_price'];
|
||
}
|
||
if (!$show_purch_price || $show_purch_price == 1) {
|
||
$res['purchase_price'] = $purchase_price ? round($purchase_price, 2) : 0;
|
||
} else {
|
||
$res['purchase_price'] = '***';
|
||
}
|
||
$res['weigh_stock'] = $weigh_stock ? floatval($weigh_stock) : 0;
|
||
$res['not_weigh_stock'] = $not_weigh_stock ? floatval($not_weigh_stock) : 0;
|
||
$res['retail_price_total'] = $retail_price_total ? floatval(round($retail_price_total, 2)) : 0;
|
||
$res['online_price_total'] = $online_price_total ? floatval(round($online_price_total, 2)) : 0;
|
||
return $res;
|
||
}
|
||
|
||
/* 库存统计导出
|
||
* */
|
||
public function stock_export()
|
||
{
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
$type = I('get.type'); // 类型
|
||
$seek_data = json_decode($_GET['seek_data'], true); // 搜索条件
|
||
/* 分类搜索 */
|
||
$class_id = array_pop($seek_data['class_ids']);
|
||
if ($class_id) {
|
||
$class1 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class_id))->find();
|
||
if ($class1['pid'] == 0) { // 一级分类
|
||
$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' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id,c3.id as c3_id')
|
||
->select();
|
||
if ($class) {
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
if ($val['c3_id']) {
|
||
if (!in_array($val['c3_id'], $arr)) {
|
||
$arr[] = $val['c3_id'];
|
||
}
|
||
}
|
||
}
|
||
$map['g.goods_class_id'] = array('in', $arr);
|
||
} else {
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) { // 二级分类
|
||
$class = M('retail_goods_class as c1')
|
||
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
|
||
->where(array('c1.id' => $class_id, 'c2.status' => 0))
|
||
->field('c2.id')
|
||
->select();
|
||
if ($class) {
|
||
$arr = array();
|
||
$arr[] = $class_id;
|
||
foreach ($class as &$val) {
|
||
if (!in_array($val['id'], $arr)) {
|
||
$arr[] = $val['id'];
|
||
}
|
||
}
|
||
$map['g.goods_class_id'] = array('in', $arr);
|
||
} else {
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
} else { // 三级分类
|
||
$map['g.goods_class_id'] = $class_id;
|
||
}
|
||
}
|
||
}
|
||
/* 商品名称搜索 */
|
||
if (trim($seek_data['goods_name'], '')) {
|
||
$map['g.goods_name'] = array('like', '%' . $seek_data['goods_name'] . '%');
|
||
}
|
||
// 商品货号、条码搜索
|
||
if (trim($seek_data['keyword'], '')) {
|
||
$map['g.goods_number|g.goods_code'] = array('like', '%' . $seek_data['keyword'] . '%');
|
||
}
|
||
// /* 供货商搜索 */
|
||
// if ($seek_data['supplier_id']) {
|
||
// $map['g.supplier_id'] = $seek_data['supplier_id'];
|
||
// }
|
||
/* 品牌搜索 */
|
||
if ($seek_data['goods_brand_id']) {
|
||
$map['g.goods_brand_id'] = $seek_data['goods_brand_id'];
|
||
}
|
||
/* 称重类型搜索 */
|
||
if ($seek_data['pricing_method']) {
|
||
$map['g.pricing_method'] = $seek_data['pricing_method'];
|
||
}
|
||
$map['g.store_id'] = $storeid;
|
||
$map['g.merchant_id'] = $user['merchantid'];
|
||
$map['g.status'] = array('in', "0,1");
|
||
if ($type == 1) {
|
||
$map['g.attribute_state'] = 1;
|
||
$result = $this->goods_export($map);
|
||
if ($result['status'] == 1) {
|
||
$store_list = $result['goods_list'];
|
||
}
|
||
} else {
|
||
$map['g.attribute_state'] = 2;
|
||
$result = $this->sku_export($map);
|
||
if ($result['status'] == 1) {
|
||
$store_list = $result['sku_list'];
|
||
}
|
||
}
|
||
$store_list = $store_list ? $store_list : array();
|
||
$name = $type == 1 ? '普通商品库存统计报表' : '规格商品库存统计报表';
|
||
$headArr = array(
|
||
array('index', '序号'),
|
||
array('goods_name', '商品名称'),
|
||
array('class_name', '商品分类'),
|
||
array('goods_number', '货号'),
|
||
array('goods_code', '条码'),
|
||
array('stock', '库存'),
|
||
array('purchase_price', '进价(单个)'),
|
||
array('all_purchase', '进价(合计)'),
|
||
array('retail_price', '售价(店内)'),
|
||
array('online_price', '售价(商城)'),
|
||
array('sell_retail_total', '预售合计(店内)'),
|
||
array('sell_online_total', '预售合计(商城)'),
|
||
array('store_mem_price', '会员价(店内)'),
|
||
array('shop_mem_price', '会员价(商城)'),
|
||
array('source_name', '渠道'),
|
||
array('pricing_method', '类型'),
|
||
// array('supplier_name', '供货商'),
|
||
array('goods_brand_name', '品牌')
|
||
);
|
||
$str = $name . '导出时间' . date('Y-m-d H:i:s', time());
|
||
$this->getExcel($name, $headArr, $store_list, $str);
|
||
}
|
||
|
||
/* 普通商品导出
|
||
* @return array()
|
||
* */
|
||
protected function goods_export($map)
|
||
{
|
||
/* 查看进价权限 */
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$goods_list = M('retail_goods as g')
|
||
->join('ls_retail_store_sale_goods as a on a.goods_id=g.id')
|
||
->field('g.id as goods_id,g.goods_class_id,g.goods_name,g.goods_number,g.goods_code,g.purchase_price,g.retail_price,g.online_price,g.company,g.pricing_method,g.shop_mem_status,g.shop_mem_price,g.store_mem_status,g.source_type,g.store_mem_price,g.goods_brand_id,g.supplier_id,a.goods_num')
|
||
->where($map)
|
||
->order('g.id desc')
|
||
->select();
|
||
if (!$goods_list) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无数据';
|
||
return $res;
|
||
}
|
||
foreach ($goods_list as $key => &$val) {
|
||
$val['index'] = $key + 1;
|
||
// 获取商品分类
|
||
$class1 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $val['goods_class_id']))->find();
|
||
if ($class1['pid'] == 0) {
|
||
$val['class_name'] = $class1['class_name'];
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
} else {
|
||
$class3 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class2['pid']))->find();
|
||
$val['class_name'] = '/' . $class3['class_name'] . '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
}
|
||
}
|
||
$val['all_purchase'] = strval(floatval(round($val['goods_num'] * $val['purchase_price'], 2)));
|
||
$val['goods_num'] = floatval($val['goods_num']);
|
||
$val['stock'] = $val['goods_num'] . $val['company'];
|
||
$val['purchase_price'] = strval(floatval($val['purchase_price']));
|
||
$val['retail_price'] = strval(floatval($val['retail_price']));
|
||
$val['online_price'] = strval(floatval($val['online_price']));
|
||
$val['sell_retail_total'] = strval(floatval(round($val['retail_price'] * $val['goods_num'], 2)));
|
||
$val['sell_online_total'] = strval(floatval(round($val['online_price'] * $val['goods_num'], 2)));
|
||
$val['shop_mem_price'] = $val['shop_mem_status'] == 1 && $val['shop_mem_price'] > 0 ? strval(floatval($val['shop_mem_price'])) : '关闭';
|
||
$val['store_mem_price'] = $val['store_mem_status'] == 1 && $val['store_mem_price'] > 0 ? strval(floatval($val['store_mem_price'])) : '关闭';
|
||
$val['pricing_method'] = $val['pricing_method'] == 1 ? '非称重' : '称重';
|
||
// 供货商
|
||
// if ($val['supplier_id']) {
|
||
// $supplier_name = M('retail_supplier')->where(array('id' => $val['supplier_id']))->getField('supplier_name');
|
||
// $val['supplier_name'] = $supplier_name ? $supplier_name : '无';
|
||
// } else {
|
||
// $val['supplier_name'] = '无';
|
||
// }
|
||
// 商品品牌
|
||
if ($val['goods_brand_id']) {
|
||
$goods_brand_name = M('retail_goods_brand')->where(array('id' => $val['goods_brand_id']))->getField('name');
|
||
$val['goods_brand_name'] = $goods_brand_name ? $goods_brand_name : '无';
|
||
} else {
|
||
$val['goods_brand_name'] = '无';
|
||
}
|
||
// 商品来源
|
||
switch ($val['source_type']) {
|
||
case 1:
|
||
$val['source_name'] = '店长添加';
|
||
break;
|
||
case 2:
|
||
$val['source_name'] = '同步商户';
|
||
break;
|
||
case 3:
|
||
$val['source_name'] = '收银员添加';
|
||
break;
|
||
default:
|
||
$val['source_name'] = '未知来源';
|
||
}
|
||
/* 判断权限 */
|
||
if ($show_purch_price && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['all_purch_price'] = '***';
|
||
}
|
||
}
|
||
$res['status'] = 1;
|
||
$res['goods_list'] = $goods_list;
|
||
return $res;
|
||
}
|
||
|
||
/* 普通商品导出
|
||
* @return array()
|
||
* */
|
||
protected function sku_export($map)
|
||
{
|
||
/* 查看进价权限 */
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$goods_ids = M('retail_goods as g')->field('g.id')->where($map)->select();
|
||
// 处理二位数组
|
||
$goods_ids = array_column($goods_ids, 'id');
|
||
if (empty($goods_ids)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无商品数据';
|
||
return $res;
|
||
}
|
||
$where['sk.goods_id'] = array('in', $goods_ids);
|
||
$where['sk.store_id'] = $map['g.store_id'];
|
||
$where['sk.status'] = 1;
|
||
$where['sk.state'] = 0;
|
||
$sku_list = M('retail_sku_goods as sk')
|
||
->join('ls_retail_store_sale_goods as a on a.sku_id=sk.id')
|
||
->join('ls_retail_goods as g on g.id=a.goods_id')
|
||
->field('sk.goods_id,sk.purchase_price,sk.sku_code as goods_code,sk.retail_price,sk.online_price,sk.sku_id_list,sk.shop_mem_price,sk.store_mem_price,g.goods_class_id,g.goods_name,g.goods_number,g.company,g.pricing_method,g.source_type,g.shop_mem_status,g.store_mem_status,g.goods_brand_id,g.supplier_id,a.goods_num')
|
||
->where($where)
|
||
->order('sk.id desc')
|
||
->select();
|
||
if (!$sku_list) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无规格商品';
|
||
return $res;
|
||
}
|
||
foreach ($sku_list as $key => &$val) {
|
||
$val['index'] = $key + 1;
|
||
$class1 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $val['goods_class_id']))->find();
|
||
if ($class1['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class1['class_name'];
|
||
} else {
|
||
$class2 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class1['pid']))->find();
|
||
if ($class2['pid'] == 0) {
|
||
$val['class_name'] = '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
} else {
|
||
$class3 = M('retail_goods_class')->field('id,class_name,pid')->where(array('id' => $class2['pid']))->find();
|
||
$val['class_name'] = '/' . $class3['class_name'] . '/' . $class2['class_name'] . '/' . $class1['class_name'];
|
||
}
|
||
}
|
||
$sku_name = M('retail_sku_list')->where(array('id' => array('in', $val['sku_id_list'])))->getField('name', true);
|
||
$val['goods_name'] = $val['goods_name'] . '(' . implode(',', $sku_name) . ')';
|
||
$val['all_purchase'] = strval(floatval(round($val['goods_num'] * $val['purchase_price'], 2)));
|
||
$val['goods_num'] = floatval($val['goods_num']);
|
||
$val['stock'] = strval($val['goods_num'] . $val['company']);
|
||
$val['purchase_price'] = strval(floatval($val['purchase_price']));
|
||
$val['retail_price'] = strval(floatval($val['retail_price']));
|
||
$val['online_price'] = strval(floatval($val['online_price']));
|
||
$val['sell_retail_total'] = strval(floatval(round($val['retail_price'] * $val['goods_num'], 2)));
|
||
$val['sell_online_total'] = strval(floatval(round($val['retail_price'] * $val['goods_num'], 2)));
|
||
$val['shop_mem_status'] = $val['shop_mem_status'] == 1 && $val['shop_mem_price'] > 0 ? strval(floatval($val['shop_mem_price'])) : ' 关闭 ';
|
||
$val['store_mem_status'] = $val['store_mem_status'] == 1 && $val['store_mem_price'] > 0 ? strval(floatval($val['store_mem_price'])) : ' 关闭 ';
|
||
$val['pricing_method'] = $val['pricing_method'] == 1 ? '非称重' : '称重';
|
||
// if ($val['supplier_id']) {
|
||
// $supplier_name = M('retail_supplier')->where(array('id' => $val['supplier_id']))->getField('supplier_name');
|
||
// $val['supplier_name'] = $supplier_name ? $supplier_name : '无';
|
||
// } else {
|
||
// $val['supplier_name'] = '无';
|
||
// }
|
||
if ($val['goods_brand_id']) {
|
||
$goods_brand_name = M('retail_goods_brand')->where(array('id' => $val['goods_brand_id']))->getField('name');
|
||
$val['goods_brand_name'] = $goods_brand_name ? $goods_brand_name : '无';
|
||
} else {
|
||
$val['goods_brand_name'] = '无';
|
||
}
|
||
// 商品来源
|
||
switch ($val['source_type']) {
|
||
case 1:
|
||
$val['source_name'] = '店长添加';
|
||
break;
|
||
case 2:
|
||
$val['source_name'] = '同步商户';
|
||
break;
|
||
case 3:
|
||
$val['source_name'] = '收银员添加';
|
||
break;
|
||
default:
|
||
$val['source_name'] = '未知来源';
|
||
}
|
||
/* 判断权限 */
|
||
if ($show_purch_price && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['all_purch_price'] = '***';
|
||
}
|
||
}
|
||
$res['status'] = 1;
|
||
$res['sku_list'] = $sku_list;
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 门店销售订单(加载模板)
|
||
*/
|
||
public function sell_order()
|
||
{
|
||
$this->display();
|
||
}
|
||
|
||
/**
|
||
* 获取销售数据展示
|
||
* @param int $page 分页
|
||
* @param int $limit 每页数量
|
||
* @param array $seek_data 搜索条件
|
||
* @return array $res
|
||
*/
|
||
public function get_bill_list()
|
||
{
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$inventory = I('session.identity');
|
||
// 获取session
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
// 接收数据
|
||
$page = (int)I('post.page');
|
||
$limit = (int)I('post.limit');
|
||
$seek_data = I('post.seek_data');
|
||
/* 时间搜索 */
|
||
$start_time = $seek_data['start_time'];
|
||
$end_time = $seek_data['end_time'];
|
||
if ($start_time && $end_time) {
|
||
$start_time = strtotime($start_time);
|
||
$end_time = strtotime($end_time);
|
||
$map['b.add_time'] = array('between', array($start_time, $end_time));
|
||
} else if ($start_time && !$end_time) {
|
||
$start_time = strtotime($start_time);
|
||
$map['b.add_time'] = array('egt', $start_time);
|
||
} else if (!$start_time && $end_time) {
|
||
$end_time = strtotime($end_time);
|
||
$map['b.add_time'] = array('elt', $end_time);
|
||
}
|
||
/* 订单号搜索 */
|
||
if (trim($seek_data['order_id'], '')) {
|
||
$map['b.order_no'] = array('like', '%' . $seek_data['order_id'] . '%');
|
||
}
|
||
/* 导购员搜索 */
|
||
if ($seek_data['guide_id']) {
|
||
$map['b.guide_id'] = $seek_data['guide_id'];
|
||
}
|
||
$map[] = array(
|
||
'b.order_type' => 1,
|
||
array(
|
||
'b.order_type' => array('in', '2,3'),
|
||
'b.refund_status' => 2,
|
||
),
|
||
'_logic' => 'OR',
|
||
);
|
||
/* 类型搜索 */
|
||
if ($seek_data['order_mold']) {
|
||
switch ($seek_data['order_mold']) {
|
||
case 1:
|
||
$map['b.type'] = 2;
|
||
break;
|
||
case 2:
|
||
$map['b.type'] = 1;
|
||
break;
|
||
case 3:
|
||
$map['b.type'] = 2;
|
||
$map['b.order_type'] = 1;
|
||
break;
|
||
case 4:
|
||
$map['b.type'] = 1;
|
||
$map['b.order_type'] = 1;
|
||
break;
|
||
case 5:
|
||
$map['b.type'] = 2;
|
||
$map['b.order_type'] = 2;
|
||
break;
|
||
case 6:
|
||
$map['b.type'] = 1;
|
||
$map['b.order_type'] = 2;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
$map['b.merchant_id'] = $user['merchantid'];
|
||
$map['b.store_id'] = $storeid;
|
||
$map['b.pay_state'] = 1;
|
||
if (!in_array('Credit', C('MODULE_ALLOW_LIST'))) {
|
||
$map['b.pay_type'] = array('neq', 0);
|
||
}
|
||
|
||
$bill_list = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->page($page, $limit)
|
||
->order('b.add_time desc')
|
||
->select();
|
||
$total = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->count();
|
||
//优惠券名称
|
||
$coupon_name = [
|
||
'门店优惠券:',
|
||
'会员优惠券:',
|
||
'微信商家券:',
|
||
];
|
||
$BillOrder = new BillOrder();
|
||
foreach ($bill_list as &$val) {
|
||
$val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
|
||
$val['pay_name'] = $BillOrder->getPayType($val['pay_type'], $val['id']);
|
||
|
||
/* 消费对象 */
|
||
if ($val['vid']) {
|
||
$RetailVipModel = new RetailVipModel();
|
||
$vip = $RetailVipModel->getFind(['id' => $val['vid']], 'name,nickname');
|
||
if ($vip) {
|
||
if ($vip['name']) {
|
||
$val['consumer_object'] = $vip['name'];
|
||
} else if ($vip['nickname']) {
|
||
$val['consumer_object'] = $vip['nickname'];
|
||
} else {
|
||
$val['consumer_object'] = '未设置';
|
||
}
|
||
} else {
|
||
$val['consumer_object'] = '散客';
|
||
}
|
||
} else if ($val['third_vip_code']) {
|
||
$val['consumer_object'] = $val['third_vip_name'];
|
||
} else {
|
||
$val['consumer_object'] = '散客';
|
||
}
|
||
/* 统计成本 */
|
||
$val['purchase_price'] = 0;
|
||
$RetailSellModel = new RetailSellModel();
|
||
$RetailGuideModel = new RetailGuideModel();
|
||
$sell_list = $RetailSellModel->getSelect(['bill_id' => $val['id']], 'num,purchase_price');
|
||
foreach ($sell_list as &$v) {
|
||
$val['purchase_price'] += floatval(round($v['num'] * $v['purchase_price'], 2));
|
||
}
|
||
$val['delivery_fee'] = $val['delivery_fee'] ? floatval($val['delivery_fee']) : 0;
|
||
$val['profit'] = floatval(round($val['all_money'] - $val['purchase_price'] - $val['delivery_fee'], 2));
|
||
$val['profit_margin'] = floatval(round($val['profit'] / ($val['all_money'] - $val['delivery_fee']) * 100, 2)) . '%';
|
||
$val['dismoney'] = $val['vip_dis_money'] && $val['vip_dis_money'] > 0 ? floatval($val['vip_dis_money']) : '无';
|
||
|
||
$val['coupon_money'] = $val['coupon_money'] > 0 ? $coupon_name[$val['coupon_type'] - 1] . floatval($val['coupon_money']) : '';
|
||
$val['pay_coupon_fee'] = $val['pay_coupon_fee'] > 0 ? '代金券金额:' . $val['pay_coupon_fee'] : '';
|
||
if (empty($val['coupon_money']) && empty($val['pay_coupon_fee'])) {
|
||
$val['reduction'] = 1;
|
||
} else {
|
||
$val['reduction'] = 2;
|
||
}
|
||
$val['delivery_fee'] = $val['delivery_fee'] && $val['delivery_fee'] > 0 ? floatval($val['delivery_fee']) : '无';
|
||
$val['small_changes_money'] = $val['small_changes_money'] && $val['small_changes_money'] > 0 ? floatval($val['small_changes_money']) : '无';
|
||
if ($val['discount'] > 0 && $val['discount'] < 1 && $val['cashier_dis_money']) {
|
||
$val['discount_remark'] = floatval($val['discount']) . '折金额:' . floatval($val['cashier_dis_money']);
|
||
} else {
|
||
$val['discount_remark'] = '未打折金额:无';
|
||
}
|
||
/* 查询导购员 */
|
||
if ($val['guide_id']) {
|
||
$guide_name = $RetailGuideModel->getFindField(['id' => $val['guide_id']], 'guidename');
|
||
$val['guide_name'] = $guide_name ? $guide_name : '未设置';
|
||
} else {
|
||
$val['guide_name'] = '自购';
|
||
}
|
||
/* 类型 */
|
||
$type_name = $val['type'] == 1 ? '商城' : '店内';
|
||
$order_type_name = $val['order_type'] == 1 ? '销售' : '退款';
|
||
$val['order_mold'] = $type_name . $order_type_name;
|
||
/* 退款订单加负号 */
|
||
if ($val['order_type'] != 1) {
|
||
$val['all_money'] = '-' . $val['all_money'];
|
||
$val['purchase_price'] = '-' . $val['purchase_price'];
|
||
$val['profit'] = 0;
|
||
$val['profit_margin'] = '0%';
|
||
}
|
||
/* 判断权限 */
|
||
if ($inventory == 2 && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['profit'] = '***';
|
||
$val['profit_margin'] = '***';
|
||
}
|
||
}
|
||
/* 统计 */
|
||
$res = $this->sell_statistics($map);
|
||
$res['status'] = 1;
|
||
$res['bill_list'] = $bill_list;
|
||
$res['total'] = $total ? (int)$total : 0;
|
||
$this->ajaxReturn($res);
|
||
}
|
||
|
||
/**
|
||
* 统计金额(公共方法)
|
||
* @param array $map 查询条件
|
||
* @return array $res
|
||
*/
|
||
protected function sell_statistics($map)
|
||
{
|
||
$RetailSellModel = new RetailSellModel();
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$inventory = I('session.identity');
|
||
// 销售
|
||
$map['b.order_type'] = 1;
|
||
// 笔数
|
||
$sell_num = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->count();
|
||
// 金额
|
||
$sell_money = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->sum('all_money');
|
||
// 所有销售成本
|
||
$sell_ids = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->field('b.id')
|
||
->where($map)
|
||
->select();
|
||
$sell_ids = array_column($sell_ids, 'id');
|
||
$sell_cost = 0;
|
||
if ($sell_ids) {
|
||
// 销售成本
|
||
$all_sell = $RetailSellModel->getSelect(['bill_id' => ['in', $sell_ids], 'purchase_price' => ['gt', 0]], 'num,purchase_price');
|
||
foreach ($all_sell as &$v1) {
|
||
$sell_cost += floatval(round($v1['num'] * $v1['purchase_price'], 2));
|
||
}
|
||
}
|
||
// 退款
|
||
unset($map['b.order_type']);
|
||
$map['b.order_type'] = 2;
|
||
// 笔数
|
||
$refund_num = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->count();
|
||
// 金额
|
||
$refund_money = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->where($map)
|
||
->sum('all_money');
|
||
// 所有退款成本
|
||
$refund_ids = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->field('b.id')
|
||
->where($map)
|
||
->select();
|
||
$refund_ids = $refund_ids ? array_column($refund_ids, 'id') : '0';
|
||
// 销售成本
|
||
$refund_cost = 0;
|
||
if ($refund_ids) {
|
||
$all_refund = $RetailSellModel->getSelect(['bill_id' => ['in', $refund_ids], 'purchase_price' => ['gt', 0]], 'num,purchase_price');
|
||
foreach ($all_refund as &$v2) {
|
||
$refund_cost += floatval(round($v2['num'] * $v2['purchase_price'], 2));
|
||
}
|
||
}
|
||
// 计算成本 (销售成本 - 退款成本)
|
||
$cost = floatval(round($sell_cost - $refund_cost, 2));
|
||
// 实际金额 (销售金额 - 退款金额)
|
||
$income = floatval(round($sell_money - $refund_money, 2));
|
||
// 计算盈利,利润()
|
||
$profit = floatval(round($income - $cost, 2));
|
||
$profit_margin = floatval(round($profit / $income * 100, 2)) . '%';
|
||
$res['sell_num'] = $sell_num ? $sell_num : 0;
|
||
$res['refund_num'] = $refund_num ? $refund_num : 0;
|
||
$res['sell_money'] = $sell_money ? $sell_money : 0;
|
||
$res['refund_money'] = $refund_money ? $refund_money : 0;
|
||
$res['income'] = $income ? $income : 0;
|
||
// 判断权限
|
||
if ($inventory == 2 && $show_purch_price == 2) {
|
||
$res['cost'] = '***';
|
||
$res['profit'] = '***';
|
||
$res['profit_margin'] = '***';
|
||
} else {
|
||
$res['cost'] = $cost ? $cost : 0;
|
||
$res['profit'] = $profit ? $profit : 0;
|
||
$res['profit_margin'] = $profit_margin ? $profit_margin : '0%';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 订单详情
|
||
* @param $sellids
|
||
*/
|
||
public function details()
|
||
{
|
||
// 查看进价权限
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
$bill_id = I('post.bill_id');
|
||
if (!$bill_id) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '参数缺失';
|
||
$this->ajaxReturn($res);
|
||
}
|
||
$order_details = M('retail_sell')
|
||
->field('id,company,num,goods_name,goods_type,purchase_price,price,sku_json_info,serial_number_state')
|
||
->where(array('merchant_id' => $user['merchantid'], 'storei_d' => $storeid, 'bill_id' => $bill_id))
|
||
->order('id asc')
|
||
->select();
|
||
if (!$order_details) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '详情查询失败';
|
||
$this->ajaxReturn($res);
|
||
}
|
||
$SerialClass = new SerialNumberGoods();
|
||
foreach ($order_details as &$val) {
|
||
if ($val['goods_name'] && $val['goods_type'] == 1) {
|
||
if ($val['sku_json_info']) {
|
||
$sku = json_decode($val['sku_json_info'], true);
|
||
if ($sku['purchase_price']) {
|
||
$val['purchase_price'] = $sku['purchase_price'];
|
||
} else {
|
||
$sku_purch_price = M('retail_sku_goods')->where(array('id' => $sku['id']))->getField('purchase_price');
|
||
$val['purchase_price'] = $sku_purch_price ? $sku_purch_price : $val['purchase_price'];
|
||
}
|
||
if ($sku['sku_id_list']) {
|
||
$name = M('retail_sku_list')->where(array('id' => array('in', $sku['sku_id_list'])))->getField('name', true);
|
||
$val['goods_name'] = $val['goods_name'] . '(' . implode(',', $name) . ')';
|
||
}
|
||
}
|
||
} else {
|
||
$val['goods_name'] = '无码商品';
|
||
}
|
||
$val['num'] = floatval($val['num']);
|
||
$val['price'] = floatval($val['price']) . '(元)';
|
||
if (!$show_purch_price || $show_purch_price == 1) {
|
||
$val['purchase_price'] = floatval($val['purchase_price']) . '(元)';
|
||
} else {
|
||
$val['purchase_price'] = '***';
|
||
}
|
||
//串号信息
|
||
if ($val['serial_number_state'] == 1) {
|
||
$val['serial_list'] = $SerialClass->sellSerialList([
|
||
'id' => $val['id']
|
||
])['data']['list'];
|
||
}
|
||
}
|
||
$res['status'] = 1;
|
||
$res['order_details'] = $order_details;
|
||
$this->ajaxReturn($res);
|
||
}
|
||
|
||
/* excel导出方法(公共方法)
|
||
* */
|
||
protected 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);
|
||
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 sell_export()
|
||
{
|
||
if (IS_POST) {
|
||
ini_set('memory_limit', '300M');
|
||
/* 查看进价权限 */
|
||
$show_purch_price = I('session.global_store_jurisdiction')['showpurch_price'];
|
||
$inventory = I('session.identity');
|
||
$user = I('session.ls_userinfo');
|
||
$storeid = I('session.storeid');
|
||
$limit = I('post.limit') ? I('post.limit') : 500;
|
||
$page = I('post.page') ? I('post.page') : 1;
|
||
$directory_name = I('post.directory_name');
|
||
$seek_data = I('post.seek_data'); // 搜索条件
|
||
/* 时间搜索 */
|
||
$start_time = $seek_data['start_time'];
|
||
$end_time = $seek_data['end_time'];
|
||
if ($start_time && $end_time) {
|
||
$start_time = strtotime($start_time);
|
||
$end_time = strtotime($end_time);
|
||
$map['b.add_time'] = ['between', [$start_time, $end_time]];
|
||
} else if ($start_time && !$end_time) {
|
||
$start_time = strtotime($start_time);
|
||
$map['b.add_time'] = ['egt', $start_time];
|
||
} else if (!$start_time && $end_time) {
|
||
$start_time = strtotime($start_time);
|
||
$map['b.add_time'] = ['elt', $start_time];
|
||
}
|
||
/* 订单号搜索 */
|
||
if (trim($seek_data['order_id'], '')) {
|
||
$map['b.order_no'] = ['like', '%' . $seek_data['order_id'] . '%'];
|
||
}
|
||
/* 导购员搜索 */
|
||
if ($seek_data['guide_id']) {
|
||
$map['b.guide_id'] = $seek_data['guide_id'];
|
||
}
|
||
$map[] = [
|
||
'b.order_type' => 1,
|
||
[
|
||
'b.order_type' => ['in', '2,3'],
|
||
'b.refund_status' => 2,
|
||
],
|
||
'_logic' => 'OR',
|
||
];
|
||
/* 类型搜索 */
|
||
if ($seek_data['order_mold']) {
|
||
switch ($seek_data['order_mold']) {
|
||
case 1:
|
||
$map['b.type'] = 2;
|
||
break;
|
||
case 2:
|
||
$map['b.type'] = 1;
|
||
break;
|
||
case 3:
|
||
$map['b.type'] = 2;
|
||
$map['b.order_type'] = 1;
|
||
break;
|
||
case 4:
|
||
$map['b.type'] = 1;
|
||
$map['b.order_type'] = 1;
|
||
break;
|
||
case 5:
|
||
$map['b.type'] = 2;
|
||
$map['b.order_type'] = 2;
|
||
break;
|
||
case 6:
|
||
$map['b.type'] = 1;
|
||
$map['b.order_type'] = 2;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
$map['b.merchant_id'] = $user['merchantid'];
|
||
$map['b.store_id'] = $storeid;
|
||
$map['b.pay_state'] = 1;
|
||
$map['b.pay_type'] = ['neq', 0];
|
||
$bill_list = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_discount as d on b.id=d.bill_id')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id', 'LEFT')
|
||
->field('b.id,b.order_no,b.all_money,b.pay_type,b.type,b.order_type,b.add_time,b.vid,b.guide_id,d.vip_dis_money,d.coupon_money,d.small_changes_money,o.delivery_fee,d.discount,d.cashier_dis_money,d.pay_coupon_fee,d.coupon_type,b.third_vip_code,b.third_vip_name')
|
||
->where($map)
|
||
->page((int)$page, (int)$limit)
|
||
->order('b.add_time desc')
|
||
->select();
|
||
//优惠券名称
|
||
$coupon_name = [
|
||
'门店优惠券:',
|
||
'会员优惠券:',
|
||
'微信商家券:',
|
||
];
|
||
$BillOrder = new BillOrder();
|
||
foreach ($bill_list as $key => &$val) {
|
||
$val['index'] = $key + 1;
|
||
$val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
|
||
|
||
$val['pay_name'] = $BillOrder->getPayType($val['pay_type'], $val['id']);
|
||
|
||
/* 消费对象 */
|
||
if ($val['vid']) {
|
||
$RetailVipModel = new RetailVipModel();
|
||
$vip = $RetailVipModel->getFind(['id' => $val['vid']], 'name,nickname');
|
||
if ($vip) {
|
||
if ($vip['name']) {
|
||
$val['consumer_object'] = $vip['name'];
|
||
} else if ($vip['nickname']) {
|
||
$val['consumer_object'] = $vip['nickname'];
|
||
} else {
|
||
$val['consumer_object'] = '未设置';
|
||
}
|
||
} else {
|
||
$val['consumer_object'] = '散客';
|
||
}
|
||
} else if ($val['third_vip_code']) {
|
||
$val['consumer_object'] = $val['third_vip_name'];
|
||
} else {
|
||
$val['consumer_object'] = '散客';
|
||
}
|
||
/* 统计成本 */
|
||
$val['purchase_price'] = 0;
|
||
$RetailSellModel = new RetailSellModel();
|
||
$RetailGuideModel = new RetailGuideModel();
|
||
$sell_list = $RetailSellModel->getSelect(['bill_id' => $val['id']], 'num,purchase_price');
|
||
foreach ($sell_list as &$v) {
|
||
$val['purchase_price'] += floatval(round($v['num'] * $v['purchase_price'], 2));
|
||
}
|
||
$val['profit'] = strval(floatval(round($val['all_money'] - $val['purchase_price'] - $val['delivery_fee'], 2)));
|
||
$val['profit_margin'] = strval(floatval(round($val['profit'] / ($val['all_money'] - $val['delivery_fee']) * 100, 2))) . '%';
|
||
$val['vip_dis_money'] = $val[' vip_dis_money'] && $val[' vip_dis_money'] > 0 ? strval(floatval($val[' vip_dis_money'])) : '无';
|
||
$coupon_money = '';
|
||
if ($val['coupon_money'] > 0) {
|
||
$coupon_money .= $coupon_name[$val['coupon_type'] - 1] . floatval($val['coupon_money']);
|
||
}
|
||
if ($val['pay_coupon_fee'] > 0) {
|
||
$coupon_money .= '代金券金额:' . floatval($val['pay_coupon_fee']);
|
||
}
|
||
$val['coupon_money'] = $coupon_money;
|
||
$val['delivery_fee'] = $val['delivery_fee'] && $val['delivery_fee'] > 0 ? strval(floatval($val['delivery_fee'])) : '无';
|
||
$val['small_changes_money'] = $val['small_changes_money'] && $val['small_changes_money'] > 0 ? strval(floatval($val['small_changes_money'])) : '无';
|
||
if ($val['discount'] > 0 && $val['discount'] < 1 && $val['cashier_dis_money']) {
|
||
$val['discount_remark'] = strval(floatval($val['discount'])) . '折金额:' . strval(floatval($val['cashier_dis_money']));
|
||
} else {
|
||
$val['discount_remark'] = '未打折金额:无';
|
||
}
|
||
/* 查询导购员 */
|
||
if ($val['guide_id']) {
|
||
$guide_name = $RetailGuideModel->getFindField(['id' => $val['guide_id']], 'guidename');
|
||
$val['guide_name'] = $guide_name ? $guide_name : '未设置';
|
||
} else {
|
||
$val['guide_name'] = '自购';
|
||
}
|
||
/* 类型 */
|
||
$type_name = $val['type'] == 1 ? '商城' : '店内';
|
||
$order_type_name = $val['order_type'] == 1 ? '销售' : '退款';
|
||
$val['order_mold'] = $type_name . $order_type_name;
|
||
$val['purchase_price'] = strval($val['purchase_price']);
|
||
$val['all_money'] = strval(floatval($val['all_money']));
|
||
/* 退款订单加负号 */
|
||
if ($val['order_type'] != 1) {
|
||
$val['all_money'] = '-' . $val['all_money'];
|
||
$val['purchase_price'] = '-' . $val['purchase_price'];
|
||
$val['profit'] = 0;
|
||
$val['profit_margin'] = '0%';
|
||
}
|
||
/* 判断权限 */
|
||
if ($inventory == 2 && $show_purch_price == 2) {
|
||
$val['purchase_price'] = '***';
|
||
$val['profit'] = '***';
|
||
$val['profit_margin'] = '***';
|
||
}
|
||
}
|
||
$bill_list = $bill_list ? $bill_list : [];
|
||
import("Vendor.PHPExcel.PHPExcel");
|
||
import("Vendor.PHPExcel.Writer.Excel5");
|
||
import("Vendor.PHPExcel.IOFactory.php");
|
||
$filename = '店内收款明细_' . date("ymdHis", time());
|
||
$headArr = [
|
||
['index', '序号'],
|
||
['order_mold', '类型'],
|
||
['order_no', '订单号'],
|
||
['add_time', '售出时间'],
|
||
['consumer_object', '消费对象'],
|
||
['guide_name', '导购'],
|
||
['pay_name', '支付方式'],
|
||
['all_money', '支付金额'],
|
||
['purchase_price', '成本合计'],
|
||
['coupon_money', '优惠券'],
|
||
['small_changes_money', '抹零'],
|
||
['vip_dis_money', '会员折扣'],
|
||
['discount_remark', '收银折扣'],
|
||
['delivery_fee', '费送费'],
|
||
['profit', '利润'],
|
||
['profit_margin', '利润率'],
|
||
];
|
||
$str = $filename . '表格生成时间:' . date('Y-m-d H:i:s');
|
||
if (!$directory_name) {
|
||
$num = createKey();
|
||
$directory_name = "./Uploads/storeGoodsExcel/{$storeid}/{$num}";
|
||
}
|
||
$ExporttableController = new ExporttableController();
|
||
$ExporttableController->setExcel($filename, $headArr, $bill_list, $str, $directory_name);
|
||
$result = array(
|
||
'status' => 1,
|
||
'type' => 1,
|
||
'directory_name' => $directory_name,
|
||
'msg' => '请继续生成文件'
|
||
);
|
||
if (count($bill_list) < $limit) {
|
||
$result['type'] = 2;
|
||
$result['msg'] = '数据生成完毕';
|
||
}
|
||
$this->ajaxReturn($result);
|
||
} else if (IS_GET) {
|
||
$directory_name = I('get.directory_name');
|
||
$ExporttalController = new ExporttableController();
|
||
$res = $ExporttalController->fileZipAddToDel($directory_name, '报表');
|
||
$this->ajaxReturn($res);
|
||
}
|
||
}
|
||
|
||
//串号列表
|
||
public function serialList()
|
||
{
|
||
$data = I('post.');
|
||
$data['user_type'] = 4;
|
||
$data['merchant_id'] = $this->merchant_id;
|
||
$data['store_id'] = $this->store_id;
|
||
$SerialClass = new SerialNumberGoods();
|
||
$result = $SerialClass->warehouseSerialList($data);
|
||
$this->ajaxReturn($result);
|
||
}
|
||
}
|