legacy_DEALS_SALES(); SE_Layout::dol(); } public function typespecialAction() { $DBG = ('1' == V::get('DBG', '', $_REQUEST)); header("Content-type: application/json"); $fld = V::get('fld', '', $_GET); switch ($fld) { case 'id_telboxes': { Lib::loadClass('TypespecialVariable'); $typeSpecialTelboxes = TypespecialVariable::getInstance(-1, '__DEALS_SALES_TELBOXES_NAME'); $query = V::get('q', '', $_REQUEST); $rawRows = null; $rows = $typeSpecialTelboxes->getValuesWithExports($query); if($DBG){echo'
rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'';}
foreach ($rows as $kID => $vItem) {
$itemJson = new stdClass();
$itemJson->id = $vItem->id;
$itemJson->name = $vItem->param_out;
if (!empty($vItem->exports)) {
$itemJson->exports = $vItem->exports;
}
$jsonData[] = $itemJson;
}
echo json_encode($jsonData);
}
break;
case 'id_companies': {
Lib::loadClass('TypespecialVariable');
$typeSpecialCompanies = TypespecialVariable::getInstance(-1, '__COMPANIES');
$query = V::get('q', '', $_REQUEST);
$rawRows = null;
$rows = $typeSpecialCompanies->getValuesWithExports($query);
if($DBG){echo'rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'';}
foreach ($rows as $kID => $vItem) {
$itemJson = new stdClass();
$itemJson->id = $vItem->id;
$itemJson->name = $vItem->param_out;
if (!empty($vItem->exports)) {
$itemJson->exports = $vItem->exports;
}
$jsonData[] = $itemJson;
}
echo json_encode($jsonData);
}
break;
}
}
public function getDataAjaxAction() {
header("Content-type: application/json");
$response = new stdClass();
$response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;
echo json_encode($response);
}
public function inlineEditAjaxAction() {
//header("Content-type: application/json");
$response = new stdClass();
$response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;// TODO: DBG
$response->_request = $_REQUEST;// TODO: DBG
$year = V::get('year', 0, $_REQUEST, 'int');
$month = V::get('month', 0, $_REQUEST, 'int');
$telbox = V::get('telbox', '', $_REQUEST, 'word');
if ($year <= 0) throw new Exception('Error: wrong year');
if ($month <= 0 || $month > 12) throw new Exception('Error: wrong month');
if (empty($telbox)) throw new Exception('Error: wrong telbox');
$record = $this->_fetchSaleByParams($telbox, $year, $month);
if (empty($record)) {
return $this->_renderInlineCreateForm($telbox, $year, $month);
} else {
return $this->_renderInlineEditForm($record);
}
}
public function saveInlineAjaxAction() {
header("Content-type: application/json");
$response = new stdClass();
$response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;
$response->_request = $_REQUEST;// TODO: DBG
$id = V::get('ID', 0, $_REQUEST, 'int');
if ($id > 0) {
return $this->_saveInlineEdit($id, $_REQUEST);
} else {
return $this->_saveInlineCreate($_REQUEST);
}
}
private function _saveInlineEdit($id, $args) {
$record = $this->_fetchSaleById($id);
if (empty($record)) throw new HttpException("Record {$id} not exists", 404);
DBG::_('DBG', '>2', "record", $record, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
DBG::_('DBG', '>2', "args", $args, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
$tblAcl = $this->_getTableAcl();
$primaryKeyFieldName = 'ID';
$primaryKey = V::get($primaryKeyFieldName, 0, $record);
$itemPatch = new stdClass();
$itemPatch->{$primaryKeyFieldName} = $primaryKey;
$visibleCols = array('SALES_VALUE', 'marka', 'A_STATUS');
foreach ($visibleCols as $fieldName) {
$fieldID = $tblAcl->getFieldIdByName($fieldName);
if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
if (!$tblAcl->isAllowed($fieldID, 'W', $record)) {
throw new Exception("Brak uprawnień do zapisu ({$fieldName})");
}
$argsFieldName = "f{$fieldID}";
if (!isset($args[$argsFieldName])) throw new Exception("Brak danych dla pola ({$fieldName})");
$itemPatch->{$fieldName} = $args[$argsFieldName];
}
DBG::_('DBG', '>1', "TODO: save record", $itemPatch, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
$response = new stdClass();
try {
$affected = $tblAcl->updateItem($itemPatch);
if ($affected > 0) {
$response->type = 'success';
$response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
} else if ($affected == 0) {
$response->type = 'info';
$response->msg = "Nie wprowadzono żadnych zmian";
}
$response->record = $tblAcl->getItem($primaryKey);
}
catch (Exception $e) {
$response->type = 'error';
$response->msg = $e->getMessage();
}
echo json_encode($response);
}
private function _saveInlineCreate($args) {
$year = V::get('SALES_YEAR', 0, $args, 'int');
$month = V::get('SALES_MONTH', 0, $args, 'int');
$telbox = V::get('T_TELBOX_NEIGHBOUR_IN', '', $args, 'word');
if ($year <= 0) throw new Exception('Error: wrong year');
if ($month <= 0 || $month > 12) throw new Exception('Error: wrong month');
if (empty($telbox)) throw new Exception('Error: wrong telbox');
$tblAcl = $this->_getTableAcl();
$primaryKeyFieldName = 'ID';
$primaryKey = V::get($primaryKeyFieldName, 0, $record);
$item = new stdClass();//$tblAcl->convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f');
$item->T_TELBOX_NEIGHBOUR_IN = $telbox;
$item->SALES_YEAR = $year;
$item->SALES_MONTH = $month;
$visibleCols = array('SALES_VALUE', 'marka');
foreach ($visibleCols as $fieldName) {
$fieldID = $tblAcl->getFieldIdByName($fieldName);
if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
if (!$tblAcl->isAllowed($fieldID, 'C', $record)) {
throw new Exception("Brak uprawnień do utworzenia ({$fieldName})");
}
$argsFieldName = "f{$fieldID}";
if (!isset($args[$argsFieldName])) throw new Exception("Brak danych dla pola ({$fieldName})");
$item->{$fieldName} = $args[$argsFieldName];
}
try {
$createdId = $tblAcl->addItem($item);
if ($createdId) {
$response->type = 'success';
$response->msg = "Utworzono pomyślnie rekord nr {$createdId}";
$response->id = $createdId;
$response->record = $tblAcl->getItem($createdId);
}
else {
$response->type = 'error';
$response->msg = "Nie udało się utworzyć nowego rekordu!";
}
}
catch (Exception $e) {
$response->type = 'error';
$response->msg = "Wystąpiły błędy!";
$response->msg = $e->getMessage();
}
echo json_encode($response);
}
private function _fetchSaleByParams($telbox, $year, $month) {
$record = null;
$db = DB::getDB();
$sqlWhere = array();
$sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
$sqlIdTelboxes = $db->_($telbox);
$sqlWhere[] = " o.`T_TELBOX_NEIGHBOUR_IN`='{$sqlIdTelboxes}' ";
$sqlWhere[] = " o.`SALES_YEAR`='{$year}' ";
$sqlWhere[] = " o.`SALES_MONTH`='{$month}' ";
$sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
$sql = "select o.`ID`
, o.`T_TELBOX_NEIGHBOUR_IN`
, o.`ID_DEALS_TABLE`
, o.`SALES_YEAR`
, o.`SALES_MONTH`
, o.`SALES_VALUE`
, o.`marka`
, o.`A_STATUS`
from `DEALS_SALES` as o
where
{$sqlWhere}
";
DBG::_('DBG_SQL', '>1', "sql:fetch({telbox:{$telbox},year:{$year},month:{$month}})", $sql, __CLASS__, __FUNCTION__, __LINE__);
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$record = $r;
}
return $record;
}
private function _fetchSaleById($idSale) {
$record = null;
$db = DB::getDB();
$sqlWhere = array();
$sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
$sqlWhere[] = " o.`ID`='{$idSale}' ";
$sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
$sql = "select o.`ID`
, o.`T_TELBOX_NEIGHBOUR_IN`
, o.`ID_DEALS_TABLE`
, o.`SALES_YEAR`
, o.`SALES_MONTH`
, o.`SALES_VALUE`
, o.`marka`
, o.`A_STATUS`
from `DEALS_SALES` as o
where
{$sqlWhere}
";
DBG::_('DBG_SQL', '>1', "sql:fetch({telbox:{$telbox},year:{$year},month:{$month}})", $sql, __CLASS__, __FUNCTION__, __LINE__);
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$record = $r;
}
return $record;
}
private function _renderInlineCreateForm($telbox, $year, $month) {
//DBG::_(true, true, "TODO: Edit Form for record", $record, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
$DBG = ('1' == V::get('DBG', '', $_REQUEST));
header("Content-type: text/plain");
$row = $record;// TODO: refactor
$tblAcl = $this->_getTableAcl();
$visibleCols = array('SALES_VALUE', 'marka');
$hasTypeSpecial = false;
foreach ($visibleCols as $fieldName) {
$fieldID = $tblAcl->getFieldIdByName($fieldName);
if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
$fieldVal = ($tblAcl->isAllowed($fieldID, 'R', $row))? V::get($fieldName, '', $row) : '*****';
$fieldVal = V::get("f{$fieldID}", $fieldVal, $_POST);
$vCol = $tblAcl->getField($fieldID);
$vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
$tsValues = array();
$typeSpecial = Typespecial::getInstance($fieldID, $vCol['name']);
if ($typeSpecial) {
if($DBG){echo'Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'';}
$specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
if($DBG){echo'Typespecial('.$fieldID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'';}
if (!empty($specialValues)) {
$tsValues[$row->ID] = implode(' (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'';}
$res = $db->query($sql);
while ($r = $db->fetch($res)) {
$year = $r->SALES_YEAR;
$month = sprintf("%02d", $r->SALES_MONTH);
$obroty[$r->ID_DEALS_TABLE][$year][$month] = $r->SALES_VALUE;
$obrotyByYear[$year][$month] = floatval($r->SALES_VALUE);
$marki[$r->marka] = true;
$obrotyToMarki[$year][$month] = $r->marka;
}
}
krsort($obrotyByYear);
//DBG::_(true, true, "obrotyByYear", $obrotyByYear, __CLASS__, __FUNCTION__, __LINE__);
//DBG::_('DBG', '>0', "marki", $marki, __CLASS__, __FUNCTION__, __LINE__);
//DBG::_('DBG', '>0', "obrotyToMarki", $obrotyToMarki, __CLASS__, __FUNCTION__, __LINE__);
$markiOut = array();
{
$markiLabels = array('primary', 'success', 'warning', 'danger', 'default');
// Default
reset($markiLabels);
foreach ($marki as $marka => $vBool) {
$markiOut[$marka] = current($markiLabels);
if (!next($markiLabels)) reset($markiLabels);
}
}// Default Primary Success Info Warning Danger
$monthsOut = array();
for ($i = 0; $i < 12; $i++) $monthsOut[] = sprintf("%02d", $i + 1);
$initData = new stdClass();
$initData->obroty = $obrotyByYear;
$initData->marki = $markiOut;
$initData->obrotyToMarki = $obrotyToMarki;
$initData->months = $monthsOut;
$initData->years = array_keys($obrotyByYear);
if (!empty($initData->years)) {
$lastYear = date("Y", mktime(date('H'), date('i'), date('s'), date("m") + 3, date("d"), date("Y")));
$firstYear = end($initData->years);
if ($firstYear <= $lastYear) {
$years = array();
for ($i = $firstYear; $i <= $lastYear; $i++) {
$years[] = $i;
}
$initData->years = array_reverse($years);
}
}
//DBG::_(true, true, "initData", $initData, __CLASS__, __FUNCTION__, __LINE__);
?>