summaryrefslogtreecommitdiff
path: root/bin/mozilla/ir.pl
blob: 416a230fee1078387bfddb490952ecffbe85c41d (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # Inventory received module
  13. #
  14. #======================================================================
  15. use LedgerSMB::IR;
  16. use LedgerSMB::PE;
  17. require "$form->{path}/io.pl";
  18. require "$form->{path}/arap.pl";
  19. 1;
  20. # end of main
  21. sub add {
  22. $form->{title} = $locale->text('Add Vendor Invoice');
  23. $form->{callback} = "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}" unless $form->{callback};
  24. &invoice_links;
  25. &prepare_invoice;
  26. &display_form;
  27. }
  28. sub edit {
  29. $form->{title} = $locale->text('Edit Vendor Invoice');
  30. &invoice_links;
  31. &prepare_invoice;
  32. &display_form;
  33. }
  34. sub invoice_links {
  35. $form->{vc} = "vendor";
  36. $form->{type} = "invoice";
  37. # create links
  38. $form->create_links("AP", \%myconfig, "vendor", 1);
  39. # currencies
  40. @curr = split /:/, $form->{currencies};
  41. $form->{defaultcurrency} = $curr[0];
  42. chomp $form->{defaultcurrency};
  43. for (@curr) { $form->{selectcurrency} .= "<option>$_\n" }
  44. if (@{ $form->{all_vendor} }) {
  45. unless ($form->{vendor_id}) {
  46. $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
  47. }
  48. }
  49. AA->get_name(\%myconfig, \%$form);
  50. delete $form->{notes};
  51. IR->retrieve_invoice(\%myconfig, \%$form);
  52. $form->{oldlanguage_code} = $form->{language_code};
  53. $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code} });
  54. if (@ { $form->{all_partsgroup} }) {
  55. $form->{selectpartsgroup} = "<option>\n";
  56. foreach $ref (@ { $form->{all_partsgroup} }) {
  57. if ($ref->{translation}) {
  58. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
  59. } else {
  60. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
  61. }
  62. }
  63. }
  64. if (@{ $form->{all_project} }) {
  65. $form->{selectprojectnumber} = "<option>\n";
  66. for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| }
  67. }
  68. $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
  69. $form->{oldtransdate} = $form->{transdate};
  70. # vendors
  71. $form->{selectvendor} = "";
  72. if (@{ $form->{all_vendor} }) {
  73. $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
  74. for (@{ $form->{all_vendor} }) { $form->{selectvendor} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  75. }
  76. # departments
  77. if (@{ $form->{all_department} }) {
  78. $form->{selectdepartment} = "<option>\n";
  79. $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department_id};
  80. for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| }
  81. }
  82. if (@{ $form->{all_language} }) {
  83. $form->{selectlanguage} = "<option>\n";
  84. for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| }
  85. }
  86. # forex
  87. $form->{forex} = $form->{exchangerate};
  88. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  89. foreach $key (keys %{ $form->{AP_links} }) {
  90. $form->{"select$key"} = "";
  91. foreach $ref (@{ $form->{AP_links}{$key} }) {
  92. $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
  93. }
  94. if ($key eq "AP_paid") {
  95. for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
  96. $form->{"AP_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
  97. # reverse paid
  98. $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount};
  99. $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
  100. $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
  101. $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
  102. $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
  103. $form->{"cleared_$i"} = $form->{acc_trans}{$key}->[$i-1]->{cleared};
  104. $form->{paidaccounts} = $i;
  105. }
  106. } else {
  107. $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}" if $form->{acc_trans}{$key}->[0]->{accno};
  108. }
  109. }
  110. $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
  111. $form->{AP} = $form->{AP_1} unless $form->{id};
  112. $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum(\%myconfig, $form->{transdate}) <= $form->datetonum(\%myconfig, $form->{closedto}));
  113. if (! $form->{readonly}) {
  114. $form->{readonly} = 1 if $myconfig{acs} =~ /AP--Vendor Invoice/;
  115. }
  116. }
  117. sub prepare_invoice {
  118. $form->{type} = "invoice";
  119. $form->{currency} =~ s/ //g;
  120. $form->{oldcurrency} = $form->{currency};
  121. if ($form->{id}) {
  122. for (qw(invnumber ordnumber ponumber quonumber)) { $form->{$_} = $form->quote($form->{$_}) }
  123. foreach $ref (@{ $form->{invoice_details} }) {
  124. $i++;
  125. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  126. $form->{"projectnumber_$i"} = qq|$ref->{projectnumber}--$ref->{project_id}| if $ref->{project_id};
  127. $form->{"partsgroup_$i"} = qq|$ref->{partsgroup}--$ref->{partsgroup_id}| if $ref->{partsgroup_id};
  128. $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
  129. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  130. $dec = length $dec;
  131. $decimalplaces = ($dec > 2) ? $dec : 2;
  132. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  133. $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
  134. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  135. for (qw(partnumber sku description unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) }
  136. $form->{rowcount} = $i;
  137. }
  138. }
  139. }
  140. sub form_header {
  141. # set option selected
  142. for (qw(AP currency)) {
  143. $form->{"select$_"} =~ s/ selected//;
  144. $form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  145. }
  146. for (qw(vendor department)) {
  147. $form->{"select$_"} = $form->unescape($form->{"select$_"});
  148. $form->{"select$_"} =~ s/ selected//;
  149. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  150. }
  151. if ($form->{selectlanguage}) {
  152. $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
  153. $form->{"selectlanguage"} =~ s/ selected//;
  154. $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  155. $lang = qq|
  156. <tr>
  157. <th align=right nowrap>|.$locale->text('Language').qq|</th>
  158. <td><select name=language_code>$form->{selectlanguage}</select></td>
  159. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  160. <input type=hidden name="selectlanguage" value="|.
  161. $form->escape($form->{selectlanguage},1).qq|">
  162. </tr>
  163. |;
  164. }
  165. $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
  166. $exchangerate = qq|<tr>|;
  167. $exchangerate .= qq|
  168. <th align=right nowrap>|.$locale->text('Currency').qq|</th>
  169. <td><select name=currency>$form->{selectcurrency}</select></td> | if $form->{defaultcurrency};
  170. $exchangerate .= qq|
  171. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  172. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  173. |;
  174. if ($form->{defaultcurrency} && $form->{currency} ne $form->{defaultcurrency}) {
  175. if ($form->{forex}) {
  176. $exchangerate .= qq|
  177. <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
  178. <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
  179. |;
  180. } else {
  181. $exchangerate .= qq|
  182. <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
  183. <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
  184. |;
  185. }
  186. }
  187. $exchangerate .= qq|
  188. <input type=hidden name=forex value=$form->{forex}>
  189. </tr>
  190. |;
  191. if ($form->{selectvendor}) {
  192. $vendor = qq|<select name=vendor>$form->{selectvendor}</select>
  193. <input type=hidden name="selectvendor" value="|.
  194. $form->escape($form->{selectvendor},1).qq|">|;
  195. } else {
  196. $vendor = qq|<input name=vendor value="$form->{vendor}" size=35>|;
  197. }
  198. $department = qq|
  199. <tr>
  200. <th align="right" nowrap>|.$locale->text('Department').qq|</th>
  201. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  202. <input type=hidden name=selectdepartment value="|.
  203. $form->escape($form->{selectdepartment},1).qq|">
  204. </td>
  205. </tr>
  206. | if $form->{selectdepartment};
  207. $n = ($form->{creditremaining} < 0) ? "0" : "1";
  208. $i = $form->{rowcount} + 1;
  209. $focus = "partnumber_$i";
  210. $form->header;
  211. print qq|
  212. <body onLoad="document.forms[0].${focus}.focus()" />
  213. <form method=post action="$form->{script}">
  214. |;
  215. $form->{vc} = "vendor";
  216. $form->hide_form(qw(id title vc type terms creditlimit creditremaining closedto locked shipped oldtransdate recurring));
  217. print qq|
  218. <table width=100%>
  219. <tr class=listtop>
  220. <th>$form->{title}</th>
  221. </tr>
  222. <tr height="5"></tr>
  223. <tr>
  224. <td>
  225. <table width=100%>
  226. <tr valign=top>
  227. <td>
  228. <table>
  229. <tr>
  230. <th align=right nowrap>|.$locale->text('Vendor').qq|</th>
  231. <td colspan=3>$vendor</td>
  232. <input type=hidden name=vendor_id value=$form->{vendor_id}>
  233. <input type=hidden name=oldvendor value="$form->{oldvendor}">
  234. </tr>
  235. <tr>
  236. <td></td>
  237. <td colspan=3>
  238. <table>
  239. <tr>
  240. <th nowrap>|.$locale->text('Credit Limit').qq|</th>
  241. <td>|.$form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0").qq|</td>
  242. <td width=20%></td>
  243. <th nowrap>|.$locale->text('Remaining').qq|</th>
  244. <td class="plus$n" nowrap>|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</td>
  245. </tr>
  246. </table>
  247. </td>
  248. <tr>
  249. <th align=right>|.$locale->text('Record in').qq|</th>
  250. <td colspan=3><select name=AP>$form->{selectAP}</select></td>
  251. <input type=hidden name=selectAP value="$form->{selectAP}">
  252. </tr>
  253. $department
  254. $exchangerate
  255. </table>
  256. </td>
  257. <td align=right>
  258. <table>
  259. <tr>
  260. <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
  261. <td><input name=invnumber size=20 value="$form->{invnumber}"></td>
  262. </tr>
  263. <tr>
  264. <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
  265. <td><input name=ordnumber size=20 value="$form->{ordnumber}"></td>
  266. <input type=hidden name=quonumber value="$form->{quonumber}">
  267. </tr>
  268. <tr>
  269. <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
  270. <td><input name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
  271. </tr>
  272. <tr>
  273. <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
  274. <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
  275. </tr>
  276. <tr>
  277. <th align=right nowrap>|.$locale->text('PO Number').qq|</th>
  278. <td><input name=ponumber size=20 value="$form->{ponumber}"></td>
  279. </tr>
  280. $lang
  281. </table>
  282. </td>
  283. </tr>
  284. </table>
  285. </td>
  286. </tr>
  287. |;
  288. $form->hide_form(qw(selectcurrency defaultcurrency taxaccounts));
  289. for (split / /, $form->{taxaccounts}) { $form->hide_form("${_}_rate", "${_}_description") }
  290. }
  291. sub form_footer {
  292. $form->{invtotal} = $form->{invsubtotal};
  293. if (($rows = $form->numtextrows($form->{notes}, 35, 8)) < 2) {
  294. $rows = 2;
  295. }
  296. if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
  297. $introws = 2;
  298. }
  299. $rows = ($rows > $introws) ? $rows : $introws;
  300. $notes = qq|<textarea name=notes rows=$rows cols=35 wrap=soft>$form->{notes}</textarea>|;
  301. $intnotes = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
  302. $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
  303. $taxincluded = "";
  304. if ($form->{taxaccounts}) {
  305. $taxincluded = qq|
  306. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b>
  307. |;
  308. }
  309. if (!$form->{taxincluded}) {
  310. foreach $item (split / /, $form->{taxaccounts}) {
  311. if ($form->{"${item}_base"}) {
  312. $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
  313. $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
  314. $tax .= qq|
  315. <tr>
  316. <th align=right>$form->{"${item}_description"}</th>
  317. <td align=right>$form->{"${item}_total"}</td>
  318. </tr>
  319. |;
  320. }
  321. }
  322. $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
  323. $subtotal = qq|
  324. <tr>
  325. <th align=right>|.$locale->text('Subtotal').qq|</th>
  326. <td align=right>$form->{invsubtotal}</td>
  327. </tr>
  328. |;
  329. }
  330. $form->{oldinvtotal} = $form->{invtotal};
  331. $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
  332. print qq|
  333. <tr>
  334. <td>
  335. <table width=100%>
  336. <tr valign=bottom>
  337. <td>
  338. <table>
  339. <tr>
  340. <th align=left>|.$locale->text('Notes').qq|</th>
  341. <th align=left>|.$locale->text('Internal Notes').qq|</th>
  342. </tr>
  343. <tr valign=top>
  344. <td>$notes</td>
  345. <td>$intnotes</td>
  346. </tr>
  347. </table>
  348. </td>
  349. <td align=right>
  350. $taxincluded
  351. <br>
  352. <table>
  353. $subtotal
  354. $tax
  355. <tr>
  356. <th align=right>|.$locale->text('Total').qq|</th>
  357. <td align=right>$form->{invtotal}</td>
  358. </tr>
  359. </table>
  360. </td>
  361. </tr>
  362. </table>
  363. </td>
  364. </tr>
  365. <tr>
  366. <td>
  367. <table width=100%>
  368. <tr>
  369. <th colspan=6 class=listheading>|.$locale->text('Payments').qq|</th>
  370. </tr>
  371. |;
  372. if ($form->{currency} eq $form->{defaultcurrency}) {
  373. @column_index = qw(datepaid source memo paid AP_paid);
  374. } else {
  375. @column_index = qw(datepaid source memo paid exchangerate AP_paid);
  376. }
  377. $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
  378. $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
  379. $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
  380. $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
  381. $column_data{source} = "<th>".$locale->text('Source')."</th>";
  382. $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
  383. print qq|
  384. <tr>
  385. |;
  386. for (@column_index) { print "$column_data{$_}\n" }
  387. print qq|
  388. </tr>
  389. |;
  390. $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
  391. for $i (1 .. $form->{paidaccounts}) {
  392. $form->hide_form("cleared_$i");
  393. print qq|
  394. <tr>
  395. |;
  396. $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
  397. $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
  398. # format amounts
  399. $totalpaid += $form->{"paid_$i"};
  400. $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
  401. $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
  402. $exchangerate = qq|&nbsp;|;
  403. if ($form->{currency} ne $form->{defaultcurrency}) {
  404. if ($form->{"forex_$i"}) {
  405. $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
  406. } else {
  407. $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
  408. }
  409. }
  410. $exchangerate .= qq|
  411. <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
  412. |;
  413. $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  414. $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
  415. $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
  416. $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
  417. $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
  418. $column_data{"memo_$i"} = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
  419. for (@column_index) { print qq|$column_data{"${_}_$i"}\n| }
  420. print qq|
  421. </tr>
  422. |;
  423. }
  424. $form->{oldtotalpaid} = $totalpaid;
  425. $form->hide_form(qw(paidaccounts selectAP_paid oldinvtotal oldtotalpaid));
  426. print qq|
  427. </table>
  428. </td>
  429. </tr>
  430. <tr>
  431. <td><hr size=3 noshade></td>
  432. </tr>
  433. </table>
  434. <br>
  435. |;
  436. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  437. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  438. # type=submit $locale->text('Update')
  439. # type=submit $locale->text('Post')
  440. # type=submit $locale->text('Post as new')
  441. # type=submit $locale->text('Schedule')
  442. # type=submit $locale->text('Purchase Order')
  443. # type=submit $locale->text('Delete')
  444. if (! $form->{readonly}) {
  445. %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  446. 'Post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  447. 'Post as new' => { ndx => 5, key => 'N', value => $locale->text('Post as new') },
  448. 'Purchase Order' => { ndx => 6, key => 'L', value => $locale->text('Purchase Order') },
  449. 'Schedule' => { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  450. 'Delete' => { ndx => 8, key => 'D', value => $locale->text('Delete') },
  451. );
  452. if ($form->{id}) {
  453. if ($form->{locked}) {
  454. for ("Post", "Delete") { delete $button{$_} }
  455. }
  456. } else {
  457. if ($transdate > $closedto) {
  458. for ('Update', 'Post', 'Schedule') { $a{$_} = 1 }
  459. }
  460. for (keys %button) { delete $button{$_} if ! $a{$_} }
  461. }
  462. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  463. }
  464. if ($form->{menubar}) {
  465. require "$form->{path}/menu.pl";
  466. &menubar;
  467. }
  468. $form->hide_form(qw(rowcount callback path login sessionid));
  469. print qq|
  470. </form>
  471. </body>
  472. </html>
  473. |;
  474. }
  475. sub update {
  476. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  477. if ($newname = &check_name(vendor)) {
  478. &rebuild_vc(vendor, AP, $form->{transdate}, 1);
  479. }
  480. if ($form->{transdate} ne $form->{oldtransdate}) {
  481. $form->{duedate} = ($form->{terms}) ? $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1) : $form->{duedate};
  482. $form->{oldtransdate} = $form->{transdate};
  483. &rebuild_vc(vendor, AP, $form->{transdate}, 1) if ! $newname;
  484. if ($form->{currency} ne $form->{defaultcurrency}) {
  485. delete $form->{exchangerate};
  486. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  487. $form->{oldcurrency} = $form->{currency};
  488. }
  489. }
  490. if ($form->{currency} ne $form->{oldcurrency}) {
  491. delete $form->{exchangerate};
  492. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  493. }
  494. $j = 1;
  495. for $i (1 .. $form->{paidaccounts}) {
  496. if ($form->{"paid_$i"}) {
  497. for (qw(datepaid source memo cleared)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
  498. for (qw(paid exchangerate)) { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  499. $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'sell')));
  500. if ($j++ != $i) {
  501. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  502. }
  503. } else {
  504. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  505. }
  506. $form->{paidaccounts} = $j;
  507. }
  508. $i = $form->{rowcount};
  509. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  510. for (qw(partsgroup projectnumber)) {
  511. $form->{"select$_"} = $form->unescape($form->{"select$_"}) if $form->{"select$_"};
  512. }
  513. if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
  514. $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
  515. &check_form;
  516. } else {
  517. IR->retrieve_item(\%myconfig, \%$form);
  518. my $rows = scalar @{ $form->{item_list} };
  519. if ($form->{language_code} && $rows == 0) {
  520. $language_code = $form->{language_code};
  521. $form->{language_code} = "";
  522. IR->retrieve_item(\%myconfig, \%$form);
  523. $form->{language_code} = $language_code;
  524. $rows = scalar @{ $form->{item_list} };
  525. }
  526. if ($rows) {
  527. if ($rows > 1) {
  528. &select_item;
  529. exit;
  530. } else {
  531. $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
  532. $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
  533. for (qw(partnumber description unit)) { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) }
  534. for (keys %{ $form->{item_list}[0] }) { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  535. $form->{"discount_$i"} = $form->{discount} * 100;
  536. if ($sellprice) {
  537. $form->{"sellprice_$i"} = $sellprice;
  538. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  539. $dec = length $dec;
  540. $decimalplaces = ($dec > 2) ? $dec : 2;
  541. } else {
  542. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  543. $dec = length $dec;
  544. $decimalplaces = ($dec > 2) ? $dec : 2;
  545. $form->{"sellprice_$i"} /= $exchangerate;
  546. }
  547. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  548. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  549. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  550. if (!$form->{taxincluded}) {
  551. for (split / /, $form->{"taxaccounts_$i"}) { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
  552. }
  553. $form->{creditremaining} -= $amount;
  554. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  555. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  556. for (qw(qty discount)) { $form->{"{_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) }
  557. }
  558. &display_form;
  559. } else {
  560. # ok, so this is a new part
  561. # ask if it is a part or service item
  562. if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
  563. $form->{"discount_$i"} = "";
  564. &display_form;
  565. } else {
  566. $form->{"id_$i"} = 0;
  567. $form->{"unit_$i"} = $locale->text('ea');
  568. &new_item;
  569. }
  570. }
  571. }
  572. }
  573. sub post {
  574. $form->isblank("transdate", $locale->text('Invoice Date missing!'));
  575. $form->isblank("vendor", $locale->text('Vendor missing!'));
  576. # if the vendor changed get new values
  577. if (&check_name(vendor)) {
  578. &update;
  579. exit;
  580. }
  581. &validate_items;
  582. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  583. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  584. $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
  585. $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
  586. for $i (1 .. $form->{paidaccounts}) {
  587. if ($form->{"paid_$i"}) {
  588. $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
  589. $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
  590. $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
  591. if ($form->{currency} ne $form->{defaultcurrency}) {
  592. $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
  593. $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
  594. }
  595. }
  596. }
  597. if (! $form->{repost}) {
  598. if ($form->{id}) {
  599. &repost;
  600. exit;
  601. }
  602. }
  603. ($form->{AP}) = split /--/, $form->{AP};
  604. ($form->{AP_paid}) = split /--/, $form->{AP_paid};
  605. if (IR->post_invoice(\%myconfig, \%$form)) {
  606. $form->redirect($locale->text('Invoice')." $form->{invnumber} ".$locale->text('posted!'));
  607. } else {
  608. $form->error($locale->text('Cannot post invoice!'));
  609. }
  610. }
  611. sub delete {
  612. $form->header;
  613. print qq|
  614. <body>
  615. <form method=post action=$form->{script}>
  616. |;
  617. $form->{action} = "yes";
  618. $form->hide_form;
  619. print qq|
  620. <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
  621. <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
  622. <p>
  623. <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
  624. </form>
  625. |;
  626. }
  627. sub yes {
  628. if (IR->delete_invoice(\%myconfig, \%$form)) {
  629. $form->redirect($locale->text('Invoice deleted!'));
  630. } else {
  631. $form->error($locale->text('Cannot delete invoice!'));
  632. }
  633. }