Class: PaisLegacy::Pais

Inherits:
Object
  • Object
show all
Defined in:
lib/pais_legacy.rb

Constant Summary collapse

SERVER =

SERVER=“192.168.200.4”

"pais.com.au"
USER =
"map7"
KEY =
"/home/map7/.ssh/id_rsa"
MAX_RETRIES =
5
RETRY_DELAY =

seconds

1
SUSPENSE_ACCOUNT =
1599
CLEARING_ACCOUNT =
5199
TB_DIRECT_ACCOUNT =
8999

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clfi_read(client = nil) ⇒ Object



112
113
114
115
# File 'lib/pais_legacy.rb', line 112

def self.clfi_read(client=nil)
  out=execute_ssh_command(clfi_read_cmd(client.to_s))
  JSON.parse(out)
end

.clfi_read_cmd(client = nil) ⇒ Object


Client




104
105
106
107
108
109
110
# File 'lib/pais_legacy.rb', line 104

def self.clfi_read_cmd(client=nil)
  if client
    pais_api("clfi_read ","10000",client)
  else
    pais_api("clfi_read ","10000")
  end
end

.common_ledger_date(cmd, ledger, end_date) ⇒ Object



384
385
386
387
388
389
390
391
392
# File 'lib/pais_legacy.rb', line 384

def self.common_ledger_date(cmd,ledger,end_date)
  cmd=fglgo(cmd) + ' {{ccode}} {{ledger}} {{end_date}}'
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger.to_s)
    .gsub("{{end_date}}",end_date)

  puts cmd
  execute_ssh_command(cmd)
end

.delete_and_rebuild(ledger, end_date) ⇒ Object



398
399
400
# File 'lib/pais_legacy.rb', line 398

def self.delete_and_rebuild(ledger,end_date)
  common_ledger_date("delete_and_rebuild",ledger,end_date)
end

.execute_ssh_command(command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/pais_legacy.rb', line 33

def self.execute_ssh_command(command)
  output = ""
  retries = 0
  
  begin
    
    # Start an SSH session
    Net::SSH.start(SERVER,USER,keys: [KEY]) do |ssh|
      # Open a new SSH channel
      ssh.open_channel do |channel|
        # Request a pseudo-tty
        channel.request_pty do |ch, success|
          raise "Could not obtain pty" unless success

          # Execute the command
          ch.exec(command) do |ch, success|
            raise "Could not execute command" unless success

            # Collect the data received from the command's standard output
            ch.on_data do |c, data|
              output += data
            end

            # Collect the data received from the command's standard error
            ch.on_extended_data do |c, type, data|
              output += "stderr: #{data}"
            end

            ch.on_close do
              puts "Command execution finished"
            end
          end
        end
      end

      # Keep the session open until the command execution is finished
      ssh.loop
    end

  rescue Net::SSH::ConnectionTimeout => e
    retries += 1
    if retries <= MAX_RETRIES
      puts "Connection timed out, retrying (#{retries}/#{MAX_RETRIES})..."
      sleep RETRY_DELAY
      retry
    else
      puts "Max retries reached. Could not connect."
    end

  rescue Errno::ECONNRESET => e
    retries += 1
    if retries <= MAX_RETRIES
      puts "Connection reset by peer, retrying (#{retries}/#{MAX_RETRIES})..."
      sleep RETRY_DELAY
      retry
    else
      puts "Max retries reached. Could not connect."
    end
    
  rescue StandardError => e
    puts "An error occurred: #{e.message}"
  end
  
  output
end

.get_performance(ledger = "1", date_to = "30/06/2022", prior = false, from = 1, to = 9999) ⇒ Object


Performance Report


‘SCREEN T1 336 spar N 2 1 2 3 4 5 01/07/2021 30/06/2022 ’



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/pais_legacy.rb', line 181

def self.get_performance(ledger = "1", date_to = "30/06/2022",prior=false,from=1,to=9999)
  program="rglch10a"

  date_to = Date.parse(date_to)
  date_to = Date.parse("30/06/#{date_to.year}")
  date_from = Date.parse("01/07/#{date_to.year - 1}")

  cmd = fglgo("rptrun") +' pgs 1 pais {{program}} \'\{{printer}} \{{ccode}} \{{ledger}} \spar \N \2 \1 \2 \3 \4 \5 \{{date_from}} \{{date_to}} \' '
    .gsub("{{program}}",program)
    .gsub("{{printer}}","SCREEN")
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger)
    .gsub("{{account_from}}",from.to_s)
    .gsub("{{account_to}}",to.to_s)
    .gsub("{{date_from}}",date_from.strftime("%d/%m/%Y") || "01/07/2021")
    .gsub("{{date_to}}",date_to.strftime("%d/%m/%Y") || "30/06/2022")
    .gsub("{{report_type}}","1")
    .gsub("{{subaccs}}","N")

  out=execute_ssh_command(cmd)
  [out]
