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