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