|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | +use App\GoodTypes; |
| 5 | +use App\Http\Traits\FindingGoods; |
| 6 | +use App\TradeZones; |
| 7 | +use App\Transactions; |
| 8 | +use \Session; |
| 9 | + |
| 10 | +class ProfitLoss extends Controller |
| 11 | +{ |
| 12 | + use FindingGoods; |
| 13 | + |
| 14 | + protected $data; |
| 15 | + |
| 16 | + public function index() { |
| 17 | + $title = "Stores P/L"; |
| 18 | + $storeType = "personal"; |
| 19 | + $stores = Session::get('stores'); |
| 20 | + $globalAverages = $this->getGlobalAveragesForGoods('server',$stores); |
| 21 | + |
| 22 | + |
| 23 | + return view('stores.pl.personal', compact('stores', 'storeType', 'title', 'globalAverages')); |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 29 | + */ |
| 30 | + public function worldIndex() { |
| 31 | + $title = "Stores P/L"; |
| 32 | + $storeType = "world"; |
| 33 | + $stores = Session::get('stores'); |
| 34 | + $globalAverages = $this->getGlobalAveragesForGoods('server',$stores); |
| 35 | + |
| 36 | + return view('stores.pl.world', compact('stores','storeType', 'title', 'globalAverages')); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 42 | + */ |
| 43 | + public function serverIndex() { |
| 44 | + $title = "Stores P/L"; |
| 45 | + $storeType = "server"; |
| 46 | + $stores = Session::get('stores'); |
| 47 | + $globalAverages = $this->getGlobalAveragesForGoods('server',$stores); |
| 48 | + |
| 49 | + return view('stores.pl.server', compact('stores','storeType', 'title', 'globalAverages')); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * @param $levelToSearch |
| 55 | + * @param $stores |
| 56 | + * |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + private function getGlobalAveragesForGoods($levelToSearch,$stores) { |
| 60 | + //$searchUsername = $currentUser->server_username ?? $currentUser->username; |
| 61 | + //todo: to actually limit to a personal/world/server levels we will have to refactor how we get the data |
| 62 | + $globalAverages = []; |
| 63 | + foreach($stores as $storeData) { |
| 64 | + foreach($storeData['Averages'] as $goodType => $goodTypeData) { |
| 65 | + foreach($goodTypeData as $transactionType => $transactionTypeData) { |
| 66 | + foreach($transactionTypeData as $good => $goodData) { |
| 67 | + $globalAverages[$goodType][$transactionType][$good]['price'] = (empty($globalAverages[$goodType][$transactionType][$good]['price'])) ? $goodData['Price'] : $globalAverages[$goodType][$transactionType][$good]['price']+$goodData['Price']; |
| 68 | + $globalAverages[$goodType][$transactionType][$good]['count'] = (empty($globalAverages[$goodType][$transactionType][$good]['count'])) ? 1 : $globalAverages[$goodType][$transactionType][$good]['count']+1; |
| 69 | + $globalAverages[$goodType][$transactionType][$good]['average'] = $globalAverages[$goodType][$transactionType][$good]['price']/$globalAverages[$goodType][$transactionType][$good]['count']; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $globalAverages; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * note: this gets all the transactions for the stores and returns it with the ids converted to titles. |
| 81 | + * @return object |
| 82 | + */ |
| 83 | + public function getTransactionsUsingTitles() { |
| 84 | + $transactions = new Transactions(); |
| 85 | + $this->data = []; |
| 86 | + $transactions->chunk(400, function ($transactions) { |
| 87 | + foreach($transactions as $transaction) { |
| 88 | + $tradeZone = TradeZones::find($transaction->trade_zone_id); |
| 89 | + $goodType = GoodTypes::find($transaction->good_type_id); |
| 90 | + $item =$this->getGoodFromGoodTypeAndGoodId($goodType, $transaction->good_id); |
| 91 | + $transactionType = $this->getTransactionTypeFromId($transaction->transaction_type_id); |
| 92 | + $gridName = $tradeZone->title; |
| 93 | + $owner = $transaction->owner; |
| 94 | + $gps = $tradeZone->gps; |
| 95 | + $price = $transaction->value; |
| 96 | + $amount = $transaction->amount; |
| 97 | + $transactionType = ($transactionType->title === 'buy') ? 'Orders' : 'Offers'; |
| 98 | + |
| 99 | + if($tradeZone->count() > 0 && $goodType->count() > 0 && $item->count() > 0) { |
| 100 | + if (empty($this->data[$gridName])) { |
| 101 | + $this->data[$gridName]['Info']['Owner'] = $owner; |
| 102 | + $this->data[$gridName]['Info']['GPS'] = $gps; |
| 103 | + $this->data[$gridName]['Totals'] = []; |
| 104 | + } |
| 105 | + if (empty($this->data[$gridName]['Data'][$goodType->title])) { |
| 106 | + $this->data[$gridName]['Data'][$goodType->title]['Offers'] = []; |
| 107 | + $this->data[$gridName]['Data'][$goodType->title]['Orders'] = []; |
| 108 | + } |
| 109 | + if (empty($this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title])) { |
| 110 | + $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title] = [ |
| 111 | + 'Amount' => 0, |
| 112 | + 'Price' => 0, |
| 113 | + 'MinPrice' => 0, |
| 114 | + 'MaxPrice' => 0, |
| 115 | + ]; |
| 116 | + $this->data[$gridName]['Averages'][$goodType->title][$transactionType][$item->title]['Price'] = 0; |
| 117 | + } |
| 118 | + |
| 119 | + $this->data[$gridName]['Data'][$goodType->title][$transactionType][$item->title]['Transactions'][] = [ |
| 120 | + 'Amount' => $amount, |
| 121 | + 'Price' => $price |
| 122 | + ]; |
| 123 | + $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Amount'] += $amount; |
| 124 | + $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Price'] += $amount * $price; |
| 125 | + if (($this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['MinPrice'] > $price) || $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['MinPrice'] === 0) { |
| 126 | + $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['MinPrice'] = $price; |
| 127 | + } |
| 128 | + if ($this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['MaxPrice'] < $price) { |
| 129 | + $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['MaxPrice'] = $price; |
| 130 | + } |
| 131 | + if ($this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Price'] > 0 && $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Amount'] > 0) { |
| 132 | + $this->data[$gridName]['Averages'][$goodType->title][$transactionType][$item->title]['Price'] = $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Price'] / $this->data[$gridName]['Totals'][$goodType->title][$transactionType][$item->title]['Amount']; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + return (object) $this->data; |
| 139 | + } |
| 140 | +} |
0 commit comments