/**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Label $labelModel, Request $request, Purchase $purchaseModel, User $userModel, Product $productModel, History $historyModel)
 {
     if (isset($_POST['update_template'])) {
         //pr($_POST);
         if (isset($_POST['col']) and !empty($_POST['col'])) {
             $fields = serialize($_POST['col']);
         } else {
             $fields = null;
         }
         if (!empty($_POST['name_template'])) {
             $template_name = $_POST['name_template'];
         } elseif (isset($_POST['selected_name_template']) and !empty($_POST['selected_name_template'])) {
             $template_name = $_POST['selected_name_template'];
         } else {
             $template_name = 'По умолчанию';
         }
         $template_id = $userModel->checkTemplates($template_name, 'products');
         if ($template_id != null) {
             $res = array('fields' => $fields);
             $userModel->updateTemplate($res, $template_id, 'products');
         } else {
             //добавляем шаблон
             $res = array('type' => 'products', 'user_id' => Auth::User()->id, 'template' => $template_name, 'fields' => $fields);
             $userModel->addTemplates($res, 'products');
         }
         Session::flash('message', GetMessages("SUCCESS_SETTINGS_SAVE"));
         return redirect($_SERVER['HTTP_REFERER']);
     }
     if (isset($_POST['delete_template'])) {
         $userModel->deleteTemplate($_POST['selected_name_template'], 'products');
         Session::flash('message', GetMessages("SUCCESS_TEMPLATE_DELETE"));
         return redirect($_SERVER['HTTP_REFERER']);
     }
     if (isset($_POST['select_template'])) {
         $userModel->updateUser(Auth::User()->id, array('template_prod_id' => $_POST['selected_name_template']));
         Session::flash('message', GetMessages("SUCCESS_SETTINGS_SAVE"));
         return redirect($_SERVER['HTTP_REFERER']);
     }
     if (isset($_POST['search_button'])) {
         //pr($_POST);
         if (strlen($_POST['search_field']) > 0) {
             return redirect()->route('product.search.index', ['string' => $_POST['search_field']]);
         } else {
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['update_products'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         unset($_POST['update_products']);
         unset($_POST['_token']);
         $res = array();
         $reViewAbsents = false;
         //pr($_POST);
         foreach ($_POST as $element) {
             if (array_key_exists('check', $element)) {
                 unset($element['check']);
                 $childs = false;
                 if (isset($element['childs'])) {
                     $childs = unserialize($productModel->where('id', $element['id'])->pluck('childs'));
                 }
                 //status
                 !isset($element['status']['new']) ? $element['status']['new'] = 0 : ($element['status']['new'] = 1);
                 if ($element['status']['new'] != $element['status']['old']) {
                     $res[$element['id']]['products.status'] = $element['status']['new'];
                 }
                 //name
                 if ($element['name']['new'] != $element['name']['old']) {
                     $res[$element['id']]['products.name'] = trim($element['name']['new']);
                 }
                 //monitoring_name
                 if ($element['monitoring_name']['new'] != $element['monitoring_name']['old']) {
                     $res[$element['id']]['products.monitoring_name'] = trim($element['monitoring_name']['new']);
                 }
                 //absent
                 if ($element['absent']['new'] != $element['absent']['old']) {
                     if (empty($element['absent']['new'])) {
                         $res[$element['id']]['products.absent'] = null;
                     } else {
                         $res[$element['id']]['products.absent'] = trim($element['absent']['new']);
                     }
                     $reViewAbsents = true;
                 }
                 //article
                 if ($element['article']['new'] != $element['article']['old']) {
                     $res[$element['id']]['products.article'] = trim($element['article']['new']);
                 }
                 //in_stock
                 !isset($element['in_stock']['new']) ? $element['in_stock']['new'] = 0 : ($element['in_stock']['new'] = 1);
                 if ($element['in_stock']['new'] != $element['in_stock']['old']) {
                     $res[$element['id']]['products.in_stock'] = $element['in_stock']['new'];
                 }
                 //flag
                 !isset($element['flag']['new']) ? $element['flag']['new'] = 0 : ($element['flag']['new'] = 1);
                 if ($element['flag']['new'] != $element['flag']['old']) {
                     $res[$element['id']]['products.flag'] = $element['flag']['new'];
                 }
                 //sort
                 if ($element['sort']['new'] != $element['sort']['old']) {
                     $res[$element['id']]['products.sort'] = $element['sort']['new'];
                 }
                 //mrc
                 if ($element['mrc']['new'] != $element['mrc']['old']) {
                     $res[$element['id']]['products.mrc'] = toFloat($element['mrc']['new']);
                 }
                 //mrc_currency
                 if ($element['mrc_currency']['new'] != $element['mrc_currency']['old']) {
                     $res[$element['id']]['products.mrc_currency'] = $element['mrc_currency']['new'];
                 }
                 //mrc_relation
                 if ($element['mrc_relation']['new'] != $element['mrc_relation']['old']) {
                     $res[$element['id']]['products.mrc_relation'] = trim($element['mrc_relation']['new']);
                 }
                 //mrc_raise_relation
                 if ($element['mrc_raise_relation']['new'] != $element['mrc_raise_relation']['old']) {
                     $res[$element['id']]['products.mrc_raise_relation'] = trim($element['mrc_raise_relation']['new']);
                 }
                 //price
                 if ($element['price']['new'] != $element['price']['old']) {
                     $res[$element['id']]['products.price'] = toFloat($element['price']['new']);
                 }
                 //price_currency
                 if ($element['price_currency']['new'] != $element['price_currency']['old']) {
                     $res[$element['id']]['products.price_currency'] = $element['price_currency']['new'];
                 }
                 //price_relation
                 if ($element['price_relation']['new'] != $element['price_relation']['old']) {
                     $res[$element['id']]['products.price_relation'] = trim($element['price_relation']['new']);
                 }
                 //price_raise_relation
                 if ($element['price_raise_relation']['new'] != $element['price_raise_relation']['old']) {
                     $res[$element['id']]['products.price_raise_relation'] = trim($element['price_raise_relation']['new']);
                 }
                 //target_margin
                 isset($element['target_margin']['new']) ? $element['target_margin']['new'] = toFloat($element['target_margin']['new']) : ($element['target_margin']['new'] = 0);
                 if ($element['target_margin']['new'] != $element['target_margin']['old']) {
                     $res[$element['id']]['products.target_margin'] = $element['target_margin']['new'];
                 }
                 //common_price
                 isset($element['common_price']['new']) ? $element['common_price']['new'] = toFloat($element['common_price']['new']) : ($element['common_price']['new'] = 0);
                 if ($element['common_price']['new'] != $element['common_price']['old']) {
                     $res[$element['id']]['products.common_price'] = $element['common_price']['new'];
                 }
                 if (isset($element['GK_enabled']['old'])) {
                     //GK_enabled
                     !isset($element['GK_enabled']['new']) ? $element['GK_enabled']['new'] = 0 : ($element['GK_enabled']['new'] = 1);
                     if ($element['GK_enabled']['new'] != $element['GK_enabled']['old']) {
                         $res[$element['id']]['GK.enabled'] = $element['GK_enabled']['new'];
                     }
                     //GK_yandex_enabled
                     !isset($element['GK_yandex_enabled']['new']) ? $element['GK_yandex_enabled']['new'] = 0 : ($element['GK_yandex_enabled']['new'] = 1);
                     if ($element['GK_yandex_enabled']['new'] != $element['GK_yandex_enabled']['old']) {
                         $res[$element['id']]['GK.yandex_enabled'] = $element['GK_yandex_enabled']['new'];
                     }
                     //GK_price
                     if ($element['GK_price']['new'] != $element['GK_price']['old']) {
                         $res[$element['id']]['GK.price'] = toFloat($element['GK_price']['new']);
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['GK.price'] = $res[$element['id']]['GK.price'] * $child['multiplicator'];
                                 $res[$child['product_id']]['GK.to_export'] = 1;
                             }
                         }
                     }
                     //GK_price_currency
                     if ($element['GK_price_currency']['new'] != $element['GK_price_currency']['old']) {
                         $res[$element['id']]['GK.currency'] = $element['GK_price_currency']['new'];
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['GK.currency'] = $res[$element['id']]['GK.currency'];
                                 $res[$child['product_id']]['GK.to_export'] = 1;
                             }
                         }
                     }
                     //GK update
                     if (isset($res[$element['id']]['products.in_stock']) or isset($res[$element['id']]['GK.enabled']) or isset($res[$element['id']]['GK.yandex_enabled']) or isset($res[$element['id']]['GK.price']) or isset($res[$element['id']]['GK.currency'])) {
                         $res[$element['id']]['GK.to_export'] = 1;
                         $res[$element['id']]['GK.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['GK.user_id'] = Auth::User()->id;
                         $res[$element['id']]['products.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['products.user_id'] = Auth::User()->id;
                     }
                 }
                 if (isset($element['TV_enabled']['old'])) {
                     //TV_enabled
                     !isset($element['TV_enabled']['new']) ? $element['TV_enabled']['new'] = 0 : ($element['TV_enabled']['new'] = 1);
                     if ($element['TV_enabled']['new'] != $element['TV_enabled']['old']) {
                         $res[$element['id']]['TV.enabled'] = $element['TV_enabled']['new'];
                     }
                     //TV_yandex_enabled
                     !isset($element['TV_yandex_enabled']['new']) ? $element['TV_yandex_enabled']['new'] = 0 : ($element['TV_yandex_enabled']['new'] = 1);
                     if ($element['TV_yandex_enabled']['new'] != $element['TV_yandex_enabled']['old']) {
                         $res[$element['id']]['TV.yandex_enabled'] = $element['TV_yandex_enabled']['new'];
                     }
                     //TV_price
                     if ($element['TV_price']['new'] != $element['TV_price']['old']) {
                         $res[$element['id']]['TV.price'] = toFloat($element['TV_price']['new']);
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['TV.price'] = $res[$element['id']]['TV.price'] * $child['multiplicator'];
                                 $res[$child['product_id']]['TV.to_export'] = 1;
                             }
                         }
                     }
                     //TV_price_currency
                     if ($element['TV_price_currency']['new'] != $element['TV_price_currency']['old']) {
                         $res[$element['id']]['TV.currency'] = $element['TV_price_currency']['new'];
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['TV.currency'] = $res[$element['id']]['TV.currency'];
                                 $res[$child['product_id']]['TV.to_export'] = 1;
                             }
                         }
                     }
                     //TV update
                     if (isset($res[$element['id']]['products.in_stock']) or isset($res[$element['id']]['TV.enabled']) or isset($res[$element['id']]['TV.yandex_enabled']) or isset($res[$element['id']]['TV.price']) or isset($res[$element['id']]['TV.currency'])) {
                         $res[$element['id']]['TV.to_export'] = 1;
                         $res[$element['id']]['TV.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['TV.user_id'] = Auth::User()->id;
                         $res[$element['id']]['products.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['products.user_id'] = Auth::User()->id;
                     }
                 }
                 if (isset($element['MK_enabled']['old'])) {
                     //MK_enabled
                     !isset($element['MK_enabled']['new']) ? $element['MK_enabled']['new'] = 0 : ($element['MK_enabled']['new'] = 1);
                     if ($element['MK_enabled']['new'] != $element['MK_enabled']['old']) {
                         $res[$element['id']]['MK.enabled'] = $element['MK_enabled']['new'];
                     }
                     //MK_yandex_enabled
                     !isset($element['MK_yandex_enabled']['new']) ? $element['MK_yandex_enabled']['new'] = 0 : ($element['MK_yandex_enabled']['new'] = 1);
                     if ($element['MK_yandex_enabled']['new'] != $element['MK_yandex_enabled']['old']) {
                         $res[$element['id']]['MK.yandex_enabled'] = $element['MK_yandex_enabled']['new'];
                     }
                     //MK_price
                     if ($element['MK_price']['new'] != $element['MK_price']['old']) {
                         $res[$element['id']]['MK.price'] = toFloat($element['MK_price']['new']);
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['MK.price'] = $res[$element['id']]['MK.price'] * $child['multiplicator'];
                                 $res[$child['product_id']]['MK.to_export'] = 1;
                             }
                         }
                     }
                     //MK_price_currency
                     if ($element['MK_price_currency']['new'] != $element['MK_price_currency']['old']) {
                         $res[$element['id']]['MK.currency'] = $element['MK_price_currency']['new'];
                         if ($childs) {
                             foreach ($childs as $child) {
                                 $res[$child['product_id']]['MK.currency'] = $res[$element['id']]['MK.currency'];
                                 $res[$child['product_id']]['MK.to_export'] = 1;
                             }
                         }
                     }
                     //MK update
                     if (isset($res[$element['id']]['products.in_stock']) or isset($res[$element['id']]['MK.enabled']) or isset($res[$element['id']]['MK.yandex_enabled']) or isset($res[$element['id']]['MK.price']) or isset($res[$element['id']]['MK.currency'])) {
                         $res[$element['id']]['MK.to_export'] = 1;
                         $res[$element['id']]['MK.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['MK.user_id'] = Auth::User()->id;
                         $res[$element['id']]['products.updated_at'] = date('Y-m-d H:i:s');
                         $res[$element['id']]['products.user_id'] = Auth::User()->id;
                     }
                 }
                 //product update
                 if (isset($res[$element['id']]['products.status']) or isset($res[$element['id']]['products.name']) or isset($res[$element['id']]['products.article']) or isset($res[$element['id']]['products.in_stock']) or isset($res[$element['id']]['products.mrc']) or isset($res[$element['id']]['products.mrc_currency']) or isset($res[$element['id']]['products.mrc_relation']) or isset($res[$element['id']]['products.mrc_raise_relation']) or isset($res[$element['id']]['products.price']) or isset($res[$element['id']]['products.price_currency']) or isset($res[$element['id']]['products.price_relation']) or isset($res[$element['id']]['products.price_raise_relation']) or isset($res[$element['id']]['products.target_margin']) or isset($res[$element['id']]['products.common_price'])) {
                     $res[$element['id']]['products.updated_at'] = date('Y-m-d H:i:s');
                     $res[$element['id']]['products.user_id'] = Auth::User()->id;
                 }
             }
         }
         if (count($res) > 0) {
             $productModel->updatingListProducts($res);
             $historyModel->saveHistory('update_products', $res);
             if ($reViewAbsents) {
                 $absentModel = new Absent();
                 $absents = $productModel->getAbsentsList();
                 $absentModel->addAbsentsList($absents);
             }
             Session::flash('message', GetMessages("SUCCESS_DATA_PRODUCTS_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         } else {
             Session::flash('message', GetMessages("ERROR_NO_DATA_FOR_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['update_product'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         //pr($_POST);
         $res = array();
         //status
         !isset($_POST['status']['new']) ? $_POST['status']['new'] = 0 : ($_POST['status']['new'] = 1);
         if ($_POST['status']['new'] != $_POST['status']['old']) {
             $res[$_POST['id']]['products.status'] = $_POST['status']['new'];
         }
         //name
         if ($_POST['name']['new'] != $_POST['name']['old']) {
             $res[$_POST['id']]['products.name'] = trim($_POST['name']['new']);
         }
         //article
         if ($_POST['article']['new'] != $_POST['article']['old']) {
             $res[$_POST['id']]['products.article'] = trim($_POST['article']['new']);
         }
         //ean
         if ($_POST['ean']['new'] != $_POST['ean']['old']) {
             $res[$_POST['id']]['products.ean'] = trim($_POST['ean']['new']);
         }
         //in_stock
         !isset($_POST['in_stock']['new']) ? $_POST['in_stock']['new'] = 0 : ($_POST['in_stock']['new'] = 1);
         if ($_POST['in_stock']['new'] != $_POST['in_stock']['old']) {
             $res[$_POST['id']]['products.in_stock'] = $_POST['in_stock']['new'];
         }
         //mrc
         isset($_POST['mrc']['new']) ? $_POST['mrc']['new'] = toFloat($_POST['mrc']['new']) : ($_POST['mrc']['new'] = 0);
         if ($_POST['mrc']['new'] != $_POST['mrc']['old']) {
             $res[$_POST['id']]['products.mrc'] = toFloat($_POST['mrc']['new']);
         }
         //price
         isset($_POST['price']['new']) ? $_POST['price']['new'] = toFloat($_POST['price']['new']) : ($_POST['price']['new'] = 0);
         if ($_POST['price']['new'] != $_POST['price']['old']) {
             $res[$_POST['id']]['products.price'] = toFloat($_POST['price']['new']);
         }
         //target_margin
         if ($_POST['target_margin']['new'] != $_POST['target_margin']['old']) {
             $res[$_POST['id']]['products.target_margin'] = toFloat($_POST['target_margin']['new']);
         }
         //category_id
         if ($_POST['category_id']['new'] != $_POST['category_id']['old']) {
             $res[$_POST['id']]['products.category_id'] = intval($_POST['category_id']['new']);
         }
         //brand_id
         if ($_POST['brand_id']['new'] != $_POST['brand_id']['old']) {
             $res[$_POST['id']]['products.brand_id'] = intval($_POST['brand_id']['new']);
         }
         //childs
         if (isset($_POST['childs']) and !empty($_POST['childs'])) {
             foreach ($_POST['childs'] as $key => $child) {
                 if (empty($child['product_id']) or empty($child['multiplicator'])) {
                     unset($_POST['childs'][$key]);
                 }
             }
             if (!empty($_POST['childs'])) {
                 $res[$_POST['id']]['products.childs'] = serialize($_POST['childs']);
             } else {
                 $res[$_POST['id']]['products.childs'] = null;
             }
         }
         if (isset($_POST['GK_enabled']['old'])) {
             //GK_id
             isset($_POST['GK_id']['new']) ? $_POST['GK_id']['new'] = intval($_POST['GK_id']['new']) : ($_POST['GK_id']['new'] = 0);
             if ($_POST['GK_id']['new'] != $_POST['GK_id']['old']) {
                 if ($_POST['GK_id']['new'] !== 0 and $_POST['GK_id']['new'] !== 1) {
                     if (count($productModel->checkSiteId('GK', $_POST['GK_id']['new'])) > 0) {
                         Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                         return redirect($_SERVER['HTTP_REFERER']);
                     }
                 }
                 $res[$_POST['id']]['GK.site_id'] = $_POST['GK_id']['new'];
             }
             //GK_enabled
             !isset($_POST['GK_enabled']['new']) ? $_POST['GK_enabled']['new'] = 0 : ($_POST['GK_enabled']['new'] = 1);
             if ($_POST['GK_enabled']['new'] != $_POST['GK_enabled']['old']) {
                 $res[$_POST['id']]['GK.enabled'] = $_POST['GK_enabled']['new'];
             }
             //GK_yandex_enabled
             !isset($_POST['GK_yandex_enabled']['new']) ? $_POST['GK_yandex_enabled']['new'] = 0 : ($_POST['GK_yandex_enabled']['new'] = 1);
             if ($_POST['GK_yandex_enabled']['new'] != $_POST['GK_yandex_enabled']['old']) {
                 $res[$_POST['id']]['GK.yandex_enabled'] = $_POST['GK_yandex_enabled']['new'];
             }
             //GK_price
             isset($_POST['GK_price']['new']) ? $_POST['GK_price']['new'] = toFloat($_POST['GK_price']['new']) : ($_POST['GK_price']['new'] = 0);
             if ($_POST['GK_price']['new'] != $_POST['GK_price']['old']) {
                 $res[$_POST['id']]['GK.price'] = $_POST['GK_price']['new'];
             }
             //GK_price_currency
             if ($_POST['GK_price_currency']['new'] != $_POST['GK_price_currency']['old']) {
                 $res[$_POST['id']]['GK.currency'] = $_POST['GK_price_currency']['new'];
             }
             //GK update
             if (isset($res[$_POST['id']]['products.in_stock']) or isset($res[$_POST['id']]['GK.enabled']) or isset($res[$_POST['id']]['GK.yandex_enabled']) or isset($res[$_POST['id']]['GK.price']) or isset($res[$_POST['id']]['GK.currency'])) {
                 $res[$_POST['id']]['GK.to_export'] = 1;
                 $res[$_POST['id']]['GK.updated_at'] = date('Y-m-d H:i:s');
                 $res[$_POST['id']]['GK.user_id'] = Auth::User()->id;
             }
         }
         if (isset($_POST['TV_enabled']['old'])) {
             //TV_id
             isset($_POST['TV_id']['new']) ? $_POST['TV_id']['new'] = intval($_POST['TV_id']['new']) : ($_POST['TV_id']['new'] = 0);
             if ($_POST['TV_id']['new'] != $_POST['TV_id']['old']) {
                 if ($_POST['TV_id']['new'] !== 0 and $_POST['TV_id']['new'] !== 1) {
                     if (count($productModel->checkSiteId('TV', $_POST['TV_id']['new'])) > 0) {
                         Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                         return redirect($_SERVER['HTTP_REFERER']);
                     }
                 }
                 $res[$_POST['id']]['TV.site_id'] = $_POST['TV_id']['new'];
             }
             //TV_enabled
             !isset($_POST['TV_enabled']['new']) ? $_POST['TV_enabled']['new'] = 0 : ($_POST['TV_enabled']['new'] = 1);
             if ($_POST['TV_enabled']['new'] != $_POST['TV_enabled']['old']) {
                 $res[$_POST['id']]['TV.enabled'] = $_POST['TV_enabled']['new'];
             }
             //TV_yandex_enabled
             !isset($_POST['TV_yandex_enabled']['new']) ? $_POST['TV_yandex_enabled']['new'] = 0 : ($_POST['TV_yandex_enabled']['new'] = 1);
             if ($_POST['TV_yandex_enabled']['new'] != $_POST['TV_yandex_enabled']['old']) {
                 $res[$_POST['id']]['TV.yandex_enabled'] = $_POST['TV_yandex_enabled']['new'];
             }
             //TV_price
             isset($_POST['TV_price']['new']) ? $_POST['TV_price']['new'] = toFloat($_POST['TV_price']['new']) : ($_POST['TV_price']['new'] = 0);
             if ($_POST['TV_price']['new'] != $_POST['TV_price']['old']) {
                 $res[$_POST['id']]['TV.price'] = $_POST['TV_price']['new'];
             }
             //TV_price_currency
             if ($_POST['TV_price_currency']['new'] != $_POST['TV_price_currency']['old']) {
                 $res[$_POST['id']]['TV.currency'] = $_POST['TV_price_currency']['new'];
             }
             //TV update
             if (isset($res[$_POST['id']]['products.in_stock']) or isset($res[$_POST['id']]['TV.enabled']) or isset($res[$_POST['id']]['TV.yandex_enabled']) or isset($res[$_POST['id']]['TV.price']) or isset($res[$_POST['id']]['TV.currency'])) {
                 $res[$_POST['id']]['TV.to_export'] = 1;
                 $res[$_POST['id']]['TV.updated_at'] = date('Y-m-d H:i:s');
                 $res[$_POST['id']]['TV.user_id'] = Auth::User()->id;
             }
         }
         if (isset($_POST['MK_enabled']['old'])) {
             //MK_id
             isset($_POST['MK_id']['new']) ? $_POST['MK_id']['new'] = intval($_POST['MK_id']['new']) : ($_POST['MK_id']['new'] = 0);
             if ($_POST['MK_id']['new'] != $_POST['MK_id']['old']) {
                 if ($_POST['MK_id']['new'] !== 0 and $_POST['MK_id']['new'] !== 1) {
                     if (count($productModel->checkSiteId('MK', $_POST['MK_id']['new'])) > 0) {
                         Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                         return redirect($_SERVER['HTTP_REFERER']);
                     }
                 }
                 $res[$_POST['id']]['MK.site_id'] = $_POST['MK_id']['new'];
             }
             //MK_enabled
             !isset($_POST['MK_enabled']['new']) ? $_POST['MK_enabled']['new'] = 0 : ($_POST['MK_enabled']['new'] = 1);
             if ($_POST['MK_enabled']['new'] != $_POST['MK_enabled']['old']) {
                 $res[$_POST['id']]['MK.enabled'] = $_POST['MK_enabled']['new'];
             }
             //MK_yandex_enabled
             !isset($_POST['MK_yandex_enabled']['new']) ? $_POST['MK_yandex_enabled']['new'] = 0 : ($_POST['MK_yandex_enabled']['new'] = 1);
             if ($_POST['MK_yandex_enabled']['new'] != $_POST['MK_yandex_enabled']['old']) {
                 $res[$_POST['id']]['MK.yandex_enabled'] = $_POST['MK_yandex_enabled']['new'];
             }
             //MK_price
             isset($_POST['MK_price']['new']) ? $_POST['MK_price']['new'] = toFloat($_POST['MK_price']['new']) : ($_POST['MK_price']['new'] = 0);
             if ($_POST['MK_price']['new'] != $_POST['MK_price']['old']) {
                 $res[$_POST['id']]['MK.price'] = $_POST['MK_price']['new'];
             }
             //MK_price_currency
             if ($_POST['MK_price_currency']['new'] != $_POST['MK_price_currency']['old']) {
                 $res[$_POST['id']]['MK.currency'] = $_POST['MK_price_currency']['new'];
             }
             //MK update
             if (isset($res[$_POST['id']]['products.in_stock']) or isset($res[$_POST['id']]['MK.enabled']) or isset($res[$_POST['id']]['MK.yandex_enabled']) or isset($res[$_POST['id']]['MK.price']) or isset($res[$_POST['id']]['MK.currency'])) {
                 $res[$_POST['id']]['MK.to_export'] = 1;
                 $res[$_POST['id']]['MK.updated_at'] = date('Y-m-d H:i:s');
                 $res[$_POST['id']]['MK.user_id'] = Auth::User()->id;
             }
         }
         //product update
         if (isset($res[$_POST['id']]['products.status']) or isset($res[$_POST['id']]['products.name']) or isset($res[$_POST['id']]['products.article']) or isset($res[$_POST['id']]['products.ean']) or isset($res[$_POST['id']]['products.in_stock']) or isset($res[$_POST['id']]['products.mrc']) or isset($res[$_POST['id']]['products.price']) or isset($res[$_POST['id']]['products.target_margin']) or isset($res[$_POST['id']]['products.category_id']) or isset($res[$_POST['id']]['products.brand_id'])) {
             $res[$_POST['id']]['products.updated_at'] = date('Y-m-d H:i:s');
             $res[$_POST['id']]['products.user_id'] = Auth::User()->id;
         }
         if (count($res) > 0 or isset($arLabels)) {
             $productModel->updatingListProducts($res);
             $historyModel->saveHistory('update_products', $res);
             Session::flash('message', GetMessages("SUCCESS_DATA_PRODUCTS_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         } else {
             Session::flash('message', GetMessages("ERROR_NO_DATA_FOR_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['update_labels'])) {
         $label = trim($_POST['labels']['new']);
         if ($label != $_POST['labels']['old']) {
             $labelModel->joinLabels($_POST['product_id'], explode(',', $_POST['labels']['new']));
             Session::flash('message', GetMessages("SUCCESS_DATA_PRODUCTS_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         } else {
             Session::flash('message', GetMessages("ERROR_NO_DATA_FOR_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['add_to_site'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         //pr($_POST);
         if (isset($_POST['GK'])) {
             if (!isset($_POST['GK']['product_id']) or strlen($_POST['GK']['product_id']) < 1) {
                 Session::flash('message', GetMessages("ERROR"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             if (!empty($_POST['GK']['site_id']) and count($productModel->checkSiteId('GK', $_POST['GK']['site_id'])) > 0) {
                 Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             $res = array();
             $res['product_id'] = $_POST['GK']['product_id'];
             !empty($_POST['GK']['site_id']) ? $res['site_id'] = intval($_POST['GK']['site_id']) : ($res['site_id'] = 1);
             isset($_POST['GK']['enabled']) ? $res['enabled'] = 1 : ($res['enabled'] = 0);
             isset($_POST['GK']['yandex_enabled']) ? $res['yandex_enabled'] = 1 : ($res['yandex_enabled'] = 0);
             !empty($_POST['GK']['currency']) ? $res['currency'] = $_POST['GK']['currency'] : ($res['currency'] = 'RUB');
             !empty($_POST['GK']['price']) ? $res['price'] = toFloat($_POST['GK']['price']) : ($res['price'] = 0);
             $res['created_at'] = date('Y-m-d H:i:s');
             $res['updated_at'] = date('Y-m-d H:i:s');
             $res['user_id'] = Auth::User()->id;
             $productModel->addToSite($res, 'GK');
             $historyModel->saveHistory('add_to_site', $res['product_id'], 'ГарантКомфорт');
             Session::flash('message', GetMessages("SUCCESS_PRODUCT_ADD_TO_SITE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
         if (isset($_POST['TV'])) {
             if (!isset($_POST['TV']['product_id']) or strlen($_POST['TV']['product_id']) < 1) {
                 Session::flash('message', GetMessages("ERROR"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             if (!empty($_POST['TV']['site_id']) and count($productModel->checkSiteId('TV', $_POST['TV']['site_id'])) > 0) {
                 Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             $res = array();
             $res['product_id'] = $_POST['TV']['product_id'];
             !empty($_POST['TV']['site_id']) ? $res['site_id'] = intval($_POST['TV']['site_id']) : ($res['site_id'] = 1);
             isset($_POST['TV']['enabled']) ? $res['enabled'] = 1 : ($res['enabled'] = 0);
             isset($_POST['TV']['yandex_enabled']) ? $res['yandex_enabled'] = 1 : ($res['yandex_enabled'] = 0);
             !empty($_POST['TV']['currency']) ? $res['currency'] = $_POST['TV']['currency'] : ($res['currency'] = 'RUB');
             !empty($_POST['TV']['price']) ? $res['price'] = toFloat($_POST['TV']['price']) : ($res['price'] = 0);
             $res['created_at'] = date('Y-m-d H:i:s');
             $res['updated_at'] = date('Y-m-d H:i:s');
             $res['user_id'] = Auth::User()->id;
             $productModel->addToSite($res, 'TV');
             $historyModel->saveHistory('add_to_site', $res['product_id'], 'Таваго');
             Session::flash('message', GetMessages("SUCCESS_PRODUCT_ADD_TO_SITE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
         if (isset($_POST['MK'])) {
             if (!isset($_POST['MK']['product_id']) or strlen($_POST['MK']['product_id']) < 1) {
                 Session::flash('message', GetMessages("ERROR"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             if (!empty($_POST['MK']['site_id']) and count($productModel->checkSiteId('MK', $_POST['MK']['site_id'])) > 0) {
                 Session::flash('message', GetMessages("ERROR_SITE_ID_EXISTS"));
                 return redirect($_SERVER['HTTP_REFERER']);
             }
             $res = array();
             $res['product_id'] = $_POST['MK']['product_id'];
             !empty($_POST['MK']['site_id']) ? $res['site_id'] = intval($_POST['MK']['site_id']) : ($res['site_id'] = 1);
             isset($_POST['MK']['enabled']) ? $res['enabled'] = 1 : ($res['enabled'] = 0);
             isset($_POST['MK']['yandex_enabled']) ? $res['yandex_enabled'] = 1 : ($res['yandex_enabled'] = 0);
             !empty($_POST['MK']['currency']) ? $res['currency'] = $_POST['MK']['currency'] : ($res['currency'] = 'RUB');
             !empty($_POST['MK']['price']) ? $res['price'] = toFloat($_POST['MK']['price']) : ($res['price'] = 0);
             $res['created_at'] = date('Y-m-d H:i:s');
             $res['updated_at'] = date('Y-m-d H:i:s');
             $res['user_id'] = Auth::User()->id;
             $productModel->addToSite($res, 'MK');
             $historyModel->saveHistory('add_to_site', $res['product_id'], 'МаксКлимат');
             Session::flash('message', GetMessages("SUCCESS_PRODUCT_ADD_TO_SITE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['add_purchase'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         //pr($_POST);
         $res = array();
         $res['vendor_id'] = $_POST['provider'];
         $res['product_id'] = $_POST['product_id'];
         $res['currency'] = $_POST['currency'];
         !empty($_POST['base_price']) ? $res['base_price'] = toFloat($_POST['base_price']) : ($res['base_price'] = 0);
         !empty($_POST['discount_price']) ? $res['discount_price'] = toFloat($_POST['discount_price']) : ($res['discount_price'] = 0);
         !empty($_POST['raise']) ? $res['raise'] = toFloat($_POST['raise']) : ($res['raise'] = 0);
         !empty($_POST['discount_bulk']) ? $res['discount_bulk'] = toFloat($_POST['discount_bulk']) : ($res['discount_bulk'] = 0);
         $res['created_at'] = date('Y-m-d H:i:s');
         $res['updated_at'] = date('Y-m-d H:i:s');
         $res['user_id'] = Auth::User()->id;
         $purchaseModel->addPurchase($res);
         $historyModel->saveHistory('add_purchase', $res['product_id']);
         Session::flash('message', GetMessages("SUCCESS_ADD_PURCHASE"));
         return redirect($_SERVER['HTTP_REFERER']);
     }
     if (isset($_POST['update_purchase'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         unset($_POST['_token']);
         unset($_POST['update_purchase']);
         //pr($_POST);
         $res = array();
         foreach ($_POST as $element) {
             //base_price
             if (isset($element['base_price'])) {
                 if ($element['base_price']['new'] != $element['base_price']['old']) {
                     $res[$element['purchase_id']]['base_price'] = $element['base_price']['new'];
                 }
             } else {
                 $res[$element['purchase_id']]['base_price'] = 0;
             }
             //currency
             if (isset($element['currency'])) {
                 if ($element['currency']['new'] != $element['currency']['old']) {
                     $res[$element['purchase_id']]['currency'] = $element['currency']['new'];
                 }
             } else {
                 $res[$element['purchase_id']]['currency'] = 'RUB';
             }
             //raise
             if (isset($element['raise'])) {
                 if ($element['raise']['new'] != $element['raise']['old']) {
                     $res[$element['purchase_id']]['raise'] = $element['raise']['new'];
                 }
             } else {
                 $res[$element['purchase_id']]['raise'] = 1;
             }
             //discount_price
             if (isset($element['discount_price'])) {
                 if ($element['discount_price']['new'] != $element['discount_price']['old']) {
                     $res[$element['purchase_id']]['discount_price'] = $element['discount_price']['new'];
                 }
             } else {
                 $res[$element['purchase_id']]['discount_price'] = 0;
             }
             //discount_bulk
             if (isset($element['discount_bulk'])) {
                 if ($element['discount_bulk']['new'] != $element['discount_bulk']['old']) {
                     $res[$element['purchase_id']]['discount_bulk'] = $element['discount_bulk']['new'];
                 }
             } else {
                 $res[$element['purchase_id']]['discount_bulk'] = 0;
             }
             if (isset($res[$element['purchase_id']]['base_price']) or isset($res[$element['purchase_id']]['currency']) or isset($res[$element['purchase_id']]['raise']) or isset($res[$element['purchase_id']]['discount_price']) or isset($res[$element['purchase_id']]['discount_bulk'])) {
                 $res[$element['purchase_id']]['product_id'] = $element['product_id'];
                 $res[$element['purchase_id']]['purchase.updated_at'] = date('Y-m-d H:i:s');
                 $res[$element['purchase_id']]['purchase.user_id'] = Auth::User()->id;
             }
         }
         //pr($res);
         if (count($res) > 0) {
             $purchaseModel->updatePurchase($res);
             $historyModel->saveHistory('update_purchase', $res);
             Session::flash('message', GetMessages("SUCCESS_DATA_PRODUCTS_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         } else {
             Session::flash('message', GetMessages("ERROR_NO_DATA_FOR_UPDATE"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['create_product'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         unset($_POST['_token']);
         unset($_POST['create_product']);
         //pr($_POST);
         if (strlen($request->input('name')) > 0) {
             $res = array();
             $res['name'] = $request->input('name');
             $res['article'] = $request->input('article');
             $res['ean'] = $request->input('ean');
             $res['status'] = isset($_POST['status']) ? 1 : 0;
             $res['category_id'] = $request->input('category_id');
             $res['brand_id'] = $request->input('brand_id');
             $res['mrc'] = empty($_POST['mrc']) ? 0 : $_POST['mrc'];
             $res['mrc_currency'] = empty($_POST['mrc_currency']) ? 'RUB' : $_POST['mrc_currency'];
             $res['price'] = empty($_POST['price']) ? 0 : $_POST['price'];
             $res['price_currency'] = empty($_POST['price_currency']) ? 'RUB' : $_POST['price_currency'];
             $res['target_margin'] = $request->input('target_margin');
             $res['in_stock'] = isset($_POST['in_stock']) ? 1 : 0;
             $res['user_id'] = Auth::User()->id;
             $res['created_at'] = date('Y-m-d H:i:s');
             $res['updated_at'] = date('Y-m-d H:i:s');
             $id = $productModel->createProduct($res);
             $historyModel->saveHistory('create_product', $id);
             Session::flash('message', GetMessages("SUCCESS_CREATE_NEW_PRODUCT"));
             return redirect()->route('product.show', ['id' => $id]);
         } else {
             Session::flash('message', GetMessages("ERROR_EMPTY_NAME_PRODUCT"));
             return redirect($_SERVER['HTTP_REFERER']);
         }
     }
     if (isset($_POST['ajax'])) {
         if (!right('EditProduct')) {
             abort(404);
         }
         return $productModel->checkSiteId($_POST['site'], $_POST['id']);
     }
     if (isset($_POST['cp_page_sbmt'])) {
         $userModel->where('id', Auth::User()->id)->update(array('count_products' => $_POST['cp_page_val']));
         Session::flash('message', GetMessages("SUCCESS_UPDATE"));
         return redirect($_SERVER['HTTP_REFERER']);
     }
 }