diff --git a/db/migrate/000194_payment.sql b/db/migrate/000194_payment.sql deleted file mode 100644 index 238cbed67..000000000 --- a/db/migrate/000194_payment.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE `payment` ADD `id_admin` int(11) unsigned DEFAULT NULL; -ALTER TABLE `payment` ADD KEY `payment_ibfk_2` (`id_admin`); -ALTER TABLE `payment` ADD CONSTRAINT `payment_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL \ No newline at end of file diff --git a/db/migrate/000194_settlement.sql b/db/migrate/000194_settlement.sql new file mode 100644 index 000000000..2f0b882a3 --- /dev/null +++ b/db/migrate/000194_settlement.sql @@ -0,0 +1,45 @@ +-- Settlement stuff + +-- payment_schedule - new table +CREATE TABLE `payment_schedule` ( + `id_payment_schedule` int(11) unsigned NOT NULL AUTO_INCREMENT, + `id_restaurant` int(11) unsigned DEFAULT NULL, + `id_driver` int(11) unsigned DEFAULT NULL, + `date` datetime DEFAULT NULL, + `amount` float DEFAULT NULL, + `note` varchar(255) DEFAULT '', + `payment_method` enum('deposit','check') DEFAULT NULL, + `type` enum('restaurant','driver') DEFAULT NULL, + `status` enum('scheduled','processing','done','error') DEFAULT NULL, + `status_date` datetime DEFAULT NULL, + `stripe_id` varchar(255) DEFAULT NULL, + `balanced_id` varchar(255) DEFAULT NULL, + `id_admin` int(11) unsigned DEFAULT NULL, + PRIMARY KEY (`id_payment_schedule`), + KEY `id_restaurant` (`id_restaurant`), + KEY `payment_schedule_ibfk_2` (`id_admin`), + KEY `payment_schedule_ibfk_3` (`id_driver`), + CONSTRAINT `payment_schedule_ibfk_1` FOREIGN KEY (`id_restaurant`) REFERENCES `restaurant` (`id_restaurant`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `payment_schedule_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL, + CONSTRAINT `payment_schedule_ibfk_3` FOREIGN KEY (`id_driver`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + +CREATE TABLE `payment_schedule_order` ( + `id_payment_schedule_order` int(11) unsigned NOT NULL AUTO_INCREMENT, + `id_payment_schedule` int(11) unsigned DEFAULT NULL, + `id_order` int(11) unsigned DEFAULT NULL, + PRIMARY KEY (`id_payment_schedule_order`), + KEY `id_payment_schedule` (`id_payment_schedule`), + KEY `payment_schedule_order_ibfk_2` (`id_order`), + CONSTRAINT `payment_schedule_order_ibfk_1` FOREIGN KEY (`id_payment_schedule`) REFERENCES `payment_schedule` (`id_payment_schedule`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `payment_schedule_order_ibfk_2` FOREIGN KEY (`id_order`) REFERENCES `order` (`id_order`) ON DELETE SET NULL ON UPDATE SET NULL +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + + +-- order table +ALTER TABLE `order` ADD `reimburse_cash_order` tinyint(11) unsigned DEFAULT 0; + +-- payment table +ALTER TABLE `payment` ADD `id_admin` int(11) unsigned DEFAULT NULL; +ALTER TABLE `payment` ADD KEY `payment_ibfk_2` (`id_admin`); +ALTER TABLE `payment` ADD CONSTRAINT `payment_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET 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 20691867b..4b3c8a870 100644 --- a/include/controllers/default/cockpit2/api/settlement/index.php +++ b/include/controllers/default/cockpit2/api/settlement/index.php @@ -49,6 +49,9 @@ class Controller_api_settlement extends Crunchbutton_Controller_RestAccount { case 'pay-if-refunded': $this->_restaurantPayIfRefunded(); break; + case 'schedule': + $this->_restaurantSchedule(); + break; default: $this->_error(); break; @@ -72,6 +75,20 @@ class Controller_api_settlement extends Crunchbutton_Controller_RestAccount { } } + private function _restaurantSchedule(){ + $start = $this->request()['start']; + $end = $this->request()['end']; + $_id_restaurants = explode( ',', $this->request()['id_restaurants'] ); + $id_restaurants = []; + foreach ( $_id_restaurants as $key => $val ) { + $id_restaurants[ trim( $val ) ] = true; + } + $pay_type = ( $this->request()['pay_type'] == 'all' ) ? '' : $this->request()['pay_type']; + $settlement = new Settlement( [ 'payment_method' => $pay_type, 'start' => $start, 'end' => $end ] ); + $settlement->schedule_payment( $id_restaurants ); + echo json_encode( [ 'success' => true ] ); + } + private function _restaurantPayIfRefunded(){ $id_order = $this->request()['id_order']; $pay_if_refunded = $this->request()['pay_if_refunded']; diff --git a/include/library/Cockpit/Payment/Schedule.php b/include/library/Cockpit/Payment/Schedule.php new file mode 100644 index 000000000..74bf124bd --- /dev/null +++ b/include/library/Cockpit/Payment/Schedule.php @@ -0,0 +1,22 @@ +table('payment_schedule')->idVar('id_payment_schedule')->load($id); + } + + public function orders() { + return Cockpit_Payment_Schedule_Order::q( 'SELECT * FROM payment_schedule_order WHERE id_payment_schedule = "' . $this->id_payment_schedule . '"' ); + } + +} \ No newline at end of file diff --git a/include/library/Cockpit/Payment/Schedule/Order.php b/include/library/Cockpit/Payment/Schedule/Order.php new file mode 100644 index 000000000..a1c7c0124 --- /dev/null +++ b/include/library/Cockpit/Payment/Schedule/Order.php @@ -0,0 +1,26 @@ +table('payment_schedule_order')->idVar('id_payment_schedule_order')->load($id); + } + public function payment_schedule() { + return Cockpit_Payment_Schedule::o($this->id_payment_schedule); + } + + 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 . '" + 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 order() { + return Cockpit_Order::o($this->order); + } +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Settlement.php b/include/library/Crunchbutton/Settlement.php index aceb9e8be..0e50fcb9b 100644 --- a/include/library/Crunchbutton/Settlement.php +++ b/include/library/Crunchbutton/Settlement.php @@ -67,7 +67,7 @@ class Crunchbutton_Settlement extends Cana_Model { foreach ( $orders as $order ) {; if( $order ){ // Pay if Refunded - if( $order[ 'refunded' ] == 1 && $order[ 'pay_if_refunded' ] == 0 ){ + if( ( $order[ 'refunded' ] == 1 && $order[ 'pay_if_refunded' ] == 0 ) || $order[ 'restaurant_paid' ] ){ continue; } $pay[ 'card_subtotal' ] += $this->orderCardSubtotalPayment( $order ); @@ -335,6 +335,8 @@ 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; + $values[ 'restaurant_paid' ] = Cockpit_Payment_Schedule_Order::checkOrderWasPaidRestaurant( $order->id_order ); + // convert all to float -> mysql returns some values as string foreach( $values as $key => $val ){ $values[ $key ] = floatval( $val ); @@ -354,4 +356,49 @@ class Crunchbutton_Settlement extends Cana_Model { return $values; } + + public function schedule_payment( $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; + } + $id_restaurant = $_restaurant->id_restaurant; + $payment_data = $_restaurant->payment_data; + + // check if it has any order to be paid + $shouldSchedule = ( $payment_data[ 'total_due' ] > 0 ) ? true : false; + + foreach ( $_restaurant->_payableOrders as $order ) { + $alreadyPaid = Cockpit_Payment_Schedule_Order::checkOrderWasPaidRestaurant( $order->id_order ); + if( !$alreadyPaid ){ + $shouldSchedule = true; + } + } + if( $shouldSchedule ){ + // schedule it + $schedule = new Cockpit_Payment_Schedule; + $schedule->id_restaurant = $_restaurant->id_restaurant; + $schedule->date = date( 'Y-m-d H:i:s' ); + $schedule->amount = min( $payment_data[ 'total_due' ], 0 ); + $schedule->payment_method = $_restaurant->payment_type()->payment_method; + $schedule->type = Cockpit_Payment_Schedule::TYPE_RESTAURANT; + $schedule->status = Cockpit_Payment_Schedule::STATUS_SCHEDULED; + $schedule->status_date = date( 'Y-m-d H:i:s' ); + $schedule->id_admin = c::user()->id_admin; + $schedule->save(); + $id_payment_schedule = $schedule->id_payment_schedule; + // save the orders + foreach ( $_restaurant->_payableOrders as $order ) { + $schedule_order = new Cockpit_Payment_Schedule_Order; + $schedule_order->id_payment_schedule = $id_payment_schedule; + $schedule_order->id_order = $order->id_order; + $schedule_order->save(); + } + } + + } + } + } diff --git a/include/views/default/cockpit2/frontend/settlement-restaurants.phtml b/include/views/default/cockpit2/frontend/settlement-restaurants.phtml index 4d6635782..880d0d220 100644 --- a/include/views/default/cockpit2/frontend/settlement-restaurants.phtml +++ b/include/views/default/cockpit2/frontend/settlement-restaurants.phtml @@ -91,6 +91,9 @@ + + +
| Name | Amount | Payment | -Refunded | -Date | + | + | - | ||||||
| - | {{order.id_order}} | -{{order.name}} | -$ {{order.total | formatPrice}} | -{{order.pay_type}} | -- Refunded - | +{{order.id_order}} | +{{order.name}} | +$ {{order.total | formatPrice}} | +{{order.pay_type}} | +{{order.date}} | - - - + + + Refunded. Include? + + + Refunded. Included! + | -{{order.date}} | ++ |