Esempio n. 1
0
 /**
  * Page where users can add new products.
  * 
  * @author unknown, Christophe
  */
 public function products()
 {
     if (!$this->store_id) {
         $this->session->set_flashdata('error_msg', 'Error: Store not selected.');
         redirect('/');
     }
     if ($this->role_id != 2) {
         $this->session->set_flashdata('error_msg', 'Error: Your account does not have access to this item.');
         redirect('/');
         exit;
     }
     $this->javascript('views/add_products.js.php');
     $this->data->error = '';
     $this->data->product_count = 1;
     $this->data->product_method = $this->input->post('product_method') == FALSE ? 'bycsv' : $this->input->post('product_method');
     $this->data->file_processing = false;
     //var_dump($this->data->product_method); exit();
     $this->data->title = $this->input->post('title');
     $this->data->upc_code = $this->input->post('upc_code');
     $this->data->sku = $this->input->post('sku');
     $this->data->retail_price = $this->input->post('retail_price');
     $this->data->wholesale_price = $this->input->post('wholesale_price');
     $this->data->price_floor = $this->input->post('price_floor');
     if ($this->input->post('product_method') == 'byform') {
         // this will be an array of x products...
         $this->data->product_count = sizeof($this->data->title);
         $rules = array(array('field' => 'title[]', 'label' => 'Title', 'rules' => 'trim|required'), array('field' => 'upc_code[]', 'label' => 'UPC Code', 'rules' => 'trim|required'), array('field' => 'retail_price[]', 'label' => 'Retail Price', 'rules' => 'trim|required'), array('field' => 'price_floor[]', 'label' => 'Price Floor', 'rules' => 'trim|required'));
         $this->form_validation->set_rules($rules);
         $this->form_validation->set_fields();
         if ($this->form_validation->run()) {
             for ($i = 0; $i < $this->data->product_count; $i++) {
                 $existing_product_id = $this->Product->check_product_exist($this->store_id, $this->data->upc_code[$i], $this->data->sku[$i]);
                 if (!$existing_product_id) {
                     $data = array('store_id' => $this->store_id, 'title' => $this->data->title[$i], 'upc_code' => $this->data->upc_code[$i], 'brand' => '', 'sku' => $this->data->sku[$i], 'retail_price' => $this->data->retail_price[$i], 'price_floor' => $this->data->price_floor[$i], 'is_tracked' => 1, 'created_at' => date('Y-m-d H:i:s'), 'wholesale_price' => $this->data->wholesale_price[$i], 'status' => 1);
                     $newProdId = $this->Product->add_product($data);
                     $iT = time();
                     if (isset($data['retail_price'])) {
                         $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'retail_price', 'pricing_value' => $data['retail_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                     if (isset($data['wholesale_price'])) {
                         $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'wholesale_price', 'pricing_value' => $data['wholesale_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                     if (isset($data['price_floor'])) {
                         $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'price_floor', 'pricing_value' => $data['price_floor'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                 } else {
                     $data = array('title' => $this->data->title[$i], 'upc_code' => $this->data->upc_code[$i], 'brand' => '', 'sku' => $this->data->sku[$i], 'retail_price' => $this->data->retail_price[$i], 'price_floor' => $this->data->price_floor[$i], 'is_tracked' => 1, 'wholesale_price' => $this->data->wholesale_price[$i], 'created_at' => date('Y-m-d H:i:s'), 'status' => 1);
                     $this->Product->update_product($existing_product_id, $data);
                     $newProdId = $existing_product_id;
                     $iT = time();
                     if (isset($data['retail_price'])) {
                         $this->db->where('product_id', $newProdId);
                         $this->db->where('pricing_type', 'retail_price');
                         $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['retail_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                     if (isset($data['wholesale_price'])) {
                         $this->db->where('product_id', $newProdId);
                         $this->db->where('pricing_type', 'wholesale_price');
                         $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['wholesale_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                     if (isset($data['price_floor'])) {
                         $this->db->where('product_id', $newProdId);
                         $this->db->where('pricing_type', 'price_floor');
                         $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['price_floor'], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                     }
                 }
             }
             redirect(base_url() . 'catalog');
         } else {
             //we're going back to the form to fix error...
             $this->data->error = 'There is an error with one or more of your products. The title, UPC, Retail &amp; MAP price values are required.';
         }
     } elseif ($this->input->post('product_method') == 'bycsv') {
         if ($_FILES['csv_file']['tmp_name']) {
             $config['upload_path'] = $this->config->item('uploaded_files') . 'brand_csv_uploads/' . $this->store_id;
             $brand_file = $this->config->item('uploaded_files') . 'brand_csv_uploads';
             $config['allowed_types'] = 'csv';
             $config['overwrite'] = true;
             $config['remove_spaces'] = true;
             //$config['file_name'] = $_FILES['csv_file']['name'];
             //if(!is_dir($brand_file)) mkdir($brand_file, 0777);
             if (!is_dir($config['upload_path'])) {
                 mkdir($config['upload_path'], 0777, true);
                 chmod($config['upload_path'], 0777);
             }
             $this->load->library('upload', $config);
             if ($this->upload->do_upload('csv_file')) {
                 //we've posted a file
                 ini_set("auto_detect_line_endings", "1");
                 $this->data->file_name = $this->upload->file_name;
                 //$config['file_name'];
                 $this->data->file_processing = true;
                 $dataArray = $headerArray = array();
                 $has_header = $this->input->post("has_header");
                 // Does user want to archive all existing products in system that are NOT contained in this CSV file?
                 $archive_products = $this->input->post("archive_products");
                 $this->data->has_header = $has_header;
                 $this->data->archive_products = $archive_products;
                 $this->data->headerColumns = array();
                 $handle = fopen($config['upload_path'] . '/' . $this->data->file_name, "r");
                 while (($data = fgetcsv($handle, 9999999, ",")) !== FALSE) {
                     $dataArray[] = $data;
                     if (count($data) > count($this->data->headerColumns)) {
                         $this->data->headerColumns = $data;
                     }
                 }
                 fclose($handle);
                 if ($this->data->has_header) {
                     if (isset($dataArray[0])) {
                         if (count($dataArray[0]) < count($this->data->headerColumns)) {
                             $missing_indexes = count($this->data->headerColumns) - count($dataArray[0]);
                             $this->data->headerColumns = array_merge($dataArray[0], array_fill(count($dataArray[0]), $missing_indexes, ''));
                         } else {
                             $this->data->headerColumns = $dataArray[0];
                         }
                     }
                     $var_to_match = '';
                     for ($i = 0, $n = count($this->data->headerColumns); $i < $n; $i++) {
                         if ($n - 1 == $i) {
                             $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]);
                         } else {
                             $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]) . '&';
                         }
                     }
                     $headerArray = generateHeaderArray($var_to_match);
                     if ($headerArray != '') {
                         $headerArray = generateHeaderPostArray($headerArray);
                     } else {
                         $headerArray = generateHeaderPostArray($var_to_match);
                     }
                 }
                 $this->data->dataArray = $dataArray;
                 $this->data->headerArray = $headerArray;
                 $this->javascript('views/import.js.php');
             } else {
                 $this->data->error = $this->upload->display_errors();
             }
         } else {
             $this->data->error = 'No file was uploaded.';
         }
     }
 }
Esempio n. 2
0
 function products($store_id = null)
 {
     $this->data->store_id = $this->store_id;
     $this->data->dataArray = array();
     $this->error = '';
     $brand = $this->Store->get_brand_by_store($this->store_id);
     if ($this->input->post('rdProduct') === 'byform') {
         $title = $this->input->post('title');
         $upc_code = $this->input->post('upc_code');
         $sku = $this->input->post('sku');
         $retail_price = $this->input->post('retail_price');
         $price_floor = $this->input->post('price_floor');
         $wholesale_price = $this->input->post('wholesale_price');
         $added_item = FALSE;
         $errors = array();
         for ($i = 0, $n = count($sku); $i < $n; $i++) {
             if (!$this->Product->product_exists_by_upc($upc_code[$i])) {
                 // product not in system, add it
                 $iT = time();
                 $data = array('store_id' => $this->store_id, 'title' => $title[$i], 'upc_code' => $upc_code[$i], 'brand' => $brand, 'sku' => $sku[$i], 'wholesale_price' => $wholesale_price[$i], 'retail_price' => $retail_price[$i], 'price_floor' => $price_floor[$i], 'created_at' => date('Y-m-d H:i:s', $iT));
                 $newProdId = $this->Product->add_product($data);
                 $added_item = ($added_item or $newProdId);
                 $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'retail_price', 'pricing_value' => $retail_price[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                 $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'wholesale_price', 'pricing_value' => $wholesale_price[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
                 $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'price_floor', 'pricing_value' => $price_floor[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT)));
             } else {
                 // product in system, generate notice
                 $errors[] = $upc_code[$i] . ' is already in our database.';
             }
         }
         if ($added_item) {
             $this->session->set_flashdata("success", 'Product(s) successfully added.');
         }
         if (count($errors)) {
             $this->session->set_flashdata('error', implode("\n<br>", $errors));
         }
         redirect(base_url() . 'catalog/catalog_add/' . $this->store_id);
     } elseif ($this->input->post('rdProduct') === 'bycsv') {
         array_push($this->javascript_files, 'views/import.php');
         $headerArray = array();
         if ($_FILES['file']['tmp_name']) {
             $ext = explode('.', $_FILES['file']['name']);
             $ext = $ext[count($ext) - 1];
             if ($ext === 'csv') {
                 ini_set("auto_detect_line_endings", "1");
                 $theFileSize = $_FILES['file']['size'];
                 if ($theFileSize < 6 * 1024 * 1024) {
                     $path = $this->config->item('csv_upload_path');
                     move_uploaded_file($_FILES['file']['tmp_name'], $path . $_FILES['file']['name']);
                     $extArray = explode('.', $_FILES['file']['tmp_name']);
                     $ext = $extArray[count($extArray) - 1];
                     if (strtolower($ext) != 'csv') {
                         $this->error = false;
                     }
                     $handle = fopen($path . $_FILES['file']['name'], "r");
                     $check = 0;
                     $dataArray = array();
                     $this->data->filename = $_FILES['file']['name'];
                     $this->data->hasHeader = 0;
                     $this->data->headerColumns = array();
                     $hasHeader = $this->input->post("hasHeader");
                     $this->data->hasHeader = $hasHeader;
                     while (($data = fgetcsv($handle, 9999999, ",")) !== FALSE) {
                         $dataArray[] = $data;
                         if (count($data) > count($this->data->headerColumns)) {
                             $this->data->headerColumns = $data;
                         }
                     }
                     fclose($handle);
                     if ($hasHeader) {
                         if (isset($dataArray[0])) {
                             if (count($dataArray[0]) < count($this->data->headerColumns)) {
                                 $missing_indexes = count($this->data->headerColumns) - count($dataArray[0]);
                                 $this->data->headerColumns = array_merge($dataArray[0], array_fill(count($dataArray[0]), $missing_indexes, ''));
                             } else {
                                 $this->data->headerColumns = $dataArray[0];
                             }
                         }
                         $var_to_match = '';
                         for ($i = 0, $n = count($this->data->headerColumns); $i < $n; $i++) {
                             if ($n - 1 == $i) {
                                 $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]);
                             } else {
                                 $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]) . '&';
                             }
                         }
                         $headerArray = generateHeaderArray($var_to_match);
                         if ($headerArray != '') {
                             $headerArray = generateHeaderPostArray($headerArray);
                         } else {
                             $headerArray = generateHeaderPostArray($var_to_match);
                         }
                     }
                     $this->data->dataArray = $dataArray;
                     $this->data->headerArray = $headerArray;
                     $this->load->view('front/merchant/import', $this->data);
                 } else {
                     $this->session->set_flashdata('error', 'Please upload file size must be less than 6MB.');
                     redirect(base_url() . 'catalog/products/');
                 }
             } else {
                 $this->session->set_flashdata('error', 'Please provide your catalog in csv format.');
                 redirect(base_url() . 'catalog/products/');
             }
         } else {
             //echo "last else";exit;
             $this->error = false;
         }
         if ($this->error) {
             echo 'error';
             exit;
             redirect(base_url() . 'dashboard');
         }
     } else {
         array_push($this->javascript_files, 'views/import.php');
         array_push($this->javascript_files, 'views/add_products.php');
         $this->load->view('front/merchant/add_products', $this->data);
     }
 }