summaryrefslogtreecommitdiff
path: root/scripts/payment.pl
blob: 551ebf90d55502635b7435e2d5de5b08b317fd75 (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 display_payments {
  64. my ($request) = @_;
  65. my $payment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  66. $payment->get_payment_detail_data();
  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 payment {
  77. my ($request) = @_;
  78. my $locale = $request->{_locale};
  79. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  80. # Lets get the project data...
  81. my @projectOptions;
  82. my @arrayOptions = $dbPayment->list_open_projects();
  83. push @projectOptions, {}; #A blank field on the select box
  84. for my $ref (0 .. $#arrayOptions) {
  85. push @projectOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description},
  86. text => $arrayOptions[$ref]->{projectnumber}."--".$arrayOptions[$ref]->{description}};
  87. }
  88. # Lets get the departments data...
  89. my @departmentOptions;
  90. my $role = $request->{type} eq 'receipt' ? 'P' : 'C';
  91. @arrayOptions = $dbPayment->list_departments($role);
  92. push @departmentOptions, {}; # A blank field on the select box
  93. for my $ref (0 .. $#arrayOptions) {
  94. push @departmentOptions, { value => $arrayOptions[$ref]->{id}."--".$arrayOptions[$ref]->{description},
  95. text => $arrayOptions[$ref]->{description}};
  96. }
  97. # Lets get the customer or vendor :)
  98. my @vcOptions;
  99. @arrayOptions = $dbPayment->get_open_accounts();
  100. for my $ref (0 .. $#arrayOptions) {
  101. push @vcOptions, { value => $arrayOptions[$ref]->{id}.'--'.$arrayOptions[$ref]->{name},
  102. text => $arrayOptions[$ref]->{name}};
  103. }
  104. # Lets get the open currencies (this uses the $dbPayment->{account_class} property)
  105. my @currOptions;
  106. @arrayOptions = $dbPayment->get_open_currencies();
  107. for my $ref (0 .. $#arrayOptions) {
  108. push @currOptions, { value => $arrayOptions[$ref]->{payments_get_open_currencies},
  109. text => $arrayOptions[$ref]->{payments_get_open_currencies} };
  110. }
  111. # Lets build filter by period
  112. my $date = LedgerSMB::DBObject::Date->new({base => $request});
  113. $date->build_filter_by_period($locale);
  114. # Lets set the data in a hash for the template system. :)
  115. my $select = {
  116. stylesheet => $request->{_user}->{stylesheet},
  117. login => { name => 'login',
  118. value => $request->{_user}->{login} },
  119. projects => {
  120. name => 'projects',
  121. options => \@projectOptions
  122. },
  123. department => {
  124. name => 'department',
  125. options => \@departmentOptions
  126. },
  127. vendor_customer => {
  128. name => 'vendor-customer',
  129. options => \@vcOptions
  130. },
  131. curr => {
  132. name => 'curr',
  133. options => \@currOptions
  134. },
  135. month => {
  136. name => 'month',
  137. options => $date->{monthsOptions}
  138. },
  139. year => {
  140. name => 'year',
  141. options => $date->{yearsOptions}
  142. },
  143. interval_radios => $date->{radioOptions},
  144. amountfrom => {
  145. name => 'amountfrom',
  146. },
  147. amountto => {
  148. name => 'amountto',
  149. },
  150. accountclass => {
  151. name => 'account_class',
  152. value => $dbPayment->{account_class}
  153. },
  154. type => {
  155. name => 'type',
  156. value => $request->{type}
  157. },
  158. action => {
  159. name => 'action',
  160. value => 'payment2',
  161. text => $locale->text("Continue"),
  162. },
  163. };
  164. # Lets call upon the template system
  165. my $template;
  166. $template = LedgerSMB::Template->new(
  167. user => $request->{_user},
  168. locale => $request->{_locale},
  169. path => 'UI/payments',
  170. # path => 'UI',
  171. template => 'payment1',
  172. format => 'HTML', );
  173. $template->render($select);# And finally, Lets print the screen :)
  174. }
  175. =pod
  176. =item payment2
  177. This method is used for the payment module, it is a consecuence of the payment sub,
  178. and its used for all the mechanics of an invoices payment module.
  179. =back
  180. =cut
  181. sub payment2 {
  182. my ($request) = @_;
  183. my $locale = $request->{_locale};
  184. my $dbPayment = LedgerSMB::DBObject::Payment->new({'base' => $request});
  185. my @array_options;
  186. # LETS GET THE CUSTOMER/VENDOR INFORMATION
  187. ($dbPayment->{entity_id}, my $vendor_customer_name) = split /--/ , $request->{'vendor-customer'};
  188. my @array_options;
  189. my $exchangerate;
  190. # LETS BUILD THE PROJECTS INFO
  191. # I DONT KNOW IF I NEED ALL THIS, BUT AS IT IS AVAILABLE I'LL STORE IT FOR LATER USAGE.
  192. my ($project_id, $project_number, $project_name) = split /--/ , $request->{projects} ;
  193. my @project = { name => 'project', text => $project_number.' '.$project_name, value => $project_id };
  194. # LETS GET THE DEPARTMENT INFO
  195. my ($department_id, $department_name) = split /--/, $request->{department};
  196. my @department = { name => 'department', text => $department_name, value => $department_id };
  197. # LETS GET ALL THE ACCOUNTS
  198. my @account_options;
  199. @array_options = $dbPayment->list_accounting();
  200. for my $ref (0 .. $#array_options) {
  201. push @account_options, { value => $array_options[$ref]->{id},
  202. text => $array_options[$ref]->{description}};
  203. }
  204. # LETS GET THE POSSIBLE SOURCES
  205. my @sources_options;
  206. @array_options = $dbPayment->get_sources(\%$locale);
  207. for my $ref (0 .. $#array_options) {
  208. push @sources_options, { value => $array_options[$ref],
  209. text => $array_options[$ref]};
  210. }
  211. # WE MUST PREPARE THE ENTITY INFORMATION
  212. @array_options = $dbPayment->get_vc_info();
  213. # LETS BUILD THE CURRENCIES INFORMATION
  214. # FIRST, WE NEED TO KNOW THE DEFAULT CURRENCY
  215. my $default_currency = $dbPayment->get_default_currency();
  216. my @currency_options;
  217. # LETS BUILD THE COLUMN HEADERS WE ALWAYS NEED
  218. # THE OTHER HEADERS WILL BE BUILT IF THE RIGHT CONDITIONS ARE MET.
  219. # -----------------------------------------------
  220. # SOME USERS WONT USE MULTIPLE CURRENCIES, AND WONT LIKE THE FACT CURRENCY BEING
  221. # ON THE SCREEN ALL THE TIME, SO IF THEY ARE USING THE DEFAULT CURRENCY WE WONT PRINT IT
  222. my $currency_text = $request->{curr} eq $default_currency ? '' : '('.$request->{curr}.')';
  223. my $default_currency_text = $currency_text ? '('.$default_currency.')' : '';
  224. my @columnAS = ({text => $locale->text('Invoice')},
  225. {text => $locale->text('Date')},
  226. {text => $locale->text('Total').$default_currency_text},
  227. {text => $locale->text('Paid').$default_currency_text},
  228. {text => $locale->text('Amount Due').$default_currency_text},
  229. {text => $locale->text('To pay').$default_currency_text}
  230. );
  231. my @column_headers = ({text => $locale->text('Invoice')},
  232. {text => $locale->text('Date')},
  233. {text => $locale->text('Total').$default_currency_text},
  234. {text => $locale->text('Paid').$default_currency_text},
  235. {text => $locale->text('Amount Due').$default_currency_text},
  236. {text => $locale->text('To pay').$default_currency_text}
  237. );
  238. # WE NEED TO KNOW IF WE ARE USING A CURRENCY THAT NEEDS AN EXCHANGERATE
  239. if ($default_currency ne $request->{curr} ) {
  240. # FIRST WE PUSH THE OTHER COLUMN HEADERS WE NEED
  241. push @column_headers, {text => $locale->text('Exchange Rate')},
  242. {text => $locale->text('Amount Due').$currency_text},
  243. {text => $locale->text('To pay').$currency_text};
  244. # WE SET THEM IN THE RIGHT ORDER FOR THE TABLE INSIDE THE UI
  245. @column_headers[5,6,7] = @column_headers[6,7,5];
  246. # DOES THE CURRENCY IN USE HAS AN EXCHANGE RATE?, IF SO
  247. # WE MUST SET THE VALUE, OTHERWISE THE UI WILL HANDLE IT
  248. $exchangerate = $dbPayment->get_exchange_rate($request->{curr}, $dbPayment->{current_date});
  249. if ($exchangerate) {
  250. @currency_options = {
  251. name => 'date_curr',
  252. value => "$exchangerate", #THERE IS A STRANGE BEHAVIOUR WITH THIS,
  253. text => "$exchangerate" #IF I DONT USE THE DOUBLE QUOTES, IT WILL PRINT THE ADDRESS
  254. #THERE MUST BE A REASON FOR THIS, I MUST RETURN TO IT LATER
  255. };
  256. } else {
  257. @currency_options = {
  258. name => 'date_curr'};
  259. }
  260. } else {
  261. # WE MUST SET EXCHANGERATE TO 1 FOR THE MATHS SINCE WE
  262. # ARE USING THE DEFAULT CURRENCY
  263. $exchangerate = 1;
  264. }
  265. # WE NEED TO QUERY THE DATABASE TO CHECK FOR OPEN INVOICES
  266. # IF WE DONT HAVE ANY INVOICES MATCHING THE FILTER PARAMETERS, WE WILL WARN THE USER AND STOP
  267. # THE PROCCESS.
  268. my @invoice_data;
  269. @array_options = $dbPayment->get_open_invoices();
  270. if (!$array_options[0]->{invoice_id}) {
  271. $request->error($locale->text("Nothing to do"));
  272. }
  273. for my $ref (0 .. $#array_options) {
  274. push @invoice_data, { invoice => { number => $array_options[$ref]->{invnumber},
  275. href => 'ar.pl?id='."$array_options[$ref]->{invoice_id}"
  276. },
  277. invoice_date => "$array_options[$ref]->{invoice_date}",
  278. amount => "$array_options[$ref]->{amount}",
  279. due => "$array_options[$ref]->{due}",
  280. paid => "$array_options[$ref]->{amount}" - "$array_options[$ref]->{due}",
  281. exchange_rate => "$exchangerate",
  282. due_fx => "$exchangerate"? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A',
  283. topay => "$array_options[$ref]->{due}",
  284. topay_fx => { name => "topay_fx_$ref",
  285. value => "$exchangerate" ? "$array_options[$ref]->{due}"/"$exchangerate" : 'N/A'
  286. }
  287. };
  288. }
  289. # LETS BUILD THE SELECTION FOR THE UI
  290. my $select = {
  291. stylesheet => $request->{_user}->{stylesheet},
  292. header => { text => $request->{type} eq 'receipt' ? $locale->text('Receipt') : $locale->text('Payment') },
  293. project => @project,
  294. department => @department,
  295. account => {
  296. name => 'account',
  297. options => \@account_options},
  298. datepaid => {
  299. name => 'datepaid',
  300. value => $dbPayment->{current_date}
  301. },
  302. source => {
  303. name => 'source',
  304. options => \@sources_options
  305. },
  306. source_text => {
  307. name => 'source_text',
  308. },
  309. defaultcurrency => {
  310. text => $default_currency
  311. },
  312. curr => {
  313. text => $request->{curr}
  314. },
  315. column_headers => \@column_headers,
  316. rows => \@invoice_data,
  317. vc => { name => $vendor_customer_name,
  318. address => [ {text => 'Crra 83 #32 -1'},
  319. {text => '442 6464'},
  320. {text => 'Medellín'},
  321. {text => 'Colombia'}]},
  322. post => {
  323. accesskey => 'O',
  324. title => 'POST ALT+O',
  325. name => 'action',
  326. value => 'post',
  327. text => "POST"
  328. },
  329. post_and_print => {
  330. accesskey => 'R',
  331. title => 'POST AND PRINT ALT+R',
  332. name => 'action',
  333. value => 'post_and_print',
  334. text => "POST AND PRINT"
  335. },
  336. format => {
  337. name => 'FORMAT',
  338. options => [
  339. {value => 1, text => "HTML" },
  340. {value => 2, text => "PDF" },
  341. {value => 3, text => "POSTSCRIPT" }
  342. ],
  343. },
  344. media => {
  345. name => 'MEDIA',
  346. options => [
  347. {value => 1, text => "Screen" },
  348. {value => 2, text => "PRINTER" },
  349. {value => 3, text => "EMAIL" }
  350. ],
  351. },
  352. date_curr => @currency_options # I HAVE TO PUT THIS LAST, BECAUSE IT CAN BE NULL
  353. # THIS IS AN UGLY HACK THAT MUST BE FIXED.
  354. };
  355. my $template = LedgerSMB::Template->new(
  356. user => $request->{_user},
  357. locale => $request->{_locale},
  358. # path => 'UI/payments',
  359. path => 'UI',
  360. template => 'payment2',
  361. format => 'HTML' );
  362. eval {$template->render($select) };
  363. if ($@) { $request->error("$@"); }
  364. }
  365. 1;