白班提成

  //服务员白班提成
    public static function addStaffCommission($date, $staffId, $staffType, $departmentInfo, $scheduleList, $is_order = false) {
        $tempData = [];
        foreach ($scheduleList as $val) {
            if ($val['type'] == DicSchedule::TYPE_DAY) {
                $tempData[] = $val;
            }
        }
        $scheduleList = $tempData;
        if (empty($scheduleList)) {
            return;
        }
        
        if (PHP_SAPI === 'cli') {
            echo "{$date}----{$staffId} ---服务员白班提成\n";
            //print_r(['科室' => $departmentInfo]);
            //print_r(['白班' => $scheduleList]);
        }
        
        $projectId = $departmentInfo['detail']['project_id'];
        $departmentId = $departmentInfo['detail']['id'];
        $departmentConfigId = $departmentInfo['config']['id'];
        $departmentType = $departmentInfo['detail']['type'];
        $dependHeavilyPrice = $departmentInfo['config']['depend_heavily_price'];
        $dayBasicsScore = $departmentInfo['config']['day_basics_score'];
        $commissionDayProportion = $departmentInfo['config']['commission_day_proportion'];
        $month = (int)($date / 100);
        
        //计算得分
        $totalScore = 0;
        
        $todayStartStr = $date . " " . $departmentInfo['config']['staff_day_start'] . ":00";
        //echo ">>>>".$todayStartStr;
        $todayStart = strtotime($todayStartStr);
        $todayEnd = $todayStart + ($departmentInfo['config']['staff_day_work_hour'] * 60 * 60);
        // $todayEnd = strtotime($date . " 18:00:00");
        
        $complex = [];
        $timeSlice = [];
        
        foreach ($scheduleList as $schedule) {
            $bedId = $schedule['bed_id'];
            $filter = [
                ['bed_id', '=', $bedId],
                ['start_time', '<', $todayEnd],
                ['end_time', '>', $todayStart]
            ];
            $matrix = [];
            $oderInfo = OrderConfigFeeModel::getInstance()->getAll("*", $filter);
            //不付费
            if (empty($oderInfo)) {
                //床位当前有订单
                $bedOrder = OrderBedModel::getInstance()->getAll("*", $filter);
                if (!empty($bedOrder)) {
                    $complex[] = [
                        ['id' => $schedule['id'], 'start' => $todayStart, 'end' => $todayEnd, 'depend' => 1]
                    ];
                }
                continue;
            }
            //付费
            foreach ($oderInfo as $order) {
                $item = ['id' => $schedule['id'], 'start' => 0, 'end' => 0, 'depend' => 0];
                $item['order_id'] = $order['order_id'];
                if ($order['start_time'] < $todayStart) {
                    $item['start'] = $todayStart;
                } else {
                    $item['start'] = $order['start_time'];
                }
                if ($order['end_time'] > $todayEnd) {
                    $item['end'] = $todayEnd;
                } else {
                    $item['end'] = $order['end_time'];
                }
                $item['depend'] = $order['depend_type'];
                $matrix[] = $item;
                $timeSlice[] = $item['start'];
                $timeSlice[] = $item['end'];
            }
            $complex[] = $matrix;
            
            
        }
        //print_r(["客户"=>$complex]);
        $timeSlice = array_unique($timeSlice);
        sort($timeSlice);
        //
        $scoreSlice = [];
        for ($i = 0; $i < count($timeSlice) - 1; $i++) {
            $core = 0;
            $bTime = $timeSlice[$i];
            $eTime = $timeSlice[$i + 1];
            foreach ($complex as $matrix) {
                foreach ($matrix as $item) {
                    if ($item['start'] <= $bTime && $item['end'] >= $eTime) {
                        $dependType = $item['depend'];
                        switch ($dependType) {
                            case DicCommon::DEPEND_LEVEL_HEAVY:
                                $core += $departmentInfo['config']['depend_heavily_score'];
                                break;
                            case DicCommon::DEPEND_LEVEL_MID:
                                $core += $departmentInfo['config']['depend_middle_score'];
                                break;
                            case DicCommon::DEPEND_LEVEL_LIGHT:
                                $core += $departmentInfo['config']['depend_light_score'];
                                break;
                            default:
                                $core += $departmentInfo['config']['depend_free_score'];
                                break;
                        }
                    }
                }
            }
            $scoreSlice[$bTime . "_" . $eTime] = $core;
            //$scoreSlice[] = $core;
        }
        
        
        $totalCommission = 0;
        if (!empty($scoreSlice)) {
            $totalScore = max($scoreSlice);
        }
        if ($is_order) {
            $bTime = 0;
            $eTime = 0;
            foreach ($scoreSlice as $key => $val) {
                if ($val == $totalScore) {
                    $Time = explode("_", $key);
                    $bTime = $Time[0];
                    $eTime = $Time[1];
                    break;
                }
            }
            $items = [];
            foreach ($complex as $matrix) {
                foreach ($matrix as $item) {
                    if ($item['start'] <= $bTime && $item['end'] >= $eTime) {
                        $items[] = $item;
                        continue;
                    }
                }
            }
            
            return $items;
        }
        //print_r(["得分"=>$scoreSlice,$timeSlice]);
        //白班提成金额 = 超出工作分值*每分提成价格*白班提成比例
        if ($totalScore - $dayBasicsScore > 0) {
            $totalCommission = ($totalScore - $dayBasicsScore) * $dependHeavilyPrice * $commissionDayProportion / 100;
        }
        
        $kv = [
            'project_id'                => $projectId,
            'department_id'             => $departmentId,
            'department_config_id'      => $departmentConfigId,
            'department_type'           => $departmentType,
            'user_staff_id'             => $staffId,
            'depend_heavily_price'      => $dependHeavilyPrice,
            'total_commission'          => $totalCommission,
            'total_score'               => $totalScore,
            'day_basics_score'          => $dayBasicsScore,
            'commission_day_proportion' => $commissionDayProportion,
            'staff_type'                => $staffType,
            'month'                     => $month,
            'date'                      => $date,
            'point_time'                => '',
        ];
        
        $commissionModel = StaffCommissionDayModel::getInstance();
        
        try {
            
            $commissionModel->begin();
            
            $gjStaffBasicWageId = $commissionModel->insert($kv);
            
            if ($gjStaffBasicWageId < 1) {
                throw new SysException('操作失败');
            }
            
            $title = date("Y-m-d", strtotime($date)) . '服务员' . '白班提成';
            
            StaffAccountInterface::add([
                'user_staff_id' => $staffId,
                'project_id'    => $departmentInfo['detail']['project_id'],
                'department_id' => $departmentInfo['detail']['id'],
                'date'          => $date,
                'month'         => $month,
                'amount'        => $totalCommission,
                'source_type'   => DicStaffAccountList::SOURCE_TYPE_ATTENDANT_DAY_COMMISSION,
                'source_id'     => $gjStaffBasicWageId,
                'title'         => $title,
            ], $staffId, true);
            
            $commissionModel->commit();
        } catch (\Exception $e) {
            $commissionModel->rollback();
            throw $e;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值