#!/usr/bin/perl use strict; use warnings; use JSON; use Data::Dumper; use CGI::Lazy; use GlobalTest; our $q = CGI::Lazy->new('/var/cache/apache2/cgi-conf/UsbInternalPOC.conf'); our $var = undef; our $id = \$var; our $stuff = $q->widget->composite({ id => 'stuff', members => [ $q->widget->dataset({ id => 'invoiceBlock', type => 'single', multiType => 'sequential', # multiType => 'list', template => "UsbInternalPOCInvoiceBlock.tmpl", multipleTemplate => 'UsbInternalPOCInvoiceBlockMultiple.tmpl', lookups => { countryLookup => { sql => 'select ID, country from countryCodeLookup', preload => 1, orderby => ['ID'], output => 'hash', primarykey => 'ID', }, }, extravars => { invoiceid => { value => $id, }, }, recordset => $q->db->recordset({ table => 'invoice', fieldlist => [ {name => 'id', hidden => 1, handle => $id}, {name => 'merchant', label => 'Merchant', multi => 1}, {name => 'batch', label => 'Bat', multi => 1}, {name => 'post_date', label => 'Post Date', multi => 1}, {name => 'cardnum', label => 'Card Number', multi => 1}, {name => 'tailno', label => 'Tail Number',}, {name => 'authCode', label => 'Auth Code',}, {name => 'icao', label => 'ICAO',}, {name => 'invoicenum', label => 'Invoice Number', multi => 1}, {name => 'invtotal', label => 'Invoice Total', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, }, {name => 'trandate', label => 'Invoice Date', }, {name => 'country', label => 'Country',}, {name => 'card_type', hidden => 1,}, ], where => '', orderby => 'id', primarykey => 'id', insertdefaults => { card_type => {#field name # sql => 'select description from prodCodeLookup where ID = 1', value => 'frobnitz', # handle => $id, }, }, insertadditional => { invoiceid => { sql => 'select LAST_INSERT_ID()', handle => $id, }, }, }), }), $q->widget->dataset({ id => 'detailBlock', template => "UsbInternalPOCDetailBlock.tmpl", # nodelete => 1, lookups => { prodcodeLookup => { sql => 'select ID, description from prodCodeLookup', preload => 1, orderby => ['ID'], output => 'hash', primarykey => 'ID', }, }, recordset => $q->db->recordset({ table => 'detail', fieldlist => [ {name => 'detail.ID', hidden => 1}, {name => 'invoiceid', hidden => 1}, {name => 'prodCode', label => 'Product Code', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'quantity', label => 'Quantity', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'unitPrice', label => 'Unit Price' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'productGross', label => 'Product Gross' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'prodCodeLookup.description', label => 'Product Description', readOnly => 1, }, ], where => '', joins => [ {type => 'inner', table => 'prodCodeLookup', field1 => 'prodCode', field2 => 'prodCodeLookup.ID',}, ], orderby => 'detail.ID', primarykey => 'detail.ID', }), }), ] }); my %nav = ( dbwrite => \&dbwrite, ); if ($q->param('nav')) { $nav{$q->param('nav')}->(); } elsif ($q->param('POSTDATA')) { ajaxHandler(); } else { display(); } #---------------------------------------------------------------------------------------- sub ajaxHandler { my $incoming = from_json($q->param('POSTDATA')); if ($incoming->{delete}) { doFullDelete($incoming); return; } my $invoiceBlock = $stuff->members->{invoiceBlock}; my $invoiceBlockOutput = $invoiceBlock->ajaxSelect(incoming => $incoming, vars => {id => {handle => $id}}); #has to be a separate step to populate the multi attribute my $detailBlock = $stuff->members->{detailBlock}; if ($invoiceBlock->multi) { print $q->header, $stuff->ajaxReturn($invoiceBlock, $invoiceBlockOutput); } else { print $q->header, $stuff->ajaxReturn( [$invoiceBlock, $detailBlock], [$invoiceBlockOutput, $detailBlock->ajaxSelect(incoming => {invoiceid => $id})] ); } } #---------------------------------------------------------------------------------------- sub display { my $recordnum; if (!defined $q->param('recordnum')) { $recordnum = 0; } else { $recordnum = $q->param('recordnum'); $recordnum++; } print $q->header, #standard http header $q->start_html({-style => {src => '/css/topbanner.css'}}), $q->javascript->modules($stuff); #javascript functions needed by widget #header section print $q->template('topbanner1.tmpl')->process({ mainTitle => 'Main Title', secondaryTitle => 'Secondary Title', versionTitle => 'version 0.1', messageTitle => 'Nik rocks', }); #help block section print $q->template('UsbInternalPOCHelpBlock.tmpl')->process({ helpMessage1 => ' ', helpMessage2 => ' ', }); #composite widget section print $q->start_form(), $q->hidden({-name => 'nav', -value => 'dbwrite'}), $q->hidden({-name => 'recordnum', -value => $recordnum, -override => 1}); my $invoiceBlock = $stuff->members->{invoiceBlock}; my $detailBlock = $stuff->members->{detailBlock}; print $invoiceBlock->displaySelect(vars => {id => {handle => $id}, -recordnum => $recordnum}), $detailBlock->displaySelect(invoiceid =>$id); print $q->javascript->load('UsbInternalPOC.js'); print $q->table({-align => 'center'}, $q->Tr( $q->td({-align => 'center'}, $q->button({ -name => 'Submit Data', -onclick => "if (pageValidate()) {this.form.submit()} " }) ), $q->td({-align => 'center'}, $q->button({ -name => 'Validate', -onclick => "pageValidate();" }) ), $q->td({-align => 'center'}, $q->button({ -name => 'Clear', -onclick => "window.location='".$q->url."';", }) ), $q->td({-align => 'center'}, $q->button({ -name => 'Search', -onclick => "search();", }) ), ) ), $q->end_form; return; } #---------------------------------------------------------------------------------------- sub doFullDelete { my $incoming = shift; my $invoiceid = $incoming->{delete}->{invoiceid}; $q->db->do('delete from detail where invoiceid = ?', $invoiceid); $q->db->do('delete from invoice where invoiceid = ?', $invoiceid); my $invoiceBlock = $stuff->members->{invoiceBlock}; my $detailBlock = $stuff->members->{detailBlock}; print $q->header, $stuff->ajaxReturn( [$invoiceBlock, $detailBlock], [$invoiceBlock->ajaxBlank, $detailBlock->ajaxBlank], ); } #---------------------------------------------------------------------------------------- sub dbwrite { $$id = $q->param('invoiceBlock-invoiceid'); $stuff->members->{invoiceBlock}->dbwrite( insert => {id => {handle => $id}}, update => {id => {handle => $id}}); $stuff->members->{detailBlock}->dbwrite( insert => {invoiceid => {value => $id}}, update => {invoiceid => {value => $id}}); display('blank'); }