文件
mashangyoujiu/Application/Common/Business/Sell.class.php

393 行
15 KiB
PHP
可执行文件

<?php
namespace Common\Business;
use Common\Model\RetailGoodsModel;
use Common\Model\RetailSellModel;
use Common\Model\SellSerialModel;
use Common\Model\SWarehouseGoodsSerialModel;
//[1.8.73]
/**
* sell明细类
* Class Sell
* [
* 根据订单号查询出售记录id
* 销售汇总 sellSummary
* 出售记录 sellList
* 配送或取消配送时商品信息验证 distributionGoodsVerify
* 出货确认商品 confirmSellGoods
* 出货时的商品串号验证 sellGoodsSerialVerify
* 订单优惠标记的商品信息 getGoodsTag
* ]
* @package Common\Business
*/
class Sell
{
/**
* 销售汇总
* @param $param
* [
* merchant_id
* store_id
* time 时间
* person1 一级分类
* person2 二级分类
* person3 三级分类
* search_info 货号或条码
* ]
* @return mixed
*/
public function sellSummary($param)
{
$where = [
's.merchant_id' => $param['merchant_id'],
's.store_id' => $param['store_id'],
's.pay_state' => 1,
's.sell_type' => ['in', ['1', '2']],
];
if (!empty($param['time']) && $param['time'] != 'null') {
if (!is_array($param['time'])) {
$time = explode(',', $param['time']);
} else {
$time = $param['time'];
}
$start_time = strtotime($time[0]);
$end_time = strtotime($time[1]);
} else {
$start_time = strtotime(date('Y-m-01 00:00:00'));
$end_time = time();
}
$where['s.add_time'] = ['between', [$start_time, $end_time]];
if (!empty($param['search_info'])) {
$where['g.goods_number|g.sku_goods_number|g.goods_code|g.sku_code'] = ['like', '%' . trim($param['search_info']) . '%'];
}
$Classify = new GoodsClassify();
$where = $Classify->getGoodsClassId($param, $where, 'g.goods_class_id');
try {
$SellModel = new RetailSellModel();
$list = $SellModel->sellSummary($where, 's.sell_type,s.goods_type,s.num,s.all_money,s.purchase_price,g.goods_id,g.sku_id,g.goods_name,g.sku_name,g.goods_class_id,g.goods_number,g.sku_goods_number,g.goods_code,g.sku_code,g.serial_number_state,g.company,c.class_name');
$sell_list = [];
$all_purchase_price = 0;
$all_profit = 0;
if (!empty($list)) {
$index = 1;
foreach ($list as $key => &$val) {
if ($val['goods_type'] == 1) {
if (!empty($val['sku_id'])) {
$new_key = $val['goods_id'] . '_' . $val['sku_id'];
} else {
$new_key = $val['goods_id'];
}
} else {
$new_key = '0_0';
}
$sell_num = $val['num'];
$subtotal = $val['all_money'];
$purchase_price = $val['purchase_price'] * $val['num'];
$profit = $val['all_money'] - $purchase_price;
if ($val['sell_type'] == 2) {
$sell_num = -$sell_num;
$subtotal = -$subtotal;
$purchase_price = -$purchase_price;
$profit = -$profit;
}
$all_purchase_price += $purchase_price;
$all_profit += $profit;
if (empty($sell_list[$new_key])) {
$sell_list[$new_key] = [
'index' => $index++,
'goods_name' => $val['goods_name'],
'sku_name' => $val['sku_name'] ? $val['sku_name'] : '',
'goods_number' => $val['sku_id'] ? $val['sku_goods_number'] : $val['goods_number'],
'goods_code' => $val['sku_id'] ? $val['sku_code'] : $val['goods_code'],
'class_name' => $val['class_name'],
'company' => $val['company'],
'serial_number_state' => $val['serial_number_state'],
'goods_id' => $val['goods_id'],
'sku_id' => $val['sku_id'],
'sell_num' => $sell_num,
'subtotal' => $subtotal,
'purchase_price' => $purchase_price,
'profit' => $profit,
];
} else {
$sell_list[$new_key]['sell_num'] += $sell_num;
$sell_list[$new_key]['subtotal'] += $subtotal;
$sell_list[$new_key]['purchase_price'] += $purchase_price;
$sell_list[$new_key]['profit'] += $profit;
}
$sell_list[$new_key]['sell_num'] = strval(round($sell_list[$new_key]['sell_num'], 3));
$sell_list[$new_key]['subtotal'] = strval(round($sell_list[$new_key]['subtotal'], 3));
$sell_list[$new_key]['purchase_price'] = strval(round($sell_list[$new_key]['purchase_price'], 3));
$sell_list[$new_key]['profit'] = strval(round($sell_list[$new_key]['profit'], 3));
}
}
$sell_list = array_values($sell_list);
return callBack(statusCode()['success'], '查询成功', [
'list' => $sell_list,
'purchaseTotal' => round($all_purchase_price, 2),
'profitTotal' => round($all_profit, 2),
]);
} catch (\Exception $e) {
return callBack(statusCode()['error'], '查询超时', []);
}
}
/**
* 出售记录
* @param $param
* [
* order_id
* ]
* @return mixed
*/
public function sellList($param)
{
if (empty($param['order_id'])) {
return callBack(statusCode()['error'], '缺少订单id');
}
try {
$SellModel = new RetailSellModel();
$list = $SellModel->getSelect([
'bill_id' => $param['order_id']
], 'id,goods_id,sku_id,goods_name,attribute_state,sku_json_info,num,serial_number_state,sell_id');
if (!empty($list)) {
$SerialClass = new SerialNumberGoods();
foreach ($list as &$val) {
if ($val['attribute_state'] == 2) {
$sku_info = json_decode($val['sku_json_info'], true);
$val['sku_id'] = $sku_info['id'];
$val['sku_name'] = $sku_info['name'];
}
$val['num'] = round($val['num'], 2);
if (!empty($val['sell_id']) && $val['serial_number_state'] == 1) {
$result = $SerialClass->sellSerialList([
'id' => $val['sell_id'],
'type' => 2,
'files' => 'id,main_serial_number,auxiliary_serial_number_one,auxiliary_serial_number_two',
]);
$val['refund_serial_list'] = $result['data']['list'];
}
}
}
return callBack(statusCode()['success'], '查询成功', $list);
} catch (\Exception $e) {
return callBack(statusCode()['error'], '查询失败');
}
}
/**
* 配送或取消配送时商品信息验证
* @param $param
* [
* merchant_id
* store_id
* order_id 订单id
* type 1配送 2取消配送
* ]
* @return mixed
*/
public function distributionGoodsVerify($param)
{
$SellModel = new RetailSellModel();
$SellSerialModel = new SellSerialModel();
$sell_list = $SellModel->getSelect([
'bill_id' => $param['order_id']
], 'id,goods_id,sku_id,serial_number_state,num');
$goods_serial_list = [];
if (!empty($sell_list)) {
$type = $param['type'];
foreach ($sell_list as &$val) {
if ($val['serial_number_state'] == 1) {
$val['serial_list'] = $SellSerialModel->getSelect([
'outside_id' => $val['id'],
'num' => ['gt', 0]
], 'main_serial_number,auxiliary_serial_number_one,auxiliary_serial_number_two');
$goods_serial_list[] = $val;
}
}
if (!empty($goods_serial_list) && $param['type'] == 1) {
$data = [
'merchant_id' => $param['merchant_id'],
'store_id' => $param['store_id'],
'goods_list' => $goods_serial_list,
'type' => $type
];
$result = $this->sellGoodsSerialVerify($data);
if ($result['status'] != 1) {
return $result;
}
}
return callBack(statusCode()['success'], '验证成功', $goods_serial_list);
}
return callBack(statusCode()['error'], '未找到该出售记录');
}
/**
* 出货收货确认商品
* @param $param
* [
* merchant_id
* store_id 门店id
* type 1出库 2入库
* goods_list 出货的商品信息
* ]
* @return mixed
*/
public function confirmSellGoods($param)
{
$verifyResult = $this->sellGoodsSerialVerify($param); //参数验证
if ($verifyResult['status'] != 1) {
return $verifyResult;
}
//串号处理
$sell_id_arr = array_columns($param['goods_list'], 'id');
if (!empty($sell_id_arr)) {
$SerialClass = new SerialNumberGoods();
$SerialClass->sellSerialDelete([
'merchant_id' => $param['merchant_id'],
'store_id' => $param['store_id'],
'number_arr' => $sell_id_arr,
'param_type' => 2
]);
}
$SellClass = new SerialNumberGoods();
$result = $SellClass->sellSerialAdd([
'merchant_id' => $param['merchant_id'],
'store_id' => $param['store_id'],
'sell_list' => $param['goods_list'],
'type' => $param['type']
]);
return $result;
}
/**
* 出货收货时的商品串号验证
* @param $param
* [
* merchant_id
* store_id 门店id
* type 1出库 2入库
* goods_list 出货的商品信息
* ]
* @return mixed
*/
public function sellGoodsSerialVerify(&$param)
{
$type = $param['type'] ? $param['type'] : 1;
if (is_array($param['goods_list'])) {
$goodsList = $param['goods_list'];
} else {
$goodsList = json_decode(htmlspecialchars_decode($param['goods_list']), true);
}
if (empty($goodsList)) {
return callBack(statusCode()['error'], '请查看该订单是否有商品');
}
try {
$SerialModel = new SWarehouseGoodsSerialModel();
$SellSerialModel = new SellSerialModel();
$where = [
'store_id' => $param['store_id']
];
foreach ($goodsList as $key => $value) {
//串号验证
if ($value['serial_number_state'] == 1) {
if (empty($value['serial_list'])) {
return callBack(statusCode()['error'], $value['goods_name'] . $value['sku_name'] . '请输入串号');
}
$serialList = $value['serial_list'];
if ($value['num'] != count($serialList)) {
return callBack(statusCode()['error'], $value['goods_name'] . $value['sku_name'] . '的串号数量不匹配');
}
foreach ($serialList as $val) {
if (empty($val['main_serial_number'])) {
return callBack(statusCode()['error'], $value['goods_name'] . $value['sku_name'] . '的' . $val['main_serial_number'] . '缺少主串号');
}
if ($type == 1) {
$where['main_serial_number'] = $val['main_serial_number'];
$where['num'] = ['gt', 0];
$state = $SerialModel->getFindField($where, 'id');
if (empty($state)) {
return callBack(statusCode()['error'], $value['goods_name'] . $value['sku_name'] . '的' . $val['main_serial_number'] . '该串号已不存在,请重新输入');
}
}
if ($type == 2) {
$id = $SellSerialModel->getFindField([
'outside_id' => $value['id'],
'num' => 1,
'type' => 2
]);
if (!empty($id)) {
return callBack(statusCode()['error'], '已绑定串号,请勿重复提交');
}
}
}
}
}
$param['goods_list'] = $goodsList;
return callBack(statusCode()['success'], '验证成功');
} catch (\Exception $e) {
return callBack(statusCode()['error'], '验证失败');
}
}
/**
* 订单优惠标记的商品信息
* @param $param
* [
* bill_id 订单id
* store_id 门店id
* pay_coupon_goods_tag 1指定商品id 2指定商品条码
* ]
* @return mixed
*/
public function getGoodsTag($param)
{
$where = [
'bill_id' => $param['bill_id']
];
$sellModel = new RetailSellModel();
$sell_list = $sellModel->getSelect($where, 'goods_id,all_money');
$goods_detail = [];
if ($sell_list) {
$pay_coupon_goods_tag = $param['pay_coupon_goods_tag'] ? $param['pay_coupon_goods_tag'] : 1;
switch ($pay_coupon_goods_tag) {
case 1:
foreach ($sell_list as $val) {
$goods_detail['goods_detail'][] = [
'goods_id' => 'S' . $param['store_id'] . 'G' . $val['goods_id'],
'quantity' => 1,
'price' => $val['all_money'] * 100
];
}
break;
case 2:
$GoodsModel = new RetailGoodsModel();
foreach ($sell_list as $val) {
$goods_code = $GoodsModel->getFindField([
'id' => $val['goods_id']
], 'goods_code');
if (!empty($goods_code)) {
$goods_detail['goods_detail'][] = [
'goods_id' => $goods_code,
'quantity' => 1,
'price' => $val['all_money'] * 100
];
}
}
break;
}
}
$details = json_encode($goods_detail);
return callBack(statusCode()['success'], "查询成功", ['goods_detail' => $details]);
}
}