This commit is contained in:
luofei 2024-06-04 09:18:24 +08:00
commit 8d36627826
8 changed files with 252 additions and 55 deletions

View File

@ -6,6 +6,7 @@ namespace app\admin\controller\system_store;
use app\admin\controller\BaseAdminController;
use app\admin\lists\system_store\SystemStoreLists;
use app\admin\lists\system_store\SystemStoreSourceLists;
use app\admin\logic\store_product\StoreProductLogic;
use app\admin\logic\system_store\SystemStoreLogic;
use app\admin\validate\system_store\SystemStoreValidate;
@ -39,7 +40,35 @@ class SystemStoreController extends BaseAdminController
{
return $this->dataLists(new SystemStoreSourceLists());
}
/**
* @notes 根据商品源获取门店列表
* @return \think\response\Json
* @author admin
* @date 2024/05/31 17:45
*/
public function source_product_update_store()
{
$addList=$this->request->post('addList');
$product_id=$this->request->post('product_id');
if($addList){
foreach ($addList as $key=>$value){
StoreProductLogic::copy($product_id,$value);
if(StoreProductLogic::hasError()){
return $this->fail(StoreProductLogic::getError());
}
}
}
$removeList=$this->request->post('removeList');
if($removeList){
foreach ($removeList as $key=>$value){
StoreProductLogic::store_del($product_id,$value);
if(StoreProductLogic::hasError()){
return $this->fail(StoreProductLogic::getError());
}
}
}
return $this->success('设置成功', [], 1, 1);
}
/**
* @notes 添加门店列表

View File

@ -45,7 +45,7 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa
public function lists(): array
{
return StoreProduct::where($this->searchWhere)
->field(['id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'is_show', 'unit', 'cost','rose','purchase','store_id','bar_code'])
->field(['id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'is_show', 'unit', 'cost','rose','purchase','bar_code'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item) {

View File

@ -114,7 +114,7 @@ class StoreProductLogic extends BaseLogic
'rose' => $params['rose'],
];
if ($params['rose'] > 0) {
$rose_price = bcmul($params['cost'],bcdiv($params['rose'], 100, 2), 2);
$rose_price = bcmul($params['cost'], bcdiv($params['rose'], 100, 2), 2);
$data['price'] = bcadd($params['cost'], $rose_price, 2);
} else {
$data['price'] = 0;
@ -195,7 +195,7 @@ class StoreProductLogic extends BaseLogic
/**
* 复制商品到门店
*/
public static function copy($id, $store_id,$stock=0)
public static function copy($id, $store_id, $stock = 0)
{
$find = StoreProduct::where('id', $id)->findOrEmpty()->toArray();
$store_find = StoreBranchProduct::where(['product_id' => $id, 'store_id' => $store_id])->findOrEmpty()->toArray();
@ -230,10 +230,28 @@ class StoreProductLogic extends BaseLogic
return true;
} catch (\Exception $e) {
Db::rollback();
d($e);
self::setError($e->getMessage());
return false;
}
}
}
/**
* 删除门店商品
*/
public static function store_del($id, $store_id)
{
Db::startTrans();
try {
StoreBranchProduct::where(['product_id' => $id, 'store_id' => $store_id])->update(['delete_time' => time()]);
StoreBranchProductAttrValue::where(['product_id' => $id, 'store_id' => $store_id])->update(['delete_time' => time()]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}

View File

@ -8,6 +8,7 @@ use app\api\logic\order\OrderLogic;
use app\api\controller\BaseApiController;
use app\api\lists\order\OrderList;
use app\api\service\WechatUserService;
use app\api\validate\OrderValidate;
use app\common\enum\PayEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\PayNotifyLogic;
@ -113,40 +114,37 @@ class OrderController extends BaseApiController
*/
public function createOrder()
{
// d(WeChatConfigService::getPayConfigByTerminal(1));
$user = User::where('id', $this->request->userId)->find();
$cartId = (array)$this->request->post('cart_id', []);
$mer_id = (array)$this->request->post('mer_id', 0);
$store_id = (array)$this->request->post('store_id', 0);
$pay_type = (int)$this->request->post('pay_type');
$addressId = (int)$this->request->post('address_id');
$auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post();
if ($mer_id <= 0 && $pay_type != 9) {
if ($store_id <= 0 && $pay_type != 9) {
return $this->fail('自提点不能为空');
}
if (count($cartId) > 100) {
return $this->fail('购物车商品不能超过100个');
}
if ($pay_type == 9 || $pay_type == 17 ||$pay_type==13) {
if (empty($this->request->userInfo['merchant'])) {
return $this->fail('请先绑定商户');
}
$mer_id = $this->request->userInfo['merchant']['mer_id'];
$params['mer_id'] = $mer_id;
}
// if ($pay_type == 9 || $pay_type == 17 ||$pay_type==13) {
// if (empty($this->request->userInfo['merchant'])) {
// return $this->fail('请先绑定商户');
// }
// $mer_id = $this->request->userInfo['merchant']['mer_id'];
// $params['mer_id'] = $mer_id;
// }
$order = OrderLogic::createOrder($cartId, $addressId, null, $params);
if ($order != false) {
switch ($pay_type) {
case PayEnum::BALANCE_PAY:
//余额支付
$user = User::where('id', $this->request->userId)->find();
$res = OrderLogic::payBalance($user, $order);
OrderLogic::payBalance($user, $order);
if (OrderLogic::hasError()) {
return $this->fail(OrderLogic::getError());
} else {
$res = OrderLogic::paySuccess($order, ['money' => $order['actual']]);
OrderLogic::paySuccess($order, ['money' => $order['actual']]);
if (OrderLogic::hasError()) {
return $this->fail(OrderLogic::getError());
}
@ -164,7 +162,6 @@ class OrderController extends BaseApiController
return $this->fail(PaymentLogic::getError(), $params);
}
return $this->success('', $result);
break;
case PayEnum::WECHAT_PAY_BARCODE:
//微信条码支付
$result = PaymentLogic::codepay($auth_code, $order);
@ -178,7 +175,6 @@ class OrderController extends BaseApiController
return $this->success('用户支付中');
}
return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::WECHAT_PAY_BARCODE,'transaction_id'=>$result['transaction_id']]);
break;
case PayEnum::ALIPAY_BARCODE:
//支付宝条码支付
$result = PaymentLogic::ali_auth_code($auth_code, $order);
@ -190,11 +186,10 @@ class OrderController extends BaseApiController
}
$result['create_time'] = $order['create_time'];
return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::ALIPAY_BARCODE,'transaction_id'=>$result['trade_no']]);
break;
default:
return $this->fail('支付方式错误');
}
return $this->data(['order_id' => $order->id]);
// return $this->data(['order_id' => $order->id]);
} else {
return $this->fail(OrderLogic::getError());
}
@ -373,4 +368,18 @@ class OrderController extends BaseApiController
return $this->success('添加成功');
}
}
//核销
public function writeoff_order()
{
$params = (new OrderValidate())->post()->goCheck('check');
$userId = $this->request->userId;
$res = OrderLogic::writeOff($params,$userId);
if ($res) {
return $this->success('核销成功');
}
return $this->fail('核销失败');
}
}

