erp_old/app/api/logic/order/OrderLogic.php

251 lines
9.0 KiB
PHP
Raw Normal View History

2024-04-26 11:26:12 +08:00
<?php
namespace app\api\logic\order;
2024-05-13 11:09:07 +08:00
use app\common\enum\PayEnum;
2024-04-26 11:26:12 +08:00
use app\common\logic\BaseLogic;
use app\common\model\goods\Goods;
2024-04-29 18:14:27 +08:00
use app\common\model\goods\Unit;
2024-05-08 11:31:50 +08:00
use app\common\model\merchant\Merchant;
2024-04-27 11:41:34 +08:00
use app\common\model\opurchase\Opurchaseclass;
use app\common\model\opurchase\Opurchaseinfo;
2024-04-26 11:26:12 +08:00
use app\common\model\order\Cart;
use app\common\model\retail\Cashierclass;
use app\common\model\retail\Cashierinfo;
2024-05-08 11:31:50 +08:00
use app\common\model\user\UserAddress;
use app\Request;
2024-04-26 11:26:12 +08:00
use think\facade\Db;
2024-05-13 11:09:07 +08:00
use Yansongda\Pay\Event\PayEnd;
2024-04-26 11:26:12 +08:00
/**
* 订单逻辑
* Class OrderLogic
* @package app\api\logic\order
*/
class OrderLogic extends BaseLogic
{
2024-05-10 17:55:36 +08:00
public static $total;
2024-04-26 11:26:12 +08:00
/**
* @notes 获取购物车商品信息
* @param $params
* @return array
*/
2024-04-29 16:36:12 +08:00
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
2024-04-26 11:26:12 +08:00
{
$where = ['is_pay' => 0, 'is_fail' => 0];
$cart_select = Cart::whereIn('cart_id', $cartId)->where($where)->field('goods_id as goods,cart_num')->select()->toArray();
if (empty($cart_select)) {
self::setError('购物车为空');
return false;
}
2024-04-29 16:36:12 +08:00
try {
2024-05-10 17:55:36 +08:00
self::$total = 0;
2024-04-29 16:36:12 +08:00
/** 计算价格 */
foreach ($cart_select as $k => $v) {
2024-04-29 18:14:27 +08:00
$find = Goods::where(['id' => $v['goods']])->field('name,imgs,unit,sell')->find();
if(!$find){
continue;
}
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['sell'], 2);
$cart_select[$k]['price'] = $find['sell'];
$cart_select[$k]['name'] = $find['name'];
$cart_select[$k]['imgs'] = $find['imgs'];
$cart_select[$k]['unit_name'] = Unit::where(['id' => $find['unit']])->value('name');
2024-05-10 17:55:36 +08:00
self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2);
2024-04-29 16:36:12 +08:00
}
$order = [
'time' => time(),
'number' => getNewOrderId('PF'),
2024-05-10 17:55:36 +08:00
'total' => self::$total,
2024-04-29 16:36:12 +08:00
'pay_type' => $params['pay_type'] ?? 0,
2024-04-29 18:14:27 +08:00
'cart_id' => implode(',', $cartId),
'delivery_msg'=>' 预计48小时发货 '
2024-04-29 16:36:12 +08:00
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
2024-04-26 11:26:12 +08:00
}
return ['order' => $order, 'cart_list' => $cart_select];
}
/**
* 创建新订单
2024-04-27 11:41:34 +08:00
* @return Object|bool
2024-04-26 11:26:12 +08:00
*/
2024-04-27 11:41:34 +08:00
static public function createOrder($cartId, $addressId, $user = null, $params = [])
2024-04-26 11:26:12 +08:00
{
$orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params);
2024-05-11 16:50:16 +08:00
if(!$orderInfo){
return false;
}
2024-04-26 11:26:12 +08:00
$_order = $orderInfo['order'];
$_order['deduction_price'] = 0;
2024-04-30 14:14:30 +08:00
$_order['merchant'] = $params['mer_id'];
2024-04-29 16:36:12 +08:00
$_order['uid'] = request()->userId;
2024-04-26 11:26:12 +08:00
$_order['money'] = 0;
$_order['user'] = request()->userId;
$_order['account'] = 0;
$_order['payinfo'] = '';
$_order['type'] = 0;
2024-05-13 11:09:07 +08:00
$_order['source'] = 0;
2024-04-26 11:26:12 +08:00
$_order['actual'] = $_order['total'];
2024-05-08 11:39:50 +08:00
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'];
2024-05-08 11:48:07 +08:00
$_order['address_id'] = $addressId;
2024-05-08 11:39:50 +08:00
}
}
2024-05-13 11:09:07 +08:00
if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){
$_order['source']=1;
}
if($params['pay_type']==PayEnum::CASH_PAY){
$_order['money']=$_order['total'];
}
2024-04-26 11:26:12 +08:00
Db::startTrans();
try {
$order = Cashierclass::create($_order);
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['pid'] = $order->id;
2024-04-30 14:14:30 +08:00
$goods_list[$k]['merchant'] = $params['mer_id'];
2024-04-29 16:36:12 +08:00
$goods_list[$k]['uid'] = request()->userId;
2024-04-26 11:26:12 +08:00
$goods_list[$k]['room'] = 0;
$goods_list[$k]['discount'] = 0;
$goods_list[$k]['warehouse'] = 0;
$goods_list[$k]['nums'] = $v['cart_num'];
}
(new Cashierinfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_fail' => 0];
Cart::whereIn('cart_id', $cartId)->where($where)->update(['is_pay'=>1]);
2024-04-26 11:26:12 +08:00
Db::commit();
return $order;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
2024-04-29 16:36:12 +08:00
/**
2024-04-26 11:26:12 +08:00
* @notes 获取购货订单购物车商品信息
* @param $params
* @return array
*/
2024-04-27 11:41:34 +08:00
static public function cartIdByPurchaseOrderInfo($user, $params)
2024-04-26 11:26:12 +08:00
{
2024-04-29 16:36:12 +08:00
if (!$user) {
2024-04-27 11:41:34 +08:00
self::setError('没有用户信息,请先登录');
return false;
}
2024-04-29 16:36:12 +08:00
$mer_id = $user['merchant']['mer_id'];
2024-05-15 10:15:04 +08:00
$where1 = ['paid' => 1,'is_opurchase'=>0];
2024-05-18 16:43:56 +08:00
$where1[] = ['pay_type','<>',9];
2024-05-14 16:11:08 +08:00
2024-05-18 16:43:56 +08:00
$arrs = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('cart_id,id,address_id');
2024-05-14 16:11:08 +08:00
// $order_id = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('id');
2024-04-29 16:36:12 +08:00
$cart_arr = [];
2024-05-14 16:11:08 +08:00
$order_id = [];
foreach ($arrs as $k => $v) {
if (empty($v['cart_id'])) {
self::setError('没有购物车信息');
return false;
}
2024-05-17 16:18:21 +08:00
// if (empty($v['address_id'])) {
// self::setError('请先设置配送地址');
// return false;
// }
2024-05-14 16:11:08 +08:00
$arr = explode(',',$v['cart_id']);
2024-04-29 16:36:12 +08:00
foreach ($arr as $kk => $vv) {
$cart_arr[] = $vv;
2024-04-27 11:41:34 +08:00
}
2024-05-14 16:11:08 +08:00
$order_id[] = $v['id'];
2024-04-27 11:41:34 +08:00
}
$where = ['is_pay' => 1, 'is_fail' => 0];
$cart_select = Cart::whereIn('cart_id', $cart_arr)->where($where)->field('goods_id as goods,cart_num')->select()->toArray();
2024-04-26 11:26:12 +08:00
if (empty($cart_select)) {
self::setError('购物车为空');
return false;
}
/** 计算价格 */
foreach ($cart_select as $k => $v) {
$sell = Goods::where(['id' => $v['goods']])->value('sell');
$cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2);
$cart_select[$k]['price'] = $sell;
}
$order = [
'time' => time(),
2024-04-27 11:41:34 +08:00
'number' => static::getNewOrderId('CG'),
2024-04-26 11:26:12 +08:00
'total' => array_sum(array_column($cart_select, 'total')),
2024-04-29 16:36:12 +08:00
'pay_type' => $params['pay_type'] ?? 0,
'cart_id' => implode(',', $cart_arr),
'order_arr' => implode(',', $order_id)
2024-04-26 11:26:12 +08:00
];
return ['order' => $order, 'cart_list' => $cart_select];
}
/**
* 创建购货订单
2024-04-27 11:41:34 +08:00
* @return Object|bool
2024-04-26 11:26:12 +08:00
*/
2024-04-27 11:41:34 +08:00
static public function createOpurchaseOrder($user = null, $params = [])
2024-04-26 11:26:12 +08:00
{
2024-04-29 16:36:12 +08:00
if (!$user) {
2024-04-27 11:41:34 +08:00
self::setError('没有用户信息,请先登录');
return false;
}
2024-04-29 16:36:12 +08:00
$mer_id = $user['merchant']['mer_id'];
2024-05-13 16:10:21 +08:00
// $merchant = Merchant::where('mer_id', $mer_id)->find();
2024-04-27 11:41:34 +08:00
$orderInfo = self::cartIdByPurchaseOrderInfo($user, $params);
2024-04-29 16:36:12 +08:00
if (!$orderInfo) {
2024-04-27 11:41:34 +08:00
return false;
}
2024-04-26 11:26:12 +08:00
$_order = $orderInfo['order'];
2024-05-13 16:10:21 +08:00
// if ($_order['total'] < $merchant['mer_money']) {
// self::setError('商户余额不足');
// return false;
// }
2024-04-27 11:41:34 +08:00
$_order['merchant'] = $mer_id;
$_order['money'] = $_order['total'];
2024-04-26 11:26:12 +08:00
$_order['actual'] = $_order['total'];
2024-04-27 11:41:34 +08:00
$_order['paid'] = 1;
2024-04-26 11:26:12 +08:00
Db::startTrans();
try {
2024-04-27 11:41:34 +08:00
$order = Opurchaseclass::create($_order);
2024-04-26 11:26:12 +08:00
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['nums'] = $v['cart_num'];
2024-05-14 16:11:08 +08:00
$goods_list[$k]['pid'] = $order->id;
2024-04-26 11:26:12 +08:00
}
2024-04-27 11:41:34 +08:00
(new Opurchaseinfo())->saveAll($goods_list);
2024-05-13 16:10:21 +08:00
// $merchant->mer_money = bcsub($merchant->mer_money, $_order['total'], 2);
2024-04-29 16:36:12 +08:00
$order_arr = explode(',', $_order['order_arr']);
Cashierclass::where('id', 'in', $order_arr)->update(['is_opurchase' => 1]);
2024-04-26 11:26:12 +08:00
Db::commit();
return $order;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 获取订单号
* @param $type
* @return string
* @author likeadmin
* @date 2021/7/28 17:05
*/
static public function getNewOrderId($type)
{
list($msec, $sec) = explode(' ', microtime());
$msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
$orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
return $orderId;
}
}