Yii Framework Forum: Update multiple text fields - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Update multiple text fields Rate Topic: -----

#1 User is offline   compact_corpse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 24-May 11

Posted 24 October 2011 - 11:16 AM

I need to update multiple text fields on my invoice form once one value is changed.
ie. A user changes the quantity needed, then the following would change:
1. The sub-total for those items
2. The franchise commission for those items
3. The total for the entire form

What I have right now is when a user changes the quantity, it will update the total for that item, but I can't get it to update multiple fields.

foreach($model->invLinesAuto as $autoLine){
echo CHtml::textField('InvLn_Unit_Qty['.$id.']', $autoLine->InvLn_Unit_Qty, array(
						'id'=>'InvLn_Unit_Qty_'.$autoLine->InvALn_ID,
						'ajax'=>array(
							'type'=>'POST',
							'url'=>CController::createUrl('unitTotal&id='.$autoLine->InvALn_ID),
							'replace'=>'#InvLn_Unit_Total_'.$autoLine->InvALn_ID,
						)
					));

echo CHtml::textField('InvLn_Unit_Total['.$autoLine->InvALn_ID.']', $autoLine->InvLn_Unit_Total);
}


And in my controller:
$total = $_POST['InvLn_Unit_Qty'][$id]*$_POST['InvLn_Unit_Price'][$id];
		
