Skip to content

Commit

Permalink
messy state here but option editing works and taxes are recalculated.…
Browse files Browse the repository at this point in the history
… moving off since we're running out of time
  • Loading branch information
CupNoodles committed Aug 18, 2021
1 parent bfa8ccc commit aae8f33
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 42 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Order Menu Edit

## Dev Notes

This extension is written specifically for use at osakana.nyc, and therefore is meant to be used in conjunction with the cupnoodles/pricebybweight extentions, and likewise is rather incomplete and shouldn't be considered a general purpose extention. For exmaple, it currently only supports option editing for the types 'checkbox' and 'radio', since those are the only ones used at the main store. The code itself can be used as a proof of concept for others, but it's not recommended to install this on any other stores as-is.

Order Menu Edit has the following features:

- adds an 'Actual Amount' colunm to order_menus, to log differenced between and ordered amount and a delivered amount
Expand All @@ -9,11 +13,6 @@ Order Menu Edit has the following features:
- add/delete/edit actions are logged in the order status history
- save button is moved to the bottom of the admin order form (this is my personal preference)

What needs to be added still:

- a modal formwidget to edit menu lines would be much cleaner, and allow for editing of order line comments as well as option values. Until that's in place, menu options cannot be edited through the admin.
- add/delete should be handled by modal UI to clean up the admin page

# Usage

Installing this plugin should cause the order edit link in the Oders list view to link to cupnoodles/orders/edit as opposed to admin/orders/edit.
Expand Down
9 changes: 9 additions & 0 deletions assets/js/ordermenuedit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$(document).ready(function(){
$('.item-edit-icon').on('click', function(){
$('#item-edit-'+$(this).data('order-menu-id')).toggle();
})
$(document).ajaxSuccess(function(event, request, settings){
if(request.handler == 'onSave'){
location.reload();
} });
});
104 changes: 97 additions & 7 deletions controllers/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace CupNoodles\OrderMenuEdit\Controllers;

use CupNoodles\OrderMenuEdit\Models\Order_Menus;
use CupNoodles\OrderMenuEdit\Models\Order_Menu_Options;
use Admin\Models\Menu_item_option_values_model;
use Admin\Models\Menu_item_options_model;

use Admin\Controllers\Orders as BaseOrders;

Expand All @@ -18,6 +21,7 @@

class Orders extends BaseOrders{


public $formConfig = [
'name' => 'lang:admin::lang.orders.text_form_name',
'model' => 'Admin\Models\Orders_model',
Expand All @@ -37,6 +41,11 @@ class Orders extends BaseOrders{
'configFile' => 'orders_model',
];

public function edit($context, $recordId)
{
parent::edit($context, $recordId);
$this->addJS('extensions/cupnoodles/ordermenuedit/assets/js/ordermenuedit.js', 'cupnoodles-ordermenuedit');
}

public function addMenuItemFromAdmin($order_id, $menu_id, $qty, $order_line_ready){

Expand Down Expand Up @@ -83,14 +92,37 @@ public function recalcTotalsFromAdmin($order_id){
->update(['value' => $subtotal]);

$subtotals = DB::table('order_totals')
->select('value')
->select('value', 'code')
->where('order_id', $order_id)
->where('code', '!=', 'subtotal')
->where('code', '!=', 'total')
->orderBy('priority')
->get();

foreach($subtotals as $sub){
foreach($subtotals as $ix=>$sub){

// this sucks a lot but it's a placeholder until we can re-write ordermenuedit to use Cart object instead of manually sifting through Order object.
// doesn't matter at all unless you've got TaxClasses extension installed

if($sub->code == 'variableTax'){
$taxAmount = 0;

$tax_rates = \CupNoodles\TaxClasses\Models\TaxClasses::all();
$menus = Order_Menus::where('order_id', $order_id)->with(['order', 'menus'])->get();




foreach($menus as $ix=>$menu){
if($menu->menus->tax_class_id && isset($menu->menus->tax_classes->rate)){
$taxAmount += $menu->subtotal * ($menu->menus->tax_classes->rate / 100);
}
}
DB::table('order_totals')
->where('order_id', $order_id)
->where('code', 'variableTax')
->update(['value' => $taxAmount]);
}
$subtotal += $sub->value;
}

Expand Down Expand Up @@ -121,7 +153,8 @@ public function edit_onSave($context, $recordId){
}
else{
$model = Order_Menus::where('order_menu_id', $order_menu_id)->first();

$menus_model = Menus_model::where('menu_id', $model->menu_id)->first();

if(isset($vals['delete']) && $vals['delete'] == 'delete'){

$status_model = $this->formFindModelObject($recordId);
Expand All @@ -142,16 +175,68 @@ public function edit_onSave($context, $recordId){
$vals['subtotal'] = ($vals['actual_amt'] ? $vals['actual_amt'] : $model->quantity ) * $model->price;

$update_str = '';

$options = Menu_item_options_model::where('menu_id', $model->menu_id)->get();
foreach($options as $menu_option){
if(isset($vals['menu_options']) && isset($vals['menu_options'][$menu_option->menu_option_id]) ){ // update or insert
$price_diff = 0;
foreach($vals['menu_options'] as $order_menu_option_id=>$option_values_str){

$option_values = json_decode($option_values_str, false);

if(is_object($option_values)){

if($option_values->menu_option_value_id == 0 ){

$option_value_model = Menu_item_option_values_model::where([
['menu_option_id', '=', $order_menu_option_id],
['option_value_id', '=', $option_values->option_value_id]
])->first();
$option_values->menu_option_value_id = $option_value_model->menu_option_value_id;
}
// build options from [post data]
$option_data =
['order_id' => $recordId,
'menu_id' => $model->menu_id,
'menu_option_value_id' => $option_values->menu_option_value_id,
'order_option_name' => $option_values->order_option_name,
'order_option_price' => $option_values->order_option_price
];
if(isset($vals['actual_amt']) && $vals['actual_amt'] != '') {
$option_data['quantity'] = $vals['actual_amt'];
$price_qty = $vals['actual_amt'];
}
else{
$price_qty = $model->quantity;
}
Order_Menu_Options::updateOrCreate(['order_menu_id' => $order_menu_id,'order_menu_option_id' => $order_menu_option_id], $option_data);
if($option_values->order_option_price != 0){
$price_diff += $option_values->order_option_price;
}
}

}
}
else{ // delete this line (checkbox unchecked)
$order_menu_options = Order_Menu_Options::where(['order_menu_id' => $order_menu_id,'order_menu_option_id' => $menu_option->menu_option_id]);
$order_menu_options->delete();
}
}
foreach($vals as $attr=>$value){
if($model->{$attr} != $value){
$update_str .= ' ' . $attr . ' : ' . $value;
if($attr != 'menu_options'){
if($model->{$attr} != $value){
$update_str .= ' ' . $attr . ' : ' . $value;
}
$model->{$attr} = $value;
}
$model->{$attr} = $value;
}
$model->price = $menus_model->menu_price + $price_diff;
$model->subtotal = $model->price * $price_qty;


if($update_str != ''){
$status_model = $this->formFindModelObject($recordId);
$status_model->updateOrderStatus(null, ['comment' => 'Admin updated ' . $model->name . ' ' . $update_str]);
$status_model->updateOrderStatus(null, ['comment' => 'Updated ' . $model->name . ' ' . $update_str]);
}

DB::transaction(function () use ($model) {
Expand All @@ -173,6 +258,7 @@ public function renderForm($options = []){
$r = parent::renderForm($options);
$dom = new DOMDocument();
$r = str_replace(' & ', ' & ', $r);

$dom->loadHTML($r);

$divs = $dom->getElementsByTagName('div')->item(0);
Expand All @@ -192,4 +278,8 @@ public function getMenus(){
return Menus_model::isEnabled()->get();
}

public function orderMenuOptionsQuery()
{
return DB::table('order_menu_options');
}
}
29 changes: 29 additions & 0 deletions models/Order_Menu_Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace CupNoodles\OrderMenuEdit\Models;

use Model;

class Order_Menu_Options extends Model{

protected $table = 'order_menu_options';

protected $primaryKey = 'order_option_id';

protected $fillable = [
'order_id',
'menu_id',
'order_option_name',
'order_option_price',
'order_menu_id',
'order_menu_option_id',
'menu_option_value_id',
'quantity'
];
public $relation = [
'belongsTo' => [
'order' => ['CupNoodles\OrderMenuEdit\Models\Order_Menus'],
],
];

}
1 change: 1 addition & 0 deletions models/Order_Menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Order_Menus extends Model{
public $relation = [
'belongsTo' => [
'order' => ['Admin\Models\Orders_model'],
'menus' => ['Admin\Models\Menus_model', 'foreignKey' => 'menu_id', 'otherKey' => 'menu_id']
],
];

Expand Down
Loading

0 comments on commit aae8f33

Please sign in to comment.