API Shopping Order backend

This commit is contained in:
Kevin Adametz 2022-07-15 21:21:56 +02:00
parent 7a040c3e19
commit d01b4bd560
24 changed files with 1584 additions and 34 deletions

View file

@ -26,7 +26,7 @@ class ShoppingUserController extends Controller
protected $successStatus = 200;
protected $member_id = 3; //thomas.krummel@gmail.com
protected $member_id = 3; //service@aloe-vera.bio
/**
@ -363,12 +363,12 @@ class ShoppingUserController extends Controller
$wp_advertising = isset($request->wp_advertising) ? $request->wp_advertising : '';
$wp_incentives = isset($request->wp_incentives) ? $request->wp_incentives : '';
$wp_notice = [
$api_notice = [
'wp_advertising' => $wp_advertising,
'wp_incentives' => $wp_incentives,
];
$wp_order = $this->prepareOrder($wp_order, $shopping_user, $wp_invoice_path, $wp_notice);
$wp_order = $this->prepareOrder($wp_order, $shopping_user, $wp_invoice_path, $api_notice);
if ($wp_order){
$user = $this->prepareForShow($shopping_user);
@ -573,7 +573,7 @@ class ShoppingUserController extends Controller
return $ret;
}
private function prepareOrder($wp_shopping_order, $shopping_user, $wp_invoice_path, $wp_notice){
private function prepareOrder($wp_shopping_order, $shopping_user, $wp_invoice_path, $api_notice){
Yard::instance('shopping')->destroy();
$ret = [];
@ -593,7 +593,8 @@ class ShoppingUserController extends Controller
if ($order->price != ($product->price * 100)) {
$error[] = "different price: " . ($product->price * 100);
}
Yard::instance('shopping')->add($product->id, $product->name, (int) $order->qty, $product->price, false, false, ['image' => [], 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission]);
$cartItem = Yard::instance('shopping')->add($product->id, $product->name, (int) $order->qty, $product->price, false, false, ['image' => [], 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission]);
Yard::setTax($cartItem->rowId, $product->getTaxWith());
}
}
$order->message = $error;
@ -604,7 +605,7 @@ class ShoppingUserController extends Controller
if($ShippingCountry){
Yard::instance('shopping')->setShippingCountryWithPrice($ShippingCountry->id);
}
$shopping_order = $this->makeShoppingOrder($shopping_user, $wp_invoice_path, $wp_notice);
$shopping_order = $this->makeShoppingOrder($shopping_user, $wp_invoice_path, $api_notice);
$this->orderStatusSendMail($shopping_order);
$shopping_user->shopping_order = $shopping_order;
@ -613,7 +614,7 @@ class ShoppingUserController extends Controller
return $ret;
}
private function makeShoppingOrder($shopping_user, $wp_invoice_path, $wp_notice){
private function makeShoppingOrder($shopping_user, $wp_invoice_path, $api_notice){
$data = [
'shopping_user_id' => $shopping_user->id,
@ -634,7 +635,8 @@ class ShoppingUserController extends Controller
'paid' => true,
'txaction' => 'extern',
'wp_invoice_path' => $wp_invoice_path,
'wp_notice' => $wp_notice,
'api_notice' => $api_notice,
'api_status' => 0,
'mode' => $shopping_user->mode,
];
$shopping_order = $shopping_user->shopping_order;

View file

@ -53,11 +53,27 @@ class ModalController extends Controller
$ret = view("admin.modal.show_product", compact('product', 'data'))->render();
}
if($data['action'] === 'user-order-show-product'){
$product = Product::find($data['id']); //current user form order
$ret = view("admin.modal.show_product", compact('product', 'data'))->render();
}
if($data['action'] === 'shop-user-order-detail'){
$user = \Auth::user();
$shopping_order = ShoppingOrder::findOrFail($data['id']);
if($shopping_order->member_id !== $user->id){
abort(404);
}
$isAdmin = false;
$ret = view("user.shop.sales.modal_api_order_detail", compact('shopping_order', 'isAdmin', 'data'))->render();
}
if($data['action'] === 'user-order-my-delivery-show'){
$user = \Auth::user();
$ret = view("admin.modal.show_user_customers", compact('user', 'data'))->render();
}
if($data['action'] === 'user-order-my-delivery-add'){
$user = \Auth::user();

View file

@ -0,0 +1,180 @@
<?php
namespace App\Http\Controllers\User;
use Request;
use App\User;
use App\Services\Payment;
use App\Models\ShoppingOrder;
use App\Services\UserService;
use App\Models\ShippingCountry;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ShopApiRepository;
class ShopApiController extends Controller
{
private $api_action = [0 => 'bitte wählen', 'order' => 'markierte bestellen', 'remove' => 'markierte entfernen', 'reset' => 'markierte zurücksetzen/offen'];
private $filter_show = [10 => 'alle anzeigen', 1 => 'offen', 2 => 'bestellt', 5 => 'entfernt'];
protected $shopApiRepository;
public function __construct(ShopApiRepository $shopApiRepository)
{
$this->middleware('active.shop');
$this->shopApiRepository = $shopApiRepository;
}
public function orders()
{
$this->setFilterVars();
$data = [
'api_action' => $this->api_action,
'filter_show' => $this->filter_show,
];
return view('user.shop.sales.api_orders', $data);
}
public function action(){
$data = Request::all();
if(isset($data['user_shop_api_orders_action'])){
switch($data['user_shop_api_orders_action']){
case 'order':
$shopApiOrderCart = $this->shopApiRepository->order($data);
/*$user = User::find(Auth::user()->id);
$shipping_country = ShippingCountry::whereCountryId($user->account->country_id)->first();
UserService::checkUserTaxShippingCountry($user, $shipping_country->id);*/
//card_info = UserService::getYardInfo();
return view('user.shop.sales.api_order_list', compact('shopApiOrderCart', 'data'));
break;
case 'remove':
$this->shopApiRepository->remove($data);
break;
case 'reset':
$this->shopApiRepository->reset($data);
break;
}
}
return redirect(route('user_shop_api_orders'));
}
public function checkout(){
$data = Request::all();
$shopApiOrderCart = $this->shopApiRepository->checkout($data);
//TODO store in ShopApiOrderCart
//TO checkout
dd($shopApiOrderCart);
return redirect(route('user_shop_api_orders'));
}
private function setFilterVars(){
if(!session('user_shop_api_orders_filter')){
session(['user_shop_api_orders_filter' => 1]);
}
if(Request::get('user_shop_api_orders_filter')){
session(['user_shop_api_orders_filter' => Request::get('user_shop_api_orders_filter')]);
}
}
private function initSearch($archive = false, $request = true)
{
$this->setFilterVars();
$user = User::find(\Auth::user()->id);
$query = ShoppingOrder::with('shopping_user')->select('shopping_orders.*')
->where('shopping_orders.member_id', $user->id)
->where('shopping_orders.payment_for', 7); //7 payment for extern
if(Request::get('user_shop_api_orders_filter')){
if(Request::get('user_shop_api_orders_filter') < 10){
if(Request::get('user_shop_api_orders_filter') == 1){
$query->where(function($query) {
return $query->where('shopping_orders.api_status', 0)
->orWhere('shopping_orders.api_status', 1)
->orWhereNull('shopping_orders.api_status');
});
}else{
$query->where('shopping_orders.api_status', Request::get('user_shop_api_orders_filter'));
}
}
}
return $query;
}
public function ordersDatatable(){
$query = $this->initSearch();
return \DataTables::eloquent($query)
->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
return '<button type="button" class="btn icon-btn btn-sm btn-secondary" data-toggle="modal" data-target="#modals-load-content"
data-id="'.$ShoppingOrder->id.'"
data-action="shop-user-order-detail"
data-back=""
data-modal="modal-xl"
data-init_from="user"
data-route="'.route('modal_load').'"><span class="far fa-eye"></span></button>';
})
->addColumn('api_status', function (ShoppingOrder $ShoppingOrder) {
return '<span class="badge badge-pill badge-'.$ShoppingOrder->getAPIStatusColor().'">'.$ShoppingOrder->getAPIStatusType().'</span>';
})
->addColumn('created_at', function (ShoppingOrder $ShoppingOrder) {
return $ShoppingOrder->created_at->format("d.m.Y");
})
->addColumn('txaction', function (ShoppingOrder $ShoppingOrder) {
return Payment::getShoppingOrderBadge($ShoppingOrder);
})
->addColumn('api_action', function (ShoppingOrder $ShoppingOrder) {
return '<label class="custom-control custom-checkbox m-0">
<input type="checkbox" class="custom-control-input" name="api_action_list['.$ShoppingOrder->id.']" id="api_action_list_'.$ShoppingOrder->id.'">
<span class="custom-control-label"></span>
</label>';
})
->addColumn('total_shipping', function (ShoppingOrder $ShoppingOrder) {
return '<span class="no-line-break">'.$ShoppingOrder->getFormattedTotalShipping()." €</span>";
})
->addColumn('orders', function (ShoppingOrder $ShoppingOrder) {
return $ShoppingOrder->shopping_user ? $ShoppingOrder->shopping_user->orders : '';
})
->addColumn('user_shop_id', function (ShoppingOrder $ShoppingOrder) {
return $ShoppingOrder->user_shop ? '<a href="'.$ShoppingOrder->user_shop->getSubdomain(false).'" target="_blank">'.$ShoppingOrder->user_shop->getSubdomain(false).'</span>' : '';
})
->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) {
return '<span class="badge badge-pill badge-'.$ShoppingOrder->getPaymentForColor().'">'.$ShoppingOrder->getPaymentForType().'</span>';
})
->addColumn('shipped', function (ShoppingOrder $ShoppingOrder) {
return '<span class="badge badge-pill badge-'.$ShoppingOrder->getShippedColor().'">'.$ShoppingOrder->getShippedType().'</span>
';
})
->orderColumn('payment_for', 'payment_for $1')
->orderColumn('id', 'id $1')
->orderColumn('api_action', 'id $1')
->orderColumn('txaction', 'txaction $1')
->orderColumn('user_shop_id', 'user_shop_id $1')
->orderColumn('total_shipping', 'total_shipping $1')
->rawColumns(['id', 'api_status', 'txaction', 'user_shop_id', 'api_action', 'shipped', 'total_shipping', 'payment_for'])
->make(true);
}
}

