示例#1
0
 /**
  * Suma la cantidad especificada al stock del artículo en el almacén especificado.
  * @param type $almacen
  * @param type $cantidad
  * @param type $recalcula_coste
  * @return boolean
  */
 public function sum_stock($almacen, $cantidad = 1, $recalcula_coste = FALSE)
 {
     if ($this->nostock) {
         return TRUE;
     } else {
         $result = FALSE;
         $stock = new stock();
         $encontrado = FALSE;
         $stocks = $stock->all_from_articulo($this->referencia);
         foreach ($stocks as $k => $value) {
             if ($value->codalmacen == $almacen) {
                 $stocks[$k]->sum_cantidad($cantidad);
                 $result = $stocks[$k]->save();
                 $encontrado = TRUE;
                 break;
             }
         }
         if (!$encontrado) {
             $stock->referencia = $this->referencia;
             $stock->codalmacen = $almacen;
             $stock->set_cantidad($cantidad);
             $result = $stock->save();
         }
         if ($result) {
             $nuevo_stock = $stock->total_from_articulo($this->referencia);
             if ($this->stockfis != $nuevo_stock) {
                 $this->stockfis = $nuevo_stock;
                 if ($recalcula_coste) {
                     $this->costemedio = $this->get_costemedio();
                 }
                 if ($this->exists) {
                     $this->clean_cache();
                     $result = $this->db->exec("UPDATE " . $this->table_name . " SET stockfis = " . $this->var2str($this->stockfis) . ",\n                     costemedio = " . $this->var2str($this->costemedio) . " WHERE referencia = " . $this->var2str($this->referencia) . ";");
                 } else {
                     if (!$this->save()) {
                         $this->new_error_msg("¡Error al actualizar el stock del artículo!");
                     }
                 }
             }
         } else {
             $this->new_error_msg("¡Error al guardar el stock!");
         }
         return $result;
     }
 }