summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: f11a06c7c2e18437fef3cc3330ca4f3d8474b1d4 (plain)
  1. =pod
  2. =head1 NAME
  3. LedgerSMB::Scripts::payment - LedgerSMB class defining the Controller functions for payment handling.
  4. =head1 SYNOPSIS
  5. Defines the controller functions and workflow logic for payment processing.
  6. =head1 COPYRIGHT
  7. Copyright (c) 2007, David Mora R and Christian Ceballos B.
  8. Licensed to the public under the terms of the GNU GPL version 2 or later.
  9. Original copyright notice below.
  10. #=====================================================================
  11. # PLAXIS
  12. # Copyright (c) 2007
  13. #
  14. # Author: David Mora R
  15. # Christian Ceballos B
  16. #
  17. #
  18. #
  19. #
  20. #
  21. # This program is free software; you can redistribute it and/or modify
  22. # it under the terms of the GNU General Public License as published by
  23. # the Free Software Foundation; either version 2 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software
  32. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. =head1 METHODS
  34. =cut
  35. package LedgerSMB::Scripts::payment;
  36. use LedgerSMB::Template;
  37. use LedgerSMB::DBObject::Payment;
  38. use LedgerSMB::DBObject::Date;
  39. use strict;
  40. # CT: A few notes for future refactoring of this code:
  41. # 1: I don't think it is a good idea to make the UI too dependant on internal
  42. # code structures but I don't see a good alternative at the moment.
  43. # 2: CamelCasing: -1
  44. =pod
  45. =item payment
  46. This method is used to set the filter screen and prints it, using the
  47. TT2 system. (hopefully it will... )
  48. =back
  49. =cut
  50. sub payments {
  51. my ($request) = @_;
  52. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  53. $payment->get_metadata();
  54. my $template = LedgerSMB::Template->new(
  55. user => $request->{_user},
  56. locale => $request->{_locale},
  57. path => 'UI/payments',
  58. template => 'payments_filter',
  59. format => 'HTML',
  60. );
  61. $template->render($payment);
  62. }
  63. sub post_payments_bulk {
  64. my ($request) = @_;
  65. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  66. $payment->post_bulk();
  67. my $template = LedgerSMB::Template->new(
  68. user => $request->{_user},
  69. locale => $request->{_locale},
  70. path => 'UI/payments',
  71. template => 'payments_filter',
  72. format => 'HTML',
  73. );
  74. $template->render($payment);
  75. }
  76. sub display_payments {
  77. my ($request) = @_;
  78. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  79. $payment->get_payment_detail_data();
  80. $payment->debug({file => '/tmp/delme'});
  81. for (@{$payment->{contact_invoices}}){
  82. $_->{total_due} = $payment->format_amount(amount => $_->{total_due});
  83. }
  84. my $template = LedgerSMB::Template->new(
  85. user => $request->{_user},
  86. locale => $request->{_locale},
  87. path => 'UI/payments',
  88. template => 'payments_detail',
  89. format => 'HTML',
  90. );
  91. $template->render($payment);
  92. }
  93. sub payment {
  94. my ($request) = @_;
  95. my $locale = $request->{_locale};
  96. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  97. # Lets get the project data...
  98. my @projectOptions;
  99. my @arrayOptions = $dbPayment->list_open_projects();
  100. push @projectOptions, {}; #A blank field on the select box
  101. for my $ref (0 .. $#arrayOptions) {
  102. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  103. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  104. }
  105. # Lets get the departments data...
  106. my @departmentOptions;
  107. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  108. @arrayOptions = $dbPayment->list_departments($role);
  109. push @departmentOptions, {}; # A blank field on the select box
  110. for my $ref (0 .. $#arrayOptions) {
  111. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  112. text => $arrayOptions[$ref]->{description}};
  113. }
  114. # Lets get the customer or vendor :)
  115. my @vcOptions;
  116. @arrayOptions = $dbPayment->get_open_accounts();
  117. for my $ref (0 .. $#arrayOptions) {
  118. push @vcOptions, { value => $arrayOptions[$ref]->{id}.'--'.$arrayOptions[$ref]->{name},
  119. text => $arrayOptions[$ref]->{name}};
  120. }
  121. # Lets get the open currencies (this uses the $dbPayment->{account_class} property)
  122. my @currOptions;
  123. @arrayOptions = $dbPayment->get_open_currencies();
  124. for my $ref (0 .. $#arrayOptions) {
  125. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  126. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  127. }
  128. # Lets build filter by period
  129. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  130. $date->build_filter_by_period($locale);
  131. # Lets set the data in a hash for the template system. :)
  132. my $select = {
  133. stylesheet => $request->{_user}->{stylesheet},
  134. login => { name => 'login',
  135. value => $request->{_user}->{login} },
  136. projects => {
  137. name => 'projects',
  138. options => \@projectOptions
  139. },
  140. department => {
  141. name => 'department',
  142. options => \@departmentOptions
  143. },
  144. vendor_customer => {
  145. name => 'vendor-customer',
  146. options => \@vcOptions
  147. },
  148. curr => {
  149. name => 'curr',
  150. options => \@currOptions
  151. },
  152. month => {
  153. name => 'month',
  154. options => $date->{monthsOptions}
  155. },
  156. year => {
  157. name => 'year',
  158. options => $date->{yearsOptions}
  159. },
  160. interval_radios => $date->{radioOptions},
  161. amountfrom => {
  162. name => 'amountfrom',
  163. },
  164. amountto => {
  165. name => 'amountto',
  166. },
  167. accountclass => {
  168. name => 'account_class',
  169. value => $dbPayment->{account_class}
  170. },
  171. type => {
  172. name => 'type',
  173. value => $request->{type}
  174. },
  175. action => {
  176. name => 'action',
  177. value => 'payment2',
  178. text => $locale->text("Continue"),
  179. },
  180. };
  181. # Lets call upon the template system
  182. my $template;
  183. $template = LedgerSMB::Template->new(
  184. user => $request->{_user},
  185. locale => $request->{_locale},
  186. path => 'UI/payments',
  187. # path => 'UI',
  188. template => 'payment1',
  189. format => 'HTML', );
  190. $template->render($select);# And finally, Lets print the screen :)
  191. }
  192. =pod
  193. =item payment2
  194. This method is used for the payment module, it is a consecuence of the payment sub,
  195. and its used for all the mechanics of an invoices payment module.
  196. =back
  197. =cut
  198. sub payment2 {
  199. my ($request) = @_;
  200. my $locale = $request->{_locale};
  201. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  202. my @array_options;
  203. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  204. ($dbPayment->{entity_id}, my $vendor_customer_name) = split /--/ , $request->{'vendor-customer'};
  205. my @array_options;
  206. my $exchangerate;
  207. # LETS BUILD THE PROJECTS INFO
  208. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  209. my ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  210. my @project = { name => 'project', text => $project_number.' '.$project_name, value => $project_id };
  211. # LETS GET THE DEPARTMENT INFO
  212. my ($department_id, $department_name) = split /--/, $request->{department};
  213. my @department = { name => 'department', text => $department_name, value => $department_id };
  214. # LETS GET ALL THE ACCOUNTS
  215. my @account_options;
  216. @array_options = $dbPayment->list_accounting();
  217. for my $ref (0 .. $#array_options) {
  218. push @account_options, { value => $array_options[$ref]->{id},
  219. text => $array_options[$ref]->{description}};
  220. }
  221. # LETS GET THE POSSIBLE SOURCES
  222. my @sources_options;
  223. @array_options = $dbPayment->get_sources(\%$locale);
  224. for my $ref (0 .. $#array_options) {
  225. push @sources_options, { value => $array_options[$ref],
  226. text => $array_options[$ref]};
  227. }
  228. # WE MUST PREPARE THE ENTITY INFORMATION
  229. @array_options = $dbPayment->get_vc_info();
  230. # LETS BUILD THE CURRENCIES INFORMATION
  231. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  232. my $default_currency = $dbPayment->get_default_currency();
  233. my @currency_options;
  234. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  235. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  236. # -----------------------------------------------
  237. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  238. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  239. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  240. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  241. my @columnAS = ({text => $locale->text('Invoice')},
  242. {text => $locale->text('Date')},
  243. {text => $locale->text('Total').$default_currency_text},
  244. {text => $locale->text('Paid').$default_currency_text},
  245. {text => $locale->text('Amount Due').$default_currency_text},
  246. {text => $locale->text('To pay').$default_currency_text}
  247. );
  248. my @column_headers = ({text => $locale->text('Invoice')},
  249. {text => $locale->text('Date')},
  250. {text => $locale->text('Total').$default_currency_text},
  251. {text => $locale->text('Paid').$default_currency_text},
  252. {text => $locale->text('Amount Due').$default_currency_text},
  253. {text => $locale->text('To pay').$default_currency_text}
  254. );
  255. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  256. if ($default_currency ne $request->{curr} ) {
  257. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  258. push @column_headers, {text => $locale->text('Exchange Rate')},
  259. {text => $locale->text('Amount Due').$currency_text},
  260. {text => $locale->text('To pay').$currency_text};
  261. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  262. @column_headers[5,6,7] = @column_headers[6,7,5];
  263. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  264. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  265. $exchangerate = $dbPayment->get_exchange_rate($request->{curr}, $dbPayment->{current_date});
  266. if ($exchangerate) {
  267. @currency_options = {
  268. name => 'date_curr',
  269. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  270. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  271. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  272. };
  273. } else {
  274. @currency_options = {
  275. name => 'date_curr'};
  276. }
  277. } else {
  278. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  279. # ARE USING THE DEFAULT CURRENCY
  280. $exchangerate = 1;
  281. }
  282. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  283. # IF WE DONT HAVE ANY INVOICES MATCHING THE FILTER PARAMETERS, WE WILL WARN THE USER AND STOP
  284. # THE PROCCESS.
  285. my @invoice_data;
  286. @array_options = $dbPayment->get_open_invoices();
  287. if (!$array_options[0]->{invoice_id}) {
  288. $request->error($locale->text("Nothing to do"));
  289. }
  290. for my $ref (0 .. $#array_options) {
  291. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  292. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  293. },
  294. invoice_date => "$array_options[$ref]->{invoice_date}",
  295. amount => "$array_options[$ref]->{amount}",
  296. due => "$array_options[$ref]->{due}",
  297. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  298. exchange_rate => "$exchangerate",
  299. due_fx => "$exchangerate"? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A',
  300. topay => "$array_options[$ref]->{due}",
  301. topay_fx => { name => "topay_fx_$ref",
  302. value => "$exchangerate" ? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A'
  303. }
  304. };
  305. }
  306. # LETS BUILD THE SELECTION FOR THE UI
  307. my $select = {
  308. stylesheet => $request->{_user}->{stylesheet},
  309. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  310. project => @project,
  311. department => @department,
  312. account => {
  313. name => 'account',
  314. options => \@account_options},
  315. datepaid => {
  316. name => 'datepaid',
  317. value => $dbPayment->{current_date}
  318. },
  319. source => {
  320. name => 'source',
  321. options => \@sources_options
  322. },
  323. source_text => {
  324. name => 'source_text',
  325. },
  326. defaultcurrency => {
  327. text => $default_currency
  328. },
  329. curr => {
  330. text => $request->{curr}
  331. },
  332. column_headers => \@column_headers,
  333. rows => \@invoice_data,
  334. vc => { name => $vendor_customer_name,
  335. address => [ {text => 'Crra 83 #32 -1'},
  336. {text => '442 6464'},
  337. {text => 'Medellín'},
  338. {text => 'Colombia'}]},
  339. post => {
  340. accesskey => 'O',
  341. title => 'POST ALT+O',
  342. name => 'action',
  343. value => 'post',
  344. text => "POST"
  345. },
  346. post_and_print => {
  347. accesskey => 'R',
  348. title => 'POST AND PRINT ALT+R',
  349. name => 'action',
  350. value => 'post_and_print',
  351. text => "POST AND PRINT"
  352. },
  353. format => {
  354. name => 'FORMAT',
  355. options => [
  356. {value => 1, text => "HTML" },
  357. {value => 2, text => "PDF" },
  358. {value => 3, text => "POSTSCRIPT" }
  359. ],
  360. },
  361. media => {
  362. name => 'MEDIA',
  363. options => [
  364. {value => 1, text => "Screen" },
  365. {value => 2, text => "PRINTER" },
  366. {value => 3, text => "EMAIL" }
  367. ],
  368. },
  369. date_curr => @currency_options # I HAVE TO PUT THIS LAST, BECAUSE IT CAN BE NULL
  370. # THIS IS AN UGLY HACK THAT MUST BE FIXED.
  371. };
  372. my $template = LedgerSMB::Template->new(
  373. user => $request->{_user},
  374. locale => $request->{_locale},
  375. # path => 'UI/payments',
  376. path => 'UI',
  377. template => 'payment2',
  378. format => 'HTML' );
  379. eval {$template->render($select) };
  380. if ($@) { $request->error("$@"); }
  381. }
  382. eval { do "scripts/custom/payment.pl"};
  383. 1;