View file

@ -40,7 +40,7 @@ class ShopSalesController extends Controller
public function ordersDatatable(){
$user = User::find(\Auth::user()->id);
$query = ShoppingOrder::with('shopping_user')->select('shopping_orders.*')->where('member_id', $user->id);
$query = ShoppingOrder::with('shopping_user')->select('shopping_orders.*')->where('shopping_orders.member_id', $user->id);
return \DataTables::eloquent($query)
->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
@ -64,19 +64,21 @@ class ShopSalesController extends Controller
->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) {
return '<span class="badge badge-pill badge-'.$ShoppingOrder->getPaymentForColor().'">'.$ShoppingOrder->getPaymentForType().'</span>';
})
->addColumn('shipped', function (ShoppingOrder $ShoppingOrder) {
return '<span class="badge badge-pill badge-'.$ShoppingOrder->getShippedColor().'">'.$ShoppingOrder->getShippedType().'</span>
';
})
->addColumn('invoice', function (ShoppingOrder $ShoppingOrder) {
return $ShoppingOrder->isInvoice() ? '<span class="no-line-break"><a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'download']).'" class="btn btn-primary btn-xs"><i class="fa fa-download"></i></a>
<a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'stream']).'" target="_blank" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i></a></span>' : '-';
})
->orderColumn('payment_for', 'payment_for $1')
->orderColumn('id', 'id $1')
->orderColumn('txaction', 'txaction $1')
->orderColumn('user_shop_id', 'user_shop_id $1')
->orderColumn('total_shipping', 'total_shipping $1')
->rawColumns(['id', 'txaction', 'user_shop_id', 'total_shipping', 'invoice', 'payment_for'])
->rawColumns(['id', 'txaction', 'user_shop_id', 'total_shipping', 'invoice', 'shipped', 'payment_for'])
->make(true);
}

