load->database(); $this->load->helper('facility_helper'); } public function get_deliveries_alert($site_id) { $now = date('Y-m-d H:i:s'); $one_minute_later = date('Y-m-d H:i:s', strtotime($now) - (60 * 1)); $this->db->where('request_open_dt <=', $now); $this->db->where('request_open_dt >=', $one_minute_later); $this->db->where('site_id', $site_id); return $this->db->get('product_client_requests')->result(); } public function save_order_field($array) { foreach ($array as $campo) { $this->db->where('field_id', $campo['field_id']); $this->db->update('product_fields_options', $campo); } return; } public function get_sizes_prices($category_id, $site_id, $product_id){ //$this->db->select('(select price from product_size_price where product_id = ' .$product_id . ') as price)'); if (! empty($product_id)) { $this->db->select('product_size.*, product_size_price.price, product_size_price.id AS price_id'); $this->db->where('product_size.site_id', $site_id); $this->db->where('product_size.category_id', $category_id); $this->db->order_by('product_size.id'); $this->db->join('product_size_price', 'product_size_price.size_id = product_size.id AND product_size_price.product_id ='.$product_id, 'left outer'); } else { $this->db->select('product_size.*'); $this->db->where('product_size.site_id', $site_id); $this->db->where('product_size.category_id', $category_id); $this->db->order_by('product_size.id'); } // $resul = $this->db->get('product_size')->result(); // echo $this->db->last_query(); // print_r($resul); // exit; return $this->db->get('product_size')->result(); } public function insert_price_size($array_price){ Return $this->db->insert('product_size_price', $array_price); } public function save_new_size($data){ $this->db->insert('product_size', $data); return $this->db->insert_id(); } public function update_size($id_size, $array){ $this->db->where('id', $id_size); return $this->db->update('product_size', $array); } public function update_price_size($id_price, $array_price){ $this->db->where('id', $id_price); return $this->db->update('product_size_price', $array_price); } public function save_new_price_size($price_size){ return $this->db->insert('product_size_price', $price_size); } public function update_delivery($id, $status, $nome){ $array = [ 'request_name_delivery' => $nome, ]; //echo $status;exit; //Só atualiza os dados abaixo se estiverem em branco no BD $this->db->where('request_id', $id); $result = $this->db->get('product_client_requests')->row(); switch ($status) { case('Recebido'): if($result->request_received == '') { $array['request_received'] = date('Y-m-d H:i:s'); $array['request_status'] = $status; } break; case('Enviado'): if($result->request_sent == '') { $array['request_sent'] = date('Y-m-d H:i:s'); $array['request_status'] = $status; } break; case('Concluido'): if($result->request_finished == '') { $array['request_finished'] = date('Y-m-d H:i:s'); $array['request_status'] = $status; } break; } //print_r($array);exit; $this->db->where('request_id', $id); $this->db->update('product_client_requests', $array); return; } public function insert_options($data){ $this->db->insert('product_fields_options', $data); return $this->db->insert_id(); } public function get_field_by_id($field_id) { $this->db->where('field_id', $field_id); return $this->db->get('product_fields_options')->row(); } public function save_edit_options ($array){ $this->db->where('field_id', $array['field_id']); $this->db->update('product_fields_options', $array); return; } public function get_product_fields_options($prod_id) { $this->db->where('prod_id', $prod_id); $this->db->order_by('ordem', 'ASC'); return $this->db->get('product_fields_options')->result(); } public function delete_field($field_id){ $this->db->where('field_id', $field_id); return $this->db->delete('product_fields_options'); } public function update_tax_delivery($user_id, $site_id, $delivery_tax, $free_tax_minimum_cost, $free_tax){ $params = array( 'delivery_tax' => $delivery_tax, 'date_update' => date('Y-m-d H:i:s'), 'free_tax_minimum_cost' => $free_tax_minimum_cost, 'free_tax' => $free_tax, ); $this->db->where('idUsuario', $user_id); $this->db->where('id', $site_id); $this->db->update('site2', $params); return; } public function insert($data, $user_id, $site_id) { $this->load->library('utils'); $data->price = $this->utils->correct_money_from_br($data->price); $params = array( 'user_id' => $user_id, 'site_id' => $site_id, 'category_id' => ( $data->category != '' ? $data->category : null ), 'description' => ucfirst($data->description), 'adicional_info' => $data->adicional_info, 'price' => $data->price, 'label' => $data->label, 'date_insert' => date('Y-m-d H:i:s'), 'date_update' => date('Y-m-d H:i:s') ); $this->db->insert('product_service', $params); return $this->db->insert_id(); } public function update($data, $id_user) { if (!isset($data->id) || !$id_user) { return false; } $this->load->library('utils'); $data->price = $this->utils->correct_money_from_br($data->price); $params = array( 'category_id' => ( $data->category != '' ? $data->category : null ), 'description' => ucfirst($data->description), 'adicional_info' => $data->adicional_info, 'price' => $data->price, 'label' => $data->label, 'active' => $data->active, ); $this->db->where('id', $data->id); $this->db->where('user_id', $id_user); $this->db->where('site_id', $data->site_id); $update = $this->db->update('product_service', $params); if (isset($data->prod_img_ord[$data->id])) { foreach ($data->prod_img_ord[$data->id] as $id_img => $k) { $this->db->where('site_id', $data->site_id); $this->db->where('id', $id_img); $this->db->update('product_service_img', ['ord' => $k]); } } return $update; } public function delete($id, $site_id) { $this->db->select('id'); $searchImg = $this->db->get_where('product_service_img', array('site_id' => $site_id, 'product_service_id' => $id)); foreach ($searchImg->result() as $im) { $this->delete_img($im->id, $site_id); } return $this->db->delete('product_service', array('id' => $id, 'site_id' => $site_id)); } public function get_list_by_user($id_user, $limit = null, $start = 0) { $this->db->select('produtoServico_old.*, categoria.nome as categoria'); $this->db->join('categoria', 'categoria.id = produtoServico_old.idCategoria', 'left'); $this->db->order_by('produtoServico_old.descricao', 'ASC'); if ($limit != null) { $this->db->limit($limit, $start); } $search = $this->db->get_where('produtoServico_old', array('produtoServico_old.idUsuario' => $id_user)); return $search->result(); } public function get_by_site2($site_id, $prod_id = null, $opts = []) { $this->load->library('utils_file'); $this->load->library('tool_image'); $this->db->select('site2.categoria as categoria, product_service.*, category.id as category_id, category.name as category, category.accept_half'); //product_service_img.id as img_id, product_service_img.path as path_img'); $this->db->join('site2', 'site2.id = product_service.site_id'); //$this->db->join('product_service_img', 'product_service_img.product_service_id = product_service.id', 'left'); $this->db->join('category', 'category.id = product_service.category_id', 'left'); $where = array('site2.id' => $site_id); if (isset($opts['enabled_venda']) && $opts['enabled_venda'] == 'S') { $where['product_service.enable_venda'] = 'S'; $where['product_service.active'] = 'Y'; } if (isset($opts['active']) && $opts['active'] == 'Y') { $where['product_service.active'] = 'Y'; } if (isset($opts['in_ids']) && count($opts['in_ids'])) { $this->db->where_in('product_service.id', $opts['in_ids']); } if ($prod_id != null) { $where['product_service.id'] = $prod_id; } $this->db->order_by("product_service.id", "asc"); //$this->db->order_by("product_service_img.ord", "asc"); $this->db->where($where); #echo $this->db->get_compiled_select("product_service"); //if ($prod_id) // return $this->db->get_where('product_service', $where)->row(); $search = $this->db->get_where('product_service', $where)->result(); if(isset($opts['get_img']) && $opts['get_img']) { foreach ($search as $p) { $params = new stdClass(); $params->site_id = $p->site_id; $params->prod_id = $p->id; if(isset($opts['size_img'])) { $params->size_img = $opts['size_img']; } $p->img_list = $this->get_img_list($params); } } if ($prod_id) { return $search[0]; } $result = []; if(isset($opts['categorize']) && $opts['categorize']) { foreach($search as $p){ $result[$p->category_id]['name'] = $p->category; $result[$p->category_id]['products'][$p->id] = $p; } return $result; } return $search; } public function get_list_by_site($site_id, $limit = null, $start = 0) { $this->db->select('product_service.*, category.name as category'); $this->db->join('category', 'category.id = product_service.category_id', 'left'); $this->db->order_by('product_service.description', 'ASC'); if ($limit != null) { $this->db->limit($limit, $start); } $search = $this->db->get_where('product_service', array('product_service.site_id' => $site_id)); return $search->result(); } public function get_by_site($id_site, $id_prod = null, $opts = []) { $this->db->select('idUsuario as id_user'); $search = $this->db->get_where('site', array('id' => $id_site)); if ($search->row() == null) return false; return $this->get_by_user($search->row()->id_user, $id_prod, $opts); } public function get_by_user($id_user, $id_prod = null, $opts = []) { $this->load->library('utils_file'); $this->load->library('tool_image'); $this->db->select('produtoServico_old.*, categoria.id as id_categoria, categoria.nome as categoria, imgProd.id as id_img, imgProd.name as nome_img, imgProd.type as tipo_img'); $this->db->join('usuario', 'usuario.id = produtoServico_old.idUsuario'); $this->db->join('imgProd', 'imgProd.idProdServ = produtoServico_old.id', 'left'); $this->db->join('categoria', 'categoria.id = produtoServico_old.idCategoria', 'left'); $where = array('usuario.id' => $id_user); if (isset($opts['enabled_venda']) && $opts['enabled_venda'] == 'S') { $where['produtoServico_old.enable_venda'] = 'S'; $where['produtoServico_old.active'] = 'Y'; } if (isset($opts['active']) && $opts['active'] == 'Y') { $where['produtoServico_old.active'] = 'Y'; } if (isset($opts['in_ids']) && count($opts['in_ids'])) { $this->db->where_in('produtoServico_old.id', $opts['in_ids']); } if ($id_prod != null) { $where['produtoServico_old.id'] = $id_prod; } $this->db->order_by("produtoServico_old.id", "asc"); $this->db->order_by("imgProd.ord", "asc"); $search = $this->db->get_where('produtoServico_old', $where); $list = array(); if ($id_prod != null) { $list["img_list"] = array(); $list["img_list_resized"] = array(); foreach ($search->result() as $i) { $list['id'] = $i->id; $list['name'] = $i->descricao; $list['preco'] = $i->preco; $list['id_categoria'] = $i->idCategoria; $list['info_adicional'] = $i->infoAdicional; $list['data_cadastro'] = $i->dataCadastro; $list['peso'] = $i->peso; $list['altura'] = $i->altura; $list['largura'] = $i->largura; $list['comprimento'] = $i->comprimento; $list['enable_pac'] = $i->enable_pac; $list['enable_sedex'] = $i->enable_sedex; $list['enable_venda'] = $i->enable_venda; $list['controlar_estoque'] = $i->controlar_estoque; $list['qtde_estoque'] = $i->qtde_estoque; $list['active'] = $i->active; $list['unidade_venda'] = 'unidade'; /* if ($i->img_principal != null) { if ($i->nome_img != '' && $i->tipo_img != '' && $i->id_img == $i->img_principal) { $t = explode('/', $i->tipo_img); $t = end($t); $list["img_list"][$i->id_img]["file_img"] = $i->nome_img . '.' . $t; } } else { if ($i->nome_img != '' && $i->tipo_img != '') { $t = explode('/', $i->tipo_img); $t = end($t); if (!isset($list["img_list"][$i->id_img]["file_img"])) { $list["img_list"][$i->id_img]["file_img"] = $i->nome_img . '.' . $t; } } } * */ if ($i->nome_img != '' && $i->tipo_img != '') { $t = explode('/', $i->tipo_img); $t = end($t); $list["img_list"][$i->id_img]["file_img"] = $this->utils_file->get_sub_path_img($i->nome_img . '.' . $t); $list["img_list"][$i->id_img]["file_img_100x100"] = $this->tool_image->resize($i->nome_img . '.' . $t, 100, 100); $list["img_list_resized"][$i->id_img]['_100x100'] = $this->tool_image->resize($i->nome_img . '.' . $t, 100, 100); $list["img_list_resized"][$i->id_img]['_220x170'] = $this->tool_image->resize($i->nome_img . '.' . $t, 220, 170); $list["img_list_resized"][$i->id_img]['_450x560'] = $this->tool_image->resize($i->nome_img . '.' . $t, 450, 560); } } $list = array_to_object($list); return $list; } # para nao incluir o id categoria nas keys do array $sort_by_category = true; if (isset($opts['sort_by_category'])) { if ($opts['sort_by_category'] == 'S') $sort_by_category = true; else if ($opts['sort_by_category'] == 'N') $sort_by_category = false; } $img_default_set = false; $tmp_id = null; foreach ($search->result() as $i) { if ($sort_by_category) { if ($i->categoria == null) { $i->id_categoria = 0; $i->categoria = ''; } $list[$i->id_categoria]["nome"] = $i->categoria; $list[$i->id_categoria]["itens"][$i->id]["id"] = $i->id; $list[$i->id_categoria]["itens"][$i->id]["descricao"] = $i->descricao; $list[$i->id_categoria]["itens"][$i->id]["info_adicional"] = $i->infoAdicional; $list[$i->id_categoria]["itens"][$i->id]["preco"] = $i->preco; $list[$i->id_categoria]["itens"][$i->id]["peso"] = $i->peso; $list[$i->id_categoria]["itens"][$i->id]["largura"] = $i->largura; $list[$i->id_categoria]["itens"][$i->id]["altura"] = $i->altura; $list[$i->id_categoria]["itens"][$i->id]["comprimento"] = $i->comprimento; $list[$i->id_categoria]["itens"][$i->id]["tipos_envios"] = []; if ($i->enable_pac == 'S') { $list[$i->id_categoria]["itens"][$i->id]["tipos_envios"][] = ['name' => 'PAC']; } if ($i->enable_sedex == 'S') { $list[$i->id_categoria]["itens"][$i->id]["tipos_envios"][] = ['name' => 'SEDEX']; } } else { $list['itens'][$i->id]["id"] = $i->id; $list['itens'][$i->id]["descricao"] = $i->descricao; $list['itens'][$i->id]["info_adicional"] = $i->infoAdicional; $list['itens'][$i->id]["preco"] = $i->preco; $list['itens'][$i->id]["peso"] = $i->peso; $list['itens'][$i->id]["largura"] = $i->largura; $list['itens'][$i->id]["altura"] = $i->altura; $list['itens'][$i->id]["comprimento"] = $i->comprimento; $list['itens'][$i->id]["tipos_envios"] = []; if ($i->enable_pac == 'S') { $list["itens"][$i->id]["tipos_envios"][] = ['name' => 'PAC']; } if ($i->enable_sedex == 'S') { $list["itens"][$i->id]["tipos_envios"][] = ['name' => 'SEDEX']; } } if ($i->id != $tmp_id) $img_default_set = false; if ($sort_by_category) { if (!isset($list[$i->id_categoria]["itens"][$i->id]["file_img"])) $list[$i->id_categoria]["itens"][$i->id]["file_img"] = ''; } else { if (!isset($list["itens"][$i->id]["file_img"])) $list["itens"][$i->id]["file_img"] = ''; } if ($i->nome_img != '' && $i->tipo_img != '') { #$i->nome_img = $this->utils_file->get_sub_path_img($i->nome_img); $t = explode('/', $i->tipo_img); $t = end($t); if ($i->id_img == $i->img_principal) { if ($sort_by_category) $list[$i->id_categoria]["itens"][$i->id]["file_img"] = $this->utils_file->get_sub_path_img($i->nome_img . '.' . $t); else $list['itens'][$i->id]["file_img"] = $this->utils_file->get_sub_path_img($i->nome_img . '.' . $t); } else { if ($sort_by_category) { if ($list[$i->id_categoria]["itens"][$i->id]["file_img"] == '') $list[$i->id_categoria]["itens"][$i->id]["file_img"] = $this->utils_file->get_sub_path_img($i->nome_img . '.' . $t); } else { if ($list['itens'][$i->id]["file_img"] == '') $list['itens'][$i->id]["file_img"] = $this->utils_file->get_sub_path_img($i->nome_img . '.' . $t); } } #if ( $this->input->server('REMOTE_ADDR') == '189.18.32.153' ) { if ($sort_by_category) { #$list[$i->id_categoria]['itens'][$i->id]["img_resized"]['_100x100'] = $this->tool_image->resize($i->nome_img . '.' . $t,100,100); $list[$i->id_categoria]['itens'][$i->id]["img_resized"]['_220x170'] = $this->tool_image->resize($i->nome_img . '.' . $t, 220, 170); $list[$i->id_categoria]['itens'][$i->id]["img_resized"]['_262x321'] = $this->tool_image->resize($i->nome_img . '.' . $t, 262, 321); } else { #$list['itens'][$i->id]["img_resized"]['_100x100'] = $this->tool_image->resize($i->nome_img . '.' . $t,100,100); $list['itens'][$i->id]["img_resized"]['_220x170'] = $this->tool_image->resize($i->nome_img . '.' . $t, 220, 170); $list['itens'][$i->id]["img_resized"]['_262x321'] = $this->tool_image->resize($i->nome_img . '.' . $t, 262, 321); } #} } $tmp_id = $i->id; } $list = array_to_object($list); return $list; } public function insert_img($data, &$error = array()) { if (!isset($data->content) || !isset($data->name)) { $error[] = "Não recebido conteúdo da imagem"; return false; } $num_ord = 1; $search_ord = $this->db->query('select max(ord) as max ' . 'from product_service_img ' . 'where site_id = ? and product_service_id = ? ', [$data->site_id, $data->product_id]); if ($search_ord->row() != null) { $num_ord = $search_ord->row()->max + 1; } $t = explode('/',$data->type); $type = end($t); $params = array( 'site_id' => $data->site_id, 'product_service_id' => $data->product_id, 'path' => $data->name.'.'.$type, 'ord' => $num_ord, 'date' => date('Y-m-d H:i:s'), ); return $this->db->insert('product_service_img', $params); } public function delete_img($id, $site_id) { $this->load->library('utils_file'); $this->db->select('path'); $search = $this->db->get_where('product_service_img', array('id' => $id, 'site_id' => $site_id)); if ($search->row() == null) return false; $file_name = $search->row()->path; if ($this->db->delete('product_service_img', array('id' => $id, 'site_id' => $site_id))) { $this->utils_file->delete_img($file_name); } } # VENDA public function update_info_venda($data, $site_id) { $params = array( 'weight' => $this->utils->correct_weight_from_br($data->peso), 'height' => $data->altura, 'width' => $data->largura, 'length' => $data->comprimento, 'enable_pac' => $data->enable_pac, 'enable_sedex' => $data->enable_sedex, 'enable_venda' => $data->enable_venda, ); #print_r ( $data ); #print_r ( $params ); #exit(); $this->db->where('site_id', $site_id); $this->db->where('id', $data->id); return $this->db->update('product_service', $params); } public function get_profits($site_id) { $array = array(); ///Ganhos totais $this->db->where('site_id', $site_id); //$this->db->where('request_status', 'Concluido'); $result = $this->db->get('product_client_requests')->result(); $total = "0.00"; //print_r($result);exit; foreach ($result as $valor) { $total += $valor->request_total; } $array['total']= 'R$ ' . number_format($total, 2, ',', '.'); ///Ganhos no dia $this->db->where('site_id', $site_id); //$this->db->where('request_status', 'Concluido'); $this->db->where('request_open_dt >= "' . date('Y-m-d' . ' 00:00:00') . '"'); $this->db->where('request_open_dt <= "' . date('Y-m-d' . ' 23:59:59') . '"'); $result2 = $this->db->get('product_client_requests')->result(); $total_dia = "0.00"; foreach ($result2 as $valor) { $total_dia += $valor->request_total; } $array['hoje']= 'R$ ' . number_format($total_dia, 2, ',', '.'); ///Ganhos no mês $this->db->where('site_id', $site_id); //$this->db->where('request_status', 'Concluido'); $this->db->where('request_open_dt >=', date('Y-m-01')); $this->db->where('request_open_dt <=', date("Y-m-t")); $result3 = $this->db->get('product_client_requests')->result(); $total_mes = "0.00"; foreach ($result3 as $valor) { $total_mes += $valor->request_total; } $array['mes']= 'R$ ' . number_format($total_mes, 2, ',', '.'); return $array; } public function get_total($site_id) { $this->db->select('count(id) as total'); return $this->db->get_where('product_service', ['site_id' => $site_id])->row()->total; } public function get_total_deliveries($site_id) { $this->db->select('count(request_id) as total'); return $this->db->get_where('product_client_requests', ['site_id' => $site_id])->row()->total; } public function get_delivery_by_id($id) { $this->db->where('request_id', $id); return $this->db->get('product_client_requests')->row(); } public function get_deliveries($site_id, $limit = null, $start = 0) { $this->db->limit($limit, $start); $this->db->order_by('request_open_dt', 'DESC'); $this->db->where('site_id', $site_id); $result = $this->db->get('product_client_requests')->result(); //echo $this->db->last_query();exit; return $result; } public function get_clients($client_id){ $this->db->select('prod_client_name, prod_client_num, prod_client_rua, ' . 'prod_client_telefone, prod_client_ponto_ref, prod_client_bairro,' . 'prod_client_cep, prod_client_cidade, prod_client_email, ' . 'prod_client_estado'); $this->db->where('prod_client_id', $client_id); $result = $this->db->get('product_client_site')->row(); //echo $this->db->last_query(); //print_r($result);exit; return $result; } public function get_img_list($data) { $this->load->library('tool_image'); $this->load->library('utils_file'); $imgs = $this->db->get_where('product_service_img', ['product_service_id' => $data->prod_id, 'site_id' => $data->site_id]); if ($imgs->result() == null) return []; $list = []; $x = 0; foreach ($imgs->result() as $i) { //$t = explode('/', $i->type); //$t = end($t); //$filename = $i->name . '.' . $t; $list[$x]['id'] = $i->id; $list[$x]['file'] = $this->utils_file->get_sub_path_img($i->path); $list[$x]['ord'] = $i->ord; if(isset($data->size_img)) { $_size = explode('x',$data->size_img); $list[$x]['_'.$data->size_img] = $this->tool_image->resize($i->path, $_size[0], $_size[1]); } else $list[$x]['_100x100'] = $this->tool_image->resize($i->path, 100, 100); $x++; } return $list; } public function update_cover($id_prod, $id_cover, $id_user) { $this->db->where('id', $id_prod); $this->db->where('idUsuario', $id_user); $this->db->update('produtoServico_old', ['img_principal' => $id_cover]); return true; } }