From 170f318dafd2702d4338cf54d3d51eb268c6b098 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Wed, 2 Jul 2014 17:07:11 -0300 Subject: [PATCH] Driver Settlement - #3232 --- .../default/cockpit2/api/settlement/index.php | 51 ++++--- .../Crunchbutton/Email/Payment/Summary.php | 7 +- include/library/Crunchbutton/Settlement.php | 37 ++++- .../frontend/settlement-drivers-payment.phtml | 63 +++++---- ...summary.phtml => settlement-summary.phtml} | 0 .../crunchbutton/payment/summary-driver.phtml | 126 ++++++++++++++++++ ...summary.phtml => summary-restaurant.phtml} | 6 +- www/assets/cockpit/js/cockpit.js | 4 +- .../cockpit/js/controllers.settlement.js | 24 +++- www/assets/cockpit/js/service.settlement.js | 30 ++++- 10 files changed, 284 insertions(+), 64 deletions(-) rename include/views/default/cockpit2/frontend/{settlement-restaurants-summary.phtml => settlement-summary.phtml} (100%) create mode 100644 include/views/mail/crunchbutton/payment/summary-driver.phtml rename include/views/mail/crunchbutton/payment/{summary.phtml => summary-restaurant.phtml} (97%) diff --git a/include/controllers/default/cockpit2/api/settlement/index.php b/include/controllers/default/cockpit2/api/settlement/index.php index 4f55dbb6d..46be9365b 100644 --- a/include/controllers/default/cockpit2/api/settlement/index.php +++ b/include/controllers/default/cockpit2/api/settlement/index.php @@ -4,30 +4,12 @@ class Controller_Api_Settlement extends Crunchbutton_Controller_RestAccount { public function init() { - $this->resultsPerPage = 20; - - // $this->_driverPayments(); - // exit; - - // $this->_driverDoPayment(); - // exit; - - // $this->_driverBegin(); - // exit; - - // $set = new Settlement; - - // echo json_encode( $set->driversProcessOrders( [ $set->orderExtractVariables( Order::o( c::getPagePiece( 2 ) ) ) ] ) );exit();; - - // $set->scheduleDriverPayment( [], Crunchbutton_Payment::PAY_TYPE_PAYMENT ); - // $set->scheduleDriverPayment( [], Crunchbutton_Payment::PAY_TYPE_REIMBURSEMENT ); - - // exit; - if( !c::admin()->permission()->check( ['global', 'settlement' ] ) ){ $this->_error(); } + $this->resultsPerPage = 20; + switch ($this->method()) { case 'get': switch ( c::getPagePiece( 2 ) ) { @@ -127,6 +109,12 @@ class Controller_Api_Settlement extends Crunchbutton_Controller_RestAccount { case 'payment': $this->_driverPayment(); break; + case 'view-summary': + $this->_driverViewSummary(); + break; + case 'send-summary': + $this->_driverSendSummary(); + break; default: $this->_error(); break; @@ -339,9 +327,9 @@ class Controller_Api_Settlement extends Crunchbutton_Controller_RestAccount { } private function _restaurantSendSummary(){ - $id_payment_schedule = c::getPagePiece( 4 ); + $id_payment = c::getPagePiece( 4 ); $settlement = new Settlement; - if( $settlement->sendRestaurantPaymentNotification( $id_payment_schedule ) ){ + if( $settlement->sendRestaurantPaymentNotification( $id_payment ) ){ echo json_encode( [ 'success' => true ] ); } else { echo json_encode( [ 'error' => true ] ); @@ -600,6 +588,25 @@ class Controller_Api_Settlement extends Crunchbutton_Controller_RestAccount { } } + private function _driverSendSummary(){ + $id_payment = c::getPagePiece( 4 ); + $settlement = new Settlement; + if( $settlement->sendDriverPaymentNotification( $id_payment ) ){ + echo json_encode( [ 'success' => true ] ); + } else { + echo json_encode( [ 'error' => true ] ); + } + } + + + public function _driverViewSummary(){ + $id_payment = c::getPagePiece( 4 ); + $settlement = new Crunchbutton_Settlement; + $summary = $settlement->driverSummaryByIdPayment( $id_payment ); + $mail = new Crunchbutton_Email_Payment_Summary( [ 'summary' => $summary ] ); + echo $mail->message(); + } + private function _range(){ $now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) ); $range = [ 'end' => $now->format( 'Y,m,d' ) ]; diff --git a/include/library/Crunchbutton/Email/Payment/Summary.php b/include/library/Crunchbutton/Email/Payment/Summary.php index 7a1250b98..ff22a6136 100644 --- a/include/library/Crunchbutton/Email/Payment/Summary.php +++ b/include/library/Crunchbutton/Email/Payment/Summary.php @@ -16,8 +16,11 @@ class Crunchbutton_Email_Payment_Summary extends Crunchbutton_Email { $this->view()->subject = $params[ 'summary' ]['notes']; $this->view()->email = $params[ 'summary' ]['summary_email']; - $params['messageHtml'] = $this->view()->render( 'payment/summary',[ 'display' => true, 'set' => [ 'summary' => $params['summary'] ] ] ); - + if( $params[ 'summary' ][ 'type' ] == Cockpit_Payment_Schedule::TYPE_DRIVER ){ + $params['messageHtml'] = $this->view()->render( 'payment/summary-driver',[ 'display' => true, 'set' => [ 'summary' => $params['summary'] ] ] ); + } else { + $params['messageHtml'] = $this->view()->render( 'payment/summary-restaurant',[ 'display' => true, 'set' => [ 'summary' => $params['summary'] ] ] ); + } parent::__construct($params); } } diff --git a/include/library/Crunchbutton/Settlement.php b/include/library/Crunchbutton/Settlement.php index ab2157f58..328d03bfc 100644 --- a/include/library/Crunchbutton/Settlement.php +++ b/include/library/Crunchbutton/Settlement.php @@ -483,7 +483,7 @@ class Crunchbutton_Settlement extends Cana_Model { $schedule->date = date( 'Y-m-d H:i:s' ); $schedule->amount = max( $payment_data[ 'total_due' ], 0 ); $schedule->adjustment = $adjustment; - $schedule->pay_type = Cockpit_Payment_Schedule::PAYMENT_TYPE_PAYMENT; + $schedule->pay_type = Cockpit_Payment_Schedule::PAY_TYPE_PAYMENT; $schedule->type = Cockpit_Payment_Schedule::TYPE_RESTAURANT; $schedule->status = Cockpit_Payment_Schedule::STATUS_SCHEDULED; $schedule->note = $notes; @@ -632,6 +632,7 @@ class Crunchbutton_Settlement extends Cana_Model { $summary[ 'summary_email' ] = $schedule->restaurant()->payment_type()->summary_email; $summary[ 'summary_fax' ] = $schedule->restaurant()->payment_type()->summary_fax; $summary[ 'payment_method' ] = $schedule->restaurant()->payment_type()->payment_method; + $summary[ 'type' ] = Cockpit_Payment_Schedule::TYPE_RESTAURANT; $payment = $schedule->payment(); if( $payment->id_payment ){ $summary[ 'balanced_id' ] = $payment->balanced_id; @@ -972,7 +973,7 @@ class Crunchbutton_Settlement extends Cana_Model { $payment_order_transaction->save(); } - // $this->sendDriverPaymentNotification( $payment->id_payment ); + $this->sendDriverPaymentNotification( $payment->id_payment ); return true; } else { $message = 'Driver Payment error! Driver: ' . $schedule->driver()->name; @@ -1022,11 +1023,10 @@ class Crunchbutton_Settlement extends Cana_Model { $settlement = new Settlement; $summary = $schedule->exports(); $summary[ 'driver' ] = $schedule->driver()->name; - $summary[ 'summary_method' ] = $schedule->driver()->payment_type()->summary_method; $summary[ 'summary_email' ] = $schedule->driver()->payment_type()->summary_email; - $summary[ 'summary_fax' ] = $schedule->driver()->payment_type()->summary_fax; $summary[ 'driver' ] = $schedule->driver()->name; $summary[ 'payment_method' ] = $schedule->driver()->payment_type()->payment_method; + $summary[ 'type' ] = Cockpit_Payment_Schedule::TYPE_DRIVER; $payment = $schedule->payment(); if( $payment->id_payment ){ $summary[ 'balanced_id' ] = $payment->balanced_id; @@ -1055,6 +1055,8 @@ class Crunchbutton_Settlement extends Cana_Model { 'total' => $variables[ 'final_price_plus_delivery_markup' ], 'date' => $variables[ 'short_date' ], 'tip' => $variables[ 'tip' ], + 'restaurant' => $variables[ 'restaurant' ], + 'delivery_fee' => $variables[ 'delivery_fee' ], 'pay_type' => $type, 'total_reimburse' => $pay_info[ 0 ][ 'total_reimburse' ], 'total_payment' => $pay_info[ 0 ][ 'total_payment' ] @@ -1133,6 +1135,33 @@ class Crunchbutton_Settlement extends Cana_Model { return false; } + public function sendDriverPaymentNotification( $id_payment ){ + + $summary = $this->driverSummaryByIdPayment( $id_payment ); + + if( !$summary ){ + return false; + } + + $this->log( 'sendDriverPaymentNotification', $summary ); + + $env = c::getEnv(); + + $mail = ( $env == 'live' ? $summary[ 'summary_email' ] : Crunchbutton_Settlement::TEST_SUMMARY_EMAIL ); + $fax = ( $env == 'live' ? $summary[ 'summary_fax' ] : Crunchbutton_Settlement::TEST_SUMMARY_FAX ); + + $mail = new Crunchbutton_Email_Payment_Summary( [ 'summary' => $summary ] ); + + if ( $mail->send() ) { + $payment = Crunchbutton_Payment::o( $id_payment ); + $payment->summary_sent_date = date('Y-m-d H:i:s'); + $payment->save(); + return true; + } + + return false; + } + private function log( $method, $message ){ Log::debug( [ 'method' => $method, 'id_admin' => c::user()->id_admin, 'message' => $message, 'env' => c::getEnv(), 'type' => 'settlement' ] ); } diff --git a/include/views/default/cockpit2/frontend/settlement-drivers-payment.phtml b/include/views/default/cockpit2/frontend/settlement-drivers-payment.phtml index b283b1b5e..41261e96d 100644 --- a/include/views/default/cockpit2/frontend/settlement-drivers-payment.phtml +++ b/include/views/default/cockpit2/frontend/settlement-drivers-payment.phtml @@ -134,7 +134,7 @@ - + @@ -146,16 +146,22 @@ + - + + - - + + @@ -164,10 +170,11 @@ + - - + + @@ -181,8 +188,10 @@ + + @@ -191,16 +200,8 @@ - - - - - - - - - + @@ -210,6 +211,7 @@ + @@ -219,6 +221,27 @@ + + + + + + + + + + + + + + + + + + + + + @@ -227,15 +250,7 @@ - - - - - - - - - +
OrdersOrders
#Curstomer nameCurstomer nameRestaurant DateTotalTip Type + Tip + + Delivery Fee + Reimbursement Payment
{{order.id_order}} {{order.name}}{{order.restaurant}} {{order.date}}$ {{order.tip | formatPrice}}$ {{order.total | formatPrice}} {{order.pay_type}}$ {{order.delivery_fee | formatPrice}}$ {{order.tip | formatPrice}} $ {{order.total_reimburse | formatPrice}} $ {{order.total_payment | formatPrice}}
Tips by Card $ {{result.calcs.tip | formatPrice}}
Visa/Mastercard/Amex Charges$ {{result.calcs.credit_charge | formatPrice}}
Adjustment $ {{result.adjustment | formatPrice}}
Total Reimburse $ {{result.calcs.total_reimburse | formatPrice}}
Total Delivery Fee$ {{result.calcs.delivery_fee | formatPrice}}
Markup$ {{result.calcs.markup | formatPrice}}
Total Payment $ {{result.calcs.total_payment | formatPrice}}
diff --git a/include/views/default/cockpit2/frontend/settlement-restaurants-summary.phtml b/include/views/default/cockpit2/frontend/settlement-summary.phtml similarity index 100% rename from include/views/default/cockpit2/frontend/settlement-restaurants-summary.phtml rename to include/views/default/cockpit2/frontend/settlement-summary.phtml diff --git a/include/views/mail/crunchbutton/payment/summary-driver.phtml b/include/views/mail/crunchbutton/payment/summary-driver.phtml new file mode 100644 index 000000000..60254dd91 --- /dev/null +++ b/include/views/mail/crunchbutton/payment/summary-driver.phtml @@ -0,0 +1,126 @@ +
+ + + + + + +
+

