Module: ProjectorPWS

Defined in:
lib/projector_pws.rb,
lib/projector_pws/legacy.rb,
lib/projector_pws/version.rb

Overview

ProjectorPWS Module

Defined Under Namespace

Modules: Legacy

Constant Summary collapse

WSDL =

Service Definition

'https://secure.projectorpsa.com/OpsProjectorWcfSvc/PwsProjectorServices.svc?wsdl'
BASE_URL =

Base URL

'https://secure.projectorpsa.com'
URL_PATH =

URL Path

'OpsProjectorWcfSvc/PwsProjectorServices.svc'
NAMESPACE =

Namespaces

'http://projectorpsa.com/PwsProjectorServices/'
EXTRA_NAMESPACES =
{
  'xmlns:req' => 'http://projectorpsa.com/DataContracts/Requests/',
  'xmlns:tim' => 'http://projectorpsa.com/DataContracts/Shared/TimeAndCost/',
  'xmlns:rep' => 'http://projectorpsa.com/DataContracts/Shared/Report/',
  'xmlns:com' => 'http://projectorpsa.com/DataContracts/Shared/Common/'
}
WEEK_START =

Work Week

:monday
WEEK_END =
:friday
WEEKEND_START_TIME =

Weekend Start Time

16
DAYS_PER_WEEK =

Days Per Week

5
HOURS_PER_DAY =

Hours Per Day

8
HOURS_PER_DAY_REAL =

Real Hours Per Day

24
MINUTES_PER_HOUR =

Minutes Per Hour

60
MINUTES_PER_DAY =

Minutes Per Day

HOURS_PER_DAY * MINUTES_PER_HOUR
MINUTES_PER_WEEK =

Minutes Per Week

MINUTES_PER_DAY * DAYS_PER_WEEK
VERSION =

Version

'0.1.11'

Class Method Summary collapse

Class Method Details

.authenticate(username, password, account_code = nil) ⇒ Object

Authenticate



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/projector_pws.rb', line 67

def self.authenticate username, password,  = nil

  # Prepare URLs
  next_url = BASE_URL
  last_url = ''

  # Prepare Credentials
  creds = {}
  creds['req:AccountCode'] =  if 
  creds['req:Password'] = password
  creds['req:UserName'] = username

  # Loop to obtain Ticket
  c = nil
  ticket = nil
  until ticket

    # Authenticate
    begin
      c = open next_url
      r = c.call(:pws_authenticate, message: { 'pws:serviceRequest' => creds }).body[:pws_authenticate_response][:pws_authenticate_result]
      last_url = next_url
      next_url = r[:redirect_url]
    rescue Savon::SOAPFault => e
      raise "Authentication failed - #{e.to_hash[:fault][:detail][:pws_fault][:messages][:pws_message][:error_text]}"
    end

    raise 'API Error (redirect loop)' if next_url == last_url
    ticket = r[:session_ticket]
  end

  # Yield if block given
  if block_given?
    r = yield c, ticket
    unauthenticate c, ticket
    return r
  end

  [c, ticket]
end

.current_week_endObject

Current Week End



353
354
355
# File 'lib/projector_pws.rb', line 353

def self.current_week_end
  current_week_start.wnext(WEEK_END)
end

.current_week_startObject

Current Week Start



348
349
350
# File 'lib/projector_pws.rb', line 348

def self.current_week_start
  is_weekend? ? Date.today.wnext(WEEK_START) : (Date.today.monday? ? Date.today : Date.today.wlast(WEEK_START))
end

.get_cost_centers(c, ticket) ⇒ Object

Get Cost Centers



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/projector_pws.rb', line 189

def self.get_cost_centers c, ticket

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:IncludeEngagementCostCentersFlag'] = true
  params['tim:IncludeResourceCostCentersFlag'] = true
  params['tim:IncludeNonResourceNonEngagementCostCentersFlag'] = true

  # Fetch Resources
  c.call(:pws_get_cost_center_list, message: { 'pws:serviceRequest' => params }).body[:pws_get_cost_center_list_response][:pws_get_cost_center_list_result][:cost_centers]
end

.get_expense_document(c, ticket, doc_number, include_cost_cards = true, include_receipts = false) ⇒ Object

Get Expense Document



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/projector_pws.rb', line 202

def self.get_expense_document c, ticket, doc_number, include_cost_cards = true, include_receipts = false

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:ExpenseDocumentIdentity'] = { 'com:DocumentNumber' => doc_number }
  params['tim:RetrieveCostCardsFlag'] = include_cost_cards
  params['tim:RetrieveReceiptsFlag'] = include_receipts

  # Fetch Expense Document
  c.call(:pws_get_expense_document, message: { 'pws:serviceRequest' => params }).body[:pws_get_expense_document_response][:pws_get_expense_document_result][:expense_document]
end

.get_expense_documents(c, ticket, reps = nil) ⇒ Object

Get Expense Documents



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/projector_pws.rb', line 215

def self.get_expense_documents c, ticket, reps = nil

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  if reps
    params['tim:ExpenseDocumentIdentity'] = []
    reps.each { |r| params['tim:ExpenseDocumentIdentity'] << { 'com:DocumentNumber' => r } }
  end

  # Fetch Expense Documents
  c.call(:pws_get_expense_document, message: { 'pws:serviceRequest' => params }).body[:pws_get_expense_document_response][:pws_get_expense_document_result]
end

.get_free_resources(c, ticket, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Free Resources (non-busy) with associated minutes



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/projector_pws.rb', line 253

def self.get_free_resources c, ticket, start_date = current_week_start, end_date = current_week_end

  # Get all resources
  res = ProjectorPWS.get_resources c, ticket

  # Collect free time
  res.collect { |r| {
    resource: r,
    free_hours: get_resource_free_hours(c, ticket, r, start_date, end_date).to_f
  } }.select { |x| x[:free_hours] > 0 }
end

.get_report(c, ticket, report_uid, format = nil) ⇒ Object

Get Report Output



116
117
118
119
120
121
122
123
124
125
# File 'lib/projector_pws.rb', line 116

def self.get_report c, ticket, report_uid, format = nil

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['rep:ReportIdentity'] = { 'com:ReportUid' => report_uid }
  params['rep:Format'] = format if format

  # Fetch Report
  c.call(:pws_get_report_output, message: { 'pws:serviceRequest' => params }).body
end

.get_resource_active_hours(c, ticket, resource, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Active Hours



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/projector_pws.rb', line 282

def self.get_resource_active_hours c, ticket, resource, start_date = current_week_start, end_date = current_week_end

  # Acquire working schedule for resource
  wsched = ProjectorPWS.get_resource_working_schedule(c, ticket, resource[:resource_uid], start_date, end_date)[:pws_working_schedule_day] rescue(return(0))
  wsched = [wsched] unless wsched.is_a? Array
  wsched.compact!

  # Extract Working Minutes
  work = wsched.inject(0) { |a, e| a + e[:working_minutes].to_i }

  # Determine Active Hours
  work.to_f / MINUTES_PER_HOUR.to_f
end

.get_resource_available_timeoff(c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Available Timeoff



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/projector_pws.rb', line 150

def self.get_resource_available_timeoff c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:EndDate'] = end_date.strftime '%Y-%m-%dz'
  params['tim:ResourceIdentity'] = { 'com:ResourceUid' => resource_uid } if resource_uid
  params['tim:StartDate'] = start_date.strftime '%Y-%m-%dz'

  # Fetch Resource Available Timeoff
  c.call(:pws_get_resource_available_time_off, message: { 'pws:serviceRequest' => params }).body[:pws_get_resource_available_time_off_response][:pws_get_resource_available_time_off_result][:resource_available_time_off_schedule]
end

.get_resource_cost_cards(c, ticket, resource_uid = nil, exclude_before = nil, flags = { approved: true }) ⇒ Object

Get Resource Cost Cards



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/projector_pws.rb', line 176

def self.get_resource_cost_cards c, ticket, resource_uid = nil, exclude_before = nil, flags = { approved: true }

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:ExcludeApprovedCardsBefore'] = exclude_before.strftime '%Y-%m-%dz' if exclude_before
  %w(approved approved_to_pay draft paid received rejected submitted transmitted).each { |f| params["tim:Include#{f.camelcase}Flag"] = flags[f.to_sym] if flags.include? f.to_sym }
  params['tim:ResourceIdentity'] = { 'com:ResourceUid' => resource_uid } if resource_uid

  # Fetch Resource Cost Cards
  c.call(:pws_get_resource_cost_cards, message: { 'pws:serviceRequest' => params }).body[:pws_get_resource_cost_cards_response][:pws_get_resource_cost_cards_result][:cost_cards]
end

.get_resource_free_hours(c, ticket, resource, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Free Hours



266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/projector_pws.rb', line 266

def self.get_resource_free_hours c, ticket, resource, start_date = current_week_start, end_date = current_week_end

  # Get scheduled hours
  scheduled_hours = get_resource_scheduled_hours c, ticket, resource, start_date, end_date

  # Get active hours
  active_hours = get_resource_active_hours c, ticket, resource, start_date, end_date

  # Compute time off into active hours
  active_hours = active_hours - scheduled_hours[:toff]

  # Compute free time (active unscheduled time)
  active_hours - scheduled_hours[:work]
end

.get_resource_schedule(c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Schedule



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/projector_pws.rb', line 135

def self.get_resource_schedule c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:EndDate'] = end_date.strftime '%Y-%m-%dz'
  params['tim:IncludeScheduledTimeFlag'] = :true
  params['tim:IncludeTimeOffFlag'] = :true
  params['tim:ResourceIdentity'] = { 'com:ResourceUid' => resource_uid } if resource_uid
  params['tim:StartDate'] = start_date.strftime '%Y-%m-%dz'

  # Fetch Resource Schedule
  c.call(:pws_get_resource_schedule, message: { 'pws:serviceRequest' => params }).body[:pws_get_resource_schedule_response][:pws_get_resource_schedule_result][:resource_schedule]
end

.get_resource_scheduled_hours(c, ticket, resource, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Scheduled Hours



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/projector_pws.rb', line 297

def self.get_resource_scheduled_hours c, ticket, resource, start_date = current_week_start, end_date = current_week_end

  # Get schedule
  sched = get_resource_schedule(c, ticket, resource[:resource_uid], start_date, end_date) rescue(return({ work: 0, toff: 0 }))

  # Extract scheduled work
  sched_work = sched[:roles][:pws_schedule_role] rescue []
  sched_work = [sched_work] unless sched_work.is_a? Array
  sched_work.compact!

  # Extract scheduled time off
  time_off = sched[:time_off][:pws_schedule_time_off] rescue []
  time_off = [time_off] unless time_off.is_a? Array
  time_off.compact!

  # Run through time off { collect total minutes }
  time_off_minutes = time_off.inject(0) do |a, e|

    # Extract Dates
    dates = e[:time_off_dates][:pws_schedule_time_off_date] rescue []
    dates = [dates] unless dates.is_a? Array
    dates.compact!

    # Collect time off dates
    a + dates.inject(0) { |da, de| da + time_off_minutes(de[:time_off_minutes].to_i) }
  end

  # Run through schedule { collect total minutes }
  scheduled_minutes = sched_work.inject(0) do |a, e|

    # Extract bookings
    bookings = e[:bookings][:pws_schedule_booking] rescue []
    bookings = [bookings] unless bookings.is_a? Array
    bookings.compact!

    # Collect bookings
    a + (bookings.inject(0) { |ba, be| ba + be[:scheduled_minutes].to_i })
  end

  {
    work: scheduled_minutes.to_f / MINUTES_PER_HOUR.to_f,
      toff: time_off_minutes.to_f / MINUTES_PER_HOUR.to_f
  }
end

.get_resource_working_schedule(c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end) ⇒ Object

Get Resource Working Schedule



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/projector_pws.rb', line 163

def self.get_resource_working_schedule c, ticket, resource_uid = nil, start_date = current_week_start, end_date = current_week_end

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:EndDate'] = end_date.strftime '%Y-%m-%dz'
  params['tim:ResourceIdentity'] = { 'com:ResourceUid' => resource_uid } if resource_uid
  params['tim:StartDate'] = start_date.strftime '%Y-%m-%dz'

  # Fetch Resource Working Schedule
  c.call(:pws_get_resource_working_schedule, message: { 'pws:serviceRequest' => params }).body[:pws_get_resource_working_schedule_response][:pws_get_resource_working_schedule_result][:working_schedule]
end

.get_resources(c, ticket) ⇒ Object

Get Resources



128
129
130
131
132
# File 'lib/projector_pws.rb', line 128

def self.get_resources c, ticket

  # Fetch Resources
  c.call(:pws_get_resource_list, message: { 'pws:serviceRequest' => { 'req:SessionTicket' => ticket } }).body[:pws_get_resource_list_response][:pws_get_resource_list_result][:resources][:pws_resource_summary]
end

.is_weekend?Boolean

Is Weekend

Returns:

  • (Boolean)


358
359
360
# File 'lib/projector_pws.rb', line 358

def self.is_weekend?
  (Date.today.thursday? && (DateTime.now >= DateTime.now.change(hour: WEEKEND_START_TIME))) || Date.today.friday? || Date.today.saturday? || Date.today.sunday?
end

.open(url = BASE_URL) ⇒ Object

Open Client



60
61
62
63
64
# File 'lib/projector_pws.rb', line 60

def self.open url = BASE_URL
  c = Savon.client wsdl: WSDL, endpoint: service_url(url), namespace: NAMESPACE, env_namespace: :soapenv, namespace_identifier: :pws, namespaces: EXTRA_NAMESPACES
  return yield c if block_given?
  c
end

.service_url(base_url) ⇒ Object

Generate Service URL



343
344
345
# File 'lib/projector_pws.rb', line 343

def self.service_url base_url
  "#{base_url}/#{URL_PATH}"
end

.set_cost_cards_payment_workflow_status(c, ticket, cost_card_uids, status, send_approval_email = false, send_approval_to_pay_email = false, send_paid_email = true) ⇒ Object

Update Cost Cards Payment Workflow Status



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/projector_pws.rb', line 229

def self.set_cost_cards_payment_workflow_status c, ticket, cost_card_uids, status, send_approval_email = false, send_approval_to_pay_email = false, send_paid_email = true

  # Prepare Params
  params = { 'req:SessionTicket' => ticket }
  params['tim:PaymentWorkflowStatus'] = status
  params['tim:SendApprovalEmailFlag'] = send_approval_email
  params['tim:SendApprovalToPayEmailFlag'] = send_approval_to_pay_email
  params['tim:SendPaidEmailFlag'] = send_paid_email
  params['tim:StatusOrders'] = cost_card_uids.collect do |cc|
    { 'tim:PwsCostPwsChangeOrder' => {
      'tim:ApproveToPayFlag' => true,
      'tim:CostCardIdentities' => {
        'tim:PwsVersionedCostCardRef' => {
          'com:CostCardUid' => cc
        }
      }
    } }
  end

  # Set Cost Card Payment Workflow Status
  c.call(:pws_set_cost_card_payment_workflow_status, message: { 'pws:serviceRequest' => params }).body[:pws_set_cost_card_payment_workflow_status_response][:pws_set_cost_card_payment_workflow_status_result]
end

.time_off_minutes(x) ⇒ Object

Time Off Minutes (adjust for full-day holidays)



363
364
365
# File 'lib/projector_pws.rb', line 363

def self.time_off_minutes x
  (x == HOURS_PER_DAY_REAL * MINUTES_PER_HOUR) ? (HOURS_PER_DAY * MINUTES_PER_HOUR) : x
end

.unauthenticate(c, ticket) ⇒ Object

Unauthenticate



109
110
111
112
113
# File 'lib/projector_pws.rb', line 109

def self.unauthenticate c, ticket

  # Unauthenticate
  c.call(:pws_unauthenticate, message: { 'pws:serviceRequest' => { 'req:SessionTicket' => ticket } }).body
end