/**
  * @param $personid
  * @return mixed
  */
 public static function get_operations($personid)
 {
     $books = array();
     $books_info = Book::get_books_info(Auth::user()->id);
     $books_left = array();
     $books_distr = array();
     $used = 0;
     $laxmi = 0;
     $os = DB::table('operations AS o')->leftJoin('books AS b', 'o.book_id', '=', 'b.id')->where('o.person_id', $personid)->orderBy('o.custom_date', 'asc')->orderBy('o.operation_type', 'asc')->orderBy('o.datetime', 'asc')->select('o.datetime', 'o.custom_date', 'o.person_id', 'o.book_id', 'o.price', 'o.quantity', 'o.operation_type', 'o.created_at', 'o.updated_at', 'o.laxmi', 'o.description', 'b.name')->get();
     $prevcase = 0;
     $prevop = 0;
     $lxm = 0;
     $gain = 0;
     $debt = 0;
     $oss = [];
     $os[] = 1;
     foreach ($os as $o) {
         if ($prevcase == 10 && (gettype($o) != 'object' || ($o->operation_type != 10 || $o->operation_type == 10 && $prevop != $o->datetime))) {
             foreach ($books as $k => $v) {
                 if (!isset($books_distr[$k])) {
                     $books_distr[$k] = 0;
                 }
                 foreach (array_slice($v, 1) as $b) {
                     $lxm += $b[0] * $b[1];
                     $gain += $b[0] * ($b[1] > $books_info[$k]->price_buy ? $b[1] - $books_info[$k]->price_buy : 0);
                     $books_distr[$k] += $b[0];
                 }
                 unset($books[$k]);
             }
             $books = $books_left;
             $books_left = [];
             $oss[] = array('type' => 'info', 'text' => 'Распространенные книги:', 'o' => '');
             $total_books = 0;
             $points = 0;
             $book_types = ['Махабиги' => 0, 'Биги' => 0, 'Средние' => 0, 'Маленькие' => 0];
             foreach ($books_distr as $k => $v) {
                 $oss[] = array('type' => 'info', 'text' => $books_info[$k]->name, 'o' => $v);
                 switch ($books_info[$k]->book_type) {
                     case 1:
                         $points += 2 * $v;
                         $book_types['Махабиги'] += $v;
                         $total_books += $v;
                         break;
                     case 2:
                         $points += 1 * $v;
                         $book_types['Биги'] += $v;
                         $total_books += $v;
                         break;
                     case 3:
                         $points += 0.5 * $v;
                         $book_types['Средние'] += $v;
                         $total_books += $v;
                         break;
                     case 4:
                         $points += 0.25 * $v;
                         $book_types['Маленькие'] += $v;
                         $total_books += $v;
                         break;
                 }
             }
             $books_distr = [];
             $oss[] = array('type' => 'info', 'text' => 'Всего распространено книг', 'o' => $total_books);
             foreach ($book_types as $k => $v) {
                 $oss[] = array('type' => 'info', 'text' => $k, 'o' => $v);
             }
             $oss[] = array('type' => 'info', 'text' => 'Всего очков', 'o' => $points);
             $oss[] = array('type' => 'info', 'text' => 'Распространено на', 'o' => $lxm);
             $oss[] = array('type' => 'info', 'text' => 'Прибыль', 'o' => $gain);
             $oss[] = array('type' => 'info', 'text' => 'Получено', 'o' => $laxmi);
             if ($laxmi - $debt > $lxm) {
                 $oss[] = array('type' => 'info', 'text' => 'Сверхпожертвование', 'o' => $laxmi - $debt - $lxm);
                 $debt = 0;
             } elseif ($laxmi - $debt < $lxm) {
                 $debt -= $laxmi - $lxm;
                 $oss[] = array('type' => 'info', 'text' => 'Долг', 'o' => $debt);
             } else {
                 $debt = 0;
             }
             $lxm = 0;
             $laxmi = 0;
             $gain = 0;
         }
         if (gettype($o) != 'object') {
             break;
         }
         if ($prevop != $o->datetime || !$prevop) {
             $oss[] = array('type' => 'operation', 'o' => $o);
         }
         $prevcase = $o->operation_type;
         $prevop = $o->datetime;
         switch ($o->operation_type) {
             case 10:
                 if (!$o->book_id) {
                 } elseif (isset($books[$o->book_id])) {
                     $used = $books[$o->book_id][0] - $o->quantity;
                     if ($used < 0) {
                         $oss[] = array('type' => 'warning', 'o' => 'Сдано больше чем было (' . $books[$o->book_id][0] . ')');
                         $used = 0;
                     }
                     if (!isset($books_distr[$o->book_id]) && $used) {
                         $books_distr[$o->book_id] = 0;
                     }
                     if ($used) {
                         $books_distr[$o->book_id] += $used;
                     }
                     foreach (array_slice($books[$o->book_id], 1) as $b) {
                         if ($b[0] <= $used) {
                             $shifted_b = array_splice($books[$o->book_id], 1, 1);
                             $used -= $shifted_b[0][0];
                             $lxm += $b[0] * $b[1];
                             $gain += $b[0] * ($b[1] > $books_info[$o->book_id]->price_buy ? $b[1] - $books_info[$o->book_id]->price_buy : 0);
                         } else {
                             $books[$o->book_id][1][0] -= $used;
                             $lxm += $used * $b[1];
                             $gain += $used * ($b[1] > $books_info[$o->book_id]->price_buy ? $b[1] - $books_info[$o->book_id]->price_buy : 0);
                             break;
                         }
                     }
                     if ($o->quantity > 0) {
                         $books_left[$o->book_id] = [$o->quantity, [$o->quantity, $o->price]];
                     }
                     unset($books[$o->book_id]);
                 } else {
                     $oss[] = array('type' => 'warning', 'o' => 'Остаток книг, которых не было');
                 }
                 $oss[] = array('type' => 'subop', 'o' => $o);
                 break;
             case 1:
                 if (isset($books[$o->book_id])) {
                     $books[$o->book_id][0] += $o->quantity;
                     $books[$o->book_id][] = array($o->quantity, $o->price);
                 } else {
                     $books[$o->book_id] = [$o->quantity, [$o->quantity, $o->price]];
                 }
                 $oss[] = array('type' => 'subop', 'o' => $o);
                 break;
             case 2:
                 $laxmi += $o->laxmi;
                 $oss[] = array('type' => 'subop', 'o' => $o);
                 break;
             case 4:
                 if (isset($books[$o->book_id])) {
                     $books[$o->book_id][0] -= $o->quantity;
                     $qty = $o->quantity;
                     $complete = 0;
                     foreach (array_slice($books[$o->book_id], 1) as $b2) {
                         if ($b2[0] <= $qty) {
                             $shifted_b = array_splice($books[$o->book_id], 1, 1);
                             $qty -= $shifted_b[0][0];
                         } else {
                             $books[$o->book_id][1][0] -= $qty;
                             $complete = 1;
                             break;
                         }
                     }
                     if ($books[$o->book_id][0] == 0) {
                         unset($books[$o->book_id]);
                     } elseif ($complete) {
                         $books[$o->book_id] = [$books[$o->book_id][0], [$books[$o->book_id][0], $o->price]];
                     } else {
                         unset($books[$o->book_id]);
                         $oss[] = array('type' => 'warning', 'o' => 'Вернули лишние книги');
                     }
                 } else {
                     $oss[] = array('type' => 'warning', 'o' => 'Сданы книги, которые не выдавались');
                 }
                 $oss[] = array('type' => 'subop', 'o' => $o);
                 break;
         }
     }
     array_pop($os);
     foreach ($os as $o) {
         if ($o->book_id && isset($books[$o->book_id])) {
             $books[$o->book_id]['name'] = $o->name;
         }
     }
     $current_books_price = 0;
     foreach ($books as $k => $v) {
         foreach (array_slice($v, 1, -1) as $b) {
             $current_books_price += $b[0] * $b[1];
         }
     }
     return [$oss, $books, $lxm, $laxmi, $current_books_price, $debt];
 }