Advertisement
matthewpoer

SugarCRM Opportunity before_save hook to create a Contract

Aug 14th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Class to assist in creating Contracts from Opportunities given that conditions are met.
  4.  *
  5.  * Conditions are that the Opportunity is being marked as "Closed Won" (i.e. that it is being changed
  6.  * to this status from another status) and that it has a valid value in install_c.
  7.  */
  8. class psi_create_contract{
  9.  
  10.     /**
  11.      * @var Opportunity object that we get just by being here
  12.      */
  13.     private $bean;
  14.    
  15.     /**
  16.      * @var Contract object we create later in the script
  17.      */
  18.     private $contract;
  19.    
  20.     /**
  21.      * @var bool set debug value for this script
  22.      */
  23.     private $debug_to_screen = TRUE;
  24.    
  25.     /**
  26.      * init/entry point from the Logic Hook, controller method
  27.      */
  28.     public function psi_create_contract($bean,$event,$args){
  29.         $this->bean = $bean;
  30.         $this->debug($this->check_sales_stage(),"BOOL result of check_sales_stage ");
  31.         $this->debug($this->check_install_date(),"BOOL result of check_install_date ");
  32.         if($this->check_sales_stage() && $this->check_install_date()){
  33.             $this->create_contract();
  34.         }
  35.     }
  36.  
  37.     /**
  38.      * check to see if the sales stage is set to Closed Won, and that this is a change
  39.      * to this value from some other value
  40.      *
  41.      * @returns bool
  42.      */
  43.     private function check_sales_stage(){
  44.         if($this->bean->sales_stage == 'Closed Won' && $this->bean->sales_stage != $this->bean->fetched_row['sales_stage'])
  45.             return true;
  46.         else
  47.             return false;
  48.     }
  49.    
  50.     /**
  51.      * check to see if the install_date_c is set
  52.      *
  53.      * @returns bool
  54.      */
  55.     private function check_install_date(){
  56.         if(isset($this->bean->install_c) && !empty($this->bean->install_c))
  57.             return true;
  58.         else
  59.             return false;
  60.     }
  61.    
  62.     /**
  63.      * create the contract using provided information, then set
  64.      * $this->contract with the new object
  65.      */
  66.     private function create_contract(){
  67.         $this->debug($this->bean->install_c, "Passed in date value ");
  68.         $contract = new Contract();
  69.         $contract->name = $this->bean->name . " Contract";
  70.         $contract->start_date = $this->bean->install_c;
  71.         $contract->end_date = $this->add_days_date_string($this->bean->install_c,'+1 Year');
  72.         $contract->opportunity_id = $this->bean->id;
  73.         $contract->opportunity_name = $this->bean->name;
  74.         $contract->account_id = $this->bean->account_id;
  75.         $contract->account_name = $this->bean->account_name;
  76.         $contract->total_contract_value = $this->bean->amount;
  77.         $contract->assigned_user_id = $this->bean->assigned_user_id;
  78.         $contract->assigned_user_name = $this->bean->assigned_user_name;
  79.         $contract->save();
  80.         $this->contract = $contract;
  81.         $this->debug($this->contract,"Newly created contract ",FALSE);
  82.     }
  83.    
  84.     /**
  85.      * convert a date string of specified format to date object, return same-format
  86.      * string plus added number of days/months/years
  87.      *
  88.      * @param $date_string string of date
  89.      * @param $addendum amount of time/days to add, e.g. +8 months
  90.      * @param $format string date-format
  91.      * @return string of date plus 1 year
  92.      */
  93.     private function add_days_date_string($date_string,$addendum = '+1 Year',$format = "Y-m-d"){
  94.         $do = DateTime::createFromFormat($format,$date_string);
  95.         $do->modify($addendum);
  96.         $this->debug($do, "The plus-one-year value ");
  97.         return $do->format($format);
  98.     }
  99.    
  100.     /**
  101.      * Basic var_dump function, checks $this->debug_to_screen and spits out to screen if true
  102.      *
  103.      * @param $holder mixed array, object, string, bool - whatever we want to spit out
  104.      * @param $msg string message to put into the screen and debug log
  105.      * @param $die bool kill the script
  106.      */
  107.     private function debug($holder,$msg,$die = FALSE){
  108.         $GLOBALS['log']->debug($msg . print_r($holder,TRUE));
  109.         if($this->debug_to_screen){
  110.             echo "<h3>{$msg}</h3>";
  111.             var_dump($holder);
  112.         }
  113.         if($die) die();
  114.     }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement