Current code.
[capital-apms.git] / inc / edit / invoice.php
blobf3932532724c855f7470141adc127962d369cce2
1 <?php
3 $c->scripts[] = "js/jquery.js";
5 param_to_global('orig_invoiceno', '#[0-9]+#');
6 //param_to_global('entitycode', 'int', 'tenantcode', 'creditorcode');
7 if ( !isset($orig_invoiceno) ) {
8 param_to_global('copy', '#[0-9]+#');
9 if ( isset($copy) ) {
10 $q = new AwlQuery("SELECT nextval('invoice_invoiceno_seq')");
11 if ( $q->Exec() && $row = $q->Fetch(true) ) {
12 $id = $row[0];
13 $q->Begin();
14 $q->QDo("INSERT INTO invoice
15 (invoiceno, invoicestatus, invoicedate, entitytype, entitycode, topay, todetail, taxapplies, termscode, taxamount, total, blurb, invoicetype, attnto )
16 SELECT $id, 'U', current_date, entitytype, entitycode, topay, todetail, taxapplies, termscode, taxamount, total, blurb, invoicetype, attnto
17 FROM invoice WHERE invoiceno = ?", $copy);
18 $q->QDo("INSERT INTO invoiceline
19 (invoiceno, lineseq, entitytype, entitycode, accountcode, accounttext, quantity, amount, percent, yourshare )
20 SELECT $id, lineseq, entitytype, entitycode, accountcode, accounttext, quantity, amount, percent, yourshare
21 FROM invoiceline WHERE invoiceno = ?", $copy);
22 $q->Commit();
23 $orig_invoiceno = $id;
28 // Editor component for invoice
29 $editor = new Editor("Invoice", 'invoice');
30 $editor->AddField( 'orig_invoiceno', 'invoice.invoiceno' );
31 $editor->AddField( 'etname', 'get_entity_type_name(entitytype)' );
32 $editor->AddField( 'loweretname', 'lower(get_entity_type_name(entitytype))' );
33 $editor->AddField( 'entityname', 'get_entity_name(entitytype,entitycode)' );
34 $editor->AddField( 'termscode', null, 'SELECT termscode, description FROM invoiceterms ORDER BY termscode' );
35 $editor->AddField( 'invoice_amount', '(total + taxamount)' );
36 $editor->AddAttribute( 'entitytype', 'id', 'entitytype');
37 $editor->AddAttribute( 'entitycode', 'id', 'entitycode');
38 $editor->AddAttribute( 'taxapplies', 'onChange', "return calculate_tax_total(document.forms[0].total, document.forms[0].taxamount, document.forms[0].invoice_amount, document.forms[0].taxapplies[1].checked);" );
39 $editor->AddAttribute( 'total', 'onChange', "return calculate_tax_total(document.forms[0].total, document.forms[0].taxamount, document.forms[0].invoice_amount, document.forms[0].taxapplies[1].checked);" );
40 $editor->AddAttribute( 'total', 'onKeydown', "return EnterToNextField(event,document.forms[0].taxamount);" );
41 $editor->AddAttribute( 'taxamount', 'onChange', "return calculate_total(document.forms[0].taxamount, document.forms[0].total, document.forms[0].invoice_amount);" );
42 $editor->AddAttribute( 'taxamount', 'onKeydown', "return EnterToNextField(event,document.forms[0].invoice_amount);" );
43 $editor->AddAttribute( 'invoice_amount', 'onChange', "return calculate_tax_split(document.forms[0].invoice_amount, document.forms[0].total, document.forms[0].taxamount, document.forms[0].taxapplies[1].checked );" );
44 $editor->AddAttribute( 'invoice_amount', 'onKeydown', "return EnterToNextField(event,document.forms[0].submit);" );
45 $editor->AddAttribute( 'entitycode', 'onBlur', "return validate_positive_integer(self);" );
46 $editor->AddAttribute( 'entitycode', 'title', "The debtor to be charged for this invoice." );
47 $editor->SetWhere( "invoiceno=".(isset($id)?$id:'0'));
48 $page_elements[] = $editor;
50 // Editor component for invoice line
51 $editrow = new Editor("InvoiceLine", 'invoiceline');
52 $editrow->AddField( 'line_invoiceno', 'invoiceline.invoiceno' );
53 $editrow->AddField( 'etname', 'get_entity_type_name(entitytype)' );
54 $editrow->AddField( 'my_lineseq', 'lineseq' );
55 $editrow->AddField( 'accountcode', 'accountcode', "SELECT accountcode, c.name FROM chartofaccount c JOIN accountgroup g USING (accountgroupcode) WHERE creditgroup AND grouptype = 'P'" );
56 $editrow->AddAttribute( 'accountcode', 'style', "width:10em;" );
57 $editrow->AddAttribute( 'amount', 'onBlur', "return calculate_percent_share(document.forms[1].amount, document.forms[1].percent, document.forms[1].yourshare);" );
58 $editrow->AddAttribute( 'percent', 'onBlur', "return calculate_percent_share(document.forms[1].amount, document.forms[1].percent, document.forms[1].yourshare);" );
59 $editrow->SetSubmitName( 'saverow' );
61 if ( $editor->IsSubmit() ) {
62 $editor->WhereNewRecord( " invoiceno=currval('invoice_invoiceno_seq') " );
63 $editor->Write();
65 else {
66 $editor->GetRecord();
68 if ( $editrow->IsSubmit() ) {
69 $_POST['invoiceno'] = $id;
70 $is_update = ($_POST['saverow'] == 'Save');
71 if ( ! $is_update ) {
72 $sql = "SELECT lineseq FROM invoiceline WHERE invoiceno = ? ORDER BY lineseq DESC LIMIT 1";
73 $qry = new PgQuery($sql, $id);
74 if ( $qry->Exec("edit/invoice") && $qry->rows == 1 ) {
75 $row = $qry->Fetch();
76 $lineseq = $row->lineseq + 1;
78 else {
79 $lineseq = 1;
81 $_POST['lineseq'] = $lineseq;
83 else {
84 $_POST['lineseq'] = intval($_GET['editseq']);
85 $lineseq = $_POST['lineseq'];
87 $editrow->SetWhere( "invoiceno=$id AND lineseq=$lineseq");
88 $editrow->WhereNewRecord( "invoiceno=$id AND lineseq=$lineseq" );
89 $editrow->Write( $is_update );
90 unset($_GET['editseq']);
93 $id = (isset($editor->Record->{'orig_invoiceno'}) ? $editor->Record->{'orig_invoiceno'} : null );
94 $submittype = 'Update';
95 if ( ! $editor->Available() ) {
96 $submittype = 'Create';
97 $editor->Initialise( array(
98 'invoicedate' => 'today',
99 'termscode' => 'M',
100 'taxapplies' => 't',
101 'todetail' => '',
102 'invoicestatus' => 'U',
103 'entitytype' => $et,
104 'entitycode' => $id
106 if ( isset($et) ) $editor->Record->{'entitytype'} = $et;
107 if ( isset($ec) ) $editor->Record->{'entitycode'} = $ec;
110 $editor->SetOptionList( 'entitytype', array('T' => 'Debtor', 'C' => 'Creditor' ), $editor->Record->{'entitytype'} );
112 $template = <<<EOTEMPLATE
113 <script type="text/javascript">
114 function do_lookup(type,et,ec,callback) {
115 code = et + ec;
116 $.getJSON("lookup.php",{request:type,code:code},callback);
119 function show_entity_name() {
120 do_lookup( "entity", $("#entitytype").val(), $("#entitycode").val(), function(result){
121 linktype = ( $("#entitytype").val() == "T" ? "debtor" : "creditor");
122 $("#entityname").html("<a href=\"view.php?t="+linktype+"&id="+$("#entitycode").val()+"\">"+result.name+"</a>");
126 $(document).ready(function(){
127 show_entity_name();
129 $("#entitycode").change(function(e){
130 show_entity_name();
133 $("#entitycode").click(function(){
134 $("#entitycode").select();
137 </script>
138 <form method="POST" enctype="multipart/form-data" id="entry">
139 <table>
140 <tr>
141 <th class="right">Invoice#:</th>
142 <td class="left">##orig_invoiceno.value## ##orig_invoiceno.hidden##</td>
143 </tr>
144 <tr>
145 <th class="right">Date:</th>
146 <td>
147 <table class="form_inner">
148 <tr>
149 <td class="left">##invoicedate.input.10##</td>
150 <th class="right">Terms:</th>
151 <td class="left">##termscode.select##</td>
152 <th class="right">GST applies:</th>
153 <td class="left">##taxapplies.checkbox##</td>
154 </tr>
155 </table>
156 </td>
157 </tr>
158 <tr>
159 <th class="right">Invoice To:</th>
160 <td class="left" style="width:30em">##entitytype.select## ##entitycode.input.5## <span id="entityname">##entityname.value##</span></td>
161 </tr>
162 <tr>
163 <th class="right">Attn:</th>
164 <td class="left">##attnto.input.50##</td>
165 </tr>
166 <tr>
167 <th class="right">Re:</th>
168 <td class="left">##todetail.input.50##</td>
169 </tr>
170 <tr>
171 <th class="right">Description:</th>
172 <td class="left">##blurb.textarea.49x4##</td>
173 </tr>
174 <tr>
175 <th class="right">Amount:</th>
176 <td>
177 <table width="100%">
178 <tr>
179 <td class="left">##total.money.12##</td>
180 <th class="right">+ Tax:</th>
181 <td class="left">##taxamount.money.12##</td>
182 <th class="right">Total:</th>
183 <td class="left">##invoice_amount.money.12##</td>
184 </tr>
185 </table>
186 </td>
187 </tr>
188 <tr>
189 <th class="right"></th>
190 <td class="left">##$submittype.submit##</td>
191 </tr>
192 </table>
193 </form>
195 EOTEMPLATE;
197 $editor->SetTemplate( $template );
198 $c->page_title = $editor->Title("Invoice $id / ".$editor->Record->{'todetail'});
200 $et = $editor->Record->{'entitytype'};
201 $ec = $editor->Record->{'entitycode'};
202 include_once("menus_entityaccount.php");
204 $related_menu->AddOption("Print Invoice","/print.php?t=invoice&id=$id","Print this invoice", false, 200);
205 if ( $editor->Record->{'invoicestatus'} == 'U' ) {
206 $related_menu->AddOption("Edit Invoice","/edit.php?t=invoice&id=$id","Edit this invoice", false, 190);
209 if ( $submittype == 'Update' ) {
211 function edit_row( $row_data ) {
212 global $editrow, $id;
214 $form_url = preg_replace( '#&delseq=\d+#', '', $_SERVER['REQUEST_URI'] );
215 $rowsubmittype = 'Save';
216 if ( $row_data->lineseq == -1 ) {
217 $form_url = preg_replace( '#&editseq=\d+#', '', $form_url );
218 $rowsubmittype = 'Add';
221 $template = <<<EOTEMPLATE
222 <form method="POST" enctype="multipart/form-data" id="rowentry" action="$form_url">
223 <td class="left">##lineseq.value##<input type="hidden" name="id" value="$id"></td>
224 <td class="left" style="white-space:nowrap">##entitytype.input.1####entitycode.input.3####accountcode.select##</td>
225 <td class="left">##accounttext.input.30##</td>
226 <td class="left">##amount.money.8##</td>
227 <td class="left">##percent.input.4##</td>
228 <td class="left">##yourshare.money.8##</td>
229 <td class="left">##$rowsubmittype.submit##</td>
230 </form>
232 EOTEMPLATE;
234 $editrow->SetTemplate( $template );
235 $editrow->SetRecord( $row_data );
236 $editrow->Title("");
237 return $editrow->Render();
240 // And the invoice line details
241 require_once('classBrowser.php');
242 $browser = new Browser("Invoice Detail");
243 $browser->AddHidden( 'etname', 'lower(get_entity_type_name(entitytype))' );
244 $browser->AddColumn( 'lineseq', '#', 'right' );
245 $browser->AddHidden( 'entitytype' );
246 $browser->AddHidden( 'entitycode' );
247 $browser->AddHidden( 'accountcode' );
248 $browser->AddHidden( 'account', "entitytype||'-'||TO_CHAR(entitycode,'FM00009')||'-'||TO_CHAR(accountcode,'FM0009.00')" );
249 $browser->AddColumn( 'accountname', 'Account', 'left', '', 'chartofaccount.name');
250 $browser->AddColumn( 'accounttext', 'Description', 'left' );
251 $browser->AddColumn( 'amount', 'Amount', 'right', '%0.2lf' );
252 $browser->AddColumn( 'percent', 'Percent', 'right', '%0.3lf' );
253 $browser->AddColumn( 'yourshare', 'Share', 'right', '%0.2lf' );
254 $edit_link = "<a href=\"/edit.php?t=invoice&id=$id&editseq=##lineseq##\" class=\"submit\">Edit</a>";
255 $del_link = "<a href=\"/edit.php?t=invoice&id=$id&delseq=##lineseq##\" class=\"submit\">Del</a>";
256 $browser->AddColumn( 'action', 'Action', 'center', '', "'$edit_link&nbsp;$del_link'" );
257 $browser->AddOrder( 'lineseq', 'ASC' );
258 $browser->SetJoins( "invoiceline LEFT OUTER JOIN chartofaccount USING ( accountcode )" );
260 $browser->AndWhere( "invoiceno=$id" );
261 $rowurl = '/view.php?t=account&id=%s';
262 $browser->RowFormat( "<tr class=\"r%d\">\n", "</tr>\n", 'account', 'account', 'accountname', '#even' );
263 $page_elements[] = $browser;
265 if ( isset($_GET['delseq']) ) {
266 $qry = new PgQuery("DELETE FROM invoiceline WHERE invoiceno=? AND lineseq = ?", $id, intval($_GET['delseq']) );
267 $qry->Exec('invoice');
269 if ( isset($_GET['editseq']) ) {
270 $browser->MatchedRow('lineseq', intval($_GET['editseq']), 'edit_row');
272 else {
273 $remaining = $editor->Record->{'total'};
274 $extra_row = array( 'lineseq' => -1, 'percent' => 100, 'amount' => $remaining, 'yourshare' => $remaining, 'accounttext' => $editor->Value('todetail') );
275 $qry = new PgQuery("SELECT entitytype, entitycode, accountcode, percent FROM invoiceline WHERE invoiceno=$id ORDER BY lineseq DESC LIMIT 1");
276 if ( $qry->Exec('invoice') && $qry->rows == 1 ) {
277 $row = $qry->Fetch();
278 $extra_row['entitytype'] = $row->entitytype;
279 $extra_row['entitycode'] = $row->entitycode;
280 $extra_row['accountcode'] = $row->accountcode;
281 $extra_row['accounttext'] = $row->accounttext;
282 $extra_row['percent'] = $row->percent;
283 $qry = new PgQuery("SELECT SUM(amount) FROM invoiceline WHERE invoiceno=$id");
284 if ( $qry->Exec('invoice') && $qry->rows == 1 ) {
285 $row = $qry->Fetch();
286 $extra_row['amount'] = $remaining - $row->sum;
288 $extra_row['yourshare'] = round( ($extra_row['amount'] * ($extra_row['percent'] / 100)), 2);
290 $browser->MatchedRow('lineseq', -1, 'edit_row');
291 $extra_row = (object) $extra_row;
292 $browser->AddRow($extra_row);