From 6eea1934df555e84505c8e7ceeed3ff9ea4d646c Mon Sep 17 00:00:00 2001 From: arzynik Date: Wed, 20 Nov 2013 21:04:50 -0800 Subject: [PATCH] fixes crunchbutton/issues#3 --- .../default/quick/api/order/index.php | 57 +++++++ include/library/Crunchbutton/Admin.php | 7 + include/library/Crunchbutton/Order.php | 141 ++++++++++++++++++ include/library/Crunchbutton/Order/Action.php | 11 ++ include/views/default/quick/layout/html.phtml | 127 ++++++++++++++-- include/views/default/quick/order/index.phtml | 22 +-- 6 files changed, 339 insertions(+), 26 deletions(-) create mode 100644 include/controllers/default/quick/api/order/index.php create mode 100644 include/library/Crunchbutton/Order/Action.php diff --git a/include/controllers/default/quick/api/order/index.php b/include/controllers/default/quick/api/order/index.php new file mode 100644 index 000000000..6d574bbfc --- /dev/null +++ b/include/controllers/default/quick/api/order/index.php @@ -0,0 +1,57 @@ +id_order) { + $order = Order::o(c::getPagePiece(2)); + } + + if (!$order->id_order) { + echo json_encode(['error' => 'invalid object']); + exit; + + } else { + if (get_class($order) != 'Crunchbutton_Order') { + $order = $order->get(0); + } + } + + if (1==1 || $this->method() == 'post') { + $res = []; + + switch (c::getPagePiece(3)) { + case 'delivery-pickedup': + $res['status'] = $order->deliveryPickedup(c::admin()); + break; + + case 'delivery-delivered': + $res['status'] = $order->deliveryDelivered(c::admin()); + break; + + case 'delivery-accept': + $res['status'] = $order->deliveryAccept(c::admin()); + break; + + case 'delivery-reject': + $order->deliveryReject(c::admin()); + $res['status'] = true; + break; + + default: + echo json_encode(['error' => 'invalid action']); + exit; + break; + } + } + + if ($order->deliveryStatus()) + $ret = $order->deliveryExports(); + $ret['status'] = $res['status']; + + echo json_encode($ret); + exit; + + } +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Admin.php b/include/library/Crunchbutton/Admin.php index 9a114ca84..9ff4a2db2 100644 --- a/include/library/Crunchbutton/Admin.php +++ b/include/library/Crunchbutton/Admin.php @@ -5,6 +5,13 @@ class Crunchbutton_Admin extends Cana_Table { return Crunchbutton_Admin::q('select * from admin where login="'.c::db()->escape($login).'" limit 1')->get(0); } + public function publicExports() { + return [ + 'name' => $this->name, + 'id_admin' => $this->id_admin + ]; + } + public function timezone() { if (!isset($this->_timezone)) { $this->_timezone = new DateTimeZone($this->timezone); diff --git a/include/library/Crunchbutton/Order.php b/include/library/Crunchbutton/Order.php index e86772d2e..ca1d00d63 100644 --- a/include/library/Crunchbutton/Order.php +++ b/include/library/Crunchbutton/Order.php @@ -1491,6 +1491,147 @@ class Crunchbutton_Order extends Cana_Table { } return array_unique( $restaurants_ids ); } + + /* + get the delivery status of the order based on reps or restaurants actions agaisnt it + @todo: add restaurant actions + */ + public function deliveryStatus($type = null) { + if (!$this->_actions) { + $this->_actions = Order_Action::q('select * from order_action where id_order="'.$this->id_order.'" order by timestamp'); + $this->_deliveryStatus = ['accepted' => false, 'delivered' => false, 'pickedup' => false]; + $acpt = []; + + foreach ($this->_actions as $action) { + switch ($action->type) { + case 'delivery-delivered': + $this->_deliveryStatus['delivered'] = Admin::o($admin); + break; + + case 'delivery-pickedup': + $this->_deliveryStatus['pickedup'] = Admin::o($admin); + break; + + case 'delivery-accepted': + $acpt[$action->id_admin] = true; + break; + + case 'delivery-rejected': + $acpt[$action->id_admin] = false; + break; + } + } + + foreach ($acpt as $admin => $status) { + if ($status) { + $this->_deliveryStatus['accepted'] = Admin::o($admin); + } + } + } + return $type === null ? $this->_deliveryStatus : $this->_deliveryStatus[$type]; + } + + public function deliveryAccept($admin) { + if ($this->deliveryStatus('accepted')) { + return false; + } + (new Order_Action([ + 'id_order' => $this->id_order, + 'id_admin' => $admin->id_admin, + 'timestamp' => date('Y-m-d H:i:s'), + 'type' => 'delivery-accepted' + ]))->save(); + $this->_actions = null; + return true; + } + + public function deliveryReject($admin) { + (new Order_Action([ + 'id_order' => $this->id_order, + 'id_admin' => $admin->id_admin, + 'timestamp' => date('Y-m-d H:i:s'), + 'type' => 'delivery-rejected' + ]))->save(); + $this->_actions = null; + return true; + } + + public function deliveryPickedup($admin) { + if (!$this->deliveryStatus('accepted')) { + die('asd'); + } + if (!$this->deliveryStatus('accepted') || $this->deliveryStatus('accepted')->id_admin != $admin->id_admin) { + return false; + } + (new Order_Action([ + 'id_order' => $this->id_order, + 'id_admin' => $admin->id_admin, + 'timestamp' => date('Y-m-d H:i:s'), + 'type' => 'delivery-pickedup' + ]))->save(); + $this->_actions = null; + return true; + } + + public function deliveryDelivered($admin) { + if (!$this->deliveryStatus('accepted')) { + die('asd'); + } + if (!$this->deliveryStatus('accepted') || $this->deliveryStatus('accepted')->id_admin != $admin->id_admin) { + return false; + } + (new Order_Action([ + 'id_order' => $this->id_order, + 'id_admin' => $admin->id_admin, + 'timestamp' => date('Y-m-d H:i:s'), + 'type' => 'delivery-delivered' + ]))->save(); + $this->_actions = null; + return true; + } + + public function deliveryReply($admin) { + $act = false; + foreach ($this->_actions as $action) { + if ($action->id_admin && $admin->id_admin) { + switch ($action->type) { + case 'delivery-delivered': + $act = 'delivered'; + continue; + break; + + case 'delivery-pickedup': + $act = 'pickedup'; + continue; + break; + + case 'delivery-accepted': + $act = 'accepted'; + continue; + break; + + case 'delivery-rejected': + $act = 'rejected'; + continue; + break; + } + } + } + return $act; + } + + public function deliveryExports() { + return [ + 'id_order' => $this->id_order, + 'uuid' => $this->uuid, + 'delivery-status' => [ + 'delivered' => $this->deliveryStatus('delivered') ? $this->deliveryStatus('delivered')->publicExports() : false, + 'pickedup' => $this->deliveryStatus('pickedup') ? $this->deliveryStatus('pickedup')->publicExports() : false, + 'accepted' => $this->deliveryStatus('accepted') ? $this->deliveryStatus('accepted')->publicExports() : false + ], + 'self-reply' => $this->deliveryReply(c::admin()) + ]; + } public function __construct($id = null) { parent::__construct(); diff --git a/include/library/Crunchbutton/Order/Action.php b/include/library/Crunchbutton/Order/Action.php new file mode 100644 index 000000000..160236b83 --- /dev/null +++ b/include/library/Crunchbutton/Order/Action.php @@ -0,0 +1,11 @@ +table('order_action') + ->idVar('id_order_action') + ->load($id); + } +} \ No newline at end of file diff --git a/include/views/default/quick/layout/html.phtml b/include/views/default/quick/layout/html.phtml index 387fce341..4ebaebde1 100644 --- a/include/views/default/quick/layout/html.phtml +++ b/include/views/default/quick/layout/html.phtml @@ -47,19 +47,17 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, } .control { - background-image: -moz-linear-gradient( 90deg, rgb(203,203,203) 0%, rgb(232,232,232) 100%); background-image: -webkit-linear-gradient( 90deg, rgb(203,203,203) 0%, rgb(232,232,232) 100%); background-image: -ms-linear-gradient( 90deg, rgb(203,203,203) 0%, rgb(232,232,232) 100%); box-shadow: 0px 1px 0px 0px rgb( 150, 150, 150 ); - - } .control .buttons { display: table-cell; vertical-align: middle; text-align: right; + width: 100%; } .content { @@ -145,7 +143,7 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, .order-comment { background: #00957f; color: #fff; - padding: .7em .9em .5em 1em; + padding: .9em .9em .8em 1em; border-radius: 4px; margin-bottom: .8em; } @@ -286,7 +284,8 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, font-size: 1.3em; display: table-cell; vertical-align: middle; - text-shadow:1px 1px 0px #fff + text-shadow:1px 1px 0px #fff; + white-space:nowrap; } .button { @@ -299,7 +298,6 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, border: 1px solid rgba(0,0,0,.2); box-shadow: 0px 0px 3px rgba(0,0,0,.3); text-shadow: 1px 1px 0 rgba(0,0,0,.4); - -webkit-transition: all .09s ease; } .button:active { @@ -316,19 +314,74 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, background: #dc534b; border-radius: 4px; } + + .order-status-delivered .status-control:not(.control-delivered) { + display: none !important; + } + + .order-status-pickedup .status-control:not(.control-pickedup) { + display: none !important; + } + + .order-status-rejected .status-control:not(.control-rejected) { + display: none !important; + } + + .order-status-accepted .status-control:not(.control-accepted) { + display: none !important; + } + + .order-status-accepted-other .status-control:not(.control-accepted-other) { + display: none !important; + } + + .order-status-none .status-control:not(.control-accept-reject) { + display: none !important; + } + +
-
-
Accept this order?
+
+
+
Accept this order?
- - + +
+
+
You rejected.
+
+ +
+
+
+
You accepted.
+
+ + +
+
+
+ This order has been accepted by order->deliveryStatus('accepted') ? $this->order->deliveryStatus('accepted')->name : ''?>. +
+
+
You picked up.
+
+ +
+
+
+ This order has been picked up by order->deliveryStatus('pickedup') ? $this->order->deliveryStatus('pickedup')->name : ''?>. +
+
+ This order has been delivered by order->deliveryStatus('delivered') ? $this->order->deliveryStatus('delivered')->name : ''?>. +
@@ -342,4 +395,58 @@ input[type="submit"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, + + \ No newline at end of file diff --git a/include/views/default/quick/order/index.phtml b/include/views/default/quick/order/index.phtml index 8d1aee0fd..e7a9f26f8 100644 --- a/include/views/default/quick/order/index.phtml +++ b/include/views/default/quick/order/index.phtml @@ -53,25 +53,25 @@

order->notes)?>

- + +
- -
order->address))?>&markers=color:red%7Clabel:B%7Corder->restaurant()->lat?>,order->restaurant()->lon?>&sensor=false&visual_refresh=true');">
+
order->address))?>&markers=color:red%7Clabel:B%7Corder->restaurant()->loc_lat?>,order->restaurant()->loc_long?>&sensor=false&visual_refresh=true');">

@@ -217,15 +217,5 @@ - - - - - - - - - - -
\ No newline at end of file +