echo CHtml::textField('InvLn_Unit_Total['.$id.']', sprintf('%01.2f', $total);

0

#2 User is offline   popo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 20-September 11
  • Location:xiamen

Posted 26 October 2011 - 11:46 PM

using renderPartial, man.

don't echo directly in the controller.
0

#3 User is offline   compact_corpse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 24-May 11

Posted 27 October 2011 - 11:55 AM

Okay, this is what I've done:

echo CHtml::textField('InvLn_Unit_Qty['.$autoLine->InvALn_ID.']', $model->lineUnits($autoLine->InvALn_ID), array(
						'id'=>'InvLn_Unit_Qty_'.$autoLine->InvALn_ID,
						'size'=>5, 
						'style'=>'text-align:right;',
						'ajax'=>array(
							'type'=>'POST',
							'dataType'=>'json',
							'url'=>CController::createUrl('totals&id='.$autoLine->InvALn_ID),
							'success'=>'updateFields',
						)
					));

 Yii::app()->clientScript->registerScript('updateFields',"
					function updateFields(data){
					   $('#InvLn_Unit_Total_'+data.id).val(data.itemTot);
					   $('#InvLn_Unit_Comm_'+data.id).val(data.com);
					   
					   $('#Inv_SL_Sub_Total').val(data.invClSub);
					   $('#Inv_SL_GST').val(data.clGst);
					   $('#Inv_SL_Total').val(data.clTot);
					   
					   $('#Inv_PL_Sub_Total').val(data.plSub);
					   $('#Inv_PL_GST').val(data.plGst);
					   $('#Inv_PL_Total').val(data.plTot);
					   
					   $('#Inv_Com_Amt').val(data.cdcsSub);
					   $('#Inv_Com_GST').val(data.cdcsGst);
					   $('#Inv_Com_Total').val(data.cdcsTot);
					  }
					", CClientScript::POS_READY); 



And in my controller:
public function actionTotals($id){
	
		$auto = InvoiceLinesAuto::model()->findByPk($id);
		
		$total = $_POST['InvLn_Unit_Qty'][$id]*$_POST['InvLn_Unit_Price'][$id];
		$com=$auto->InvALn_Comm_Percent*$total;
		$_POST['InvLn_Unit_Total'][$id]=$total;
		$_POST['InvLn_Unit_Comm'][$id]=$com;
		
		$invCl=0;
		$cdcsSub=0;
		foreach($_POST['InvLn_Unit_Total'] as $aId=>$unit){
			$invCl+=$unit;
			$cdcsSub+=$_POST['InvLn_Unit_Comm'][$aId];
		}
		
		$clGst=$auto->InvALn_Tax1*$invCl;
		$clTot=$invCl+$clGst-$_POST['Inv_PL_Deductible'];
		
		$plSub=$invCl-$cdcsSub;
		$plGst=$auto->InvALn_Tax1*$plSub;
		$plTot=$plSub+$plGst-$_POST['Inv_PL_Deductible'];
		
		$cdcsGst=$auto->InvALn_Tax1*$cdcsSub;
		$cdcsTot=$cdcsSub+$cdcsGst;
		
		echo CJSON::encode(array(
                        'id' => $id,
                        'itemTot' => sprintf('%01.2f', round($total,2)),
                        'com' => sprintf('%01.2f', round($com,2)),
						
                        'invClSub' => sprintf('%01.2f', round($invCl,2)),
                        'clGst' => sprintf('%01.2f', round($clGst,2)),
                        'clTot' => sprintf('%01.2f', round($clTot,2)),
						
                        'plSub' => sprintf('%01.2f', round($plSub,2)),
                        'plGst' => sprintf('%01.2f', round($plGst,2)),
                        'plTot' => sprintf('%01.2f', round($plTot,2)),
						
                        'cdcsSub' => sprintf('%01.2f', $cdcsSub),
                        'cdcsGst' => sprintf('%01.2f', $cdcsGst),
                        'cdcsTot' => sprintf('%01.2f', $cdcsTot),
                    ));
	}


It works. I'm not sure how 'correct' it is, but it works.


However, I have a new function where, if I click on a checkbox it will toggle the tax:
<?php echo $form->checkBox($model,'Inv_No_Invoicee_GST', array(
						'id'=>'Invoices_Inv_No_Invoicee_GST',
						'ajax'=>array(
							'type'=>'POST',
							'url'=>CController::createUrl('noGst'),
							'dataType'=>'json',
							'success'=>'noGst',
						)
					)); ?>

<?php Yii::app()->clientScript->registerScript('noGst',"
					function noGst(data){
						$('#Invoices_Inv_No_Invoicee_GST').attr('checked', data.inv);
						$('#Invoices_Inv_No_Fran_GST').attr('checked', data.fran);
					   
						$('#Inv_SL_Total').val(data.clTot);
						$('#Inv_SL_GST').val(data.clGst);
						
						$('#Inv_PL_Total').val(data.plTot);
						$('#Inv_PL_GST').val(data.plGst);
					   
					  }
					", CClientScript::POS_READY); ?>


And in the controller:
public function actionNoGst(){
	
		$clTot=$_POST['Inv_SL_Total'];
		$clGst=$_POST['Inv_SL_GST'];
		$plTot=$_POST['Inv_PL_Total'];
		$plGst=$_POST['Inv_PL_GST'];
		
		if($_POST['Invoices']['Inv_No_Invoicee_GST']==1){
			$clTot=$_POST['Inv_SL_Sub_Total'];
			$clGst='0.0';
		}
		elseif($_POST['Invoices']['Inv_No_Invoicee_GST']==0){
			$clGst=round($_POST['Inv_SL_Sub_Total']*Invoices::model()->gstRate,2);
			$clTot=round($_POST['Inv_SL_Sub_Total']+$clGst,2);
		}
		
		if($_POST['Invoices']['Inv_No_Fran_GST']==1){
			$plTot=$_POST['Inv_PL_Sub_Total'];
			$plGst='0.0';
		}
		elseif($_POST['Invoices']['Inv_No_Fran_GST']==0){
			$plGst=round($_POST['Inv_PL_Sub_Total']*Invoices::model()->gstRate,2);
			$plTot=round($_POST['Inv_PL_Sub_Total']+$plGst,2);
		}
		
		echo CJSON::encode(array(
                        'inv' => $_POST['Invoices']['Inv_No_Invoicee_GST']==1?true:false,
                        'fran' => $_POST['Invoices']['Inv_No_Fran_GST']==1?true:false,
                        'clTot'=> sprintf('%01.2f', $clTot),
                        'clGst'=> sprintf('%01.2f', $clGst),
                        'plTot'=> sprintf('%01.2f', $plTot),
                        'plGst'=> sprintf('%01.2f', $plGst),
                    ));
	}


And this works perfect in FF/Chrome/Safari, but ie won't do anything when I check the box. Does anybody know anything about this? My users (for some stupid reason) only use IE.
0

#4 User is offline   Yii_tips 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-September 12
  • Location:Pinjore India

Posted 26 March 2013 - 12:06 AM

this is very helpfull for me..
thanks ::)
Anil Kumar
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users