Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class to assist in creating Contracts from Opportunities given that conditions are met.
- *
- * Conditions are that the Opportunity is being marked as "Closed Won" (i.e. that it is being changed
- * to this status from another status) and that it has a valid value in install_c.
- */
- class psi_create_contract{
- /**
- * @var Opportunity object that we get just by being here
- */
- private $bean;
- /**
- * @var Contract object we create later in the script
- */
- private $contract;
- /**
- * @var bool set debug value for this script
- */
- private $debug_to_screen = TRUE;
- /**
- * init/entry point from the Logic Hook, controller method
- */
- public function psi_create_contract($bean,$event,$args){
- $this->bean = $bean;
- $this->debug($this->check_sales_stage(),"BOOL result of check_sales_stage ");
- $this->debug($this->check_install_date(),"BOOL result of check_install_date ");
- if($this->check_sales_stage() && $this->check_install_date()){
- $this->create_contract();
- }
- }
- /**
- * check to see if the sales stage is set to Closed Won, and that this is a change
- * to this value from some other value
- *
- * @returns bool
- */
- private function check_sales_stage(){
- if($this->bean->sales_stage == 'Closed Won' && $this->bean->sales_stage != $this->bean->fetched_row['sales_stage'])
- return true;
- else
- return false;
- }
- /**
- * check to see if the install_date_c is set
- *
- * @returns bool
- */
- private function check_install_date(){
- if(isset($this->bean->install_c) && !empty($this->bean->install_c))
- return true;
- else
- return false;
- }
- /**
- * create the contract using provided information, then set
- * $this->contract with the new object
- */
- private function create_contract(){
- $this->debug($this->bean->install_c, "Passed in date value ");
- $contract = new Contract();
- $contract->name = $this->bean->name . " Contract";
- $contract->start_date = $this->bean->install_c;
- $contract->end_date = $this->add_days_date_string($this->bean->install_c,'+1 Year');
- $contract->opportunity_id = $this->bean->id;
- $contract->opportunity_name = $this->bean->name;
- $contract->account_id = $this->bean->account_id;
- $contract->account_name = $this->bean->account_name;
- $contract->total_contract_value = $this->bean->amount;
- $contract->assigned_user_id = $this->bean->assigned_user_id;
- $contract->assigned_user_name = $this->bean->assigned_user_name;
- $contract->save();
- $this->contract = $contract;
- $this->debug($this->contract,"Newly created contract ",FALSE);
- }
- /**
- * convert a date string of specified format to date object, return same-format
- * string plus added number of days/months/years
- *
- * @param $date_string string of date
- * @param $addendum amount of time/days to add, e.g. +8 months
- * @param $format string date-format
- * @return string of date plus 1 year
- */
- private function add_days_date_string($date_string,$addendum = '+1 Year',$format = "Y-m-d"){
- $do = DateTime::createFromFormat($format,$date_string);
- $do->modify($addendum);
- $this->debug($do, "The plus-one-year value ");
- return $do->format($format);
- }
- /**
- * Basic var_dump function, checks $this->debug_to_screen and spits out to screen if true
- *
- * @param $holder mixed array, object, string, bool - whatever we want to spit out
- * @param $msg string message to put into the screen and debug log
- * @param $die bool kill the script
- */
- private function debug($holder,$msg,$die = FALSE){
- $GLOBALS['log']->debug($msg . print_r($holder,TRUE));
- if($this->debug_to_screen){
- echo "<h3>{$msg}</h3>";
- var_dump($holder);
- }
- if($die) die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement