文件
mashangyoujiu/Application/Retail/View/Sale/SaleController.class.php

2370 行
108 KiB
PHP
可执行文件

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

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

<?php
namespace Retail\Controller;
use Think\Controller;
//[1.1.51]
class SaleController extends BaseController
{
//收款明细
public function recebles()
{
//获取门店信息
$storeid = session('storeid');
$store_people = M('retail_cashier')->where(array('storeid' => $storeid, 'status' => 1))->select();
$this->assign('store_people', $store_people);
$this->display();
}
public function sell_info_old()
{
$storeid = session('storeid');
$where['b.pay_state'] = 1;
$where['b.type'] = 2;
$where['b.storeid'] = $storeid;
$date = I('get.date01');
$title = I('get.title'); //单号
$person = I('get.person'); //店员
$card = I('get.card'); // 会员号
$pay = I('get.pay'); //支付方式
//具体时间
if (!empty($date)) {
$time = I('get.date01');
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d', strtotime($time[1]))) + 86400;
$where['b.addtime'] = array('between', array($start_time, $end_time));
}
//单号
if (!empty($title)) {
$where['b.order_id'] = $title;
}
//店员
if (!empty($person)) {
$where['b.admin_id'] = $person;
}
//会员卡号
if (!empty($card)) {
$merchantid = M('retail_store')->where(array('storeid' => $storeid))->getfield('merchantid');
$vipmap['merchantid'] = $merchantid;
$vipmap['mobile_phone|card_key'] = $card;
$id = M('retail_vip')->where($vipmap)->getfield('id', true);
$where['b.vipid'] = array('in', $id ? $id : '0');
}
//付款方式
if (!empty($pay)) {
$where['b.paytype'] = $pay;
}
//订单类型
$type = I("get.type");//当type为2时表示查看退款订单信息
if (!empty($type)) {
$where['b.order_type'] = $type;
}
$count = M('retail_bill as b')->where($where)->count();
$p = I('get.page');
$l = I('get.limit');
$sell_info = M('retail_bill as b')
->join('ls_retail_sell as s on b.sellid=s.id')
->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')
->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')
->where($where)->order('b.addtime desc')->page($p, $l)
->field('b.*,s.discount as vipdiscount,v.mobile_phone as v_mobile,v.card_key as v_key,c.name as adminname')
->select();
foreach ($sell_info as $key => &$val) {
$val['key'] = $key + 1;
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
//支付方式
if ($val['paytype'] == 1) {
$val['payname'] = '现金';
} else if ($val['paytype'] == 2) {
$val['payname'] = '银行卡';
} else if ($val['paytype'] == 3) {
$val['payname'] = '微信支付';
} else if ($val['paytype'] == 4) {
$val['payname'] = '支付宝支付';
} else if ($val['paytype'] == 5) {
$val['payname'] = '会员卡余额支付';
} else if ($val['paytype'] == 6) {
$val['payname'] = '微信支付';
} else if ($val['paytype'] == 7) {
$val['payname'] = '其他支付方式';
}
//交易类型
if ($val['type'] == 1) {
$val['typename'] = '商城';
} else if ($val['type'] == 2) {
$val['typename'] = '店内';
}
if ($val['vipid']) {
$val['vipid'] = $val['v_mobile'] ? $val['v_mobile'] : '';
$val['vipkey'] = $val['v_key'] ? $val['v_key'] : '';
} else {
$val['vipid'] = '';
$val['vipkey'] = '';
}
//收银员名称
if (!$val['adminname']) {
$val['adminname'] = '---';
}
//未发货
if ($val['order_type'] == 2) {
$val['status_name'] = '退款订单';
} else {
if ($val['refund_status'] == 3) {
$val['status_name'] = '有退款';
} else {
$val['status_name'] = '无退款';
}
}
$val['discount'] = $val['discount'] == 1 ? '' : $val['discount'];
if($val['guideid']){
$guide_name = M('retail_guide')->where(array('id'=>$val['guideid']))->getField('guidename');
$val['guide_name'] = $guide_name ? $guide_name : '自购';
}else{
$val['guide_name'] = '自购';
}
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $sell_info;
$rt['count'] = $count;
$this->ajaxReturn($rt);
}
// 收款金额汇总
public function sellallmoney()
{
$storeid = session('storeid');
//查询今天页面默认收款数据
//查询条件
//具体时间
$date = I('get.date01');
if (!empty($date)) {
$time = explode('至', $date);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
$map['addtime'] = array('between', array($start_time, $end_time));
}
//单号
$order_id = I('get.title');
if (!empty($order_id)) {
$where['order_id'] = $order_id;
$map['order_id'] = $order_id;
}
//店员
$person = I('get.person');
if (!empty($person)) {
$where['admin_id'] = $person;
$map['admin_id'] = $person;
}
//会员卡号
$card = I('get.card');
if (!empty($card)) {
$merchantid = M('retail_store')->where(array('id' => $storeid))->getfield('merchantid');
$vipmap['merchantid'] = $merchantid;
$vipmap['mobile_phone|card_key'] = $card;
$id = M('retail_vip')->where($vipmap)->getfield('id', true);
$where['vipid'] = array('in', $id ? $id : '0');
$map['vipid'] = array('in', $id ? $id : '0');
}
$where['paytype'] = array('neq', 0);
$map['paytype'] = array('neq', 0);
$pay = I('get.pay'); //付款方式
if (!empty($pay)) {
$where['paytype'] = $pay;
$map['paytype'] = $pay;
}
$where['order_type'] = 1;
$where['type'] = 2;
$where['pay_state'] = 1;
$where['storeid'] = $storeid;
$allmoney = M('retail_bill')->where($where)->sum('allmoney');
$allmoney = $allmoney ? $allmoney : 0;
//退款金额
$where['order_type'] = 2;
$where['refund_status'] = 3;
$r_fund = M("retail_bill")->where($where)->sum('allmoney');
$r_fund = $r_fund ? $r_fund : 0;
//优惠金额
$map['pay_state'] = 1;
$map['refund_status'] = array('in', '0,1');
$map['type'] = 2;
$map['order_type'] = 1;
$map['storeid'] = $storeid;
$couponmoey = M('retail_bill')->where($map)->sum('exchange_money');
$couponmoey = $couponmoey ? round($couponmoey, 2) : '0.00';
$dismoney = M('retail_bill')->where($map)->sum('dismoney');
$dismoney = $dismoney ? round($dismoney, 2) : 0;
//抹零金额
$changesmoney = M('retail_bill')->where($map)->sum('small_changes_money');
$changesmoney = $changesmoney ? round($changesmoney, 2) : '0.00';
$rt['allmoney'] = $allmoney;
$rt['refund_money'] = $r_fund;
$rt['dismoney'] = $dismoney;
$rt['changesmoney'] = $changesmoney;
$rt['couponmoey'] = $couponmoey;
$this->ajaxReturn($rt);
}
//商品详情
public function sell_goods_info()
{
$id = I('get.id');
$sell_good_info = M('retail_bill')->where(array('id' => $id))->find();
if ($sell_good_info['isneedsend'] == 1) {
$name = $sell_good_info['link_name'];
$phone = $sell_good_info['mobile_phone'];
$address = '到店自提';
} else if ($sell_good_info['isneedsend'] == 2) {
$address_info = M('retail_address')->where(array('id' => $sell_good_info['xcxmsgid']))->find();
$name = $address_info['name'];
$phone = $address_info['phone'];
$address = $address_info['address'];
}
$toallmonety = $sell_good_info['allmoney'] + $sell_good_info['exchange_money'] + $sell_good_info['small_changes_money'];
//出售表id
$sell_id = explode(',', $sell_good_info['sellid']);
foreach ($sell_id as $key => &$vall) {
if ($vall) {
$sell = M('retail_sell')->where(array('id' => $vall))->find();
// $sell['discount'] = $sell_good_info['discount'];
$sell['price'] = round($sell_good_info['discount'] * $sell['price'], 2);
$sell['discount_price'] = round(($sell['yuan_price'] - $sell['price']) * $sell['num'], 2);
if ($sell['can_exchange_weigh'] == 2) {
$sell['num'] = $sell['num'] . "(称重)";
}
if (intval($sell['num']) == $sell['num']) {
$sell['num'] = intval($sell['num']);
}
if ($sell['goods_type'] == 2 || $sell['goods_id'] == 0) {
$sell['goods_name'] = '无码商品';
} else {
if ($sell['sku_json_info']) {
$sell['goods_name'] .= '(' . json_decode($sell['sku_json_info'], true)['name'] . ')';
} else if ($sell['skuinfo']) {
$skuinfo = explode(',', trim(json_decode($sell['skuinfo'], true)[0]['sku'], ','));
if (!empty($skuinfo[0]) && !empty($skuinfo[1])) {
$sell['goods_name'] = $sell['goods_name'] . '-' . $skuinfo[0] . '-' . $skuinfo[1];
} elseif (!empty($skuinfo[0])) {
$sell['goods_name'] = $sell['goods_name'] . '-' . $skuinfo[0];
}
}
}
if ($sell['promotion_type'] == 1) {
$sell['promotion_info'] = json_decode($sell['promotion_info'], true);
if (intval($sell['promotion_info']['bundle_number']) == $sell['promotion_info']['bundle_number']) {
$sell['promotion_info']['bundle_number'] = intval($sell['promotion_info']['bundle_number']);
}
$sell['goods_name'] .= '<br>' . $sell['promotion_info']['bundle_number'] . '件促销';
}
$sell_good_info2[] = $sell;
}
}
if (!$id) {
$rt['code'] = 1;
$rt['msg'] = '';
$rt['data'] = array();
$this->ajaxReturn($rt);
}
$rt['id'] = $id;
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $sell_good_info2;
$rt['res'] = $skuinfo;
$rt['toallmonety'] = $toallmonety; //合计
$rt['allmoney'] = $sell_good_info['allmoney']; //总计
$rt['dismoney'] = $sell_good_info['dismoney']; //总计
$rt['distribution_fee'] = $sell_good_info['distribution_fee']; //配送费
$rt['exchange_money'] = $sell_good_info['exchange_money']; //优惠券费用
$rt['is_meet_reduce'] = $sell_good_info['is_meet_reduce']?$sell_good_info['is_meet_reduce']:1; // 满减?
$rt['reduce_money'] = $sell_good_info['reduce_money']?$sell_good_info['reduce_money']:0; // 满减价格
$rt['name'] = $name;
$rt['phone'] = $phone;
$rt['address'] = $address;
$rt['remark'] = $sell_good_info['remark'];
$this->ajaxReturn($rt);
}
//销售汇总
public function saleDetails()
{
//获取门店信息
$storeid = session('storeid');
//查出门店名称
$merchantid = M('retail_store')->where(array('id' => $storeid, 'status' => 1))->getField('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 saleDetailsinfo()
{
//获取门店信息
$storeid = session('storeid');
//查询时间
$datetime = I('get.date01');
if (!empty($datetime)) {
$time = explode('至', $datetime);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['s.addtime'] = array('between', array($start_time, $end_time));
}
//货号
$goods_number = I('get.title');
if (!empty($goods_number)) {
$where['s.goods_number'] = $goods_number;
}
//类别
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$where['s.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['s.class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['s.class_id'] = $person2 ? $person2 : $person1;
}
$where['s.paytype'] = array('neq', 0);
$where['s.storeid'] = $storeid;
$where['s.status'] = 1;
$where['s.is_refund'] = 1;
$count = M('retail_sell as s')->where($where)->group('s.goods_id')->select();
$count = count($count);
$page = I('get.page');
$limit = I('get.limit');
$sell_summary = M('retail_sell as s')
->join('ls_retail_goods as g on g.id=s.goods_id', 'LEFT')
->where($where)
->group('s.goods_id')
->field('s.*,g.goods_name,g.goods_code,g.goods_num,g.retail_price,g.purch_price')
->order('s.addtime desc')
->page($page, $limit)
->select();
// print_r($sell_summary);die;
if (!empty($sell_summary)) {
$map = array(
'storeid' => $storeid,
'paytype' => array('neq', 0),
'status' => 1,
'is_refund' => 1
);
if ($start_time && $end_time) {
$map['addtime'] = array('between', array($start_time, $end_time));
}
foreach ($sell_summary as $key => &$val) {
if ($val['goods_type'] == 2 && !$val['goods_id']) {
$map['goods_id'] = 0;
$val['goods_name'] = '无码商品';
$val['goods_num'] = 0;
$val['retail_price'] = $val['price'];
$val['goods_code'] = '---';
$val['goods_number'] = '';
$num = M('retail_sell')->where($map)->sum('num');//销量
$sell_number = M('retail_sell')->where($map)->sum('sell_number'); // 实际销量
$money = M('retail_sell')->where($map)->sum('allmoney');//销量
$money = $money ? $money : 0;
$val['retail_price'] = round($money / $num, 2);
$val['yuan_price'] = $val['retail_price'];
$val['purch_price'] = $val['retail_price'];
} else {
$map['goods_id'] = $val['goods_id'];
$goods = M('retail_goods')->where(array('id' => $val['goods_id']))->find();
$val['goods_code'] = $goods['goods_code'] ? $goods['goods_code'] : '---';
$num = M('retail_sell')->where($map)->sum('num'); //销量
$sell_number = M('retail_sell')->where($map)->sum('sell_number'); // 实际销量
$money = M('retail_sell')->where($map)->sum('allmoney');//金额小计
}
$val['xs_num'] = $num ? round($num,3) : 0;
$val['sell_number'] = $sell_number ? round($sell_number,2) : 0;
$val['je_money'] = $money ? round($money,2) : 0;
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$val['key'] = $key + 1;
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $sell_summary;
$rt['count'] = $count;
} else {
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = array();
$rt['count'] = 0;
}
$this->ajaxReturn($rt);
}
// 商品销售明细
public function particulars()
{
//获取门店信息
$storeid = session('storeid');
//店员
$merchantid = M('retail_store')->where(array('id' => $storeid, 'status' => 1))->getField('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 particularsinfo()
{
$storeid = session('storeid');
if (I('get.date01') != "") {
$time = I('get.date01');
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
}
//单号
if (I('get.title') != "") {
$where['goods_number'] = I('get.title');
}
$where['paytype'] = array('neq', 0);
$where['storeid'] = $storeid;
$p = I('get.page');
$l = I('get.limit');
//类别
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$where['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['class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['class_id'] = $person2 ? $person2 : $person1;
}
$count = M('retail_sell')->where($where)->count();
$sell_summary = M('retail_sell')->where($where)->order('addtime desc')->page($p, $l)->select();
foreach ($sell_summary as $key => &$val) {
$val['key'] = $key + 1;
//商品信息
if (!empty($val['goods_id'])) {
$goods = M('retail_goods')->where(array('id' => $val['goods_id']))->find();
$val['goods_number'] = $goods['good_number'];
$val['goods_code'] = $goods['goods_code'] ? $goods['goods_code'] : '---';
$val['goods_name'] = $goods['goods_name'];//品名
} else if ($val['goods_type'] == 2 || !$val['goods_id']) {
$val['goods_code'] = '---';
$val['goods_name'] = '无码商品';//品名
}
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$val['cou_price'] = ($val['yuan_price'] - $val['price']) * $val['num'];
$val['je_money'] = round($val['price'] * $val['num'],2);//金额小计
$val['je_money'] = $val['je_money'] ? $val['je_money'] : 0;
$val['type'] = $val['type'] == 1 ? '商城' : '店内';
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
if ($val['status'] == 1) {
if ($val['is_refund'] == 1) {
$val['refund'] = '销售';
} elseif ($val['is_refund'] == 2) {
$val['refund'] = '有退款';
} else if ($val['is_refund'] == 3) {
$val['refund'] = '已退款';
}
} else if ($val['status'] == 2) {
$val['refund'] = '退款订单';
}
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $sell_summary;
$rt['count'] = $count;
$this->ajaxReturn($rt);
}
//获取分类
public function getChildClass()
{
$user = session('ls_userinfo');
if(IS_POST && !empty($user)){
$id = I('post.id');
$type = I('post.type');
if ($type == 2) {
$class = M('retail_goods_class as c1')
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
->where(array('c1.id' => $id, 'c2.status' => 0))
->field('c2.id,c2.class_name')
->order('c2.code asc')
->select();
} else {
$class = M('retail_goods_class as c2')
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
->where(array('c2.id' => $id, 'c3.status' => 0))
->field('c3.id,c3.class_name')
->order('c3.code asc')
->select();
}
if (!empty($class)) {
$result['status'] = 1;
$result['data'] = $class;
$result['msg'] = '查询成功';
} else {
$result['status'] = 2;
$result['msg'] = '暂无分类';
}
}else{
$result['status'] = 0;
$result['msg'] = '登录超时';
}
$this->ajaxReturn($result);
}
//滞销商品
public function deadStock()
{
//获取门店信息
$storeid = session('storeid');
//查出门店名称
$store_name = M('retail_store')->where(array('id' => $storeid))->find();
$this->assign('store_name', $store_name);
$this->display();
}
public function deadStockinfo()
{
//获取门店信息
$storeid = session('storeid');
//出售的商品
$start = strtotime("-1 month");
$end = time();
$where['paytype'] = array('neq', 0);
$where['storeid'] = $storeid;
$where['status'] = 1;
$where['is_refund'] = 1;
$where['addtime'] = array('between', array($start, $end));
$dead_info = M('retail_sell')->where($where)->group('goods_id')->getField('goods_id', true);
$dead_info = $dead_info ? $dead_info : array(0);
//所有商品
//查询条件货号
$goodsnum = I('get.name02');
if (!empty($goodsnum)) {
$data['good_number'] = $goodsnum;
}
//查询品名
$product = I('get.name03');
if (!empty($product)) {
$data['goods_name'] = array('like', array("%" . $product . "%"));
}
$data['storeid'] = $storeid;
$data['status'] = 0;
$data['id'] = array('not in', $dead_info);
$good_info = M('retail_goods')->where($data)->page($_GET['page'], $_GET['limit'])->order('id desc')->select();
$count = M('retail_goods')->where($data)->count();
if (!$good_info) {
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $good_info;
$rt['count'] = $count;
$this->ajaxReturn($rt);
exit;
}
foreach ($good_info as $key => &$val) {
$val['key'] = $key + 1;
//销量
$volume = M('retail_sell')->where(array('goods_id' => $val['id'], 'status' => 1, 'paytype' => array('neq', 0), 'is_refund' => 1))->sum('num');
$val['saleVol'] = $volume ? $volume : 0;
$val['goods_code'] = $val['goods_code'] ? $val['goods_code'] : '---';
$val['good_number'] = $val['good_number'] ? $val['good_number'] : '---';
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $good_info;
$rt['count'] = $count;
$this->ajaxReturn($rt);
}
//畅销商品
public function salable()
{
//获取门店信息
$storeid = session('storeid');
//查出门店名称
$store_name = M('retail_store')->where(array('id' => $storeid))->find();
$this->assign('store_name', $store_name);
$this->display();
}
public function salableinfo()
{
$storeid = session('storeid');
//按照出售商品销量排序
//查询条件货号
$number = I('get.name02');
$page = I('get.page');
$limit = I('get.limit');
if (!empty($number)) {
$where['goods_number'] = $number;
}
$product = I('get.name03');
//查询品名
if (!empty($product)) {
$where['goods_name'] = array('like', '%' . $product . '%');
}
$start = strtotime("-1 month");
$end = time();
$where['addtime'] = array('between', array($start, $end));
$where['storeid'] = $storeid;
$where['paytype'] = array('neq', 0);
$where['status'] = 1;
$where['is_refund'] = 1;
$count = M('retail_sell')->where($where)->group('goods_id')->select();
$count = count($count);
$best_sell = M('retail_sell')->where($where)->group('goods_id')->page($page, $limit)->order('id desc')->select();
if (!$best_sell) {
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $best_sell;
$rt['count'] = $count;
$this->ajaxReturn($rt);
exit;
}
foreach ($best_sell as $key => &$val) {
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$val['key'] = $key + 1;
$val['goods_code'] = M('retail_goods')->where(array('id' => $val['goods_id']))->getField('goods_code');
$val['goods_code'] = $val['goods_code'] ? $val['goods_code'] : '---';
$val['good_name'] = M('retail_goods')->where(array('id' => $val['goods_id']))->getField('goods_name');
//30天销量
$where['goods_id'] = $val['goods_id'];
$val['ynum'] = M('retail_sell')->where($where)->sum('num');
//总销量
$data['storeid'] = $storeid;
$data['paytype'] = array('neq', 0);
$data['status'] = 1;
$data['goods_id'] = $val['goods_id'];
$data['is_refund'] = 1;
//查询条件货号
if (!empty($number)) {
$data['goods_number'] = $number;
}
//查询品名
if (!empty($product)) {
$data['goods_name'] = array('like', '%' . $product . '%');
}
$val['znum'] = M('retail_sell')->where($data)->sum('num');
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $best_sell;
$rt['count'] = $count;
$this->ajaxReturn($rt);
}
// 毛利
public function profit()
{
$this->display();
}
public function profitinfo()
{
$storeid = session('storeid');
$time = I('get.date01');
if (!empty($time)) {
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
$rt['code'] = 0;
$count = 1;
$where['paytype'] = array('neq', 0);
$where['status'] = 1;
$where['storeid'] = $storeid;
$myinfo = M('retail_sell')->where(array('storeid' => $storeid))->where($where)->select();
$allpaymoney = 0;
$allgetmoney = 0;
foreach ($myinfo as $k => $v) {
if ($v['is_refund'] == 1) {
$allpaymoney += ($v['purch_price'] * $v['num']);
$allgetmoney += $v['allmoney'];
} else if ($v['is_refund'] == 2) {
$allpaymoney += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$allgetmoney += ($v['allmoney'] - ($v['price'] * $v['refund_num']));
}
}
$map = array();
//总优惠金额
$map['storeid'] = $storeid;
$map['paytype'] = array('neq', 0);
$map['refund_status'] = 0;
$map['order_type'] = 1;
$map['addtime'] = array('between', array($start_time, $end_time));
$map['pay_state'] = 1;
$allcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
//总门店配送费
$distribution_fee = M('retail_bill')->where($map)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
$map['fee_state'] = 2;
$map['refund_status'] = 2;
$fee_state = M('retail_bill')->where($map)->sum('distribution_fee');
$fee_state = $fee_state ? $fee_state : 0;
$distribution_fee = $distribution_fee + $fee_state;
//达达配送费
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$cost['addtime'] = array('between', array($start_time, $end_time));
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee = $deduct_fee ? $deduct_fee : 0;
$distribution_fee_da = $deduct_fee + $fee;
$allgetmoney = $allgetmoney - $allcouponmoney - $small_changes_money;
$profitmoney = round($allgetmoney + $distribution_fee - $allpaymoney - $distribution_fee_da, 2);
$item['allcouponmoney'] = $allcouponmoney;
$item['small_changes_money'] = $small_changes_money;
$item['allpaymoney'] = $allpaymoney == 0 ? '无销售数据' : $allpaymoney;
$item['allgetmoney'] = $allgetmoney == 0 ? '无销售数据' : $allgetmoney;
$item['profitmoney'] = $profitmoney;
$item['distribution_fee'] = $distribution_fee;
$item['distribution_fee_da'] = $distribution_fee_da;
$item['index'] = 1;
$item['chatime'] = I('get.date01');
$infoarr[] = $item;
$rt['msg'] = '';
$rt['data'] = $infoarr;
$rt['count'] = $count;
$this->ajaxReturn($rt);
} else {
// 查询最早的一条出售记录
$starttime = M('retail_sell')->where(array('storeid' => $storeid, 'status' => 1))->order('addtime asc')->getField('addtime');
if (!$starttime) {
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = array();
$rt['count'] = 0;
$this->ajaxReturn($rt);
exit;
}
$end = strtotime(date('Y-m-d') . ' 23:59:59') + 1;
$start = strtotime(date('Y-m-d', $starttime) . ' 00:00:00');
$count = ($end - $start) / 86400;
$mystart = $end - 86400 * ($_GET['page']) * $_GET['limit'];
$myend = $end - 86400 * ($_GET['page'] - 1) * $_GET['limit'];
$chastart = strtotime(date('Y-m-d', $mystart) . ' 00:00:00');
$chastart = $chastart <= $start ? $start : $chastart;
$chaend = strtotime(date('Y-m-d', $myend) . ' 00:00:00');
$infoarr = array();
$index = 1;
while ($chaend > $chastart) {
$item = array();
$myinfo = array();
$item['chatime'] = date('Y-m-d', $chaend - 1);
$mw['storeid'] = $storeid;
$mw['addtime'] = array('between', array($chaend - 86400, $chaend));
$mw['paytype'] = array('neq', 0);
$mw['status'] = 1;
$myinfo = M('retail_sell')->where($mw)->select();
$allpaymoney = 0;
$allgetmoney = 0;
foreach ($myinfo as $k => $v) {
if ($v['is_refund'] == 1) {
$allpaymoney += $v['purch_price'] * $v['num'];
$allgetmoney += $v['allmoney'];
} else if ($v['is_refund'] == 2) {
$allpaymoney += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$allgetmoney += ($v['allmoney'] - ($v['price'] * $v['refund_num']));
}
}
$map = array();
//总优惠金额
$map['storeid'] = $storeid;
$map['paytype'] = array('neq', 0);
$map['order_type'] = 1;
$map['addtime'] = array('between', array($chaend - 86400, $chaend));
$map['refund_status'] = 0;
$map['pay_state'] = 1;
$allcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
//总门店配送费
$distribution_fee = M('retail_bill')->where($map)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
$map['refund_status'] = 2;
$map['fee_state'] = 2;
$fee_state = M('retail_bill')->where($map)->sum('distribution_fee');
$fee_state = $fee_state ? $fee_state : 0;
$distribution_fee = $distribution_fee + $fee_state;
//总达达配送费
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$cost['addtime'] = array('between', array($chaend - 86400, $chaend));
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee = $deduct_fee ? $deduct_fee : 0;
$distribution_fee_da = $fee + $deduct_fee;
$allgetmoney = round(($allgetmoney - $allcouponmoney - $small_changes_money), 2);
$profitmoney = round($allgetmoney + $distribution_fee - $allpaymoney - $distribution_fee_da, 2);
$item['allcouponmoney'] = $allcouponmoney;
$item['small_changes_money'] = $small_changes_money;
$item['allpaymoney'] = $allpaymoney == 0 ? '无销售数据' : $allpaymoney;
$item['allgetmoney'] = $allgetmoney == 0 ? '无销售数据' : $allgetmoney;
$item['profitmoney'] = $profitmoney;
$item['profitmoney'] = $item['profitmoney'] ? round($item['profitmoney'], 2) : '0.00';
$item['distribution_fee'] = $distribution_fee;
$item['distribution_fee_da'] = $distribution_fee_da;
$item['index'] = $index;
$infoarr[] = $item;
$chaend = $chaend - 86400;
$index++;
}
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $infoarr;
$rt['count'] = $count;
$this->ajaxReturn($rt);
}
}
// 收款毛利汇总
public function profitallmoney()
{
$storeid = session('storeid');
$time = I('get.date01');
if (!empty($time)) {
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
$mw['addtime'] = array('between', array($start_time, $end_time));
$cost['addtime'] = array('between', array($start_time, $end_time));
}
$where['paytype'] = array('neq', 0);
$where['status'] = 1;
$where['storeid'] = $storeid;
$info = M('retail_sell')->where($where)->select();
$allpaymoney = 0;
$allgetmoney = 0;
foreach ($info as $k => $v) {
if ($v['is_refund'] == 1) {
$allpaymoney += $v['purch_price'] * $v['num'];
$allgetmoney += $v['allmoney'];
} else if ($v['is_refund'] == 2) {
$allpaymoney += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$allgetmoney += ($v['allmoney'] - ($v['price'] * $v['refund_num']));
}
}
//总优惠金额
$mw['storeid'] = $storeid;
$mw['paytype'] = array('neq', 0);
$mw['refund_status'] = 0;
$mw['order_type'] = 1;
$mw['pay_state'] = 1;
// 所有收款
$billallmoney = M('retail_bill')->where($mw)->sum('allmoney-refund_money');
$billallmoney = $billallmoney ? round($billallmoney, 2) : '0.00';
$allcouponmoney = M('retail_bill')->where($mw)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($mw)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
//门店总收配送费
$distribution_fee = M('retail_bill')->where($mw)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
$mw['refund_status'] = 2;
$mw['fee_state'] = 2;
$fee_state = M('retail_bill')->where($mw)->sum('distribution_fee');
$fee_state = $fee_state ? $fee_state : 0;
$distribution_fee = $distribution_fee + $fee_state;
//达达的配送费$fee 扣除的费用 $deduct_fee_da取消配送扣除的费用
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee_da = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee_da = $deduct_fee_da ?: 0;
$distribution_fee_da = $fee + $deduct_fee_da;
//总销售额
$allgetmoney = $allgetmoney - $allcouponmoney - $small_changes_money;
$allgetmoney = $allgetmoney ? $allgetmoney : 0;
$allpaymoney = $allpaymoney ? $allpaymoney : 0;
$allprofitmoney = $allgetmoney + $distribution_fee - $allpaymoney - $distribution_fee_da;
$allprofitmoney = $allprofitmoney ? round($allprofitmoney, 2) : 0;
$allgetmoney = round($allgetmoney, 2);
$rt['distribution_fee_da'] = $distribution_fee_da;
$rt['distribution_fee'] = $distribution_fee;
$rt['small_changes_money'] = $small_changes_money;
$rt['allcouponmoney'] = $allcouponmoney;
$rt['allprofitmoney'] = $allprofitmoney;
$rt['allmoney'] = $allgetmoney;
$rt['billallmoney'] = $billallmoney;
$this->ajaxReturn($rt);
}
public function salechatimedetail()
{
$chatime = I('get.chatime');
$this->assign('chatime', $chatime);
$this->display();
}
public function salechatimedetaillist()
{
$storeid = session('storeid');
$chatime = I('get.chatime');
if (strpos($chatime, '至')) {
$chatime = explode('至', $chatime);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($chatime[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($chatime[1])));
} else {
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($chatime)));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($chatime)));
}
$where['paytype'] = array('neq', 0);
$where['addtime'] = array('between', array($start_time, $end_time));
$where['storeid'] = $storeid;
$info = M('retail_sell')->where($where)->order('addtime desc')->select();
$count = M('retail_sell')->where($where)->count();
$countprofit = 0;
$countallpurch = 0;
$countallprice = 0;
$countallrefund = 0;
foreach ($info as $k => $v) {
$one = M('retail_goods')->where(array('id' => $v['goods_id']))->find();
$info[$k]['good_number'] = $one['good_number'];
$info[$k]['goods_code'] = $one['goods_code'];
if ($v['is_refund'] == 2) {
$info[$k]['refundname'] = '有退款';
$profit = ($v['num'] - $v['refund_num']) * ($v['price'] - $v['purch_price']);
} else if ($v['is_refund'] == 3) {
$info[$k]['refundname'] = '已退款';
$profit = ($v['num'] - $v['refund_num']) * ($v['price'] - $v['purch_price']);
} else {
$info[$k]['refundname'] = '无退款';
$profit = $v['num'] * ($v['price'] - $v['purch_price']);
}
$allpurch = $v['purch_price'] * $v['num'];
$allprice = $v['price'] * $v['num'];
$allprice = $v['allmoney'];
$allrefund = $v['price'] * $v['refund_num'];
if ($v['is_refund'] == 1) {
$countprofit += $profit;
$countallpurch += $allpurch;
$countallprice += $allprice;
} else if ($v['is_refund'] == 2) {
$countprofit += $profit;
$countallpurch += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$countallprice += ($v['price'] * ($v['num'] - $v['refund_num']));
$countallrefund += $allrefund;
}
$info[$k]['profit'] = $profit ? round($profit, 2) : 0;
$info[$k]['allpurch'] = $allpurch ? round($allpurch, 2) : 0;
$info[$k]['allprice'] = $allprice ? round($allprice, 2) : 0;
$info[$k]['allrefund'] = $allrefund ? round($allrefund, 2) : 0;
}
//总优惠金额
$map['storeid'] = $storeid;
$map['paytype'] = array('neq', 0);
$map['order_type'] = 1;
$map['pay_state'] = 1;
$map['addtime'] = array('between', array($start_time, $end_time));
//退款成功总优惠
$map['refund_status'] = array('in', '2,3');
$refundcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$refundcouponmoney = $refundcouponmoney ? $refundcouponmoney : 0;
$small_changes_money_refund = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money_refund = $small_changes_money_refund ? $small_changes_money_refund : 0;
//优惠的金额
$map['refund_status'] = 0;
$allcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
//门店总收配送费
$distribution_fee = M('retail_bill')->where($map)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
$map['refund_status'] = 2;
$map['fee_state'] = 2;
$fee_state = M('retail_bill')->where($map)->sum('distribution_fee');
$fee_state = $fee_state ? $fee_state : 0;
$distribution_fee = $distribution_fee + $fee_state;
//门店退款时的配送费选择不退配送费
//达达的配送费$fee 扣除的费用 $deduct_fee_da取消配送扣除的费用
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$cost['addtime'] = array('between', array($start_time, $end_time));
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee_da = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee_da = $deduct_fee_da ?: 0;
$distribution_fee_da = $fee + $deduct_fee_da;
$countprofit = round(($countprofit + $distribution_fee - $allcouponmoney - $small_changes_money - $distribution_fee_da), 2);
$countallpurch = round($countallpurch, 2);
$countallprice = round($countallprice - $allcouponmoney - $small_changes_money, 2);
$countallrefund = round(($countallrefund - $refundcouponmoney - $small_changes_money_refund), 2);
$rt['code'] = 0;
$rt['msg'] = '';
$rt['data'] = $info;
$rt['count'] = $count;
$rt['moneycount'] = array(
'profit' => $countprofit,
'allpurch' => $countallpurch,
'allprice' => $countallprice,
'allrefund' => $countallrefund,
'allcouponmoney' => $allcouponmoney,
'small_changes_money' => $small_changes_money,
'distribution_fee' => $distribution_fee,
'distribution_fee_da' => $distribution_fee_da
);
$this->ajaxReturn($rt);
}
//店内收款明细导出
public function export_old()
{
$date = I('get.date'); //日期
$order_id = I('get.title'); //订单号
$person = I('get.person'); //店员
$pay = I('get.pay'); //付款方式
$type = I('get.type'); //订单类型当type为2时查看退款
$card = I('get.card'); //会员号
$storeid = session('storeid');
$where['b.storeid'] = $storeid;
$where['b.pay_state'] = 1;
$where['b.type'] = 2;//店内
if (!empty($date)) {
$time = explode('至', $date);
$start_time = strtotime(date('Y-m-d', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d', strtotime($time[1]))) + 86400;
$where['b.addtime'] = array('between', array($start_time, $end_time));
} else {
$start_time = strtotime(date('Y-m-d', time()));
$end_time = strtotime(date('Y-m-d', time())) + 86400;
$where['b.addtime'] = array('between', array($start_time, $end_time));
}
if (!empty($title)) {
$where['b.order_id'] = $order_id;
}
if (!empty($person)) {
$where['b.admin_id'] = $person;
}
if (!empty($pay)) {
$where['b.paytype'] = $pay;
}
if (!empty($type)) {
if ($type == 2) {
$where['b.order_type'] = 2;
}
}
if (!empty($card)) {
$merchantid = M('retail_store')->where(array('id' => $storeid))->getField('merchantid');
$vipmap['merchantid'] = $merchantid;
$vipmap['mobile_phone|card_key'] = $card;
$vipid = M('retail_vip')->where($vipmap)->getField('id', true);
$where['b.vipid'] = array('in', $vipid);
}
//根据条件查出收款明细
$content = M('retail_bill as b')
->join('ls_retail_cashier as c on c.id=b.admin_id', "LEFT")
->join('ls_retail_vip as v on v.id=b.vipid', "LEFT")
->where($where)->order('b.addtime desc')
->field('b.*,c.name as adminname,v.mobile_phone as vipnum,v.card_key as v_key')
->select();
if ($content) {
foreach ($content as $key => &$val) {
//序号
$val['key'] = $key + 1;
//单据类型
if ($val['type'] == 1) {
$val['type'] = '商城';
} else if ($val['type'] == 2) {
$val['type'] = '店内';
}
//支付方式
if ($val['paytype'] == 1) {
$val['payname'] = '现金';
} else if ($val['paytype'] == 2) {
$val['payname'] = '银行卡';
} else if ($val['paytype'] == 3) {
$val['payname'] = '微信支付';
} else if ($val['paytype'] == 4) {
$val['payname'] = '支付宝支付';
} else if ($val['paytype'] == 5) {
$val['payname'] = '会员卡余额支付';
} else if ($val['paytype'] == 6) {
$val['payname'] = '微信支付';
} else if ($val['paytype'] == 7) {
$val['payname'] = '其他支付方式';
}
//退款
if ($val['order_type'] == 1) {
if ($val['refund_status'] == 0) {
$val['status_name'] = '无退款';
} else {
$val['status_name'] = '有退款';
}
} else {
$val['status_name'] = '退款订单';
}
if (empty($val['v_key'])) {
$val['v_key'] = '';
}
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);//时间
}
} else {
$content = array();
}
$filename = '店内收款明细';
$headArr = array(
array('key', '序号'),
array('type', '单据类型'),
array('order_id', '单号'),
array('vipnum', '会员手机号'),
array('v_key', '会员实体卡号'),
array('payname', '付款方式'),
array('allmoney', '付款金额'),
array('addtime', '时间'),
array('exchange_money', '优惠券减免金额'),
array('dismoney', '折扣金额'),
array('small_changes_money', '抹零'),
array('adminname', '收银员'),
array('status_name', '是否退款')
);
$str = $filename . '导出时间' . date('Y-m-d H:i:s', time());
$this->getExcel($filename, $headArr, $content, $str);
}
//商品明细导出
public function particularsexport()
{
$storeid = session('storeid');
$date_time = I('get.date01');
if (!empty($date_time)) {
$time = $date_time;
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
} else {
$start_time = strtotime(date('Y-m-d 00:00:00', time()));
$end_time = $start_time + 86399;
$where['addtime'] = array('between', array($start_time, $end_time));
}
//单号
if (I('get.title') != "") {
$where['goods_number'] = I('get.title');
}
$where['paytype'] = array('neq', 0);
$where['storeid'] = $storeid;
//类别
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$where['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['class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['class_id'] = $person2 ? $person2 : $person1;
}
$sell_summary = M('retail_sell')->where($where)->order('addtime desc')->select();
foreach ($sell_summary as $key => &$val) {
$val['key'] = $key + 1;
//商品信息
$goods = M('retail_goods')->where(array('id' => $val['goods_id']))->find();
$val['goods_code'] = $goods['goods_code'] ? $goods['goods_code'] : '---';
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$val['good_name'] = $goods['goods_type'] == 2 ? '无码商品' : $goods['goods_name'];
$val['je_money'] = (string)round($val['price'] * $val['num'], 2); //金额小计
$val['type'] = $val['type'] == 1 ? '商城' : '店内';
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
if ($val['status'] == 1) {
if ($val['is_refund'] == 1) {
$val['refund'] = '销售';
} elseif ($val['is_refund'] == 2) {
$val['refund'] = '有退款';
}
} else if ($val['status'] == 2) {
$val['refund'] = '退款订单';
}
}
$filename = '商品销售明细';
$headArr = array(
array('key', '序号'),
array('type', '类型'),
array('goods_number', '货号'),
array('goods_code', '条码'),
array('good_name', '品名'),
array('purch_price', '进货价(元)'),
array('yuan_price', '原价(元)'),
array('price', '实售价(元)'),
array('num', '销售数量'),
array('refund', '订单类型'),
array('refund_num', '退货数量'),
array('addtime', '出售时间'),
array('je_money', '金额小计(元)'),
);
$str = $filename . '导出时间' . date('Y-m-d H:i:s', time());
$this->getExcel($filename, $headArr, $sell_summary, $str);
}
//汇总导出
public function saledetailsexport()
{
//获取门店信息
$storeid = session('storeid');
//查询时间
$datetime = I('get.date01');
if (!empty($datetime)) {
$time = explode('至', $datetime);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['s.addtime'] = array('between', array($start_time, $end_time));
} else {
$start_time = strtotime(date('Y-m-d 00:00:00', time()));
$end_time = $start_time + 86399;
$where['s.addtime'] = array('between', array($start_time, $end_time));
}
//货号
$goods_number = I('get.title');
if (!empty($goods_number)) {
$where['s.goods_number'] = $goods_number;
}
//类别
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$where['s.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['s.class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$where['s.class_id'] = $person2 ? $person2 : $person1;
}
$where['s.paytype'] = array('neq', 0);
$where['s.storeid'] = $storeid;
$where['s.status'] = 1;
$where['s.is_refund'] = 1;
$sell_summary = M('retail_sell as s')
->join('ls_retail_goods as g on g.id=s.goods_id', 'LEFT')
->where($where)
->group('s.goods_id')
->field('s.*,g.goods_name,g.goods_code,g.goods_num,g.retail_price,g.purch_price')
->order('s.addtime desc')->select();
if (!empty($sell_summary)) {
$map = array(
'storeid' => $storeid,
'paytype' => array('neq', 0),
'status' => 1,
'is_refund' => 1
);
if ($start_time && $end_time) {
$map['addtime'] = array('between', array($start_time, $end_time));
}
foreach ($sell_summary as $key => &$val) {
if ($val['goods_type'] == 2 && $val['goods_id'] == 0) {
$map['goods_id'] = 0;
$val['goods_name'] = '无码商品';
$val['goods_num'] = 0;
$val['retail_price'] = $val['price'];
$val['goods_code'] = '---';
$val['goods_number'] = '';
$num = M('retail_sell')->where($map)->sum('num');//销量
$money = M('retail_sell')->where($map)->sum('allmoney');//销量
$money = $money ? $money : 0;
$val['retail_price'] = round($money / $num, 2);
$val['yuan_price'] = $val['retail_price'];
$val['purch_price'] = $val['retail_price'];
} else {
$map['goods_id'] = $val['goods_id'];
$goods = M('retail_goods')->where(array('id' => $val['goods_id']))->find();
$val['goods_code'] = $goods['goods_code'] ? $goods['goods_code'] : '---';
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$num = M('retail_sell')->where($map)->sum('num');//销量
$money = M('retail_sell')->where($map)->sum('allmoney');//金额小计
}
$val['key'] = $key + 1;
$val['xs_num'] = $num ? round($num,3) : 0;
$val['je_money'] = $money ? round($money,2) : 0;
}
} else {
$sell_summary = array();
}
$filename = "商品销售汇总";
$str = $filename . "导出时间:" . date('Y-m-d H:i:s', time());
$headArr = array(
array('key', '序号'),
array('goods_number', '货号'),
array('goods_code', '条码'),
array('goods_name', '品名'),
array('purch_price', '进货价(元)'),
array('retail_price', '零售价(元)'),
array('xs_num', '销售数量'),
array('goods_num', '库存'),
array('je_money', '金额小计'),
);
$this->getExcel($filename, $headArr, $sell_summary, $str);
}
//滞销商品导出
public function unsalableexport()
{
//获取门店信息
$storeid = session('storeid');
//出售的商品
$start = strtotime("-1 month");
$end = time();
$where['paytype'] = array('neq', 0);
$where['storeid'] = $storeid;
$where['status'] = 1;
$where['is_refund'] = 1;
$where['addtime'] = array('between', array($start, $end));
$dead_info = M('retail_sell')->where($where)->group('goods_id')->getField('goods_id', true);
$dead_info = $dead_info ? $dead_info : array(0);
//所有商品
//查询条件货号
$goodsnum = I('get.name02');
if (!empty($goodsnum)) {
$data['good_number'] = $goodsnum;
}
//查询品名
$product = I('get.name03');
if (!empty($product)) {
$data['goods_name'] = array('like', array("%" . $product . "%"));
}
$data['storeid'] = $storeid;
$data['status'] = 0;
$data['id'] = array('not in', $dead_info);
$good_info = M('retail_goods')->where($data)->order('id desc')->select();
$count = M('retail_goods')->where($data)->count();
if (!empty($good_info)) {
foreach ($good_info as $key => &$val) {
$val['key'] = $key + 1;
//销量
$volume = M('retail_sell')->where(array('goods_id' => $val['id'], 'status' => 1, 'paytype' => array('neq', 0), 'is_refund' => 1))->sum('num');
$val['saleVol'] = $volume ? $volume : 0;
$val['goods_code'] = $val['goods_code'] ? $val['goods_code'] : '---';
$val['good_number'] = $val['good_number'] ? $val['good_number'] : '---';
}
} else {
$good_info = array();
}
$filename = "滞销商品";
$str = $filename . "导出时间:" . date('Y-m-d H:i:s', time());
$headArr = array(
array('key', '序号'),
array('good_number', '货号'),
array('goods_code', '商品条码'),
array('goods_name', '品名'),
array('saleVol', '总销量'),
);
$this->getExcel($filename, $headArr, $good_info, $str);
}
//畅销导出
public function Bestexport()
{
$storeid = session('storeid');
//按照出售商品销量排序
//查询条件货号
$number = I('get.name02');
if (!empty($number)) {
$where['goods_number'] = $number;
}
$product = I('get.name03');
//查询品名
if (!empty($product)) {
$where['goods_name'] = array('like', '%' . $product . '%');
}
$start = strtotime("-1 month");
$end = time();
$where['addtime'] = array('between', array($start, $end));
$where['storeid'] = $storeid;
$where['paytype'] = array('neq', 0);
$where['status'] = 1;
$where['is_refund'] = 1;
$count = M('retail_sell')->where($where)->group('goods_id')->select();
$count = count($count);
$best_sell = M('retail_sell')->where($where)->group('goods_id')->order('id desc')->select();
if (!empty($best_sell)) {
foreach ($best_sell as $key => &$val) {
$val['goods_number'] = $val['goods_number'] ? $val['goods_number'] : '---';
$val['key'] = $key + 1;
$val['goods_code'] = M('retail_goods')->where(array('id' => $val['goods_id']))->getField('goods_code');
$val['goods_code'] = $val['goods_code'] ? $val['goods_code'] : '---';
$val['good_name'] = M('retail_goods')->where(array('id' => $val['goods_id']))->getField('goods_name');
//30天销量
$where['goods_id'] = $val['goods_id'];
$val['ynum'] = M('retail_sell')->where($where)->sum('num');
//总销量
$data['storeid'] = $storeid;
$data['paytype'] = array('neq', 0);
$data['status'] = 1;
$data['goods_id'] = $val['goods_id'];
$data['is_refund'] = 1;
//查询条件货号
if (!empty($number)) {
$data['goods_number'] = $number;
}
//查询品名
if (!empty($product)) {
$data['goods_name'] = array('like', '%' . $product . '%');
}
$val['znum'] = M('retail_sell')->where($data)->sum('num');
}
} else {
$best_sell = array();
}
$filename = "畅销商品";
$str = $filename . "导出时间:" . date('Y-m-d H:i:s', time());
$headArr = array(
array('key', '序号'),
array('goods_number', '货号'),
array('goods_code', '商品条码'),
array('good_name', '品名'),
array('ynum', '近30天销量'),
array('znum', '总销量'),
);
$this->getExcel($filename, $headArr, $best_sell, $str);
}
//销售毛利润导出
public function profitexport()
{
$storeid = session('storeid');
$date = I('get.date01');
if (!empty($date)) {
$time = I('get.date01');
$time = explode('至', $time);
$start_time = strtotime(date('Y-m-d 0:0:0', strtotime($time[0])));
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime($time[1])));
$where['addtime'] = array('between', array($start_time, $end_time));
$rt['code'] = 0;
$count = 1;
$where['paytype'] = array('neq', 0);
$where['status'] = 1;
$where['storeid'] = $storeid;
$myinfo = M('retail_sell')->where(array('storeid' => $storeid))->where($where)->select();
$allpaymoney = 0;
$allgetmoney = 0;
foreach ($myinfo as $k => $v) {
if ($v['is_refund'] == 1) {
$allpaymoney += $v['purch_price'] * $v['num'];
$allgetmoney += $v['allmoney'];
} else if ($v['is_refund'] == 2) {
$allpaymoney += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$allgetmoney += ($v['allmoney'] - ($v['price'] * $v['refund_num']));
}
}
//总优惠金额
$map['storeid'] = $storeid;
$map['paytype'] = array('neq', 0);
$map['refund_status'] = 0;
$map['order_type'] = 1;
$map['addtime'] = array('between', array($start_time, $end_time));
$allcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
$allgetmoney = $allgetmoney - $allcouponmoney - $small_changes_money;
//总门店配送费
$distribution_fee = M('retail_bill')->where($map)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
$map['refund_status'] = 2;
$map['fee_state'] = 2;
$fee_state = M('retail_bill')->where($map)->sum('distribution_fee');
$fee_state = $fee_state ? $fee_state : 0;
$distribution_fee = $distribution_fee + $fee_state;
//达达配送费
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$cost['addtime'] = array('between', array($start_time, $end_time));
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee = $deduct_fee ? $deduct_fee : 0;
$distribution_fee_da = $deduct_fee + $fee;
$profitmoney = round($allgetmoney + $distribution_fee - $allpaymoney - $distribution_fee_da, 2);
$item['allcouponmoney'] = $allcouponmoney;
$item['small_changes_money'] = $small_changes_money;
$item['allpaymoney'] = $allpaymoney == 0 ? '无销售数据' : $allpaymoney;
$item['allgetmoney'] = $allgetmoney == 0 ? '无销售数据' : $allgetmoney;
$item['profitmoney'] = $profitmoney;
$item['distribution_fee_da'] = $distribution_fee_da;
$item['distribution_fee'] = $distribution_fee;
$item['index'] = 1;
$item['chatime'] = I('get.date01');
$infoarr[] = $item;
} else {
// 查询最早的一条出售记录
$starttime = M('retail_sell')->where(array('storeid' => $storeid, 'status' => 1))->order('addtime asc')->getField('addtime');
if (!$starttime) {
$infoarr = array();
} else {
$start = strtotime(date('Y-m-d 00:00:00', time()));
$end = $start + 86399;
$item = array();
$item['chatime'] = date('Y-m-d');
$mw['storeid'] = $storeid;
$mw['addtime'] = array('between', array($start, $end));
$mw['paytype'] = array('neq', 0);
$mw['status'] = 1;
$myinfo = M('retail_sell')->where($mw)->select();
$allpaymoney = 0;
$allgetmoney = 0;
foreach ($myinfo as $k => $v) {
if ($v['is_refund'] == 1) {
$allpaymoney += $v['purch_price'] * $v['num'];
$allgetmoney += $v['allmoney'];
} else if ($v['is_refund'] == 2) {
$allpaymoney += ($v['purch_price'] * ($v['num'] - $v['refund_num']));
$allgetmoney += ($v['allmoney'] - ($v['price'] * $v['refund_num']));
}
}
$item['index'] = 1;
//总优惠金额
$map['storeid'] = $storeid;
$map['paytype'] = array('neq', 0);
$map['refund_status'] = 0;
$map['order_type'] = 1;
$map['addtime'] = array('between', array($start, $end));
$allcouponmoney = M('retail_bill')->where($map)->sum('exchange_money');
$allcouponmoney = $allcouponmoney ? $allcouponmoney : '0.00';
//总抹零
$small_changes_money = M('retail_bill')->where($map)->sum('small_changes_money');
$small_changes_money = $small_changes_money ? $small_changes_money : '0.00';
//总门店配送费
$distribution_fee = M('retail_bill')->where($map)->sum('distribution_fee');
$distribution_fee = $distribution_fee ? $distribution_fee : 0;
//达达配送费
$cost['storeid'] = $storeid;
$cost['status'] = 2;
$cost['addtime'] = array('between', array($start, $end));
$fee = M('retail_da_cost')->where($cost)->sum('fee');
$fee = $fee ? $fee : 0;
$cost['status'] = 1;
$deduct_fee = M('retail_da_cost')->where($cost)->sum('deduct_fee');
$deduct_fee = $deduct_fee ? $deduct_fee : 0;
$distribution_fee_da = $deduct_fee + $fee;
$allgetmoney = $allgetmoney - $allcouponmoney - $small_changes_money;
$profitmoney = round($allgetmoney + $distribution_fee - $allpaymoney - $distribution_fee_da, 2);
$item['allcouponmoney'] = $allcouponmoney;
$item['small_changes_money'] = $small_changes_money;
$item['allpaymoney'] = $allpaymoney == 0 ? '无销售数据' : $allpaymoney;
$item['allgetmoney'] = $allgetmoney == 0 ? '无销售数据' : $allgetmoney;
$item['profitmoney'] = $profitmoney;
$item['distribution_fee'] = $distribution_fee;
$item['distribution_fee_da'] = $distribution_fee_da;
$infoarr[] = $item;
}
}
$filename = "销售毛利润";
$str = $filename . "导出时间:" . date('Y-m-d H:i:s', time());
$headArr = array(
array('index', '序号'),
array('chatime', '日期'),
array('allpaymoney', '总成本(元)'),
array('allgetmoney', '总销售金额(元)'),
array('distribution_fee', '总门店配送金额(元)'),
array('distribution_fee_da', '总达达配送金额(元)'),
array('allcouponmoney', '总优惠金额(元)'),
array('small_changes_money', '总抹零金额(元)'),
array('profitmoney', '毛利(元)'),
);
$this->getExcel($filename, $headArr, $infoarr, $str);
}
//导出方法
public 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 gross_margin()
{
$storeid = session('storeid');
if (!$storeid) {
$this->error('您无权操作');
exit;
}
$merchantid = M('retail_store')->where(array('id' => $storeid))->getField('merchantid'); // 商户id
$goods_class = M('retail_goods_class')->where(array('merchantid' => $merchantid, 'status' => 0, 'type' => 1))->order('code asc')->select(); // 分类
$supplier = M("retail_supplier")->where(array('merchantid' => $merchantid, 'status' => 1))->select(); // 供货商
$goods_brand = M('retail_goods_brand')->where(array('merchantid' => $merchantid, 'state' => 0))->select(); // 品牌
$this->assign('goods_class', $goods_class);
$this->assign('supplier', $supplier);
$this->assign('goods_brand', $goods_brand);
$this->display();
}
/* 获取销售数据 */
public function get_sell_list()
{
$storeid = session('storeid');
if (!$storeid) {
$this->error('您无权操作');
exit;
}
$merchantid = M('retail_store')->where(array('id' => $storeid))->getField('merchantid'); // 商户id
$global_store_jurisdiction = I('session.global_store_jurisdiction'); // 权限
$showpurch_price = $global_store_jurisdiction['showpurch_price'] ? $global_store_jurisdiction['showpurch_price'] : 2; // 查看进价权限
// 商品名称搜索
$keyword = I('get.keyword');
if (!empty($keyword)) {
$keyword = trim($keyword, ' ');
$map['g.goods_name'] = array('like', '%' . $keyword . '%');
}
// 供货商搜索
$supplier_id = I('get.supplier_id');
if (!empty($supplier_id)) {
$map['g.supplier_id'] = trim($supplier_id, '');
}
// 品牌搜索
$goods_brand_id = I('get.goods_brand_id');
if (!empty($goods_brand_id)) {
$map['g.goods_brand_id'] = trim($goods_brand_id, '');
}
// 时间搜索
$start_time = I('get.start_time');
$end_time = I('get.end_time');
if (!empty($start_time) && !empty($end_time)) {
$start_time = strtotime($start_time);
$end_time = strtotime($end_time . '23:59:59');
$map['s.addtime'] = array('between', array($start_time, $end_time));
}
if (!empty($start_time) && empty($end_time)) {
$start_time = strtotime($start_time);
$map['s.addtime'] = array('egt', $start_time);
}
if (empty($start_time) && !empty($end_time)) {
$end_time = strtotime(($end_time . '23:59:59'));
$map['s.addtime'] = array('elt', $end_time);
}
// 按照商品分类搜索
$person1 = I('get.person1');
$person2 = I('get.person2');
$person3 = I('get.person3');
$flag = 0;
if (!empty($person3)) {
$map['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);
$map['g.goods_class_id'] = array('in', $str . ',' . ($person2 ? $person2 : $person1));
} else if ($flag == 1) {
$map['g.goods_class_id'] = $person2 ? $person2 : $person1;
}
$map['s.paytype'] = array('neq', 0);
$map['s.merchantid'] = $merchantid;
$map['s.storeid'] = $storeid;
$sell_list = M('retail_sell as s')
->join('ls_retail_goods as g on g.id=s.goods_id')
->field('s.id,s.price,s.num,s.allmoney,s.paytype,s.type,s.purch_price,s.yuan_price,s.discount,s.addtime,s.skuinfo,s.status,s.sku_json_info,g.goods_name,g.supplier_id,goods_brand_id,g.goods_class_id')
->page($_GET['page'], $_GET['limit'])
->where($map)
->order('id desc')
->select();
$count = M('retail_sell as s')
->join('ls_retail_goods as g on g.id=s.goods_id')
->where($map)
->count();
if ($sell_list) {
foreach ($sell_list as $key => &$val) {
$val['index'] = ++$key;
$val['num'] = intval($val['num']) == $val['num'] ? intval($val['num']) : $val['num'];
$val['addtime'] = date('Y-m-d', $val['addtime']);
if ($val['sku_json_info']) {
$sku = json_decode($val['sku_json_info'], true);
$val['yuan_price'] = $sku['retail_price'] ? $sku['retail_price'] : $sku['actprice'];
$val['sku_name'] = $sku['name'];
}
if ($val['skuinfo']) {
$sku = json_decode($val['skuinfo'], true);
$val['yuan_price'] = $sku[0]['retail_price'] ? $sku[0]['retail_price'] : $sku[0]['actprice'];
$val['sku_name'] = $sku[0]['sku'];
}
switch ($val['paytype']) {
case 1:
$val['pay_type'] = '现金支付';
break;
case 2:
$val['pay_type'] = '银行卡支付';
break;
case 3:
$val['pay_type'] = '微信支付';
break;
case 4:
$val['pay_type'] = '支付宝支付';
break;
case 5:
$val['pay_type'] = '余额支付';
break;
case 6:
$val['pay_type'] = '微信支付';
break;
case 7:
$val['pay_type'] = '其他支付';
break;
}
if ($showpurch_price == 1) {
$val['profit'] = round($val['allmoney'] - ($val['purch_price'] * $val['num']), 2);
$val['profit_margin'] = round(($val['profit'] * 100) / $val['allmoney'], 2) . '%';
} else {
$val['purch_price'] = '*';
$val['profit'] = '*';
$val['profit_margin'] = '*';
}
$val['discount'] = $val['discount'] == 1 ? '' : $val['discount']; // 折扣
$val['class'] = M('retail_goods_class')->where(array('id' => $val['goods_class_id'], 'merchantid' => $merchantid, 'status' => 0))->getField('class_name');
$val['supplier'] = M('retail_supplier')->where(array('id' => $val['supplier_id'], 'merchantid' => $merchantid, 'status' => 1))->getField('supplier_name');
$val['brand'] = M('retail_goods_brand')->where(array('id' => $val['goods_brand_id'], 'merchantid' => $merchantid, 'state' => 0))->getField('name');
$val['sell_type'] = $val['type'] == 1 ? '商城' : '店内';
$val['sell_status'] = $val['status'] == 1 ? '购买' : '退款';
}
}
$res = $this->statistics_sell($map);
$res['code'] = 0;
$res['count'] = $count ? $count : 0;
$res['data'] = $sell_list;
$this->ajaxReturn($res);
}
/* 统计总金额 总成本 总利润 */
protected function statistics_sell($map)
{
$global_store_jurisdiction = I('session.global_store_jurisdiction'); // 权限
$showpurch_price = $global_store_jurisdiction['showpurch_price'] ? $global_store_jurisdiction['showpurch_price'] : 2; // 查看进价权限
$sell_data = M('retail_sell as s')
->join('ls_retail_goods as g on g.id=s.goods_id')
->field('s.id,s.num,s.allmoney,s.type,s.purch_price,s.yuan_price,s.discount,s.addtime,s.skuinfo,s.status,s.sku_json_info')
->where($map)
->select();
if ($sell_data) {
$sell_count = 0;
$refund_count = 0;
$all_purch_price = 0; // 总进价
$all_sell_money = 0; // 总销售金额
foreach ($sell_data as &$value) {
if ($value['status'] == 1) {
$all_purch_price += $value['purch_price'] * $value['num'];
$all_sell_money += $value['allmoney'];
++$sell_count;
} else {
$all_purch_price -= $value['purch_price'] * $value['num'];
$all_sell_money -= $value['allmoney'];
++$refund_count;
}
}
$all_sell_money = $all_sell_money ? round($all_sell_money, 2) : 0;
if ($showpurch_price == 1) {
$all_purch_price = $all_purch_price ? round($all_purch_price, 2) : 0;
$all_profit = round($all_sell_money - $all_purch_price, 2);
$all_profit_margin = round(($all_profit * 100) / $all_sell_money, 2) . '%';
} else {
$all_purch_price = '*';
$all_profit = '*';
$all_profit_margin = '*';
}
}
$res['sell_count'] = $sell_count ? $sell_count : 0;
$res['refund_count'] = $refund_count ? $refund_count : 0;
$res['all_sell_money'] = $all_sell_money ? $all_sell_money : 0;
$res['all_purch_price'] = $all_purch_price ? $all_purch_price : 0;
$res['all_profit'] = $all_profit ? $all_profit : 0;
$res['all_profit_margin'] = $all_profit_margin ? $all_profit_margin : '0%';
return $res;
}
/* 店内收款明细 */
public function get_store_sell_list()
{
$storeid = I('session.storeid');
/* 时间查询 */
$start_date = I('get.start_date');
$end_date = I('get.end_date');
if(I('get.user_type')==1){
$map['b.vipid'] = array('gt',0);
}else if(I('get.user_type')==2){
$map['b.vipid'] = array(array('exp','IS NULL'),0,'OR');
}
if($start_date && $end_date){
$start_date = strtotime($start_date);
$end_date = strtotime($end_date . '23:59:59');
$map['b.addtime'] = array('between',array($start_date,$end_date));
}else if($start_date && !$end_date){
$start_date = strtotime($start_date);
$map['b.addtime'] = array('gt',$start_date);
}else if(!$start_date && $end_date){
$end_date = strtotime($end_date . '23:59:59');
$map['b.addtime'] = array('lt',$end_date);
}
/* 订单号搜索 */
$order_id = I('get.order_id');
if($order_id){
$map['b.order_id'] = $order_id;
}
/* 会员号搜索 */
$member_number = I('get.member_number');
if($member_number){
$map['v.mobile_phone|v.card_key|v.cardnum'] = array('like','%'.$member_number.'%');
}
/* 收银员搜索 */
$admin_id = I('get.admin_id');
if($admin_id){
$map['b.admin_id'] = $admin_id;
}
/* 支付方式搜索 */
$pay_type = I('get.pay_type');
if($pay_type){
$map['b.paytype'] = $pay_type;
}
/* 订单类型 */
$order_type = I('get.order_type');
if($order_type){
$map['b.order_type'] = $order_type;
}
$map['b.pay_state'] = 1;
$map['b.type'] = 2;
$map['b.storeid'] = $storeid;
$sell_list = M('retail_bill as b')
->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')
->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')
->field('b.*,v.name as vip_name,v.nickname,v.mobile_phone as vip_mobile,v.card_key as v_key,c.name as adminname')
->where($map)
->order('b.addtime desc')
->page((int)I('get.page'), (int)I('get.limit'))
->select();
$m = M()->_sql();
$count = M('retail_bill as b')
->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')
->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')
->where($map)->count();
$res = $this->statistics_money($map);
$res['m'] = $m;
foreach ($sell_list as $key=>&$val) {
$val['index'] = $key + 1;
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
$total_discount = round($val['exchange_money'] + $val['cashierdiscountmoney'] + $val['dismoney'] + $val['small_changes_money'],2); // 总减少金额
$val['type_name'] = $val['type'] == 1 ? '商城' : '店内';
$val['allmoney'] = floatval($val['allmoney']) . '(元)'; // 消费金额
$val['discount'] = $val['discount'] >= 1 ? '无折扣' : floatval($val['discount']) . ' 折'; // 手动折扣
$val['exchange_money'] = $val['exchange_money'] && $val['exchange_money'] > 0 ? floatval($val['exchange_money']) . '(元)': '无优惠'; // 优惠券金额
$val['cashierdiscountmoney'] = $val['cashierdiscountmoney'] && $val['cashierdiscountmoney'] > 0 ? floatval($val['cashierdiscountmoney']) . '(元)': '无优惠'; // 手动折扣金额
$val['dismoney'] = $val['dismoney'] && $val['dismoney'] > 0 ? floatval($val['dismoney']) . '(元)' : '无优惠';
$val['small_changes_money'] = $val['small_changes_money'] && $val['small_changes_money'] > 0 ? floatval($val['small_changes_money']) . '(元)': '未抹零'; // 手动折扣金额
$val['total_discount'] = $total_discount ? $total_discount . '(元)' : '无优惠';
if($val['vipid']){ // 会员信息
$val['vip_name'] = $val['vip_name'] ? $val['vip_name'] : ($val['nickname'] ? $val['nickname'] : '会员');
$val['vip_mobile'] = $val['vip_mobile'] ? $val['vip_mobile'] : '未设置';
$val['card_key'] = $val['v_key'] ? $val['v_key'] : '未绑定';
}else{
$val['vip_name'] = '散客';
$val['vip_mobile'] = '未设置';
$val['card_key'] = '未绑定';
}
switch ($val['paytype']){
case 1:
$val['pay_name'] = '现金支付';
break;
case 2:
$val['pay_name'] = '银行卡支付';
break;
case 3:
$val['pay_name'] = '微信支付';
break;
case 4:
$val['pay_name'] = '支付宝支付';
break;
case 5:
$val['pay_name'] = '余额支付';
break;
case 6:
$val['pay_name'] = '微信支付';
break;
case 7:
$val['pay_name'] = '其他支付';
break;
default:
$val['pay_name'] = '其他支付';
}
/* 订单类型 */
if ($val['order_type'] == 2) {
$val['status_name'] = '退款订单';
} else {
if ($val['refund_status'] == 3) {
$val['status_name'] = '有退款';
} else {
$val['status_name'] = '购买订单';
}
}
/* 导购员 */
if($val['guideid']){
$guide_name = M('retail_guide')->where(array('id'=>$val['guideid']))->getField('guidename');
$val['guide_name'] = $guide_name ? $guide_name : '自购';
}else{
$val['guide_name'] = '自购';
}
/* 会员折扣 */
if($val['vipdiscount']){
$val['vipdiscount'] = floatval($val['vipdiscount']);
$val['dismoney'] = $val['dismoney'] ? floatval($val['dismoney']): 0;
}
}
$res['code'] = 0;
$res['data'] = $sell_list;
$res['count'] = $count;
$this->ajaxReturn($res);
}
/*
* 支付方式
*
*/
public function payTypeShow()
{
$id = I('get.id');
$where['bill_id'] = $id;
$where['order_state'] = 'SUCCESS';
$field = "pay_type,trade_amount,other_pay_id";
$info = D('PayOrder')->getSelect($where,$field);
if($info){
$count = D('PayOrder')->getCount($where);
foreach ($info as $key=>&$val){
switch ($val['pay_type']){
case 1:
$val['pay_name'] = '微信';
break;
case 2:
$val['pay_name'] = '支付宝';
break;
case 3:
$val['pay_name'] = '现金';
break;
case 4:
$val['pay_name'] = '小程序微信';
break;
case 5:
$val['pay_name'] = M('retail_other_pay')->where(array('id'=>$val['other_pay_id']))->getField("name");
$val['pay_name'] = $val['pay_name']?$val['pay_name']:'其他支付';
break;
case 6:
$val['pay_name'] = '余额支付';
break;
default:
$val['pay_name'] = '其他支付';
break;
}
}
}
$res['code'] = 0;
$res['msg'] = '';
$res['data'] = $info?$info:array();
$res['count'] = $count?$count:0;
$this->ajaxReturn($res);
}
/* 店内收款明细统计金额 */
public function statistics_money($map)
{
/* 销售 */
$map['b.order_type'] = 1;
/* 销售总数 */
$sell_count = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->count();
/* 销售金额 */
$sell_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.allmoney');
/* 优惠券金额 */
$sell_exchange_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.exchange_money');
/* 会员折扣金额 */
$sell_dismoney = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.dismoney');
/* 手动折扣金额 */
$sell_cashierdiscountmoney = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.cashierdiscountmoney');
/* 抹零金额 */
$sell_small_changes_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.small_changes_money');
/* 退款 */
$map['b.order_type'] = 2;
/* 退款总数 */
$refund_count = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->count();
/* 退款金额 */
$refund_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.allmoney');
/* 退款优惠券金额 */
$refund_exchange_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.exchange_money');
/* 退款会员折扣金额 */
$refund_dismoney = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.dismoney');
/* 退款手动折扣金额 */
$refund_cashierdiscountmoney = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.cashierdiscountmoney');
/* 退款抹零金额 */
$refund_small_changes_money = M('retail_bill as b')->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')->where($map)->sum('b.small_changes_money');
/* 实际优惠券金额 */
$exchange_money = round($sell_exchange_money - $refund_exchange_money, 2);
/* 实际会员折扣金额 */
$dismoney = round($sell_dismoney - $refund_dismoney, 2);
/* 实际手动折扣金额 */
$cashierdiscountmoney = round($sell_cashierdiscountmoney - $refund_cashierdiscountmoney, 2);
/* 实际抹零金额 */
$small_changes_money = round($sell_small_changes_money - $refund_small_changes_money,2);
/* 实售金额 */
$money = round($sell_money - $refund_money, 2);
$res['sell_count'] = $sell_count ? $sell_count . '(笔)' : 0 . '(笔)';
$res['refund_count'] = $refund_count ? $refund_count . '(笔)' : 0 . '(笔)';
$res['exchange_money'] = $exchange_money ? $exchange_money . '(元)' : 0 . '(元)';
$res['dismoney'] = $dismoney ? $dismoney . '(元)' : 0 . '(元)';
$res['cashierdiscountmoney'] = $cashierdiscountmoney ? $cashierdiscountmoney . '(元)' : 0 . '(元)';
$res['small_changes_money'] = $small_changes_money ? $small_changes_money . '(元)' : 0 . '(元)';
$res['sell_money'] = $sell_money ? $sell_money . '(元)' : 0 . '(元)';
$res['refund_money'] = $refund_money ? $refund_money . '(元)' : 0 . '(元)';
$res['money'] = $money ? $money . '(元)' : 0 . '(元)';
return $res;
}
/* 店内收款明细导出 */
public function export()
{
$storeid = I('session.storeid');
/* 时间查询 */
$start_date = I('get.start_date');
$end_date = I('get.end_date');
if($start_date && $end_date){
$start_date = strtotime($start_date);
$end_date = strtotime($end_date . '23:59:59');
$map['b.addtime'] = array('between',array($start_date,$end_date));
}else if($start_date && !$end_date){
$start_date = strtotime($start_date);
$map['b.addtime'] = array('gt',$start_date);
}else if(!$start_date && $end_date){
$end_date = strtotime($end_date . '23:59:59');
$map['b.addtime'] = array('lt',$end_date);
}else{
$start_date = strtotime(date('Y-m-d', time()));
$end_date = strtotime(date('Y-m-d', time()) . '23:59:59');
$map['b.addtime'] = array('between', array($start_date, $end_date));
}
/* 订单号搜索 */
$order_id = I('get.order_id');
if($order_id){
$map['b.order_id'] = $order_id;
}
/* 会员号搜索 */
$member_number = I('get.member_number');
if($member_number){
$map['v.mobile_phone|v.card_key|v.cardnum'] = array('like','%'.$member_number.'%');
}
if(I('get.user_type')==1){
$map['b.vipid'] = array('gt',0);
}else if(I('get.user_type')==2){
$map['b.vipid'] = array(array('exp','IS NULL'),0,'OR');
}
/* 收银员搜索 */
$admin_id = I('get.admin_id');
if($admin_id){
$map['b.admin_id'] = $admin_id;
}
/* 支付方式搜索 */
$pay_type = I('get.pay_type');
if($pay_type){
$map['b.paytype'] = $pay_type;
}
/* 订单类型 */
$order_type = I('get.order_type');
if($order_type){
$map['b.order_type'] = $order_type;
}
$map['b.pay_state'] = 1;
$map['b.type'] = 2;
$map['b.storeid'] = $storeid;
$sell_list = M('retail_bill as b')
->join('ls_retail_cashier as c on c.id=b.admin_id', 'LEFT')
->join('ls_retail_vip as v on v.id=b.vipid', 'LEFT')
->field('b.*,v.name as vip_name,v.nickname,v.mobile_phone as vip_mobile,v.card_key as v_key,c.name as adminname')
->where($map)
->order('b.addtime desc')
->select();
foreach ($sell_list as $key=>&$val) {
$val['index'] = $key + 1;
$val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
$total_discount = round($val['exchange_money'] + $val['cashierdiscountmoney'] + $val['dismoney'] + $val['small_changes_money'],2); // 总减少金额
$val['type_name'] = $val['type'] == 1 ? '商城' : '店内';
$val['allmoney'] = floatval($val['allmoney']) . '(元)'; // 消费金额
$val['discount'] = $val['discount'] >= 1 ? '无折扣' : floatval($val['discount']) . ' 折'; // 手动折扣
$val['exchange_money'] = $val['exchange_money'] && $val['exchange_money'] > 0 ? floatval($val['exchange_money']) . '(元)': '无优惠'; // 优惠券金额
$val['cashierdiscountmoney'] = $val['cashierdiscountmoney'] && $val['cashierdiscountmoney'] > 0 ? floatval($val['cashierdiscountmoney']) . '(元)': '无优惠'; // 手动折扣金额
$val['dismoney'] = $val['dismoney'] && $val['dismoney'] > 0 ? floatval($val['dismoney']) . '(元)' : '无优惠';
$val['small_changes_money'] = $val['small_changes_money'] && $val['small_changes_money'] > 0 ? floatval($val['small_changes_money']) . '(元)': '未抹零'; // 手动折扣金额
$val['total_discount'] = $total_discount ? $total_discount . '(元)' : '无优惠';
if($val['vipid']){ // 会员信息
$val['vip_name'] = $val['vip_name'] ? $val['vip_name'] : ($val['nickname'] ? $val['nickname'] : '会员');
$val['vip_mobile'] = $val['vip_mobile'] ? $val['vip_mobile'] : '未设置';
$val['card_key'] = $val['card_key'] ? $val['card_key'] : '未绑定';
}else{
$val['vip_name'] = '散客';
$val['vip_mobile'] = '未设置';
$val['card_key'] = '未绑定';
}
switch ($val['paytype']){
case 1:
$val['pay_name'] = '现金支付';
break;
case 2:
$val['pay_name'] = '银行卡支付';
break;
case 3:
$val['pay_name'] = '微信支付';
break;
case 4:
$val['pay_name'] = '支付宝支付';
break;
case 5:
$val['pay_name'] = '余额支付';
break;
case 6:
$val['pay_name'] = '微信支付';
break;
case 7:
$val['pay_name'] = '其他支付';
break;
default:
$val['pay_name'] = '其他支付';
}
/* 订单类型 */
if ($val['order_type'] == 2) {
$val['status_name'] = '退款订单';
} else {
if ($val['refund_status'] == 3) {
$val['status_name'] = '有退款';
} else {
$val['status_name'] = '购买订单';
}
}
/* 导购员 */
if($val['guideid']){
$guide_name = M('retail_guide')->where(array('id'=>$val['guideid']))->getField('guidename');
$val['guide_name'] = $guide_name ? $guide_name : '自购';
}else{
$val['guide_name'] = '自购';
}
/* 会员折扣 */
if($val['vipdiscount']){
$val['vipdiscount'] = floatval($val['vipdiscount']);
$val['dismoney'] = $val['dismoney'] ? floatval($val['dismoney']): 0;
}
}
$name = '店内收款明细';
$headArr = array(
array('index', '序号'),
array('type_name', '订单类型'),
array('order_id', '订单号'),
array('vip_name', '消费对象'),
array('vip_mobile', '会员手机号'),
array('card_key', '实体卡号'),
array('allmoney', '支付金额'),
array('pay_name', '支付类型'),
array('addtime', '消费时间'),
array('adminname', '收银员'),
array('guide_name', '导购员'),
array('exchange_money', '优惠券减免'),
array('discount', '手动折扣'),
array('cashierdiscountmoney', '手动折扣优惠'),
array('dismoney', '会员价及会员折扣优惠'),
array('small_changes_money', '抹零'),
array('total_discount', '总优惠'),
array('status_name', '订单类型'),
);
$str = $name . '导出时间' . date('Y-m-d H:i:s', time());
$this->getExcel($name, $headArr, $sell_list, $str);
}
/*
* 打印
*/
public function storeIdGet()
{
$store_id = session("storeid");
$order_id = I('post.order_id');
if(!$store_id || !$order_id){
$res['status'] = 0;
$res['msg'] = '登录超时,请刷新重试';
$this->ajaxReturn($res);
}
A('Sellapi/Printer')->linePrinting($store_id,$order_id);
}
}