From 4a72fa752190c1a9c74e9bfa79a3fb32de022b74 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Tue, 1 Jul 2014 18:47:04 -0300 Subject: [PATCH] Driver Settlement - #3232 --- db/migrate/000198_settlement.sql | 5 + .../default/cockpit2/api/settlement/index.php | 52 +-- include/library/Cockpit/Payment/Schedule.php | 3 + .../Cockpit/Payment/Schedule/Order.php | 24 +- .../Crunchbutton/Order/Transaction.php | 25 +- include/library/Crunchbutton/Payment.php | 4 + include/library/Crunchbutton/Settlement.php | 125 ++++++- .../frontend/settlement-drivers.phtml | 322 ++++++++++-------- .../frontend/settlement-restaurants.phtml | 2 +- www/assets/cockpit/js/cockpit.js | 5 + .../cockpit/js/controllers.settlement.js | 63 +++- www/assets/cockpit/js/service.driverorders.js | 14 +- www/assets/cockpit/js/service.settlement.js | 7 + 13 files changed, 450 insertions(+), 201 deletions(-) create mode 100644 db/migrate/000198_settlement.sql diff --git a/db/migrate/000198_settlement.sql b/db/migrate/000198_settlement.sql new file mode 100644 index 000000000..3a277c2e0 --- /dev/null +++ b/db/migrate/000198_settlement.sql @@ -0,0 +1,5 @@ +ALTER TABLE `payment_schedule` ADD `pay_type` enum('payment','reimbursement') DEFAULT 'payment'; + +ALTER TABLE `payment` ADD `pay_type` enum('payment','reimbursement') DEFAULT 'payment'; + +ALTER TABLE `order_transaction` CHANGE `type` `type` enum('debit','credit','paid-to-restaurant','paid-to-driver','reimbursed-driver') DEFAULT NULL; \ No newline at end of file diff --git a/include/controllers/default/cockpit2/api/settlement/index.php b/include/controllers/default/cockpit2/api/settlement/index.php index 989f9a4d3..964d93cfe 100644 --- a/include/controllers/default/cockpit2/api/settlement/index.php +++ b/include/controllers/default/cockpit2/api/settlement/index.php @@ -3,29 +3,19 @@ class Controller_Api_Settlement extends Crunchbutton_Controller_RestAccount { public function init() { -/* - $set = new Settlement; + // $this->_driverBegin(); + // exit; - $order = Order::o( 24462 ); - $order = Order::o( 24463 ); - $order = Order::o( 24464 ); + // $set = new Settlement; - $vars = [ - $set->orderExtractVariables( Order::o( 24464 ) ), - $set->orderExtractVariables( Order::o( 24463 ) ), - $set->orderExtractVariables( Order::o( 24462 ) ), - ]; + // 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 ); -echo json_encode( $set->driversProcessOrders( $vars ) );exit; + // exit; - $this->_driverBegin(); - - - - exit; -*/ if( !c::admin()->permission()->check( ['global', 'settlement' ] ) ){ $this->_error(); } @@ -114,6 +104,9 @@ echo json_encode( $set->driversProcessOrders( $vars ) );exit; case 'transfer-driver': $this->_driverTransferDeliveryDriver(); break; + case 'schedule': + $this->_driverSchedule(); + break; default: $this->_error(); break; @@ -396,8 +389,10 @@ echo json_encode( $set->driversProcessOrders( $vars ) );exit; $settlement = new Settlement( [ 'start' => $start, 'end' => $end ] ); $orders = $settlement->startDriver(); - $out = [ 'drivers' => [] ]; - $out = [ 'notes' => Crunchbutton_Settlement::DEFAULT_NOTES ]; + $out = [ 'drivers' => [], + 'notes' => Crunchbutton_Settlement::DEFAULT_NOTES, + 'payment' => Cockpit_Payment_Schedule::PAY_TYPE_PAYMENT, + 'reimbursement' => Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT ]; foreach ( $orders as $key => $val ) { if( !$orders[ $key ][ 'name' ] ){ continue; @@ -458,6 +453,25 @@ echo json_encode( $set->driversProcessOrders( $vars ) );exit; echo json_encode( [ 'success' => true ] ); } + private function _driverSchedule(){ + $start = $this->request()['start']; + $end = $this->request()['end']; + $pay_type = $this->request()['pay_type']; + $_id_drivers = explode( ',', $this->request()['id_drivers'] ); + $id_drivers = []; + foreach ( $_id_drivers as $key => $val ) { + $id_driver = trim( $val ); + $notes = $this->request()[ 'notes_' . $id_driver ]; + $adjustment = $this->request()[ 'adjustments_' . $id_driver ]; + $id_drivers[ $id_driver ] = []; + $id_drivers[ $id_driver ][ 'notes' ] = ( $notes ) ? $notes : Crunchbutton_Settlement::DEFAULT_NOTES; + $id_drivers[ $id_driver ][ 'adjustment' ] = $adjustment; + } + $settlement = new Settlement( [ 'payment_method' => $pay_type, 'start' => $start, 'end' => $end ] ); + $settlement->scheduleDriverPayment( $id_drivers, $pay_type ); + echo json_encode( [ 'success' => true ] ); + } + private function _range(){ $now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) ); $range = [ 'end' => $now->format( 'Y,m,d' ) ]; diff --git a/include/library/Cockpit/Payment/Schedule.php b/include/library/Cockpit/Payment/Schedule.php index 808753119..a00bbb3ca 100644 --- a/include/library/Cockpit/Payment/Schedule.php +++ b/include/library/Cockpit/Payment/Schedule.php @@ -5,6 +5,9 @@ class Cockpit_Payment_Schedule extends Cana_Table { const TYPE_RESTAURANT = 'restaurant'; const TYPE_DRIVER = 'driver'; + const PAY_TYPE_PAYMENT = 'payment'; + const PAY_TYPE_REIMBURSEMENT = 'reimbursement'; + const STATUS_SCHEDULED = 'scheduled'; const STATUS_PROCESSING = 'processing'; const STATUS_DONE = 'done'; diff --git a/include/library/Cockpit/Payment/Schedule/Order.php b/include/library/Cockpit/Payment/Schedule/Order.php index 5c6acaae0..1c6c5b457 100644 --- a/include/library/Cockpit/Payment/Schedule/Order.php +++ b/include/library/Cockpit/Payment/Schedule/Order.php @@ -11,7 +11,29 @@ class Cockpit_Payment_Schedule_Order extends Cana_Table { public function checkOrderWasPaidRestaurant( $id_order ){ $query = 'SELECT * FROM payment_schedule_order pso - INNER JOIN payment_schedule ps ON ps.id_payment_schedule = pso.id_payment_schedule AND ps.type = "' . Cockpit_Payment_Schedule::TYPE_RESTAURANT . '" + INNER JOIN payment_schedule ps ON ps.id_payment_schedule = pso.id_payment_schedule AND ps.type = "' . Cockpit_Payment_Schedule::TYPE_RESTAURANT . '" AND ps.pay_type = "' . Cockpit_Payment_Schedule::PAY_TYPE_PAYMENT . '" + WHERE pso.id_order = "' . $id_order . '" LIMIT 1'; + $order = Cockpit_Payment_Schedule_Order::q( $query ); + if( $order->id_payment_schedule_order ){ + return true; + } + return false; + } + + public function checkOrderWasReimbursedDriver( $id_order ){ + $query = 'SELECT * FROM payment_schedule_order pso + INNER JOIN payment_schedule ps ON pso.id_payment_schedule = pso.id_payment_schedule AND ps.type = "' . Cockpit_Payment_Schedule::TYPE_DRIVER . '" AND ps.pay_type = "' . Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT . '" + WHERE pso.id_order = "' . $id_order . '" LIMIT 1'; + $order = Cockpit_Payment_Schedule_Order::q( $query ); + if( $order->id_payment_schedule_order ){ + return true; + } + return false; + } + + public function checkOrderWasPaidDriver( $id_order ){ + $query = 'SELECT * FROM payment_schedule_order pso + INNER JOIN payment_schedule ps ON pso.id_payment_schedule = pso.id_payment_schedule AND ps.type = "' . Cockpit_Payment_Schedule::TYPE_DRIVER . '" AND ps.pay_type = "' . Cockpit_Payment_Schedule::PAY_TYPE_PAYMENT . '" WHERE pso.id_order = "' . $id_order . '" LIMIT 1'; $order = Cockpit_Payment_Schedule_Order::q( $query ); if( $order->id_payment_schedule_order ){ diff --git a/include/library/Crunchbutton/Order/Transaction.php b/include/library/Crunchbutton/Order/Transaction.php index 193791f54..eaa9c11f1 100644 --- a/include/library/Crunchbutton/Order/Transaction.php +++ b/include/library/Crunchbutton/Order/Transaction.php @@ -7,6 +7,7 @@ class Crunchbutton_Order_Transaction extends Cana_Table { const TYPE_PAID_TO_RESTAURANT = 'paid-to-restaurant'; const TYPE_PAID_TO_DRIVER = 'paid-to-driver'; + const TYPE_REIMBURSED_TO_DRIVER = 'reimbursed-driver'; const PAYMENT_TYPE_GIFT = 'gift'; const PAYMENT_TYPE_CARD = 'card'; @@ -14,9 +15,27 @@ class Crunchbutton_Order_Transaction extends Cana_Table { const SOURCE_RESTAURANT = 'restaurant'; public function checkOrderWasPaidRestaurant( $id_order ){ - $query = 'SELECT * FROM order_transaction ot WHERE type = "' . Crunchbutton_Order_Transaction::TYPE_PAID_TO_RESTAURANT . '" LIMIT 1'; - $order = Cockpit_Payment_Schedule_Order::q( $query ); - if( $order->id_payment_schedule_order ){ + $query = 'SELECT * FROM order_transaction ot WHERE type = "' . Crunchbutton_Order_Transaction::TYPE_PAID_TO_RESTAURANT . '" AND id_order = "' . $id_order . '" LIMIT 1'; + $order = Crunchbutton_Order_Transaction::q( $query ); + if( $order->id_order_transaction ){ + return true; + } + return false; + } + + public function checkOrderWasReimbursedDriver( $id_order ){ + $query = 'SELECT * FROM order_transaction ot WHERE type = "' . Crunchbutton_Order_Transaction::TYPE_REIMBURSED_TO_DRIVER . '" AND id_order = "' . $id_order . '" LIMIT 1'; + $order = Crunchbutton_Order_Transaction::q( $query ); + if( $order->id_order_transaction ){ + return true; + } + return false; + } + + public function checkOrderWasPaidDriver( $id_order ){ + $query = 'SELECT * FROM order_transaction ot WHERE type = "' . Crunchbutton_Order_Transaction::TYPE_PAID_TO_DRIVER . '" AND id_order = "' . $id_order . '" LIMIT 1'; + $order = Crunchbutton_Order_Transaction::q( $query ); + if( $order->id_order_transaction ){ return true; } return false; diff --git a/include/library/Crunchbutton/Payment.php b/include/library/Crunchbutton/Payment.php index ef7d0406c..e5d029ed0 100644 --- a/include/library/Crunchbutton/Payment.php +++ b/include/library/Crunchbutton/Payment.php @@ -1,6 +1,10 @@ orderSubtotalDriveryPay( $order ); $order[ 'pay_info' ][ 'tax' ] = $this->orderTaxDriverPay( $order ); @@ -158,8 +153,19 @@ class Crunchbutton_Settlement extends Cana_Model { $order[ 'pay_info' ][ 'total_reimburse' ] = $this->orderReimburseDriver( $order ); $order[ 'pay_info' ][ 'total_payment' ] = $this->orderCalculateTotalDueDriver( $order[ 'pay_info' ] ); + if( $order[ 'driver_reimbursed' ] ){ + $order[ 'pay_info' ][ 'total_reimburse' ] = 0; + } + if( $order[ 'driver_paid' ] ){ + $order[ 'pay_info' ][ 'total_payment' ] = 0; + } + $pay[ $driver ][ 'orders' ][] = $order; - // echo json_encode( $order_info );exit; + + if( $order[ 'do_not_pay_driver' ] == 1 ){ + continue; + } + $pay[ $driver ][ 'subtotal' ] += $order[ 'pay_info' ][ 'subtotal' ]; $pay[ $driver ][ 'tax' ] += $order[ 'pay_info' ][ 'tax' ]; $pay[ $driver ][ 'delivery_fee' ] += $order[ 'pay_info' ][ 'delivery_fee' ]; @@ -170,14 +176,10 @@ class Crunchbutton_Settlement extends Cana_Model { $pay[ $driver ][ 'restaurant_fee' ] += $order[ 'pay_info' ][ 'restaurant_fee' ]; $pay[ $driver ][ 'gift_card' ] += $order[ 'pay_info' ][ 'gift_card' ]; $pay[ $driver ][ 'total_reimburse' ] += $order[ 'pay_info' ][ 'total_reimburse' ]; - + $pay[ $driver ][ 'total_payment' ] += $order[ 'pay_info' ][ 'total_payment' ]; } } - foreach( $pay as $key => $val ){ - $pay[ $key ][ 'total_payment' ] = $this->orderCalculateTotalDueDriver( $pay[ $key ] ); - } - usort( $pay, function( $a, $b ) { return $a[ 'name'] > $b[ 'name' ]; }); @@ -313,7 +315,7 @@ class Crunchbutton_Settlement extends Cana_Model { // https://github.com/crunchbutton/crunchbutton/issues/3232#issuecomment-47254475 // https://github.com/crunchbutton/crunchbutton/issues/3232#issuecomment-47283481 public function orderReimburseDriver( $arr ){ - return ( $arr[ 'subtotal' ] + $arr[ 'tax' ] ) * $arr[ 'credit' ] * $arr[ 'delivery_service' ]; + return ( $arr[ 'subtotal' ] + $arr[ 'tax' ] ) * $arr[ 'credit' ] * $arr[ 'delivery_service' ] * ( 1 - $arr[ 'formal_relationship' ] ); } // Drivers are paid the whole tip from credit orders. @@ -412,14 +414,24 @@ class Crunchbutton_Settlement extends Cana_Model { $values[ 'pay_if_refunded' ] = ( $order->pay_if_refunded > 0 ) ? 1: 0; $values[ 'reimburse_cash_order' ] = ( $order->reimburse_cash_order > 0 ) ? 1: 0; + // convert all to float -> mysql returns some values as string + foreach( $values as $key => $val ){ + $values[ $key ] = floatval( $val ); + } + $values[ 'restaurant_paid' ] = Cockpit_Payment_Schedule_Order::checkOrderWasPaidRestaurant( $order->id_order ); if( !$values[ 'restaurant_paid' ] ){ $values[ 'restaurant_paid' ] = Crunchbutton_Order_Transaction::checkOrderWasPaidRestaurant( $order->id_order ); } - // convert all to float -> mysql returns some values as string - foreach( $values as $key => $val ){ - $values[ $key ] = floatval( $val ); + $values[ 'driver_reimbursed' ] = Cockpit_Payment_Schedule_Order::checkOrderWasReimbursedDriver( $order->id_order ); + if( !$values[ 'driver_reimbursed' ] ){ + $values[ 'driver_reimbursed' ] = Crunchbutton_Order_Transaction::checkOrderWasReimbursedDriver( $order->id_order ); + } + + $values[ 'driver_paid' ] = Cockpit_Payment_Schedule_Order::checkOrderWasPaidDriver( $order->id_order ); + if( !$values[ 'driver_paid' ] ){ + $values[ 'driver_paid' ] = Crunchbutton_Order_Transaction::checkOrderWasPaidDriver( $order->id_order ); } if( $values[ 'id_admin' ] ){ @@ -442,7 +454,6 @@ class Crunchbutton_Settlement extends Cana_Model { $this->log( 'scheduleRestaurantPayment', $id_restaurants ); $restaurants = $this->startRestaurant(); foreach ( $restaurants as $_restaurant ) { - // todo: build a better way to filter - this way is very ugly if( !$id_restaurants[ $_restaurant->id_restaurant ] ){ continue; } @@ -472,6 +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->type = Cockpit_Payment_Schedule::TYPE_RESTAURANT; $schedule->status = Cockpit_Payment_Schedule::STATUS_SCHEDULED; $schedule->note = $notes; @@ -491,8 +503,87 @@ class Crunchbutton_Settlement extends Cana_Model { } } $settlement = new Crunchbutton_Settlement; - // Cana::timeout(function() use( $settlement ) { + Cana::timeout(function() use( $settlement ) { $settlement->doRestaurantPayments(); + } ); + } + + public function scheduleDriverPayment( $id_drivers, $type ){ + + $this->log( 'scheduleDriversPayment', $id_drivers ); + + $drivers = $this->startDriver(); + + foreach ( $drivers as $_driver ) { + + if( !$id_drivers[ $_driver[ 'id_admin' ] ] ){ + continue; + } + + $notes = $id_drivers[ $_driver[ 'id_admin' ] ][ 'notes' ]; + $adjustment = $id_drivers[ $_driver[ 'id_admin' ] ][ 'adjustment' ]; + $id_driver = $_driver[ 'id_admin' ]; + + $shouldSchedule = false; + if( $type == Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT ){ + $shouldSchedule = ( $_driver[ 'total_reimburse' ] != 0 ) ? true : false; + } else { + $shouldSchedule = ( $_driver[ 'total_payment' ] != 0 ) ? true : false; + } + + foreach ( $_driver[ 'orders' ] as $order ) { + if( $type == Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT ){ + if( !$order[ 'driver_reimbursed' ] ){ + $shouldSchedule = true; + } + } else { + if( !$order[ 'driver_paid' ] ){ + $shouldSchedule = true; + } + } + } + + if( $shouldSchedule ){ + + // schedule it + $schedule = new Cockpit_Payment_Schedule; + $schedule->id_driver = $id_driver; + $schedule->date = date( 'Y-m-d H:i:s' ); + + if( $type == Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT ){ + $amount = $_driver[ 'total_reimburse' ]; + } else { + $amount = $_driver[ 'total_payment' ] + $adjustment; + } + + $schedule->amount = max( $amount, 0 ); + $schedule->adjustment = $adjustment; + $schedule->pay_type = $type; + $schedule->type = Cockpit_Payment_Schedule::TYPE_DRIVER; + $schedule->status = Cockpit_Payment_Schedule::STATUS_SCHEDULED; + $schedule->note = $notes; + $schedule->id_admin = c::user()->id_admin; + $schedule->save(); + $id_payment_schedule = $schedule->id_payment_schedule; + + foreach ( $_driver[ 'orders' ] as $order ) { + if( $type == Cockpit_Payment_Schedule::PAY_TYPE_REIMBURSEMENT ){ + $order_amount = $order[ 'pay_info' ][ 'total_reimburse' ]; + } else { + $order_amount = $order[ 'pay_info' ][ 'total_payment' ]; + } + $schedule_order = new Cockpit_Payment_Schedule_Order; + $schedule_order->id_payment_schedule = $id_payment_schedule; + $schedule_order->id_order = $order[ 'id_order' ]; + $schedule_order->amount = $order_amount; + $schedule_order->save(); + } + $this->log( 'scheduleDriverPayment', $schedule->properties() ); + } + } + // $settlement = new Crunchbutton_Settlement; + // Cana::timeout(function() use( $settlement ) { + // $settlement->dodriverPayments(); // } ); } diff --git a/include/views/default/cockpit2/frontend/settlement-drivers.phtml b/include/views/default/cockpit2/frontend/settlement-drivers.phtml index 4dfe3f585..23acbe7a0 100644 --- a/include/views/default/cockpit2/frontend/settlement-drivers.phtml +++ b/include/views/default/cockpit2/frontend/settlement-drivers.phtml @@ -82,6 +82,10 @@ Total of Not Included Orders {{total_not_included}} + + Total Reimbursements + $ {{total_reimbursements | formatPrice}} + Total Payments $ {{total_payments | formatPrice}} @@ -94,17 +98,22 @@ + + - + + + + - {{total_due}} - - + +
# DriversTotal DueReimbursementPayment
+ {{driver.id_admin}} {{driver.name}}$ {{driver.total_due | formatPrice}}Details$ {{driver.total_reimburse | formatPrice}}$ {{driver.total_payment | formatPrice}}
@@ -156,165 +165,186 @@ Adjustments $ {{sum.adjustment | formatPrice}} - - Total due - $ {{sum.total_due | formatPrice}} + + Total reimbursements + $ {{sum.total_reimburse | formatPrice}} + + + Total payments + $ {{sum.total_payment | formatPrice}} +