end

.get_trial_balance(ledger = "1", date_to = "30/06/2021", prior = false, from = 1, to = 9999) ⇒ Object


Trial Balance




207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/pais_legacy.rb', line 207

def self.get_trial_balance(ledger = "1", date_to = "30/06/2021",prior=false,from=1,to=9999)
  if prior
    program="rglch01p"
  else
    program="rglch01a"
  end

  cmd = fglgo("rptrun") +' pgs 1 pais {{program}} \'\{{printer}} \{{ccode}} \{{ledger}} \spar \{{account_from}} \{{account_to}} \{{date}}\ \1 \N\' '
    .gsub("{{program}}",program)
    .gsub("{{printer}}","SCREEN")
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger)
    .gsub("{{account_from}}",from.to_s)
    .gsub("{{account_to}}",to.to_s)
    .gsub("{{date}}",Date.parse(date_to).strftime("%d/%m/%Y") || "30/06/2021")
    .gsub("{{report_type}}","1")
    .gsub("{{subaccs}}","N")

  out=execute_ssh_command(cmd)

  # if from and to
  #   out.split("\n").each_with_index do |line,index|
  #     if index >= from and index <= to
  #       puts "#{index}-#{line}"
  #     end
  #   end
  # end

  out.split("\n")
end

.get_trial_balance_accounts(ledger = "1", date_to = "30/06/2021", periods = 1, from = 1, to = 9999) ⇒ Object

Get the accounts using the trial balance



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/pais_legacy.rb', line 239

def self.get_trial_balance_accounts(ledger = "1", date_to = "30/06/2021", periods=1,from=1,to=9999)

  results = []

  periods.times do |period|

    date_to = Date.parse(date_to)
    date_to = date_to.strftime("%d/%m/") + (date_to.year.to_i - period).to_s

    report = get_trial_balance(ledger,date_to,period>0,from,to)
    tb = PaisLegacy::TrialBalance.new(report)
    results << tb.accounts_from_trial_balance
  end

  results_merged = []
  results.each_with_index do |result,i|
    if i == 0
      results_merged = result
    else
      result.each do ||
        hash=results_merged.find {|r| r[:code] == [:code]}

        hash = {code: [:code], name: [:name]} unless hash
        hash["dr#{i}".to_sym] = [:dr]
        hash["cr#{i}".to_sym] = [:cr]
      end
    end
  end

  results_merged
end

.glch_read(ledger) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/pais_legacy.rb', line 142

def self.glch_read(ledger)
  # out=execute_ssh_command(glch_read_cmd(ledger.to_s))
  # out.
  #   split("\n").
  #   map{|line| line.split(",")}.
  #   map{|line| {value: line[0], text: "#{line[0]}-#{line[1]}"}}
  glch_read_all(ledger)["accounts"].map {|h| {value: h["code"], text: "#{h['code']}-#{h['description']}"}}
end

.glch_read_all(ledger) ⇒ Object



151
152
153
154
155
# File 'lib/pais_legacy.rb', line 151

def self.glch_read_all(ledger)
  puts glch_read_cmd(ledger.to_s)
  out=execute_ssh_command(glch_read_cmd(ledger.to_s))
  JSON.parse(out)
end

.glch_read_cmd(ledger) ⇒ Object


Accounts




138
139
140
# File 'lib/pais_legacy.rb', line 138

def self.glch_read_cmd(ledger)
  pais_api("glch_read",ledger)
end

.glfi_read(ledger = nil) ⇒ Object



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

def self.glfi_read(ledger=nil)
  # out,stderr,status=Open3.capture2(glfi_read_cmd(ledger.to_s))
  out=execute_ssh_command(glfi_read_cmd(ledger.to_s))

  JSON.parse(out)
