Exemplo n.º 1
0
             break;
             // Deposits for reservations.
         // Deposits for reservations.
         case 9:
             $sales += $transaction->Amount;
             $cash += $transaction->Amount;
             break;
         case 10:
             $creditSales += $transaction->Amount;
             $sales += $transaction->Amount;
             break;
     }
 }
 $sold = array();
 // Get only sales transactions.
 foreach (CashboxTransaction::where('CashboxId', '=', $cashbox->Id)->where('Type', '=', 1)->get() as $transaction) {
     foreach (Sale::where('TransactionId', '=', $transaction->Id)->get() as $sale) {
         // Go through sale breackdown.
         foreach (SaleBreakdown::where('SaleId', '=', $sale->Id)->get() as $salebreakdown) {
             // Extract Product or Service.
             // TODO: Get only stock from user's branch.
             $product = Stock::where('Code', '=', $salebreakdown->Code)->first();
             if ($product) {
                 // Check if we have already sold this product.
                 if (array_key_exists($product->Code, $sold)) {
                     $sold[$salebreakdown->Code]['Quantity'] += $salebreakdown->Quantity;
                 } else {
                     $sold[$salebreakdown->Code] = array('Description' => $product->Description, 'Quantity' => $salebreakdown->Quantity);
                 }
             } else {
                 $service = Service::where('Code', '=', $salebreakdown->Code)->first();
Exemplo n.º 2
0
 public function cashboxRevision()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('date' => 'required', 'worker' => '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 worker.
     $worker = Worker::find(Input::get('worker'));
     // Get the user Id of worker.
     $user = User::where('TypeId', '=', $worker->Id)->where('Type', '=', 1)->first();
     // Now get the cashbox.
     $cashbox = Cashbox::where('Date', '=', Input::get('date'))->where('UserId', '=', $user->Id)->first();
     if ($cashbox == null) {
         $response['state'] = 'Error';
         $response['error'] = 'No se encontro una caja para este trabajador en el dia especificado!';
         return response()->json($response);
     }
     // Calculate how much the cashbox opened with.
     $open = json_decode($cashbox->Open);
     $close = json_decode($cashbox->Close);
     $openTotal = 0;
     $closeTotal = 0;
     $config = Configuration::find(0);
     foreach ($open as $value => $quantity) {
         if ($value == 'dollar') {
             $openTotal += $config->Dollar * $quantity;
         } else {
             $openTotal += $value * $quantity;
         }
     }
     if ($close !== null) {
         foreach ($close as $value => $quantity) {
             if ($value == 'dollar') {
                 $closeTotal += $config->Dollar * $quantity;
             } else {
                 $closeTotal += $value * $quantity;
             }
         }
     }
     // Now get the transactions.
     $transactions = CashboxTransaction::where('CashboxId', '=', $cashbox->Id)->get();
     $totalCashbox = $openTotal;
     $totalCreditSales = 0;
     $totalCreditPayments = 0;
     $totalProvider = 0;
     $totalStaff = 0;
     $totalOthers = 0;
     $totalWithdrawal = 0;
     $totalRefund = 0;
     $totalSales = 0;
     $allTransactions = array();
     $allSales = array();
     $response['transactions'] = $transactions;
     foreach ($transactions as $transaction) {
         switch ($transaction->Type) {
             case 1:
                 // Get the sale and it's breakdown.
                 $sale = Sale::where('TransactionId', '=', $transaction->Id)->first();
                 $salebreakdown = SaleBreakdown::where('SaleId', '=', $sale->Id)->get();
                 // Loop through the breakdown and start counting products and services.
                 foreach ($salebreakdown as $breakdown) {
                     if (array_key_exists($breakdown->Code, $allSales)) {
                         $allSales[$breakdown->Code]['quantity'] += $breakdown->Quantity;
                     } else {
                         // Get the product or service.
                         $product = Stock::where('BranchId', '=', $worker->BranchId)->where('Code', '=', $breakdown->Code)->first();
                         if ($product) {
                             $allSales[$breakdown->Code] = array('description' => $product->Description, 'quantity' => $breakdown->Quantity);
                         } else {
                             $service = Service::where('BranchId', '=', $worker->BranchId)->where('Code', '=', $breakdown->Code)->first();
                             $allSales[$breakdown->Code] = array('description' => $service->Description, 'quantity' => $breakdown->Quantity);
                         }
                     }
                 }
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => $sale->Card ? 'Venta con Tarjeta' : 'Venta Efectivo'));
                 $totalSales += $transaction->Amount;
                 break;
             case 2:
                 $totalProvider += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => $transaction->Reason));
                 break;
             case 3:
                 $totalStaff += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => 'Pago a Trabajadores'));
                 break;
             case 4:
                 $totalOthers += $transaction->Amount;
                 // TODO: What the f**k was I thinking? I should use $transaction->Reason so I can store the reason of their withdrawal.
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => $transaction->Reason));
                 break;
             case 5:
                 $totalWithdrawal += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => 'Retiro de Caja'));
                 break;
             case 6:
                 $totalRefund += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => 'Reembolso de Caja'));
                 break;
                 // Contract Payments.
             // Contract Payments.
             case 7:
                 $totalSales += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => 'Pago de Contrato'));
                 break;
                 // Credit payments.
             // Credit payments.
             case 8:
                 $totalCreditPayments += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => 'Pago a Deuda de Credito'));
                 break;
                 // Deposits for reservations.
             // Deposits for reservations.
             case 9:
                 $totalSales += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => $transaction->Reason));
                 break;
                 // Credit sales (no payment received yet)
             // Credit sales (no payment received yet)
             case 10:
                 $totalCreditSales += $transaction->Amount;
                 array_push($allTransactions, array('id' => $transaction->Id, 'date' => $transaction->DateTime, 'amount' => $transaction->Amount, 'description' => $transaction->Reason));
                 break;
         }
     }
     $totalCashbox += $totalSales - $totalProvider - $totalOthers - $totalWithdrawal - $totalStaff + $totalRefund + $totalCreditPayments;
     $response['state'] = 'Success';
     $response['total'] = $totalCashbox;
     $response['open'] = $openTotal;
     $response['close'] = $closeTotal;
     $response['creditsales'] = $totalCreditSales;
     $response['creditpayments'] = $totalCreditPayments;
     $response['provider'] = $totalProvider;
     $response['staff'] = $totalStaff;
     $response['others'] = $totalOthers;
     $response['withdrawal'] = $totalWithdrawal;
     $response['refund'] = $totalRefund;
     $response['sales'] = $totalSales;
     $response['allTransactions'] = $allTransactions;
     $response['allSales'] = $allSales;
     return response()->json($response);
 }
