<?php
namespace Modules\Admin\KamSalesTargets; class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing { protected $listOrderingColumns = ['year']; protected function initialize() { parent::initialize(); $this->actionsCfg['insert'] = ['checkSection' => true, 'checkOption' => false]; $this->actionsCfg['update'] = ['checkSection' => true, 'checkOption' => false]; $this->actionsCfg['delete'] = ['checkSection' => true, 'checkOption' => false]; } protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection) { switch ($option->getId()) { case 'insert': case 'update': case 'delete': break; default: return false; } return true; } protected function optionGetEntityName($id) { return $id; } protected function insert_getDefaultFormData($R) { return [ 'salesTarget' => [ 1 => '0.00', 2 => '0.00', 3 => '0.00', 4 => '0.00', 5 => '0.00', 6 => '0.00', 7 => '0.00', 8 => '0.00', 9 => '0.00', 10 => '0.00', 11 => '0.00', 12 => '0.00', ], 'contactsTarget' => [ 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0, ], ]; } protected function insert_doInsert($formData, \App\MessagesCollector $msgs) { $model = new Model(); \DB::startTransaction(); try { foreach ($formData['salesTarget'] as $month=>$salesTarget) { $model->insertKamMonthTargets($formData['idKam']['id'], $formData['year'], $month, $formData['salesTarget'][$month], $formData['contactsTarget'][$month]); } $msgs->addSuccess($this->lang['messages']['insertSuccess']); $this->insertIdRecord = sprintf("%s-%s", $formData['idKam']['id'], $formData['year']); \DB::commitTransaction(); return true; } catch (\App\MessagesException $ex) { \DB::rollbackTransaction(); $msgs->appendMessages($ex->getMessagesCollector()); } } protected function update_getDefaultFormData($idEntity) { list($idKam, $year) = explode("-", $idEntity); $model = new Model(); $res = $model->getKamYearTargets($idKam, $year); return [ 'idKam' => $idKam, 'year' => $year, 'salesTarget' => [ 1 => $res[1]['salesTarget'], 2 => $res[2]['salesTarget'], 3 => $res[3]['salesTarget'], 4 => $res[4]['salesTarget'], 5 => $res[5]['salesTarget'], 6 => $res[6]['salesTarget'], 7 => $res[7]['salesTarget'], 8 => $res[8]['salesTarget'], 9 => $res[9]['salesTarget'], 10 => $res[10]['salesTarget'], 11 => $res[11]['salesTarget'], 12 => $res[12]['salesTarget'], ], 'contactsTarget' => [ 1 => $res[1]['contactsTarget'], 2 => $res[2]['contactsTarget'], 3 => $res[3]['contactsTarget'], 4 => $res[4]['contactsTarget'], 5 => $res[5]['contactsTarget'], 6 => $res[6]['contactsTarget'], 7 => $res[7]['contactsTarget'], 8 => $res[8]['contactsTarget'], 9 => $res[9]['contactsTarget'], 10 => $res[10]['contactsTarget'], 11 => $res[11]['contactsTarget'], 12 => $res[12]['contactsTarget'], ], ]; } protected function update_doUpdate($formData, \App\MessagesCollector $msgs) { list($idKam, $year) = explode("-", $formData['id']); $model = new Model(); \DB::startTransaction(); try { foreach ($formData['salesTarget'] as $month=>$salesTarget) { $model->updateKamMonthTargets($idKam, $year, $month, $formData['salesTarget'][$month], $formData['contactsTarget'][$month]); } $msgs->addSuccess($this->lang['messages']['updateSuccess']); $this->updateIdRecord = sprintf("%s-%s", $idKam, $year); \DB::commitTransaction(); return true; } catch (\App\MessagesException $ex) { \DB::rollbackTransaction(); $msgs->appendMessages($ex->getMessagesCollector()); } } protected function delete_doDelete($recordsIds, \App\MessagesCollector $msgs, $R) { list($idKam, $year) = explode("-", $recordsIds[0]); $model = new Model(); \DB::startTransaction(); try { for ($m=1; $m<=12; $m++) { $model->deleteKamMonthTargets($idKam, $year, $m); } \DB::commitTransaction(); $this->getListingSelection()->delete($recordsIds); $msgs->addSuccess($this->lang['messages']['deleteSuccess']); } catch (\App\MessagesException $ex) { \DB::rollbackTransaction(); $msgs->appendMessages($ex->getMessagesCollector()); } } }