diff --git a/chart.php b/chart.php new file mode 100644 index 0000000..e2c18a7 --- /dev/null +++ b/chart.php @@ -0,0 +1,1987 @@ + + + + + + +
+ + +
+ + +
+ +
+

+
+ +
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ + +
+
+ +
+
+

+
+ +
+ +
+
+
+ + +
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+

+
+ +
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+
+
+

+
+ +
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+
+ + + 0 ? '+' : '-'; + $time_zone = abs( $time_zone ) * 3600; + $time_zone = gmdate( 'H:i', $time_zone ); + $time_zone = $prefix . $time_zone; + + $today = date( 'Y-m-d H:i:s' ); + $date = new DateTime( $today ); + + $tzn = abs( $time_zone_orig ) * 3600; + $tzh = intval( gmdate( 'H', $tzn ) ); + $tzm = intval( gmdate( 'i', $tzn ) ); + try { + if ( intval( $time_zone_orig ) < 0 ) { + $date->sub( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } else { + $date->add( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } + } catch ( Exception $e ) { + return array( 'tz' => $time_zone, 'today' => time() ); + } + $today = $date->format( 'Y-m-d H:i:s' ); + $today = strtotime( $today ); + + return array( 'tz' => $time_zone, 'today' => $today ); + } + + + public static function lastxdays_chart_info( $form_id, $chart, $x ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + $n = $t = ''; + $series = $options = $datat = $tooltips = $revenue_label = $revenue_week = $sales_label = $sales_week = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( 'پی‌پینگ این فرم', 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( 'همه روشهای این فرم', 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرمهای سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + $sales_week = 0; + $revenue_week = 0; + $tooltips = ""; + + $today = date( 'Y-m-d', $tday ); + $today_n = date( 'Ymd', $tday ); + $targetdb = $today_n; + + if ( ! empty( $results ) ) { + + $data = "["; + foreach ( $results as $result ) { + + $timeX_tooltips = GF_jdate( 'l - d F', strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + + $target = date( 'Ymd', strtotime( $result->date ) ); + $target2 = date( 'Y-m-d', strtotime( $result->date ) ); + $date = new DateTime( $today_n ); + if ( $x == 7 ) { + $date->sub( new DateInterval( 'P6DT0H0M' ) ); + } + if ( $x == 30 ) { + $date->sub( new DateInterval( 'P29DT0H0M' ) ); + } + $lastxt = $date->format( 'Y-m-d' ); + $lastxtf = $date->format( 'Ymd' ); + if ( $target > $targetdb ) { + $targetdb = $target; + $today = $target2; + } + if ( $target >= $lastxtf && $today_n >= $target ) { + $sales_week += $result->new_sales; + $revenue_week += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + if ( $target >= $lastxtf && $targetdb >= $target ) { + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time', timeformat: '%d',tickFormatter: shamsi_1, minTickSize:[1, 'day'],min: (new Date('$lastxt')).getTime(),max: (new Date('$today')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + if ( $x == 7 ) { + $n = __( '7 روز', 'gravityformspayping' ); + $mid = 7; + } + if ( $x == 30 ) { + $n = __( '30 روز', 'gravityformspayping' ); + $mid = 30; + } + + $sales_label = sprintf( __( "تعداد پرداخت های %s گذشته %s", 'gravityformspayping' ), $n, $t ); + + $midt = $mid ? $sales_week / $mid : 0; + $mid = ( $mid ? GFCommon::to_money( $revenue_week / $mid ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $midt = number_format( $midt, 3, '.', '' ) . __( " در روز", 'gravityformspayping' ); + $midt_label = sprintf( __( "میانگین تعداد پرداخت های %s گذشته %s", 'gravityformspayping' ), $n, $t ); + $mid_label = sprintf( __( "میانگین پرداخت های %s گذشته %s", 'gravityformspayping' ), $n, $t ); + $revenue_week = GFCommon::to_money( $revenue_week ); + $revenue_label = sprintf( __( "جمع پرداخت های %s گذشته %s", 'gravityformspayping' ), $n, $t ); + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_week, + "sales_label" => $sales_label, + "sales" => $sales_week, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + + public static function thisweek_chart_info( $form_id, $chart ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + + $series = $options = $datat = $tooltips = $revenue_label = $revenue_week = $sales_label = $sales_week = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرم های سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + $sales_week = 0; + $revenue_week = 0; + $tooltips = ""; + + $today_n = date( 'Y-m-d H:i:s', $tday ); + $today_w = date( 'w', $tday ); + if ( $today_w < 6 ) { + $today_w = $today_w + 1; + } else if ( $today_w == 6 ) { + $today_w = 0; + } + + switch ( $today_w ) { + case "0" : // شنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P0DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P6DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "1" : //یکشنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P1DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P5DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "2" : //دوشنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P2DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P4DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "3" : //سه شنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P3DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P3DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "4" : //چهار شنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P4DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P2DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "5" : // پنجشنبه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P5DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P1DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + + case "6" : //جمعه + $date = new DateTime( $today_n ); + $date->sub( new DateInterval( 'P6DT0H0M' ) ); + $abz = $date->format( 'm d, Y' ); + $abz_t = $date->format( 'Ymd' ); + $date = new DateTime( $today_n ); + $date->add( new DateInterval( 'P0DT0H0M' ) ); + $ebz = $date->format( 'm d, Y' ); + $ebz_t = $date->format( 'Ymd' ); + break; + } + + if ( ! empty( $results ) ) { + + $data = "["; + foreach ( $results as $result ) { + + $timeX_tooltips = GF_jdate( 'l - d F Y', strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + + $target = date( 'Ymd', strtotime( $result->date ) ); + if ( $target >= $abz_t && $ebz_t >= $target ) { + $sales_week += $result->new_sales; + $revenue_week += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: weekday, tickSize:[1, 'day'],min: (new Date('$abz 00:00:00')).getTime(),max: (new Date('$ebz 23:59:59')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + + $sales_label = __( "تعداد پرداخت های این هفته ", 'gravityformspayping' ) . $t; + + $midt = $sales_week / 7; + $midt = ( $midt ? number_format( $midt, 3, '.', '' ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $midt_label = __( "میانگین تعداد پرداخت های این هفته ", 'gravityformspayping' ) . $t; + $mid = GFCommon::to_money( $revenue_week / 7 ) . __( " در روز", 'gravityformspayping' ); + $mid_label = __( "میانگین پرداخت های این هفته ", 'gravityformspayping' ) . $t; + $revenue_week = GFCommon::to_money( $revenue_week ); + $revenue_label = __( "جمع پرداخت های این هفته ", 'gravityformspayping' ) . $t; + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_week, + "sales_label" => $sales_label, + "sales" => $sales_week, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + + public static function targetmdays_chart_info( $form_id, $chart, $xmonth ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + $n = $t = ''; + $strd = ''; + $series = $options = $datat = $tooltips = $revenue_label = $revenue_thistday = $sales_label = $sales_thistday = $mid_label = $mid = $midt_label = $midt = ''; + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + $sales_thistday = 0; + $revenue_thistday = 0; + $tooltips = ""; + + $today = date( 'Y-m-d', $tday ); + $saremaheshamsi = strtotime( $today ) + ( ( GF_jdate( 't', strtotime( $today ), '', date_default_timezone_get(), 'en' ) - GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ); + $entbaz = date( 'm d , Y', $saremaheshamsi ); + $endd = date( 'Y-m-d', $saremaheshamsi ); + + if ( ! empty( $results ) ) { + if ( $xmonth == 2 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P1M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) + ( ( GF_jdate( 't', strtotime( $today ), '', date_default_timezone_get(), 'en' ) - GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ); + $entbaz = date( 'm d , Y', $saremaheshamsi ); + $endd = date( 'Y-m-d', $saremaheshamsi ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 1 ) { + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 60 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P1M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 3 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P2M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 6 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P5M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 9 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P8M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + if ( $xmonth == 12 ) { + $date = new DateTime( $today ); + $date->sub( new DateInterval( 'P11M' ) ); + $today = $date->format( 'Y-m-d' ); + $saremaheshamsi = strtotime( $today ) - ( ( GF_jdate( 'j', strtotime( $today ), '', date_default_timezone_get(), 'en' ) ) * 86400 ) + 86400; + $ebtbaz = date( 'm d , Y', $saremaheshamsi ); + $strd = date( 'Y-m-d', $saremaheshamsi ); + } + + list( $m, $d, $n, $y ) = explode( " ", $ebtbaz ); + $date = new DateTime( "$y-$m-$d" ); + $ebtbaz_w = $date->format( 'Ymd' ); + list( $m, $d, $n, $y ) = explode( " ", $entbaz ); + $date = new DateTime( "$y-$m-$d" ); + $entbaz_w = $date->format( 'Ymd' ); + $data = "["; + + foreach ( $results as $result ) { + + $timeX_tooltips = GF_jdate( 'l - d F Y', strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + + $target = date( 'Ymd', strtotime( $result->date ) ); + if ( $entbaz_w >= $target && $target >= $ebtbaz_w ) { + $sales_thistday += $result->new_sales; + $revenue_thistday += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( " تعداد پرداخت", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + + if ( $xmonth == 1 || $xmonth == 2 ) { + $n = GF_jdate( 'F', strtotime( $today ), '', date_default_timezone_get(), 'en' ); + $n = $n . __( " ماه", "gravityformspayping" ); + } + if ( $xmonth == 60 || $xmonth == 3 || $xmonth == 6 || $xmonth == 9 || $xmonth == 12 ) { + $n = $xmonth; + + if ( $xmonth == 60 ) { + $n = 2; + } + $n = $n . __( " ماه اخیر", "gravityformspayping" ); + + if ( $xmonth == 12 ) { + $n = __( " یکسال اخیر", "gravityformspayping" ); + } + } + if ( $xmonth == 1 || $xmonth == 2 || $xmonth == 60 ) { + $mt = 1; + } + if ( $xmonth == 3 || $xmonth == 6 ) { + $mt = 5; + } + if ( $xmonth == 9 || $xmonth == 12 ) { + $mt = 10; + } + + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: shamsi_1, minTickSize:[$mt, 'day'],min: (new Date('$ebtbaz 00:00:00')).getTime(),max: (new Date('$entbaz 23:59:59')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + + if ( $xmonth == 1 || $xmonth == 2 ) { + $n = GF_jdate( 'F', strtotime( $today ), '', date_default_timezone_get(), 'en' ); + $n = $n . __( " ماه", 'gravityformspayping' ); + } + + if ( $xmonth == 60 || $xmonth == 3 || $xmonth == 6 || $xmonth == 9 || $xmonth == 12 ) { + + $n = $xmonth; + + if ( $xmonth == 60 ) { + $n = 2; + } + + $n = $n . __( ' ماه اخیر', 'gravityformspayping' ); + + if ( $xmonth == 12 ) { + $n = __( 'یک سال اخیر', 'gravityformspayping' ); + } + } + + $sales_label = __( 'تعداد پرداخت های ', 'gravityformspayping' ) . $n . ' ' . $t; + + $strd = date_create( $strd ); + $endd = date_create( $endd ); + $diff = date_diff( $strd, $endd ); + $midd = $diff->format( "%a" ) + 1; + $midt = $midd ? $sales_thistday / $midd : 0; + $midt = number_format( $midt, 3, '.', '' ) . __( " در روز", 'gravityformspayping' ); + $midt_label = __( 'میانگین تعداد پرداخت های ', 'gravityformspayping' ) . $n . ' ' . $t; + $mid = ( $midd ? GFCommon::to_money( $revenue_thistday / $midd ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $mid_label = __( 'میانگین پرداخت های ', 'gravityformspayping' ) . $n . ' ' . $t; + $revenue_label = __( 'جمع پرداخت های ', 'gravityformspayping' ) . $n . ' ' . $t; + $revenue_thistday = GFCommon::to_money( $revenue_thistday ); + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_thistday, + "sales_label" => $sales_label, + "sales" => $sales_thistday, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + + public static function tyday_chart_info( $form_id, $chart, $day ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + + $series = $options = $datat = $tooltips = $revenue_label = $revenue_today = $sales_label = $sales_today = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY hour(date) , day(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY hour(date) , day(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "color: '#EDC240'}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY hour(date) , day(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرم های سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY hour(date) , day(date) + ORDER BY payment_date desc" ); + } + + $sales_today = 0; + $revenue_today = 0; + $tooltips = ""; + $n = ''; + $today = date( 'Y-m-d H:i:s', $tday ); + $date = new DateTime( $today ); + if ( $day == 1 ) { + $n = __( "امروز", 'gravityformspayping' ); + $baze = date( 'm d , Y', $tday ); + $ty = date( 'Ymd', $tday ); + } else if ( $day == 2 ) { + $n = __( "دیروز", 'gravityformspayping' ); + $date->sub( new DateInterval( 'P1DT0H0M' ) ); + $baze = $date->format( 'm d , Y' ); + $ty = $date->format( 'Ymd' ); + } + + if ( ! empty( $results ) ) { + + $data = "["; + foreach ( $results as $result ) { + + $h = GF_jdate( 'H', strtotime( $result->date ), '', date_default_timezone_get(), 'en' ); + $h = intval( $h ) + 1; + if ( $h < 10 ) { + $h = "0" . $h; + } + + $timeX_tooltips = GF_jdate( "l - d F Y ساعت H تا $h", strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $target = date( 'Ymd', strtotime( $result->date ) ); + $date = new DateTime( $result->date ); + $H = $date->format( 'H' ); + $H = intval( $H ) + 1; + + if ( $H < 10 ) { + $H = "0" . $H; + } + $d = $date->format( 'd' ); + $m = $date->format( 'm' ); + $y = $date->format( 'Y' ); + + if ( $target == $ty ) { + $sales_today += $result->new_sales; + $revenue_today += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + + $data .= "[(new Date('$m $d , $y $H:00:30')).getTime(),{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: shamsi_2, tickSize:[1, 'hour'], + min: (new Date('$baze 00:00:00')).getTime(),max: (new Date('$baze 24:59:00')).getTime()}, + yaxis: {tickFormatter: convertToMoney}, + bars: {show:true, align:'right', barWidth: (1 * 59 * 60 * 1000)}, + colors: ['#a3bcd3', '#14568a'], + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'} + }"; + } + + $sales_label = __( "تعداد پرداخت های ", "gravityformspayping" ) . $n . " " . $t; + + $midt = $sales_today / 24; + $midt = number_format( $midt, 3, '.', '' ) . __( " در ساعت", "gravityformspayping" ); + $midt_label = __( "میانگین تعداد پرداخت های ", "gravityformspayping" ) . $n . " " . $t; + $mid = GFCommon::to_money( $revenue_today / 24 ) . __( " در ساعت", "gravityformspayping" ); + $mid_label = __( "میانگین پرداخت های ", "gravityformspayping" ) . $n . " " . $t; + $revenue_today = GFCommon::to_money( $revenue_today ); + $revenue_label = __( "جمع پرداخت های ", "gravityformspayping" ) . $n . " " . $t; + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_today, + "sales_label" => $sales_label, + "sales" => $sales_today, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + public static function yearly_chart_info( $form_id, $chart ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + + $t = ''; + $series = $options = $datat = $tooltips = $revenue_label = $revenue_season = $sales_label = $sales_season = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + group by date + order by date desc" ); + } + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + group by date + order by date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + group by date + order by date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرمهای سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + group by date + order by date desc" ); + } + + $sales_yearly = 0; + $revenue_yearly = 0; + $tooltips = ""; + + $emsal = date( 'Y', $tday ); + $kabise = GF_jdate( 'L', $tday, '', date_default_timezone_get(), 'en' ); + if ( $kabise == 1 ) { + $avalesal = new DateTime( "$emsal-03-20" ); + $emsal ++; + $akharesal = new DateTime( "$emsal-03-19" ); + } else { + $avalesal = new DateTime( "$emsal-03-21" ); + $emsal ++; + $akharesal = new DateTime( "$emsal-03-20" ); + } + + $avalesal_w = $avalesal->format( 'Ymd' ); + $strd = $avalesal->format( 'Y-m-d' ); + $avalesal = $avalesal->format( 'm d , Y' ); + $akharesal_w = $akharesal->format( 'Ymd' ); + $endd = $akharesal->format( 'Y-m-d' ); + $akharesal = $akharesal->format( 'm d , Y' ); + + if ( ! empty( $results ) ) { + $data = "["; + foreach ( $results as $result ) { + // + $timeX_tooltips = GF_jdate( "d F Y", strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + // + $target = date( 'Ymd', strtotime( $result->date ) ); + if ( $akharesal_w >= $target && $target >= $avalesal_w ) { + $sales_yearly += $result->new_sales; + $revenue_yearly += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: shamsi_1, minTickSize:[10, 'day'],min: (new Date('$avalesal 00:00:00')).getTime(),max: (new Date('$akharesal 00:00:00')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + + $sales_label = __( "تعداد پرداخت های امسال ", "gravityformspayping" ) . $t; + + $strd = date_create( $strd ); + $endd = date_create( $endd ); + $diff = date_diff( $strd, $endd ); + $midd = $diff->format( "%a" ) + 1; + $midt = $midd ? $sales_yearly / $midd : 0; + $midt = number_format( $midt, 3, '.', '' ) . __( " در روز", "gravityformspayping" ); + $midt_label = __( "میانگین تعداد پرداخت های امسال ", "gravityformspayping" ) . $t; + $mid = ( $midd ? GFCommon::to_money( $revenue_yearly / $midd ) : 0 ) . __( " در روز", "gravityformspayping" ); + $mid_label = __( "میانگین پرداخت های امسال ", "gravityformspayping" ) . $t; + $revenue_yearly = GFCommon::to_money( $revenue_yearly ); + $revenue_label = __( "جمع پرداخت های امسال ", "gravityformspayping" ) . $t; + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_yearly, + "sales_label" => $sales_label, + "sales" => $sales_yearly, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + + public static function season_chart_info( $form_id, $chart, $season ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + $tday = $tz['today']; + + $t = ''; + $midd = ''; + $series = $options = $tooltips = $revenue_label = $revenue_today = $sales_label = $sales_today = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرم های سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + $sales_season = 0; + $revenue_season = 0; + $tooltips = ""; + + $today = date( 'Y-m-d', $tday ); + $avalesal = strtotime( $today ) - ( GF_jdate( 'z', $tday, '', date_default_timezone_get(), 'en' ) * 86400 ); + $avalesal = date( 'm d , Y', $avalesal ); + $akharesal = strtotime( $today ) + ( GF_jdate( 'Q', $tday, '', date_default_timezone_get(), 'en' ) * 86400 ); + $akharesal = date( 'm d , Y', $akharesal ); + list( $m, $d, $n, $y ) = explode( " ", $avalesal ); + + $date = new DateTime( "$y-$m-$d" ); + $avalesal_w = $date->format( 'Ymd' ); + $avalesal_t = $date->format( 'Y-m-d' ); + list( $m, $d, $n, $y ) = explode( " ", $akharesal ); + + $date = new DateTime( "$y-$m-$d" ); + $akharesal_w = $date->format( 'Ymd' ); + $akharesal_t = $date->format( 'Y-m-d' ); + $endd = $akharesal_t; + + if ( $season == 1 ) { + $n = __( 'بهار', 'gravityformspayping' ); + $ebtda = $avalesal_t; + $enteha = strtotime( $ebtda ) + ( 93 * 86400 ) - 86400; + $enteha = date( 'm d , Y', $enteha ); + $ebtda = $avalesal; + $midd = 93; + } + + if ( $season == 2 ) { + $n = __( 'تابستان', 'gravityformspayping' ); + $ebtda = $avalesal_t; + $ebtda = strtotime( $ebtda ) + ( 93 * 86400 ); + $ebtda = date( 'm d , Y', $ebtda ); + $enteha = $avalesal_t; + $enteha = strtotime( $enteha ) + ( 186 * 86400 ) - 86400; + $enteha = date( 'm d , Y', $enteha ); + $midd = 93; + } + + if ( $season == 3 ) { + $n = __( 'پاییز', 'gravityformspayping' ); + $ebtda = $avalesal_t; + $ebtda = strtotime( $ebtda ) + ( 186 * 86400 ); + $ebtda = date( 'm d , Y', $ebtda ); + $enteha = $avalesal_t; + $enteha = strtotime( $enteha ) + ( 276 * 86400 ) - 86400; + $enteha = date( 'm d , Y', $enteha ); + $midd = 90; + } + + if ( $season == 4 ) { + $n = __( 'زمستان', 'gravityformspayping' ); + $ebtda = $avalesal_t; + $ebtda = strtotime( $ebtda ) + ( 276 * 86400 ); + $strd = date( 'Y-m-d', $ebtda ); + $ebtda = date( 'm d , Y', $ebtda ); + $strd = date_create( $strd ); + $endd = date_create( $endd ); + $diff = date_diff( $strd, $endd ); + $midd = $diff->format( "%a" ) + 1; + $enteha = $akharesal; + } + + if ( ! empty( $results ) ) { + + $data = "["; + foreach ( $results as $result ) { + + $timeX_tooltips = GF_jdate( 'l - d F Y', strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + + $faslt = GF_jdate( 'b', strtotime( $result->date ), '', date_default_timezone_get(), 'en' ); + $target = date( 'Ymd', strtotime( $result->date ) ); + if ( ( $akharesal_w >= $target && $target >= $avalesal_w && $faslt == $season ) || ( $enteha >= $target && $target >= $ebtda ) ) { + $sales_season += $result->new_sales; + $revenue_season += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: shamsi_1, minTickSize:[3, 'day'],min: (new Date('$ebtda 00:00:00')).getTime(),max: (new Date('$enteha 23:59:59')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + + + $sales_label = __( "تعداد پرداخت های ", 'gravityformspayping' ) . $n . " " . $t; + + $midt = $midt ? $sales_season / $midd : 0; + $midt = ( $midt ? number_format( $midt, 3, '.', '' ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $midt_label = __( "میانگین تعداد پرداخت های ", 'gravityformspayping' ) . $n . " " . $t; + $mid = ( $midd ? GFCommon::to_money( $revenue_season / $midd ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $mid_label = __( "میانگین پرداخت های ", 'gravityformspayping' ) . $n . " " . $t; + $revenue_label = __( "جمع پرداخت های ", 'gravityformspayping' ) . $n . " " . $t; + $revenue_season = GFCommon::to_money( $revenue_season ); + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_season, + "sales_label" => $sales_label, + "sales" => $sales_season, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + + + public static function selection_chart_info( $form_id, $chart, $min, $max ) { + + global $wpdb; + $tz = self::get_mysql_tz_offset(); + $tz_offset = esc_sql( $tz['tz'] ); + + + $midd = ''; + $t = $n = ''; + + $series = $options = $datat = $tooltips = $revenue_label = $revenue_today = $sales_label = $sales_today = $mid_label = $mid = $midt_label = $midt = ''; + + if ( $chart == 1 ) { + $c = 'blue'; + $dt = "points: { symbol: 'diamond', fillColor: '#058DC7' }, color: '#058DC7'}"; + $t = __( "پی‌پینگ این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 2 ) { + $c = 'green'; + $dt = "points: { symbol: 'square', fillColor: '#50B432' }, color: '#50B432'}"; + $t = __( "همه روشهای این فرم", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE form_id={$form_id} AND l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 3 ) { + $c = 'orang'; + $dt = "}"; + $t = __( "همه فرمهای پی‌پینگ", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 AND l.payment_method='payping' + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + if ( $chart == 4 ) { + $c = 'red'; + $dt = "points: { symbol: 'triangle', fillColor: '#AA4643' }, color: '#AA4643'}"; + $t = __( "همه فرمهای سایت", 'gravityformspayping' ); + $results = $wpdb->get_results( "SELECT CONVERT_TZ(l.payment_date, '+00:00', '{$tz_offset}') as date, sum(l.payment_amount) as amount_sold, count(l.id) as new_sales + FROM " . GFPersian_DB_payping::get_entry_table_name() . " l + WHERE l.status='active' AND l.is_fulfilled=1 + GROUP BY date(date) + ORDER BY payment_date desc" ); + } + + $sales_today = 0; + $revenue_today = 0; + $tooltips = ""; + if ( ! empty( $results ) && isset( $_POST['submit'] ) && $max && $min ) { + list( $y2, $m2, $d2 ) = explode( "-", $max ); + + if ( $y2 < 2000 ) { + $max = GF_jalali_to_gregorian( $y2, $m2, $d2 ); + $date = new DateTime( "$max[0]-$max[1]-$max[2]" ); + } else { + $date = new DateTime( "$y2-$m2-$d2" ); + } + + $max_w = $date->format( 'Ymd' ); + $max_t = $date->format( 'm d , Y' ); + $endd = $date->format( 'Y-m-d' ); + + list( $y1, $m1, $d1 ) = explode( "-", $min ); + + if ( $y1 < 2000 ) { + $min = GF_jalali_to_gregorian( $y1, $m1, $d1 ); + $date = new DateTime( "$min[0]-$min[1]-$min[2]" ); + } else { + $date = new DateTime( "$y1-$m1-$d1" ); + } + + $min_w = $date->format( 'Ymd' ); + $min_t = $date->format( 'm d , Y' ); + $strd = $date->format( 'Y-m-d' ); + $data = "["; + foreach ( $results as $result ) { + + $timeX_tooltips = GF_jdate( 'l - d F Y', strtotime( $result->date ), '', date_default_timezone_get(), 'fa' ); + $timeX = self::get_graph_timestamp( $result->date ); + + $target = date( 'Ymd', strtotime( $result->date ) ); + if ( $max_w >= $target && $target >= $min_w ) { + $sales_today += $result->new_sales; + $revenue_today += $result->amount_sold; + $datat = isset( $result->amount_sold ) ? $result->amount_sold : 0; + } + $datat = isset( $datat ) ? $datat : ''; + $data .= "[{$timeX},{$datat}],"; + + $sales_line = "
" . __( "تعداد پرداخت ", "gravityformspayping" ) . ": " . $result->new_sales . "
"; + + $tooltips .= "\"
" . $timeX_tooltips . "
{$sales_line}
" . __( "پرداختی", "gravityformspayping" ) . ": " . GFCommon::to_money( $result->amount_sold ) . "
\","; + } + + $data = substr( $data, 0, strlen( $data ) - 1 ); + $tooltips = substr( $tooltips, 0, strlen( $tooltips ) - 1 ); + $data .= "]"; + $series = "{data:" . $data . ", " . $dt . ""; + + $strd = date_create( $strd ); + $endd = date_create( $endd ); + $diff = date_diff( $strd, $endd ); + $midd = $diff->format( "%a" ) + 1; + $mt = 1; + $tt = 'day'; + if ( $midd > 30 ) { + $mt = 5; + } + if ( $midd > 63 ) { + $mt = 10; + } + if ( $midd > 100 ) { + $mt = 20; + } + if ( $midd > 364 ) { + $mt = 1; + $tt = 'month'; + } + $options = "{ + series: {lines: {show: true}, + points: {show: true}}, + grid: {hoverable: true, clickable: true, tickColor: '#F1F1F1', backgroundColor:'#FFF', borderWidth: 1, borderColor: '#CCC'}, + xaxis: {mode: 'time',timeformat: '%d',tickFormatter: shamsi_1, minTickSize:[$mt, '$tt'],min: (new Date('$min_t 00:00:00')).getTime(),max: (new Date('$max_t 23:59:59')).getTime()}, + yaxis: {tickFormatter: convertToMoney} + }"; + } + + $sales_label = __( "تعداد پرداخت های بازه انتخابی ", 'gravityformspayping' ) . $t; + + $midt = $midd ? $sales_today / $midd : 0; + $midt = number_format( $midt, 3, '.', '' ) . __( " در روز", 'gravityformspayping' ); + $midt_label = __( "میانگین تعداد پرداخت های ", 'gravityformspayping' ) . $t . ""; + $mid = ( $midd ? GFCommon::to_money( $revenue_today / $midd ) : 0 ) . __( " در روز", 'gravityformspayping' ); + $mid_label = __( "میانگین پرداخت های ", 'gravityformspayping' ) . $t . ""; + $revenue_today = GFCommon::to_money( $revenue_today ); + $revenue_label = __( "جمع پرداخت های بازه انتخابی ", 'gravityformspayping' ) . $t; + + return array( + "series" => $series, + "options" => $options, + "tooltips" => "[$tooltips]", + "revenue_label" => $revenue_label, + "revenue" => $revenue_today, + "sales_label" => $sales_label, + "sales" => $sales_today, + "mid_label" => $mid_label, + "mid" => $mid, + "midt_label" => $midt_label, + "midt" => $midt + ); + } + + +} \ No newline at end of file diff --git a/database.php b/database.php new file mode 100644 index 0000000..61dfe0a --- /dev/null +++ b/database.php @@ -0,0 +1,237 @@ +prefix . "rg_payping"; + if ( $wpdb->get_var( "SHOW TABLES LIKE '$old_table'" ) ) { + $wpdb->query( "RENAME TABLE $old_table TO $table_name" ); + } + + $charset_collate = ''; + if ( ! empty( $wpdb->charset ) ) { + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; + } + if ( ! empty( $wpdb->collate ) ) { + $charset_collate .= " COLLATE $wpdb->collate"; + } + + $feed = "CREATE TABLE IF NOT EXISTS $table_name ( + id mediumint(8) unsigned not null auto_increment, + form_id mediumint(8) unsigned not null, + is_active tinyint(1) not null default 1, + meta longtext, + PRIMARY KEY (id), + KEY form_id (form_id) + )$charset_collate;"; + + require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); + dbDelta( $feed ); + } + + public static function get_table_name() { + global $wpdb; + + return $wpdb->prefix . "gf_payping"; + } + + public static function get_entry_table_name() { + + $version = GFCommon::$version; + if ( method_exists( 'GFFormsModel', 'get_database_version' ) ) { + $version = GFFormsModel::get_database_version(); + } + + return version_compare( $version, '2.3-dev-1', '<' ) ? GFFormsModel::get_lead_table_name() : GFFormsModel::get_entry_table_name(); + } + + public static function drop_tables() { + global $wpdb; + $wpdb->query( "DROP TABLE IF EXISTS " . self::get_table_name() ); + } + + public static function get_available_forms() { + $forms = RGFormsModel::get_forms(); + $available_forms = array(); + foreach ( $forms as $form ) { + $available_forms[] = $form; + } + + return $available_forms; + } + + public static function get_feed( $id ) { + global $wpdb; + $table_name = self::get_table_name(); + $sql = $wpdb->prepare( "SELECT id, form_id, is_active, meta FROM $table_name WHERE id=%d", $id ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + if ( empty( $results ) ) { + return array(); + } + $result = $results[0]; + $result["meta"] = maybe_unserialize( $result["meta"] ); + + return $result; + } + + public static function get_feeds() { + global $wpdb; + $table_name = self::get_table_name(); + $form_table_name = RGFormsModel::get_form_table_name(); + $sql = "SELECT s.id, s.is_active, s.form_id, s.meta, f.title as form_title + FROM $table_name s + INNER JOIN $form_table_name f ON s.form_id = f.id"; + $results = $wpdb->get_results( $sql, ARRAY_A ); + $count = sizeof( $results ); + for ( $i = 0; $i < $count; $i ++ ) { + $results[ $i ]["meta"] = maybe_unserialize( $results[ $i ]["meta"] ); + } + + return $results; + } + + public static function get_feed_by_form( $form_id, $only_active = false ) { + global $wpdb; + $table_name = self::get_table_name(); + $active_clause = $only_active ? " AND is_active=1" : ""; + $sql = $wpdb->prepare( "SELECT id, form_id, is_active, meta FROM $table_name WHERE form_id=%d $active_clause", $form_id ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + if ( empty( $results ) ) { + return array(); + } + $count = sizeof( $results ); + for ( $i = 0; $i < $count; $i ++ ) { + $results[ $i ]["meta"] = maybe_unserialize( $results[ $i ]["meta"] ); + } + + return $results; + } + + public static function update_feed( $id, $form_id, $is_active, $setting ) { + global $wpdb; + $table_name = self::get_table_name(); + $setting = maybe_serialize( $setting ); + if ( $id == 0 ) { + $wpdb->insert( $table_name, array( + "form_id" => $form_id, + "is_active" => $is_active, + "meta" => $setting + ), array( "%d", "%d", "%s" ) ); + $id = $wpdb->get_var( "SELECT LAST_INSERT_ID()" ); + } else { + $wpdb->update( $table_name, array( + "form_id" => $form_id, + "is_active" => $is_active, + "meta" => $setting + ), array( "id" => $id ), array( "%d", "%d", "%s" ), array( "%d" ) ); + } + + return $id; + } + + public static function delete_feed( $id ) { + global $wpdb; + $table_name = self::get_table_name(); + $wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE id=%s", $id ) ); + } + + //----جمع پرداخت های این درگاه این فرم---------- + public static function get_transaction_totals( $form_id ) { + global $wpdb; + $entry_table_name = self::get_entry_table_name(); + $sql = $wpdb->prepare( " SELECT l.status, sum(l.payment_amount) revenue, count(l.id) transactions + FROM {$entry_table_name} l + WHERE l.form_id=%d AND l.status=%s AND l.is_fulfilled=%d AND l.payment_method=%s + GROUP BY l.status", $form_id, 'active', 1, self::$method ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + $totals = array(); + if ( is_array( $results ) ) { + foreach ( $results as $result ) { + $totals[ $result["status"] ] = array( + "revenue" => empty( $result["revenue"] ) ? 0 : $result["revenue"], + "transactions" => empty( $result["transactions"] ) ? 0 : $result["transactions"] + ); + } + } + + return $totals; + } + + //-----جمع پرداخت های همه فرمهای این درگاه----------- + public static function get_transaction_totals_this_gateway() { + global $wpdb; + $entry_table_name = self::get_entry_table_name(); + $sql = $wpdb->prepare( " SELECT l.status, sum(l.payment_amount) revenue, count(l.id) transactions + FROM {$entry_table_name} l + WHERE l.status=%s AND l.is_fulfilled=%d AND l.payment_method=%s + GROUP BY l.status", 'active', 1, self::$method ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + $totals = array(); + if ( is_array( $results ) ) { + foreach ( $results as $result ) { + $totals[ $result["status"] ] = array( + "revenue" => empty( $result["revenue"] ) ? 0 : $result["revenue"], + "transactions" => empty( $result["transactions"] ) ? 0 : $result["transactions"] + ); + } + } + + return $totals; + } + + //-----جمع پرداخت های همه روشهای این فرم--------------- + public static function get_transaction_totals_gateways( $form_id ) { + global $wpdb; + $entry_table_name = self::get_entry_table_name(); + $sql = $wpdb->prepare( " SELECT l.status, sum(l.payment_amount) revenue, count(l.id) transactions + FROM {$entry_table_name} l + WHERE l.form_id=%d AND l.status=%s AND l.is_fulfilled=%d + GROUP BY l.status", $form_id, 'active', 1 ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + $totals = array(); + if ( is_array( $results ) ) { + foreach ( $results as $result ) { + $totals[ $result["status"] ] = array( + "revenue" => empty( $result["revenue"] ) ? 0 : $result["revenue"], + "transactions" => empty( $result["transactions"] ) ? 0 : $result["transactions"] + ); + } + } + + return $totals; + } + + //-----جمع کل پرداخت های همه فرمهای سایت------------- + public static function get_transaction_totals_site() { + global $wpdb; + $entry_table_name = self::get_entry_table_name(); + $sql = $wpdb->prepare( " SELECT l.status, sum(l.payment_amount) revenue, count(l.id) transactions + FROM {$entry_table_name} l + WHERE l.status=%s AND l.is_fulfilled=%d + GROUP BY l.status", 'active', 1 ); + $results = $wpdb->get_results( $sql, ARRAY_A ); + $totals = array(); + if ( is_array( $results ) ) { + foreach ( $results as $result ) { + $totals[ $result["status"] ] = array( + "revenue" => empty( $result["revenue"] ) ? 0 : $result["revenue"], + "transactions" => empty( $result["transactions"] ) ? 0 : $result["transactions"] + ); + } + } + + return $totals; + } + +} \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..f1ea4cd --- /dev/null +++ b/index.php @@ -0,0 +1,12 @@ + 'gf_payping', + 'tab_label' => __( 'درگاه پی‌پینگ', 'gravityformspayping' ), + 'title' => __( 'تنظیمات درگاه پی‌پینگ', 'gravityformspayping' ), + 'handler' => array( __CLASS__, 'settings_page' ), + ) + ); + } + + if ( self::is_payping_page() ) { + wp_enqueue_script( array( "sack" ) ); + self::setup(); + } + + add_action( 'wp_ajax_gf_payping_update_feed_active', array( __CLASS__, 'update_feed_active' ) ); + } + if ( get_option( "gf_payping_configured" ) ) { + add_filter( "gform_disable_post_creation", array( __CLASS__, "delay_posts" ), 10, 3 ); + add_filter( "gform_is_delayed_pre_process_feed", array( __CLASS__, "delay_addons" ), 10, 4 ); + + add_filter( "gform_confirmation", array( __CLASS__, "Request" ), 1000, 4 ); + add_action( 'wp', array( __CLASS__, 'Verify' ), 5 ); + } + + add_filter( "gform_logging_supported", array( __CLASS__, "set_logging_supported" ) ); + + // -------------------------------------------------------------------------------------------- + add_filter( 'gf_payment_gateways', array( __CLASS__, 'gravityformspayping' ), 2 ); + do_action( 'gravityforms_gateways' ); + do_action( 'gravityforms_payping' ); + // -------------------------------------------------------------------------------------------- + } + + + // ------------------------------------------------- + public static function admin_notice_persian_gf() { + $class = 'notice notice-error'; + $message = sprintf( __( "برای استفاده از نسخه جدید درگاه های پرداخت گرویتی فرم نصب بسته فارسی ساز نسخه 2.3.1 به بالا الزامی است. برای نصب فارسی ساز %sکلیک کنید%s.", "gravityformspayping" ), '', '' ); + printf( '

%2$s

', $class, $message ); + } + + // ------------------------------------------------- + public static function admin_notice_gf_support() { + $class = 'notice notice-error'; + $message = sprintf( __( "درگاه پی‌پینگ نیاز به گرویتی فرم نسخه %s به بالا دارد. برای بروز رسانی هسته گرویتی فرم به %sسایت گرویتی فرم فارسی%s مراجعه نمایید .", "gravityformspayping" ), self::$min_gravityforms_version, "", "" ); + printf( '

%2$s

', $class, $message ); + } + + + // #1 + // ------------------------------------------------- + public static function gravityformspayping( $form, $entry ) { + $payping = array( + 'class' => ( __CLASS__ . '|' . self::$author ), + 'title' => __( 'پی‌پینگ', 'gravityformspayping' ), + 'param' => array( + 'email' => __( 'ایمیل', 'gravityformspayping' ), + 'mobile' => __( 'موبایل', 'gravityformspayping' ), + 'desc' => __( 'توضیحات', 'gravityformspayping' ) + ) + ); + + return apply_filters( self::$author . '_gf_payping_detail', apply_filters( self::$author . '_gf_gateway_detail', $payping, $form, $entry ), $form, $entry ); + } + + // ------------------------------------------------- + public static function add_permissions() { + global $wp_roles; + $editable_roles = get_editable_roles(); + foreach ( (array) $editable_roles as $role => $details ) { + if ( $role == 'administrator' || in_array( 'gravityforms_edit_forms', $details['capabilities'] ) ) { + $wp_roles->add_cap( $role, 'gravityforms_payping' ); + $wp_roles->add_cap( $role, 'gravityforms_payping_uninstall' ); + } + } + } + + // ------------------------------------------------- + public static function members_get_capabilities( $caps ) { + return array_merge( $caps, array( "gravityforms_payping", "gravityforms_payping_uninstall" ) ); + } + + // ------------------------------------------------- + private static function setup() { + if ( get_option( "gf_payping_version" ) != self::$version ) { + GFPersian_DB_payping::update_table(); + update_option( "gf_payping_version", self::$version ); + } + } + + // ------------------------------------------------- + public static function tooltips( $tooltips ) { + $tooltips["gateway_name"] = __( "تذکر مهم : این قسمت برای نمایش به بازدید کننده می باشد و لطفا جهت جلوگیری از مشکل و تداخل آن را فقط یکبار تنظیم نمایید و از تنظیم مکرر آن خود داری نمایید .", "gravityformspayping" ); + + return $tooltips; + } + + // ------------------------------------------------- + public static function menu( $menus ) { + $permission = "gravityforms_payping"; + if ( ! empty( $permission ) ) { + $menus[] = array( + "name" => "gf_payping", + "label" => __( "پی‌پینگ", "gravityformspayping" ), + "callback" => array( __CLASS__, "payping_page" ), + "permission" => $permission + ); + } + + return $menus; + } + + // ------------------------------------------------- + public static function toolbar( $menu_items ) { + $menu_items[] = array( + 'name' => 'payping', + 'label' => __( 'پی‌پینگ', 'gravityformspayping' ) + ); + + return $menu_items; + } + + // ------------------------------------------------- + private static function is_gravityforms_supported() { + if ( class_exists( "GFCommon" ) ) { + $is_correct_version = version_compare( GFCommon::$version, self::$min_gravityforms_version, ">=" ); + + return $is_correct_version; + } else { + return false; + } + } + + // ------------------------------------------------- + protected static function has_access( $required_permission = 'gravityforms_payping' ) { + if ( ! function_exists( 'wp_get_current_user' ) ) { + include( ABSPATH . "wp-includes/pluggable.php" ); + } + + return GFCommon::current_user_can_any( $required_permission ); + } + + // ------------------------------------------------- + protected static function get_base_url() { + return plugins_url( null, __FILE__ ); + } + + // ------------------------------------------------- + protected static function get_base_path() { + $folder = basename( dirname( __FILE__ ) ); + + return WP_PLUGIN_DIR . "/" . $folder; + } + + // ------------------------------------------------- + public static function set_logging_supported( $plugins ) { + $plugins[ basename( dirname( __FILE__ ) ) ] = "payping"; + + return $plugins; + } + + // ------------------------------------------------- + public static function uninstall() { + if ( ! self::has_access( "gravityforms_payping_uninstall" ) ) { + die( __( "شما مجوز کافی برای این کار را ندارید . سطح دسترسی شما پایین تر از حد مجاز است . ", "gravityformspayping" ) ); + } + GFPersian_DB_payping::drop_tables(); + delete_option( "gf_payping_settings" ); + delete_option( "gf_payping_configured" ); + delete_option( "gf_payping_version" ); + $plugin = basename( dirname( __FILE__ ) ) . "/index.php"; + deactivate_plugins( $plugin ); + update_option( 'recently_activated', array( $plugin => time() ) + (array) get_option( 'recently_activated' ) ); + } + + // ------------------------------------------------- + private static function is_payping_page() { + $current_page = in_array( trim( strtolower( rgget( "page" ) ) ), array( 'gf_payping', 'payping' ) ); + $current_view = in_array( trim( strtolower( rgget( "view" ) ) ), array( 'gf_payping', 'payping' ) ); + $current_subview = in_array( trim( strtolower( rgget( "subview" ) ) ), array( 'gf_payping', 'payping' ) ); + + return $current_page || $current_view || $current_subview; + } + + // ------------------------------------------------- + public static function feed_page() { + GFFormSettings::page_header(); ?> +

+ + "> + "> +

+ + $condition_field_ids ); + } + } else { + return true; + } + + if ( ! empty( $config['meta']['payping_conditional_value'] ) ) { + $condition_values = $config['meta']['payping_conditional_value']; + if ( ! is_array( $condition_values ) ) { + $condition_values = array( '1' => $condition_values ); + } + } else { + $condition_values = array( '1' => '' ); + } + + if ( ! empty( $config['meta']['payping_conditional_operator'] ) ) { + $condition_operators = $config['meta']['payping_conditional_operator']; + if ( ! is_array( $condition_operators ) ) { + $condition_operators = array( '1' => $condition_operators ); + } + } else { + $condition_operators = array( '1' => 'is' ); + } + + $type = ! empty( $config['meta']['payping_conditional_type'] ) ? strtolower( $config['meta']['payping_conditional_type'] ) : ''; + $type = $type == 'all' ? 'all' : 'any'; + + foreach ( $condition_field_ids as $i => $field_id ) { + + if ( empty( $field_id ) ) { + continue; + } + + $field = RGFormsModel::get_field( $form, $field_id ); + if ( empty( $field ) ) { + continue; + } + + $value = ! empty( $condition_values[ '' . $i . '' ] ) ? $condition_values[ '' . $i . '' ] : ''; + $operator = ! empty( $condition_operators[ '' . $i . '' ] ) ? $condition_operators[ '' . $i . '' ] : 'is'; + + $is_visible = ! RGFormsModel::is_field_hidden( $form, $field, array() ); + $field_value = RGFormsModel::get_field_value( $field, array() ); + $is_value_match = RGFormsModel::is_value_match( $field_value, $value, $operator ); + $check = $is_value_match && $is_visible; + + if ( $type == 'any' && $check ) { + return true; + } else if ( $type == 'all' && ! $check ) { + return false; + } + } + + if ( $type == 'any' ) { + return false; + } else { + return true; + } + } + + // ------------------------------------------------- + public static function get_config_by_entry( $entry ) { + $feed_id = gform_get_meta( $entry["id"], "payping_feed_id" ); + $feed = ! empty( $feed_id ) ? GFPersian_DB_payping::get_feed( $feed_id ) : ''; + $return = ! empty( $feed ) ? $feed : false; + + return apply_filters( self::$author . '_gf_payping_get_config_by_entry', apply_filters( self::$author . '_gf_gateway_get_config_by_entry', $return, $entry ), $entry ); + } + + // ------------------------------------------------- + public static function delay_posts( $is_disabled, $form, $entry ) { + + $config = self::get_active_config( $form ); + + if ( ! empty( $config ) && is_array( $config ) && $config ) { + return true; + } + + return $is_disabled; + } + + // ------------------------------------------------- + public static function delay_addons( $is_delayed, $form, $entry, $slug ) { + + $config = self::get_active_config( $form ); + + if ( ! empty( $config["meta"] ) && is_array( $config["meta"] ) && $config = $config["meta"] ) { + + $user_registration_slug = apply_filters( 'gf_user_registration_slug', 'gravityformsuserregistration' ); + + if ( $slug != $user_registration_slug && ! empty( $config["addon"] ) && $config["addon"] == 'true' ) { + $flag = true; + } elseif ( $slug == $user_registration_slug && ! empty( $config["type"] ) && $config["type"] == "subscription" ) { + $flag = true; + } + + if ( ! empty( $flag ) ) { + $fulfilled = gform_get_meta( $entry['id'], $slug . '_is_fulfilled' ); + $processed = gform_get_meta( $entry['id'], 'processed_feeds' ); + + $is_delayed = empty( $fulfilled ) && rgempty( $slug, $processed ); + } + } + + return $is_delayed; + } + + // ------------------------------------------------- + private static function redirect_confirmation( $url, $ajax ) { + + if ( headers_sent() || $ajax ) { + $confirmation = "'; + } else { + $confirmation = array( 'redirect' => $url ); + } + + return $confirmation; + } + + // ------------------------------------------------- + public static function get_active_config( $form ) { + + if ( ! empty( self::$config ) ) { + return self::$config; + } + + $configs = GFPersian_DB_payping::get_feed_by_form( $form["id"], true ); + + $configs = apply_filters( self::$author . '_gf_payping_get_active_configs', apply_filters( self::$author . '_gf_gateway_get_active_configs', $configs, $form ), $form ); + + $return = false; + + if ( ! empty( $configs ) && is_array( $configs ) ) { + + foreach ( $configs as $config ) { + if ( self::has_payping_condition( $form, $config ) ) { + $return = $config; + } + break; + } + } + + self::$config = apply_filters( self::$author . '_gf_payping_get_active_config', apply_filters( self::$author . '_gf_gateway_get_active_config', $return, $form ), $form ); + + return self::$config; + } + + // ------------------------------------------------- + public static function payping_page() { + $view = rgget( "view" ); + if ( $view == "edit" ) { + self::config_page(); + } else if ( $view == "stats" ) { + GFPersian_Chart_payping::stats_page(); + } else { + self::list_page( '' ); + } + } + + // ------------------------------------------------- + private static function list_page( $arg ) { + + if ( ! self::is_gravityforms_supported() ) { + die( sprintf( __( "درگاه پی‌پینگ نیاز به گرویتی فرم نسخه %s دارد. برای بروز رسانی هسته گرویتی فرم به %sسایت گرویتی فرم فارسی%s مراجعه نمایید .", "gravityformspayping" ), self::$min_gravityforms_version, "", "" ) ); + } + + if ( rgpost( 'action' ) == "delete" ) { + check_admin_referer( "list_action", "gf_payping_list" ); + $id = absint( rgpost( "action_argument" ) ); + GFPersian_DB_payping::delete_feed( $id ); + ?> +
+
+ +
+ + + +

+ + + +

+ + + +
+ + + +
+
+ + + '; + ?> + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 ) { + foreach ( $settings as $setting ) { + ?> + + + + + + + + + + + + + + + + + + + + + +
+
+ ', "" ); ?> +
"/>.png" + alt="" + title="" + onclick="ToggleActive(this, ); "/> + + + + + " + title=""> + + + + +
+ ', "" ); + } else { + echo sprintf( __( "شما هیچ فید پی‌پینگی ندارید . %sیکی بسازید%s .", "gravityformspayping" ), '', "" ); + } + ?> +
+
+
+ + $form_id, + 'entry' => $entry_id + ), $pageURL ) ); + + return apply_filters( self::$author . '_payping_return_url', apply_filters( self::$author . '_gateway_return_url', $pageURL, $form_id, $entry_id, __CLASS__ ), $form_id, $entry_id, __CLASS__ ); + } + + // ------------------------------------------------- + public static function get_order_total( $form, $entry ) { + + $total = GFCommon::get_order_total( $form, $entry ); + $total = ( ! empty( $total ) && $total > 0 ) ? $total : 0; + + return apply_filters( self::$author . '_payping_get_order_total', apply_filters( self::$author . '_gateway_get_order_total', $total, $form, $entry ), $form, $entry ); + } + + // ------------------------------------------------- + private static function get_mapped_field_list( $field_name, $selected_field, $fields ) { + $str = ""; + + return $str; + } + + // ------------------------------------------------- + private static function get_form_fields( $form ) { + $fields = array(); + if ( is_array( $form["fields"] ) ) { + foreach ( $form["fields"] as $field ) { + if ( isset( $field["inputs"] ) && is_array( $field["inputs"] ) ) { + foreach ( $field["inputs"] as $input ) { + $fields[] = array( $input["id"], GFCommon::get_label( $field, $input["id"] ) ); + } + } else if ( ! rgar( $field, 'displayOnly' ) ) { + $fields[] = array( $field["id"], GFCommon::get_label( $field ) ); + } + } + } + + return $fields; + } + + // --------------------------------------------------------------------------------------------- + //desc + private static function get_customer_information_desc( $form, $config = null ) { + $form_fields = self::get_form_fields( $form ); + $selected_field = ! empty( $config["meta"]["customer_fields_desc"] ) ? $config["meta"]["customer_fields_desc"] : ''; + + return self::get_mapped_field_list( 'payping_customer_field_desc', $selected_field, $form_fields ); + } + + //email + private static function get_customer_information_email( $form, $config = null ) { + $form_fields = self::get_form_fields( $form ); + $selected_field = ! empty( $config["meta"]["customer_fields_email"] ) ? $config["meta"]["customer_fields_email"] : ''; + + return self::get_mapped_field_list( 'payping_customer_field_email', $selected_field, $form_fields ); + } + + //mobile + private static function get_customer_information_mobile( $form, $config = null ) { + $form_fields = self::get_form_fields( $form ); + $selected_field = ! empty( $config["meta"]["customer_fields_mobile"] ) ? $config["meta"]["customer_fields_mobile"] : ''; + + return self::get_mapped_field_list( 'payping_customer_field_mobile', $selected_field, $form_fields ); + } + // ------------------------------------------------------------------------------------------------------------ + + + // ------------------------------------------------- + public static function payment_entry_detail( $form_id, $entry ) { + + $payment_gateway = rgar( $entry, "payment_method" ); + + if ( ! empty( $payment_gateway ) && $payment_gateway == "payping" ) { + + do_action( 'gf_gateway_entry_detail' ); + + ?> +
+ + + +
+
+ sub( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } else { + $date->add( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } + + $payment_date = $date->format( 'Y-m-d H:i:s' ); + $payment_date = GF_jdate( 'Y-m-d H:i:s', strtotime( $payment_date ), '', date_default_timezone_get(), 'en' ); + + if ( $payment_status == 'Paid' ) { + $payment_status_persian = __( 'موفق', 'gravityformspayping' ); + } + + if ( $payment_status == 'Active' ) { + $payment_status_persian = __( 'موفق', 'gravityformspayping' ); + } + + if ( $payment_status == 'Cancelled' ) { + $payment_status_persian = __( 'منصرف شده', 'gravityformspayping' ); + } + + if ( $payment_status == 'Failed' ) { + $payment_status_persian = __( 'ناموفق', 'gravityformspayping' ); + } + + if ( $payment_status == 'Processing' ) { + $payment_status_persian = __( 'معلق', 'gravityformspayping' ); + } + + if ( ! strtolower( rgpost( "save" ) ) || RGForms::post( "screen_mode" ) != "edit" ) { + echo __( 'وضعیت پرداخت : ', 'gravityformspayping' ) . $payment_status_persian . '

'; + echo __( 'تاریخ پرداخت : ', 'gravityformspayping' ) . '' . $payment_date . '

'; + echo __( 'مبلغ پرداختی : ', 'gravityformspayping' ) . GFCommon::to_money( $payment_amount, rgar( $entry, "currency" ) ) . '

'; + echo __( 'کد رهگیری : ', 'gravityformspayping' ) . $transaction_id . '

'; + echo __( 'درگاه پرداخت : پی‌پینگ', 'gravityformspayping' ); + } else { + $payment_string = ''; + $payment_string .= ''; + + echo __( 'وضعیت پرداخت :', 'gravityformspayping' ) . $payment_string . '

'; + ?> +
+ + + + + + + + + + + + + + +
+
+
+ '; + } + } + + // ------------------------------------------------- + public static function update_payment_entry( $form, $entry_id ) { + + check_admin_referer( 'gforms_save_entry', 'gforms_save_entry' ); + + do_action( 'gf_gateway_update_entry' ); + + $entry = GFPersian_Payments::get_entry( $entry_id ); + + $payment_gateway = rgar( $entry, "payment_method" ); + + if ( empty( $payment_gateway ) ) { + return; + } + + if ( $payment_gateway != "payping" ) { + return; + } + + $payment_status = rgpost( "payment_status" ); + if ( empty( $payment_status ) ) { + $payment_status = rgar( $entry, "payment_status" ); + } + + $payment_amount = rgpost( "payment_amount" ); + $payment_transaction = rgpost( "payping_transaction_id" ); + $payment_date_Checker = $payment_date = rgpost( "payment_date" ); + + list( $date, $time ) = explode( " ", $payment_date ); + list( $Y, $m, $d ) = explode( "-", $date ); + list( $H, $i, $s ) = explode( ":", $time ); + $miladi = GF_jalali_to_gregorian( $Y, $m, $d ); + + $date = new DateTime( "$miladi[0]-$miladi[1]-$miladi[2] $H:$i:$s" ); + $payment_date = $date->format( 'Y-m-d H:i:s' ); + + if ( empty( $payment_date_Checker ) ) { + if ( ! empty( $entry["payment_date"] ) ) { + $payment_date = $entry["payment_date"]; + } else { + $payment_date = rgar( $entry, "date_created" ); + } + } else { + $payment_date = date( "Y-m-d H:i:s", strtotime( $payment_date ) ); + $date = new DateTime( $payment_date ); + $tzb = get_option( 'gmt_offset' ); + $tzn = abs( $tzb ) * 3600; + $tzh = intval( gmdate( "H", $tzn ) ); + $tzm = intval( gmdate( "i", $tzn ) ); + if ( intval( $tzb ) < 0 ) { + $date->add( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } else { + $date->sub( new DateInterval( 'P0DT' . $tzh . 'H' . $tzm . 'M' ) ); + } + $payment_date = $date->format( 'Y-m-d H:i:s' ); + } + + global $current_user; + $user_id = 0; + $user_name = __( "مهمان", 'gravityformspayping' ); + if ( $current_user && $user_data = get_userdata( $current_user->ID ) ) { + $user_id = $current_user->ID; + $user_name = $user_data->display_name; + } + + $entry["payment_status"] = $payment_status; + $entry["payment_amount"] = $payment_amount; + $entry["payment_date"] = $payment_date; + $entry["transaction_id"] = $payment_transaction; + if ( $payment_status == 'Paid' || $payment_status == 'Active' ) { + $entry["is_fulfilled"] = 1; + } else { + $entry["is_fulfilled"] = 0; + } + GFAPI::update_entry( $entry ); + + $new_status = ''; + switch ( rgar( $entry, "payment_status" ) ) { + case "Active" : + $new_status = __( 'موفق', 'gravityformspayping' ); + break; + + case "Paid" : + $new_status = __( 'موفق', 'gravityformspayping' ); + break; + + case "Cancelled" : + $new_status = __( 'منصرف شده', 'gravityformspayping' ); + break; + + case "Failed" : + $new_status = __( 'ناموفق', 'gravityformspayping' ); + break; + + case "Processing" : + $new_status = __( 'معلق', 'gravityformspayping' ); + break; + } + + RGFormsModel::add_note( $entry["id"], $user_id, $user_name, sprintf( __( "اطلاعات تراکنش به صورت دستی ویرایش شد . وضعیت : %s - مبلغ : %s - کد رهگیری : %s - تاریخ : %s", "gravityformspayping" ), $new_status, GFCommon::to_money( $entry["payment_amount"], $entry["currency"] ), $payment_transaction, $entry["payment_date"] ) ); + + } + + // #2 + // ------------------------------------------------- + public static function settings_page() { + + + if ( rgpost( "uninstall" ) ) { + check_admin_referer( "uninstall", "gf_payping_uninstall" ); + self::uninstall(); + echo '
' . __( "درگاه با موفقیت غیرفعال شد و اطلاعات مربوط به آن نیز از بین رفت برای فعالسازی مجدد میتوانید از طریق افزونه های وردپرس اقدام نمایید .", "gravityformspayping" ) . '
'; + + return; + } else if ( isset( $_POST["gf_payping_submit"] ) ) { + + check_admin_referer( "update", "gf_payping_update" ); + $settings = array( + "merchent" => rgpost( 'gf_payping_merchent' ), + "gname" => rgpost( 'gf_payping_gname' ), + ); + update_option( "gf_payping_settings", array_map( 'sanitize_text_field', $settings ) ); + if ( isset( $_POST["gf_payping_configured"] ) ) { + update_option( "gf_payping_configured", sanitize_text_field( $_POST["gf_payping_configured"] ) ); + } else { + delete_option( "gf_payping_configured" ); + } + } else { + $settings = get_option( "gf_payping_settings" ); + } + + if ( ! empty( $_POST ) ) { + + if ( isset( $_POST["gf_payping_configured"] ) && ( $Response = self::Request( 'valid_checker', '', '', '' ) ) && $Response != false ) { + + if ( $Response === true ) { + echo '
' . __( "ارتباط با درگاه برقرار شد و اطلاعات وارد شده صحیح است .", "gravityformspayping" ) . '
'; + } else if ( $Response == 'sandbox' ) { + echo '
' . __( "در حالت تستی نیاز به ورود اطلاعات صحیح نمی باشد .", "gravityformspayping" ) . '
'; + } else { + echo '
' . $Response . '
'; + } + + } else { + echo '
' . __( "تنظیمات ذخیره شدند .", "gravityformspayping" ) . '
'; + } + } else if ( isset( $_GET['subview'] ) && $_GET['subview'] == 'gf_payping' && isset( $_GET['updated'] ) ) { + echo '
' . __( "تنظیمات ذخیره شدند .", "gravityformspayping" ) . '
'; + } + ?> + +
+ + + +

+ + + + +

+ + + + + + + + + + + + + + + + + + + + + + + + +
+ + /> + +
+ + +
+ + + +
"/>
+ +
+ +
+ +
+
+ +

+ + +

+ +
+ + '; + echo apply_filters( "gform_payping_uninstall_button", $uninstall_button ); + ?> + +
+ + +
+ + + + + + +
+ + array(), + "is_active" => true + ) : GFPersian_DB_payping::get_feed( $id ); + $get_feeds = GFPersian_DB_payping::get_feeds(); + $form_name = ''; + + + $_get_form_id = rgget( 'fid' ) ? rgget( 'fid' ) : ( ! empty( $config["form_id"] ) ? $config["form_id"] : '' ); + + foreach ( (array) $get_feeds as $get_feed ) { + if ( $get_feed['id'] == $id ) { + $form_name = $get_feed['form_title']; + } + } + ?> + + +

+ + + + + + + + +

+ + + $val ) { + if ( ! is_array( $val ) ) { + $safe_data[ $key ] = sanitize_text_field( $val ); + } else { + $safe_data[ $key ] = array_map( 'sanitize_text_field', $val ); + } + } + $config["meta"] = $safe_data; + + $config = apply_filters( self::$author . '_gform_gateway_save_config', $config ); + $config = apply_filters( self::$author . '_gform_payping_save_config', $config ); + + $id = GFPersian_DB_payping::update_feed( $id, $config["form_id"], $config["is_active"], $config["meta"] ); + if ( ! headers_sent() ) { + wp_redirect( admin_url( 'admin.php?page=gf_payping&view=edit&id=' . $id . '&updated=true' ) ); + exit; + } else { + echo ""; + exit; + } + ?> +
", "" ) ?>
+ + +
", "" ) ?>
+ + + +
+ +
+ + + '' ); + $condition_values = array( '1' => '' ); + $condition_operators = array( '1' => 'is' ); + ?> + +
+ + + +
+
+
+

