summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: d0896dc9df62d0acbbb9551ca2cf9974f88e47ee (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. $payment->debug({file => '/tmp/payment'});
  274. $template = LedgerSMB::Template->new(
  275. user => $payment->{_user}, template => 'check_multiple',
  276. format => uc $payment->{'format'},
  277. no_auto_output => 1,
  278. output_args => $payment,
  279. );
  280. try {
  281. $template->render($payment);
  282. $template->output(%$payment);
  283. }
  284. catch Error::Simple with {
  285. my $E = shift;
  286. $payment->error( $E->stacktrace );
  287. };
  288. } else {
  289. }
  290. }
  291. sub display_payments {
  292. my ($request) = @_;
  293. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  294. $payment->get_payment_detail_data();
  295. for (@{$payment->{contact_invoices}}){
  296. $_->{total_due} = $payment->format_amount(amount => $_->{total_due});
  297. }
  298. @{$payment->{media_options}} = (
  299. {text => $request->{_locale}->text('Screen'),
  300. value => 'screen'});
  301. for (keys %LedgerSMB::Sysconfig::printer){
  302. push @{$payment->{media_options}},
  303. {text => $_,
  304. value => $LedgerSMB::Sysconfig::printer{$_}};
  305. }
  306. if ($LedgerSMB::Sysconfig::latex){
  307. @{$payment->{format_options}} = (
  308. {text => 'PDF', value => 'PDF'},
  309. {text => 'Postscript', value => 'Postscript'},
  310. );
  311. $payment->{can_print} = 1;
  312. }
  313. my $template = LedgerSMB::Template->new(
  314. user => $request->{_user},
  315. locale => $request->{_locale},
  316. path => 'UI/payments',
  317. template => 'payments_detail',
  318. format => 'HTML',
  319. );
  320. $template->render($payment);
  321. }
  322. =item payment
  323. This method is used to set the filter screen and prints it, using the
  324. TT2 system.
  325. =back
  326. =cut
  327. sub payment {
  328. my ($request) = @_;
  329. my $locale = $request->{_locale};
  330. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  331. # Lets get the project data...
  332. my @projectOptions;
  333. my @arrayOptions = $dbPayment->list_open_projects();
  334. push @projectOptions, {}; #A blank field on the select box
  335. for my $ref (0 .. $#arrayOptions) {
  336. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  337. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  338. }
  339. # Lets get the departments data...
  340. my @departmentOptions;
  341. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  342. @arrayOptions = $dbPayment->list_departments($role);
  343. push @departmentOptions, {}; # A blank field on the select box
  344. for my $ref (0 .. $#arrayOptions) {
  345. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  346. text => $arrayOptions[$ref]->{description}};
  347. }
  348. # Lets get the currencies (this uses the $dbPayment->{account_class} property)
  349. my @currOptions;
  350. @arrayOptions = $dbPayment->get_open_currencies();
  351. for my $ref (0 .. $#arrayOptions) {
  352. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  353. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  354. }
  355. # Lets build filter by period
  356. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  357. $date->build_filter_by_period($locale);
  358. # Lets set the data in a hash for the template system. :)
  359. my $select = {
  360. stylesheet => $request->{_user}->{stylesheet},
  361. login => { name => 'login',
  362. value => $request->{_user}->{login} },
  363. projects => {
  364. name => 'projects',
  365. options => \@projectOptions
  366. },
  367. department => {
  368. name => 'department',
  369. options => \@departmentOptions
  370. },
  371. curr => {
  372. name => 'curr',
  373. options => \@currOptions
  374. },
  375. month => {
  376. name => 'month',
  377. options => $date->{monthsOptions}
  378. },
  379. year => {
  380. name => 'year',
  381. options => $date->{yearsOptions}
  382. },
  383. interval_radios => $date->{radioOptions},
  384. amountfrom => {
  385. name => 'amountfrom',
  386. },
  387. amountto => {
  388. name => 'amountto',
  389. },
  390. accountclass => {
  391. name => 'account_class',
  392. value => $dbPayment->{account_class}
  393. },
  394. type => {
  395. name => 'type',
  396. value => $request->{type}
  397. },
  398. action => {
  399. name => 'action',
  400. value => 'payment1_5',
  401. text => $locale->text("Continue"),
  402. }
  403. };
  404. my $template;
  405. $template = LedgerSMB::Template->new(
  406. user => $request->{_user},
  407. locale => $request->{_locale},
  408. path => 'UI/payments',
  409. template => 'payment1',
  410. format => 'HTML' );
  411. $template->render($select);# And finally, Lets print the screen :)
  412. }
  413. =pod
  414. =item payment1_5
  415. This method is called between payment and payment2, it will search the database
  416. for entity_credit_accounts that match the parameter, if only one is found it will
  417. run unnoticed by the user, if more than one is found it will ask the user to pick
  418. one to handle the payment against
  419. =back
  420. =cut
  421. sub payment1_5 {
  422. my ($request) = @_;
  423. my $locale = $request->{_locale};
  424. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  425. my @array_options = $dbPayment->get_entity_credit_account();
  426. if ($#array_options == -1) {
  427. &payment($request);
  428. } elsif ($#array_options == 0) {
  429. $request->{'vendor-customer'} = $array_options[0]->{id}.'--'.$array_options[0]->{name};
  430. &payment2($request);
  431. } else {
  432. # Lets call upon the template system
  433. my @company_options;
  434. for my $ref (0 .. $#array_options) {
  435. push @company_options, { id => $array_options[$ref]->{id},
  436. name => $array_options[$ref]->{name}};
  437. }
  438. my $select = {
  439. companies => \@company_options,
  440. stylesheet => $request->{_user}->{stylesheet},
  441. login => { name => 'login',
  442. value => $request->{_user}->{login}},
  443. department => { name => 'department',
  444. value => $request->{department}},
  445. currency => { name => 'curr',
  446. value => $request->{curr}},
  447. datefrom => { name => 'datefrom',
  448. value => $request->{datefrom}},
  449. dateto => { name => 'dateto',
  450. value => $request->{dateto}},
  451. amountfrom => { name => 'amountfrom',
  452. value => $request->{datefrom}},
  453. amountto => { name => 'amountto',
  454. value => $request->{dateto}},
  455. accountclass => { name => 'account_class',
  456. value => $dbPayment->{account_class}},
  457. type => { name => 'type',
  458. value => $request->{type}},
  459. action => { name => 'action',
  460. value => 'payment2',
  461. text => $locale->text("Continue")}
  462. };
  463. my $template;
  464. $template = LedgerSMB::Template->new(
  465. user => $request->{_user},
  466. locale => $request->{_locale},
  467. path => 'UI/payments',
  468. template => 'payment1_5',
  469. format => 'HTML' );
  470. eval {$template->render($select) };
  471. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  472. }
  473. }
  474. =pod
  475. =item payment2
  476. This method is used for the payment module, it is a consecuence of the payment sub,
  477. and its used for all the mechanics of an invoices payment module.
  478. =back
  479. =cut
  480. sub payment2 {
  481. my ($request) = @_;
  482. my $locale = $request->{_locale};
  483. my $Payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  484. # VARIABLES
  485. my ($project_id, $project_number, $project_name, $department_id, $department_name );
  486. my @array_options;
  487. my @project;
  488. my @selected_checkboxes;
  489. my @department;
  490. my @array_options;
  491. my @currency_options;
  492. my $exchangerate;
  493. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  494. ($Payment->{entity_credit_id}, $Payment->{company_name}) = split /--/ , $request->{'vendor-customer'};
  495. # WE NEED TO RETRIEVE A BILLING LOCATION, THIS IS HARDCODED FOR NOW... Should we change it?
  496. $Payment->{location_class_id} = '1';
  497. my @vc_options;
  498. @vc_options = $Payment->get_vc_info();
  499. # LETS BUILD THE PROJECTS INFO
  500. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  501. if ($request->{projects}) {
  502. ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  503. @project = { name => 'projects', text => $project_number.' '.$project_name, value => $request->{projects}};
  504. }
  505. # LETS GET THE DEPARTMENT INFO
  506. # WE HAVE TO SET $dbPayment->{department_id} NOW, THIS DATA WILL BE USED LATER WHEN WE
  507. # CALL FOR payment_get_open_invoices. :)
  508. if ($request->{department}) {
  509. ($Payment->{department_id}, $department_name) = split /--/, $request->{department};
  510. @department = { name => 'department', text => $department_name, value => $request->{department}};
  511. }
  512. # LETS GET ALL THE ACCOUNTS
  513. my @account_options = $Payment->list_accounting();
  514. # LETS GET THE POSSIBLE SOURCES
  515. my @sources_options = $Payment->get_sources(\%$locale);
  516. # WE MUST PREPARE THE ENTITY INFORMATION
  517. @array_options = $Payment->get_vc_info();
  518. # LETS BUILD THE CURRENCIES INFORMATION
  519. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  520. my $default_currency = $Payment->get_default_currency();
  521. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  522. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  523. # -----------------------------------------------
  524. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  525. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  526. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  527. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  528. my @column_headers = ({text => $locale->text('Invoice')},
  529. {text => $locale->text('Date')},
  530. {text => $locale->text('Total').$default_currency_text},
  531. {text => $locale->text('Paid').$default_currency_text},
  532. {text => $locale->text('Discount').$default_currency_text},
  533. {text => $locale->text('Amount Due').$default_currency_text},
  534. {text => $locale->text('To pay').$default_currency_text}
  535. );
  536. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  537. if ($default_currency ne $request->{curr} ) {
  538. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  539. push @column_headers, {text => $locale->text('Exchange Rate')},
  540. {text => $locale->text('Amount Due').$currency_text},
  541. {text => $locale->text('To pay').$currency_text};
  542. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  543. @column_headers[6,7,8] = @column_headers[7,8,6];
  544. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  545. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  546. $exchangerate = $request->{exrate} ?
  547. $request->{exrate} :
  548. $Payment->get_exchange_rate($request->{curr},
  549. $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date});
  550. if ($exchangerate) {
  551. @currency_options = {
  552. name => 'exrate',
  553. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  554. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  555. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  556. };
  557. } else {
  558. @currency_options = {
  559. name => 'exrate'};
  560. }
  561. } else {
  562. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  563. # ARE USING THE DEFAULT CURRENCY
  564. $exchangerate = 1;
  565. @currency_options = {
  566. name => 'exrate',
  567. value => 1,
  568. text => 1
  569. };
  570. }
  571. # FINALLY WE ADD TO THE COLUMN HEADERS A LAST FIELD TO PRINT THE CLOSE INVOICE CHECKBOX TRICK :)
  572. push @column_headers, {text => 'X'};
  573. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  574. # WE WONT DO ANYTHING IF WE DONT FIND ANY INVOICES, THE USER CAN STILL POST A PREPAYMENT
  575. my @invoice_data;
  576. my @topay_state; # WE WILL USE THIS TO HELP UI TO DETERMINE WHAT IS VISIBLE
  577. @array_options = $Payment->get_open_invoices();
  578. for my $ref (0 .. $#array_options) {
  579. if ( !$request->{"checkbox_$array_options[$ref]->{invoice_id}"}) {
  580. # SHOULD I APPLY DISCCOUNTS?
  581. # LETS SET THE EXCHANGERATE VALUES
  582. my $due_fx; my $topay_fx_value;
  583. if ("$exchangerate") {
  584. $topay_fx_value = $due_fx = "$array_options[$ref]->{due}"/"$exchangerate" - "$array_options[$ref]->{discount}"/"$exchangerate";
  585. } else {
  586. $topay_fx_value = $due_fx = "N/A";
  587. }
  588. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  589. id => $array_options[$ref]->{invoice_id},
  590. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  591. },
  592. invoice_date => "$array_options[$ref]->{invoice_date}",
  593. amount => "$array_options[$ref]->{amount}",
  594. due => "$array_options[$ref]->{due}" - "$array_options[$ref]->{discount}",
  595. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  596. discount => "$array_options[$ref]->{discount}",
  597. exchange_rate => "$exchangerate",
  598. due_fx => $due_fx, # This was set at the begining of the for statement
  599. topay => "$array_options[$ref]->{due}" - "$array_options[$ref]->{discount}",
  600. source_text => $request->{"source_text_$array_options[$ref]->{invoice_id}"},
  601. optional => $request->{"optional_pay_$array_options[$ref]->{invoice_id}"},
  602. selected_account => $request->{"account_$array_options[$ref]->{invoice_id}"},
  603. selected_source => $request->{"source_$array_options[$ref]->{invoice_id}"},
  604. topay_fx => { name => "topay_fx_$array_options[$ref]->{invoice_id}",
  605. value => $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} ?
  606. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} eq 'N/A' ?
  607. $topay_fx_value :
  608. $request->{"topay_fx_$array_options[$ref]->{invoice_id}"} :
  609. $topay_fx_value
  610. # Ugly hack, but works ;) ...
  611. }#END HASH
  612. };# END PUSH
  613. push @topay_state, {
  614. id => "topaystate_$array_options[$ref]->{invoice_id}",
  615. value => $request->{"topaystate_$array_options[$ref]->{invoice_id}"}
  616. }; #END PUSH
  617. }
  618. else {
  619. push @selected_checkboxes, {name => "checkbox_$array_options[$ref]->{invoice_id}",
  620. value => "checked"} ;
  621. } #END IF
  622. }# END FOR
  623. # And finally, we are going to store the information for the overpayment / prepayment / advanced payment
  624. # and all the stuff, this is only needed for the update function.
  625. my @overpayment;
  626. my @overpayment_account;
  627. # Got to build the account selection box first.
  628. my @overpayment_account = $Payment->list_overpayment_accounting();
  629. # Now we build the structure for the UI
  630. for (my $i=1 ; $i <= $request->{overpayment_qty}; $i++) {
  631. if (!$request->{"overpayment_checkbox_$i"}) {
  632. if ( $request->{"overpayment_topay_$i"} ) {
  633. # Now we split the account selected options
  634. my ($id, $accno, $description) = split(/--/, $request->{"overpayment_account_$i"});
  635. my ($cashid, $cashaccno, $cashdescription ) = split(/--/, $request->{"overpayment_cash_account_$i"});
  636. push @overpayment, {amount => $request->{"overpayment_topay_$i"},
  637. source1 => $request->{"overpayment_source1_$i"},
  638. source2 => $request->{"overpayment_source2_$i"},
  639. memo => $request->{"overpayment_memo_$i"},
  640. account => { id => $id,
  641. accno => $accno,
  642. description => $description
  643. },
  644. cashaccount => { id => $cashid,
  645. accno => $cashaccno,
  646. description => $cashdescription
  647. }
  648. };
  649. } else {
  650. $i = $request->{overpayment_qty} + 1;
  651. }
  652. }
  653. }
  654. # We need to set the availible media and format from printing
  655. my @media_options;
  656. push @media_options, {value => 1, text => "Screen"};
  657. if ($#{LedgerSMB::Sysconfig::printer}) {
  658. for (keys %{LedgerSMB::Sysconfig::printer}) {
  659. push @media_options, {value => 1, text => $_};
  660. }
  661. }
  662. #$request->error("@media_options");
  663. my @format_options;
  664. push @format_options, {value => 1, text => "HTML"};
  665. if (${LedgerSMB::Sysconfig::latex}) {
  666. push @format_options, {value => 2, text => "PDF" }, {value => 3, text => "POSTSCRIPT" };
  667. }
  668. # LETS BUILD THE SELECTION FOR THE UI
  669. my $select = {
  670. stylesheet => $request->{_user}->{stylesheet},
  671. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  672. type => { name => 'type',
  673. value => $request->{type} },
  674. login => { name => 'login',
  675. value => $request->{_user}->{login} },
  676. accountclass => {
  677. name => 'account_class',
  678. value => $Payment->{account_class}
  679. },
  680. project => @project ? @project : '' , # WE NEED TO VERIFY THAT THE ARRAY EXISTS, IF IT DOESNT,
  681. department => @department ? @department : '', # WE WILL PASS A NULL STRING, THIS FIXES THE ISSUES
  682. # I WAS HAVING WITH THE NULL ARRAYS, STILL UGLY :P
  683. account => \@account_options,
  684. selected_account => $request->{account},
  685. datepaid => {
  686. name => 'datepaid',
  687. value => $request->{datepaid} ? $request->{datepaid} : $Payment->{current_date}
  688. },
  689. source => \@sources_options,
  690. selected_source => $request->{source},
  691. source_value => $request->{source_value},
  692. defaultcurrency => {
  693. text => $default_currency
  694. },
  695. curr => { name => 'curr',
  696. value => $request->{curr},
  697. },
  698. column_headers => \@column_headers,
  699. rows => \@invoice_data,
  700. topay_state => \@topay_state,
  701. vendorcustomer => { name => 'vendor-customer',
  702. value => $request->{'vendor-customer'}
  703. },
  704. vc => { name => $Payment->{company_name}, # We will assume that the first Billing Information as default
  705. address => [ {text => $vc_options[0]->{'line_one'}},
  706. {text => $vc_options[0]->{'line_two'}},
  707. {text => $vc_options[0]->{'line_three'}},
  708. {text => $vc_options[0]->{city}},
  709. {text => $vc_options[0]->{state}},
  710. {text => $vc_options[0]->{country}}]
  711. },
  712. format => {
  713. name => 'FORMAT',
  714. options => \@format_options
  715. },
  716. media => {
  717. name => 'MEDIA',
  718. options => \@media_options
  719. },
  720. exrate => @currency_options,
  721. selectedcheckboxes => @selected_checkboxes ? \@selected_checkboxes : '',
  722. notes => $request->{notes},
  723. overpayment => \@overpayment,
  724. overpayment_account => \@overpayment_account
  725. };
  726. my $template = LedgerSMB::Template->new(
  727. user => $request->{_user},
  728. locale => $request->{_locale},
  729. path => 'UI/payments',
  730. template => 'payment2',
  731. format => 'HTML' );
  732. eval {$template->render($select) };
  733. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  734. }
  735. =pod
  736. =item post_payment
  737. This method is used for the payment module (not the bulk payment),
  738. and its used for all the mechanics of storing a payment.
  739. =back
  740. =cut
  741. sub post_payment {
  742. my ($request) = @_;
  743. my $locale = $request->{_locale};
  744. my $Payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  745. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  746. ($Payment->{entity_id}, $Payment->{company_name}) = split /--/ , $request->{'vendor-customer'};
  747. # LETS GET THE DEPARTMENT INFO
  748. # WE HAVE TO SET $dbPayment->{department_id} in order to process
  749. if ($request->{department}) {
  750. $request->{department} =~ /^(\d+)--*/;
  751. $Payment->{department_id} = $1;
  752. }
  753. #
  754. # We want to set a gl_description,
  755. # since we are using two tables there is no need to use doubled information,
  756. # we could specify this gl is the result of a payment movement...
  757. #
  758. $Payment->{gl_description} = $locale->text('This gl movement, is the result of a payment transaction');
  759. #
  760. # Im not sure what this is for... gotta comment this later
  761. $Payment->{approved} = 'true';
  762. #
  763. # We have to setup a lot of things before we can process the payment
  764. # they are related to payment_post sql function, so if you have any doubts
  765. # look there.
  766. #-------------------------------------------------------------------------
  767. #
  768. # Variable definition
  769. #
  770. # We use the prefix op to refer to the overpayment variables.
  771. my $overpayment; # This variable might be fuzzy, we are using it to handle invalid data
  772. # i.e. a user set an overpayment qty inside an invoice.
  773. my @array_options;
  774. my @amount;
  775. my @cash_account_id;
  776. my @source;
  777. my @transaction_id;
  778. my @op_amount;
  779. my @op_cash_account_id;
  780. my @op_source;
  781. my @op_memo;
  782. my @op_account_id;
  783. #
  784. # We need the invoices in order to process the income data, this is done this way
  785. # since the data we have isn't indexed in any way.
  786. #
  787. @array_options = $Payment->get_open_invoices();
  788. for my $ref (0 .. $#array_options) {
  789. if ( !$request->{"checkbox_$array_options[$ref]->{invoice_id}"}) {
  790. #
  791. # The prefix cash is to set the movements of the cash accounts,
  792. # same names are used for ap/ar accounts w/o the cash prefix.
  793. #
  794. # Maybe i should move this to another sub, so i can call it from payment2 as well :). D.M.
  795. if ($array_options[$ref]->{amount} < $request->{"topay_$array_options[$ref]->{invoice_id}"} ) {
  796. # THERE IS AN OVERPAYMENT!, we should store it and see if we can use it on the UI
  797. $overpayment = $overpayment + $request->{"topay_$array_options[$ref]->{invoice_id}"} - $array_options[$ref]->{amount};
  798. $request->{"topay_$array_options[$ref]->{invoice_id}"} = $request->{"topay_$array_options[$ref]->{invoice_id}"};
  799. }
  800. push @amount, $request->{"topay_fx_$array_options[$ref]->{invoice_id}"}; # We'll use this for both cash and ap/ar accounts
  801. push @cash_account_id, $request->{"optional_pay_$array_options[$ref]->{invoice_id}"} ? $request->{"account_$array_options[$ref]->{invoice_id}"} : $request->{account};
  802. 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
  803. push @transaction_id, $array_options[$ref]->{invoice_id};
  804. }
  805. }
  806. #
  807. # Now we need the overpayment information.
  808. #
  809. # We will use the prefix op to indicate it is an overpayment information.
  810. #
  811. # note: I love the for's C-like syntax.
  812. for (my $i=1 ; $i <= $request->{overpayment_qty}; $i++) {
  813. if (!$request->{"overpayment_checkbox_$i"}) { # Is overpayment marked as deleted ?
  814. if ( $request->{"overpayment_topay_$i"} ) { # Is this overpayment an used field?
  815. # Now we split the account selected options, using the namespace the if statement
  816. # provides for us.
  817. $request->{"overpayment_account_$i"} =~ /^(\d+)--*/;
  818. my $id = $1;
  819. $request->{"overpayment_cash_account_$i"} =~ /^(\d+)--*/;
  820. my $cashid = $1;
  821. push @op_amount, $request->{"overpayment_topay_$i"};
  822. push @op_cash_account_id, $cashid;
  823. push @op_source, $request->{"overpayment_source1_$i"}.' '.$request->{"overpayment_source2_$i"};
  824. push @op_memo, $request->{"overpayment_memo_$i"};
  825. push @op_account_id, $id;
  826. }
  827. }
  828. }
  829. # Finally we store all the data inside the LedgerSMB::DBObject::Payment object.
  830. $Payment->{cash_account_id} = $Payment->_db_array_scalars(@cash_account_id);
  831. $Payment->{amount} = $Payment->_db_array_scalars(@amount);
  832. $Payment->{source} = $Payment->_db_array_scalars(@source);
  833. $Payment->{transaction_id} = $Payment->_db_array_scalars(@transaction_id);
  834. $Payment->{op_amount} = $Payment->_db_array_scalars(@op_amount);
  835. $Payment->{op_cash_account_id} = $Payment->_db_array_scalars(@op_cash_account_id);
  836. $Payment->{op_source} = $Payment->_db_array_scalars(@op_source);
  837. $Payment->{op_memo} = $Payment->_db_array_scalars(@op_memo);
  838. $Payment->{op_account_id} = $Payment->_db_array_scalars(@op_account_id);
  839. # Ok, hoping for the best...
  840. $Payment->post_payment();
  841. # We've gotta print anything, in the near future this will redirect to a new payment.
  842. my $select = {};
  843. my $template = LedgerSMB::Template->new(
  844. user => $request->{_user},
  845. locale => $request->{_locale},
  846. path => 'UI/payments',
  847. template => 'payment2',
  848. format => 'HTML' );
  849. eval {$template->render($select) };
  850. if ($@) { $request->error("$@"); } # PRINT ERRORS ON THE UI
  851. }
  852. eval { do "scripts/custom/payment.pl"};
  853. 1;