<?php
Page::$need_view = false;
$zhres=(new Model('vshop_order'))->list_all();
foreach($zhres as $re){
	$zhm = new Model('vshop_order_task');
	$zhm->find(array('wid'=>$re->wid));
	if($zhm->receiptConfirm){
		if($zhm->receiptdateset){
			$zhday=$zhm->receiptdateset;
		}else{
			$zhday=7;
		}
		if($re->PayStatus && $re->DeliveryStatus){//已经支付并且发货
			if($re->OrderStatus!=2){//未完成
				$zhstrtotime=strtotime("+{$zhday} day", strtotime($re->delivertime));
				if($zhstrtotime>=time()){//超过规定时间
				     //修改订单状态
					 $updateOrder=new Model('vshop_order');
					 $updateOrder->update(array('id'=>$re->id),array('OrderStatus'=>2,'EvaluationStatus'=>0,'oktime'=>date('Y-m-d H:i:s')));
					 $m = new Model('vshop_order');
					 $m->find($re->id);
					 //确认佣金
					 /**$setting = (new Model('vshop_vagent_setting'))->find(['wid' => $m->wid]);
					 $me=(new Model('vshop_member'))->find(['wxid'=>$m->wxid]);
					 $pm=(new Model('vshop_member'))->find(['id'=>$me->pid]);
					 if($pm->id){//找到了上级
						$level1_add = floor($m->payAmount * $setting->level1) / 100;
						$pm->unextract_money = $pm->unextract_money - $level1_add;
						$pm->extract_money = $pm->extract_money + $level1_add;//不可提现转移为可提现
						$pm->save();
						$ppm=(new Model('vshop_member'))->find(['id'=>$pm->pid]);
						if($ppm->id){//找到了上上级
							$level2_add = floor($m->payAmount * $setting->level2) / 100;
							$ppm->unextract_money = $ppm->unextract_money - $level2_add;
							$ppm->extract_money = $ppm->extract_money + $level2_add;//不可提现转移为可提现
							$ppm->save();
						 }
					 }*/
					 confirm_brokerage($m);
				}
			}
		}
	}
}

//确认佣金					 
function confirm_brokerage($m){
	$me=(new Model('vshop_member'))->find(['wxid'=>$m->wxid]);
	$pm=(new Model('vshop_member'))->find(['id'=>$me->pid]);//上级用户
	if($pm->id){//找到了上级
		$blog1 = (new Model('vshop_brokerage'))->find(['wid'=>$m->wid,'orderid'=>$m->id,'sid'=>$pm->id]);
		$level1_add = $blog1->money;//该笔订单获得的佣金
		$pm->unextract_money = $pm->unextract_money - $level1_add;
		$pm->extract_money = $pm->extract_money + $level1_add;//不可提现转移为可提现
		$pm->save();
		$ppm=(new Model('vshop_member'))->find(['id'=>$pm->pid]);
		if($ppm->id){//找到了上上级
			$blog2 = (new Model('vshop_brokerage'))->find(['wid'=>$m->wid,'orderid'=>$m->id,'sid'=>$ppm->id]);
			$level2_add = $blog2->money;//该笔订单获得的佣金
			$ppm->unextract_money = $ppm->unextract_money - $level2_add;
			$ppm->extract_money = $ppm->extract_money + $level2_add;//不可提现转移为可提现
			$ppm->save();
		}
	}
}