summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: af67868f84a99a8629e605a17f39707ad4b1965a (plain)
  1. =pod
  2. =head1 NAME
  3. LedgerSMB::Scripts::payment - LedgerSMB class defining the Controller functions for payment handling.
  4. =head1 SYNOPSIS
  5. Defines the controller functions and workflow logic for payment processing.
  6. =head1 COPYRIGHT
  7. Copyright (c) 2007, David Mora R and Christian Ceballos B.
  8. Licensed to the public under the terms of the GNU GPL version 2 or later.
  9. Original copyright notice below.
  10. #=====================================================================
  11. # PLAXIS
  12. # Copyright (c) 2007
  13. #
  14. # Author: David Mora R
  15. # Christian Ceballos B
  16. #
  17. #
  18. #
  19. #
  20. #
  21. # This program is free software; you can redistribute it and/or modify
  22. # it under the terms of the GNU General Public License as published by
  23. # the Free Software Foundation; either version 2 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software
  32. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. =head1 METHODS
  34. =cut
  35. package LedgerSMB::Scripts::payment;
  36. use LedgerSMB::Template;
  37. use LedgerSMB::DBObject::Payment;
  38. use LedgerSMB::DBObject::Date;
  39. use strict;
  40. # CT: A few notes for future refactoring of this code:
  41. # 1: I don't think it is a good idea to make the UI too dependant on internal
  42. # code structures but I don't see a good alternative at the moment.
  43. # 2: CamelCasing: -1
  44. =pod
  45. =item payment
  46. This method is used to set the filter screen and prints it, using the
  47. TT2 system. (hopefully it will... )
  48. =back
  49. =cut
  50. sub payments {
  51. my ($request) = @_;
  52. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  53. $payment->get_metadata();
  54. my $template = LedgerSMB::Template->new(
  55. user => $request->{_user},
  56. locale => $request->{_locale},
  57. path => 'UI/payments',
  58. template => 'payments_filter',
  59. format => 'HTML',
  60. );
  61. $template->render($payment);
  62. }
  63. sub get_search_criteria {
  64. my ($request) = @_;
  65. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  66. $payment->get_metadata();
  67. if ($payment->{batch_id} && $payment->{batch_date}){
  68. $payment->{date_reversed} = $payment->{batch_date};
  69. }
  70. my $template = LedgerSMB::Template->new(
  71. user => $request->{_user},
  72. locale => $request->{_locale},
  73. path => 'UI/payments',
  74. template => 'search',
  75. format => 'HTML',
  76. );
  77. $template->render($payment);
  78. }
  79. sub get_search_results {
  80. my ($request) = @_;
  81. my $rows = [];
  82. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  83. my @search_results = $payment->search;
  84. my $template = LedgerSMB::Template->new(
  85. user => $request->{_user},
  86. locale => $request->{_locale},
  87. path => 'UI',
  88. template => 'form-dynatable',
  89. format => ($payment->{format}) ? $payment->{format} : 'HTML',
  90. );
  91. my $base_url = "payment.pl?";
  92. my $search_url = "$base_url";
  93. for my $key (keys %{$request->take_top_level}){
  94. if ($base_url =~ /\?$/){
  95. $base_url .= "$key=$request->{key}";
  96. } else {
  97. $base_url .= "&$key=$request->{key}";
  98. }
  99. }
  100. my @columns = qw(selected meta_number date_paid amount source company_paid);
  101. my $contact_type = ($payment->{account_class} == 1) ? 'Vendor' : 'Customer';
  102. # CT: Locale strings for gettext:
  103. # $request->{_locale}->text("Vendor Number");
  104. # $request->{_locale}->text("Customer Number");
  105. my $heading = {
  106. selected => $request->{_locale}->text('Selected'),
  107. company_paid => {
  108. text => $request->{_locale}->text('Company Name'),
  109. href => "$search_url&orderby=company_paid",
  110. },
  111. meta_number => {
  112. text => $request->{_locale}->text(
  113. "$contact_type Number"
  114. ),
  115. href => "$search_url&orderby=meta_number",
  116. },
  117. date_paid => {
  118. text => $request->{_locale}->text('Date Paid'),
  119. href => "$search_url&orderby=date_paid",
  120. },
  121. amount => {
  122. text => $request->{_locale}->text('Total Paid'),
  123. href => "$search_url&orderby=amount",
  124. },
  125. source => {
  126. text => $request->{_locale}->text('Source'),
  127. href => "$search_url&orderby=source",
  128. },
  129. };
  130. my $classcount;
  131. $classcount = 0;
  132. my $rowcount;
  133. $rowcount = 1;
  134. for my $line (@search_results){
  135. $classcount ||= 0;
  136. $rowcount += 1;
  137. push(@$rows, {
  138. company_paid => $line->{company_paid},
  139. amount => $request->format_amount(amount => $line->{amount}),
  140. i => "$classcount",
  141. date_paid => $line->{date_paid},
  142. source => $line->{source},
  143. meta_number => $line->{meta_number},
  144. selected => {
  145. input => {
  146. type => "checkbox",
  147. name => "payment_$rowcount",
  148. value => "1",
  149. },
  150. }
  151. });
  152. $payment->{"credit_id_$rowcount"} = $line->{credit_id};
  153. $payment->{"date_paid_$rowcount"} = $line->{date_paid};
  154. $payment->{"source_$rowcount"} = $line->{source};
  155. $classcount = ($classcount + 1) % 2;
  156. ++$rowcount;
  157. }
  158. $payment->{rowcount} = $rowcount;
  159. $payment->{script} = 'payment.pl';
  160. $payment->{title} = $request->{_locale}->text("Payment Results");
  161. my $hiddens = $payment->take_top_level;
  162. $template->render({
  163. form => $payment,
  164. columns => \@columns,
  165. heading => $heading,
  166. hiddens => $payment->take_top_level,
  167. rows => $rows,
  168. buttons => [{
  169. value => 'reverse_payments',
  170. name => 'action',
  171. class => 'submit',
  172. type => 'submit',
  173. text => $request->{_locale}->text('Reverse Payments'),
  174. }]
  175. });
  176. }
  177. sub get_search_results_reverse_payments {
  178. my ($request) = @_;
  179. my $payment = LedgerSMB::DBObject::Payment->new({base => $request});
  180. for my $count (1 .. $payment->{rowcount}){
  181. if ($payment->{"payment_$count"}){
  182. $payment->{credit_id} = $payment->{"credit_id_$count"};
  183. $payment->{date_paid} = $payment->{"date_paid_$count"};
  184. $payment->{source} = $payment->{"source_$count"};
  185. $payment->reverse;
  186. }
  187. }
  188. get_search_criteria($payment);
  189. }
  190. sub check_job {
  191. my ($request) = @_;
  192. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  193. $payment->check_job;
  194. my $template = LedgerSMB::Template->new(
  195. user => $request->{_user},
  196. locale => $request->{_locale},
  197. path => 'UI/payments',
  198. template => 'check_job',
  199. format => 'HTML',
  200. );
  201. $template->render($payment);
  202. }
  203. sub post_payments_bulk {
  204. my ($request) = @_;
  205. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  206. $payment->post_bulk();
  207. my $template;
  208. if ($payment->{queue_payments}){
  209. $payment->{job_label} = 'Payments';
  210. $template = LedgerSMB::Template->new(
  211. user => $request->{_user},
  212. locale => $request->{_locale},
  213. path => 'UI/payments',
  214. template => 'check_job',
  215. format => 'HTML',
  216. );
  217. } else {
  218. $template = LedgerSMB::Template->new(
  219. user => $request->{_user},
  220. locale => $request->{_locale},
  221. path => 'UI/payments',
  222. template => 'payments_filter',
  223. format => 'HTML',
  224. );
  225. }
  226. $template->render($payment);
  227. }
  228. sub display_payments {
  229. my ($request) = @_;
  230. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  231. $payment->get_payment_detail_data();
  232. $payment->debug({file => '/tmp/delme'});
  233. for (@{$payment->{contact_invoices}}){
  234. $_->{total_due} = $payment->format_amount(amount => $_->{total_due});
  235. }
  236. my $template = LedgerSMB::Template->new(
  237. user => $request->{_user},
  238. locale => $request->{_locale},
  239. path => 'UI/payments',
  240. template => 'payments_detail',
  241. format => 'HTML',
  242. );
  243. $template->render($payment);
  244. }
  245. =item payment
  246. This method is used to set the filter screen and prints it, using the
  247. TT2 system. (hopefully it will... )
  248. =back
  249. =cut
  250. sub payment {
  251. my ($request) = @_;
  252. my $locale = $request->{_locale};
  253. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  254. # Lets get the project data...
  255. my @projectOptions;
  256. my @arrayOptions = $dbPayment->list_open_projects();
  257. push @projectOptions, {}; #A blank field on the select box
  258. for my $ref (0 .. $#arrayOptions) {
  259. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  260. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  261. }
  262. # Lets get the departments data...
  263. my @departmentOptions;
  264. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  265. @arrayOptions = $dbPayment->list_departments($role);
  266. push @departmentOptions, {}; # A blank field on the select box
  267. for my $ref (0 .. $#arrayOptions) {
  268. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  269. text => $arrayOptions[$ref]->{description}};
  270. }
  271. # Lets get the customer or vendor :)
  272. my @vcOptions;
  273. @arrayOptions = $dbPayment->get_open_accounts();
  274. for my $ref (0 .. $#arrayOptions) {
  275. push @vcOptions, { value => $arrayOptions[$ref]->{id}.'--'.$arrayOptions[$ref]->{name},
  276. text => $arrayOptions[$ref]->{name}};
  277. }
  278. # Lets get the open currencies (this uses the $dbPayment->{account_class} property)
  279. my @currOptions;
  280. @arrayOptions = $dbPayment->get_open_currencies();
  281. for my $ref (0 .. $#arrayOptions) {
  282. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  283. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  284. }
  285. # Lets build filter by period
  286. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  287. $date->build_filter_by_period($locale);
  288. # Lets set the data in a hash for the template system. :)
  289. my $select = {
  290. stylesheet => $request->{_user}->{stylesheet},
  291. login => { name => 'login',
  292. value => $request->{_user}->{login} },
  293. projects => {
  294. name => 'projects',
  295. options => \@projectOptions
  296. },
  297. department => {
  298. name => 'department',
  299. options => \@departmentOptions
  300. },
  301. vendor_customer => {
  302. name => 'vendor-customer',
  303. options => \@vcOptions
  304. },
  305. curr => {
  306. name => 'curr',
  307. options => \@currOptions
  308. },
  309. month => {
  310. name => 'month',
  311. options => $date->{monthsOptions}
  312. },
  313. year => {
  314. name => 'year',
  315. options => $date->{yearsOptions}
  316. },
  317. interval_radios => $date->{radioOptions},
  318. amountfrom => {
  319. name => 'amountfrom',
  320. },
  321. amountto => {
  322. name => 'amountto',
  323. },
  324. accountclass => {
  325. name => 'account_class',
  326. value => $dbPayment->{account_class}
  327. },
  328. type => {
  329. name => 'type',
  330. value => $request->{type}
  331. },
  332. action => {
  333. name => 'action',
  334. value => 'payment2',
  335. text => $locale->text("Continue"),
  336. },
  337. };
  338. # Lets call upon the template system
  339. my $template;
  340. $template = LedgerSMB::Template->new(
  341. user => $request->{_user},
  342. locale => $request->{_locale},
  343. path => 'UI/payments',
  344. template => 'payment1',
  345. format => 'HTML', );
  346. $template->render($select);# And finally, Lets print the screen :)
  347. }
  348. =pod
  349. =item payment2
  350. This method is used for the payment module, it is a consecuence of the payment sub,
  351. and its used for all the mechanics of an invoices payment module.
  352. =back
  353. =cut
  354. sub payment2 {
  355. my ($request) = @_;
  356. my $locale = $request->{_locale};
  357. my $Payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  358. # VARIABLES
  359. my ($project_id, $project_number, $project_name, $department_id, $department_name );
  360. my @array_options;
  361. my @project;
  362. my @selected_checkboxes;
  363. my @department;
  364. my @array_options;
  365. my @currency_options;
  366. my $exchangerate;
  367. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  368. ($Payment->{entity_id}, my $vendor_customer_name) = split /--/ , $request->{'vendor-customer'};
  369. my @vc_options;
  370. #@array_options = $Payment->get_vc_info();
  371. #$request->error($array_options[0]->{name});
  372. # LETS BUILD THE PROJECTS INFO
  373. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  374. if ($request->{projects}) {
  375. ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  376. @project = { name => 'projects', text => $project_number.' '.$project_name, value => $request->{projects}};
  377. }
  378. # LETS GET THE DEPARTMENT INFO
  379. if ($request->{department}) {
  380. ($department_id, $department_name) = split /--/, $request->{department};
  381. @department = { name => 'department', text => $department_name, value => $request->{department}};
  382. }
  383. # LETS GET ALL THE ACCOUNTS
  384. my @account_options = $Payment->list_accounting();
  385. # LETS GET THE POSSIBLE SOURCES
  386. my @sources_options = $Payment->get_sources(\%$locale);
  387. # WE MUST PREPARE THE ENTITY INFORMATION
  388. @array_options = $Payment->get_vc_info();
  389. # LETS BUILD THE CURRENCIES INFORMATION
  390. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  391. my $default_currency = $Payment->get_default_currency();
  392. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  393. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  394. # -----------------------------------------------
  395. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  396. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  397. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  398. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  399. my @column_headers = ({text => $locale->text('Invoice')},
  400. {text => $locale->text('Date')},
  401. {text => $locale->text('Total').$default_currency_text},
  402. {text => $locale->text('Paid').$default_currency_text},
  403. {text => $locale->text('Amount Due').$default_currency_text},
  404. {text => $locale->text('To pay').$default_currency_text}
  405. );
  406. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  407. if ($default_currency ne $request->{curr} ) {
  408. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  409. push @column_headers, {text => $locale->text('Exchange Rate')},
  410. {text => $locale->text('Amount Due').$currency_text},
  411. {text => $locale->text('To pay').$currency_text};
  412. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  413. @column_headers[5,6,7] = @column_headers[6,7,5];
  414. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  415. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  416. $exchangerate = $request->{exrate} ?
  417. $request->{exrate} :
  418. $Payment->get_exchange_rate($request->{curr},
  419. $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date});
  420. if ($exchangerate) {
  421. @currency_options = {
  422. name => 'exrate',
  423. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  424. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  425. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  426. };
  427. } else {
  428. @currency_options = {
  429. name => 'exrate'};
  430. }
  431. } else {
  432. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  433. # ARE USING THE DEFAULT CURRENCY
  434. $exchangerate = 1;
  435. @currency_options = {
  436. name => 'exrate',
  437. value => 1,
  438. text => 1
  439. };
  440. }
  441. # FINALLY WE ADD TO THE COLUMN HEADERS A LAST FIELD TO PRINT THE CLOSE INVOICE CHECKBOX TRICK :)
  442. push @column_headers, {text => 'X'};
  443. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  444. # IF WE DONT HAVE ANY INVOICES MATCHING THE FILTER PARAMETERS, WE WILL WARN THE USER AND STOP
  445. # THE PROCCESS.
  446. my @invoice_data;
  447. @array_options = $Payment->get_open_invoices();
  448. if (!$array_options[0]->{invoice_id}) {
  449. $request->error($locale->text("Nothing to do"));
  450. }
  451. for my $ref (0 .. $#array_options) {
  452. if ( !$request->{"checkbox_$array_options[$ref]->{invoice_id}"}) {
  453. #We have to set some things first ...
  454. my $due_fx; my $topay_fx_value;
  455. if ("$exchangerate") {
  456. $topay_fx_value = $due_fx = "$array_options[$ref]->{due}"/"$exchangerate";
  457. } else {
  458. $topay_fx_value = $due_fx = "N/A";
  459. }
  460. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  461. id => $array_options[$ref]->{invoice_id},
  462. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  463. },
  464. invoice_date => "$array_options[$ref]->{invoice_date}",
  465. amount => "$array_options[$ref]->{amount}",
  466. due => "$array_options[$ref]->{due}",
  467. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  468. exchange_rate => "$exchangerate",
  469. due_fx => $due_fx, # This was set at the begining of the for statement
  470. topay => "$array_options[$ref]->{due}",
  471. source_text => $request->{"source_text_$array_options[$ref]->{invoice_id}"},
  472. optional => $request->{"optional_pay_$array_options[$ref]->{invoice_id}"},
  473. selected_account => $request->{"account_$array_options[$ref]->{invoice_id}"},
  474. selected_source => $request->{"source_$array_options[$ref]->{invoice_id}"},
  475. topay_fx => { name => "topay_fx_$array_options[$ref]->{invoice_id}",
  476. value => $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} ?
  477. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} eq 'N/A' ?
  478. $topay_fx_value :
  479. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} :
  480. $topay_fx_value
  481. # Ugly hack, but works ;) ... not sure if i should
  482. # change it.
  483. }
  484. };# END PUSH
  485. }
  486. else {
  487. push @selected_checkboxes, {name => "checkbox_$array_options[$ref]->{invoice_id}",
  488. value => "checked"} ;
  489. } #END IF
  490. }# END FOR
  491. if (!@invoice_data) {
  492. $request->error($locale->text("Nothing to do").'!');
  493. }
  494. # LETS BUILD THE SELECTION FOR THE UI
  495. my $select = {
  496. stylesheet => $request->{_user}->{stylesheet},
  497. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  498. login => { name => 'login',
  499. value => $request->{_user}->{login} },
  500. accountclass => {
  501. name => 'account_class',
  502. value => $Payment->{account_class}
  503. },
  504. project => @project ? @project : '' , # WE NEED TO VERIFY THAT THE ARRAY EXISTS, IF IT DOESNT,
  505. department => @department ? @department : '', # WE WILL PASS A NULL STRING, THIS FIXES THE ISSUES
  506. # I WAS HAVING WITH THE NULL ARRAYS, STILL UGLY :P
  507. account => \@account_options,
  508. selected_account => $request->{account},
  509. datepaid => {
  510. name => 'datepaid',
  511. value => $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date}
  512. },
  513. source => \@sources_options,
  514. selected_source => $request->{source},
  515. source_value => $request->{source_value},
  516. defaultcurrency => {
  517. text => $default_currency
  518. },
  519. curr => { name => 'curr',
  520. value => $request->{curr},
  521. },
  522. column_headers => \@column_headers,
  523. rows => \@invoice_data,
  524. vendorcustomer => { name => 'vendor-customer',
  525. value => $request->{'vendor-customer'}
  526. },
  527. vc => { name => $vendor_customer_name,
  528. address => [ {text => 'Crra 83 #32 -1'},
  529. {text => '442 6464'},
  530. {text => 'Medellín'},
  531. {text => 'Colombia'}]
  532. },
  533. update => {
  534. title => 'UPDATE ALT+U',
  535. name => 'action',
  536. value => 'payment2',
  537. text => $locale->text('UPDATE')
  538. },
  539. post => {
  540. title => 'POST ALT+O',
  541. name => 'action',
  542. value => 'post',
  543. text => $locale->text('POST')
  544. },
  545. post_and_print => {
  546. title => 'POST AND PRINT ALT+R',
  547. name => 'action',
  548. value => 'post_and_print',
  549. text => $locale->text("POST AND PRINT")
  550. },
  551. format => {
  552. name => 'FORMAT',
  553. options => [
  554. {value => 1, text => "HTML" },
  555. {value => 2, text => "PDF" },
  556. {value => 3, text => "POSTSCRIPT" }
  557. ],
  558. },
  559. media => {
  560. name => 'MEDIA',
  561. options => [
  562. {value => 1, text => "Screen" },
  563. {value => 2, text => "PRINTER" },
  564. {value => 3, text => "EMAIL" }
  565. ],
  566. },
  567. exrate => @currency_options,
  568. selectedcheckboxes => @selected_checkboxes ? \@selected_checkboxes : '',
  569. notes => $request->{notes}
  570. };
  571. my $template = LedgerSMB::Template->new(
  572. user => $request->{_user},
  573. locale => $request->{_locale},
  574. path => 'UI/payments',
  575. template => 'payment2',
  576. format => 'HTML' );
  577. eval {$template->render($select) };
  578. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  579. }
  580. eval { do "scripts/custom/payment.pl"};
  581. 1;