Total Complexity | 63 |
Total Lines | 393 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CopyModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CopyModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class CopyModel extends Controller |
||
42 | { |
||
43 | const MODEL_NAMESPACE = '\\FacturaScripts\\Dinamic\\Model\\'; |
||
44 | const TEMPLATE_ASIENTO = 'CopyAsiento'; |
||
45 | const TEMPLATE_PRODUCTO = 'CopyProducto'; |
||
46 | |||
47 | /** @var CodeModel */ |
||
48 | public $codeModel; |
||
49 | |||
50 | /** @var object */ |
||
51 | public $model; |
||
52 | |||
53 | /** @var string */ |
||
54 | public $modelClass; |
||
55 | |||
56 | /** @var string */ |
||
57 | public $modelCode; |
||
58 | |||
59 | public function getPageData(): array |
||
60 | { |
||
61 | $data = parent::getPageData(); |
||
62 | $data['menu'] = 'sales'; |
||
63 | $data['title'] = 'copy'; |
||
64 | $data['icon'] = 'fas fa-cut'; |
||
65 | $data['showonmenu'] = false; |
||
66 | return $data; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param Response $response |
||
71 | * @param User $user |
||
72 | * @param ControllerPermissions $permissions |
||
73 | */ |
||
74 | public function privateCore(&$response, $user, $permissions) |
||
75 | { |
||
76 | parent::privateCore($response, $user, $permissions); |
||
77 | $this->codeModel = new CodeModel(); |
||
78 | |||
79 | $action = $this->request->get('action'); |
||
80 | if ($action === 'autocomplete') { |
||
81 | $this->autocompleteAction(); |
||
82 | return; |
||
83 | } elseif (false === $this->loadModel()) { |
||
84 | Tools::log()->warning('record-not-found'); |
||
85 | return; |
||
86 | } |
||
87 | |||
88 | // si no es un documento de compra o venta, cargamos su plantilla |
||
89 | switch ($this->modelClass) { |
||
90 | case 'Asiento': |
||
91 | $this->setTemplate(self::TEMPLATE_ASIENTO); |
||
92 | break; |
||
93 | |||
94 | case 'Producto': |
||
95 | $this->setTemplate(self::TEMPLATE_PRODUCTO); |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | if (false === $this->pipeFalse('before', $this->model)) { |
||
|
|||
100 | return; |
||
101 | } |
||
102 | |||
103 | $this->title .= ' ' . $this->model->primaryDescription(); |
||
104 | if ($action === 'save') { |
||
105 | switch ($this->modelClass) { |
||
106 | case 'AlbaranCliente': |
||
107 | case 'FacturaCliente': |
||
108 | case 'PedidoCliente': |
||
109 | case 'PresupuestoCliente': |
||
110 | $this->saveSalesDocument(); |
||
111 | break; |
||
112 | |||
113 | case 'AlbaranProveedor': |
||
114 | case 'FacturaProveedor': |
||
115 | case 'PedidoProveedor': |
||
116 | case 'PresupuestoProveedor': |
||
117 | $this->savePurchaseDocument(); |
||
118 | break; |
||
119 | |||
120 | case 'Asiento': |
||
121 | $this->saveAccountingEntry(); |
||
122 | break; |
||
123 | |||
124 | case 'Producto': |
||
125 | $this->saveProduct(); |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | protected function autocompleteAction(): void |
||
132 | { |
||
133 | $this->setTemplate(false); |
||
134 | $results = []; |
||
135 | $data = $this->request->request->all(); |
||
136 | foreach ($this->codeModel->search($data['source'], $data['fieldcode'], $data['fieldtitle'], $data['term']) as $value) { |
||
137 | $results[] = [ |
||
138 | 'key' => Tools::fixHtml($value->code), |
||
139 | 'value' => Tools::fixHtml($value->description) |
||
140 | ]; |
||
141 | } |
||
142 | |||
143 | $this->response->setContent(json_encode($results)); |
||
144 | } |
||
145 | |||
146 | protected function loadModel(): bool |
||
147 | { |
||
148 | $this->modelClass = $this->request->get('model'); |
||
149 | $this->modelCode = $this->request->get('code'); |
||
150 | if (empty($this->modelClass) || empty($this->modelCode)) { |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | $className = self::MODEL_NAMESPACE . $this->modelClass; |
||
155 | $this->model = new $className(); |
||
156 | return $this->model->loadFromCode($this->modelCode); |
||
157 | } |
||
158 | |||
159 | protected function saveDocumentEnd(BusinessDocument $newDoc): void |
||
160 | { |
||
161 | $lines = $newDoc->getLines(); |
||
162 | if (false === Calculator::calculate($newDoc, $lines, true)) { |
||
163 | Tools::log()->warning('record-save-error'); |
||
164 | $this->dataBase->rollback(); |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | $this->dataBase->commit(); |
||
169 | Tools::log()->notice('record-updated-correctly'); |
||
170 | $this->redirect($newDoc->url() . '&action=save-ok'); |
||
171 | } |
||
172 | |||
173 | protected function saveAccountingEntry(): void |
||
231 | } |
||
232 | |||
233 | protected function savePurchaseDocument(): void |
||
234 | { |
||
235 | if (false === $this->validateFormToken()) { |
||
291 | } |
||
292 | |||
293 | protected function saveSalesDocument(): void |
||
294 | { |
||
295 | if (false === $this->validateFormToken()) { |
||
296 | return; |
||
297 | } |
||
298 | |||
299 | // buscamos el cliente |
||
300 | $subject = new Cliente(); |
||
301 | if (false === $subject->loadFromCode($this->request->request->get('codcliente'))) { |
||
302 | Tools::log()->warning('record-not-found'); |
||
303 | return; |
||
304 | } |
||
305 | |||
306 | $this->dataBase->beginTransaction(); |
||
307 | |||
308 | // creamos el nuevo documento |
||
309 | $className = self::MODEL_NAMESPACE . $this->modelClass; |
||
310 | $newDoc = new $className(); |
||
311 | $newDoc->setAuthor($this->user); |
||
312 | $newDoc->setSubject($subject); |
||
313 | $newDoc->codalmacen = $this->request->request->get('codalmacen'); |
||
314 | $newDoc->setCurrency($this->model->coddivisa); |
||
315 | $newDoc->codpago = $this->request->request->get('codpago'); |
||
316 | $newDoc->codserie = $this->request->request->get('codserie'); |
||
317 | $newDoc->dtopor1 = (float)$this->request->request->get('dtopor1', 0); |
||
318 | $newDoc->dtopor2 = (float)$this->request->request->get('dtopor2', 0); |
||
319 | $newDoc->setDate($this->request->request->get('fecha'), $this->request->request->get('hora')); |
||
320 | $newDoc->numero2 = $this->request->request->get('numero2'); |
||
321 | $newDoc->observaciones = $this->request->request->get('observaciones'); |
||
322 | |||
323 | if (false === $this->pipeFalse('beforeSaveSales', $newDoc)) { |
||
324 | $this->dataBase->rollback(); |
||
325 | return; |
||
326 | } |
||
327 | |||
328 | if (false === $newDoc->save()) { |
||
329 | Tools::log()->warning('record-save-error'); |
||
330 | $this->dataBase->rollback(); |
||
331 | return; |
||
332 | } |
||
333 | |||
334 | // copiamos las líneas del documento |
||
335 | foreach ($this->model->getLines() as $line) { |
||
336 | $newLine = $newDoc->getNewLine($line->toArray()); |
||
337 | |||
338 | if (false === $this->pipeFalse('beforeSaveSalesLine', $newLine)) { |
||
339 | $this->dataBase->rollback(); |
||
340 | return; |
||
341 | } |
||
342 | |||
343 | if (false === $newLine->save()) { |
||
344 | Tools::log()->warning('record-save-error'); |
||
345 | $this->dataBase->rollback(); |
||
346 | return; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | $this->saveDocumentEnd($newDoc); |
||
351 | } |
||
352 | |||
353 | protected function saveProduct(): void |
||
434 | } |
||
435 | } |
||
436 |