erp/app/common/logic/order/RetailOrderLogic.php

115 lines
3.3 KiB
PHP

<?php
namespace app\common\logic\order;
use app\common\enum\OrderEnum;
use app\common\logic\BaseLogic;
use app\common\model\goods\Goods;
use app\common\model\order\Cart;
use app\common\model\order\FinancialRecord;
use app\common\model\retail\Cashierinfo;
use app\common\model\user\User;
use Exception;
use support\Log;
use taoser\exception\ValidateException;
use think\facade\Db;
/**
* 零售订单逻辑处理
*/
class RetailOrderLogic extends BaseLogic
{
/**
* 余额订单支付
* @param User $user
* @param $order
* @return bool
* @throws Exception
* @throws ValidateException
*/
static public function payBalance(User $user, $order)
{
if ($user['user_money'] < $order['actual']) {
self::setError('余额不足,请更换支付方式');
return false;
}
Db::startTrans();
try {
$user->user_money = bcsub($user->user_money, $order['actual'], 2);
$user->save();
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
Log::error('余额支付失败' . $e->getMessage() . '。line:' . $e->getLine() . '。file:' . $e->getFile());
self::setError('余额支付失败' . $e->getMessage());
return false;
}
}
/**
* @notes 订单支付成功
* @param $order 订单
* @param $CallbackData 回调数据
* @date 2021/7/8 00:40
*/
static function paySuccess($order, $CallbackData = [])
{
Db::startTrans();
try {
$order->money = $CallbackData['money'];
$order->paid = 1;
$order->save();
Log::info('支付成功');
// 提交事务
Db::commit();
return true;
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
self::setError('支付失败' . $e->getMessage());
return false;
}
}
/**
* 获取用户常用购买记录
*/
public static function frequentlyPurchase($params)
{
try {
$goods_id = Cashierinfo::where('uid', Request()->userId)->page($params['page_no'])->limit(50)->column('goods');
if(!$goods_id){
return [];
}
$goods_arr = array_unique($goods_id);
$select = Goods::where('id', 'in', $goods_arr)->with('unitName')->field('id,name,sell,imgs,unit')->select();
return $select->toArray();
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function purchaseAgain($order_id){
$arr= Cashierinfo::where('pid',$order_id)->field('goods,nums')->select();
$data=[];
foreach ($arr as $k=>$v){
$data[$k]['goods_id']=$v['goods'];
$data[$k]['cart_num']=$v['nums'];
$data[$k]['uid']=Request()->userId;
$data[$k]['source']=0;
$data[$k]['mer_id']=0;
$data[$k]['staff_id']=0;
$data[$k]['is_new']=0;
}
if($data){
( new Cart())->saveAll($data);
return true;
}
return false;
}
}