-

- Drivers Details -

+
-
- -

- {{driver.name}} +

+ Drivers Details

-
+
-
+
-

- Last payment #{{driver.last_payment.id_payment}}: $ {{driver.last_payment.amount | formatPrice}} on {{driver.last_payment.date}} -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - Include this driver at the payment? - - - Include this driver at the payment? - - -
-
    -
  • -
    Payment notes:
    -
    -
  • -
-
-
    -
  • -
    Adjustments:
    -
    -
  • -
-
Subtotal$ {{driver.subtotal | formatPrice}}
Tax$ {{driver.tax | formatPrice}}
Delivery Fee$ {{driver.delivery_fee | formatPrice}}
Tip$ {{driver.tip | formatPrice}}
Customer Fee$ {{driver.customer_fee | formatPrice}}
Markup$ {{driver.markup | formatPrice}}
Credit Card Charge$ {{driver.credit_charge | formatPrice}}
Crunchbutton Fees$ {{driver.restaurant_fee | formatPrice}}
Gift Card$ {{driver.gift_card | formatPrice}}
Adjustments$ {{driver.adjustment | formatPrice}}
Total due$ {{driver.total_due | formatPrice}}
+

+ {{driver.name}} +

-

- Show orders - Hide orders (total {{driver.orders_count}} orders + {{driver.not_included}} not included order(s) ) -