end

.glfi_read_cmd(ledger = nil) ⇒ Object


Ledger




120
121
122
123
124
125
126
# File 'lib/pais_legacy.rb', line 120

def self.glfi_read_cmd(ledger=nil)
  if ledger
    pais_api("glfi_read",ledger)
  else
    pais_api("glfi_read")
  end
end

.gltr_create(ledger, line) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/pais_legacy.rb', line 335

def self.gltr_create(ledger,line)
  pais_api "gltr_create", ledger, "{{autopost}} {{lasttrnno}} {{date}} {{dr}} {{dr_sub}} {{cr}} {{cr_sub}} \'{{description}}\' {{amount}}"
    .gsub("{{autopost}}",line[0])
    .gsub("{{lasttrnno}}",line[1])
    .gsub("{{date}}",line[2])
    .gsub("{{dr}}",line[3])
    .gsub("{{dr_sub}}",sub(line[4]))
    .gsub("{{cr}}",line[5])
    .gsub("{{cr_sub}}",sub(line[6]))
    .gsub("{{description}}",line[7])
    .gsub("{{amount}}",line[8])
end

.gltr_read_all(ledger, start_date, end_date) ⇒ Object



324
325
326
327
328
329
330
331
332
333
# File 'lib/pais_legacy.rb', line 324

def self.gltr_read_all(ledger,start_date,end_date)
  cmd=fglgo("gltr_read") + ' {{ccode}} {{ledger}} {{start_date}} {{end_date}}'
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger.to_s)
    .gsub("{{start_date}}",start_date)
    .gsub("{{end_date}}",end_date)

  out=execute_ssh_command(cmd)
  JSON.parse(out)
end

.lasttrnno_read(ledger) ⇒ Object



319
320
321
322
# File 'lib/pais_legacy.rb', line 319

def self.lasttrnno_read(ledger)
  out=execute_ssh_command(lasttrnno_read_cmd(ledger.to_s))
  out.strip
end

.lasttrnno_read_cmd(ledger) ⇒ Object



315
316
317
# File 'lib/pais_legacy.rb', line 315

def self.lasttrnno_read_cmd(ledger)
  pais_api("lasttrnno",ledger)
end

.pais_api(file, ledger, options = "") ⇒ Object



304
305
306
307
308
309
310
311
312
313
# File 'lib/pais_legacy.rb', line 304

def self.pais_api(file,ledger,options="")
  # ssh + ' "bash -c \"' + fglgo(file) + ' {{ccode}} {{ledger}} {{options}}\""'
  #   .gsub("{{ccode}}","T1")
  #   .gsub("{{ledger}}",ledger.to_s)
  #   .gsub("{{options}}",options)
  fglgo(file) + ' {{ccode}} {{ledger}} {{options}}'
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger.to_s)
    .gsub("{{options}}",options)
end

.post_journals(ledger) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/pais_legacy.rb', line 368

def self.post_journals(ledger)
  count = 0

  # Step through each item in the 'tmp/pais_journal_post_666.csv'
  CSV.readlines(journals_file(ledger)).drop(1).each do |line|
    unless line[8].nil?
      cmd = gltr_create(ledger,line)
      puts cmd
      execute_ssh_command(cmd) # Post to PAIS
      count += 1
    end
  end

  count
end

.raw(params, program = "rdefi00a") ⇒ Object

SCREEN T1 10000 1 999999 0 0 0 001 0 9999 0 9999 0 3 N Y N 5200 6153 1 6110 6 A 1 30/06/2023



159
160
161
162
163
164
165
166
167
# File 'lib/pais_legacy.rb', line 159

def self.raw(params,program="rdefi00a")
  params = params.gsub(" "," \\")

  # cmd = ssh + ' "bash -c \"'+ fglgo("rptrun") +' pgs 1 pais '+program+' \''+ params + ' \' \""'
  cmd =  fglgo("rptrun") +' api 1 pais '+program+' \''+ params + ' \' '

  out=execute_ssh_command(cmd)
  [out]
end

.raw_zsh(params, program = "rdefi00a") ⇒ Object



169
170
171
172
173
174
175
# File 'lib/pais_legacy.rb', line 169

