diff --git a/include/library/Crunchbutton/Promo.php b/include/library/Crunchbutton/Promo.php index 444457dda..7ab455dd0 100644 --- a/include/library/Crunchbutton/Promo.php +++ b/include/library/Crunchbutton/Promo.php @@ -427,6 +427,11 @@ class Crunchbutton_Promo extends Cana_Table $gifts = self::q($query); return $gifts; } + + public function groups(){ + return Crunchbutton_Promo_Group::q( "SELECT g.* FROM promo_group g INNER JOIN promo_group_promo pgp ON pgp.id_promo_group = g.id_promo_group AND pgp.id_promo = {$this->id_promo}" ); + } + public function order_reference(){ return Order::o($this->id_order_reference); } diff --git a/include/library/Crunchbutton/Promo/Group.php b/include/library/Crunchbutton/Promo/Group.php index 472075923..41eb7da8f 100644 --- a/include/library/Crunchbutton/Promo/Group.php +++ b/include/library/Crunchbutton/Promo/Group.php @@ -20,19 +20,39 @@ class Crunchbutton_Promo_Group extends Cana_Table public function giftcards(){ if ( !isset( $this->_giftcards ) ) { - $sql = "SELECT * FROM promo p INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group}"; - $this->_giftcards = Promo::q($sql, $this->db()); + $query = "SELECT p.* FROM promo p INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group}"; + $this->_giftcards = Promo::q( $query, $this->db() ); } return $this->_giftcards; } + public function giftcards_redeemed(){ + if ( !isset( $this->_giftcards_redeemed ) ) { + $query = "SELECT p.* FROM promo p + INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group} + INNER JOIN credit c ON p.id_promo = c.id_promo AND p.id_promo = c.id_promo"; + $this->_giftcards_redeemed = Promo::q( $query, $this->db() ); + } + return $this->_giftcards_redeemed; + } + + public function giftcards_redeemed_total(){ + $query = "SELECT COUNT(*) AS total FROM promo p + INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group} + INNER JOIN credit c ON p.id_promo = c.id_promo AND p.id_promo = c.id_promo"; + $total = c::db()->get( $query ); + return $total->_items[0]->total; + } + public function remove_giftcards(){ $query = "DELETE FROM promo_group_promo WHERE id_promo_group = {$this->id_promo_group}"; Cana::db()->query( $query ); } public function giftcards_total(){ - return $this->giftcards()->count(); + $query = "SELECT COUNT(*) AS total FROM promo p INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group}"; + $total = c::db()->get( $query ); + return $total->_items[0]->total; } public function save_giftcards( $range ){ @@ -80,32 +100,36 @@ class Crunchbutton_Promo_Group extends Cana_Table public function range(){ if( $this->giftcards_total() > 0 ){ - $giftcards = $this->giftcards(); + $query = "SELECT p.id_promo FROM promo p INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group}"; + $giftcards = c::db()->get( $query ); $ids = []; foreach ( $giftcards as $giftcard ) { $ids[] = intval( $giftcard->id_promo ); } sort( $ids ); + $groups = array(); - - for( $i = 0; $i < count( $ids ); $i++ ){ - if( $i > 0 && ( $ids[$i - 1] == $ids[ $i ] - 1 ) ){ - array_push( $groups[ count( $groups ) - 1 ], $ids[ $i ] ); + $total = sizeof( $ids ); + for( $i = 0; $i < $total; $i++ ){ + if( $i > 0 && ( $ids[ $i - 1 ] == $ids[ $i ] - 1 ) ){ + $groups[ count( $groups ) - 1 ][ 1 ] = $ids[ $i ]; } else { - array_push( $groups, array( $ids[ $i ] ) ); + $groups[] = array( $ids[ $i ] ); } } + $str_group = ''; $commas = ''; foreach($groups as $group){ if( count( $group ) == 1 ){ $str_group .= $commas . $group[ 0 ]; } else { - $str_group .= $commas . $group[0] . ' - ' . $group[count($group) - 1]; + $str_group .= $commas . $group[0] . ' - ' . $group[ count( $group ) - 1]; } $commas = ', '; } + return $str_group; } else { return ''; diff --git a/include/views/default/cockpit/giftcards/content.phtml b/include/views/default/cockpit/giftcards/content.phtml index 45e00b2a3..a0a56c0b4 100644 --- a/include/views/default/cockpit/giftcards/content.phtml +++ b/include/views/default/cockpit/giftcards/content.phtml @@ -12,6 +12,7 @@ Email Note Issued as + Group Redeemed giftcards as $giftcard) : ?> @@ -43,6 +44,20 @@ Paper (print) - + + groups(); + if( $groups->count() > 0 ){ + $commas = ''; + foreach ( $groups as $group ) { + echo $commas . $group->name ; + $commas = ', '; + } + } else { + echo '-'; + } + ?> + Redeemed diff --git a/include/views/default/cockpit/giftcards/groups/content.phtml b/include/views/default/cockpit/giftcards/groups/content.phtml index f30de149c..732e967c3 100644 --- a/include/views/default/cockpit/giftcards/groups/content.phtml +++ b/include/views/default/cockpit/giftcards/groups/content.phtml @@ -8,7 +8,9 @@ $groups = $this->giftcards_groups; Name Date - Number of Gift cards + Range + Gift cards + Gift cards Redeemed giftcards_groups; name;?> format('M jS Y')?> - format('g:i:s A')?> - giftcards_total(); ?> + range(); ?> + giftcards_redeemed_total(); ?> + giftcards_redeemed_total(); ?> Edit   diff --git a/include/views/default/cockpit/giftcards/groups/form.phtml b/include/views/default/cockpit/giftcards/groups/form.phtml index 1484d6c4f..2daf04344 100644 --- a/include/views/default/cockpit/giftcards/groups/form.phtml +++ b/include/views/default/cockpit/giftcards/groups/form.phtml @@ -7,6 +7,7 @@ $this->title2icon = 'group'; $group = $this->group; + if( $group->id_promo_group ){ $range = $group->range(); } @@ -34,9 +35,16 @@ giftcards_total(); ?> +
  • + Number of giftcards redeemed + + giftcards_redeemed_total(); ?> + +
  • +
  • - Range of giftcards + Range of giftcards ids
    @@ -61,8 +69,14 @@ $(function() { } ); }); +var processing = false; + function sendForm(){ + if( processing ){ + return; + } + var name = $.trim( $( '#name' ).val() ); var range = $.trim( $( '#range' ).val() ); if( name == '' ){ @@ -72,6 +86,11 @@ function sendForm(){ } var data = { 'name' : name, 'range' : range }; + + processing = true; + + $( '.admin-giftcard-group-save' ).html( ' Please wait' ); + var url = App.service + 'giftcard/group/id_promo_group; ?>'; $.ajax({ @@ -80,6 +99,7 @@ function sendForm(){ data: data, url: url, success: function( json ) { + processing = false; if( json.error ){ alert( 'Error at saving the group!' ); } else { @@ -88,6 +108,7 @@ function sendForm(){ } }, error: function( ){ + processing = false; alert( 'Error at saving the group!' ); } });