Home  >  Article  >  php教程  >  Teach you how to implement a sales funnel

Teach you how to implement a sales funnel

大家讲道理
大家讲道理Original
2016-11-12 14:36:343037browse

Foreword
A relatively complete CRM, a sales funnel is essential. It can intuitively and graphically point out the proportional relationship, or the conversion rate, of a company's customer resources from the potential customer stage to the intended customer stage, the negotiation stage and the transaction stage. This information is extremely important for any seller. The traditional method of calculating statistics using paper and pen is time-consuming, labor-intensive, and unintuitive. The emergence of sales funnel is to solve such a problem. So, how to implement a sales funnel? This should be an issue that everyone is more concerned about. Let me explain it one by one.


Demand analysis
Implement sales funnel chart through Highcharts plug-in. (This is a plug-in for Yii2. You can download and install it by yourself. I will attach the download address at the end of the article)


Renderings
Teach you how to implement a sales funnel
Implementation ideas
I checked the information online and did not find any article that directly explains it. The PHP usage of sales funnels all refers to JS usage. There is no reference to the mother parent, so you can only work hard on your own. With an idea, I converted the array format of JS usage found by Baidu into PHP language, and it was successful. Although the plug-ins have different languages, their usage is still the same haha.

Code analysis
1. Reference format of plug-in requirement array.

$funnel=['0'=>[
     'name'=> 'Unique users',
         'data'=> [
            ['Website visits',   15654],
            ['Downloads',       4064],
            ['Requested price list', 1987],
            ['Invoice sent',    976],
            ['Finalized',    846]
        ]
      ]
  ];

2. Arrange the array according to the array format required by the plug-in.

    public function actionIndex()
    {  
       $company_id=isset(Yii::$app->user->identity->attributes['company_id'])?Yii::$app->user->identity->attributes['company_id']:"-1";
       $company=Company::getAllN($company_id);
       $funnel=[];
       $_time=$this->currentMonth();  
       //调用销售漏斗方法
       $funnel=$this->actionCountMoney($_time['begin_time'],$_time['end_time']);       
        return $this->render('index', [
            'funnel'=>$funnel,
            'company'=>$company,
        ]);
    }
 /*
    *销售漏斗
    *按公司按销售阶段统计线索的销售金额
   */
    public function actionCountMoney($begin_time,$end_time)
    {   
      $company_id=isset(Yii::$app->user->identity->attributes['company_id'])?Yii::$app->user->identity->attributes['company_id']:"-1";
      $uids=UserService::getCUser($company_id);
      $query = new Query();
      $query->select([
            'sell_status.status as status',
            'sum(`money`) as count_money'
        ])
            ->from('t_chance')
            ->groupBy([
            'status'
        ])
            ->join('left join','sell_status','t_chance.status = sell_status.id')
            ->orderBy('status');
      //匹配公司所有员工
      $query->andWhere(['in','owner_id',$uids]);    
      //按本月、本季度、本年查找 
      $query->andWhere(['between','end_date',strtotime($begin_time),strtotime($end_time)]);          
      $data=$query->all(); 
      //销售漏斗的主要数组格式部分(重点)
      $_data=[];
      if(!empty($data)){
        foreach ($data as $k => $val) { 
          $data1[0]=$val['status'];
          if(empty($val['status'])){
             $data1[0]=Yii::t('yii','Not status');
          }
          $data1[1]=(int)$val['count_money'];//数字部分必须转为整型(int)才行
          $_data[]=$data1;
        }
      }else{
        $_data[]=[Yii::t('yii','Not status'),0]; 
      } 
     
      $data2['name']=Yii::t('yii','Sales amount');
      $data2['data']=$_data;  
      $_data2[0]=$data2;
      return $_data2;
    }

3. View call.

<?php 
use yii\helpers\Html;
use hr\assets\AppAsset;
use miloschuman\highcharts\Highcharts; 
use yii\web\JsExpression;
?>
 <?php 
    //这部分参设置的数组格式就是我前面说的仿造Js格式来的。
    echo Highcharts::widget([
           &#39;id&#39;=>&#39;funnel_highcharts&#39;, //定义一个唯一的id
           &#39;scripts&#39; => [
           &#39;modules/funnel&#39;,
           &#39;themes/funnel.src&#39;,
           ],
           &#39;options&#39;=>[
                &#39;chart&#39;=>[
                      &#39;type&#39;=> &#39;funnel&#39;,
                      &#39;height&#39;=>300,  //设置图表的高度
                      &#39;marginRight&#39;=>100 
                ],
                &#39;title&#39;=> [
                      &#39;text&#39;=>Yii::t(&#39;yii&#39;,&#39;Funnel chart of sales amount in different sales stages&#39;),
                      &#39;x&#39;=>-50
                ],
                &#39;plotOptions&#39;=>[
                      &#39;series&#39;=> [
                            &#39;dataLabels&#39;=> [
                                    &#39;enabled&#39;=>true,
                                    &#39;format&#39;=>&#39;<b>{point.name}</b>: {point.y:,.0f}&#39;,
                                    &#39;color&#39;=> &#39;(Highcharts.theme && Highcharts.theme.contrastTextColor) || black&#39;,
                                    &#39;color&#39; => new JsExpression(&#39;(Highcharts.theme && Highcharts.theme.contrastTextColor) || "black"&#39;),
                                    &#39;softConnector&#39;=> true
                                                                    
                             ],
                           &#39;neckWidth&#39;=>&#39;15%&#39;,
                           &#39;neckHeight&#39;=>&#39;12.5%&#39;
                    ],
                   &#39;funnel&#39;=>[
                           &#39;height&#39;=>250,  //设置漏斗的高度
                           &#39;width&#39; => 200
                    ],
                ],
                &#39;legend&#39;=>[
                    &#39;enabled&#39;=>false
                 ],
                &#39;series&#39;=> $funnel,
        ]
 ]);
?>

Notes
1. Define a unique ID for the plug-in to avoid conflicts caused by using the same plug-in multiple times on a page.

2. Arrange the array according to the array format required by the plug-in. The format must be consistent, and the numeric part must be converted to an integer (int).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn