[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/cron/modules/SalesOrder/ -> RecurringInvoice.service (source)

   1  <?php
   2  /*********************************************************************************
   3    ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4     * ("License"); You may not use this file except in compliance with the License
   5     * The Original Code is:  vtiger CRM Open Source
   6     * The Initial Developer of the Original Code is vtiger.
   7     * Portions created by vtiger are Copyright (C) vtiger.
   8     * All Rights Reserved.
   9    *
  10   ********************************************************************************/
  11  require_once ('include/utils/utils.php');
  12  require_once ('include/logging.php');
  13  
  14  // Get the list of Invoice for which Recurring is enabled.
  15  
  16  global $adb, $log;
  17  $log =& LoggerManager::getLogger('RecurringInvoice');
  18  $log->debug("invoked RecurringInvoice");
  19  
  20  $sql="SELECT vtiger_salesorder.salesorderid, recurring_frequency, start_period, end_period, last_recurring_date,
  21           payment_duration, invoice_status FROM vtiger_salesorder
  22           INNER JOIN vtiger_crmentity ON vtiger_salesorder.salesorderid = vtiger_crmentity.crmid AND vtiger_crmentity.deleted = 0
  23           INNER JOIN vtiger_invoice_recurring_info ON vtiger_salesorder.salesorderid = vtiger_invoice_recurring_info.salesorderid
  24           WHERE DATE_FORMAT(start_period,'%Y-%m-%d') <= '".date('Y-m-d')."' AND DATE_FORMAT(end_period,'%Y-%m-%d') >= '".date('Y-m-d')."'";
  25  $result = $adb->pquery($sql, array());
  26  $no_of_salesorder = $adb->num_rows($result);
  27  
  28  for($i=0; $i<$no_of_salesorder;$i++) {
  29      $salesorder_id = $adb->query_result($result, $i,'salesorderid');
  30      $start_period = $adb->query_result($result, $i,'start_period');
  31      $end_period = $adb->query_result($result, $i,'end_period');
  32      $last_recurring_date = $adb->query_result($result, $i,'last_recurring_date');
  33      $recurring_frequency = $adb->query_result($result, $i,'recurring_frequency');
  34  
  35      if ($last_recurring_date == NULL  || $last_recurring_date == '' || $last_recurring_date == '0000-00-00') {
  36          $last_recurring_date = $start_period;
  37      }
  38      $currentDate = date('Y-m-d');
  39      if(strtotime($last_recurring_date) < strtotime($currentDate)){
  40          $last_recurring_date = $currentDate;
  41      }
  42      $currentRecurringDate = $last_recurring_date;
  43  
  44      if(strtotime($currentRecurringDate) > strtotime($end_period)) {
  45          continue;
  46      }
  47  
  48      switch(strtolower($recurring_frequency)) {
  49          case 'daily'    :    $period = '+1 day';        break;
  50          case 'weekly'    :    $period = '+1 week';    break;
  51          case 'monthly'    :    $period = '+1 month';    break;
  52          case 'quarterly':    $period = '+3 month';    break;
  53          case 'yearly'    :    $period = '+1 year';    break;
  54          default            :    $period = '';
  55      }
  56  
  57      if (strtotime($currentRecurringDate) <= strtotime($currentDate)) {
  58          list($y, $m, $d) = explode('-', $last_recurring_date);
  59          $nextRecurringDate = date('Y-m-d', strtotime($period, mktime(0, 0, 0, $m, $d, $y)));
  60      } else {
  61          $nextRecurringDate = $currentRecurringDate;
  62      }
  63      if(strtotime($currentRecurringDate) == strtotime($currentDate)) {
  64          createInvoice($salesorder_id);
  65          
  66      }
  67      if(strtotime($nextRecurringDate) != strtotime($last_recurring_date)){
  68          $adb->pquery('UPDATE vtiger_invoice_recurring_info SET last_recurring_date = ? WHERE salesorderid = ?', array($nextRecurringDate, $salesorder_id));
  69      }
  70  }
  71  
  72  /* Function to create a new Invoice using the given Sales Order id */
  73  function createInvoice($salesorder_id) {
  74      require_once ('include/utils/utils.php');
  75      require_once ('modules/SalesOrder/SalesOrder.php');
  76      require_once ('modules/Invoice/Invoice.php');
  77      require_once ('modules/Users/Users.php');
  78  
  79      global $log, $adb;
  80      global $current_user;
  81  
  82      // Payment duration in days
  83      $payment_duration_values = Array(
  84          'net 01 day' => '1',
  85          'net 05 days' => '5',
  86          'net 07 days' => '7',
  87          'net 10 days' => '10',
  88          'net 15 days' => '15',
  89          'net 30 days' => '30',
  90          'net 45 days' => '45',
  91          'net 60 days' => '60'
  92      );
  93  
  94      if(!$current_user) {
  95          $current_user = Users::getActiveAdminUser();
  96      }
  97      $so_focus = new SalesOrder();
  98      $so_focus->id = $salesorder_id;
  99      $so_focus->retrieve_entity_info($salesorder_id,"SalesOrder");
 100      foreach($so_focus->column_fields as $fieldname=>$value) {
 101          $so_focus->column_fields[$fieldname] = decode_html($value);
 102      }
 103  
 104      $focus = new Invoice();
 105      // This will only fill in the basic columns from SO to Invoice and also Update the SO id in new Invoice
 106      $focus = getConvertSoToInvoice($focus,$so_focus,$salesorder_id);
 107      
 108      // Pick up the Payment due date based on the Configuration in SO
 109      $payment_duration = $so_focus->column_fields['payment_duration'];
 110      $due_duration = $payment_duration_values[trim(strtolower($payment_duration))];
 111      $durationinsec = mktime(0,0,0,date('m'),date('d')+$due_duration,date('Y'));
 112  
 113      // Cleanup focus object, to duplicate the Invoice.
 114      $focus->id = '';
 115      $focus->mode = '';
 116      $focus->column_fields['invoicestatus'] = $so_focus->column_fields['invoicestatus'];
 117      $focus->column_fields['invoicedate'] = date('Y-m-d');
 118      $focus->column_fields['duedate'] = date('Y-m-d', $durationinsec);
 119  
 120      // Additional SO fields to copy -> Invoice field name mapped to equivalent SO field name
 121      $invoice_so_fields = Array (
 122          'txtAdjustment' => 'txtAdjustment',
 123          'hdnSubTotal' => 'hdnSubTotal',
 124          'hdnGrandTotal' => 'hdnGrandTotal',
 125          'hdnTaxType' => 'hdnTaxType',
 126          'hdnDiscountPercent' => 'hdnDiscountPercent',
 127          'hdnDiscountAmount' => 'hdnDiscountAmount',
 128          'hdnS_H_Amount' => 'hdnS_H_Amount',
 129          'assigned_user_id' => 'assigned_user_id',
 130          'currency_id' => 'currency_id',
 131          'conversion_rate' => 'conversion_rate',
 132      );
 133      foreach($invoice_so_fields as $invoice_field => $so_field) {
 134          $focus->column_fields[$invoice_field] = $so_focus->column_fields[$so_field];
 135      }
 136      $focus->_salesorderid = $salesorder_id;
 137      $focus->_recurring_mode = 'recurringinvoice_from_so';
 138      $focus->save("Invoice");
 139  }
 140  
 141  
 142  
 143  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1