def self.raw_zsh(params,program="rdefi00a")
  params = params.gsub(" "," \\")

  cmd = 'zsh -c "'+ fglgo("rptrun") +' api 1 pais '+program+' \''+ params + ' \' "'
  stdout, stderr, status_=Open3.capture3(cmd)
  [stdout]
end

.rebuild_ledger(ledger, end_date) ⇒ Object



394
395
396
# File 'lib/pais_legacy.rb', line 394

def self.rebuild_ledger(ledger,end_date)
  common_ledger_date("rebuild_after_post",ledger,end_date)
end

.rptrun(ledger, date_to, from = "0001", to = "9999", program = "rglch01a") ⇒ Object

Example of P&L for ledger 336 with 2 periods ‘SCREEN T1 336 spar N 2 1 Y 3 4 5 N 01/07/2021 30/06/2022 N ALL ALL N 2’



350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/pais_legacy.rb', line 350

def self.rptrun(ledger,date_to,from="0001",to="9999",program="rglch01a")
  fglgo("rptrun") +' api 1 pais {{program}} \'\{{printer}} \{{ccode}} \{{ledger}} \spar \N \{{account_from}} \{{account_to}} \1 \{{report_type}} \{{subaccs}} \{{date}}\' N'
    .gsub("{{program}}",program)
    .gsub("{{printer}}","SCREEN")
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger)
    .gsub("{{account_from}}",from.to_s)
    .gsub("{{account_to}}",to.to_s)
    .gsub("{{date}}",Date.parse(date_to).strftime("%d/%m/%Y") || "30/06/2021")
    .gsub("{{report_type}}","1")
    .gsub("{{subaccs}}","N")
end

.rptrun_exec(ledger, date_to, from = "0001", to = "9999", program = "rglch01a") ⇒ Object



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

def self.rptrun_exec(ledger,date_to,from="0001",to="9999",program="rglch01a")
  cmd=rptrun(ledger,date_to,from="0001",to="9999",program)
  [execute_ssh_command(cmd)]
end

.save_journals(ledger_code, data) ⇒ Object


General Journals


data should be the whole params, which will include the pais_code, dr & cr amounts



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/pais_legacy.rb', line 277

def self.save_journals(ledger_code, data)
  tranno=Pais.lasttrnno_read(ledger_code).to_i 

  File.open(journals_file(ledger_code), "w") do |f|
    f.write "AutoPost,LastTrnNo,Date,DR,DR_SUB,CR,CR_SUB,Description,Amount\n"

    data[:dr].each_with_index do |d,i|
      if data[:pais][i]
        
        # Determine if it's a credit/debit transaction
        if (amount = data[:dr][i].to_f) > 0
          ,dr_sub, = accounts(data[:pais][i])
        else
          ,cr_sub, = accounts(data[:pais][i])
          amount = data[:cr][i]
        end

        tranno += 1
        date = data[:date_to].nil? ? Time.now : Date.parse(data[:date_to])
        date = date.strftime("%d/%m/%Y")
        
        f.write "GJ,#{tranno},#{date},#{},#{dr_sub},#{},#{cr_sub},#{description(data,i)},#{amount}\n"
      end
    end
  end
end

.table_data(table) ⇒ Object



498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/pais_legacy.rb', line 498

def self.table_data(table)
  columns=table_schema(table)

  table_data_raw(table).map {|line|
    array=line.split("|")
    hash = Hash.new

    columns.each_with_index do |col,i|
      hash[col[:name].to_sym] = array[i].strip
    end
    hash
  }
end

.table_data_cmd(table) ⇒ Object



488
489
490
491
# File 'lib/pais_legacy.rb', line 488

def self.table_data_cmd(table)
  fglgo("db") + ' data {{table}}'
    .gsub("{{table}}",table)
end

.table_data_raw(table) ⇒ Object



493
494
495
496
# File 'lib/pais_legacy.rb', line 493

def self.table_data_raw(table)
  out=execute_ssh_command(table_data_cmd(table))
  out.split("\n")
end

.table_indexes(table) ⇒ Object



466
467
468
469
# File 'lib/pais_legacy.rb', line 466

