/** * @param Request $request */ public function getAssetId(Request $request) { $type = $this->asset_type[$request->get('type')]; $brand = str_pad($request->get('brand'), 5, "_"); $date = $request->get('entry_at'); $yy = substr($date, 2, 2); $mm = substr($date, 5, 2); $dd = substr($date, 8, 2); $section = $this->sections[$request->get('section')]; $warranty = $request->get('warranty'); $serial = Asset::where('type', '=', $request->get('type'))->count(); return $type . $dd . $mm . $yy . $brand . $warranty . $section . $serial; }
/** * Execute the console command. * * @return mixed */ public function handle() { // Get assets that should decay. $assets = Asset::where('Type', '=', 1)->get(); // Loop through the assets and decay them. foreach ($assets as $asset) { // Check that we haven't already decayed this asset. $decay = AssetDecay::where('AssetId', '=', $asset->Id)->where('Date', '=', date('Y-m-d'))->first(); if (!$decay) { $decayValue = $asset->Value / $asset->Days; $decay = AssetDecay::create(array('AssetId' => $asset->Id, 'Date' => date('Y-m-d'), 'Value' => $decayValue)); // Update asset. $asset->Value -= $decayValue; $asset->Days--; $asset->save(); } $permissions = json_decode(UserLevel::find(1)->Permissions); print_r($permissions); } }
public function financeAnalyticsGraph() { // Validate Input. $validator = Validator::make(Input::all(), array('start' => 'required', 'end' => 'required', 'toGraph' => 'required', 'currency' => 'required', 'interval' => 'required')); if ($validator->fails()) { return response()->json(['error' => 'Informacion incompleta!']); } // Check that user is part of authorized staff. if (Auth::user()->Type != 1) { // If they are unauthorized no point in returning anything. return response()->json(array()); } // Get the branch of the current worker. $branchId = Worker::find(Auth::user()->TypeId)->BranchId; // Now get all the sales made in this branch. $sales = Sale::where('BranchId', '=', $branchId)->where('Created', '>=', Input::get('start') . ' 00:00:00')->where('Created', '<=', Input::get('end') . ' 23:59:59')->get(); $cashboxes = Cashbox::where('BranchId', '=', $branchId)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); $totalSales = 0; $totalStock = 0; $totalProfit = 0; $totalExpense = 0; $valueDollarByDate = array(); $profitByDate = array(); $salesByDate = array(); $stockByDate = array(); $salaryByDate = array(); $expenseByDate = array(); $months = array(); // First let's get all the dollar values in this period of time. foreach ($cashboxes as $cashbox) { if (!array_key_exists($cashbox->Date, $valueDollarByDate)) { $valueDollarByDate[$cashbox->Date] = $cashbox->Dollar; } } // Define keys for all arrays. $initDate = date_create(date('Y-m-d', strtotime(Input::get('start')))); $finalDate = date_create(date('Y-m-d', strtotime(Input::get('end')))); $currentDate = $initDate; $currentDollarValue = 1; while ($currentDate <= $finalDate) { // Check if we have saved the first day of this month. if (!in_array(date_format($currentDate, 'Y-m') . '-01', $months)) { array_push($months, date_format($currentDate, 'Y-m') . '-01'); } $c = date_format($currentDate, 'Y-m-d'); $profitByDate[$c] = 0; $salesByDate[$c] = 0; $stockByDate[$c] = 0; $salaryByDate[$c] = 0; $expenseByDate[$c] = 0; if (!array_key_exists($c, $valueDollarByDate)) { $valueDollarByDate[$c] = $currentDollarValue; } else { $currentDollarValue = $valueDollarByDate[$c]; } date_add($currentDate, date_interval_create_from_date_string("1 day")); } // Loop through sales and calculate the stock used. foreach ($sales as $sale) { // Check if we are using cordobas or dollars. if (Input::get('currency') == 'cordoba') { $totalSales += $sale->Value - $sale->Card; $totalProfit += $sale->Value - $sale->Card; $profitByDate[date('Y-m-d', strtotime($sale->Created))] += $sale->Value - $sale->Card; $salesByDate[date('Y-m-d', strtotime($sale->Created))] += $sale->Value - $sale->Card; $breakdowns = SaleBreakdown::where('SaleId', '=', $sale->Id)->get(); foreach ($breakdowns as $breakdown) { $totalProfit -= $breakdown->Cost * $breakdown->Quantity; $totalStock += $breakdown->Cost * $breakdown->Quantity; $profitByDate[date('Y-m-d', strtotime($sale->Created))] -= $breakdown->Cost * $breakdown->Quantity; $stockByDate[date('Y-m-d', strtotime($sale->Created))] += $breakdown->Cost * $breakdown->Quantity; } } else { $totalSales += ($sale->Value - $sale->Card) / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $totalProfit += ($sale->Value - $sale->Card) / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $profitByDate[date('Y-m-d', strtotime($sale->Created))] += ($sale->Value - $sale->Card) / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $salesByDate[date('Y-m-d', strtotime($sale->Created))] += ($sale->Value - $sale->Card) / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $breakdowns = SaleBreakdown::where('SaleId', '=', $sale->Id)->get(); foreach ($breakdowns as $breakdown) { $totalProfit -= $breakdown->Cost * $breakdown->Quantity / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $totalStock += $breakdown->Cost * $breakdown->Quantity / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $profitByDate[date('Y-m-d', strtotime($sale->Created))] -= $breakdown->Cost * $breakdown->Quantity / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; $stockByDate[date('Y-m-d', strtotime($sale->Created))] += $breakdown->Cost * $breakdown->Quantity / $valueDollarByDate[date('Y-m-d', strtotime($sale->Created))]; } } } // Ok now let's loop through the cashboxes again, but this time we will get all "other" expenses. foreach ($cashboxes as $cashbox) { // Get all transactions for current cashbox that where for "other" expenses. $transactions = CashboxTransaction::where('CashboxId', '=', $cashbox->Id)->where('Type', '=', 4)->get(); if (Input::get('currency') == 'cordoba') { foreach ($transactions as $transaction) { $totalProfit -= $transaction->Amount; $totalExpense += $transaction->Amount; $profitByDate[date('Y-m-d', strtotime($transaction->DateTime))] -= $transaction->Amount; $expenseByDate[date('Y-m-d', strtotime($transaction->DateTime))] += $transaction->Amount; } } else { foreach ($transactions as $transaction) { $totalProfit -= $transaction->Amount / $valueDollarByDate[date('Y-m-d', strtotime($transaction->DateTime))]; $totalExpense += $transaction->Amount / $valueDollarByDate[date('Y-m-d', strtotime($transaction->DateTime))]; $profitByDate[date('Y-m-d', strtotime($transaction->DateTime))] -= $transaction->Amount / $valueDollarByDate[date('Y-m-d', strtotime($transaction->DateTime))]; $expenseByDate[date('Y-m-d', strtotime($transaction->DateTime))] += $transaction->Amount / $valueDollarByDate[date('Y-m-d', strtotime($transaction->DateTime))]; } } } // Now calculate how much was paid in salaries in that branch. $workers = Worker::where('BranchId', '=', $branchId)->get(); $totalSalary = 0; foreach ($workers as $worker) { $salaries = WorkerSalary::where('WorkerId', '=', $worker->Id)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); if (Input::get('currency') == 'cordoba') { foreach ($salaries as $salary) { /* Ok, so big change here, I now will have an aguinaldo field for each salary day this field will be generated upon aguinaldo payment based on the worker's current salary. Because of this I will only calculate aguinaldo on salary days that haven't been paid by aguinaldo. */ if ($salary->DayType != 3 || $salary->DayType != 5) { if ($salary->AguinaldoPaid) { $totalSalary += $salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance; $totalProfit -= $salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance; $profitByDate[$salary->Date] -= $salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance; $salaryByDate[$salary->Date] += $salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance; } else { // Get the worker. $worker = Worker::find($salary->WorkerId); $totalSalary += $salary->Basic + $salary->Bonus + $salary->Insurance; $totalSalary += $worker->Basic * 0.2465; $totalProfit -= $salary->Basic + $salary->Bonus + $salary->Insurance; $totalProfit -= $worker->Basic * 0.2465; $profitByDate[$salary->Date] -= $salary->Basic + $salary->Bonus + $salary->Insurance; $salaryByDate[$salary->Date] += $salary->Basic + $salary->Bonus + $salary->Insurance; $salaryByDate[$salary->Date] += $worker->Basic * 0.2465; $profitByDate[$salary->Date] -= $worker->Basic * 0.2465; } } } } else { foreach ($salaries as $salary) { /* Ok, so big change here, I now will have an aguinaldo field for each salary day this field will be generated upon aguinaldo payment based on the worker's current salary. Because of this I will only calculate aguinaldo on salary days that haven't been paid by aguinaldo. */ if ($salary->DayType != 3 || $salary->DayType != 5) { if ($salary->AguinaldoPaid) { $totalSalary += ($salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $totalProfit -= ($salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $profitByDate[$salary->Date] -= ($salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $salaryByDate[$salary->Date] += ($salary->Basic + $salary->Bonus + $salary->Aguinaldo + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; } else { // Get the worker. $worker = Worker::find($salary->WorkerId); $totalSalary += ($salary->Basic + $salary->Bonus + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $totalSalary += $worker->Basic * 0.2465 / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $totalProfit -= ($salary->Basic + $salary->Bonus + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $totalProfit -= $worker->Basic * 0.2465 / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $profitByDate[$salary->Date] -= ($salary->Basic + $salary->Bonus + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $salaryByDate[$salary->Date] += ($salary->Basic + $salary->Bonus + $salary->Insurance) / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $salaryByDate[$salary->Date] += $worker->Basic * 0.2465 / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; $profitByDate[$salary->Date] -= $worker->Basic * 0.2465 / $valueDollarByDate[date('Y-m-d', strtotime($salary->Date))]; } } } } } // Now to finish up we need to get our expenses. $expenses = Expense::where('Date', '>=', $months[0])->where('Date', '<=', $months[count($months) - 1])->where('BranchId', '=', $branchId)->get(); foreach ($expenses as $expense) { $currentMonthExpense = 0; if ($expense->Regimen == 'cuotafija') { $currentMonthExpense = $expense->Electricity + $expense->Water + $expense->Telecomunications + $expense->Rent + $expense->Depreciation + $expense->Security + $expense->Government + $expense->Taxes; } else { $currentMonthExpense = $expense->Electricity + $expense->Water + $expense->Telecomunications + $expense->Rent + $expense->Depreciation + $expense->Security + $expense->Government; } // Get amount of days in this month. $totalDays = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($expense->Date)), date('y', strtotime($expense->Date))); $dailyTotal = $currentMonthExpense / $totalDays; if (Input::get('currency') == 'cordoba') { foreach ($expenseByDate as $expenseDate => $dailyExpense) { // Check if Date is of current month. if (date('m', strtotime($expense->Date)) == date('m', strtotime($expenseDate))) { $totalExpense += $dailyTotal; $totalProfit -= $dailyTotal; $dailyExpense += $dailyTotal; $profitByDate[$expenseDate] -= $dailyTotal; $expenseByDate[$expenseDate] += $dailyTotal; } } } else { foreach ($expenseByDate as $expenseDate => $dailyExpense) { // Check if Date is of current month. if (date('m', strtotime($expense->Date)) == date('m', strtotime($expenseDate))) { $totalExpense += $dailyTotal / $valueDollarByDate[$expenseDate]; $totalProfit -= $dailyTotal / $valueDollarByDate[$expenseDate]; $dailyExpense += $dailyTotal / $valueDollarByDate[$expenseDate]; $profitByDate[$expenseDate] -= $dailyTotal / $valueDollarByDate[$expenseDate]; $expenseByDate[$expenseDate] += $dailyTotal / $valueDollarByDate[$expenseDate]; } } } } // Get vehicles of this branch. $vehicles = Vehicle::where('BranchId', '=', $branchId)->get(); // Loop through vehicles and get any transport requests carried out in defined period of time. foreach ($vehicles as $vehicle) { // Get transport requests carried out this day. $transportRequests = Transport::where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->where('VehicleId', '=', $vehicle->Id)->where('State', '=', 1)->get(); if (Input::get('currency') == 'cordoba') { // Loop through transport requests and add the depreciation of vehicles as expense. foreach ($transportRequests as $transport) { $totalProfit -= $transport->Depreciation; $totalExpense += $transport->Depreciation; $profitByDate[$transport->Date] -= $transport->Depreciation; $expenseByDate[$transport->Date] += $transport->Depreciation; } } else { // Loop through transport requests and add the depreciation of vehicles as expense. foreach ($transportRequests as $transport) { $totalProfit -= $transport->Depreciation / $valueDollarByDate[$transport->Date]; $totalExpense += $transport->Depreciation / $valueDollarByDate[$transport->Date]; $profitByDate[$transport->Date] -= $transport->Depreciation / $valueDollarByDate[$transport->Date]; $expenseByDate[$transport->Date] += $transport->Depreciation / $valueDollarByDate[$transport->Date]; } } } // Get assets from this branch. $assets = Asset::where('BranchId', '=', $branchId)->where('Type', '=', 1)->get(); foreach ($assets as $asset) { $assetDecay = AssetDecay::where('AssetId', '=', $asset->Id)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); if (Input::get('currency') == 'cordoba') { // Loop through decays and add it as expense. foreach ($assetDecay as $decay) { $totalProfit -= $decay->Value; $totalExpense += $decay->Value; $profitByDate[$decay->Date] -= $decay->Value; $expenseByDate[$decay->Date] += $decay->Value; } } else { // Loop through decays and add it as expense. foreach ($assetDecay as $decay) { $totalProfit -= $decay->Value / $valueDollarByDate[$decay->Date]; $totalExpense += $decay->Value / $valueDollarByDate[$decay->Date]; $profitByDate[$decay->Date] -= $decay->Value / $valueDollarByDate[$decay->Date]; $expenseByDate[$decay->Date] += $decay->Value / $valueDollarByDate[$decay->Date]; } } } switch (Input::get('interval')) { case 'days': $labels = array(); foreach ($salaryByDate as $date => $sales) { array_push($labels, $date); } // Return required information. $response['label'] = $labels; $response['profitByDate'] = $profitByDate; $response['salesByDate'] = $salesByDate; $response['stockByDate'] = $stockByDate; $response['expenseByDate'] = $expenseByDate; $response['salaryByDate'] = $salaryByDate; $response['totalProfit'] = $totalProfit; $response['totalSales'] = $totalSales; $response['totalStock'] = $totalStock; $response['totalExpense'] = $totalExpense; $response['totalSalary'] = $totalSalary; break; case 'weeks': $labels = array(); $salesByWeek = array(); $profitByWeek = array(); $stockByWeek = array(); $expenseByWeek = array(); $salaryByWeek = array(); foreach ($salesByDate as $date => $sales) { if (!in_array(date('W-Y', strtotime($date)), $labels)) { array_push($labels, date('W-Y', strtotime($date))); $salesByWeek[date('W-Y', strtotime($date))] = $salesByDate[$date]; $profitByWeek[date('W-Y', strtotime($date))] = $profitByDate[$date]; $stockByWeek[date('W-Y', strtotime($date))] = $stockByDate[$date]; $expenseByWeek[date('W-Y', strtotime($date))] = $expenseByDate[$date]; $salaryByWeek[date('W-Y', strtotime($date))] = $salaryByDate[$date]; } else { $salesByWeek[date('W-Y', strtotime($date))] += $salesByDate[$date]; $profitByWeek[date('W-Y', strtotime($date))] += $profitByDate[$date]; $stockByWeek[date('W-Y', strtotime($date))] += $stockByDate[$date]; $expenseByWeek[date('W-Y', strtotime($date))] += $expenseByDate[$date]; $salaryByWeek[date('W-Y', strtotime($date))] += $salaryByDate[$date]; } } // Return required information. $response['label'] = $labels; $response['profitByDate'] = $profitByWeek; $response['salesByDate'] = $salesByWeek; $response['stockByDate'] = $stockByWeek; $response['expenseByDate'] = $expenseByWeek; $response['salaryByDate'] = $salaryByWeek; $response['totalProfit'] = $totalProfit; $response['totalSales'] = $totalSales; $response['totalStock'] = $totalStock; $response['totalExpense'] = $totalExpense; $response['totalSalary'] = $totalSalary; break; case 'months': $labels = array(); $salesByMonth = array(); $profitByMonth = array(); $stockByMonth = array(); $expenseByMonth = array(); $salaryByMonth = array(); foreach ($salesByDate as $date => $sales) { if (!in_array(date('m-Y', strtotime($date)), $labels)) { array_push($labels, date('m-Y', strtotime($date))); $salesByMonth[date('m-Y', strtotime($date))] = $salesByDate[$date]; $profitByMonth[date('m-Y', strtotime($date))] = $profitByDate[$date]; $stockByMonth[date('m-Y', strtotime($date))] = $stockByDate[$date]; $expenseByMonth[date('m-Y', strtotime($date))] = $expenseByDate[$date]; $salaryByMonth[date('m-Y', strtotime($date))] = $salaryByDate[$date]; } else { $salesByMonth[date('m-Y', strtotime($date))] += $salesByDate[$date]; $profitByMonth[date('m-Y', strtotime($date))] += $profitByDate[$date]; $stockByMonth[date('m-Y', strtotime($date))] += $stockByDate[$date]; $expenseByMonth[date('m-Y', strtotime($date))] += $expenseByDate[$date]; $salaryByMonth[date('m-Y', strtotime($date))] += $salaryByDate[$date]; } } // Return required information. $response['label'] = $labels; $response['profitByDate'] = $profitByMonth; $response['salesByDate'] = $salesByMonth; $response['stockByDate'] = $stockByMonth; $response['expenseByDate'] = $expenseByMonth; $response['salaryByDate'] = $salaryByMonth; $response['totalProfit'] = $totalProfit; $response['totalSales'] = $totalSales; $response['totalStock'] = $totalStock; $response['totalExpense'] = $totalExpense; $response['totalSalary'] = $totalSalary; break; case 'years': $labels = array(); $salesByYear = array(); $profitByYear = array(); $stockByYear = array(); $expenseByYear = array(); $salaryByYear = array(); foreach ($salesByDate as $date => $sales) { if (!in_array(date('Y', strtotime($date)), $labels)) { array_push($labels, date('Y', strtotime($date))); $salesByYear[date('Y', strtotime($date))] = $salesByDate[$date]; $profitByYear[date('Y', strtotime($date))] = $profitByDate[$date]; $stockByYear[date('Y', strtotime($date))] = $stockByDate[$date]; $expenseByYear[date('Y', strtotime($date))] = $expenseByDate[$date]; $salaryByYear[date('Y', strtotime($date))] = $salaryByDate[$date]; } else { $salesByYear[date('Y', strtotime($date))] += $salesByDate[$date]; $profitByYear[date('Y', strtotime($date))] += $profitByDate[$date]; $stockByYear[date('Y', strtotime($date))] += $stockByDate[$date]; $expenseByYear[date('Y', strtotime($date))] += $expenseByDate[$date]; $salaryByYear[date('Y', strtotime($date))] += $salaryByDate[$date]; } } // Return required information. $response['label'] = $labels; $response['profitByDate'] = $profitByYear; $response['salesByDate'] = $salesByYear; $response['stockByDate'] = $stockByYear; $response['expenseByDate'] = $expenseByYear; $response['salaryByDate'] = $salaryByYear; $response['totalProfit'] = $totalProfit; $response['totalSales'] = $totalSales; $response['totalStock'] = $totalStock; $response['totalExpense'] = $totalExpense; $response['totalSalary'] = $totalSalary; break; default: // Anything else we just don't do anything. break; } $response['state'] = 'Success'; // Return suggestions. return response()->json($response); }