Exemplo n.º 3
0
                                unset($transactions[$key]);
                            }
                        }
                    } else {
                        if (isset($salesman) && $salesman != 0) {
                            $user = User::where('Type', '=', 1)->where('TypeId', '=', $salesman)->first();
                            $transactions = CashboxTransaction::where('DateTime', '>', $startDate . ' 00:00:00')->where('DateTime', '<', $endDate . ' 23:59:00')->get();
                            // Make sure transaction was made by specified salesman.
                            foreach ($transactions as $key => $transaction) {
                                $cashbox = Cashbox::find($transaction->CashboxId);
                                if ($cashbox->UserId != $user->Id) {
                                    unset($transactions[$key]);
                                }
                            }
                        } else {
                            $transactions = CashboxTransaction::where('DateTime', '>', $startDate . ' 00:00:00')->where('DateTime', '<', $endDate . ' 23:59:00')->get();
                        }
                    }
                }
            }
        }
    }
}
// Define function that translates transaction to a string.
function transactionType($transaction)
{
    switch ($transaction->Type) {
        case 1:
            // Check if a credit card was used for sale.
            if (Sale::where('TransactionId', '=', $transaction->Id)->first()->Card) {
                return 'Venta con Tarjeta';
Exemplo n.º 4
0
 public function refreshCashbox()
 {
     // Check if a cashbox is open and get all transactions, etc from it if there is.
     $cashbox = Cashbox::where('UserId', '=', Auth::user()->Id)->where('Close', '=', NULL)->first();
     if (!$cashbox) {
         $response['state'] = 'Error';
         $response['error'] = 'No hay una caja abierta y por lo tanto no se pudo actualizar los valores de la caja!';
         return response()->json($response);
     }
     $transactions = CashboxTransaction::where('CashboxId', '=', $cashbox->Id)->get();
     $cash = 0;
     $card = 0;
     $providers = 0;
     $staff = 0;
     $others = 0;
     $withdrawals = 0;
     $refunds = 0;
     $sales = 0;
     $initial = 0;
     $creditSales = 0;
     $creditPayments = 0;
     if ($cashbox) {
         // Count the initial cash.
         $bills = json_decode($cashbox->Open);
         foreach ($bills as $bill => $amount) {
             if ($bill != 'dollar') {
                 $cash += $bill * $amount;
             } else {
                 $cash += Configuration::find(0)->Dollar * $amount;
             }
         }
         $initial = $cash;
         // Now count all the other transactions.
         foreach ($transactions as $transaction) {
             switch ($transaction->Type) {
                 case 1:
                     $sales += $transaction->Amount;
                     // Check if a credit card was used for sale.
                     if (Sale::where('TransactionId', '=', $transaction->Id)->first()->Card) {
                         $card += $transaction->Amount;
                     } else {
                         $cash += $transaction->Amount;
                     }
                     break;
                 case 2:
                     $providers += $transaction->Amount;
                     break;
                 case 3:
                     $staff += $transaction->Amount;
                     break;
                 case 4:
                     $others += $transaction->Amount;
                     break;
                 case 5:
                     $withdrawals += $transaction->Amount;
                     break;
                 case 6:
                     $refunds += $transaction->Amount;
                     break;
                     // Contract Payments.
                 // Contract Payments.
                 case 7:
                     $sales += $transaction->Amount;
                     $cash += $transaction->Amount;
                     break;
                     // Credit sales.
                 // Credit sales.
                 case 8:
                     $creditSales += $transaction->Amount;
                     $sales += $transaction->Amount;
                     break;
                     // Deposits for reservations.
                 // Deposits for reservations.
                 case 9:
                     $sales += $transaction->Amount;
                     $cash += $transaction->Amount;
                     break;
                 case 10:
                     $creditPayments += $transaction->Amount;
                     $cash += $transaction->Amount;
                     break;
             }
         }
         $total = $initial + $sales + $refunds + $creditPayments - $providers - $staff - $withdrawals - $others - $creditSales;
         $cash = $cash + $refunds + $creditPayments - $providers - $staff - $withdrawals - $others;
         // Inform user operation was completed succesfully.
         $response['state'] = 'Success';
         $response['total'] = $total;
         $response['cash'] = $cash;
         $response['card'] = $card;
         $response['providers'] = $providers;
         $response['staff'] = $staff;
         $response['others'] = $others;
         $response['withdrawals'] = $withdrawals;
         $response['refunds'] = $refunds;
         $response['sales'] = $sales;
         $response['creditPayments'] = $creditPayments;
         $response['creditSales'] = $creditSales;
         return response()->json($response);
     }
 }