def self.table_indexes(table)
  out=execute_ssh_command(table_indexes_cmd(table))
  out.split("\n").select{|i| i =~ /^#{table}_/}
end

.table_indexes_cmd(table) ⇒ Object


DB related




462
463
464
# File 'lib/pais_legacy.rb', line 462

def self.table_indexes_cmd(table)
  fglgo("db") + ' indexes'
end

.table_schema(table) ⇒ Object



481
482
483
484
485
486
# File 'lib/pais_legacy.rb', line 481

def self.table_schema(table)
  table_schema_raw(table).map {|line|
    array=line.split("|")
    {name: array[0].strip, type_id: array[1].strip, type_name: array[2].strip}
  }
end

.table_schema_cmd(table) ⇒ Object



471
472
473
474
# File 'lib/pais_legacy.rb', line 471

def self.table_schema_cmd(table)
  fglgo("db") + ' schema {{table}}'
    .gsub("{{table}}",table)
end

.table_schema_raw(table) ⇒ Object



476
477
478
479
# File 'lib/pais_legacy.rb', line 476

def self.table_schema_raw(table)
  out=execute_ssh_command(table_schema_cmd(table))
  out.split("\n")
end

.table_unload(table) ⇒ Object



517
518
519
# File 'lib/pais_legacy.rb', line 517

def self.table_unload(table)
  out=execute_ssh_command(table_unload_cmd(table))
end

.table_unload_cmd(table) ⇒ Object



512
513
514
515
# File 'lib/pais_legacy.rb', line 512

def self.table_unload_cmd(table)
  fglgo("db") + ' unload {{table}}'
    .gsub("{{table}}",table)
end

.tablesObject



525
526
527
528
# File 'lib/pais_legacy.rb', line 525

def self.tables
  out=execute_ssh_command(tables_cmd)
  out.split("\n")
end

.tables_cmdObject



521
522
523
# File 'lib/pais_legacy.rb', line 521

def self.tables_cmd
  fglgo("db") + ' tables'
end

Instance Method Details

#display_invoices(invoices) ⇒ Object



450
451
452
453
454
455
456
# File 'lib/pais_legacy.rb', line 450

def display_invoices(invoices)
  pastel = Pastel.new
  puts pastel.green.bold("PAIS Legacy: Invoices #{invoices.count}")
  invoices.each do |inv|
    puts "#{inv["client"]["code"].ljust(8)} #{inv["no"].strip.ljust(10)} #{inv["date"].ljust(11)} #{inv["workname"].strip[0..50].ljust(50)} #{inv["total"].strip.rjust(10)}"
  end
end

#execute_ssh_command(command) ⇒ Object



29
30
31
# File 'lib/pais_legacy.rb', line 29

def execute_ssh_command(command)
  PaisLegacy.execute_ssh_command(command)
end

#legacy_import_invoice(ledger, date, inv_no, client) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/pais_legacy.rb', line 425

def legacy_import_invoice(ledger,date,inv_no,client)

  date = Date.parse(date).strftime("%d/%m/%Y")

  cmd=PaisLegacy::Pais.fglgo("invo_read") + ' {{ccode}} {{ledger}} {{date}} {{inv_no}} {{client}}'
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger.to_s)
    .gsub("{{date}}",date)
    .gsub("{{inv_no}}",inv_no)
    .gsub("{{client}}",client)

  puts cmd
  out=PaisLegacy::Pais.execute_ssh_command(cmd)

  begin
    out = JSON.parse(out)
  rescue JSON::JSONError, ArgumentError => e
    puts e.to_s
    raise "Couldn't find Invoice Number: #{inv_no} for client #{client}."
  end

  # pp out
  out
end

#legacy_import_invoices(ledger, date) ⇒ Object


Invoice related




405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/pais_legacy.rb', line 405

def legacy_import_invoices(ledger,date)
  cmd=PaisLegacy::Pais.fglgo("invo_read") + ' {{ccode}} {{ledger}} {{date}}'
    .gsub("{{ccode}}","T1")
    .gsub("{{ledger}}",ledger.to_s)
    .gsub("{{date}}",date)

  puts cmd
  out=execute_ssh_command(cmd)
  out.delete!("\\\\")
  begin
    out = JSON.parse(out)
  rescue JSON::JSONError, ArgumentError => e
    puts e.to_s
    exit
  end
  out = out["invoices"]
  # pp out
  out
end