Exemple #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = CategoryModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('1=0');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
 public function actionByProductMonthCsv()
 {
     header('Content-Type: application/excel');
     header('Content-Disposition: attachment; filename="sales_monthly.csv"');
     $searchModel = new SalesDtlSearch();
     $searchModel->fr_date = date('Y-m-01');
     $searchModel->to_date = date('Y-m-d');
     $dataProvider = $searchModel->searchByProduct(Yii::$app->request->queryParams);
     $dataProvider->pagination = false;
     $dcats = \backend\models\master\Category::find();
     $rcats = [];
     foreach ($dcats->all() as $crows) {
         $rcats[$crows->id] = [$crows->code, $crows->name];
     }
     $days = $this->getDay();
     $fp = fopen('php://output', 'w');
     $dbfore = '';
     $is_first = true;
     $i = 0;
     $tot = 0;
     $tot_disc = 0;
     $tot_all = 0;
     $tot_jend = 0;
     $tot_jend_disc = 0;
     $tot_jend_all = 0;
     $hdr = ['HARI/TANGGAL', 'NO.BON', 'ARTIKEL', 'NAMA BARANG', 'KATEGORI', 'SIZE', 'QTY', 'HARGA', 'DISKON', 'JUMLAH'];
     fputcsv($fp, $hdr, chr(9));
     foreach ($dataProvider->models as $row) {
         if (!$is_first && $row->SDate != $dbfore) {
             if ($tot_jend > $tot) {
                 fputcsv($fp, [null, null, null, null, null, null, null, $tot, $tot_disc, $tot_all], chr(9));
                 fputcsv($fp, [null, null, null, null, null, null, null, $tot_jend, $tot_jend_disc, $tot_jend_all, PHP_EOL], chr(9));
             } else {
                 fputcsv($fp, [null, null, null, null, null, null, null, $tot, $tot_disc, $tot_all, PHP_EOL], chr(9));
             }
             fputcsv($fp, $hdr, chr(9));
             $tot = $row->price * $row->qty;
             $tot_disc = $row->disc;
             $tot_all = $tot - $tot_disc;
         } else {
             $tot += $row->price * $row->qty;
             $tot_disc += $row->disc;
             $tot_all += $row->price * $row->qty - $row->disc;
         }
         $tot_jend += $row->price * $row->qty;
         $tot_jend_disc += $row->disc;
         $tot_jend_all += $row->price * $row->qty - $row->disc;
         $tanggal = $row->sdate;
         $day = date('D', strtotime($tanggal));
         $r = [];
         $r[] = $row->SDate == $dbfore ? '' : $days[$day] . ', ' . $row->SDate;
         //tgl
         $r[] = $row->faktur;
         //bon
         $prod = explode(';', $row->pname);
         $r[] = isset($prod[1]) ? $prod[1] : '';
         //artikel
         $r[] = isset($prod[0]) ? $prod[0] : '';
         //product name
         $r[] = isset($row->ctgr) ? $rcats[$row->ctgr][1] : '';
         //category
         $r[] = isset($prod[2]) ? $prod[2] : '';
         //size
         $r[] = $row->qty;
         $r[] = $row->price * $row->qty;
         $r[] = $row->disc;
         $r[] = $row->price * $row->qty - $row->disc;
         fputcsv($fp, $r, chr(9));
         $dbfore = $row->SDate;
         $is_first = false;
         $i++;
     }
     fputcsv($fp, [null, null, null, null, null, null, null, $tot, $tot_disc, $tot_all], chr(9));
     if ($tot_jend > $tot) {
         fputcsv($fp, [null, null, null, null, null, null, null, $tot_jend, $tot_jend_disc, $tot_jend_all], chr(9));
     }
     fclose($fp);
     return false;
 }