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