|  | @@ -753,7 +753,7 @@ class Manage
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      # 体验店毛利:采购价-成本价
 | 
	
		
			
				|  |  | -    public function t_buy($where, $shop_id, $per)
 | 
	
		
			
				|  |  | +    public function t_buy($where, $shop_id, $shop_name, $per, &$no_t)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          $where['type_id'] = $shop_id;
 | 
	
		
			
				|  |  |          $where['status'] = '5,6';
 | 
	
	
		
			
				|  | @@ -771,8 +771,10 @@ class Manage
 | 
	
		
			
				|  |  |                  $c_price = 0;
 | 
	
		
			
				|  |  |                  foreach($order_goods as $k1 => $v1){
 | 
	
		
			
				|  |  |                      $goods = Dever::load('goods/lib/info')->getPayInfo($v1['goods_id'], $v1['sku_id']);
 | 
	
		
			
				|  |  | -                    if($goods['c_price'] && $goods['c_price'] >= 0){
 | 
	
		
			
				|  |  | +                    if ($goods['c_price'] && $goods['c_price'] >= 0) {
 | 
	
		
			
				|  |  |                          $c_price += $goods['c_price'] * $v1['num'];
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        $no_t[$v1['goods_id'] . '_' . $v1['sku_id']] = $goods['name'] . '_' . $goods['sku_name'];
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |                  $price = ($v['price'] - $c_price);
 | 
	
	
		
			
				|  | @@ -785,8 +787,9 @@ class Manage
 | 
	
		
			
				|  |  |          return array(round($total, 2), round($total*$per, 2), $shop);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      # 零售店毛利
 | 
	
		
			
				|  |  | -    public function l_buy($where, $shop_id, $per)
 | 
	
		
			
				|  |  | +    public function l_buy($where, $shop_id, $shop_name, $per, &$no_l)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        /*
 | 
	
		
			
				|  |  |          $where['shop_id'] = $shop_id;
 | 
	
		
			
				|  |  |          $data = Dever::db('mail/area_entry')->getBuy($where);
 | 
	
		
			
				|  |  |          $total = 0;
 | 
	
	
		
			
				|  | @@ -803,6 +806,24 @@ class Manage
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return array(round($total, 2), round($total*$per, 2), $shop);
 | 
	
		
			
				|  |  | +        */
 | 
	
		
			
				|  |  | +        $total = 0;
 | 
	
		
			
				|  |  | +        $shop = array();
 | 
	
		
			
				|  |  | +        foreach ($shop_id as $k => $v) {
 | 
	
		
			
				|  |  | +            $where['shop_id'] = $v;
 | 
	
		
			
				|  |  | +            $data = Dever::db('mail/area_entry')->getBuy($where);
 | 
	
		
			
				|  |  | +            if ($data && $data['total'] >= 0) {
 | 
	
		
			
				|  |  | +                $total += $data['total'];
 | 
	
		
			
				|  |  | +                if (!isset($shop[$v])) {
 | 
	
		
			
				|  |  | +                    $shop[$v] = 0;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                $shop[$v] += $data['total'] * $per;
 | 
	
		
			
				|  |  | +                $shop[$v] = round($shop[$v], 2);
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                $no_l[$v] = $shop_name[$k];
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return array(round($total, 2), round($total*$per, 2), $shop);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      # 区域分润
 | 
	
	
		
			
				|  | @@ -828,6 +849,23 @@ class Manage
 | 
	
		
			
				|  |  |          list($where['start'], $where['end']) = Dever::month();
 | 
	
		
			
				|  |  |          $month = Dever::maketime(date('Y-m', $where['start']));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        # 获取本月未对账的对账单
 | 
	
		
			
				|  |  | +        $cash_shop_where = $where;
 | 
	
		
			
				|  |  | +        $cash_shop_where['status'] = 1;
 | 
	
		
			
				|  |  | +        $cash_shop = Dever::db('cash/shop')->getData($cash_shop_where);
 | 
	
		
			
				|  |  | +        if ($cash_shop) {
 | 
	
		
			
				|  |  | +            $shop_data = array();
 | 
	
		
			
				|  |  | +            foreach ($cash_shop as $k => $v) {
 | 
	
		
			
				|  |  | +                $shop = Dever::db('shop/info')->find($v['shop_id']);
 | 
	
		
			
				|  |  | +                if ($shop) {
 | 
	
		
			
				|  |  | +                    $shop_data[$v['shop_id']] = $shop['name'];
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if ($shop_data) {
 | 
	
		
			
				|  |  | +                Dever::alert('以下门店未完成对账:' . implode(',', $shop_data));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          # 获取区域
 | 
	
		
			
				|  |  |          $area = array();
 | 
	
		
			
				|  |  |          $shop = Dever::db('shop/info')->select(array('status' => 1));
 | 
	
	
		
			
				|  | @@ -838,16 +876,19 @@ class Manage
 | 
	
		
			
				|  |  |                      if ($v['city'] > 0) {
 | 
	
		
			
				|  |  |                          $key = $v['province'] . ',' . $v['city'];
 | 
	
		
			
				|  |  |                          $area[$key]['shop_id'][$v['type']][] = $v['id'];
 | 
	
		
			
				|  |  | +                        $area[$key]['shop_name'][$v['type']][] = $v['name'];
 | 
	
		
			
				|  |  |                          $area[$key]['type'] = 1;
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                      if ($v['county'] > 0) {
 | 
	
		
			
				|  |  |                          $key = $v['province'] . ',' . $v['city'] . ',' . $v['county'];
 | 
	
		
			
				|  |  |                          $area[$key]['shop_id'][$v['type']][] = $v['id'];
 | 
	
		
			
				|  |  | +                        $area[$key]['shop_name'][$v['type']][] = $v['name'];
 | 
	
		
			
				|  |  |                          $area[$key]['type'] = 2;
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                      if ($v['town'] > 0) {
 | 
	
		
			
				|  |  |                          $key = $v['province'] . ',' . $v['city'] . ',' . $v['county'] . ',' . $v['town'];
 | 
	
		
			
				|  |  |                          $area[$key]['shop_id'][$v['type']][] = $v['id'];
 | 
	
		
			
				|  |  | +                        $area[$key]['shop_name'][$v['type']][] = $v['name'];
 | 
	
		
			
				|  |  |                          $area[$key]['type'] = 3;
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  }
 | 
	
	
		
			
				|  | @@ -855,11 +896,16 @@ class Manage
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              $area_id = array();
 | 
	
		
			
				|  |  |              $shop_data = array();
 | 
	
		
			
				|  |  | +            $area_stat = array();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # 记录体验店和零售店的错误信息
 | 
	
		
			
				|  |  | +            $no_t = array();
 | 
	
		
			
				|  |  | +            $no_l = array();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              foreach ($area as $k => $v) {
 | 
	
		
			
				|  |  |                  $update = array();
 | 
	
		
			
				|  |  |                  $update['month'] = $month;
 | 
	
		
			
				|  |  |                  $update['name'] = $k;
 | 
	
		
			
				|  |  | -                $info = Dever::db('mail/area_stat')->find($update);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  # 设置金额
 | 
	
		
			
				|  |  |                  $update['t_num'] = $update['l_num'] = $update['t_cash'] = $update['l_cash'] = $update['t_buy'] = $update['l_buy'] = 0;
 | 
	
	
		
			
				|  | @@ -868,14 +914,14 @@ class Manage
 | 
	
		
			
				|  |  |                      if ($k1 == 1) {
 | 
	
		
			
				|  |  |                          # 体验店
 | 
	
		
			
				|  |  |                          $update['t_num'] = count($v1);
 | 
	
		
			
				|  |  | -                        list($update['t_buy'], $update['t_cash'], $shop_data) = $this->t_buy($where, $v1, $config[$v['type']]);
 | 
	
		
			
				|  |  | +                        list($update['t_buy'], $update['t_cash'], $shop_data) = $this->t_buy($where, $v1, $v['shop_name'][$k1], $config[$v['type']], $no_t);
 | 
	
		
			
				|  |  |                          if ($shop_data) {
 | 
	
		
			
				|  |  |                              $update['data'] += $shop_data;
 | 
	
		
			
				|  |  |                          }
 | 
	
		
			
				|  |  |                      } elseif ($k1 == 2) {
 | 
	
		
			
				|  |  |                          # 零售店
 | 
	
		
			
				|  |  |                          $update['l_num'] = count($v1);
 | 
	
		
			
				|  |  | -                        list($update['l_buy'], $update['l_cash'], $shop_data) = $this->l_buy($where, $v1, $config[$v['type']]);
 | 
	
		
			
				|  |  | +                        list($update['l_buy'], $update['l_cash'], $shop_data) = $this->l_buy($where, $v1, $v['shop_name'][$k1], $config[$v['type']], $no_l);
 | 
	
		
			
				|  |  |                          if ($shop_data) {
 | 
	
		
			
				|  |  |                              $update['data'] += $shop_data;
 | 
	
		
			
				|  |  |                          }
 | 
	
	
		
			
				|  | @@ -883,21 +929,9 @@ class Manage
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  if ($update['t_buy'] == 0 && $update['l_buy'] == 0) {
 | 
	
		
			
				|  |  | -                    continue;
 | 
	
		
			
				|  |  | +                    //continue;
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                # 设置上级id,用来做列表层级展示
 | 
	
		
			
				|  |  | -                if ($v['type'] == 1) {
 | 
	
		
			
				|  |  | -                    $update['parent_id'] = -1;
 | 
	
		
			
				|  |  | -                } elseif ($v['type'] == 2) {
 | 
	
		
			
				|  |  | -                    $area_value = explode(',', $k);
 | 
	
		
			
				|  |  | -                    $k2 = $area_value[0] . ',' . $area_value[1];
 | 
	
		
			
				|  |  | -                    $update['parent_id'] = isset($area_id[$k2]) ? $area_id[$k2] : -1;
 | 
	
		
			
				|  |  | -                } elseif ($v['type'] == 3) {
 | 
	
		
			
				|  |  | -                    $area_value = explode(',', $k);
 | 
	
		
			
				|  |  | -                    $k2 = $area_value[0] . ',' . $area_value[1] . ',' . $area_value[2];
 | 
	
		
			
				|  |  | -                    $update['parent_id'] = isset($area_id[$k2]) ? $area_id[$k2] : -1;
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  |                  $update['type'] = $v['type'];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  # 设置收益代理商
 | 
	
	
		
			
				|  | @@ -917,19 +951,45 @@ class Manage
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                      $num[$v['type']]++;
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |                  $update['data'] = Dever::json_encode($update['data']);
 | 
	
		
			
				|  |  | -                if (!$info) {
 | 
	
		
			
				|  |  | -                    $stat_id = Dever::db('mail/area_stat')->insert($update);
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    $stat_id = $update['where_id'] = $info['id'];
 | 
	
		
			
				|  |  | -                    Dever::db('mail/area_stat')->update($update);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | +                $area_stat[$k] = $update;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                if ($v['type'] == 1) {
 | 
	
		
			
				|  |  | -                    $area_id[$k] = $stat_id;
 | 
	
		
			
				|  |  | -                } elseif ($v['type'] == 2) {
 | 
	
		
			
				|  |  | -                    $area_id[$k] = $stat_id;
 | 
	
		
			
				|  |  | +            if ($no_t) {
 | 
	
		
			
				|  |  | +                Dever::alert('以下商品未填写成本价:<br />' . implode(',', $no_t));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if ($no_l) {
 | 
	
		
			
				|  |  | +                Dever::alert('以下门店未填写毛利:<br />' . implode(',', $no_l));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if ($area_stat) {
 | 
	
		
			
				|  |  | +                foreach ($area_stat as $k => $v) {
 | 
	
		
			
				|  |  | +                    # 设置上级id,用来做列表层级展示
 | 
	
		
			
				|  |  | +                    if ($v['type'] == 1) {
 | 
	
		
			
				|  |  | +                        $v['parent_id'] = -1;
 | 
	
		
			
				|  |  | +                    } elseif ($v['type'] == 2) {
 | 
	
		
			
				|  |  | +                        $area_value = explode(',', $k);
 | 
	
		
			
				|  |  | +                        $k2 = $area_value[0] . ',' . $area_value[1];
 | 
	
		
			
				|  |  | +                        $v['parent_id'] = isset($area_id[$k2]) ? $area_id[$k2] : -1;
 | 
	
		
			
				|  |  | +                    } elseif ($v['type'] == 3) {
 | 
	
		
			
				|  |  | +                        $area_value = explode(',', $k);
 | 
	
		
			
				|  |  | +                        $k2 = $area_value[0] . ',' . $area_value[1] . ',' . $area_value[2];
 | 
	
		
			
				|  |  | +                        $v['parent_id'] = isset($area_id[$k2]) ? $area_id[$k2] : -1;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    $info = Dever::db('mail/area_stat')->find(array('month' => $v['month'], 'name' => $v['name'], 'clear' => true));
 | 
	
		
			
				|  |  | +                    if (!$info) {
 | 
	
		
			
				|  |  | +                        $stat_id = Dever::db('mail/area_stat')->insert($v);
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        $stat_id = $v['where_id'] = $info['id'];
 | 
	
		
			
				|  |  | +                        Dever::db('mail/area_stat')->update($v);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    if ($v['type'] == 1) {
 | 
	
		
			
				|  |  | +                        $area_id[$k] = $stat_id;
 | 
	
		
			
				|  |  | +                    } elseif ($v['type'] == 2) {
 | 
	
		
			
				|  |  | +                        $area_id[$k] = $stat_id;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -987,7 +1047,7 @@ class Manage
 | 
	
		
			
				|  |  |                          $member = Dever::db('agent/member')->find($v['mid']);
 | 
	
		
			
				|  |  |                          if ($cash > 0 && $member && $member['status'] == 2) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                            $desc = Dever::load("area/api.string", $v['name'], '') . $date . '区域分润';
 | 
	
		
			
				|  |  | +                            $desc = Dever::load("area/api.string", $v['name'], '') . $date . '区域收入';
 | 
	
		
			
				|  |  |                              $state = Dever::load('bill/lib/cash')->up($v['mid'], 15, $cash, $member['role'], $v['id'], $desc, 2, true);
 | 
	
		
			
				|  |  |                              if ($state) {
 | 
	
		
			
				|  |  |                                  $where = array();
 |