Crunchbutton

+
+ Payment #
+ +
+ +
+ + + + + +
+ + + + +
+
+ + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Orders
CustomerRestaurantDateTypeTipDelivery FeePaymentReimbursement
+ + $ + + - + + $$$
 
Tips by Card$
Total Delivery Fee$
Markup$
Total Payment$
Total Reimbursement$
+

+ + + + + +
+ + Payment has been deposited into your bank account + + + + Crunchbutton support: call or text phone->restaurant?>
+ +
diff --git a/include/views/mail/crunchbutton/payment/summary.phtml b/include/views/mail/crunchbutton/payment/summary-restaurant.phtml similarity index 97% rename from include/views/mail/crunchbutton/payment/summary.phtml rename to include/views/mail/crunchbutton/payment/summary-restaurant.phtml index 8916bc264..b41aa0f05 100644 --- a/include/views/mail/crunchbutton/payment/summary.phtml +++ b/include/views/mail/crunchbutton/payment/summary-restaurant.phtml @@ -26,12 +26,12 @@ - + - +
@@ -132,7 +132,7 @@ - Payment has been deposited into your back account + Payment has been deposited into your bank account diff --git a/www/assets/cockpit/js/cockpit.js b/www/assets/cockpit/js/cockpit.js index 39ba56092..884cf2d79 100644 --- a/www/assets/cockpit/js/cockpit.js +++ b/www/assets/cockpit/js/cockpit.js @@ -123,7 +123,7 @@ NGApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $l .when('/settlement/restaurants/summary/:id', { action: 'settlement', controller: 'SettlementRestaurantsSummaryCtrl', - templateUrl: 'assets/view/settlement-restaurants-summary.html' + templateUrl: 'assets/view/settlement-summary.html' }) .when('/settlement/restaurants/payments', { action: 'settlement', @@ -158,7 +158,7 @@ NGApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $l .when('/settlement/drivers/summary/:id', { action: 'settlement', controller: 'SettlementDriversSummaryCtrl', - templateUrl: 'assets/view/settlement-drivers-summary.html' + templateUrl: 'assets/view/settlement-summary.html' }) /* Driver shifts */ .when('/drivers/orders', { diff --git a/www/assets/cockpit/js/controllers.settlement.js b/www/assets/cockpit/js/controllers.settlement.js index 92f8ddf8b..5916f27e1 100644 --- a/www/assets/cockpit/js/controllers.settlement.js +++ b/www/assets/cockpit/js/controllers.settlement.js @@ -415,7 +415,7 @@ NGApp.controller( 'SettlementDriversCtrl', function ( $scope, $filter, Settlemen if( json.start && json.end ){ $scope.range = { 'start' : new Date( json.start ), 'end' : new Date( json.end ) }; $scope.ready = true; - setTimeout( function() { $scope.begin() }, 100 ); + // setTimeout( function() { $scope.begin() }, 100 ); } } ); } @@ -767,3 +767,25 @@ NGApp.controller( 'SettlementDriversPaymentCtrl', function ( $scope, $routeParam } }); + +NGApp.controller( 'SettlementDriversSummaryCtrl', function ( $scope, $routeParams, SettlementService ) { + + $scope.ready = false; + + var load = function(){ + SettlementService.drivers.view_summary( function( data ){ + $scope.summary = data; + $scope.ready = true; + } ); + } + + $scope.payment = function(){ + $scope.navigation.link( '/settlement/drivers/payment/' + $routeParams.id ); + } + + // Just run if the user is loggedin + if( $scope.account.isLoggedIn() ){ + load(); + } + +}); \ No newline at end of file diff --git a/www/assets/cockpit/js/service.settlement.js b/www/assets/cockpit/js/service.settlement.js index 7cd27c013..f3a3a3540 100644 --- a/www/assets/cockpit/js/service.settlement.js +++ b/www/assets/cockpit/js/service.settlement.js @@ -34,6 +34,7 @@ NGApp.factory( 'SettlementService', function( $resource, $http, $routeParams ) { 'scheduled' : { 'method': 'POST', params : { action: 'scheduled' } }, 'scheduled_payment' : { 'method': 'POST', params : { action: 'scheduled' } }, 'do_payment' : { 'method': 'POST', params : { action: 'do-payment' } }, + 'send_summary' : { 'method': 'POST', params : { action: 'send-summary' } }, 'payment' : { 'method': 'POST', params : { action: 'payment' } }, 'payments' : { 'method': 'POST', params : { action: 'payments' } }, 'begin' : { 'method': 'POST', params : { action: 'begin' } } @@ -90,12 +91,12 @@ NGApp.factory( 'SettlementService', function( $resource, $http, $routeParams ) { service.restaurants.view_summary = function( callback ){ var url = App.service + 'settlement/restaurants/view-summary/' + $routeParams.id; $http( { method: 'POST', url: url } ). - success( function( data, status, headers, config ) { - callback( data ); - }). - error( function(data, status, headers, config ) { - callback( false ); - } ); + success( function( data, status, headers, config ) { + callback( data ); + }). + error( function(data, status, headers, config ) { + callback( false ); + } ); } service.restaurants.payments = function( params, callback ){ @@ -182,6 +183,23 @@ NGApp.factory( 'SettlementService', function( $resource, $http, $routeParams ) { } ); } + service.drivers.send_summary = function( callback ){ + settlement.drivers.send_summary( { 'id_payment_schedule' : $routeParams.id }, function( json ){ + callback( json ); + } ); + } + + service.drivers.view_summary = function( callback ){ + var url = App.service + 'settlement/drivers/view-summary/' + $routeParams.id; + $http( { method: 'POST', url: url } ). + success( function( data, status, headers, config ) { + callback( data ); + }). + error( function(data, status, headers, config ) { + callback( false ); + } ); + } + service.pay_types = function(){ var tips = []; tips.push( { type: 0, label: 'All' } );