This commit is contained in:
Daniel Camargo 2013-06-28 15:27:20 -03:00
parent 40073d04bc
commit a33cda7a08
5 changed files with 82 additions and 13 deletions

View File

@ -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);
}

View File

@ -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 '';

View File

@ -12,6 +12,7 @@
<td>Email</td>
<td>Note</td>
<td>Issued as</td>
<td>Group</td>
<td>Redeemed</td>
</thead>
<? foreach ($this->giftcards as $giftcard) : ?>
@ -43,6 +44,20 @@
<i class="icon-print"></i> Paper (print)
<?php } else { ?> - <?php } ?>
</td>
<td>
<?php
$groups = $giftcard->groups();
if( $groups->count() > 0 ){
$commas = '';
foreach ( $groups as $group ) {
echo $commas . $group->name ;
$commas = ', ';
}
} else {
echo '-';
}
?>
</td>
<td class="center">
<?php if( $wasUsed ) { ?>
<span class="badge badge-green">Redeemed</span>

View File

@ -8,7 +8,9 @@ $groups = $this->giftcards_groups;
<thead>
<td>Name</td>
<td>Date</td>
<td>Number of Gift cards</td>
<td>Range</td>
<td>Gift cards</td>
<td>Gift cards Redeemed</td>
<td></td>
</thead>
<?php foreach ( $groups as $group ) {
@ -17,7 +19,9 @@ $groups = $this->giftcards_groups;
<tr>
<td><?php echo $group->name;?> </td>
<td><?php echo $date->format('M jS Y')?> - <?php echo $date->format('g:i:s A')?> </td>
<td><?php echo $group->giftcards_total(); ?></td>
<td><?php echo $group->range(); ?></td>
<td><?php echo $group->giftcards_redeemed_total(); ?></td>
<td><?php echo $group->giftcards_redeemed_total(); ?></td>
<td style="width:150px;">
<a href="/giftcards/groups/<?php echo $group->id_promo_group; ?>" class="btn btn-green admin-giftcard-group-new"><i class="icon-edit"></i> Edit</a>
&nbsp;

View File

@ -7,6 +7,7 @@
$this->title2icon = 'group';
$group = $this->group;
if( $group->id_promo_group ){
$range = $group->range();
}
@ -34,9 +35,16 @@
<?php echo $group->giftcards_total(); ?>
</span>
</li>
<li>
<span>Number of giftcards redeemed</span>
<span class="pull-right">
<?php echo $group->giftcards_redeemed_total(); ?>
</span>
</li>
<?php } ?>
<li>
<span>Range of giftcards</span>
<span>Range of giftcards ids</span>
<span class="pull-right">
<input type="text" name="range" maxlength="250" id="range" value="<?php echo $range; ?>" />
<div class="note large">
@ -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( '<i class="icon-spinner icon-spin"></i> Please wait' );
var url = App.service + 'giftcard/group/<?php echo $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!' );
}
});