gmdate( 'Y-m-d', $now - 29 * DAY_IN_SECONDS ) . ' 00:00:00', 'to' => $to, ); } if ( 'month' === $unit ) { return array( 'from' => gmdate( 'Y-m', strtotime( '-11 months', $now ) ) . '-01 00:00:00', 'to' => $to, ); } return array( 'from' => gmdate( 'Y', strtotime( '-4 years', $now ) ) . '-01-01 00:00:00', 'to' => $to, ); } /* * ----------------------------------------------------------------------- * CSV * ----------------------------------------------------------------------- */ /** * レポートを CSV で出力する。 * * @return void */ public function handle_export() { ACSCW_Admin::require_capability(); check_admin_referer( self::ACTION_EXPORT ); $unit = $this->get_unit(); $range = $this->get_range( $unit ); $rows = array(); foreach ( ACSCW_Stats::get_series( $unit, $range['from'], $range['to'] ) as $row ) { $rows[] = array( $row['label'], $row['count'], $row['article'], $row['tip'], $row['total'], ); } ACSCW_Buyers_Page::send_csv( 'acscw-report-' . $unit . '-' . gmdate( 'Ymd-His' ) . '.csv', array( __( 'Period', 'acs-content-for-woocommerce' ), __( 'Sales count', 'acs-content-for-woocommerce' ), __( 'Article revenue', 'acs-content-for-woocommerce' ), __( 'Tips', 'acs-content-for-woocommerce' ), __( 'Total', 'acs-content-for-woocommerce' ), ), $rows ); } /* * ----------------------------------------------------------------------- * 描画 * ----------------------------------------------------------------------- */ /** * レポート画面を描画する。 * * @return void */ public function render() { ACSCW_Admin::require_capability(); $unit = $this->get_unit(); $range = $this->get_range( $unit ); $series = ACSCW_Stats::get_series( $unit, $range['from'], $range['to'] ); ACSCW_Admin::page_header( __( 'Sales Report', 'acs-content-for-woocommerce' ), __( 'Trends for article sales and tips, plus the quality of your buyers.', 'acs-content-for-woocommerce' ) ); $this->render_filters( $unit ); $this->render_chart( $unit, $series ); echo '
'; $this->render_detail_table( $series ); $this->render_quality_card( $range ); echo '
'; ACSCW_Admin::page_footer(); } /** * 期間セグメントと CSV ボタンを描画する。 * * @param string $unit 現在の単位。 * @return void */ private function render_filters( $unit ) { $units = array( 'day' => __( 'Daily', 'acs-content-for-woocommerce' ), 'month' => __( 'Monthly', 'acs-content-for-woocommerce' ), 'year' => __( 'Yearly', 'acs-content-for-woocommerce' ), ); echo '
'; echo ''; foreach ( $units as $value => $label ) { printf( '%3$s', ( $value === $unit ) ? ' is-on' : '', esc_url( ACSCW_Admin::page_url( self::PAGE_SLUG, array( 'unit' => $value ) ) ), esc_html( $label ) ); } echo ''; printf( '%2$s', esc_url( wp_nonce_url( add_query_arg( array( 'action' => self::ACTION_EXPORT, 'unit' => $unit, ), admin_url( 'admin-post.php' ) ), self::ACTION_EXPORT ) ), esc_html__( 'Download CSV', 'acs-content-for-woocommerce' ) ); echo '
'; } /** * グラフカードを描画する。 * * @param string $unit 単位。 * @param array[] $series 系列。 * @return void */ private function render_chart( $unit, $series ) { $titles = array( 'day' => __( 'Daily revenue', 'acs-content-for-woocommerce' ), 'month' => __( 'Monthly revenue', 'acs-content-for-woocommerce' ), 'year' => __( 'Yearly revenue', 'acs-content-for-woocommerce' ), ); ACSCW_Admin::card_open( $titles[ $unit ] ); ACSCW_Admin::render_bar_chart( $series, $titles[ $unit ] ); ACSCW_Admin::card_close(); } /** * 明細表を描画する。 * * @param array[] $series 系列。 * @return void */ private function render_detail_table( $series ) { ACSCW_Admin::card_open( __( 'Breakdown', 'acs-content-for-woocommerce' ) ); if ( empty( $series ) ) { ACSCW_Admin::empty_state( __( 'No sales in this period yet.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); return; } echo ''; printf( '', esc_html__( 'Period', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Sales', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Article revenue', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Tips', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Total', 'acs-content-for-woocommerce' ) ); echo ''; foreach ( array_reverse( $series ) as $row ) { printf( '', esc_html( $row['label'] ), esc_html( number_format_i18n( $row['count'] ) ), wp_kses_post( ACSCW_Admin::price( $row['article'] ) ), wp_kses_post( ACSCW_Admin::price( $row['tip'] ) ), wp_kses_post( ACSCW_Admin::price( $row['total'] ) ) ); } echo '
%s%s%s%s%s
%1$s%2$s%3$s%4$s%5$s
'; ACSCW_Admin::card_close(); } /** * 購入者の質カードを描画する。 * * @param array $range 集計期間。 * @return void */ private function render_quality_card( $range ) { $summary = ACSCW_Stats::get_summary( $range['from'], $range['to'] ); $average = ( $summary['sales'] > 0 ) ? ( $summary['revenue'] / $summary['sales'] ) : 0; $per_buyer = ( $summary['buyers'] > 0 ) ? ( $summary['revenue'] / $summary['buyers'] ) : 0; $repeat_rate = ( $summary['buyers'] > 0 ) ? number_format_i18n( ( $summary['repeat_buyers'] / $summary['buyers'] ) * 100, 1 ) . '%' : '—'; ACSCW_Admin::card_open( __( 'Buyer quality', 'acs-content-for-woocommerce' ) ); echo ''; $rows = array( array( __( 'Buyers (unique)', 'acs-content-for-woocommerce' ), esc_html( number_format_i18n( $summary['buyers'] ) ), ), array( __( 'Repeat buyers', 'acs-content-for-woocommerce' ), esc_html( number_format_i18n( $summary['repeat_buyers'] ) . '(' . $repeat_rate . ')' ), ), array( __( 'Average order value', 'acs-content-for-woocommerce' ), ACSCW_Admin::price( $average ), ), array( __( 'Lifetime value per buyer', 'acs-content-for-woocommerce' ), ACSCW_Admin::price( $per_buyer ), ), array( __( 'Refunds and revokes', 'acs-content-for-woocommerce' ), esc_html( number_format_i18n( $summary['refunded'] ) ) . '(' . ACSCW_Admin::price( $summary['refunded_amount'] ) . ')', ), ); foreach ( $rows as $row ) { printf( '', esc_html( $row[0] ), wp_kses_post( $row[1] ) ); } echo ''; printf( '

%s

', esc_html__( 'A refunded order revokes access automatically and drops out of revenue. Revenue is the total of the amounts actually paid, recorded at the time of purchase.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); } }