Explorar o código

DealsSales add inline create; use marka and status; render marki in js

Piotr Labudda %!s(int64=10) %!d(string=hai) anos
pai
achega
8eb0a7ce35
Modificáronse 1 ficheiros con 212 adicións e 68 borrados
  1. 212 68
      SE/se-lib/Route/DealsSales.php

+ 212 - 68
SE/se-lib/Route/DealsSales.php

@@ -17,8 +17,6 @@ class Route_DealsSales extends RouteBase {
 		SE_Layout::gora();
 		SE_Layout::menu();
 
-		?>
-<?php
 		$this->legacy_DEALS_SALES();
 
 		SE_Layout::dol();
@@ -125,20 +123,23 @@ class Route_DealsSales extends RouteBase {
 		$primaryKeyFieldName = 'ID';
 		$primaryKey = V::get($primaryKeyFieldName, 0, $record);
 
-		$fieldName = 'SALES_VALUE';
-		$fieldID = $tblAcl->getFieldIdByName($fieldName);
-		if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
+		$itemPatch = new stdClass();
+		$itemPatch->{$primaryKeyFieldName} = $primaryKey;
 
-		if (!$tblAcl->isAllowed($fieldID, 'W', $record)) {
-			throw new Exception("Brak uprawnień do zapisu ({$fieldName})");
-		}
+		$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})");
 
-		$argsFieldName = "f{$fieldID}";
-		if (!isset($args[$argsFieldName])) throw new Exception("Brak danych dla pola ({$fieldName})");
+			if (!$tblAcl->isAllowed($fieldID, 'W', $record)) {
+				throw new Exception("Brak uprawnień do zapisu ({$fieldName})");
+			}
 
