bztang-admin/plugins/team-dividend/src/services/GetAmountService.php

368 lines
12 KiB
PHP

<?php
/**
* Author:
* Date: 2018/11/6
* Time: 1:47 PM
*/
namespace Yunshop\TeamDividend\services;
use Yunshop\SpecialSettlement\common\Recalculate;
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
/**
* Class GetAmountService
* @package Yunshop\TeamDividend\services
*/
class GetAmountService
{
private $agent;
private $order;
private $order_goods;
private $set;
private $team_goods;
public $amount;
public $fixed;
public $ratio;
private $dividendType = 0;
/**
* GetAmountService constructor.
* @param $agent
* @param $order
* @param $order_goods
* @param $set
* @param $team_goods
* @param $finish_ratio
* @param $finish_price
* @param $dividendType
*/
public function __construct($agent, $order, $order_goods, $set, $team_goods, $finish_ratio, $finish_price, $dividendType = 0)
{
$this->dividendType = $dividendType;
$this->agent = $agent;
$this->order = $order;
$this->order_goods = $order_goods;
$this->set = $set;
$this->team_goods = $team_goods;
$this->finish_ratio = $finish_ratio;
$this->finish_price = $finish_price;
}
/**
* @name 商品独立设置-比例-解序列化
* @author
* @return mixed
*/
private function getDividendRates()
{
return unserialize($this->team_goods->has_dividend_rates);
}
/**
* @name 商品独立设置-固定金额-解序列化
* @author
* @return mixed
*/
private function getDividendPrices()
{
return unserialize($this->team_goods->has_dividend_prices);
}
/**
* @name 商品独立设置-额外分红-解序列化
* @author
* @return mixed
*/
public function getDividendExtra()
{
return unserialize($this->team_goods->has_dividend_extra);
}
/**
* @name 商品是否启用独立设置
* @author
* @return mixed
*/
private function hasDividend()
{
return $this->team_goods->has_dividend;
}
/**
* @name 收银台商品与存在独立奖励设置
* @author
* @return bool
*/
private function hasDividendRate()
{
return ($this->order->plugin_id == 31 || $this->order->plugin_id == 36 || $this->order->plugin_id == 39) && $this->team_goods->has_dividend_rate;
}
/**
* @name 商城商品设置奖励比例
* @author
* @return bool
*/
private function hasDividendRates()
{
return !empty($this->team_goods->has_dividend_rates);
}
/**
* @name 商城商品设置奖励固定金额
* @author
* @return bool
*/
private function hasDividendPrices()
{
return !empty($this->team_goods->has_dividend_prices);
}
/**
* 商品是否开启平级奖
* @return mixed
*/
public function isHierarchy()
{
if ($this->order->plugin_id == 48 && app('plugins')->isEnabled('hotel-supply')) {
return true;
}
return $this->team_goods->is_hierarchy;
}
/**
* @name 商品是否开启平级奖独立设置
* @author
* @return mixed
*/
private function hasPeers()
{
return $this->team_goods->has_peers;
}
/**
* @name 平级奖等级设置
* @author
* @return mixed
*/
private function getHierarchys()
{
return unserialize($this->team_goods->has_hierarchys);
}
/**
* @name 获取平级奖奖励层级
* @author
* @return mixed
*/
public function getAwardHierarchy()
{
$award_hierarchy = $this->dividendType == 1 ? $this->agent['cultural_level']['award_hierarchy'] : $this->agent['has_one_level']['award_hierarchy'];
if ($this->hasPeers()) {
$hierarchys = $this->getHierarchys();
$hierarchy_level = $hierarchys[$this->agent['level']];
if ($hierarchy_level && $hierarchy_level['hierarchy'] > 0) {
$award_hierarchy = $hierarchy_level['hierarchy'];
}
}
return $award_hierarchy;
}
/**
* @name 获取平级奖奖励比例
* @author
* @return mixed
*/
public function getAwardHierarchyRatio()
{
$ratio = $this->dividendType == 1 ? $this->agent['cultural_level']['award_ratio'] : $this->agent['has_one_level']['award_ratio'];
if ($this->hasPeers()) {
$hierarchys = $this->getHierarchys();
$hierarchy_level = $hierarchys[$this->agent['level']];
$ratio = $hierarchy_level['ratio'];
}
return $ratio;
}
public function getAwardHierarchyAmount()
{
// 基础设置
$amount = OrderCreatedService::getAmount($this->order_goods, $this->set, $this->team_goods);
// 商品独立设置
if ($this->hasDividend()) {
if ($this->hasDividendRates()) {
$dividend_rates = $this->getDividendRates();
$ratio = $dividend_rates['level_' . $this->agent['level']];
if (isset($ratio) && $ratio > 0) {
// 提成结算金额
$amount = $this->order_goods->payment_amount;
return $amount;
}
}
// 商城商品设置奖励固定金额
if ($this->hasDividendPrices()) {
$dividend_prices = $this->getDividendPrices();
// 提成结算金额
$amount = $this->order_goods->total * $dividend_prices['level_' . $this->agent['level']];
}
}
if ($this->order->plugin_id == 48 && app('plugins')->isEnabled('hotel-supply')) {
$amount = OrderCreatedService::gerCulateMethodAmount($this->order_goods, $this->set['culate_method']);
}
// 奖励金额
return $amount;
}
/**
* @name 获取奖励金额
* @author
* @param $ratio
* @return float|int
*/
// 传入 比例 是防止重复调用getRatio 造成finish_ratio重新赋值,结果计算错误
public function getDividendAmount($ratio)
{
// 商品独立设置
if ($this->hasDividend()) {
// 收银台商品
if ($this->hasDividendRate()) {
// 提成结算金额
$this->amount = proportionMath($this->order_goods->payment_amount, $this->team_goods->has_dividend_rate);
// 奖励金额
return proportionMath($this->amount, $ratio);
}
// 存在独立奖励设置
// 商城商品设置奖励比例
if ($this->hasDividendRates()) {
// $dividend_rates = $this->getDividendRates();
// $ratio = $dividend_rates['level_' . $this->agent['level']];
if (isset($ratio) && $ratio > 0) {
// 提成结算金额
$this->amount = $this->order_goods->payment_amount;
// 奖励金额
return proportionMath($this->amount, $ratio);
}
}
// 商城商品设置奖励固定金额
if ($this->hasDividendPrices()) {
\Log::debug('经销商特殊结算商城商品1---',$this->order_goods->payment_amount);
$dividend_prices = $this->getDividendPrices();
// 提成结算金额
$this->amount = $this->order_goods->total * $dividend_prices['level_' . $this->agent['level']];
$this->fixed = 1;
$this->ratio = 0;
// 奖励金额
return $this->amount - $this->finish_price;
}
}
// 基础设置
$this->amount = OrderCreatedService::getAmount($this->order_goods, $this->set, $this->team_goods);
// 门店设置
if ($this->order->plugin_id == 32 || $this->order->plugin_id == 33) {
if ($this->set['has_dividend_rate']) {
$this->amount = proportionMath($this->amount, $this->set['has_dividend_rate']);
}
}
// 酒店供应链
if ($this->order->plugin_id == 48 && app('plugins')->isEnabled('hotel-supply')) {
\Setting::$uniqueAccountId = $this->order->uniacid;
$setting = \Setting::get('plugin.hotel_supply');
if ($setting['team_dividend_ratio']) {
$this->amount = OrderCreatedService::gerCulateMethodAmount($this->order_goods, $this->set['culate_method']);
$this->amount = proportionMath($this->amount , $setting['team_dividend_ratio']);
}
}
// 奖励金额
return proportionMath($this->amount, $ratio);
}
/**
* @name 获取奖励比例
* @author
* @return int
*/
public function getRatio()
{
// 商品独立设置 else 基础设置
if ($this->hasDividend()) {
// 收银台商品 与 存在独立奖励设置 else 商城
if ($this->hasDividendRate()) {
// 经销商奖励比例 - 下级经销商奖励比例 = 奖励比例
if($this->dividendType == 1){
$ratio = bcsub($this->agent['cultural_level']['dividend_ratio'], $this->finish_ratio,2);
$this->finish_ratio = $this->agent['cultural_level']['dividend_ratio'];
}else{
$ratio = bcsub($this->agent['has_one_level']['dividend_ratio'], $this->finish_ratio,2);
$this->finish_ratio = $this->agent['has_one_level']['dividend_ratio'];
}
return $ratio;
} else {
// 商城商品设置奖励比例 else 商城商品设置奖励固定金额
if ($this->hasDividendRates()) {
$dividend_rates = $this->getDividendRates();
$ratio = bcsub($dividend_rates['level_'. $this->agent['level']], $this->finish_ratio,2);
$this->finish_ratio = $dividend_rates['level_' . $this->agent['level']];
return $ratio;
}
}
} else {
if($this->dividendType == 1){
$ratio = bcsub($this->agent['cultural_level']['dividend_ratio'], $this->finish_ratio,2);
$this->finish_ratio = $this->agent['cultural_level']['dividend_ratio'];
}else{
$ratio = bcsub($this->agent['has_one_level']['dividend_ratio'], $this->finish_ratio,2);
$this->finish_ratio = $this->agent['has_one_level']['dividend_ratio'];
}
return $ratio;
}
}
/**
* 获取最大奖励
*/
public function getTotalDividend()
{
$maxRatio = max(TeamDividendLevelModel::uniacid()->select('dividend_ratio')->get()->toArray());
if ($this->hasDividend()) {
if ($this->hasDividendRate()) {
$amount = $this->order_goods->payment_amount * $this->team_goods->has_dividend_rate / 100;
return $amount * $maxRatio['dividend_ratio'] / 100;
} else {
if (max($this->getDividendRates())) {
$dividend_rates = $this->getDividendRates();
$amount = $this->order_goods->payment_amount;
return $amount * max($dividend_rates) / 100;
}
if (max($this->hasDividendPrices())) {
$dividend_prices = $this->getDividendPrices();
// 提成结算金额
$amount = $this->order_goods->total * max($dividend_prices);
// 奖励金额
return $amount;
}
}
} else {
$amount = OrderCreatedService::getAmount($this->order_goods, $this->set, $this->team_goods);
if ($this->order->plugin_id == 32 || $this->order->plugin_id == 33) {
if ($this->set['has_dividend_rate']) {
$amount = $amount * $this->set['has_dividend_rate'] / 100;
}
}
return $amount * $maxRatio['dividend_ratio'] / 100;
}
}
}