summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: 991a8f541b45ced2f03c285602e4d51772b63b10 (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. sub payment {
  246. my ($request) = @_;
  247. my $locale = $request->{_locale};
  248. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  249. # Lets get the project data...
  250. my @projectOptions;
  251. my @arrayOptions = $dbPayment->list_open_projects();
  252. push @projectOptions, {}; #A blank field on the select box
  253. for my $ref (0 .. $#arrayOptions) {
  254. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  255. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  256. }
  257. # Lets get the departments data...
  258. my @departmentOptions;
  259. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  260. @arrayOptions = $dbPayment->list_departments($role);
  261. push @departmentOptions, {}; # A blank field on the select box
  262. for my $ref (0 .. $#arrayOptions) {
  263. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  264. text => $arrayOptions[$ref]->{description}};
  265. }
  266. # Lets get the customer or vendor :)
  267. my @vcOptions;
  268. @arrayOptions = $dbPayment->get_open_accounts();
  269. for my $ref (0 .. $#arrayOptions) {
  270. push @vcOptions, { value => $arrayOptions[$ref]->{id}.'--'.$arrayOptions[$ref]->{name},
  271. text => $arrayOptions[$ref]->{name}};
  272. }
  273. # Lets get the open currencies (this uses the $dbPayment->{account_class} property)
  274. my @currOptions;
  275. @arrayOptions = $dbPayment->get_open_currencies();
  276. for my $ref (0 .. $#arrayOptions) {
  277. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  278. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  279. }
  280. # Lets build filter by period
  281. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  282. $date->build_filter_by_period($locale);
  283. # Lets set the data in a hash for the template system. :)
  284. my $select = {
  285. stylesheet => $request->{_user}->{stylesheet},
  286. login => { name => 'login',
  287. value => $request->{_user}->{login} },
  288. projects => {
  289. name => 'projects',
  290. options => \@projectOptions
  291. },
  292. department => {
  293. name => 'department',
  294. options => \@departmentOptions
  295. },
  296. vendor_customer => {
  297. name => 'vendor-customer',
  298. options => \@vcOptions
  299. },
  300. curr => {
  301. name => 'curr',
  302. options => \@currOptions
  303. },
  304. month => {
  305. name => 'month',
  306. options => $date->{monthsOptions}
  307. },
  308. year => {
  309. name => 'year',
  310. options => $date->{yearsOptions}
  311. },
  312. interval_radios => $date->{radioOptions},
  313. amountfrom => {
  314. name => 'amountfrom',
  315. },
  316. amountto => {
  317. name => 'amountto',
  318. },
  319. accountclass => {
  320. name => 'account_class',
  321. value => $dbPayment->{account_class}
  322. },
  323. type => {
  324. name => 'type',
  325. value => $request->{type}
  326. },
  327. action => {
  328. name => 'action',
  329. value => 'payment2',
  330. text => $locale->text("Continue"),
  331. },
  332. };
  333. # Lets call upon the template system
  334. my $template;
  335. $template = LedgerSMB::Template->new(
  336. user => $request->{_user},
  337. locale => $request->{_locale},
  338. path => 'UI/payments',
  339. # path => 'UI',
  340. template => 'payment1',
  341. format => 'HTML', );
  342. $template->render($select);# And finally, Lets print the screen :)
  343. }
  344. =pod
  345. =item payment2
  346. This method is used for the payment module, it is a consecuence of the payment sub,
  347. and its used for all the mechanics of an invoices payment module.
  348. =back
  349. =cut
  350. sub payment2 {
  351. my ($request) = @_;
  352. my $locale = $request->{_locale};
  353. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  354. my @array_options;
  355. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  356. ($dbPayment->{entity_id}, my $vendor_customer_name) = split /--/ , $request->{'vendor-customer'};
  357. my @array_options;
  358. my $exchangerate;
  359. # LETS BUILD THE PROJECTS INFO
  360. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  361. my ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  362. my @project = { name => 'project', text => $project_number.' '.$project_name, value => $project_id };
  363. # LETS GET THE DEPARTMENT INFO
  364. my ($department_id, $department_name) = split /--/, $request->{department};
  365. my @department = { name => 'department', text => $department_name, value => $department_id };
  366. # LETS GET ALL THE ACCOUNTS
  367. my @account_options;
  368. @array_options = $dbPayment->list_accounting();
  369. for my $ref (0 .. $#array_options) {
  370. push @account_options, { value => $array_options[$ref]->{id},
  371. text => $array_options[$ref]->{description}};
  372. }
  373. # LETS GET THE POSSIBLE SOURCES
  374. my @sources_options;
  375. @array_options = $dbPayment->get_sources(\%$locale);
  376. for my $ref (0 .. $#array_options) {
  377. push @sources_options, { value => $array_options[$ref],
  378. text => $array_options[$ref]};
  379. }
  380. # WE MUST PREPARE THE ENTITY INFORMATION
  381. @array_options = $dbPayment->get_vc_info();
  382. # LETS BUILD THE CURRENCIES INFORMATION
  383. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  384. my $default_currency = $dbPayment->get_default_currency();
  385. my @currency_options;
  386. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  387. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  388. # -----------------------------------------------
  389. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  390. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  391. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  392. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  393. my @columnAS = ({text => $locale->text('Invoice')},
  394. {text => $locale->text('Date')},
  395. {text => $locale->text('Total').$default_currency_text},
  396. {text => $locale->text('Paid').$default_currency_text},
  397. {text => $locale->text('Amount Due').$default_currency_text},
  398. {text => $locale->text('To pay').$default_currency_text}
  399. );
  400. my @column_headers = ({text => $locale->text('Invoice')},
  401. {text => $locale->text('Date')},
  402. {text => $locale->text('Total').$default_currency_text},
  403. {text => $locale->text('Paid').$default_currency_text},
  404. {text => $locale->text('Amount Due').$default_currency_text},
  405. {text => $locale->text('To pay').$default_currency_text}
  406. );
  407. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  408. if ($default_currency ne $request->{curr} ) {
  409. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  410. push @column_headers, {text => $locale->text('Exchange Rate')},
  411. {text => $locale->text('Amount Due').$currency_text},
  412. {text => $locale->text('To pay').$currency_text};
  413. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  414. @column_headers[5,6,7] = @column_headers[6,7,5];
  415. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  416. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  417. $exchangerate = $dbPayment->get_exchange_rate($request->{curr}, $dbPayment->{current_date});
  418. if ($exchangerate) {
  419. @currency_options = {
  420. name => 'date_curr',
  421. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  422. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  423. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  424. };
  425. } else {
  426. @currency_options = {
  427. name => 'date_curr'};
  428. }
  429. } else {
  430. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  431. # ARE USING THE DEFAULT CURRENCY
  432. $exchangerate = 1;
  433. }
  434. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  435. # IF WE DONT HAVE ANY INVOICES MATCHING THE FILTER PARAMETERS, WE WILL WARN THE USER AND STOP
  436. # THE PROCCESS.
  437. my @invoice_data;
  438. @array_options = $dbPayment->get_open_invoices();
  439. if (!$array_options[0]->{invoice_id}) {
  440. $request->error($locale->text("Nothing to do"));
  441. }
  442. for my $ref (0 .. $#array_options) {
  443. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  444. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  445. },
  446. invoice_date => "$array_options[$ref]->{invoice_date}",
  447. amount => "$array_options[$ref]->{amount}",
  448. due => "$array_options[$ref]->{due}",
  449. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  450. exchange_rate => "$exchangerate",
  451. due_fx => "$exchangerate"? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A',
  452. topay => "$array_options[$ref]->{due}",
  453. topay_fx => { name => "topay_fx_$ref",
  454. value => "$exchangerate" ? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A'
  455. }
  456. };
  457. }
  458. # LETS BUILD THE SELECTION FOR THE UI
  459. my $select = {
  460. stylesheet => $request->{_user}->{stylesheet},
  461. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  462. project => @project,
  463. department => @department,
  464. account => {
  465. name => 'account',
  466. options => \@account_options},
  467. datepaid => {
  468. name => 'datepaid',
  469. value => $dbPayment->{current_date}
  470. },
  471. source => {
  472. name => 'source',
  473. options => \@sources_options
  474. },
  475. source_text => {
  476. name => 'source_text',
  477. },
  478. defaultcurrency => {
  479. text => $default_currency
  480. },
  481. curr => {
  482. text => $request->{curr}
  483. },
  484. column_headers => \@column_headers,
  485. rows => \@invoice_data,
  486. vc => { name => $vendor_customer_name,
  487. address => [ {text => 'Crra 83 #32 -1'},
  488. {text => '442 6464'},
  489. {text => 'Medellín'},
  490. {text => 'Colombia'}]},
  491. post => {
  492. accesskey => 'O',
  493. title => 'POST ALT+O',
  494. name => 'action',
  495. value => 'post',
  496. text => "POST"
  497. },
  498. post_and_print => {
  499. accesskey => 'R',
  500. title => 'POST AND PRINT ALT+R',
  501. name => 'action',
  502. value => 'post_and_print',
  503. text => "POST AND PRINT"
  504. },
  505. format => {
  506. name => 'FORMAT',
  507. options => [
  508. {value => 1, text => "HTML" },
  509. {value => 2, text => "PDF" },
  510. {value => 3, text => "POSTSCRIPT" }
  511. ],
  512. },
  513. media => {
  514. name => 'MEDIA',
  515. options => [
  516. {value => 1, text => "Screen" },
  517. {value => 2, text => "PRINTER" },
  518. {value => 3, text => "EMAIL" }
  519. ],
  520. },
  521. date_curr => @currency_options # I HAVE TO PUT THIS LAST, BECAUSE IT CAN BE NULL
  522. # THIS IS AN UGLY HACK THAT MUST BE FIXED.
  523. };
  524. my $template = LedgerSMB::Template->new(
  525. user => $request->{_user},
  526. locale => $request->{_locale},
  527. # path => 'UI/payments',
  528. path => 'UI',
  529. template => 'payment2',
  530. format => 'HTML' );
  531. eval {$template->render($select) };
  532. if ($@) { $request->error("$@"); }
  533. }
  534. eval { do "scripts/custom/payment.pl"};
  535. 1;