summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: 986cd764f91479c4f12285b7659376988a598938 (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 Error::Simple;
  40. use strict;
  41. # CT: A few notes for future refactoring of this code:
  42. # 1: I don't think it is a good idea to make the UI too dependant on internal
  43. # code structures but I don't see a good alternative at the moment.
  44. # 2: CamelCasing: -1
  45. =pod
  46. =item payment
  47. This method is used to set the filter screen and prints it, using the
  48. TT2 system.
  49. =back
  50. =cut
  51. sub payments {
  52. my ($request) = @_;
  53. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  54. $payment->get_metadata();
  55. my $template = LedgerSMB::Template->new(
  56. user => $request->{_user},
  57. locale => $request->{_locale},
  58. path => 'UI/payments',
  59. template => 'payments_filter',
  60. format => 'HTML',
  61. );
  62. $template->render($payment);
  63. }
  64. sub print {
  65. }
  66. sub get_search_criteria {
  67. my ($request) = @_;
  68. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  69. $payment->get_metadata();
  70. if ($payment->{batch_id} && $payment->{batch_date}){
  71. $payment->{date_reversed} = $payment->{batch_date};
  72. }
  73. my $template = LedgerSMB::Template->new(
  74. user => $request->{_user},
  75. locale => $request->{_locale},
  76. path => 'UI/payments',
  77. template => 'search',
  78. format => 'HTML',
  79. );
  80. $template->render($payment);
  81. }
  82. sub get_search_results {
  83. my ($request) = @_;
  84. my $rows = [];
  85. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  86. my @search_results = $payment->search;
  87. my $template = LedgerSMB::Template->new(
  88. user => $request->{_user},
  89. locale => $request->{_locale},
  90. path => 'UI',
  91. template => 'form-dynatable',
  92. format => ($payment->{format}) ? $payment->{format} : 'HTML',
  93. );
  94. my $base_url = "payment.pl?";
  95. my $search_url = "$base_url";
  96. for my $key (keys %{$request->take_top_level}){
  97. if ($base_url =~ /\?$/){
  98. $base_url .= "$key=$request->{key}";
  99. } else {
  100. $base_url .= "&$key=$request->{key}";
  101. }
  102. }
  103. my @columns = qw(selected meta_number date_paid amount source company_paid);
  104. my $contact_type = ($payment->{account_class} == 1) ? 'Vendor' : 'Customer';
  105. # CT: Locale strings for gettext:
  106. # $request->{_locale}->text("Vendor Number");
  107. # $request->{_locale}->text("Customer Number");
  108. my $heading = {
  109. selected => $request->{_locale}->text('Selected'),
  110. company_paid => {
  111. text => $request->{_locale}->text('Company Name'),
  112. href => "$search_url&orderby=company_paid",
  113. },
  114. meta_number => {
  115. text => $request->{_locale}->text(
  116. "$contact_type Number"
  117. ),
  118. href => "$search_url&orderby=meta_number",
  119. },
  120. date_paid => {
  121. text => $request->{_locale}->text('Date Paid'),
  122. href => "$search_url&orderby=date_paid",
  123. },
  124. amount => {
  125. text => $request->{_locale}->text('Total Paid'),
  126. href => "$search_url&orderby=amount",
  127. },
  128. source => {
  129. text => $request->{_locale}->text('Source'),
  130. href => "$search_url&orderby=source",
  131. },
  132. };
  133. my $classcount;
  134. $classcount = 0;
  135. my $rowcount;
  136. $rowcount = 1;
  137. for my $line (@search_results){
  138. $classcount ||= 0;
  139. $rowcount += 1;
  140. push(@$rows, {
  141. company_paid => $line->{company_paid},
  142. amount => $request->format_amount(amount => $line->{amount}),
  143. i => "$classcount",
  144. date_paid => $line->{date_paid},
  145. source => $line->{source},
  146. meta_number => $line->{meta_number},
  147. selected => {
  148. input => {
  149. type => "checkbox",
  150. name => "payment_$rowcount",
  151. value => "1",
  152. },
  153. }
  154. });
  155. $payment->{"credit_id_$rowcount"} = $line->{credit_id};
  156. $payment->{"date_paid_$rowcount"} = $line->{date_paid};
  157. $payment->{"source_$rowcount"} = $line->{source};
  158. $classcount = ($classcount + 1) % 2;
  159. ++$rowcount;
  160. }
  161. $payment->{rowcount} = $rowcount;
  162. $payment->{script} = 'payment.pl';
  163. $payment->{title} = $request->{_locale}->text("Payment Results");
  164. my $hiddens = $payment->take_top_level;
  165. $template->render({
  166. form => $payment,
  167. columns => \@columns,
  168. heading => $heading,
  169. hiddens => $payment->take_top_level,
  170. rows => $rows,
  171. buttons => [{
  172. value => 'reverse_payments',
  173. name => 'action',
  174. class => 'submit',
  175. type => 'submit',
  176. text => $request->{_locale}->text('Reverse Payments'),
  177. }]
  178. });
  179. }
  180. sub get_search_results_reverse_payments {
  181. my ($request) = @_;
  182. my $payment = LedgerSMB::DBObject::Payment->new({base => $request});
  183. for my $count (1 .. $payment->{rowcount}){
  184. if ($payment->{"payment_$count"}){
  185. $payment->{credit_id} = $payment->{"credit_id_$count"};
  186. $payment->{date_paid} = $payment->{"date_paid_$count"};
  187. $payment->{source} = $payment->{"source_$count"};
  188. $payment->reverse;
  189. }
  190. }
  191. get_search_criteria($payment);
  192. }
  193. sub check_job {
  194. my ($request) = @_;
  195. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  196. $payment->check_job;
  197. my $template = LedgerSMB::Template->new(
  198. user => $request->{_user},
  199. locale => $request->{_locale},
  200. path => 'UI/payments',
  201. template => 'check_job',
  202. format => 'HTML',
  203. );
  204. $template->render($payment);
  205. }
  206. sub post_payments_bulk {
  207. my ($request) = @_;
  208. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  209. $payment->post_bulk();
  210. my $template;
  211. if ($payment->{queue_payments}){
  212. $payment->{job_label} = 'Payments';
  213. $template = LedgerSMB::Template->new(
  214. user => $request->{_user},
  215. locale => $request->{_locale},
  216. path => 'UI/payments',
  217. template => 'check_job',
  218. format => 'HTML',
  219. );
  220. } else {
  221. payments($request);
  222. }
  223. $template->render($payment);
  224. }
  225. sub print {
  226. use LedgerSMB::DBObject::Company;
  227. my ($request) = @_;
  228. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  229. $payment->{company} = $payment->{_user}->{company};
  230. $payment->{address} = $payment->{_user}->{address};
  231. my $template;
  232. if ($payment->{multiple}){
  233. $payment->{checks} = [];
  234. print "Multiple checks printing";
  235. for my $line (1 .. $payment->{contact_count}){
  236. my $id = $payment->{"contact_$line"};
  237. next if !defined $payment->{"id_$id"};
  238. my $check = LedgerSMB::DBObject::Company->new(
  239. {base => $request, copy => 'base' }
  240. );
  241. $check->{entity_class} = $payment->{account_class};
  242. $check->{id} = $id;
  243. $check->get_billing_info;
  244. $check->{amount} = $check->parse_amount(amount => '0');
  245. $check->{invoices} = [];
  246. $check->{source} = $payment->{"source_$id"};
  247. for my $inv (1 .. $payment->{"invoice_count_$id"}){
  248. print STDERR "Invoice $inv of " .$payment->{"invoice_count_$id"} . "\n";
  249. my $invhash = {};
  250. my $inv_id = $payment->{"invoice_${id}_$inv"};
  251. for (qw(invnumber invdate)){
  252. $invhash->{$_} = $payment->{"${_}_$inv_id"};
  253. }
  254. if ($payment->{"paid_$id"} eq 'some'){
  255. $invhash->{paid} = $payment->parse_amount(amount => $payment->{"payment_$inv_id"});
  256. } elsif ($payment->{"paid_$id"} eq 'all'){
  257. $invhash->{paid} = $payment->parse_amount(amount => $payment->{"net_$inv_id"});
  258. } else {
  259. $payment->error("Invalid Payment Amount Option");
  260. }
  261. $check->{amount} += $invhash->{paid};
  262. $invhash->{paid} = $check->format_amount(amount => $invhash->{paid});
  263. push @{$check->{invoices}}, $invhash;
  264. }
  265. my $amt = $check->{amount}->copy;
  266. $amt->bfloor();
  267. $check->{text_amount} = $payment->text_amount($amt);
  268. $check->{amount} = $check->format_amount(amount => $check->{amount});
  269. $check->{decimal} = $check->format_amount(amount => ($check->{amount} - $amt) * 100);
  270. print STDERR "amount = $check->{amount}, texamount = $check->{textamount}\n";
  271. push @{$payment->{checks}}, $check;
  272. }
  273. $template = LedgerSMB::Template->new(
  274. user => $payment->{_user}, template => 'check_multiple',
  275. format => uc $payment->{'format'},
  276. no_auto_output => 1,
  277. output_args => $payment,
  278. );
  279. try {
  280. $template->render($payment);
  281. $template->output(%$payment);
  282. }
  283. catch Error::Simple with {
  284. my $E = shift;
  285. $payment->error( $E->stacktrace );
  286. };
  287. } else {
  288. }
  289. }
  290. sub display_payments {
  291. my ($request) = @_;
  292. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  293. $payment->get_payment_detail_data();
  294. for (@{$payment->{contact_invoices}}){
  295. $_->{total_due} = $payment->format_amount(amount => $_->{total_due});
  296. }
  297. @{$payment->{media_options}} = (
  298. {text => $request->{_locale}->text('Screen'),
  299. value => 'screen'});
  300. for (keys %LedgerSMB::Sysconfig::printer){
  301. push @{$payment->{media_options}},
  302. {text => $_,
  303. value => $LedgerSMB::Sysconfig::printer{$_}};
  304. }
  305. if ($LedgerSMB::Sysconfig::latex){
  306. @{$payment->{format_options}} = (
  307. {text => 'PDF', value => 'PDF'},
  308. {text => 'Postscript', value => 'Postscript'},
  309. );
  310. $payment->{can_print} = 1;
  311. }
  312. my $template = LedgerSMB::Template->new(
  313. user => $request->{_user},
  314. locale => $request->{_locale},
  315. path => 'UI/payments',
  316. template => 'payments_detail',
  317. format => 'HTML',
  318. );
  319. $template->render($payment);
  320. }
  321. =item payment
  322. This method is used to set the filter screen and prints it, using the
  323. TT2 system.
  324. =back
  325. =cut
  326. sub payment {
  327. my ($request) = @_;
  328. my $locale = $request->{_locale};
  329. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  330. # Lets get the project data...
  331. my @projectOptions;
  332. my @arrayOptions = $dbPayment->list_open_projects();
  333. push @projectOptions, {}; #A blank field on the select box
  334. for my $ref (0 .. $#arrayOptions) {
  335. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  336. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  337. }
  338. # Lets get the departments data...
  339. my @departmentOptions;
  340. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  341. @arrayOptions = $dbPayment->list_departments($role);
  342. push @departmentOptions, {}; # A blank field on the select box
  343. for my $ref (0 .. $#arrayOptions) {
  344. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  345. text => $arrayOptions[$ref]->{description}};
  346. }
  347. # Lets get the currencies (this uses the $dbPayment->{account_class} property)
  348. my @currOptions;
  349. @arrayOptions = $dbPayment->get_open_currencies();
  350. for my $ref (0 .. $#arrayOptions) {
  351. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  352. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  353. }
  354. # Lets build filter by period
  355. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  356. $date->build_filter_by_period($locale);
  357. # Lets set the data in a hash for the template system. :)
  358. my $select = {
  359. stylesheet => $request->{_user}->{stylesheet},
  360. login => { name => 'login',
  361. value => $request->{_user}->{login} },
  362. projects => {
  363. name => 'projects',
  364. options => \@projectOptions
  365. },
  366. department => {
  367. name => 'department',
  368. options => \@departmentOptions
  369. },
  370. curr => {
  371. name => 'curr',
  372. options => \@currOptions
  373. },
  374. month => {
  375. name => 'month',
  376. options => $date->{monthsOptions}
  377. },
  378. year => {
  379. name => 'year',
  380. options => $date->{yearsOptions}
  381. },
  382. interval_radios => $date->{radioOptions},
  383. amountfrom => {
  384. name => 'amountfrom',
  385. },
  386. amountto => {
  387. name => 'amountto',
  388. },
  389. accountclass => {
  390. name => 'account_class',
  391. value => $dbPayment->{account_class}
  392. },
  393. type => {
  394. name => 'type',
  395. value => $request->{type}
  396. },
  397. action => {
  398. name => 'action',
  399. value => 'payment1_5',
  400. text => $locale->text("Continue"),
  401. }
  402. };
  403. my $template;
  404. $template = LedgerSMB::Template->new(
  405. user => $request->{_user},
  406. locale => $request->{_locale},
  407. path => 'UI/payments',
  408. template => 'payment1',
  409. format => 'HTML' );
  410. $template->render($select);# And finally, Lets print the screen :)
  411. }
  412. =pod
  413. =item payment1_5
  414. This method is called between payment and payment2, it will search the database
  415. for entity_credit_accounts that match the parameter, if only one is found it will
  416. run unnoticed by the user, if more than one is found it will ask the user to pick
  417. one to handle the payment against
  418. =back
  419. =cut
  420. sub payment1_5 {
  421. my ($request) = @_;
  422. my $locale = $request->{_locale};
  423. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  424. my @array_options = $dbPayment->get_entity_credit_account();
  425. if ($#array_options == -1) {
  426. &payment($request);
  427. } elsif ($#array_options == 0) {
  428. $request->{'vendor-customer'} = $array_options[0]->{id}.'--'.$array_options[0]->{name};
  429. &payment2($request);
  430. } else {
  431. # Lets call upon the template system
  432. my @company_options;
  433. for my $ref (0 .. $#array_options) {
  434. push @company_options, { id => $array_options[$ref]->{id},
  435. name => $array_options[$ref]->{name}};
  436. }
  437. my $select = {
  438. companies => \@company_options,
  439. stylesheet => $request->{_user}->{stylesheet},
  440. login => { name => 'login',
  441. value => $request->{_user}->{login}},
  442. department => { name => 'department',
  443. value => $request->{department}},
  444. currency => { name => 'curr',
  445. value => $request->{curr}},
  446. datefrom => { name => 'datefrom',
  447. value => $request->{datefrom}},
  448. dateto => { name => 'dateto',
  449. value => $request->{dateto}},
  450. amountfrom => { name => 'amountfrom',
  451. value => $request->{datefrom}},
  452. amountto => { name => 'amountto',
  453. value => $request->{dateto}},
  454. accountclass => { name => 'account_class',
  455. value => $dbPayment->{account_class}},
  456. type => { name => 'type',
  457. value => $request->{type}},
  458. action => { name => 'action',
  459. value => 'payment2',
  460. text => $locale->text("Continue")}
  461. };
  462. my $template;
  463. $template = LedgerSMB::Template->new(
  464. user => $request->{_user},
  465. locale => $request->{_locale},
  466. path => 'UI/payments',
  467. template => 'payment1_5',
  468. format => 'HTML' );
  469. eval {$template->render($select) };
  470. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  471. }
  472. }
  473. =pod
  474. =item payment2
  475. This method is used for the payment module, it is a consecuence of the payment sub,
  476. and its used for all the mechanics of an invoices payment module.
  477. =back
  478. =cut
  479. sub payment2 {
  480. my ($request) = @_;
  481. my $locale = $request->{_locale};
  482. my $Payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  483. # VARIABLES
  484. my ($project_id, $project_number, $project_name, $department_id, $department_name );
  485. my @array_options;
  486. my @project;
  487. my @selected_checkboxes;
  488. my @department;
  489. my @array_options;
  490. my @currency_options;
  491. my $exchangerate;
  492. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  493. ($Payment->{entity_credit_id}, $Payment->{company_name}) = split /--/ , $request->{'vendor-customer'};
  494. # WE NEED TO RETRIEVE A BILLING LOCATION, THIS IS HARDCODED FOR NOW... Should we change it?
  495. $Payment->{location_class_id} = '1';
  496. my @vc_options;
  497. @vc_options = $Payment->get_vc_info();
  498. # LETS BUILD THE PROJECTS INFO
  499. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  500. if ($request->{projects}) {
  501. ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  502. @project = { name => 'projects', text => $project_number.' '.$project_name, value => $request->{projects}};
  503. }
  504. # LETS GET THE DEPARTMENT INFO
  505. # WE HAVE TO SET $dbPayment->{department_id} NOW, THIS DATA WILL BE USED LATER WHEN WE
  506. # CALL FOR payment_get_open_invoices. :)
  507. if ($request->{department}) {
  508. ($Payment->{department_id}, $department_name) = split /--/, $request->{department};
  509. @department = { name => 'department', text => $department_name, value => $request->{department}};
  510. }
  511. # LETS GET ALL THE ACCOUNTS
  512. my @account_options = $Payment->list_accounting();
  513. # LETS GET THE POSSIBLE SOURCES
  514. my @sources_options = $Payment->get_sources(\%$locale);
  515. # WE MUST PREPARE THE ENTITY INFORMATION
  516. @array_options = $Payment->get_vc_info();
  517. # LETS BUILD THE CURRENCIES INFORMATION
  518. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  519. my $default_currency = $Payment->get_default_currency();
  520. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  521. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  522. # -----------------------------------------------
  523. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  524. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  525. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  526. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  527. my @column_headers = ({text => $locale->text('Invoice')},
  528. {text => $locale->text('Date')},
  529. {text => $locale->text('Total').$default_currency_text},
  530. {text => $locale->text('Paid').$default_currency_text},
  531. {text => $locale->text('Discount').$default_currency_text},
  532. {text => $locale->text('Amount Due').$default_currency_text},
  533. {text => $locale->text('To pay').$default_currency_text}
  534. );
  535. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  536. if ($default_currency ne $request->{curr} ) {
  537. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  538. push @column_headers, {text => $locale->text('Exchange Rate')},
  539. {text => $locale->text('Amount Due').$currency_text},
  540. {text => $locale->text('To pay').$currency_text};
  541. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  542. @column_headers[6,7,8] = @column_headers[7,8,6];
  543. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  544. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  545. $exchangerate = $request->{exrate} ?
  546. $request->{exrate} :
  547. $Payment->get_exchange_rate($request->{curr},
  548. $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date});
  549. if ($exchangerate) {
  550. @currency_options = {
  551. name => 'exrate',
  552. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  553. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  554. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  555. };
  556. } else {
  557. @currency_options = {
  558. name => 'exrate'};
  559. }
  560. } else {
  561. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  562. # ARE USING THE DEFAULT CURRENCY
  563. $exchangerate = 1;
  564. @currency_options = {
  565. name => 'exrate',
  566. value => 1,
  567. text => 1
  568. };
  569. }
  570. # FINALLY WE ADD TO THE COLUMN HEADERS A LAST FIELD TO PRINT THE CLOSE INVOICE CHECKBOX TRICK :)
  571. push @column_headers, {text => 'X'};
  572. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  573. # WE WONT DO ANYTHING IF WE DONT FIND ANY INVOICES, THE USER CAN STILL POST A PREPAYMENT
  574. my @invoice_data;
  575. my @topay_state; # WE WILL USE THIS TO HELP UI TO DETERMINE WHAT IS VISIBLE
  576. @array_options = $Payment->get_open_invoices();
  577. for my $ref (0 .. $#array_options) {
  578. if ( !$request->{"checkbox_$array_options[$ref]->{invoice_id}"}) {
  579. # SHOULD I APPLY DISCCOUNTS?
  580. # LETS SET THE EXCHANGERATE VALUES
  581. my $due_fx; my $topay_fx_value;
  582. if ("$exchangerate") {
  583. $topay_fx_value = $due_fx = "$array_options[$ref]->{due}"/"$exchangerate" - "$array_options[$ref]->{discount}"/"$exchangerate";
  584. } else {
  585. $topay_fx_value = $due_fx = "N/A";
  586. }
  587. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  588. id => $array_options[$ref]->{invoice_id},
  589. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  590. },
  591. invoice_date => "$array_options[$ref]->{invoice_date}",
  592. amount => "$array_options[$ref]->{amount}",
  593. due => "$array_options[$ref]->{due}" - "$array_options[$ref]->{discount}",
  594. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  595. discount => "$array_options[$ref]->{discount}",
  596. exchange_rate => "$exchangerate",
  597. due_fx => $due_fx, # This was set at the begining of the for statement
  598. topay => "$array_options[$ref]->{due}" - "$array_options[$ref]->{discount}",
  599. source_text => $request->{"source_text_$array_options[$ref]->{invoice_id}"},
  600. optional => $request->{"optional_pay_$array_options[$ref]->{invoice_id}"},
  601. selected_account => $request->{"account_$array_options[$ref]->{invoice_id}"},
  602. selected_source => $request->{"source_$array_options[$ref]->{invoice_id}"},
  603. topay_fx => { name => "topay_fx_$array_options[$ref]->{invoice_id}",
  604. value => $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} ?
  605. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} eq 'N/A' ?
  606. $topay_fx_value :
  607. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} :
  608. $topay_fx_value
  609. # Ugly hack, but works ;) ...
  610. }#END HASH
  611. };# END PUSH
  612. push @topay_state, {
  613. id => "topaystate_$array_options[$ref]->{invoice_id}",
  614. value => $request->{"topaystate_$array_options[$ref]->{invoice_id}"}
  615. }; #END PUSH
  616. }
  617. else {
  618. push @selected_checkboxes, {name => "checkbox_$array_options[$ref]->{invoice_id}",
  619. value => "checked"} ;
  620. } #END IF
  621. }# END FOR
  622. # And finally, we are going to store the information for the overpayment / prepayment / advanced payment
  623. # and all the stuff, this is only needed for the update function.
  624. my @overpayment;
  625. my @overpayment_account;
  626. # Got to build the account selection box first.
  627. my @overpayment_account = $Payment->list_overpayment_accounting();
  628. # Now we build the structure for the UI
  629. for (my $i=1 ; $i <= $request->{overpayment_qty}; $i++) {
  630. if (!$request->{"overpayment_checkbox_$i"}) {
  631. if ( $request->{"overpayment_topay_$i"} ) {
  632. # Now we split the account selected options
  633. my ($id, $accno, $description) = split(/--/, $request->{"overpayment_account_$i"});
  634. my ($cashid, $cashaccno, $cashdescription ) = split(/--/, $request->{"overpayment_cash_account_$i"});
  635. push @overpayment, {amount => $request->{"overpayment_topay_$i"},
  636. source1 => $request->{"overpayment_source1_$i"},
  637. source2 => $request->{"overpayment_source2_$i"},
  638. memo => $request->{"overpayment_memo_$i"},
  639. account => { id => $id,
  640. accno => $accno,
  641. description => $description
  642. },
  643. cashaccount => { id => $cashid,
  644. accno => $cashaccno,
  645. description => $cashdescription
  646. }
  647. };
  648. } else {
  649. $i = $request->{overpayment_qty} + 1;
  650. }
  651. }
  652. }
  653. # We need to set the availible media and format from printing
  654. my @media_options;
  655. push @media_options, {value => 1, text => "Screen"};
  656. if ($#{LedgerSMB::Sysconfig::printer}) {
  657. for (keys %{LedgerSMB::Sysconfig::printer}) {
  658. push @media_options, {value => 1, text => $_};
  659. }
  660. }
  661. #$request->error("@media_options");
  662. my @format_options;
  663. push @format_options, {value => 1, text => "HTML"};
  664. if (${LedgerSMB::Sysconfig::latex}) {
  665. push @format_options, {value => 2, text => "PDF" }, {value => 3, text => "POSTSCRIPT" };
  666. }
  667. # LETS BUILD THE SELECTION FOR THE UI
  668. my $select = {
  669. stylesheet => $request->{_user}->{stylesheet},
  670. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  671. type => { name => 'type',
  672. value => $request->{type} },
  673. login => { name => 'login',
  674. value => $request->{_user}->{login} },
  675. accountclass => {
  676. name => 'account_class',
  677. value => $Payment->{account_class}
  678. },
  679. project => @project ? @project : '' , # WE NEED TO VERIFY THAT THE ARRAY EXISTS, IF IT DOESNT,
  680. department => @department ? @department : '', # WE WILL PASS A NULL STRING, THIS FIXES THE ISSUES
  681. # I WAS HAVING WITH THE NULL ARRAYS, STILL UGLY :P
  682. account => \@account_options,
  683. selected_account => $request->{account},
  684. datepaid => {
  685. name => 'datepaid',
  686. value => $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date}
  687. },
  688. source => \@sources_options,
  689. selected_source => $request->{source},
  690. source_value => $request->{source_value},
  691. defaultcurrency => {
  692. text => $default_currency
  693. },
  694. curr => { name => 'curr',
  695. value => $request->{curr},
  696. },
  697. column_headers => \@column_headers,
  698. rows => \@invoice_data,
  699. topay_state => \@topay_state,
  700. vendorcustomer => { name => 'vendor-customer',
  701. value => $request->{'vendor-customer'}
  702. },
  703. vc => { name => $Payment->{company_name}, # We will assume that the first Billing Information as default
  704. address => [ {text => $vc_options[0]->{'line_one'}},
  705. {text => $vc_options[0]->{'line_two'}},
  706. {text => $vc_options[0]->{'line_three'}},
  707. {text => $vc_options[0]->{city}},
  708. {text => $vc_options[0]->{state}},
  709. {text => $vc_options[0]->{country}}]
  710. },
  711. format => {
  712. name => 'FORMAT',
  713. options => \@format_options
  714. },
  715. media => {
  716. name => 'MEDIA',
  717. options => \@media_options
  718. },
  719. exrate => @currency_options,
  720. selectedcheckboxes => @selected_checkboxes ? \@selected_checkboxes : '',
  721. notes => $request->{notes},
  722. overpayment => \@overpayment,
  723. overpayment_account => \@overpayment_account
  724. };
  725. my $template = LedgerSMB::Template->new(
  726. user => $request->{_user},
  727. locale => $request->{_locale},
  728. path => 'UI/payments',
  729. template => 'payment2',
  730. format => 'HTML' );
  731. eval {$template->render($select) };
  732. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  733. }
  734. =pod
  735. =item post_payment
  736. This method is used for the payment module (not the bulk payment),
  737. and its used for all the mechanics of storing a payment.
  738. =back
  739. =cut
  740. sub post_payment {
  741. my ($request) = @_;
  742. my $locale = $request->{_locale};
  743. my $Payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  744. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  745. ($Payment->{entity_id}, $Payment->{company_name}) = split /--/ , $request->{'vendor-customer'};
  746. # LETS GET THE DEPARTMENT INFO
  747. # WE HAVE TO SET $dbPayment->{department_id} in order to process
  748. if ($request->{department}) {
  749. $request->{department} =~ /^(\d+)--*/;
  750. $Payment->{department_id} = $1;
  751. }
  752. #
  753. # We want to set a gl_description,
  754. # since we are using two tables there is no need to use doubled information,
  755. # we could specify this gl is the result of a payment movement...
  756. #
  757. $Payment->{gl_description} = $locale->text('This gl movement, is the result of a payment transaction');
  758. #
  759. # Im not sure what this is for... gotta comment this later
  760. $Payment->{approved} = 'true';
  761. #
  762. # We have to setup a lot of things before we can process the payment
  763. # they are related to payment_post sql function, so if you have any doubts
  764. # look there.
  765. #-------------------------------------------------------------------------
  766. #
  767. # Variable definition
  768. #
  769. # We use the prefix op to refer to the overpayment variables.
  770. my $overpayment; # This variable might be fuzzy, we are using it to handle invalid data
  771. # i.e. a user set an overpayment qty inside an invoice.
  772. my @array_options;
  773. my @amount;
  774. my @cash_account_id;
  775. my @source;
  776. my @transaction_id;
  777. my @op_amount;
  778. my @op_cash_account_id;
  779. my @op_source;
  780. my @op_memo;
  781. my @op_account_id;
  782. #
  783. # We need the invoices in order to process the income data, this is done this way
  784. # since the data we have isn't indexed in any way.
  785. #
  786. @array_options = $Payment->get_open_invoices();
  787. for my $ref (0 .. $#array_options) {
  788. if ( !$request->{"checkbox_$array_options[$ref]->{invoice_id}"}) {
  789. #
  790. # The prefix cash is to set the movements of the cash accounts,
  791. # same names are used for ap/ar accounts w/o the cash prefix.
  792. #
  793. # Maybe i should move this to another sub, so i can call it from payment2 as well :). D.M.
  794. if ($array_options[$ref]->{amount} < $request->{"topay_$array_options[$ref]->{invoice_id}"} ) {
  795. # THERE IS AN OVERPAYMENT!, we should store it and see if we can use it on the UI
  796. $overpayment = $overpayment + $request->{"topay_$array_options[$ref]->{invoice_id}"} - $array_options[$ref]->{amount};
  797. $request->{"topay_$array_options[$ref]->{invoice_id}"} = $request->{"topay_$array_options[$ref]->{invoice_id}"};
  798. }
  799. push @amount, $request->{"topay_fx_$array_options[$ref]->{invoice_id}"}; # We'll use this for both cash and ap/ar accounts
  800. push @cash_account_id, $request->{"optional_pay_$array_options[$ref]->{invoice_id}"} ? $request->{"account_$array_options[$ref]->{invoice_id}"} : $request->{account};
  801. push @source, $request->{"source1_$array_options[$ref]->{invoice_id}"}.' '.$request->{"source2_$array_options[$ref]->{invoice_id}"}; # We'll use this for both source and ap/ar accounts
  802. push @transaction_id, $array_options[$ref]->{invoice_id};
  803. }
  804. }
  805. #
  806. # Now we need the overpayment information.
  807. #
  808. # We will use the prefix op to indicate it is an overpayment information.
  809. #
  810. # note: I love the for's C-like syntax.
  811. for (my $i=1 ; $i <= $request->{overpayment_qty}; $i++) {
  812. if (!$request->{"overpayment_checkbox_$i"}) { # Is overpayment marked as deleted ?
  813. if ( $request->{"overpayment_topay_$i"} ) { # Is this overpayment an used field?
  814. # Now we split the account selected options, using the namespace the if statement
  815. # provides for us.
  816. $request->{"overpayment_account_$i"} =~ /^(\d+)--*/;
  817. my $id = $1;
  818. $request->{"overpayment_cash_account_$i"} =~ /^(\d+)--*/;
  819. my $cashid = $1;
  820. push @op_amount, $request->{"overpayment_topay_$i"};
  821. push @op_cash_account_id, $cashid;
  822. push @op_source, $request->{"overpayment_source1_$i"}.' '.$request->{"overpayment_source2_$i"};
  823. push @op_memo, $request->{"overpayment_memo_$i"};
  824. push @op_account_id, $id;
  825. }
  826. }
  827. }
  828. # Finally we store all the data inside the LedgerSMB::DBObject::Payment object.
  829. $Payment->{cash_account_id} = $Payment->_db_array_scalars(@cash_account_id);
  830. $Payment->{amount} = $Payment->_db_array_scalars(@amount);
  831. $Payment->{source} = $Payment->_db_array_scalars(@source);
  832. $Payment->{transaction_id} = $Payment->_db_array_scalars(@transaction_id);
  833. $Payment->{op_amount} = $Payment->_db_array_scalars(@op_amount);
  834. $Payment->{op_cash_account_id} = $Payment->_db_array_scalars(@op_cash_account_id);
  835. $Payment->{op_source} = $Payment->_db_array_scalars(@op_source);
  836. $Payment->{op_memo} = $Payment->_db_array_scalars(@op_memo);
  837. $Payment->{op_account_id} = $Payment->_db_array_scalars(@op_account_id);
  838. # Ok, hoping for the best...
  839. $Payment->post_payment();
  840. # We've gotta print anything, in the near future this will redirect to a new payment.
  841. my $select = {};
  842. my $template = LedgerSMB::Template->new(
  843. user => $request->{_user},
  844. locale => $request->{_locale},
  845. path => 'UI/payments',
  846. template => 'payment2',
  847. format => 'HTML' );
  848. eval {$template->render($select) };
  849. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  850. }
  851. eval { do "scripts/custom/payment.pl"};
  852. 1;