decideFromBudget.php 16.6 KB
Newer Older
Clément Damiens's avatar
 
Clément Damiens committed
1 2 3
<?php if($answer){ 
	$copy = "opalProcess1.depense";

4 5
	if( isset($this->costum["form"]["params"][$kunik]["budgetCopy"]) ) 
		$copy = $this->costum["form"]["params"][$kunik]["budgetCopy"];
6 7 8
	else if( count(Yii::app()->session["budgetInputList"]) == 1 )
		$copy = array_keys( Yii::app()->session["budgetInputList"])[0];

Clément Damiens's avatar
 
Clément Damiens committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	//var_dump($copy);
	$copyT = explode(".", $copy);
	$copyF = $copyT[0];
	$budgetKey = $copyT[1];
	$answers = null;	
	//var_dump($budgetKey);
	if($wizard){
		if( $budgetKey ){
			if( isset($answer["answers"][$copyF][$budgetKey]) && count($answer["answers"][$copyF][$budgetKey])>0)
				$answers = $answer["answers"][$copyF][$budgetKey];
		} else if( isset($answer["answers"][$copyF][$kunik]) && count($answer["answers"][$copyF][$budgetKey])>0 )
			$answers = $answer["answers"][$copyF][$kunik];
	} else {
		if($budgetKey)
			$answers = $answer["answers"][$budgetKey];
		else if(isset($answer["answers"][$kunik]))
			$answers = $answer["answers"][$kunik];
	}

	
$editBtnL =  "";

31
$editParamsBtn = ($canEdit) ? " <a href='javascript:;' data-id='".$el["_id"]."' data-collection='".$this->costum["contextType"]."' data-path='costum.form.params.".$kunik."' class='previewTpl edit".$kunik."Params btn btn-xs btn-danger'><i class='fa fa-cog'></i> </a>" : "";
Clément Damiens's avatar
 
Clément Damiens committed
32 33 34 35 36 37 38 39 40 41 42 43 44


$paramsData = [ 
	"group" => ["Feature","Costum","Chef de Projet","Data","Mantenance"],
	"nature" => [
        "investissement" => "Investissement",
        "fonctionnement" => "Fonctionnement"
    ],
	"amounts" => [
    	"price" => "Price"
    ]
];

45 46 47 48 49 50 51
if( isset($this->costum["form"]["params"][$kunik]) ) {
	if( isset($this->costum["form"]["params"][$budgetKey]["group"]) ) 
		$paramsData["group"] =  $this->costum["form"]["params"][$budgetKey]["group"];
	if( isset($this->costum["form"]["params"][$budgetKey]["nature"]) ) 
		$paramsData["nature"] =  $this->costum["form"]["params"][$budgetKey]["nature"];
	if( isset($this->costum["form"]["params"][$budgetKey]["amounts"]) ) 
		$paramsData["amounts"] =  $this->costum["form"]["params"][$kunik]["amounts"];
Clément Damiens's avatar
 
Clément Damiens committed
52 53
}

54
$communityLinks = Element::getCommunityByTypeAndId($this->costum["contextType"],$this->costum["contextId"]);
Clément Damiens's avatar
 
Clément Damiens committed
55 56 57 58 59
$organizations = Link::groupFindByType( Organization::COLLECTION,$communityLinks,["name","links"] );

$orgs = [];

foreach ($organizations as $id => $or) {
60
	$roles = $or["links"]["memberOf"][$this->costum["contextId"]]["roles"];
Clément Damiens's avatar
 
Clément Damiens committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	if( $paramsData["limitRoles"] && !empty($roles))
	{
		foreach ($roles as $i => $r) {
			if( in_array($r, $paramsData["limitRoles"]) )
				$orgs[$id] = $or["name"];
		}		
	}
}
//var_dump($orgs);exit;
$listLabels = array_merge($orgs);//Ctenat::$financerTypeList,

$properties = [
    
    "poste" => [
        "inputType" => "text",
        "label" => "Contexte",
        "placeholder" => "Contexte",
        "rules" => [ "required" => true ]
    ]
];
foreach ($paramsData["amounts"] as $k => $l) {
	$properties[$k] = [ "inputType" => "text",
			            "label" => $l,
			            "propType" =>"amount",
			            "placeholder" => $l,
			            "rules" => [ "required" => true, "number" => true ]
			        ];
}
$properties["votes"] = [
    "inputType" => "text",
    "label" => "Votes",
    "placeholder" => "Votes",
    "rules" => [ "required" => true,"number" => true  ]
];

	?>	
<div class="form-group">
	<table class="table table-bordered table-hover  directoryTable" id="<?php echo $kunik?>">

	<thead>
		<tr>
			<td colspan='<?php echo count( $properties)+2?>' ><h4 style="color:<?php echo ($titleColor) ? $titleColor : "black"; ?>"><?php echo $label.$editQuestionBtn.$editParamsBtn.$editBtnL?></h4>
				<?php echo $info;
104
				if( !isset($this->costum["form"]["params"][$kunik]["budgetCopy"]) ) 
Clément Damiens's avatar
 
Clément Damiens committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
					echo "<br/><span class='text-red text-center'><i class='fa fa-warning fa-2x'></i> THIS FIELD HAS TO BE CONFIGURED FIRST ".$editParamsBtn."</span>"; ?>
			</td>
		</tr>	
		<?php if($answers){ ?>
		<tr>
			</th>
			<?php 
			
			foreach ($properties as $i => $inp) {
				echo "<th>".$inp["placeholder"]."</th>";
			} ?>
			<th></th>
		</tr>
		<?php } ?>
	</thead>
	<tbody class="directoryLines">	
		<?php 
		$ct = 0;
		$totalVotes = 0;
		$bigTotal = 0;
		$totalPayed = 0;
		$validDecision = 0;
		$invalidDecision = 0;
		$payedWork = 0;
		if($answers){
			foreach ($answers as $q => $a) {

				$trStyle = "";
				$tds = "";
				foreach ($properties as $i => $inp) 
				{
					$tds .= "<td style='vertical-align:middle'>";
					
					if( $i == "poste") 
						$tds .= $a["poste"];
					if( $i == "price" ) 
						$tds .= $a["price"]."€";
					else if( $i == "workType" && isset( $a["worker"]["workType"] ) ) 
						$tds .= $a["worker"]["workType"];
					else if( $i == "votes"){
						$votes = (!empty($a["votes"]) ) ? (int)$a["votes"] : 0;
						

						$thisVoteTotal = 0;
						$percol = "danger";
						$youVoted = "";
						if(!empty($a["votes"]) ){
							$youVoted = "<span class='label label-default'>".count($a["votes"])."voter(s)"."</span>";
							foreach ($a["votes"] as $uid => $v) {
								$thisVoteTotal += (int)$v["vote"];
							}
							$thisVoteTotal = $thisVoteTotal / count($a["votes"]);
							if( $thisVoteTotal > 50 ){
								$percol = "warning";
								$validDecision++;
							}
							if( $thisVoteTotal > 75 )
								$percol = "success";
							if( $thisVoteTotal < 50 )
								$invalidDecision++;


							if( isset( $a["votes"][ Yii::app()->session["userId"] ] ) ){
								$col = "danger";
								if( (int)$a["votes"][Yii::app()->session["userId"]]["vote"] > 50 )
									$col = "success";
								$youVoted .= "<br/><span class='label label-".$col."'> you : ".$a["votes"][Yii::app()->session["userId"]]["vote"]."%</span>";
							}
						}
						$totalVotes += $thisVoteTotal;
						
						$tds .= "<a href='javascript:;' data-id='".$answer["_id"]."' data-budgetpath='".$copy."' data-form='".$copyF."' data-pos='".$q."'  class=' btnVote margin-left-5 padding-10'>".$thisVoteTotal."%</a>";
						$tds .= '<div class="progress btnVote"  data-id="'.$answer["_id"].'" data-budgetpath="'.$copy.'" data-form="'.$copyF.'" data-pos="'.$q.'"  style="cursor:pointer;margin-bottom: 0px;">'.
						  '<div class="progress-bar progress-bar-'.$percol.'" style="width:'.$thisVoteTotal.'%">'.
							    '<span class="sr-only">'.$thisVoteTotal.'% Complete</span>'.
						  '</div>'.
						'</div>'.$youVoted;
					}
					else if( isset( $a[$i] ) ) 
						$tds .= $a[$i];
					
					$tds .= "</td>";
				}
					
			?>
			
			<?php 
				$ct++;
				echo "<tr id='".$kunik.$q."' class='".$kunik."Line text-center' style='".$trStyle."'>";
				echo $tds;?>
				<td>
					<a href="javascript:;" class="btn btn-xs btn-primary openAnswersComment" onclick="commentObj.openPreview('answers','<?php echo $answer["_id"]?>','<?php echo $answer["_id"].$key.$q ?>', '<?php echo @$a['step'] ?>')"><?php echo PHDB::count(Comment::COLLECTION, array("contextId"=>$answer["_id"],"contextType"=>"answers", "path"=>$answer["_id"].$key.$q))?> <i class='fa fa-commenting'></i></a>
				</td>
			</tr>
			<?php 
			}
		}
?>
		</tbody>
	</table>

<?php 
$percol = "danger";
$totalVotes = (!empty($answers)) ? $totalVotes / count($answers) : 0;
if( $totalVotes > 50  ){
	$percol = "success";
}
echo "<h4 style='color:".(($titleColor) ? $titleColor : "black")."'>Décision Globale</h4>".
'<div class="progress " style="cursor:pointer" >'.
  '<div class="progress-bar progress-bar-'.$percol.'" style="width:'.$totalVotes.'%">'.
	    '<span class="sr-only">'.$totalVotes.'% Complete</span>'.
  '</div>'.
'</div>'; ?>

<table class="table table-bordered table-hover  ">
	<tbody class="">
		<tr>
			<td>Pour</td>
			<td><?php echo floor($totalVotes) ?>%</td>
		</tr>
		<tr>
			<td>Contre</td>
			<td><?php echo floor(100-$totalVotes)+1 ?>%</td>
		</tr>
		<tr>
			<td>Nombres de Décisions Validés </td>
			<td><?php echo $validDecision."/".count($answers) ?></td>
		</tr>
		<tr>
			<td>Nombres de Décisions Refusés </td>
			<td><?php echo $validDecision."/".count($answers) ?></td>
		</tr>
		
	</tbody>
</table>


</div>

<div class="form-worker" style="display:none;">
	<select id="worker" style="width:100%;">
		<option>Choisir un maitre d'ouvrage</option>
		<?php foreach ($orgs as $v => $f) {
			echo "<option value='".$v."'>".$f."</option>";
		} ?>
	</select>
	<br>
	Type de travaux effectués : <br/>
	<input type="text" id="workType" name="workType" style="width:100%;">
	<br><br>
	<span class="bold">Organisme qui effectue les travaux , s'il n'existe pas, créez le et ajoutez le à la communauté ici 
		<a class="btn btn-primary">Ajouter un maitre d'ouvrage</a>
	</span>
	<br>
</div>

<div class="form-votes" style="display:none;">
	<select id="votes" style="width:100%;">
		<option> VOTEZ pour cette partie </option>
		<?php foreach ([0,10,20,30,40,50,60,70,80,90,100] as $v => $f) {
			if($f == 0)
				$lbl = "Je ne suis pas pour!";
			else 
				$lbl = "Je suis pour à ".$f."%";
			echo "<option value='".$f."'>".$lbl."</option>";
		} ?>
	</select>
  
</div>





<?php 
280 281 282
if( isset($this->costum["form"]["params"]["financement"]["tpl"])){
	//if( $this->costum["form"]["params"]["financement"]["tpl"] == "tpls.forms.equibudget" )
		$this->renderPartial( "costum.views.".$this->costum["form"]["params"]["financement"]["tpl"] , 
Clément Damiens's avatar
 
Clément Damiens committed
283 284 285
		[ "totalFin"   => $total,
		  "totalBudg" => Yii::app()->session["totalBudget"]["totalBudget"] ] );
	// else 
286
	// 	$this->renderPartial( "costum.views.".$this->costum["form"]["params"]["financement"]["tpl"]);
Clément Damiens's avatar
 
Clément Damiens committed
287 288 289 290 291 292
}
 ?>

<script type="text/javascript">
if(typeof costum.lists == "undefined")
	costum.lists = {};
293 294
if(typeof costum.lists == "undefined")
	costum.lists.budgetInputList = <?php echo json_encode( Yii::app()->session["budgetInputList"] ); ?>;
Clément Damiens's avatar
 
Clément Damiens committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
//costum.lists.financerTypeList = <?php //echo json_encode(Ctenat::$financerTypeList); ?>;
costum.lists.workerList = <?php echo json_encode($orgs); ?>;


var <?php echo $kunik ?>Data = <?php echo json_encode( (isset($answer["answers"][$copy])) ? $answer["answers"][$copy] : null ); ?>;
sectionDyf.<?php echo $kunik ?>ParamsData = <?php echo json_encode( $paramsData ); ?>;

$(document).ready(function() { 
	
	sectionDyf.<?php echo $kunik ?> = {
		"jsonSchema" : {	
	        "title" : "Plan de Financement",
            "icon" : "fa-money",
            "text" : "Décrire ici les financements mobilisés ou à mobiliser. Les coûts doivent être en <b>hors taxe</b>.",
	        "properties" : <?php echo json_encode( $properties ); ?>,
	        save : function () {  
	            tplCtx.value = {};
	            $.each( sectionDyf.<?php echo $kunik ?>.jsonSchema.properties , function(k,val) { 
	        		tplCtx.value[k] = $("#"+k).val();
	        	 });
	            mylog.log("save tplCtx",tplCtx);
	            if(typeof tplCtx.value == "undefined")
	            	toastr.error('value cannot be empty!');
	            else {
	                dataHelper.path2Value( tplCtx, function(params) { 
320
	                    urlCtrl.loadByHash(location.hash);
Clément Damiens's avatar
 
Clément Damiens committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	                } );
	            }

	    	}
	    }
	};

	sectionDyf.<?php echo $kunik ?>Params = {
		"jsonSchema" : {	
	        "title" : "<?php echo $kunik ?> config",
	        "icon" : "fa-cog",
	        "properties" : {
	            limitRoles : {
	                inputType : "array",
	                label : "Liste des roles financeurs",
	                values :  sectionDyf.<?php echo $kunik ?>ParamsData.limitRoles
	            },
	            tpl : {
	                label : "Sub Template",
	                value :  sectionDyf.<?php echo $kunik ?>ParamsData.tpl
	            },
	            budgetCopy : {
	            	label : "Input Bugdet",
	            	inputType : "select",
	            	options :  costum.lists.budgetInputList
	            }
	        },
	        save : function () {  
	            tplCtx.value = {};
	            $.each( sectionDyf.<?php echo $kunik ?>Params.jsonSchema.properties , function(k,val) { 
	        		if(val.inputType == "array")
	        		 	tplCtx.value[k] = getArray('.'+k+val.inputType);
	        		else if(val.inputType == "properties")
	        		 	tplCtx.value[k] = getPairsObj('.'+k+val.inputType);
	        		else
	        		 	tplCtx.value[k] = $("#"+k).val();
	        	 });
	            mylog.log("save tplCtx",tplCtx);
	            
	            if(typeof tplCtx.value == "undefined")
	            	toastr.error('value cannot be empty!');
	            else {
	                dataHelper.path2Value( tplCtx, function(params) { 
364
	                    urlCtrl.loadByHash(location.hash);
Clément Damiens's avatar
 
Clément Damiens committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	                } );
	            }

	    	}
	    }
	};


    mylog.log("render","/modules/costum/views/tpls/forms/<?php echo $kunik ?>.php");

    //adds a line into answer
    $(".add<?php echo $kunik ?>").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection");            
        tplCtx.path = $(this).data("path")+((notNull(<?php echo $kunik ?>Data) ? <?php echo $kunik ?>Data.length : "0"));
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?> );
    });

    $(".edit<?php echo $kunik ?>").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection"); 
        tplCtx.path = $(this).data("path");
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?>,null, <?php echo $kunik ?>Data[$(this).data("key")]);
    });

    $(".edit<?php echo $kunik ?>Params").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection");
        tplCtx.path = $(this).data("path");
        //mylog.log(".edit<?php echo $kunik ?>Params",tplCtx,sectionDyf.<?php echo $kunik ?>ParamsData);
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?>Params,null, sectionDyf.<?php echo $kunik ?>ParamsData);
    });

    
    $('.btnValidateWork').off().click(function() { 
		tplCtx.pos = $(this).data("pos");
		tplCtx.budgetpath = $(this).data("budgetpath");
		tplCtx.collection = "answers";
		tplCtx.id = $(this).data("id");
		tplCtx.form = $(this).data("form");
		prioModal = bootbox.dialog({
	        message: $(".form-validate-work").html(),
	        title: "Validation des travaux",
	        show: false,
	        buttons: {
                success: {
                    label: trad.save,
                    className: "btn-primary",
                    callback: function () {
			            
			        	var formInputsHere = formInputs;
			        	tplCtx.path = "answers";
			        	if( notNull(formInputs [tplCtx.form]) )
			        		tplCtx.path = "answers."+tplCtx.form;    

			        	tplCtx.path = tplCtx.path+"."+tplCtx.budgetpath+"."+tplCtx.pos+".validFinal";	   
			        	
			        	var today = new Date();
						today = today.getDate() + '/' + (today.getMonth()+1) + '/' + today.getFullYear();	
			            tplCtx.value = { 
		        			valid : $(".bootbox #validDecision").val(),
		        			user : userId,
		        			date : today
		        		};
		        		delete tplCtx.pos;
		        		delete tplCtx.budgetpath;
				    	mylog.log("btnValidateWork save",tplCtx);
432 433 434
				  	 	dataHelper.path2Value( tplCtx, function(){
				  	 		saveLinks(answerObj._id.$id,"validated",userId,closePrioModalRel);
				  	 	} );
Clément Damiens's avatar
 
Clément Damiens committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
				  	 }		
                },
                cancel: {
                  label: trad.cancel,
                  className: "btn-secondary",
                  callback: closePrioModal
                }
              },
		        onEscape: closePrioModal
	    });
	    prioModal.modal("show");
	});

	$('.btnVote').off().click(function() { 
		tplCtx.pos = $(this).data("pos");
		tplCtx.budgetpath = $(this).data("budgetpath");
		tplCtx.collection = "answers";
		tplCtx.id = $(this).data("id");
		tplCtx.form = $(this).data("form");
		prioModal = bootbox.dialog({
	        message: $(".form-votes").html(),
	        title: "Voter pour partie",
	        show: false,
	        buttons: {
                success: {
                    label: trad.save,
                    className: "btn-primary",
                    callback: function () {
			        	tplCtx.path = "answers."+tplCtx.budgetpath+"."+tplCtx.pos+".votes."+userId; 

			        	var today = new Date();
						today = today.getDate() + '/' + (today.getMonth()+1) + '/' + today.getFullYear();	
			            tplCtx.value = {
			            	vote : $(".bootbox #votes").val(),
			            	date : today
			            };

		        		delete tplCtx.pos;
		        		delete tplCtx.budgetpath;
				    	mylog.log("btnProgress save",tplCtx);
475 476 477
				  	 	dataHelper.path2Value( tplCtx, function(){
				  	 		saveLinks(answerObj._id.$id,"voted",userId);
				  	 	} );
Clément Damiens's avatar
 
Clément Damiens committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491
			        }		
                },
                cancel: {
                  label: trad.cancel,
                  className: "btn-secondary",
                  callback: closePrioModal
                }
              },
		        onEscape: closePrioModal
	    });
	    prioModal.modal("show");
	});

});
Tibor Katelbach's avatar
Tibor Katelbach committed
492

Clément Damiens's avatar
 
Clément Damiens committed
493 494 495 496
</script>
<?php } else {
	//echo "<h4 class='text-red'>CALENDAR works with existing answers</h4>";
} ?>