+
+ +
+ +

+ Last payment #{{driver.last_payment.id_payment}}: $ {{driver.last_payment.amount | formatPrice}} on {{driver.last_payment.date}} +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + Include this driver at the payment? + + + Include this driver at the payment? + + +
+
    +
  • +
    Payment notes:
    +
    +
  • +
+
+
    +
  • +
    Adjustments:
    +
    +
  • +
+
Subtotal$ {{driver.subtotal | formatPrice}}
Tax$ {{driver.tax | formatPrice}}
Delivery Fee$ {{driver.delivery_fee | formatPrice}}
Tip$ {{driver.tip | formatPrice}}
Customer Fee$ {{driver.customer_fee | formatPrice}}
Markup$ {{driver.markup | formatPrice}}
Credit Card Charge$ {{driver.credit_charge | formatPrice}}
Crunchbutton Fees$ {{driver.restaurant_fee | formatPrice}}
Gift Card$ {{driver.gift_card | formatPrice}}
Adjustments$ {{driver.adjustment | formatPrice}}
Reimbursement$ {{driver.total_reimburse | formatPrice}}
Payment$ {{driver.total_payment | formatPrice}}
+ +

+ Show orders + Hide orders (total {{driver.orders_count}} orders + {{driver.not_included}} not included order(s) ) +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
#RestaurantNameAmountReimbursePaymentTypeDate
{{order.id_order}}{{order.restaurant}}{{order.name}}$ {{order.total | formatPrice}}$ {{order.total_reimburse | formatPrice}}$ {{order.total_payment | formatPrice}}{{order.pay_type}}{{order.date}} + + Do not pay for it. + + + Will not be paid! + + + +
+
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#RestaurantNameAmountPaymentDate
{{order.id_order}}{{order.restaurant}}{{order.name}}$ {{order.total | formatPrice}}{{order.pay_type}}{{order.date}} - - Do not pay for it. - - - Will not be paid! - - - -
diff --git a/include/views/default/cockpit2/frontend/settlement-restaurants.phtml b/include/views/default/cockpit2/frontend/settlement-restaurants.phtml index 8443f9509..fe1c97f29 100644 --- a/include/views/default/cockpit2/frontend/settlement-restaurants.phtml +++ b/include/views/default/cockpit2/frontend/settlement-restaurants.phtml @@ -357,7 +357,7 @@ # Name Amount - Payment + Type Date diff --git a/www/assets/cockpit/js/cockpit.js b/www/assets/cockpit/js/cockpit.js index cfe7d5df7..716f6365e 100644 --- a/www/assets/cockpit/js/cockpit.js +++ b/www/assets/cockpit/js/cockpit.js @@ -135,6 +135,11 @@ NGApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $l controller: 'SettlementDriversCtrl', templateUrl: 'assets/view/settlement-drivers.html' }) + .when('/settlement/drivers/scheduled', { + action: 'settlement', + controller: 'SettlementDriversScheduledCtrl', + templateUrl: 'assets/view/settlement-drivers-scheduled.html' + }) .when('/drivers/orders', { action: 'drivers-orders', controller: 'DriversOrdersCtrl', diff --git a/www/assets/cockpit/js/controllers.settlement.js b/www/assets/cockpit/js/controllers.settlement.js index 5267c9b5b..bd7d6fcf6 100644 --- a/www/assets/cockpit/js/controllers.settlement.js +++ b/www/assets/cockpit/js/controllers.settlement.js @@ -201,7 +201,6 @@ NGApp.controller('SettlementRestaurantsCtrl', function ( $scope, $filter, Settle } $scope.show_details = function( restaurant ){ - if( !restaurant.show_details ){ $scope.showing_details = true; restaurant.show_details = true; @@ -408,7 +407,7 @@ NGApp.controller('SettlementDriversCtrl', function ( $scope, $filter, Settlement 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 ); } } ); } @@ -464,20 +463,22 @@ NGApp.controller('SettlementDriversCtrl', function ( $scope, $filter, Settlement $scope.summary = function(){ - var sum = { 'subtotal': 0, 'tax': 0, 'delivery_fee': 0, 'tip': 0, 'customer_fee': 0, 'markup': 0, 'credit_charge': 0, 'gift_card': 0, 'restaurant_fee': 0, 'total_due': 0, 'adjustment' : 0 }; + var sum = { 'subtotal': 0, 'tax': 0, 'delivery_fee': 0, 'tip': 0, 'customer_fee': 0, 'markup': 0, 'credit_charge': 0, 'gift_card': 0, 'restaurant_fee': 0, 'total_payment': 0, 'total_reimburse':0, 'adjustment' : 0 }; var total_drivers = 0; var total_payments = 0; + var total_reimbursements = 0; var total_orders = 0; var total_not_included = 0; var total_adjustments = 0; var total_refunded = 0; for( x in $scope.result.drivers ){ - $scope.result.drivers[ x ].total_due = ( $scope.result.drivers[ x ].total_due_without_adjustment + $scope.result.drivers[ x ].adjustment ); + $scope.result.drivers[ x ].total_payments = ( $scope.result.drivers[ x ].total_payment_without_adjustment + $scope.result.drivers[ x ].adjustment ); if( $scope.result.drivers[ x ].pay ){ total_drivers++; // include the adjustment at total_due - total_payments += $scope.result.drivers[ x ].total_due; + total_payments += $scope.result.drivers[ x ].total_payments; + total_reimbursements += $scope.result.drivers[ x ].total_reimburse; total_orders += $scope.result.drivers[ x ].orders_count; total_not_included += $scope.result.drivers[ x ].not_included; @@ -492,6 +493,7 @@ NGApp.controller('SettlementDriversCtrl', function ( $scope, $filter, Settlement } $scope.total_drivers = total_drivers; $scope.total_payments = total_payments; + $scope.total_reimbursements = total_reimbursements; $scope.total_orders = total_orders; $scope.total_not_included = total_not_included; $scope.total_refunded = total_refunded; @@ -535,8 +537,55 @@ NGApp.controller('SettlementDriversCtrl', function ( $scope, $filter, Settlement } } - $scope.show_details = function( id_driver ){ - $scope.walkTo( '#driver-' + id_driver, -80 ); + $scope.schedule_reimbursement = function(){ + schedule( $scope.result.reimbursement ); + } + + $scope.schedule_payment = function(){ + schedule( $scope.result.payment ); + } + + var schedule = function( pay_type ){ + + $scope.makeBusy(); + + var params = { 'start': $filter( 'date' )( $scope.range.start, 'yyyy-MM-dd' ), + 'end': $filter( 'date' )( $scope.range.end, 'yyyy-MM-dd' ), + 'pay_type': pay_type } + + var id_drivers = new Array(); + for( x in $scope.result.drivers ){ + if( $scope.result.drivers[ x ].pay ){ + id_drivers.push( $scope.result.drivers[ x ].id_admin ); + params[ 'notes_' + $scope.result.drivers[ x ].id_admin ] = $scope.result.drivers[ x ].notes; + params[ 'adjustments_' + $scope.result.drivers[ x ].id_admin ] = $scope.result.drivers[ x ].adjustment; + } + } + id_drivers = id_drivers.join( ',' ); + params[ 'id_drivers' ] = id_drivers; + SettlementService.drivers.schedule( params, function( json ){ + $scope.unBusy(); + $scope.navigation.link( '/settlement/drivers/scheduled' ); + } ); + } + + + $scope.show_details = function( driver ){ + if( !driver.show_details ){ + $scope.showing_details = true; + driver.show_details = true; + setTimeout( function(){ + $scope.walkTo( '#driver-' + driver.id_admin, -80 ); + } ); + } else { + driver.show_details = false; + $scope.showing_details = false; + for( x in $scope.result.drivers ){ + if( $scope.result.drivers[ x ].show_details ){ + $scope.showing_details = true; + } + } + } } // Just run if the user is loggedin diff --git a/www/assets/cockpit/js/service.driverorders.js b/www/assets/cockpit/js/service.driverorders.js index d2b7b26f3..5d269f121 100644 --- a/www/assets/cockpit/js/service.driverorders.js +++ b/www/assets/cockpit/js/service.driverorders.js @@ -1,5 +1,5 @@ NGApp.factory( 'DriverOrdersService', function( $rootScope, $resource, $routeParams ) { - + var service = {}; // Create a private resource 'orders' @@ -11,21 +11,21 @@ NGApp.factory( 'DriverOrdersService', function( $rootScope, $resource, $routePar 'reject' : { 'method': 'POST', params : { 'action' : 'delivery-reject' } }, 'pickedup' : { 'method': 'POST', params : { 'action' : 'delivery-pickedup' } }, 'delivered' : { 'method': 'POST', params : { 'action' : 'delivery-delivered' } } - } + } ); service.list = function( callback ){ - orders.query( {}, function( data ){ + orders.query( {}, function( data ){ var orders = []; for( var x in data ){ var order = data[ x ]; if( order && order.date && order.date.date ){ - order._date = new Date( order.date.date ); + order._date = new Date( order.date.date ); orders.push( order ); } } service.newOrdersBadge(); - callback( orders ); + callback( orders ); } ); } @@ -51,9 +51,9 @@ NGApp.factory( 'DriverOrdersService', function( $rootScope, $resource, $routePar service.get = function( callback ){ var id_order = $routeParams.id; - orders.get( { 'id_order': id_order }, function( order ){ + orders.get( { 'id_order': id_order }, function( order ){ order._date = new Date( order.date ); - callback( order ); + callback( order ); } ); } diff --git a/www/assets/cockpit/js/service.settlement.js b/www/assets/cockpit/js/service.settlement.js index c10111f5e..07d0b4ab6 100644 --- a/www/assets/cockpit/js/service.settlement.js +++ b/www/assets/cockpit/js/service.settlement.js @@ -26,6 +26,7 @@ NGApp.factory( 'SettlementService', function( $resource, $http, $routeParams ) { 'range' : { 'method': 'GET', params : { action: 'range' } }, 'do_not_pay_driver' : { 'method': 'POST', params : { action: 'do-not-pay-driver' } }, 'transfer_driver' : { 'method': 'POST', params : { action: 'transfer-driver' } }, + 'schedule' : { 'method': 'POST', params : { action: 'schedule' } }, 'begin' : { 'method': 'POST', params : { action: 'begin' } } } ); @@ -136,6 +137,12 @@ NGApp.factory( 'SettlementService', function( $resource, $http, $routeParams ) { } ); } + service.drivers.schedule = function( params, callback ){ + settlement.drivers.schedule( params, function( json ){ + callback( json ); + } ); + } + service.drivers.range = function( callback ){ settlement.drivers.range( function( json ){ callback( json );