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