2024.12.16更新
购买装饰资金记录
修改文件增加即时扣除:flarum-decoration-store\src\Controller\DecorationStorePurchaseController.php
<?php
namespace Ziven\decorationStore\Controller;
use Ziven\decorationStore\Serializer\DecorationStorePurchaseSerializer;
use Ziven\decorationStore\Model\DecorationStore;
use Ziven\decorationStore\Model\DecorationStorePurchase;
use Ziven\decorationStore\Helpers\CommonHelper;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Api\Controller\AbstractCreateController;
use Flarum\User\User;
use Flarum\Foundation\ValidationException;
use Flarum\Locale\Translator;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Mattoid\MoneyHistory\model\UserMoneyHistory; // 增加引用
class DecorationStorePurchaseController extends AbstractCreateController{
public $serializer = DecorationStorePurchaseSerializer::class;
protected $settings;
protected $translator;
public function __construct(Translator $translator,SettingsRepositoryInterface $settings){
$this->settings = $settings;
$this->translator = $translator;
}
protected function data(ServerRequestInterface $request, Document $document){
$requestData = $request->getParsedBody()['data']['attributes'];
$itemID = $requestData['itemID'];
$currentUserID = $request->getAttribute('actor')->id;
$errorMessage = "";
if(!isset($itemID)){
$errorMessage = 'ziven-decoration-store.forum.purchase-error';
}else{
$itemData = DecorationStore::where(["isActivate"=>1,"id"=>$itemID])->first();
if(isset($itemData)){
$itemCount = 1;
$itemCost = $itemData->item_cost;
$itemDiscount = $itemData->item_discount;
$itemActualCost = (new CommonHelper)->getActualCost($itemData,$itemCount,"store");
$itemSold = $itemData->item_sold;
$itemAmount = $itemData->item_amount;
$itemType = $itemData->item_type;
$itemAvailableAmount = $itemAmount-$itemSold;
$itemPurchaseType = $itemData->purchase_type;
if($itemAvailableAmount>=$itemCount){
$currentUserData = User::find($currentUserID);
$currentUserMoneyRemain = $currentUserData->money-($itemActualCost*$itemCount);
if($currentUserMoneyRemain<0){
$errorMessage = 'ziven-decoration-store.forum.purchase-error-insufficient-fund';
}else{
$itemPurchaseCount = DecorationStorePurchase::where(["user_id"=>$currentUserID,"item_id"=>$itemID])->count();
if($itemPurchaseCount===0){
$itemData->item_sold+=$itemCount;
$itemData->save();
$currentUserData->money = $currentUserMoneyRemain;
$currentUserData->save();
$settingTimezone = (new CommonHelper)->getSettingTimezone();
$expiredDays = 0;
if($itemPurchaseType==="monthly"){
$expiredDays = 31;
}else if($itemPurchaseType==="yearly"){
$expiredDays = 365;
}
$itemPurchase = new DecorationStorePurchase();
$itemPurchase->item_id = $itemID;
$itemPurchase->user_id = $currentUserID;
$itemPurchase->item_type = $itemType;
$itemPurchase->item_count = $itemCount;
$itemPurchase->purchase_type = $itemPurchaseType;
$itemPurchase->purchase_cost = $itemCost;
$itemPurchase->purchase_discount = $itemDiscount;
$itemPurchase->expired_days = $expiredDays;
$itemPurchase->assigned_at = Carbon::now($settingTimezone);
$itemPurchase->save();
// 以下为新增内容
// 添加用户支出记录
$userHistory = new UserMoneyHistory();
$userHistory->user_id = $currentUserID;
$userHistory->type = "D"; // D表示支出
$userHistory->money = $itemActualCost * $itemCount;
$userHistory->source = 'DECORATION_STORE_PURCHASE';
$userHistory->source_desc = "购买装饰物品 #" . $itemID;
$userHistory->balance_money = $currentUserData->money + ($itemActualCost * $itemCount);
$userHistory->last_money = $currentUserMoneyRemain;
$userHistory->create_user_id = $currentUserID;
$userHistory->change_time = Carbon::now($settingTimezone)->format("Y-m-d H:i:s");
$userHistory->save();
// 添加系统收入记录
$systemHistory = new UserMoneyHistory();
$systemHistory->user_id = 0; // 0表示系统
$systemHistory->type = "C"; // C表示收入
$systemHistory->money = $itemActualCost * $itemCount;
$systemHistory->source = 'DECORATION_STORE_PURCHASE_INCOME';
$systemHistory->source_desc = "装饰物品销售收入 #" . $itemID;
$systemHistory->balance_money = 0; // 系统余额,根据实际情况设置
$systemHistory->last_money = 0; // 系统上次余额,根据实际情况设置
$systemHistory->create_user_id = 0;
$systemHistory->change_time = Carbon::now($settingTimezone)->format("Y-m-d H:i:s");
$systemHistory->save();
// 以上为新增内容
return $itemPurchase;
}else{
$errorMessage = 'ziven-decoration-store.forum.purchase-error-item-alreay-have';
}
}
}else{
$errorMessage = 'ziven-decoration-store.forum.sold-out';
}
}else{
$errorMessage = 'ziven-decoration-store.forum.purchase-error';
}
}
if($errorMessage!==""){
throw new ValidationException(['message' => $this->translator->trans($errorMessage)]);
}
}
}
修改文件增加到期扣除:flarum-decoration-store\src\Console\DecorationStoreScheduleCommand.php
<?php
namespace Ziven\decorationStore\Console;
use Ziven\decorationStore\Model\DecorationStorePurchase;
use Ziven\decorationStore\Model\DecorationStore;
use Ziven\decorationStore\Notification\DecorationSubscriptionBlueprint;
use Ziven\decorationStore\Helpers\CommonHelper;
use Mattoid\MoneyHistory\model\UserMoneyHistory;
use Flarum\Console\AbstractCommand;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Flarum\Notification\NotificationSyncer;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Arr;
use Symfony\Contracts\Translation\TranslatorInterface;
use Illuminate\Support\Carbon;
class DecorationStoreScheduleCommand extends AbstractCommand{
protected $bus;
protected $settings;
protected $translator;
protected $notifications;
public function __construct(NotificationSyncer $notifications, Dispatcher $bus, SettingsRepositoryInterface $settings, TranslatorInterface $translator){
parent::__construct();
$this->bus = $bus;
$this->settings = $settings;
$this->translator = $translator;
$this->notifications = $notifications;
}
protected function configure(){
$this->setName('decorationStore:checkDate')->setDescription('Check date');
}
protected function fire(){
foreach($decorationStorePurchaseData as $key => $value) {
$itemType = $value->item_type;
$userID = $value->user_id;
$itemStatus = $value->item_status;
$purchaseCount = $value->item_count;
$purchaseCost = $value->purchase_cost;
$purchaseDiscount = $value->purchase_discount;
$purchaseType = $value->purchase_type;
$purchaseActualCost = (new CommonHelper)->getActualCost($itemData,$purchaseCount,"purchase");
$isExpired = false;
$userData = User::find($userID);
$userMoney = $userData->money;
$userMoneyRemain = $userMoney - $purchaseActualCost;
if($userMoneyRemain < 0){
if($itemType === "avatarFrame"){
$userData->decoration_avatarFrame = null;
}else if($itemType === "profileBackground"){
$userData->decoration_profileBackground = null;
}else if($itemType === "usernameColor"){
$userData->decoration_usernameColor = null;
}
$isExpired = true;
}else{
$userData->money = $userMoneyRemain;
if($purchaseType === "monthly"){
$value->expired_days = 31;
}else if($purchaseType === "yearly"){
$value->expired_days = 365;
}
// 记录用户支出历史
$userHistory = new UserMoneyHistory();
$userHistory->user_id = $userID;
$userHistory->type = "D"; // D表示支出
$userHistory->money = $purchaseActualCost;
$userHistory->source = 'DECORATION_STORE_SUBSCRIPTION';
$userHistory->source_desc = "装饰物品订阅续费 #" . $value->id;
$userHistory->balance_money = $userMoneyRemain;
$userHistory->last_money = $userMoney;
$userHistory->create_user_id = $userID;
$userHistory->change_time = Carbon::now()->format("Y-m-d H:i:s");
$userHistory->save();
// 记录系统收入历史
$systemHistory = new UserMoneyHistory();
$systemHistory->user_id = 0; // 0表示系统
$systemHistory->type = "C"; // C表示收入
$systemHistory->money = $purchaseActualCost;
$systemHistory->source = 'DECORATION_STORE_SUBSCRIPTION_INCOME';
$systemHistory->source_desc = "装饰物品订阅续费收入 #" . $value->id;
$systemHistory->balance_money = 0; // 系统余额,根据实际情况设置
$systemHistory->last_money = 0; // 系统上次余额,根据实际情况设置
$systemHistory->create_user_id = $userID;
$systemHistory->change_time = Carbon::now()->format("Y-m-d H:i:s");
$systemHistory->save();
}
$userData->save();
if($isExpired === true){
$value->item_status = 0;
$value->is_expired = 1;
}
$value->save();
$this->notifications->sync(new DecorationSubscriptionBlueprint($value), [$userData]);
}
// $this->info('Sync done.');
}
}