View file

@ -81,7 +81,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTracking($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWpInvoicePath($value)
* @property int|null $homeparty_id
* @property array|null $wp_notice
* @property array|null $api_notice
* @property int|null $api_status
* @property-read \App\Models\Homeparty|null $homeparty
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereHomepartyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereWpNotice($value)
@ -119,14 +120,15 @@ class ShoppingOrder extends Model
'paid',
'txaction',
'wp_invoice_path',
'wp_notice',
'api_notice',
'api_status',
'mode',
'shipped',
'tracking'
];
protected $casts = [
'wp_notice' => 'array',
'api_notice' => 'array',
'tax_split' => 'array',
];
@ -146,6 +148,19 @@ class ShoppingOrder extends Model
10 => 'cancel'
];
public static $apiStatusTypes = [
0 => 'offen',
1 => 'offen / pre',
2 => 'bestellt',
5 => 'entfernt',
];
public static $apiStatusColors = [
0 => 'warning',
1 => 'warning',
2 => 'success',
5 => 'danger',
];
public static $shippedColors = [
0 => 'warning',
1 => 'info',
@ -295,6 +310,15 @@ class ShoppingOrder extends Model
return isset(self::$shippedColors[$this->shipped]) ? self::$shippedColors[$this->shipped] : "default";
}
public function getAPIStatusType(){
return isset(self::$apiStatusTypes[$this->api_status]) ? self::$apiStatusTypes[$this->api_status] : "offen";
}
public function getAPIStatusColor(){
return isset(self::$apiStatusColors[$this->api_status]) ? self::$apiStatusColors[$this->api_status] : "warning";
}
public function getFormattedTotal()
{
return formatNumber($this->attributes['total']);

View file

@ -0,0 +1,83 @@
<?php
namespace App\Repositories;
use App\Models\ShoppingOrder;
use App\Services\ShopApiOrderCart;
class ShopApiRepository extends BaseRepository {
private $shopApiOrderCart;
public function __construct()
{
//$this->model = $model;
}
public function order($data)
{
$this->shopApiOrderCart = new ShopApiOrderCart();
$this->performOrderActionList($data, 0);
return $this->shopApiOrderCart;
}
public function remove($data){
$this->performActionList($data, 5);
return true;
}
public function reset($data){
$this->performActionList($data, 0);
return true;
}
public function checkout($data){
$this->shopApiOrderCart = new ShopApiOrderCart();
$this->performOrderCheckoutList($data, 1);
return $this->shopApiOrderCart;
}
private function performActionList($data, $status){
if(isset($data['api_action_list'])){
foreach($data['api_action_list'] as $orderItemId=>$v){
$ShoppingOrder = ShoppingOrder::findOrFail($orderItemId);
$ShoppingOrder->api_status = $status;
$ShoppingOrder->save();
}
}
}
private function performOrderActionList($data, $status){
if(isset($data['api_action_list'])){
foreach($data['api_action_list'] as $orderItemId=>$v){
$ShoppingOrder = ShoppingOrder::findOrFail($orderItemId);
$this->shopApiOrderCart->add($ShoppingOrder);
$ShoppingOrder->api_status = $status; //<- no status change is the show list
$ShoppingOrder->save();
}
$this->shopApiOrderCart->calculate();
return $this->shopApiOrderCart;
}
}
private function performOrderCheckoutList($data, $status){
if(isset($data['api_action_list'])){
foreach($data['api_action_list'] as $k=>$orderItemId){
$ShoppingOrder = ShoppingOrder::findOrFail($orderItemId);
$this->shopApiOrderCart->add($ShoppingOrder);
$ShoppingOrder->api_status = $status;
$ShoppingOrder->save();
}
$this->shopApiOrderCart->calculate();
$this->shopApiOrderCart->store();
return $this->shopApiOrderCart;
}
}
}

View file

@ -0,0 +1,191 @@
<?php
namespace App\Services;
use stdClass;
use App\Models\ShoppingOrder;
class ShopApiOrderCart
{
public $shipping;
public $shipping_net;
public $shipping_tax;
public $points;
public $points_total; //check
public $price_total_net;
public $price_total;
public $tax_total;
public $tax_split;
public $qty_total;
public $orders = [];
public $shop_items = [];
public function __construct()
{
$this->shipping = 0;
$this->shipping_net = 0;
$this->shipping_tax = 0;
$this->points_total = 0;
$this->price_total_net = 0;
$this->price_total = 0;
$this->tax_total = 0;
$this->tax_split = [];
$this->qty_total = 0;
$this->shop_items = [];
}
public function add(ShoppingOrder $shopping_order){
$order = new stdClass();
$order->order_id = $shopping_order->id;
$order->wp_order_number = $shopping_order->shopping_user->wp_order_number;
$order->billing_address = $this->setOrderAdress('billing', $shopping_order->shopping_user);
$order->shipping_address = $this->setOrderAdress('shipping', $shopping_order->shopping_user);
$order->shopping_order = $shopping_order;
$this->shipping += $shopping_order->shipping;
$this->shipping_net += $shopping_order->shipping_net;
foreach ($shopping_order->shopping_order_items as $item){
$tax_rate = intval($item->product->tax);
$user_price_net = $this->calcuPriceWith($item->price, $tax_rate, $item->discount);
$user_price_net_qty = round($user_price_net * $item->qty, 2);
$user_tax = $this->calcuTaxWith($user_price_net, $tax_rate);
$user_tax_qty = round($user_tax * $item->qty, 2);
$shop_item_id = $item->product->id.'-'.$item->price;
//set to item
if(isset($this->shop_items[$shop_item_id])){
$shop_item = $this->shop_items[$shop_item_id];
$shop_item->user_price_total_net += $user_price_net_qty;
$shop_item->user_tax_total += $user_tax_qty;
$shop_item->points_total += ($item->points * $item->qty);
$shop_item->qty += $item->qty;
}else{
$shop_item = new stdClass();
$shop_item->pid = $item->product->id;
$shop_item->article = $item->product->wp_number;
$shop_item->number = $item->product->number;
$shop_item->name = $item->product->name;
$shop_item->qty = $item->qty;
$shop_item->tax_rate = $tax_rate;
$shop_item->points = $item->points;
$shop_item->user_price_net = $user_price_net;
$shop_item->user_price_total_net = $user_price_net_qty;
$shop_item->user_tax = $user_tax;
$shop_item->user_tax_total = $user_tax_qty;
$shop_item->points_total = ($item->points * $item->qty);
}
//only for tax split / tax and price on calculate function
$tax = isset($this->tax_split[$tax_rate]) ? $this->tax_split[$tax_rate] : 0;
$tax += $user_tax_qty;
$this->tax_split[$tax_rate] = $tax;
$this->tax_total += $user_tax_qty;
$this->price_total_net += $user_price_net_qty;
$this->points_total += ($item->points * $item->qty);
$this->qty_total += $item->qty;
$this->shop_items[$shop_item_id] = $shop_item;
}
$this->orders[] = $order;
}
public function calculate(){
$this->shipping_tax = round($this->shipping - $this->shipping_net, 2);
$this->tax_total += $this->shipping_tax;
//add shipping tax to split
$tax = isset($this->tax_split[config('app.shipping_tax')]) ? $this->tax_split[config('app.shipping_tax')] : 0;
$tax += $this->shipping_tax;
$this->tax_split[config('app.shipping_tax')] = $tax;
$this->price_total_net += $this->shipping_net;
$this->price_total = round($this->tax_total + $this->price_total_net, 2);
//$this->price_total += round($user_price * $item->qty, 2);
}
public function store(){
//TODO
}
//price brutto calu with
private function calcuPriceWith($price, $tax_rate = null, $discount = null){
$tax_dec = ($tax_rate + 100) / 100;
$price / $tax_dec;
$margin = (($discount -100)*-1) / 100;
$price = $price * $margin;
return round($price, 2);
}
private function calcuTaxWith($price, $tax_rate = null){
$tax_dec = ($tax_rate + 100) / 100;
$tax = ($price * $tax_dec) - $price;
return round($tax, 2);
}
private function setOrderAdress($from, $shopping_user){
$ret = "";
if($from === 'billing'){
$ret .= $shopping_user->billing_company ? 'Firma: '.$shopping_user->billing_company.' | ' : '';
$ret .= \App\Services\HTMLHelper::getSalutationLang($shopping_user->billing_salutation).' ';
$ret .= $shopping_user->billing_firstname.' ';
$ret .= $shopping_user->billing_lastname.' | ';
$ret .= $shopping_user->billing_address.' | ';
$ret .= $shopping_user->billing_zipcode.' ';
$ret .= $shopping_user->billing_city.' | ';
$ret .= $shopping_user->billing_country->getLocated().' | ';
$ret .= $shopping_user->billing_email;
}
if($from === 'shipping'){
if($shopping_user->same_as_billing == 1){
return 'Lieferadresse ist gleich Rechnungsadresse';
}
$ret .= $shopping_user->shipping_company ? 'Firma: '.$shopping_user->shipping_company.' | ' : '';
$ret .= \App\Services\HTMLHelper::getSalutationLang($shopping_user->shipping_salutation).' ';
$ret .= $shopping_user->shipping_firstname.' ';
$ret .= $shopping_user->shipping_lastname.' | ';
$ret .= $shopping_user->shipping_address.' | ';
$ret .= $shopping_user->shipping_zipcode.' ';
$ret .= $shopping_user->shipping_city.' | ';
$ret .= $shopping_user->shipping_country->getLocated().' | ';
}
return $ret;
}
public function getTotalTax()
{
return $this->tax_total;
}
public function getTotalPriceNetto()
{
return $this->price_total_net;
}
public function getTotalPrice()
{
return $this->price_total;
}
public function getTaxSplit()
{
return 0;
}
}

View file

@ -261,6 +261,15 @@ class User extends Authenticatable
return false;
}
public function isUserHasApi()
{
if($this->id === 3){
return true;
}
return false;
}
/**
* @return bool
*/