View File

@ -2,16 +2,13 @@
namespace app\api\logic\order;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\goods\Goods;
use app\common\model\goods\Unit;
use app\common\model\merchant\Merchant;
use app\common\model\opurchase\Opurchaseclass;
use app\common\model\opurchase\Opurchaseinfo;
use app\common\model\order\Cart;
use app\common\model\retail\Cashierclass;
use app\common\model\retail\Cashierinfo;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
@ -22,6 +19,7 @@ use app\common\model\user\UserAddress;
use app\Request;
use support\Log;
use taoser\exception\ValidateException;
use think\Exception;
use think\facade\Db;
use Yansongda\Pay\Event\PayEnd;
@ -51,15 +49,21 @@ class OrderLogic extends BaseLogic
self::$total = 0;
/** 计算价格 */
foreach ($cart_select as $k => $v) {
$find = StoreProduct::where(['id' => $v['goods']])->field('store_name,image,unit,price')->find();
$find = StoreBranchProduct::where(['id' => $v['goods']])->field('store_name,image,unit,price')->find();
if(!$find){
continue;
}
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);//钱
$cart_select[$k]['price'] = $find['price'];
$cart_select[$k]['name'] = $find['store_name'];
$cart_select[$k]['imgs'] = $find['image'];
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
$cart_select[$k]['product_id'] = $find['goods'];
$cart_select[$k]['old_cart_id'] = implode(',',$cartId);
$cart_select[$k]['cart_num'] = $v['cart_num'];
$cart_select[$k]['verify_code'] = $params['verify_code'];
//理论上每笔都是拆分了
// $cart_select[$k]['name'] = $find['store_name'];
// $cart_select[$k]['imgs'] = $find['image'];
// $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2);
}
$order = [
@ -67,9 +71,12 @@ class OrderLogic extends BaseLogic
'create_time' => time(),
'order_id' => getNewOrderId('PF'),
'total_price' => self::$total,//总价
'pay_price' => self::$total,//后期可能有降价抵扣
'total_num' => count($cart_select),//总数
'pay_type' => $params['pay_type'] ?? 0,
'cart_id' => implode(',', $cartId),
'store_id'=>$params['store_id'],
'shipping_type'=>$params['shipping_type']//配送方式 1=快递 2=门店自提
// 'delivery_msg'=>' 预计48小时发货 '
];
} catch (\Exception $e) {
@ -81,52 +88,57 @@ class OrderLogic extends BaseLogic
/**
* 创建新订单
* @return Object|bool
* @return Object|bool|array
*/
static public function createOrder($cartId, $addressId, $user = null, $params = [])
{
$verify_code = generateUniqueVerificationCode();
$params['verify_code'] = $verify_code;
$orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params);
if(!$orderInfo){
return false;
}
// `delivery_name`快递名称/送货人姓名',
// `delivery_code`'快递公司编码',
// `delivery_type` '发货类型',
// `delivery_id'快递单号/手机号',
$_order = $orderInfo['order'];
if($orderInfo['order']['shipping_type'] == 1){
$_order['delivery_name'] = $params['delivery_name'];
$_order['delivery_id'] = $params['delivery_id'];
}
$_order['deduction_price'] = 0;
$_order['merchant'] = $params['mer_id'];
$_order['uid'] = request()->userId;
$_order['money'] = 0;
$_order['user'] = request()->userId;
$_order['account'] = 0;
$_order['payinfo'] = '';
$_order['type'] = 0;
$_order['source'] = 0;
$_order['actual'] = $_order['total'];
$user = User::where('id',\request()->userId)->find();
$_order['real_name'] = $user['real_name'];
$_order['mobile'] = $user['mobile'];
$_order['pay_type'] = $user['pay_type'];
$_order['verify_code'] = $verify_code;
if($addressId>0){
$address=UserAddress::where(['address_id'=>$addressId,'uid'=>Request()->userId])->find();
if($address){
$_order['real_name'] = $address['real_name'];
$_order['user_phone'] = $address['phone'];
$_order['user_address'] = $address['detail'];
$_order['address_id'] = $addressId;
}
}
if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){
$_order['source']=1;
}
if($params['pay_type']==PayEnum::CASH_PAY){
$_order['money']=$_order['total'];
}
// if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){
// $_order['source']=1;
// }
// if($params['pay_type']==PayEnum::CASH_PAY){
// $_order['money']=$_order['total'];
// }
Db::startTrans();
try {
$order = StoreOrder::create($_order);
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['pid'] = $order->id;
$goods_list[$k]['merchant'] = $params['mer_id'];
$goods_list[$k]['oid'] = $order->id;
$goods_list[$k]['uid'] = request()->userId;
$goods_list[$k]['room'] = 0;
$goods_list[$k]['discount'] = 0;
$goods_list[$k]['warehouse'] = 0;
$goods_list[$k]['nums'] = $v['cart_num'];
$goods_list[$k]['cart_id'] = implode(',',$cartId);
$goods_list[$k]['delivery_id'] = $params['store_id'];//商家id
}
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_del' => 0];
@ -385,4 +397,59 @@ class OrderLogic extends BaseLogic
}
return $find;
}
//核销
/**
* @param $params
* @param $uid
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author: codeliu
* @Time: 2024/6/3 22:42
*/
public static function writeOff($params,$uid): bool
{
$data = StoreOrderCartInfo::where([
'oid'=>$params['order_id'],
'verify_code'=>$params['verify_code'],
'uid'=>$uid
])->select()->toArray();
if (empty($data)){
return false;
}
Db::startTrans();
try {
$newArr = [];
$oid = [];
foreach ($data as $k =>$value){
$oid [] = $value['oid'];
$newArr[$k]['writeoff_time'] = time();
$newArr[$k]['id'] = $value['id'];
$newArr[$k]['is_writeoff'] = YesNoEnum::YES;
$newArr[$k]['update_time'] = time();
}
(new StoreOrderCartInfo())->saveAll($newArr);
$oidArr = array_values(array_unique($oid));
StoreOrder::whereIn('id',$oidArr)
->update([
'status' => OrderEnum::RECEIVED_GOODS,
'update_time' => time(),
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace app\api\validate;
use app\common\validate\BaseValidate;
/**
* 订单验证器
* Class OrderValidate
* @package app\admin\validate\order
*/
class OrderValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'order_id' => 'require|number',
'verify_code' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'order_id' => '订单',
'verify_code' => '验证码',
];
/**
* @notes 添加场景
* @return OrderValidate
* @author likeadmin
* @date 2024/04/24 10:37
*/
public function sceneCheck()
{
return $this->only(['order_id','verify_code']);
}
}

View File

@ -39,6 +39,14 @@ class OrderEnum
const EXPENDITURE =0;
const INCOME =1;
/**
* 状态
* @RECEIVED_GOODS 已收货
*/
const RECEIVED_GOODS = 2;
/**
* 账户类型
* @USER 用户

View File

@ -342,3 +342,21 @@ if (!function_exists('setUnique')) {
}
}
if (!function_exists('generateUniqueVerificationCode')) {
function generateUniqueVerificationCode() {
// 获取当前时间的毫秒部分
list($msec, $sec) = explode(' ', microtime());
$msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
// 生成一个随机数作为核销码的后缀
$randomNumber = mt_rand(10000000, 99999999); // 假设核销码是8位数
// 将前缀、毫秒时间戳和随机数连接起来
$type = rand(1, 10); // 生成一个1-10之间的随机数作为前缀
return $type . $msectime . $randomNumber;
}
}