wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
 
/**
 * 管理中心 返佣管理
 
 * 
 */
 
if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
 
//获取本系统中货到付款的支付id
function getPayHoudaofukuan(){
    global $db,$ecs;
 
    return $db->getOne('select pay_id from '.$ecs->table('payment').' where is_cod=1 and is_pickup=0');
}
 
//判断佣金信息是否存在
function rebateHave($rid){
    global $ecs,$db;
    $sql = "SELECT r.*, s.supplier_name, s.bank, s.supplier_rebate FROM " . $ecs->table('supplier_rebate') . " AS r left join ". $ecs->table('supplier'). 
              "  AS s on r.supplier_id=s.supplier_id WHERE r.rebate_id = '$rid'";
    $rebate = $db->getRow($sql);
    return empty($rebate) ? false : $rebate;
}
 
//计算时间
function datecha($times){
    $i = 0;
    $tj = true;
    $nowtime = gmtime();
    while ($tj){
        if($times <= ($nowtime+$i*3600*24)){
            $tj=false;
        }else{
            $i++;
        }
    }
    return $i;
}
 
//佣金状态
function rebateStatus($status=-1){
    $status_info = array(0=>'冻结',1=>'可结算',2=>'等待商家确认',3=>'等待平台付款',4=>'结算完成');
    if(array_key_exists($status,$status_info)){
        return $status_info[$status];
    }else{
        return $status_info;
    }
}
 
//根据佣金状态返回操作事件
function getRebateDo($status,$rid,$act){
    $do_info = array(
        'list'=>array(//佣金列表页
            0=>array(
                array('name'=>'查看明细','url'=>'supplier_order.php?act=view&rid='.$rid)
            ),
            1=>array(
                array('name'=>'发起明细','url'=>'supplier_rebate.php?act=view&rid='.$rid),
                array('name'=>'查看明细','url'=>'supplier_order.php?act=view&rid='.$rid)
            ),
            2=>array(
                array('name'=>'查看结算单','url'=>'supplier_rebate.php?act=view&rid='.$rid),
                array('name'=>'查看明细','url'=>'supplier_order.php?act=view&rid='.$rid)
            ),
            3=>array(
                array('name'=>'查看结算单','url'=>'supplier_rebate.php?act=view&rid='.$rid),
                array('name'=>'查看明细','url'=>'supplier_order.php?act=view&rid='.$rid)
            ),
            4=>array(
                array('name'=>'查看结算单','url'=>'supplier_rebate.php?act=view&rid='.$rid),
                array('name'=>'查看明细','url'=>'supplier_order.php?act=view&rid='.$rid)
            )
        ),
        'view'=>array(//查看佣金明细页
            0=>array(
                array('name'=>'结算佣金','url'=>'')
            ),
            1=>array(
                array('name'=>'撤销全部佣金','url'=>'')
            ),
            2=>array(
                array('name'=>'查看结算单','url'=>'#'),
                array('name'=>'查看明细','url'=>'#')
            ),
            3=>array(
                array('name'=>'查看结算单','url'=>'#'),
                array('name'=>'查看明细','url'=>'#')
            )
        ),
        'rebate_view'=>array(//发起结算佣金明细页
            1=>array(
                array('name'=>'发起结算','type'=>'submit','act'=>'update','text'=>'')
            ),
            2=>array(
                array('name'=>'取消发起结算','type'=>'submit','act'=>'cancel','text'=>'等待商家确认')
            ),
            3=>array(
                array('name'=>'结算完成','type'=>'submit','act'=>'finish','text'=>'')
            ),
            4=>array(
                array('name'=>'确认添加','type'=>'submit','act'=>'beizhu','text'=>'')
            )
        )
    );
    return $do_info[$act][$status];
}
//生成编号
function createSign($rid,$suppid){
    return $suppid.sprintf("%07s", $rid);
}
 
//获取佣金中相关的退换货的订单
function getBackOrderByRebate($rid){
    global $ecs,$db;
    $sql = "select oi.order_id,bo.back_type from ".$ecs->table('order_info')." as oi right join ".$ecs->table('back_order')." as bo on oi.order_id=bo.order_id and bo.status_back < 6 where oi.rebate_id=".$rid;
    $query = $db->query($sql);
    $ret = array();
    while($row = $db->fetchRow($query)){
        if($row['back_type']!=3){
            //排除维修的订单
            $ret[] = $row['order_id'];
        }
    }
    return (empty($ret)) ? false : $ret;
}
 
//获取相关佣金所有订单金额
function getRebateOrderMoney($rid){
    global $ecs,$db;
    $back_and = '';
    if(($back_order_id = getBackOrderByRebate($rid)) != false){
        //获取退货订单中相关订单
        $back_and = "and order_id not in(".implode(',',$back_order_id).")";
    }
    $pay_id = getPayHoudaofukuan();//获取货到付款的id
    $sql = "select (" . order_amount_field() . ") AS total_fee,pay_id from ".$ecs->table('order_info')." where rebate_id=".$rid." $back_and and rebate_ispay=2";
    $query = $db->query($sql);
    $online = $onout = 0;
    while($row = $db->fetchRow($query)){
        if($row['pay_id'] == $pay_id){
            //货到付款
            $onout += $row['total_fee'];
        }else{
            //在线支付
            $online += $row['total_fee'];
        }
    }
    return array('online'=>$online,'onout'=>$onout);
}
 
//记录入驻商佣金日志
function writelog($rid,$inout=0){
    return true;
    //已经不用了
    global $db,$ecs;
    $sql = "select order_sn, (" . order_amount_field() . ") AS total_fee,supplier_id from ".$ecs->table('order_info')." where rebate_id=".$rid." and rebate_ispay=2";
    $query = $db->query($sql);
    $addtime = gmtime();
    $bs_qian = '';//前缀内容
    $bs_do = '+';//增减
    if($inout>0){
        $bs_qian = '撤销';
        $bs_do = '-';
    }
    $nowmoney = 0;
    $suppid = 0;
    $suppmoney = array();
    while($row = $db->fetchRow($query)){
        if($suppid != $row['supplier_id']){
            $nowmoney = $db->getOne("select supplier_money from ".$ecs->table('supplier')." where supplier_id=".$row['supplier_id']);
        }
        if($inout>0){
            $nowmoney -= $row['total_fee'];
        }else{
            $nowmoney += $row['total_fee'];
        }
        $loginfo = array(
            'rebateid'=>$rid,
            'addtime'=>$addtime,
            'reason'=>$bs_qian.'订单'.$row['order_sn'].'分佣:'.$bs_do.$row['total_fee'],
            'supplier_money'=>$nowmoney,
            'doman'=>'平台方:'.$_SESSION['user_name'],
            'supplier_id'=>$row['supplier_id']
        );
        $db->autoExecute($ecs->table('supplier_money_log'), $loginfo, 'INSERT');
        $suppid = $row['supplier_id'];
        $suppmoney[$suppid] = $nowmoney;
        unset($loginfo);
    }
    //保存目前的资金
    foreach($suppmoney as $k => $v){
        $db->query('update '.$ecs->table('supplier')." set supplier_money='".$v."' where supplier_id=".$k);
    }
}
 
?>