Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class VendaController extends AppController { |
||
| 8 | |||
| 9 | View Code Duplication | public function pdv() { |
|
| 34 | |||
| 35 | public function recuperar_dados_venda_ajax() { |
||
| 57 | |||
| 58 | View Code Duplication | public function listar_cadastros() { |
|
| 59 | $this->layout = 'wadmin'; |
||
| 60 | |||
| 61 | $this->set('vendas', $this->Venda->find('all', |
||
| 62 | array('conditions' => |
||
| 63 | array( |
||
| 64 | 'ativo' => 1, |
||
| 65 | 'id_usuario' => $this->instancia, |
||
| 66 | 'orcamento' => 0 |
||
| 67 | ) |
||
| 68 | ) |
||
| 69 | ) |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | View Code Duplication | public function adicionar_cadastro() { |
|
| 98 | |||
| 99 | public function conveter_venda($vendaId) { |
||
| 100 | $this->layout = 'wadmin'; |
||
| 101 | |||
| 102 | $this->loadModel('Cliente'); |
||
| 103 | |||
| 104 | $this->set('clientes', $this->Cliente->find('all', |
||
| 105 | array('conditions' => |
||
| 106 | array('ativo' => 1, |
||
| 107 | 'id_usuario' => $this->instancia |
||
| 108 | ) |
||
| 109 | ) |
||
| 110 | ) |
||
| 111 | ); |
||
| 112 | |||
| 113 | $this->loadModel('Produto'); |
||
| 114 | |||
| 115 | $this->set('produtos', $this->Produto->find('all', |
||
| 116 | array('conditions' => |
||
| 117 | array('ativo' => 1, |
||
| 118 | 'id_usuario' => $this->instancia |
||
| 119 | ) |
||
| 120 | ) |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | |||
| 124 | $this->set('venda', $this->Venda->find('all', |
||
| 125 | array('conditions' => |
||
| 126 | array( |
||
| 127 | 'Venda.ativo' => 1, |
||
| 128 | 'Venda.id' => $vendaId, |
||
| 129 | 'id_usuario' => $this->instancia |
||
| 130 | ) |
||
| 131 | ) |
||
| 132 | ) |
||
| 133 | ); |
||
| 134 | |||
| 135 | $this->loadModel('VendaItensProduto'); |
||
| 136 | |||
| 137 | $venda_produtos = $this->VendaItensProduto->find('all', |
||
| 138 | array('conditions' => |
||
| 139 | array( |
||
| 140 | 'VendaItensProduto.ativo' => 1, |
||
| 141 | 'VendaItensProduto.id' => $vendaId |
||
| 142 | ) |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | |||
| 146 | $this->loadModel('Produto'); |
||
| 147 | |||
| 148 | $produtos = []; |
||
| 149 | foreach ($venda_produtos as $i => $venda_produto) |
||
| 150 | { |
||
| 151 | $produto = $this->Produto->find('all', |
||
| 152 | array('conditions' => |
||
| 153 | array( |
||
| 154 | 'Produto.ativo' => 1, |
||
| 155 | 'Produto.id' => $venda_produto['VendaItensProduto']['produto_id'] |
||
| 156 | ) |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | |||
| 160 | View Code Duplication | if ($produto[0]['Produto']['estoque'] <= 0) |
|
| 161 | { |
||
| 162 | $this->Session->setFlash('O produto (' . $produto[0]['Produto']['nome'] .') não tem mais estoque disponivel!'); |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | $produtos[$i] = $produto[0]['Produto']; |
||
| 167 | $produtos[$i]['quantidade'] = $venda_produto['VendaItensProduto']['quantidade_produto']; |
||
| 168 | |||
| 169 | $total = $produtos[$i]['preco'] * $venda_produto['VendaItensProduto']['quantidade_produto']; |
||
| 170 | |||
| 171 | $produtos[$i]['preco'] = number_format($produtos[$i]['preco'], 2, ',', '.'); |
||
| 172 | $produtos[$i]['total'] = number_format($total, 2, ',', '.'); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->set('venda_produtos', $produtos); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function s_adicionar_cadastro() { |
||
| 201 | |||
| 202 | View Code Duplication | public function calcular_valor_venda($produtos) { |
|
| 218 | |||
| 219 | View Code Duplication | public function calcular_custo_venda($produtos) { |
|
| 220 | $this->loadModel('Produto'); |
||
| 221 | |||
| 222 | (float) $custo = 0.00; |
||
| 223 | foreach ($produtos as $indice => $item) { |
||
| 224 | $produto = $this->Produto->find('all', |
||
| 225 | array('conditions' => |
||
| 226 | array('Produto.id' => $item['id_produto']) |
||
| 227 | ) |
||
| 228 | ); |
||
| 229 | |||
| 230 | $custo += $produto[0]['Produto']['custo'] * $item['quantidade']; |
||
| 231 | } |
||
| 232 | |||
| 233 | return $custo; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function validar_itens_venda($produtos) { |
||
| 255 | |||
| 256 | public function salvar_venda($produtos, $lancamento, $informacoes, $usuario_id) { |
||
| 257 | unset($informacoes['id_cliente']); |
||
| 258 | |||
| 259 | $informacoes['data_venda'] = date('Y-m-d'); |
||
| 260 | $informacoes['id_usuario'] = $this->instancia != 'winners' ? $this->instancia : $usuario_id; |
||
| 261 | $informacoes['ativo'] = 1; |
||
| 262 | $informacoes['desconto'] = (float) $informacoes['desconto']; |
||
| 263 | $informacoes['valor'] = $informacoes['valor'] - $informacoes['desconto']; |
||
| 264 | $informacoes['orcamento'] = $informacoes['orcamento']; |
||
| 265 | |||
| 266 | if (!$this->Venda->save($informacoes)) { |
||
| 267 | $this->Session->setFlash('Ocorreu algum erro ao salvar a venda'); |
||
| 268 | return false; |
||
| 269 | } |
||
| 270 | |||
| 271 | $id_venda = $this->Venda->getLastInsertId(); |
||
| 272 | |||
| 273 | $objVendaItensProdutoController = new VendaItensProdutoController(); |
||
| 274 | if ($objVendaItensProdutoController->adicionar_itens_venda($id_venda, $produtos, $informacoes['orcamento']) === false) { |
||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | $objLancamentoVendasController = new LancamentoVendasController(); |
||
| 279 | if ($objLancamentoVendasController->salvar_lancamento($id_venda, $lancamento, $informacoes['valor'], $informacoes['id_usuario']) === false) { |
||
| 280 | return false; |
||
| 281 | } |
||
| 282 | |||
| 283 | return array('status' => true, 'id' => $id_venda); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function relatorio_diario() { |
||
| 343 | |||
| 344 | public function recoverDataToDashboardOneWeek(){ |
||
| 345 | $vendas = $this->Venda->find('all', |
||
| 346 | array('conditions' => |
||
| 367 | |||
| 368 | View Code Duplication | public function excluir_cadastro() { |
|
| 382 | |||
| 383 | } |
||
| 384 |
This check looks for function calls that miss required arguments.