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