-		$itemPatch = new stdClass();
-		$itemPatch->{$primaryKeyFieldName} = $primaryKey;
-		$itemPatch->{$fieldName} = $args[$argsFieldName];
+			$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();
@@ -163,14 +164,57 @@ class Route_DealsSales extends RouteBase {
 	}
 
 	private function _saveInlineCreate($args)	{
-		throw new Exception('TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__);
-		$year = V::get('year', 0, $args, 'int');
-		$month = V::get('month', 0, $args, 'int');
-		$telbox = V::get('telbox', '', $args, 'word');
+		$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)	{
@@ -190,6 +234,7 @@ class Route_DealsSales extends RouteBase {
 				, o.`SALES_MONTH`
 				, o.`SALES_VALUE`
 				, o.`marka`
+				, o.`A_STATUS`
 			from `DEALS_SALES` as o
 			where
 				{$sqlWhere}
@@ -216,6 +261,7 @@ class Route_DealsSales extends RouteBase {
 				, o.`SALES_MONTH`
 				, o.`SALES_VALUE`
 				, o.`marka`
+				, o.`A_STATUS`
 			from `DEALS_SALES` as o
 			where
 				{$sqlWhere}
@@ -229,8 +275,65 @@ class Route_DealsSales extends RouteBase {
 	}
 
 	private function _renderInlineCreateForm($telbox, $year, $month) {
-		throw new Exception('TODO: L.' . __LINE__);// TODO: DBG
-		DBG::_(true, true, "TODO: Create Form for", array($telbox, $year, $month), __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
+		//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'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
+				$specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
+				if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
+				if (!empty($specialValues)) {
+					$tsValues[$row->ID] = implode('<br>', $specialValues);
+				}
+			}
+?>
+		<label for="<?php echo "f{$fieldID}"; ?>" class="AjaxTableEdit-label">
+			<strong title="<?php echo "[{$fieldID}] {$fieldName}"; ?>"><?php echo "{$vCol['label']}"; ?></strong>
+			<?php if (!empty($vCol['opis'])) : ?>
+				<em><?php echo $vCol['opis']; ?></em>
+				<?php $perms = $tblAcl->getFieldPerms($fieldID); SE_Layout::hotKeyDBG($perms); ?>
+			<?php endif; ?>
+		</label>
+<?php
+			$fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
+			if (!empty($tsValues[$row->ID])) {
+				$fieldParams['typespecialValue'] = $tsValues[$row->ID];
+			}
+			$vDefault = $tblAcl->getColDefault($fieldName);
+			if (!empty($vDefault)) {
+				$fieldParams['default'] = $vDefault;
+			}
+			echo $tblAcl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
+			if ($typeSpecial) $hasTypeSpecial = true;
+		}
+		if ($hasTypeSpecial) {
+			echo '<p style="padding:100px 0;"></p>';
+		}
+
+?>
+		<input type="hidden" name="T_TELBOX_NEIGHBOUR_IN" value="<?php echo $telbox; ?>" />
+		<input type="hidden" name="SALES_YEAR" value="<?php echo $year; ?>" />
+		<input type="hidden" name="SALES_MONTH" value="<?php echo $month; ?>" />
+<?php
+		exit;
 	}
 
 	private function _renderInlineEditForm($record) {
@@ -244,32 +347,24 @@ class Route_DealsSales extends RouteBase {
 
 		// TODO: $cols = array('T_TELBOX_NEIGHBOUR_IN','SALES_YEAR','SALES_MONTH','SALES_VALUE','marka');
 		// TODO: form for only 'SALES_VALUE' and eventually 'marka'
-		$fieldName = 'SALES_VALUE';
-		$fieldID = $tblAcl->getFieldIdByName($fieldName);
-		if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
-
-		$fieldVal = '';
-		if ($tblAcl->isAllowed($fieldID, 'R', $row)) {
-			$fieldVal = V::get($fieldName, $fieldVal, $row);
-		} else {
-			$fieldVal = '*****';
-		}
-
-		$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'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
-			$specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
-			if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
-			if (!empty($specialValues)) {
-				$tsValues[$row->ID] = implode('<br>', $specialValues);
+		$visibleCols = array('SALES_VALUE', 'marka', 'A_STATUS');
+		$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) {
+				$specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
+				if (!empty($specialValues)) {
+					$tsValues[$row->ID] = implode('<br>', $specialValues);
+				}
 			}
-		}
 ?>
 		<label for="<?php echo "f{$fieldID}"; ?>" class="AjaxTableEdit-label">
 			<strong title="<?php echo "[{$fieldID}] {$fieldName}"; ?>"><?php echo "{$vCol['label']}"; ?></strong>
@@ -279,16 +374,19 @@ class Route_DealsSales extends RouteBase {
 			<?php endif; ?>
 		</label>
 <?php
-		$fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
-		if (!empty($tsValues[$row->ID])) {
-			$fieldParams['typespecialValue'] = $tsValues[$row->ID];
-		}
-		$vDefault = $tblAcl->getColDefault($fieldName);
-		if (!empty($vDefault)) {
-			$fieldParams['default'] = $vDefault;
+			$fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
+			if (!empty($tsValues[$row->ID])) {
+				$fieldParams['typespecialValue'] = $tsValues[$row->ID];
+			}
+			$vDefault = $tblAcl->getColDefault($fieldName);
+			if (!empty($vDefault)) {
+				$fieldParams['default'] = $vDefault;
+			}
+			echo $tblAcl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
+			if ($typeSpecial) $hasTypeSpecial = true;
 		}
-		echo $tblAcl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
-		if ($typeSpecial) {
+
+		if ($hasTypeSpecial) {
 			echo '<p style="padding:100px 0;"></p>';
 		}
 
@@ -327,7 +425,7 @@ class Route_DealsSales extends RouteBase {
 	if (!empty($id_telboxes) || $id_companies > 0) {
 		$db = DB::getDB();
 		$sqlWhere = array();
-		//if (!empty($id_telboxes))  $sqlWhere[] = " d.`T_TELBOX_NEIGHBOUR_IN`='{$id_telboxes}' ";
+		$sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
 		if (!empty($id_telboxes)) {
 			if (false !== strpos($id_telboxes, ': ')) {
 				$parts = explode(': ', $id_telboxes, 2);
@@ -338,7 +436,7 @@ class Route_DealsSales extends RouteBase {
 			}
 		}
 		//if ($id_companies > 0) $sqlWhere[] = " d.`ID_BILLING_USERS`='{$id_companies}' ";
-		$sqlWhere = (!empty($sqlWhere))? implode(" or ", $sqlWhere) : '';
+		$sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
 		$sql = "select o.`ID`
 				-- , d.`T_TELBOX_NEIGHBOUR_IN`
 				, o.`T_TELBOX_NEIGHBOUR_IN`
@@ -463,15 +561,6 @@ class Route_DealsSales extends RouteBase {
 
 		<hr>
 
-		<table class="table table-bordered" style="width:auto">
-			<tr>
-				<th>Marki:</th>
-				<?php foreach ($markiOut as $markaLabel => $labelType) : ?>
-					<td class="cell_marka_<?php echo $labelType; ?>"><b><?php echo $markaLabel; ?></b></td>
-				<?php endforeach; ?>
-			</tr>
-		</table>
-
 		<div id="dealsSalesByMonth"></div>
 <script>
 (function($, undefined) {
@@ -514,6 +603,10 @@ class Route_DealsSales extends RouteBase {
 		};
 
 		priv.initialRender = function () {
+			_uiNode$Marki = $('<table class="table table-bordered" style="width:auto"></table>');
+			$('<tbody><tr><th>Marki:</th></tr></tbody>').appendTo(_uiNode$Marki);
+			_uiNode$Marki.appendTo(_uiNodeCont);
+
 			_uiNode$Table = $('<table class="table table-bordered table-hover"></table>');
 			{// thead
 				var thead$Node = $('<thead></thead>').appendTo(_uiNode$Table);
@@ -528,9 +621,23 @@ class Route_DealsSales extends RouteBase {
 			_uiNode$Table.appendTo(_uiNodeCont);
 			priv.renderInitInlineEditBox();// .dealsSales__inlineEditBox
 
+			priv.renderHeaderMarki();
 			priv.renderTableBody();
 		};
 
+		priv.renderHeaderMarki = function(record) {
+			var currentNode = _uiNode$Marki.children('tbody').children('tr'),
+					node = $('<tr></tr>'),
+					marki = _state.marki
+			;
+			$('<th>Marki:</th>').appendTo(node);
+			$.each(_state.marki, function(markaLabel, markaClassName) {
+				var td$Node = $('<td><b>' + markaLabel + '</b></td>').appendTo(node);
+				td$Node.addClass('cell_marka_' + markaClassName);
+			});
+			currentNode.replaceWith(node);
+		};
+
 		priv.renderTableBody = function(record) {
 			var currentNode = _uiNode$Table.children('tbody'),
 					node = $('<tbody></tbody>'),
@@ -624,14 +731,28 @@ class Route_DealsSales extends RouteBase {
 					if (data.record) {
 						var year = data.record.SALES_YEAR,
 								month = data.record.SALES_MONTH,
+								status = data.record.A_STATUS,
 								saleValue = parseFloat(data.record.SALES_VALUE),
+								marka = data.record.marka,
 								state = {}
 						;
 						if (month < 10) month = '0' + month;
 						// TODO: if marka changed - fix state
-						state.obroty = {};
-						state.obroty[year] = {};
-						state.obroty[year][month] = saleValue;
+						if ('DELETED' === status) {
+							state.obroty = {};
+							state.obroty[year] = {};
+							state.obroty[year][month] = null;
+							state.obrotyToMarki = {};
+							state.obrotyToMarki[year] = {};
+							state.obrotyToMarki[year][month] = null;
+						} else {
+							state.obroty = {};
+							state.obroty[year] = {};
+							state.obroty[year][month] = saleValue;
+							state.obrotyToMarki = {};
+							state.obrotyToMarki[year] = {};
+							state.obrotyToMarki[year][month] = marka;
+						}
 						priv.setState(state);
 					}
 					_inlineEditBox$Node.modal('hide');
@@ -730,6 +851,7 @@ class Route_DealsSales extends RouteBase {
 				for (var i=1; i<arguments.length; i++) {
 					switch (arguments[i]) {
 						case 'body': priv.renderTableBody(); break;
+						case 'marki': priv.renderHeaderMarki(); break;
 					}
 				}
 			}
@@ -741,11 +863,33 @@ class Route_DealsSales extends RouteBase {
 			if (state.obroty) {
 				$.each(state.obroty, function(kYear, vSalesByMonth) {
 					$.each(vSalesByMonth, function(kMonth, vSaleValue) {
-						_state.obroty[kYear][kMonth] = vSaleValue;
+						if (null === vSaleValue) {
+							_state.obroty[kYear][kMonth] = undefined;
+						} else {
+							_state.obroty[kYear][kMonth] = vSaleValue;
+						}
 					});
 				});
 				renderParts['body'] = true;
 			}
+			/*
+						state.obrotyToMarki[year][month] = marka;
+			*/
+			if (state.obrotyToMarki) {
+				$.each(state.obrotyToMarki, function(kYear, vMarkiByMonth) {
+					$.each(vMarkiByMonth, function(kMonth, vMarka) {
+						if (null === vMarka) {
+							_state.obrotyToMarki[kYear][kMonth] = undefined;
+						} else {
+							_state.obrotyToMarki[kYear][kMonth] = vMarka;
+							if (undefined  === _state.marki[vMarka]) {
+								_state.marki[vMarka] = 'default';
+							}
+						}
+					});
+				});
+				renderParts['marki'] = true;
+			}
 
 			renderParts = Object.keys(renderParts);