Пример #1
0
 /**
  * Function that submits stocktake.
  *
  * @return Response
  */
 public function submitStockTake()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('stockItems' => 'required'));
     $response = array();
     if ($validator->fails()) {
         $response['state'] = 'Error';
         $response['error'] = 'Es necesario escribir el codigo del producto';
         return response()->json($response);
     }
     // Check that user is part of authorized staff.
     if (Auth::user()->Type != 1) {
         $response['state'] = 'No Autorizado';
         $response['error'] = 'Usuario no autorizado!';
         return response()->json($response);
     }
     // Get the branch of the current worker.
     $branchId = Worker::find(Auth::user()->TypeId)->BranchId;
     // Create Stocktake.
     $stocktake = StockTake::create(array('Created' => date('Y-m-d H:i:s'), 'BranchId' => $branchId));
     // Now insert the breakdown of the stock.
     foreach (Input::get('stockItems') as $item) {
         // Get product.
         $product = Stock::find($item['id']);
         // Insert breakdown.
         StockTakeBreakdown::create(array('StockTakeId' => $stocktake->Id, 'Code' => $product->Code, 'SystemQuantity' => $product->Quantity, 'Counted' => $item['quantity'], 'Difference' => $item['quantity'] - $product->Quantity, 'State' => 0, 'ExtraData' => ''));
     }
     $response['state'] = 'Success';
     $response['message'] = 'Toma de inventario agregado exitosamente!';
     return response()->json($response);
 }
Пример #2
0
 public function updateStockTakeData()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('stocktake' => '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.
     $worker = Worker::find(Auth::user()->TypeId);
     $branchId = $worker->BranchId;
     $cashbox = Cashbox::where('BranchId', '=', $branchId)->where('Close', '=', null)->where('UserId', '=', Auth::user()->Id)->first();
     if (!$cashbox) {
         $response['state'] = 'Error';
         $response['error'] = 'Es necesario tener la caja abierta para realizar esta accion!';
         return response()->json($response);
     }
     // Let's loop through the stocktake breakdown and start making necessary changes.
     foreach (Input::get('stocktake') as $item) {
         $breakdown = StockTakeBreakdown::find($item['id']);
         // Compare current state to submitted state and that difference is more or less than 0.
         if ($breakdown->State != $item['state'] && $breakdown->Difference != 0) {
             // If we are changing state make sure to undo any previous changes if we have to.
             if ($breakdown->State > 1 && $breakdown->State < 4) {
                 if ($breakdown->State == 2) {
                     // Get the salebreakdown.
                     $salebreakdown = SaleBreakdown::find(json_decode($breakdown->ExtraData)->salebreakdownId);
                     // Now the sale.
                     $sale = Sale::find($salebreakdown->SaleId);
                     // Now the transaction.
                     $transaction = CashboxTransaction::find($sale->TransactionId);
                     // Now delete them all.
                     $transaction->delete();
                     $sale->delete();
                     $salebreakdown->delete();
                     $breakdown->ExtraData = '';
                     $breakdown->save();
                 }
                 $product = Stock::where('Code', '=', $breakdown->Code)->where('BranchId', '=', $branchId)->first();
                 $product->Quantity -= $breakdown->Difference;
                 $product->save();
             }
             // Make changes.
             if ($item['state'] == 2) {
                 // Get the product.
                 $product = Stock::where('Code', '=', $breakdown->Code)->where('BranchId', '=', $branchId)->first();
                 // Now make the sale.
                 if ($breakdown->Difference > 0) {
                     // Make an empty transaction.
                     $transaction = CashboxTransaction::create(array('CashboxId' => $cashbox->Id, 'DateTime' => date('Y-m-d H:i:s'), 'Type' => 10, 'Amount' => 0, 'Reason' => 'Correccion de Inventario.'));
                     // Now make the sale so we can reflect the difference.
                     $sale = Sale::create(array('Created' => date('Y-m-d H:i:s'), 'WorkerId' => $worker->Id, 'Value' => 0, 'Tax' => 0, 'Card' => 0, 'TransactionId' => $transaction->Id, 'BranchId' => $branchId, 'Credit' => 0, 'CreditorId' => 0, 'CreditorType' => 1, 'Cancelled' => 1));
                     $salebreakdown = SaleBreakdown::create(array('SaleId' => $sale->Id, 'Code' => $breakdown->Code, 'Quantity' => -1 * $breakdown->Difference, 'Cost' => $product->AverageCost, 'Price' => 0, 'ExtraData' => json_encode(array('stocktakeBreakdownId' => $breakdown->Id))));
                     $product->Quantity += $breakdown->Difference;
                     $product->save();
                     $breakdown->ExtraData = json_encode(array('salebreakdownId' => $salebreakdown->Id));
                     $breakdown->save();
                 } else {
                     if ($breakdown->Difference < 0) {
                         // Make an empty transaction.
                         $transaction = CashboxTransaction::create(array('CashboxId' => $cashbox->Id, 'DateTime' => date('Y-m-d H:i:s'), 'Type' => 10, 'Amount' => 0, 'Reason' => 'Correccion de Inventario.'));
                         // Now make the sale so we can reflect the difference.
                         $sale = Sale::create(array('Created' => date('Y-m-d H:i:s'), 'WorkerId' => $worker->Id, 'Value' => 0, 'Tax' => 0, 'Card' => 0, 'TransactionId' => $transaction->Id, 'BranchId' => $branchId, 'Credit' => 0, 'CreditorId' => 0, 'CreditorType' => 1, 'Cancelled' => 1));
                         $salebreakdown = SaleBreakdown::create(array('SaleId' => $sale->Id, 'Code' => $breakdown->Code, 'Quantity' => -1 * $breakdown->Difference, 'Cost' => $product->AverageCost, 'Price' => 0, 'ExtraData' => json_encode(array('stocktakeBreakdownId' => $breakdown->Id))));
                         $product->Quantity += $breakdown->Difference;
                         $product->save();
                         $breakdown->ExtraData = json_encode(array('salebreakdownId' => $salebreakdown->Id));
                         $breakdown->save();
                     }
                 }
             } else {
                 if ($item['state'] == 3) {
                     // Get the product.
                     $product = Stock::where('Code', '=', $breakdown->Code)->where('BranchId', '=', $branchId)->first();
                     $product->Quantity += $breakdown->Difference;
                     $product->save();
                 }
             }
         }
         // Now update breakdown State.
         $breakdown->State = $item['state'];
         $breakdown->save();
     }
     $response['state'] = 'Success';
     return response()->json($response);
 }