// ==========================================================================
// Project:   xTuple Postbooks - Business Management System Framework        
// Copyright: ©2012 OpenMFG LLC, d/b/a xTuple                             
// ==========================================================================

/*globals XM */

sc_require('mixins/_terms');

/**
  @class

  @extends XM.Document
*/
XM.Terms = XM.Document.extend(XM._Terms,
  /** @scope XM.Terms.prototype */ {

  documentKey: 'code',

  // .................................................
  // CALCULATED PROPERTIES
  //

  //..................................................
  // METHODS
  //

  //..................................................
  // OBSERVERS
  //

});


XM.Terms.mixin( /** @scope XM.Terms */ {

// Terms Type values
/**
  Days
  
  @static
  @constant
  @type String
  @default D
*/
  DAYS: 'D',

/**
  Proximo
  
  @static
  @constant
  @type String
  @default P
*/
  PROXIMO: 'P'

});

@@ -658,89 +658,1053 @@
  //%0A
-%0A  //..................................................%0A  // OBSERVERS%0A  //%0A%0A%7D);%0A
+  %0A  calculateDueDate: function(date) %7B%0A    return XM.Terms.calculateDueDate(this, date);%0A  %7D%0A%0A  //..................................................%0A  // OBSERVERS%0A  //%0A%0A%7D);%0A%0A/**%0A  Calculate a due date based on a given document date.%0A  %0A  @param %7BXM.Terms%7D terms%0A  @param %7BSC.DateTime%7D document date.%0A  @returns SC.DateTime%0A*/%0AXM.Terms.calculateDueDate = function(terms, date) %7B%0A  if (!SC.kindOf(terms, XM.Terms)) return date;%0A  var termsType = terms.get('termsType'),%0A      dueDays = terms.get('dueDays'),%0A      cutOffDay = terms.get('cutOffDay'),%0A      result = date;%0A      %0A  // handle days%0A  if (termsType === 'D') %7B%0A    result = date.advance(%7B day: dueDays %7D);%0A    %0A  // handle proximo (day of month)%0A  %7D else if (termsType === 'P') %7B%0A    result = SC.DateTime.create(%7B %0A      month: date.get('month'), %0A      day: dueDays, %0A      year: date.get('year')%0A    %7D);%0A    %0A    // if day has already cut off, then advance to next month%0A    if (date.get('day') %3E cutOffDay) %7B%0A      result = result.advance(%7B month: 1 %7D);%0A    %7D%0A  %7D%0A  return result%0A%7D
 %0A%0AXM

add function to calculate due date based on terms.