/** * Add Company service level payment */ public function actionAddCompanyPayment() { if (Yii::app()->request->isAjaxRequest && isset($_POST['amount']) && isset($_POST['clientID']) && isset($_POST['date'])) { $result = ''; $amount = floatval($_POST['amount']); $clientID = intval($_POST['clientID']); $date = trim($_POST['date']); $number = trim($_POST['number']); $date = Helper::checkDate($date); $client = Clients::model()->findByPk($clientID); if ($client && $amount > 0 && preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $date)) { $result = ServicePayments::addJustPayment($clientID, $amount, $date,$number,''); } echo CJSON::encode($result); } }
public static function generateInvoiceNumber($clientId) { /* $sql = "SELECT COUNT(*) FROM service_payments where Client_ID='".$clientId."'"; $num = Yii::app()->db->createCommand($sql)->queryScalar();*/ $criteria = new CDbCriteria(); $criteria->condition = 't.Client_ID = '.$clientId; //$criteria->group = 'UserId'; $num = ServicePayments::Model()->count($criteria); $res=10000+$num; return $res; }
/** * 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(); } } }