+ + + + +

+
+ + + + + + + + + + + + + + +
+ + + + +
+ + +
+ +
+ + + cellspacing="0" cellpadding="0"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + > + + + + + > + + + + + + + + + + + + + + + + + + + + +
+ + + /> + + +
+ + + "/> + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + /> + + +
+ + + /> +
+
+ + + + +
+
> + + + + + + + $condition_field_ids ); + } + } + + if ( ! empty( $config["meta"]["payping_conditional_value"] ) ) { + $condition_values = $config["meta"]["payping_conditional_value"]; + if ( ! is_array( $condition_values ) ) { + $condition_values = array( '1' => $condition_values ); + } + } + + if ( ! empty( $config["meta"]["payping_conditional_operator"] ) ) { + $condition_operators = $config["meta"]["payping_conditional_operator"]; + if ( ! is_array( $condition_operators ) ) { + $condition_operators = array( '1' => $condition_operators ); + } + } + + ksort( $condition_field_ids ); + foreach ( $condition_field_ids as $i => $value ):?> + +
+ + + + + +
+
+ + + + + + + + +
+ + + + + + +
+
+
+ "/> +
+ +
+
+
+
+
+
+ + + + ID ) ) { + $user_id = $current_user->ID; + $user_name = $user_data->display_name; + } + + if ( ! $valid_checker ) { + + $entry_id = $entry['id']; + + if ( ! $custom ) { + + if ( RGForms::post( "gform_submit" ) != $form['id'] ) { + return $confirmation; + } + + $config = self::get_active_config( $form ); + if ( empty( $config ) ) { + return $confirmation; + } + + gform_update_meta( $entry['id'], 'payping_feed_id', $config['id'] ); + gform_update_meta( $entry['id'], 'payment_type', 'form' ); + gform_update_meta( $entry['id'], 'payment_gateway', self::get_gname() ); + + switch ( $config["meta"]["type"] ) { + case "subscription" : + $transaction_type = 2; + break; + + default : + $transaction_type = 1; + break; + } + + if ( GFCommon::has_post_field( $form["fields"] ) ) { + if ( ! empty( $config["meta"]["update_post_action2"] ) ) { + if ( $config["meta"]["update_post_action2"] != 'dont' ) { + if ( $config["meta"]["update_post_action2"] != 'default' ) { + $form['postStatus'] = $config["meta"]["update_post_action2"]; + } + } else { + $dont_create = true; + } + } + if ( empty( $dont_create ) ) { + RGFormsModel::create_post( $form, $entry ); + } + } + + $Amount = self::get_order_total( $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_form_gateway_price_{$form['id']}", apply_filters( self::$author . "_gform_form_gateway_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_form_payping_price_{$form['id']}", apply_filters( self::$author . "_gform_form_payping_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_gateway_price_{$form['id']}", apply_filters( self::$author . "_gform_gateway_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_payping_price_{$form['id']}", apply_filters( self::$author . "_gform_payping_price", $Amount, $form, $entry ), $form, $entry ); + + if ( empty( $Amount ) || ! $Amount || $Amount == 0 ) { + unset( $entry["payment_status"], $entry["payment_method"], $entry["is_fulfilled"], $entry["transaction_type"], $entry["payment_amount"], $entry["payment_date"] ); + $entry["payment_method"] = "payping"; + GFAPI::update_entry( $entry ); + + return self::redirect_confirmation( add_query_arg( array( 'no' => 'true' ), self::Return_URL( $form['id'], $entry['id'] ) ), $ajax ); + } else { + + $Desc1 = ''; + if ( ! empty( $config["meta"]["desc_pm"] ) ) { + $Desc1 = str_replace( array( '{entry_id}', '{form_title}', '{form_id}' ), array( + $entry['id'], + $form['title'], + $form['id'] + ), $config["meta"]["desc_pm"] ); + } + $Desc2 = ''; + if ( rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_desc"] ) ) ) { + $Desc2 = rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_desc"] ) ); + } + + if ( ! empty( $Desc1 ) && ! empty( $Desc2 ) ) { + $Description = $Desc1 . ' - ' . $Desc2; + } else if ( ! empty( $Desc1 ) && empty( $Desc2 ) ) { + $Description = $Desc1; + } else if ( ! empty( $Desc2 ) && empty( $Desc1 ) ) { + $Description = $Desc2; + } else { + $Description = ' '; + } + $Description = sanitize_text_field( $Description ); + + $Email = ''; + if ( rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_email"] ) ) ) { + $Email = sanitize_text_field( rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_email"] ) ) ); + } + + $Mobile = ''; + if ( rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_mobile"] ) ) ) { + $Mobile = sanitize_text_field( rgpost( 'input_' . str_replace( ".", "_", $config["meta"]["customer_fields_mobile"] ) ) ); + } + + } + + } else { + + $Amount = gform_get_meta( rgar( $entry, 'id' ), 'hannanstd_part_price_' . $form['id'] ); + $Amount = apply_filters( self::$author . "_gform_custom_gateway_price_{$form['id']}", apply_filters( self::$author . "_gform_custom_gateway_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_custom_payping_price_{$form['id']}", apply_filters( self::$author . "_gform_custom_payping_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_gateway_price_{$form['id']}", apply_filters( self::$author . "_gform_gateway_price", $Amount, $form, $entry ), $form, $entry ); + $Amount = apply_filters( self::$author . "_gform_payping_price_{$form['id']}", apply_filters( self::$author . "_gform_payping_price", $Amount, $form, $entry ), $form, $entry ); + + $Description = gform_get_meta( rgar( $entry, 'id' ), 'hannanstd_part_desc_' . $form['id'] ); + $Description = apply_filters( self::$author . '_gform_payping_gateway_desc_', apply_filters( self::$author . '_gform_custom_gateway_desc_', $Description, $form, $entry ), $form, $entry ); + + $Paymenter = gform_get_meta( rgar( $entry, 'id' ), 'hannanstd_part_name_' . $form['id'] ); + $Email = gform_get_meta( rgar( $entry, 'id' ), 'hannanstd_part_email_' . $form['id'] ); + $Mobile = gform_get_meta( rgar( $entry, 'id' ), 'hannanstd_part_mobile_' . $form['id'] ); + + $entry_id = GFAPI::add_entry( $entry ); + $entry = GFPersian_Payments::get_entry( $entry_id ); + + do_action( 'gf_gateway_request_add_entry', $confirmation, $form, $entry, $ajax ); + do_action( 'gf_payping_request_add_entry', $confirmation, $form, $entry, $ajax ); + + //----------------------------------------------------------------- + gform_update_meta( $entry_id, 'payment_gateway', self::get_gname() ); + gform_update_meta( $entry_id, 'payment_type', 'custom' ); + } + + unset( $entry["payment_status"] ); + unset( $entry["payment_method"] ); + unset( $entry["is_fulfilled"] ); + unset( $entry["transaction_type"] ); + unset( $entry["payment_amount"] ); + unset( $entry["payment_date"] ); + unset( $entry["transaction_id"] ); + + $entry["payment_status"] = "Processing"; + $entry["payment_method"] = "payping"; + $entry["is_fulfilled"] = 0; + if ( ! empty( $transaction_type ) ) { + $entry["transaction_type"] = $transaction_type; + } + GFAPI::update_entry( $entry ); + $entry = GFPersian_Payments::get_entry( $entry_id ); + + + $ReturnPath = self::Return_URL( $form['id'], $entry_id ); + $ResNumber = apply_filters( 'gf_payping_res_number', apply_filters( 'gf_gateway_res_number', $entry_id, $entry, $form ), $entry, $form ); + } else { + + $Amount = 2000; + $ReturnPath = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; + $Email = ''; + $Mobile = ''; + $ResNumber = rand( 1000, 9999 ); + $Description = __( 'جهت بررسی صحیح بودن تنظیمات درگاه گرویتی فرم پی‌پینگ', 'gravityformspayping' ); + + } + $Mobile = GFPersian_Payments::fix_mobile( $Mobile ); + + do_action( 'gf_gateway_request_2', $confirmation, $form, $entry, $ajax ); + do_action( 'gf_payping_request_2', $confirmation, $form, $entry, $ajax ); + + if ( ! $custom ) { + $Amount = GFPersian_Payments::amount( $Amount, 'IRT', $form, $entry ); + } + + //$Email = !filter_var($Email, FILTER_VALIDATE_EMAIL) === false ? $Email : ''; + //$Mobile = preg_match('/^09[0-9]{9}/i', $Mobile) ? $Mobile : ''; + + + $data = array( + 'payerName' => $user_name , + 'Amount' => $Amount, + 'payerIdentity'=> $Mobile , + 'returnUrl' => $ReturnPath, + 'Description' => $Description , + 'clientRefId' => $entry_id + ); + + + try { + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => "https://api.payping.ir/v1/pay", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_HTTPHEADER => array( + "accept: application/json", + "authorization: Bearer " . self::get_merchent(), + "cache-control: no-cache", + "content-type: application/json"), + ) + ); + + $response = curl_exec($curl); + $header = curl_getinfo($curl); + $err = curl_error($curl); + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + if ($header['http_code'] == 200) { + $response = json_decode($response, true); + if (isset($response["code"]) and $response["code"] != '') { + $Payment_URL = sprintf('https://api.payping.ir/v1/pay/gotoipg/%s', $response["code"]); + if ( $valid_checker ) { + return true; + } else { + return self::redirect_confirmation( $Payment_URL, $ajax ); + } + } else { + $Message = 'عدم وجود کد ارجاع '; + } + } elseif ($header['http_code'] == 400) { + $Message = implode('. ',array_values (json_decode($response,true))); + } else { + $Message = self::Fault($header['http_code']) . '(' . $header['http_code'] . ')'; + } + } + } catch (Exception $e){ + $Message = ' تراکنش ناموفق بود- شرح خطا سمت برنامه شما : ' . $e->getMessage(); + } + + $Message = ! empty( $Message ) ? $Message : __( 'خطایی رخ داده است.', 'gravityformspayping' ); + + + + + $confirmation = __( 'متاسفانه نمیتوانیم به درگاه متصل شویم. علت : ', 'gravityformspayping' ) . $Message; + + if ( $valid_checker ) { + return $Message; + } + + $entry = GFPersian_Payments::get_entry( $entry_id ); + $entry['payment_status'] = 'Failed'; + GFAPI::update_entry( $entry ); + + RGFormsModel::add_note( $entry_id, $user_id, $user_name, sprintf( __( 'خطا در اتصال به درگاه رخ داده است : %s', "gravityformspayping" ), $Message ) ); + + if ( ! $custom ) { + GFPersian_Payments::notification( $form, $entry ); + } + + $default_anchor = 0; + $anchor = gf_apply_filters( 'gform_confirmation_anchor', $form['id'], $default_anchor ) ? "" : ''; + $nl2br = ! empty( $form['confirmation'] ) && rgar( $form['confirmation'], 'disableAutoformat' ) ? false : true; + $cssClass = rgar( $form, 'cssClass' ); + + return $confirmation = empty( $confirmation ) ? "{$anchor} " : "{$anchor}
" . GFCommon::replace_variables( $confirmation, $form, $entry, false, true, $nl2br ) . '
'; + } + + + // #5 + public static function Verify() { + + if ( apply_filters( 'gf_gateway_payping_return', apply_filters( 'gf_gateway_verify_return', false ) ) ) { + return; + } + + if ( ! self::is_gravityforms_supported() ) { + return; + } + + if ( empty( $_GET['id'] ) || empty( $_GET['entry'] ) || ! is_numeric( rgget( 'id' ) ) || ! is_numeric( rgget( 'entry' ) ) ) { + return; + } + + $form_id = $_GET['id']; + $entry_id = $_GET['entry']; + + $entry = GFPersian_Payments::get_entry( $entry_id ); + + if ( is_wp_error( $entry ) ) { + return; + } + + if ( isset( $entry["payment_method"] ) && $entry["payment_method"] == 'payping' ) { + + $form = RGFormsModel::get_form_meta( $form_id ); + + $payment_type = gform_get_meta( $entry["id"], 'payment_type' ); + gform_delete_meta( $entry['id'], 'payment_type' ); + + if ( $payment_type != 'custom' ) { + $config = self::get_config_by_entry( $entry ); + if ( empty( $config ) ) { + return; + } + } else { + $config = apply_filters( self::$author . '_gf_payping_config', apply_filters( self::$author . '_gf_gateway_config', array(), $form, $entry ), $form, $entry ); + } + + + if ( ! empty( $entry["payment_date"] ) ) { + /* + if( ! class_exists("GFFormDisplay") ) + require_once(GFCommon::get_base_path() . "/form_display.php"); + + $default_anchor = 0; + $anchor = gf_apply_filters( 'gform_confirmation_anchor', $form['id'], $default_anchor ) ? "" : ''; + $nl2br = !empty( $form['confirmation'] ) && rgar( rgar( $form, 'confirmation' ), 'disableAutoformat' ) ? false : true; + $cssClass = rgar( $form, 'cssClass' ); + $confirmation = __('نتیجه تراکنش قبلا مشخص شده است.' , 'gravityformspayping'); + $confirmation = empty( $confirmation ) ? "{$anchor} " : "{$anchor}
" . GFCommon::replace_variables( $confirmation, $form, $entry, false, true, $nl2br ) . '
'; + GFFormDisplay::$submission[$form_id] = array("is_confirmation" => true, "confirmation_message" => $confirmation, "form" => $form, "entry" => $entry, "lead" => $entry,"page_number"=> 1); + */ + return; + } + + global $current_user; + $user_id = 0; + $user_name = __( "مهمان", "gravityformspayping" ); + if ( $current_user && $user_data = get_userdata( $current_user->ID ) ) { + $user_id = $current_user->ID; + $user_name = $user_data->display_name; + } + + $transaction_type = 1; + if ( ! empty( $config["meta"]["type"] ) && $config["meta"]["type"] == 'subscription' ) { + $transaction_type = 2; + } + + if ( $payment_type == 'custom' ) { + $Amount = $Total = gform_get_meta( $entry["id"], 'hannanstd_part_price_' . $form_id ); + } else { + $Amount = $Total = self::get_order_total( $form, $entry ); + } + $Total_Money = GFCommon::to_money( $Total, $entry["currency"] ); + + $free = false; + if ( empty( $_GET['no'] ) || $_GET['no'] != 'true' ) { + + //Start of payping + if ( $payment_type != 'custom' ) { + $Amount = GFPersian_Payments::amount( $Amount, 'IRT', $form, $entry ); + } + + + + + $Authority = isset( $_GET['refId'] ) ? sanitize_text_field( $_GET['refId'] ) : ''; + $MerchantID = self::get_merchent(); + + try { + + $__params = $Amount . $Authority; + if ( GFPersian_Payments::check_verification( $entry, __CLASS__, $__params ) ) { + return; + } + + $data = array('refId' => $_GET['refid'], 'amount' => $Amount); + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => "https://api.payping.ir/v1/pay/verify", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_HTTPHEADER => array( + "accept: application/json", + "authorization: Bearer ".$MerchantID, + "cache-control: no-cache", + "content-type: application/json", + ), + )); + $response = curl_exec($curl); + $err = curl_error($curl); + $header = curl_getinfo($curl); + curl_close($curl); + + if ($err) { + $Status = 'failed'; + $Fault = 'Curl Error.'; + $Message = 'خطا در ارتباط به پی‌پینگ : شرح خطا '.$err; + } else { + if ($header['http_code'] == 200) { + $response = json_decode($response, true); + if (isset($_GET["refid"]) and $_GET["refid"] != '') { + $Status = 'completed'; + } else { + $Message = 'متافسانه سامانه قادر به دریافت کد پیگیری نمی باشد! ' ; + $Status = 'failed'; + } + } elseif ($header['http_code'] == 400) { + $Message = implode('. ',array_values (json_decode($response,true))); + $Status = 'failed'; + } else { + $Message = self::Fault($header['http_code']) . '(' . $header['http_code'] . ')'; + $Status = 'failed'; + } + + } + } catch (Exception $e){ + $Message = $e->getMessage(); + $Status = 'failed'; + } + + $Transaction_ID = ! empty( $_GET['refid'] ) ? $_GET['refid'] : '-'; + //End of payping + } else { + $Status = 'completed'; + $Message = ''; + $Transaction_ID = apply_filters( self::$author . '_gf_rand_transaction_id', GFPersian_Payments::transaction_id( $entry ), $form, $entry ); + $free = true; + } + + $Status = ! empty( $Status ) ? $Status : 'failed'; + $transaction_id = ! empty( $Transaction_ID ) ? $Transaction_ID : ''; + $transaction_id = apply_filters( self::$author . '_gf_real_transaction_id', $transaction_id, $Status, $form, $entry ); + + //---------------------------------------------------------------------------------------- + $entry["payment_date"] = gmdate( "Y-m-d H:i:s" ); + $entry["transaction_id"] = $transaction_id; + $entry["transaction_type"] = $transaction_type; + + if ( $Status == 'completed' ) { + + $entry["is_fulfilled"] = 1; + $entry["payment_amount"] = $Total; + + if ( $transaction_type == 2 ) { + $entry["payment_status"] = "Active"; + RGFormsModel::add_note( $entry["id"], $user_id, $user_name, __( "تغییرات اطلاعات فیلدها فقط در همین پیام ورودی اعمال خواهد شد و بر روی وضعیت کاربر تاثیری نخواهد داشت .", "gravityformspayping" ) ); + } else { + $entry["payment_status"] = "Paid"; + } + + if ( $free == true ) { + //unset( $entry["payment_status"] ); + unset( $entry["payment_amount"] ); + unset( $entry["payment_method"] ); + unset( $entry["is_fulfilled"] ); + gform_delete_meta( $entry['id'], 'payment_gateway' ); + $Note = sprintf( __( 'وضعیت پرداخت : رایگان - بدون نیاز به درگاه پرداخت', "gravityformspayping" ) ); + } else { + $Note = sprintf( __( 'وضعیت پرداخت : موفق - مبلغ پرداختی : %s - کد تراکنش : %s', "gravityformspayping" ), $Total_Money, $transaction_id ); + } + + GFAPI::update_entry( $entry ); + + + if ( apply_filters( self::$author . '_gf_payping_post', apply_filters( self::$author . '_gf_gateway_post', ( $payment_type != 'custom' ), $form, $entry ), $form, $entry ) ) { + + $has_post = GFCommon::has_post_field( $form["fields"] ) ? true : false; + + if ( ! empty( $config["meta"]["update_post_action1"] ) && $config["meta"]["update_post_action1"] != 'default' ) { + $new_status = $config["meta"]["update_post_action1"]; + } else { + $new_status = rgar( $form, 'postStatus' ); + } + + if ( empty( $entry["post_id"] ) && $has_post ) { + $form['postStatus'] = $new_status; + RGFormsModel::create_post( $form, $entry ); + $entry = GFPersian_Payments::get_entry( $entry_id ); + } + + if ( ! empty( $entry["post_id"] ) && $has_post ) { + $post = get_post( $entry["post_id"] ); + if ( is_object( $post ) ) { + if ( $new_status != $post->post_status ) { + $post->post_status = $new_status; + wp_update_post( $post ); + } + } + } + } + + if ( ! empty( $__params ) ) { + GFPersian_Payments::set_verification( $entry, __CLASS__, $__params ); + } + + $user_registration_slug = apply_filters( 'gf_user_registration_slug', 'gravityformsuserregistration' ); + $paypal_config = array( 'meta' => array() ); + if ( ! empty( $config["meta"]["addon"] ) && $config["meta"]["addon"] == 'true' ) { + if ( class_exists( 'GFAddon' ) && method_exists( 'GFAddon', 'get_registered_addons' ) ) { + $addons = GFAddon::get_registered_addons(); + foreach ( (array) $addons as $addon ) { + if ( is_callable( array( $addon, 'get_instance' ) ) ) { + $addon = call_user_func( array( $addon, 'get_instance' ) ); + if ( is_object( $addon ) && method_exists( $addon, 'get_slug' ) ) { + $slug = $addon->get_slug(); + if ( $slug != $user_registration_slug ) { + $paypal_config['meta'][ 'delay_' . $slug ] = true; + } + } + } + } + } + } + if ( ! empty( $config["meta"]["type"] ) && $config["meta"]["type"] == "subscription" ) { + $paypal_config['meta'][ 'delay_' . $user_registration_slug ] = true; + } + + do_action( "gform_payping_fulfillment", $entry, $config, $transaction_id, $Total ); + do_action( "gform_gateway_fulfillment", $entry, $config, $transaction_id, $Total ); + do_action( "gform_paypal_fulfillment", $entry, $paypal_config, $transaction_id, $Total ); + } else if ( $Status == 'cancelled' ) { + $entry["payment_status"] = "Cancelled"; + $entry["payment_amount"] = 0; + $entry["is_fulfilled"] = 0; + GFAPI::update_entry( $entry ); + + $Note = sprintf( __( 'وضعیت پرداخت : منصرف شده - مبلغ قابل پرداخت : %s - کد تراکنش : %s', "gravityformspayping" ), $Total_Money, $transaction_id ); + } else { + $entry["payment_status"] = "Failed"; + $entry["payment_amount"] = 0; + $entry["is_fulfilled"] = 0; + GFAPI::update_entry( $entry ); + + $Note = sprintf( __( 'وضعیت پرداخت : ناموفق - مبلغ قابل پرداخت : %s - کد تراکنش : %s - علت خطا : %s', "gravityformspayping" ), $Total_Money, $transaction_id, $Message ); + } + + $entry = GFPersian_Payments::get_entry( $entry_id ); + RGFormsModel::add_note( $entry["id"], $user_id, $user_name, $Note ); + do_action( 'gform_post_payment_status', $config, $entry, strtolower( $Status ), $transaction_id, '', $Total, '', '' ); + do_action( 'gform_post_payment_status_' . __CLASS__, $config, $form, $entry, strtolower( $Status ), $transaction_id, '', $Total, '', '' ); + + + if ( apply_filters( self::$author . '_gf_payping_verify', apply_filters( self::$author . '_gf_gateway_verify', ( $payment_type != 'custom' ), $form, $entry ), $form, $entry ) ) { + GFPersian_Payments::notification( $form, $entry ); + GFPersian_Payments::confirmation( $form, $entry, $Message ); + } + } + } + + + // #6 + private static function Fault( $err_code ) { + switch ($err_code){ + case 200 : + return 'عملیات با موفقیت انجام شد'; + break ; + case 400 : + return 'مشکلی در ارسال درخواست وجود دارد'; + break ; + case 500 : + return 'مشکلی در سرور رخ داده است'; + break; + case 503 : + return 'سرور در حال حاضر قادر به پاسخگویی نمی‌باشد'; + break; + case 401 : + return 'عدم دسترسی'; + break; + case 403 : + return 'دسترسی غیر مجاز'; + break; + case 404 : + return 'آیتم درخواستی مورد نظر موجود نمی‌باشد'; + break; + } + return ''; + } + +} \ No newline at end of file