/** * Lists all models. */ public function actionIndex() { if (isset($_POST['oper']) && $_POST['oper'] == 'edit') { $setID = intval($_POST["id"]); $set = ServiceLevelSettings::model()->findByPk($setID); if ($set) { $set->Tier_Name = $_POST["Tier_Name"] ? $_POST["Tier_Name"] : null; $set->Users_Count = $_POST["Users_Count"] ? $_POST["Users_Count"] : null; $set->Projects_Count = $_POST["Projects_Count"] ? $_POST["Projects_Count"] : null; $set->Storage_Count = $_POST["Storage_Count"] ? $_POST["Storage_Count"] : null; $set->Base_Fee = $_POST["Base_Fee"] ? $_POST["Base_Fee"] : null; $set->Additional_User_Fee = $_POST["Additional_User_Fee"] ? $_POST["Additional_User_Fee"] : null; $set->Additional_Project_Fee = $_POST["Additional_Project_Fee"] ? $_POST["Additional_Project_Fee"] : null; $set->Additional_Storage_Fee = $_POST["Additional_Storage_Fee"] ? $_POST["Additional_Storage_Fee"] : null; $set->Trial_Period = $_POST["Trial_Period"] ? $_POST["Trial_Period"] : null; $set->Description = $_POST["Description"] ? $_POST["Description"] : null; if ($set->validate()) { $set->save(); echo "settings\n"; } } die; } if (isset($_POST['oper']) && $_POST['oper'] == 'add') { die; } if (isset($_POST['oper']) && $_POST['oper'] == 'del') { die; } $conn = mysql_connect(Yii::app()->params->dbhost, Yii::app()->params->dbuser, Yii::app()->params->dbpassword); mysql_select_db(Yii::app()->params->dbname); mysql_query("SET NAMES 'utf8'"); Yii::import('ext.phpgrid.inc.jqgrid'); // set columns $col = array(); $col["title"] = "Service Level ID"; // caption of column $col["name"] = "Service_Level_ID"; $col["dbname"] = "Service_Level_ID"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = false; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = false; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Tier Name"; // caption of column $col["name"] = "Tier_Name"; $col["dbname"] = "Tier_Name"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Users Count"; // caption of column $col["name"] = "Users_Count"; $col["dbname"] = "Users_Count"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Projects Count"; // caption of column $col["name"] = "Projects_Count"; $col["dbname"] = "Projects_Count"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Storage Count"; // caption of column $col["name"] = "Storage_Count"; $col["dbname"] = "Storage_Count"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Base Fee"; // caption of column $col["name"] = "Base_Fee"; $col["dbname"] = "Base_Fee"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Additional User Fee"; // caption of column $col["name"] = "Additional_User_Fee"; $col["dbname"] = "Additional_User_Fee"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Additional Project Fee"; // caption of column $col["name"] = "Additional_Project_Fee"; $col["dbname"] = "Additional_Project_Fee"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Additional Storage Fee"; // caption of column $col["name"] = "Additional_Storage_Fee"; $col["dbname"] = "Additional_Storage_Fee"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Trial_Period"; // caption of column $col["name"] = "Trial_Period"; $col["dbname"] = "Trial_Period"; // grid column name, same as db field or alias from sql $col["resizable"] = false; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = true; $cols[] = $col; $col = array(); $col["title"] = "Description"; // caption of column $col["name"] = "Description"; $col["dbname"] = "Description"; // grid column name, same as db field or alias from sql $col["resizable"] = true; $col["editable"] = true; // this column is editable $col["hidden"] = false; $col["viewable"] = true; $col["search"] = true; $col["sortable"] = false; $col["edittype"] = "textarea"; $col["editoptions"] = array("rows"=>15, "cols"=>80); $cols[] = $col; $g = new jqgrid(); $grid["caption"] = "Service Level Settings"; // $grid["multiselect"] = true; $grid["autowidth"] = true; $grid["resizable"] = true; //$grid["toppager"] = true; $grid["sortname"] = 'Service_Level_ID'; $grid["sortorder"] = "ASC"; $grid["add_options"] = array( 'width'=>'600', "closeAfterEdit"=>true, // close dialog after add/edit "top"=>"200", // absolute top position of dialog "left"=>"200" // absolute left position of dialog ); $g->set_options($grid); $g->set_actions(array( "add"=>true, // allow/disallow add "edit"=>true, // allow/disallow edit "delete"=>false, // allow/disallow delete "rowactions"=>true, // show/hide row wise edit/del/save option "export"=>true, // show/hide export to excel option "autofilter" => true, // show/hide autofilter for search "search" => "advance" // show single/multi field search condition (e.g. simple or advance) ) ); $g->select_command = "SELECT service_level_settings.* FROM service_level_settings"; // set database table for CRUD operations $g->table = "service_level_settings"; $g->set_columns($cols); // group columns header $g->set_group_header( array( "useColSpanStyle"=>true, "groupHeaders"=>array( array( "startColumnName"=>'Service_Level_ID', // group starts from this column "numberOfColumns"=>11, // group span to next 2 columns "titleText"=>'Service Level Settings Information' // caption of group header ), ) ) ); // render grid and get html/js output $out = $g->render("service_level_settings"); $this->render('index',array( 'out'=>$out, )); }
/** * Returns array of summarized settings for current client * @return array Tier_Name,Users_Count, Projects_Count, Storage_Count, Base_Fee, Additional_Users,Additional_Projects,Additional_Storage,Additional_Fee */ public static function getSummarySettings($client_id) { $result = array(); $i= 0; $limiter =''; $client_id = $client_id ? $client_id :Yii::app()->user->clientID; $css = ClientServiceSettings::model()->findByPk($client_id); $result['Tiers_Str'] = $css->Service_Level_ID; $tiers_array = explode(',',$result['Tiers_Str']); foreach ($tiers_array as $id) { $settings = ServiceLevelSettings::model()->findByPk($id); if ($i != 0) {$limiter = ', ';} if ($settings->Service_Level_ID == 1) { $w9_exist =true; $w9_price = $settings->Base_Fee; $result['levels_checksum']+=1; } if ($settings->Service_Level_ID == 2) { $result['levels_checksum']+=5; } if ($settings->Service_Level_ID == 3) { $result['levels_checksum']+=10; }if ($settings->Service_Level_ID == 4) { $result['levels_checksum']+=20; } $result['Tier_Name'] .= $limiter.$settings->Tier_Name; $i++; } $add_users_price = $settings->Additional_User_Fee; $add_projects_price = $settings->Additional_Project_Fee; $add_storage_price = $settings->Additional_Storage_Fee; $result['Base_Fee'] = ClientServiceSettings::CalculateBaseFee($tiers_array); $result['Users_Count'] = $settings->Users_Count; $result['Projects_Count'] = $settings->Projects_Count; $result['Storage_Count'] = $settings->Storage_Count; $result['Additional_Users'] =$css->Additional_Users; $result['Additional_Projects'] =$css->Additional_Projects; $result['Additional_Storage'] =$css->Additional_Storage; $result['Storage_Index'] = ServiceLevelSettings::getStorageIndexByValue($css->Additional_Storage); $result['Additional_Fee'] = $add_users_price * $css->Additional_Users + $add_projects_price*($css->Additional_Projects) + $add_storage_price* ($result['Storage_Index']); //$result['Additional_Storage'] =$settings-> Additional_Storage; $users = UsersClientList::model()->findAllByAttributes(array( 'Client_ID'=>$client_id )); $result['Used_Users'] =count($users); $result['Used_Projects'] = Projects::clientProjectUsage(); $result['Used_Storage'] = ceil(Images::getUsedStorage($client_id)); return $result; }
public static function CalculateBaseFee($tier_levels_array) { $fee = 0; $w9_exist =false; //if this flag is true we have to cons W9 Tier value from the result price; $w9_price = 0; foreach ($tier_levels_array as $id) { $serviceLevel = ServiceLevelSettings::model()->findByPk($id); if( $serviceLevel->Service_Level_ID == 1) { $w9_exist =true; $w9_price = $serviceLevel->Base_Fee; } if ($serviceLevel) { $fee += $serviceLevel->Base_Fee; } } if ($w9_exist && $fee>$w9_price) {$fee = $fee - $w9_price;} //prices higher than w9 price we have to reduce, because w9 service is included to other levels return $fee; }
/** * Get service settings */ public function actionGetServiceLevelSettings() { if (Yii::app()->request->isAjaxRequest) { $serviceLevelsList = array(); $serviceLevels = ServiceLevelSettings::model()->findAll(); foreach ($serviceLevels as $serviceLevel) { $serviceLevelsList[$serviceLevel->Service_Level_ID] = $serviceLevel; } echo CJSON::encode($serviceLevelsList); } }
/** * Add client Payment, apply new settings and update 'Active To' date * @param $clientID * @param $amount * @param $date */ public static function addClientPayment($clientID, $amount, $date, $monthly_payment,$paydoc_number='auto') { $client = Clients::model()->with('service_settings', 'pending_service_settings','company.adreses')->findByPk($clientID); //$user = Users::model()->with('person')->findByPk(Yii::app()->user->userID); //$uid = UsersClientList::model()->findByAttributes(array('Client_ID'=>$clientID))->User_ID; $user = Users::model()->with('person')->findByPk(Yii::app()->user->userID); $sumToPay=$amount; //$sum_settings = ServiceLevelSettings::getSummarySettings($clientID); if ($client && $amount > 0 && preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $date)) { $pendingSettings = $client->pending_service_settings; $currentSettings = $client->service_settings; $dcss = DelayedClientServiceSettings::model()->findByPk(Yii::app()->user->clientID); if($dcss) {$dcss->delete();} $sum_settings = ClientServiceSettings::getBaseTierValues(explode(',',$pendingSettings->Service_Level_ID)); //variables for email notification, not for logic $settingsBefore = "Level : ".$currentSettings->Service_Level_ID." Users:".$currentSettings->Additional_Users." Projects:".$currentSettings->Additional_Projects." Storage : ". $currentSettings->Additional_Storage; $dateBefore=date_format(date_create($currentSettings->Active_To),'Y-m-d'); $tierNameBefore=ServiceLevelSettings::model()->findByPk($currentSettings->Service_Level_ID)->Tier_Name; $settings_are_delayed = $pendingSettings->Pending_Active_From=='0000-00-00' ? 0 : 1; // begin transaction $transaction = Yii::app()->db->beginTransaction(); try { $payment = new ServicePayments(); $payment->Client_ID = $clientID; $payment->Payment_Date = $date; $payment->Payment_Amount = $amount; $payment->Payment_Amount = $paydoc_number; if ($payment->validate()) { // save payment $payment->save(); //echo "Payment validated and saved\n"; $dateFrom = $currentSettings->Active_To; if ($dateFrom < date('Y-m-d')) { $dateFrom = date('Y-m-d'); } // apply new settings if necessary if ($pendingSettings && $amount >= $pendingSettings->Fee_To_Upgrade && !$monthly_payment && !$settings_are_delayed) { $currentSettings->Service_Level_ID = $pendingSettings->Service_Level_ID; $currentSettings->Additional_Users = $pendingSettings->Additional_Users-$sum_settings['Users_Count']; $currentSettings->Additional_Projects = $pendingSettings->Additional_Projects;//-$sum_settings['Projects_Count']; $currentSettings->Additional_Storage = $pendingSettings->Additional_Storage;//-$sum_settings['Storage_Count']; $currentSettings->Active_To = $pendingSettings->Pending_Active_To; $currentSettings->Fee = $pendingSettings->Fee; $currentSettings->save(); $user_client_settings = $client->service_settings; $user_tier_settings = TiersSettings::agregateTiersSettings($user_client_settings->Service_Level_ID); Yii::app()->user->setState('tier_settings', $user_tier_settings); // $amount -= $pendingSettings->getCurrentAmountToUpgrade($dateFrom); $pendingSettings->delete(); } if ($pendingSettings && $amount >= $pendingSettings->Fee_To_Upgrade && !$monthly_payment && $settings_are_delayed) { /// DelayedClientServiceSettings::createDelayedFromPending($pendingSettings,$clientID,$sum_settings['Users_Count'], $sum_settings['Projects_Count'],$sum_settings['Storage_Count'] ); /* $dcss->Additional_Users = $pendingSettings->Additional_Users-$sum_settings['Users_Count']; $dcss->Additional_Projects = $pendingSettings->Additional_Projects-$sum_settings['Projects_Count']; $dcss->Additional_Storage = $pendingSettings->Additional_Storage-$sum_settings['Storage_Count'];*/ } // update Active_To date if ( $monthly_payment) { /*$addDays = ceil($amount/($currentSettings->Fee/self::DEFAULT_DAYS_IN_MONTH)); $dateOb = date_create($dateFrom); date_add($dateOb, date_interval_create_from_date_string($addDays . ' days')); $activeTo = date_format($dateOb, 'Y-m-d');*/ $number_of_periods = floor($amount/$currentSettings->Fee); $activeTo = strtotime(date("m/d/Y", strtotime($dateFrom)) . " +".$number_of_periods." month"); $currentSettings->Active_To = date('Y-m-d',$activeTo); $currentSettings->save(); if ( $pendingSettings ) { //we need to update active_to date in the pending settings. $pendingSettings->Pending_Active_To = date('Y-m-d',$activeTo); $pendingSettings->save(); } } $transaction->commit(); //variables for email notification, not for logic $email=$user->person->Email; $settingsAfter = "Level : ".$currentSettings->Service_Level_ID." Users:".$currentSettings->Additional_Users." Projects:".$currentSettings->Additional_Projects." Storage : ". $currentSettings->Additional_Storage; $dateAfter=date_format(date_create($currentSettings->Active_To),'Y-m-d'); $tierNameAfter=ServiceLevelSettings::model()->findByPk($currentSettings->Service_Level_ID)->Tier_Name; $company_name=$client->company->Company_Name; Mail::notifyAdminAboutStripePaymentExecuted($email,$company_name,$settingsBefore,$settingsAfter,$tierNameBefore,$tierNameAfter,$dateBefore,$dateAfter,$sumToPay); } else { Yii::app()->user->setFlash('error', "ServicePayments validation error!"); } } catch(Exception $e) { $transaction->rollback(); } } }