|
Lines 21-27
use Modern::Perl;
Link Here
|
| 21 |
use FindBin qw( $Bin ); |
21 |
use FindBin qw( $Bin ); |
| 22 |
|
22 |
|
| 23 |
use Test::NoWarnings; |
23 |
use Test::NoWarnings; |
| 24 |
use Test::More tests => 6; |
24 |
use Test::More tests => 7; |
| 25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
| 26 |
|
26 |
|
| 27 |
use t::lib::Mocks; |
27 |
use t::lib::Mocks; |
|
Lines 1616-1618
subtest 'create_edi_order_logging' => sub {
Link Here
|
| 1616 |
|
1616 |
|
| 1617 |
$schema->storage->txn_rollback; |
1617 |
$schema->storage->txn_rollback; |
| 1618 |
}; |
1618 |
}; |
| 1619 |
- |
1619 |
|
|
|
1620 |
subtest 'duplicate_invoice_blocking' => sub { |
| 1621 |
plan tests => 7; |
| 1622 |
|
| 1623 |
$schema->storage->txn_begin; |
| 1624 |
|
| 1625 |
# Get dirname for transport |
| 1626 |
my $dirname = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} ); |
| 1627 |
|
| 1628 |
# Test 1: Backward compatibility - duplicate detection disabled |
| 1629 |
subtest 'duplicate_detection_disabled' => sub { |
| 1630 |
plan tests => 4; |
| 1631 |
|
| 1632 |
$schema->storage->txn_begin; |
| 1633 |
|
| 1634 |
# Disable duplicate blocking preference |
| 1635 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 0 ); |
| 1636 |
|
| 1637 |
# Create file transport for local testing |
| 1638 |
my $file_transport = $builder->build( |
| 1639 |
{ |
| 1640 |
source => 'FileTransport', |
| 1641 |
value => { |
| 1642 |
name => 'Test Invoice Transport', |
| 1643 |
transport => 'local', |
| 1644 |
download_directory => $dirname, |
| 1645 |
upload_directory => $dirname, |
| 1646 |
} |
| 1647 |
} |
| 1648 |
); |
| 1649 |
|
| 1650 |
# Create vendor EDI account |
| 1651 |
my $account = $builder->build( |
| 1652 |
{ |
| 1653 |
source => 'VendorEdiAccount', |
| 1654 |
value => { |
| 1655 |
description => 'test vendor', |
| 1656 |
file_transport_id => $file_transport->{file_transport_id}, |
| 1657 |
plugin => '', |
| 1658 |
san => '5013546027173', |
| 1659 |
} |
| 1660 |
} |
| 1661 |
); |
| 1662 |
|
| 1663 |
# Create test invoice that already exists |
| 1664 |
my $existing_invoice = $builder->build( |
| 1665 |
{ |
| 1666 |
source => 'Aqinvoice', |
| 1667 |
value => { |
| 1668 |
invoicenumber => 'INV00003', |
| 1669 |
booksellerid => $account->{vendor_id}, |
| 1670 |
shipmentdate => '2020-01-01', |
| 1671 |
} |
| 1672 |
} |
| 1673 |
); |
| 1674 |
|
| 1675 |
# Create test basket and order |
| 1676 |
my $basket = $builder->build_object( |
| 1677 |
{ |
| 1678 |
class => 'Koha::Acquisition::Baskets', |
| 1679 |
value => { |
| 1680 |
booksellerid => $account->{vendor_id}, |
| 1681 |
basketname => 'Test Basket', |
| 1682 |
} |
| 1683 |
} |
| 1684 |
); |
| 1685 |
my $order = $builder->build_object( |
| 1686 |
{ |
| 1687 |
class => 'Koha::Acquisition::Orders', |
| 1688 |
value => { |
| 1689 |
basketno => $basket->id, |
| 1690 |
orderstatus => 'new', |
| 1691 |
biblionumber => undef, |
| 1692 |
} |
| 1693 |
} |
| 1694 |
); |
| 1695 |
my $ordernumber = $order->ordernumber; |
| 1696 |
|
| 1697 |
# Prepare invoice message |
| 1698 |
my $filename = 'INVOICE.CEI'; |
| 1699 |
ok( -e $dirname . $filename, 'File INVOICE.CEI found' ); |
| 1700 |
|
| 1701 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 1702 |
$trans->working_directory($dirname); |
| 1703 |
|
| 1704 |
my $mhash = $trans->message_hash(); |
| 1705 |
$mhash->{message_type} = 'INVOICE'; |
| 1706 |
$trans->ingest( $mhash, $filename ); |
| 1707 |
|
| 1708 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 1709 |
my $raw_msg = $invoice_message->raw_msg; |
| 1710 |
$raw_msg =~ s/ORDERNUMBER1/$ordernumber/g; |
| 1711 |
$raw_msg =~ s/ORDERNUMBER2/$ordernumber/g; |
| 1712 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 1713 |
|
| 1714 |
# Clear logger |
| 1715 |
$logger->clear(); |
| 1716 |
|
| 1717 |
# Process the invoice - should succeed despite duplicate |
| 1718 |
my $error; |
| 1719 |
eval { |
| 1720 |
process_invoice($invoice_message); |
| 1721 |
1; |
| 1722 |
} or do { |
| 1723 |
$error = $@; |
| 1724 |
}; |
| 1725 |
ok( !$error, 'Invoice processing completed without dying when preference disabled' ); |
| 1726 |
|
| 1727 |
# Verify duplicate was NOT blocked (second invoice created) |
| 1728 |
my $duplicate_invoices = $schema->resultset('Aqinvoice')->search( |
| 1729 |
{ |
| 1730 |
invoicenumber => 'INV00003', |
| 1731 |
booksellerid => $account->{vendor_id}, |
| 1732 |
} |
| 1733 |
); |
| 1734 |
is( $duplicate_invoices->count, 2, 'Duplicate invoice was allowed when preference disabled' ); |
| 1735 |
|
| 1736 |
# Verify no duplicate error was logged |
| 1737 |
my $errors = $invoice_message->edifact_errors; |
| 1738 |
my $duplicate_error = $errors->search( { details => { 'like', '%Duplicate invoice%' } } )->count; |
| 1739 |
is( $duplicate_error, 0, 'No duplicate error logged when preference disabled' ); |
| 1740 |
|
| 1741 |
$logger->clear(); |
| 1742 |
$schema->storage->txn_rollback; |
| 1743 |
}; |
| 1744 |
|
| 1745 |
# Test 2: Duplicate blocking enabled |
| 1746 |
subtest 'duplicate_blocking_enabled' => sub { |
| 1747 |
plan tests => 6; |
| 1748 |
|
| 1749 |
$schema->storage->txn_begin; |
| 1750 |
|
| 1751 |
# Enable duplicate blocking preference |
| 1752 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 1753 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailNotice', 0 ); |
| 1754 |
|
| 1755 |
# Create file transport for local testing |
| 1756 |
my $file_transport = $builder->build( |
| 1757 |
{ |
| 1758 |
source => 'FileTransport', |
| 1759 |
value => { |
| 1760 |
name => 'Test Invoice Transport', |
| 1761 |
transport => 'local', |
| 1762 |
download_directory => $dirname, |
| 1763 |
upload_directory => $dirname, |
| 1764 |
} |
| 1765 |
} |
| 1766 |
); |
| 1767 |
|
| 1768 |
# Create vendor EDI account |
| 1769 |
my $account = $builder->build( |
| 1770 |
{ |
| 1771 |
source => 'VendorEdiAccount', |
| 1772 |
value => { |
| 1773 |
description => 'test vendor', |
| 1774 |
file_transport_id => $file_transport->{file_transport_id}, |
| 1775 |
plugin => '', |
| 1776 |
san => '5013546027173', |
| 1777 |
} |
| 1778 |
} |
| 1779 |
); |
| 1780 |
|
| 1781 |
# Create test invoice that already exists |
| 1782 |
my $existing_invoice = $builder->build( |
| 1783 |
{ |
| 1784 |
source => 'Aqinvoice', |
| 1785 |
value => { |
| 1786 |
invoicenumber => 'INV00003', |
| 1787 |
booksellerid => $account->{vendor_id}, |
| 1788 |
shipmentdate => '2020-01-01', |
| 1789 |
} |
| 1790 |
} |
| 1791 |
); |
| 1792 |
|
| 1793 |
# Create test basket and order |
| 1794 |
my $basket = $builder->build_object( |
| 1795 |
{ |
| 1796 |
class => 'Koha::Acquisition::Baskets', |
| 1797 |
value => { |
| 1798 |
booksellerid => $account->{vendor_id}, |
| 1799 |
basketname => 'Test Basket', |
| 1800 |
} |
| 1801 |
} |
| 1802 |
); |
| 1803 |
my $order = $builder->build_object( |
| 1804 |
{ |
| 1805 |
class => 'Koha::Acquisition::Orders', |
| 1806 |
value => { |
| 1807 |
basketno => $basket->id, |
| 1808 |
orderstatus => 'new', |
| 1809 |
biblionumber => undef, |
| 1810 |
} |
| 1811 |
} |
| 1812 |
); |
| 1813 |
my $ordernumber = $order->ordernumber; |
| 1814 |
|
| 1815 |
# Prepare invoice message |
| 1816 |
my $filename = 'INVOICE.CEI'; |
| 1817 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 1818 |
$trans->working_directory($dirname); |
| 1819 |
|
| 1820 |
my $mhash = $trans->message_hash(); |
| 1821 |
$mhash->{message_type} = 'INVOICE'; |
| 1822 |
$trans->ingest( $mhash, $filename ); |
| 1823 |
|
| 1824 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 1825 |
my $raw_msg = $invoice_message->raw_msg; |
| 1826 |
$raw_msg =~ s/ORDERNUMBER1/$ordernumber/g; |
| 1827 |
$raw_msg =~ s/ORDERNUMBER2/$ordernumber/g; |
| 1828 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 1829 |
|
| 1830 |
# Clear logger |
| 1831 |
$logger->clear(); |
| 1832 |
|
| 1833 |
# Process the invoice - should block duplicate |
| 1834 |
my $error; |
| 1835 |
eval { |
| 1836 |
process_invoice($invoice_message); |
| 1837 |
1; |
| 1838 |
} or do { |
| 1839 |
$error = $@; |
| 1840 |
}; |
| 1841 |
ok( !$error, 'Invoice processing completed without dying' ); |
| 1842 |
|
| 1843 |
# Verify duplicate was blocked (only original invoice exists) |
| 1844 |
my $duplicate_invoices = $schema->resultset('Aqinvoice')->search( |
| 1845 |
{ |
| 1846 |
invoicenumber => 'INV00003', |
| 1847 |
booksellerid => $account->{vendor_id}, |
| 1848 |
} |
| 1849 |
); |
| 1850 |
is( $duplicate_invoices->count, 1, 'Duplicate invoice was blocked' ); |
| 1851 |
|
| 1852 |
# Verify error was logged |
| 1853 |
$logger->error_like( |
| 1854 |
qr/Duplicate invoice INV00003 for vendor.*/, |
| 1855 |
'Error logged for duplicate invoice' |
| 1856 |
); |
| 1857 |
|
| 1858 |
# Verify error recorded in edifact_errors table |
| 1859 |
my $errors = $invoice_message->edifact_errors; |
| 1860 |
my $duplicate_error = $errors->search( { details => { 'like', '%Duplicate invoice%' } } )->first; |
| 1861 |
ok( $duplicate_error, 'Duplicate error recorded in edifact_errors table' ); |
| 1862 |
like( |
| 1863 |
$duplicate_error->details, qr/Duplicate invoice number 'INV00003'/, |
| 1864 |
'Error details contain invoice number' |
| 1865 |
); |
| 1866 |
|
| 1867 |
# Verify message status set to error |
| 1868 |
$invoice_message->discard_changes; |
| 1869 |
is( $invoice_message->status, 'error', 'Message status set to error' ); |
| 1870 |
|
| 1871 |
$logger->clear(); |
| 1872 |
$schema->storage->txn_rollback; |
| 1873 |
}; |
| 1874 |
|
| 1875 |
# Test 3: Same invoice number with different vendor should be allowed |
| 1876 |
subtest 'different_vendor_allowed' => sub { |
| 1877 |
plan tests => 3; |
| 1878 |
|
| 1879 |
$schema->storage->txn_begin; |
| 1880 |
|
| 1881 |
# Enable duplicate blocking preference |
| 1882 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 1883 |
|
| 1884 |
# Create file transport for local testing |
| 1885 |
my $file_transport = $builder->build( |
| 1886 |
{ |
| 1887 |
source => 'FileTransport', |
| 1888 |
value => { |
| 1889 |
name => 'Test Invoice Transport', |
| 1890 |
transport => 'local', |
| 1891 |
download_directory => $dirname, |
| 1892 |
upload_directory => $dirname, |
| 1893 |
} |
| 1894 |
} |
| 1895 |
); |
| 1896 |
|
| 1897 |
# Create two different vendors |
| 1898 |
my $account1 = $builder->build( |
| 1899 |
{ |
| 1900 |
source => 'VendorEdiAccount', |
| 1901 |
value => { |
| 1902 |
description => 'test vendor 1', |
| 1903 |
file_transport_id => $file_transport->{file_transport_id}, |
| 1904 |
plugin => '', |
| 1905 |
san => '5013546027173', |
| 1906 |
} |
| 1907 |
} |
| 1908 |
); |
| 1909 |
|
| 1910 |
my $account2 = $builder->build( |
| 1911 |
{ |
| 1912 |
source => 'VendorEdiAccount', |
| 1913 |
value => { |
| 1914 |
description => 'test vendor 2', |
| 1915 |
file_transport_id => $file_transport->{file_transport_id}, |
| 1916 |
plugin => '', |
| 1917 |
san => '5013546027999', |
| 1918 |
} |
| 1919 |
} |
| 1920 |
); |
| 1921 |
|
| 1922 |
# Create invoice for vendor 1 |
| 1923 |
my $invoice1 = $builder->build( |
| 1924 |
{ |
| 1925 |
source => 'Aqinvoice', |
| 1926 |
value => { |
| 1927 |
invoicenumber => 'INV00003', |
| 1928 |
booksellerid => $account1->{vendor_id}, |
| 1929 |
} |
| 1930 |
} |
| 1931 |
); |
| 1932 |
|
| 1933 |
# Create test basket and order for vendor 2 |
| 1934 |
my $basket = $builder->build_object( |
| 1935 |
{ |
| 1936 |
class => 'Koha::Acquisition::Baskets', |
| 1937 |
value => { |
| 1938 |
booksellerid => $account2->{vendor_id}, |
| 1939 |
basketname => 'Test Basket', |
| 1940 |
} |
| 1941 |
} |
| 1942 |
); |
| 1943 |
my $order = $builder->build_object( |
| 1944 |
{ |
| 1945 |
class => 'Koha::Acquisition::Orders', |
| 1946 |
value => { |
| 1947 |
basketno => $basket->id, |
| 1948 |
orderstatus => 'new', |
| 1949 |
biblionumber => undef, |
| 1950 |
} |
| 1951 |
} |
| 1952 |
); |
| 1953 |
|
| 1954 |
# Prepare invoice message for vendor 2 using same file |
| 1955 |
my $filename = 'INVOICE.CEI'; |
| 1956 |
my $trans = Koha::Edifact::Transport->new( $account2->{id} ); |
| 1957 |
$trans->working_directory($dirname); |
| 1958 |
|
| 1959 |
my $mhash = $trans->message_hash(); |
| 1960 |
$mhash->{message_type} = 'INVOICE'; |
| 1961 |
$trans->ingest( $mhash, $filename ); |
| 1962 |
|
| 1963 |
my $invoice_message = $schema->resultset('EdifactMessage')->search( |
| 1964 |
{ filename => $filename }, |
| 1965 |
{ order_by => { -desc => 'id' }, rows => 1 } |
| 1966 |
)->single; |
| 1967 |
my $raw_msg = $invoice_message->raw_msg; |
| 1968 |
$raw_msg =~ s/ORDERNUMBER1/$order->ordernumber/g; |
| 1969 |
$raw_msg =~ s/ORDERNUMBER2/$order->ordernumber/g; |
| 1970 |
$raw_msg =~ s/5013546027173/$account2->{san}/g; # Replace vendor SAN |
| 1971 |
$invoice_message->update( |
| 1972 |
{ |
| 1973 |
raw_msg => $raw_msg, |
| 1974 |
vendor_id => $account2->{vendor_id}, |
| 1975 |
edi_acct => $account2->{id} |
| 1976 |
} |
| 1977 |
); |
| 1978 |
|
| 1979 |
# Clear logger |
| 1980 |
$logger->clear(); |
| 1981 |
|
| 1982 |
# Process the invoice - should succeed (different vendor) |
| 1983 |
my $error; |
| 1984 |
eval { |
| 1985 |
process_invoice($invoice_message); |
| 1986 |
1; |
| 1987 |
} or do { |
| 1988 |
$error = $@; |
| 1989 |
}; |
| 1990 |
ok( !$error, 'Invoice processing completed without dying' ); |
| 1991 |
|
| 1992 |
# Verify both invoices exist (one per vendor) |
| 1993 |
my $invoices_vendor1 = $schema->resultset('Aqinvoice')->search( |
| 1994 |
{ |
| 1995 |
invoicenumber => 'INV00003', |
| 1996 |
booksellerid => $account1->{vendor_id}, |
| 1997 |
} |
| 1998 |
); |
| 1999 |
is( $invoices_vendor1->count, 1, 'Invoice exists for vendor 1' ); |
| 2000 |
|
| 2001 |
my $invoices_vendor2 = $schema->resultset('Aqinvoice')->search( |
| 2002 |
{ |
| 2003 |
invoicenumber => 'INV00003', |
| 2004 |
booksellerid => $account2->{vendor_id}, |
| 2005 |
} |
| 2006 |
); |
| 2007 |
is( $invoices_vendor2->count, 1, 'Invoice allowed for vendor 2 with same invoice number' ); |
| 2008 |
|
| 2009 |
$logger->clear(); |
| 2010 |
$schema->storage->txn_rollback; |
| 2011 |
}; |
| 2012 |
|
| 2013 |
# Test 4: Library staff email notification |
| 2014 |
subtest 'library_email_notification' => sub { |
| 2015 |
plan tests => 5; |
| 2016 |
|
| 2017 |
$schema->storage->txn_begin; |
| 2018 |
|
| 2019 |
# Enable duplicate blocking and email notifications |
| 2020 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 2021 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailNotice', 1 ); |
| 2022 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailAddresses', 'library@example.com' ); |
| 2023 |
|
| 2024 |
# Create letter templates for notifications (delete first if exist) |
| 2025 |
$schema->resultset('Letter')->search( |
| 2026 |
{ |
| 2027 |
module => 'acquisition', |
| 2028 |
code => [ 'EDI_DUP_INV_LIBRARY', 'EDI_DUP_INV_VENDOR' ], |
| 2029 |
} |
| 2030 |
)->delete; |
| 2031 |
|
| 2032 |
$builder->build( |
| 2033 |
{ |
| 2034 |
source => 'Letter', |
| 2035 |
value => { |
| 2036 |
module => 'acquisition', |
| 2037 |
code => 'EDI_DUP_INV_LIBRARY', |
| 2038 |
branchcode => '', |
| 2039 |
name => 'Test library notification', |
| 2040 |
is_html => 0, |
| 2041 |
title => 'Duplicate Invoice - <<invoicenumber>>', |
| 2042 |
content => 'Duplicate invoice <<invoicenumber>>. Processing has been blocked.', |
| 2043 |
message_transport_type => 'email', |
| 2044 |
lang => 'default', |
| 2045 |
} |
| 2046 |
} |
| 2047 |
); |
| 2048 |
$builder->build( |
| 2049 |
{ |
| 2050 |
source => 'Letter', |
| 2051 |
value => { |
| 2052 |
module => 'acquisition', |
| 2053 |
code => 'EDI_DUP_INV_VENDOR', |
| 2054 |
branchcode => '', |
| 2055 |
name => 'Test vendor notification', |
| 2056 |
is_html => 0, |
| 2057 |
title => 'Duplicate Invoice - <<invoicenumber>>', |
| 2058 |
content => 'Duplicate invoice <<invoicenumber>>. Please use UNIQUE invoice number.', |
| 2059 |
message_transport_type => 'email', |
| 2060 |
lang => 'default', |
| 2061 |
} |
| 2062 |
} |
| 2063 |
); |
| 2064 |
|
| 2065 |
# Create file transport for local testing |
| 2066 |
my $file_transport = $builder->build( |
| 2067 |
{ |
| 2068 |
source => 'FileTransport', |
| 2069 |
value => { |
| 2070 |
name => 'Test Invoice Transport', |
| 2071 |
transport => 'local', |
| 2072 |
download_directory => $dirname, |
| 2073 |
upload_directory => $dirname, |
| 2074 |
} |
| 2075 |
} |
| 2076 |
); |
| 2077 |
|
| 2078 |
# Create vendor EDI account |
| 2079 |
my $account = $builder->build( |
| 2080 |
{ |
| 2081 |
source => 'VendorEdiAccount', |
| 2082 |
value => { |
| 2083 |
description => 'test vendor', |
| 2084 |
file_transport_id => $file_transport->{file_transport_id}, |
| 2085 |
plugin => '', |
| 2086 |
san => '5013546027173', |
| 2087 |
} |
| 2088 |
} |
| 2089 |
); |
| 2090 |
|
| 2091 |
# Create test invoice that already exists |
| 2092 |
my $existing_invoice = $builder->build( |
| 2093 |
{ |
| 2094 |
source => 'Aqinvoice', |
| 2095 |
value => { |
| 2096 |
invoicenumber => 'INV00003', |
| 2097 |
booksellerid => $account->{vendor_id}, |
| 2098 |
shipmentdate => '2020-01-01', |
| 2099 |
} |
| 2100 |
} |
| 2101 |
); |
| 2102 |
|
| 2103 |
# Create test basket and order |
| 2104 |
my $basket = $builder->build_object( |
| 2105 |
{ |
| 2106 |
class => 'Koha::Acquisition::Baskets', |
| 2107 |
value => { |
| 2108 |
booksellerid => $account->{vendor_id}, |
| 2109 |
basketname => 'Test Basket', |
| 2110 |
} |
| 2111 |
} |
| 2112 |
); |
| 2113 |
my $order = $builder->build_object( |
| 2114 |
{ |
| 2115 |
class => 'Koha::Acquisition::Orders', |
| 2116 |
value => { |
| 2117 |
basketno => $basket->id, |
| 2118 |
orderstatus => 'new', |
| 2119 |
biblionumber => undef, |
| 2120 |
} |
| 2121 |
} |
| 2122 |
); |
| 2123 |
|
| 2124 |
# Prepare invoice message |
| 2125 |
my $filename = 'INVOICE.CEI'; |
| 2126 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 2127 |
$trans->working_directory($dirname); |
| 2128 |
|
| 2129 |
my $mhash = $trans->message_hash(); |
| 2130 |
$mhash->{message_type} = 'INVOICE'; |
| 2131 |
$trans->ingest( $mhash, $filename ); |
| 2132 |
|
| 2133 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 2134 |
my $raw_msg = $invoice_message->raw_msg; |
| 2135 |
$raw_msg =~ s/ORDERNUMBER1/$order->ordernumber/g; |
| 2136 |
$raw_msg =~ s/ORDERNUMBER2/$order->ordernumber/g; |
| 2137 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 2138 |
|
| 2139 |
# Process the invoice |
| 2140 |
process_invoice($invoice_message); |
| 2141 |
|
| 2142 |
# Verify library email was queued in message_queue |
| 2143 |
my $library_messages = $schema->resultset('MessageQueue')->search( |
| 2144 |
{ |
| 2145 |
to_address => 'library@example.com', |
| 2146 |
status => [ 'sent', 'pending', 'failed' ], # Accept any status - queuing is what matters |
| 2147 |
} |
| 2148 |
); |
| 2149 |
is( $library_messages->count, 1, 'Library notification was queued' ); |
| 2150 |
|
| 2151 |
my $library_message = $library_messages->next; |
| 2152 |
like( $library_message->subject, qr/Duplicate Invoice/, 'Library email has correct subject' ); |
| 2153 |
like( $library_message->content, qr/INV00003/, 'Library email contains invoice number' ); |
| 2154 |
like( $library_message->content, qr/Processing has been blocked/, 'Library email contains blocking message' ); |
| 2155 |
|
| 2156 |
# Verify message was recorded in message_queue for audit trail |
| 2157 |
ok( $library_message->message_id, 'Message has ID for audit trail' ); |
| 2158 |
|
| 2159 |
$schema->storage->txn_rollback; |
| 2160 |
}; |
| 2161 |
|
| 2162 |
# Test 5: Vendor email notification |
| 2163 |
subtest 'vendor_email_notification' => sub { |
| 2164 |
plan tests => 5; |
| 2165 |
|
| 2166 |
$schema->storage->txn_begin; |
| 2167 |
|
| 2168 |
# Enable duplicate blocking and email notifications |
| 2169 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 2170 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailNotice', 1 ); |
| 2171 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailAddresses', 'library@example.com' ); |
| 2172 |
|
| 2173 |
# Create letter templates for notifications (delete first if exist) |
| 2174 |
$schema->resultset('Letter')->search( |
| 2175 |
{ |
| 2176 |
module => 'acquisition', |
| 2177 |
code => [ 'EDI_DUP_INV_LIBRARY', 'EDI_DUP_INV_VENDOR' ], |
| 2178 |
} |
| 2179 |
)->delete; |
| 2180 |
|
| 2181 |
$builder->build( |
| 2182 |
{ |
| 2183 |
source => 'Letter', |
| 2184 |
value => { |
| 2185 |
module => 'acquisition', |
| 2186 |
code => 'EDI_DUP_INV_LIBRARY', |
| 2187 |
branchcode => '', |
| 2188 |
name => 'Test library notification', |
| 2189 |
is_html => 0, |
| 2190 |
title => 'Duplicate Invoice - <<invoicenumber>>', |
| 2191 |
content => 'Duplicate invoice <<invoicenumber>>. Processing has been blocked.', |
| 2192 |
message_transport_type => 'email', |
| 2193 |
lang => 'default', |
| 2194 |
} |
| 2195 |
} |
| 2196 |
); |
| 2197 |
$builder->build( |
| 2198 |
{ |
| 2199 |
source => 'Letter', |
| 2200 |
value => { |
| 2201 |
module => 'acquisition', |
| 2202 |
code => 'EDI_DUP_INV_VENDOR', |
| 2203 |
branchcode => '', |
| 2204 |
name => 'Test vendor notification', |
| 2205 |
is_html => 0, |
| 2206 |
title => 'Duplicate Invoice - <<invoicenumber>>', |
| 2207 |
content => 'Duplicate invoice <<invoicenumber>>. Please use UNIQUE invoice number.', |
| 2208 |
message_transport_type => 'email', |
| 2209 |
lang => 'default', |
| 2210 |
} |
| 2211 |
} |
| 2212 |
); |
| 2213 |
|
| 2214 |
# Create file transport for local testing |
| 2215 |
my $file_transport = $builder->build( |
| 2216 |
{ |
| 2217 |
source => 'FileTransport', |
| 2218 |
value => { |
| 2219 |
name => 'Test Invoice Transport', |
| 2220 |
transport => 'local', |
| 2221 |
download_directory => $dirname, |
| 2222 |
upload_directory => $dirname, |
| 2223 |
} |
| 2224 |
} |
| 2225 |
); |
| 2226 |
|
| 2227 |
# Create vendor EDI account |
| 2228 |
my $account = $builder->build( |
| 2229 |
{ |
| 2230 |
source => 'VendorEdiAccount', |
| 2231 |
value => { |
| 2232 |
description => 'test vendor', |
| 2233 |
file_transport_id => $file_transport->{file_transport_id}, |
| 2234 |
plugin => '', |
| 2235 |
san => '5013546027173', |
| 2236 |
} |
| 2237 |
} |
| 2238 |
); |
| 2239 |
|
| 2240 |
# Create vendor contact with EDI error notification enabled |
| 2241 |
my $vendor_contact = $builder->build( |
| 2242 |
{ |
| 2243 |
source => 'Aqcontact', |
| 2244 |
value => { |
| 2245 |
name => 'Test Vendor Contact', |
| 2246 |
email => 'vendor@supplier.com', |
| 2247 |
booksellerid => $account->{vendor_id}, |
| 2248 |
edi_error_notification => 1, |
| 2249 |
} |
| 2250 |
} |
| 2251 |
); |
| 2252 |
|
| 2253 |
# Create test invoice that already exists |
| 2254 |
my $existing_invoice = $builder->build( |
| 2255 |
{ |
| 2256 |
source => 'Aqinvoice', |
| 2257 |
value => { |
| 2258 |
invoicenumber => 'INV00003', |
| 2259 |
booksellerid => $account->{vendor_id}, |
| 2260 |
shipmentdate => '2020-01-01', |
| 2261 |
} |
| 2262 |
} |
| 2263 |
); |
| 2264 |
|
| 2265 |
# Create test basket and order |
| 2266 |
my $basket = $builder->build_object( |
| 2267 |
{ |
| 2268 |
class => 'Koha::Acquisition::Baskets', |
| 2269 |
value => { |
| 2270 |
booksellerid => $account->{vendor_id}, |
| 2271 |
basketname => 'Test Basket', |
| 2272 |
} |
| 2273 |
} |
| 2274 |
); |
| 2275 |
my $order = $builder->build_object( |
| 2276 |
{ |
| 2277 |
class => 'Koha::Acquisition::Orders', |
| 2278 |
value => { |
| 2279 |
basketno => $basket->id, |
| 2280 |
orderstatus => 'new', |
| 2281 |
biblionumber => undef, |
| 2282 |
} |
| 2283 |
} |
| 2284 |
); |
| 2285 |
|
| 2286 |
# Prepare invoice message |
| 2287 |
my $filename = 'INVOICE.CEI'; |
| 2288 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 2289 |
$trans->working_directory($dirname); |
| 2290 |
|
| 2291 |
my $mhash = $trans->message_hash(); |
| 2292 |
$mhash->{message_type} = 'INVOICE'; |
| 2293 |
$trans->ingest( $mhash, $filename ); |
| 2294 |
|
| 2295 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 2296 |
my $raw_msg = $invoice_message->raw_msg; |
| 2297 |
$raw_msg =~ s/ORDERNUMBER1/$order->ordernumber/g; |
| 2298 |
$raw_msg =~ s/ORDERNUMBER2/$order->ordernumber/g; |
| 2299 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 2300 |
|
| 2301 |
# Process the invoice |
| 2302 |
process_invoice($invoice_message); |
| 2303 |
|
| 2304 |
# Verify vendor email was queued in message_queue |
| 2305 |
my $vendor_messages = $schema->resultset('MessageQueue')->search( |
| 2306 |
{ |
| 2307 |
to_address => 'vendor@supplier.com', |
| 2308 |
status => [ 'sent', 'pending', 'failed' ], # Accept any status - queuing is what matters |
| 2309 |
} |
| 2310 |
); |
| 2311 |
is( $vendor_messages->count, 1, 'Vendor notification was queued' ); |
| 2312 |
|
| 2313 |
my $vendor_message = $vendor_messages->next; |
| 2314 |
like( $vendor_message->subject, qr/Duplicate Invoice/, 'Vendor email has correct subject' ); |
| 2315 |
like( $vendor_message->content, qr/INV00003/, 'Vendor email contains invoice number' ); |
| 2316 |
like( $vendor_message->content, qr/UNIQUE invoice number/, 'Vendor email contains action required message' ); |
| 2317 |
|
| 2318 |
# Verify message was recorded for audit trail |
| 2319 |
ok( $vendor_message->message_id, 'Message has ID for audit trail' ); |
| 2320 |
|
| 2321 |
$schema->storage->txn_rollback; |
| 2322 |
}; |
| 2323 |
|
| 2324 |
# Test 6: Multiple email recipients |
| 2325 |
subtest 'multiple_email_recipients' => sub { |
| 2326 |
plan tests => 3; |
| 2327 |
|
| 2328 |
$schema->storage->txn_begin; |
| 2329 |
|
| 2330 |
# Enable duplicate blocking and email notifications with multiple addresses |
| 2331 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 2332 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailNotice', 1 ); |
| 2333 |
t::lib::Mocks::mock_preference( |
| 2334 |
'EdiBlockDuplicateInvoiceEmailAddresses', |
| 2335 |
'library1@example.com, library2@example.com, library3@example.com' |
| 2336 |
); |
| 2337 |
|
| 2338 |
# Create letter templates (delete first if exist) |
| 2339 |
$schema->resultset('Letter')->search( |
| 2340 |
{ |
| 2341 |
module => 'acquisition', |
| 2342 |
code => 'EDI_DUP_INV_LIBRARY', |
| 2343 |
} |
| 2344 |
)->delete; |
| 2345 |
|
| 2346 |
$builder->build( |
| 2347 |
{ |
| 2348 |
source => 'Letter', |
| 2349 |
value => { |
| 2350 |
module => 'acquisition', |
| 2351 |
code => 'EDI_DUP_INV_LIBRARY', |
| 2352 |
branchcode => '', |
| 2353 |
name => 'Test library notification', |
| 2354 |
is_html => 0, |
| 2355 |
title => 'Duplicate Invoice', |
| 2356 |
content => 'Duplicate invoice.', |
| 2357 |
message_transport_type => 'email', |
| 2358 |
lang => 'default', |
| 2359 |
} |
| 2360 |
} |
| 2361 |
); |
| 2362 |
|
| 2363 |
# Create file transport for local testing |
| 2364 |
my $file_transport = $builder->build( |
| 2365 |
{ |
| 2366 |
source => 'FileTransport', |
| 2367 |
value => { |
| 2368 |
name => 'Test Invoice Transport', |
| 2369 |
transport => 'local', |
| 2370 |
download_directory => $dirname, |
| 2371 |
upload_directory => $dirname, |
| 2372 |
} |
| 2373 |
} |
| 2374 |
); |
| 2375 |
|
| 2376 |
# Create vendor EDI account |
| 2377 |
my $account = $builder->build( |
| 2378 |
{ |
| 2379 |
source => 'VendorEdiAccount', |
| 2380 |
value => { |
| 2381 |
description => 'test vendor', |
| 2382 |
file_transport_id => $file_transport->{file_transport_id}, |
| 2383 |
plugin => '', |
| 2384 |
san => '5013546027173', |
| 2385 |
} |
| 2386 |
} |
| 2387 |
); |
| 2388 |
|
| 2389 |
# Create test invoice that already exists |
| 2390 |
my $existing_invoice = $builder->build( |
| 2391 |
{ |
| 2392 |
source => 'Aqinvoice', |
| 2393 |
value => { |
| 2394 |
invoicenumber => 'INV00003', |
| 2395 |
booksellerid => $account->{vendor_id}, |
| 2396 |
} |
| 2397 |
} |
| 2398 |
); |
| 2399 |
|
| 2400 |
# Create test basket and order |
| 2401 |
my $basket = $builder->build_object( |
| 2402 |
{ |
| 2403 |
class => 'Koha::Acquisition::Baskets', |
| 2404 |
value => { |
| 2405 |
booksellerid => $account->{vendor_id}, |
| 2406 |
basketname => 'Test Basket', |
| 2407 |
} |
| 2408 |
} |
| 2409 |
); |
| 2410 |
my $order = $builder->build_object( |
| 2411 |
{ |
| 2412 |
class => 'Koha::Acquisition::Orders', |
| 2413 |
value => { |
| 2414 |
basketno => $basket->id, |
| 2415 |
orderstatus => 'new', |
| 2416 |
biblionumber => undef, |
| 2417 |
} |
| 2418 |
} |
| 2419 |
); |
| 2420 |
|
| 2421 |
# Prepare invoice message |
| 2422 |
my $filename = 'INVOICE.CEI'; |
| 2423 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 2424 |
$trans->working_directory($dirname); |
| 2425 |
|
| 2426 |
my $mhash = $trans->message_hash(); |
| 2427 |
$mhash->{message_type} = 'INVOICE'; |
| 2428 |
$trans->ingest( $mhash, $filename ); |
| 2429 |
|
| 2430 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 2431 |
my $raw_msg = $invoice_message->raw_msg; |
| 2432 |
$raw_msg =~ s/ORDERNUMBER1/$order->ordernumber/g; |
| 2433 |
$raw_msg =~ s/ORDERNUMBER2/$order->ordernumber/g; |
| 2434 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 2435 |
|
| 2436 |
# Process the invoice |
| 2437 |
process_invoice($invoice_message); |
| 2438 |
|
| 2439 |
# Verify all three library emails were queued |
| 2440 |
my $library_messages = $schema->resultset('MessageQueue')->search( |
| 2441 |
{ |
| 2442 |
to_address => [ 'library1@example.com', 'library2@example.com', 'library3@example.com' ], |
| 2443 |
status => [ 'sent', 'pending', 'failed' ], # Accept any status - queuing is what matters |
| 2444 |
} |
| 2445 |
); |
| 2446 |
is( $library_messages->count, 3, 'Three library notification emails queued' ); |
| 2447 |
|
| 2448 |
my @to_addresses = map { $_->to_address } $library_messages->all; |
| 2449 |
ok( ( grep { $_ eq 'library1@example.com' } @to_addresses ), 'Email sent to library1' ); |
| 2450 |
ok( ( grep { $_ eq 'library2@example.com' } @to_addresses ), 'Email sent to library2' ); |
| 2451 |
|
| 2452 |
$schema->storage->txn_rollback; |
| 2453 |
}; |
| 2454 |
|
| 2455 |
# Test 7: Invalid email handling |
| 2456 |
subtest 'invalid_email_handling' => sub { |
| 2457 |
plan tests => 3; |
| 2458 |
|
| 2459 |
$schema->storage->txn_begin; |
| 2460 |
|
| 2461 |
# Enable duplicate blocking and email notifications with invalid addresses |
| 2462 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoice', 1 ); |
| 2463 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailNotice', 1 ); |
| 2464 |
t::lib::Mocks::mock_preference( 'EdiBlockDuplicateInvoiceEmailAddresses', 'invalid-email, valid@example.com' ); |
| 2465 |
|
| 2466 |
# Create letter templates (delete first if exist) |
| 2467 |
$schema->resultset('Letter')->search( |
| 2468 |
{ |
| 2469 |
module => 'acquisition', |
| 2470 |
code => [ 'EDI_DUP_INV_LIBRARY', 'EDI_DUP_INV_VENDOR' ], |
| 2471 |
} |
| 2472 |
)->delete; |
| 2473 |
|
| 2474 |
$builder->build( |
| 2475 |
{ |
| 2476 |
source => 'Letter', |
| 2477 |
value => { |
| 2478 |
module => 'acquisition', |
| 2479 |
code => 'EDI_DUP_INV_LIBRARY', |
| 2480 |
branchcode => '', |
| 2481 |
name => 'Test library notification', |
| 2482 |
is_html => 0, |
| 2483 |
title => 'Duplicate Invoice', |
| 2484 |
content => 'Duplicate invoice.', |
| 2485 |
message_transport_type => 'email', |
| 2486 |
lang => 'default', |
| 2487 |
} |
| 2488 |
} |
| 2489 |
); |
| 2490 |
$builder->build( |
| 2491 |
{ |
| 2492 |
source => 'Letter', |
| 2493 |
value => { |
| 2494 |
module => 'acquisition', |
| 2495 |
code => 'EDI_DUP_INV_VENDOR', |
| 2496 |
branchcode => '', |
| 2497 |
name => 'Test vendor notification', |
| 2498 |
is_html => 0, |
| 2499 |
title => 'Duplicate Invoice', |
| 2500 |
content => 'Duplicate invoice.', |
| 2501 |
message_transport_type => 'email', |
| 2502 |
lang => 'default', |
| 2503 |
} |
| 2504 |
} |
| 2505 |
); |
| 2506 |
|
| 2507 |
# Create file transport for local testing |
| 2508 |
my $file_transport = $builder->build( |
| 2509 |
{ |
| 2510 |
source => 'FileTransport', |
| 2511 |
value => { |
| 2512 |
name => 'Test Invoice Transport', |
| 2513 |
transport => 'local', |
| 2514 |
download_directory => $dirname, |
| 2515 |
upload_directory => $dirname, |
| 2516 |
} |
| 2517 |
} |
| 2518 |
); |
| 2519 |
|
| 2520 |
# Create vendor EDI account |
| 2521 |
my $account = $builder->build( |
| 2522 |
{ |
| 2523 |
source => 'VendorEdiAccount', |
| 2524 |
value => { |
| 2525 |
description => 'test vendor', |
| 2526 |
file_transport_id => $file_transport->{file_transport_id}, |
| 2527 |
plugin => '', |
| 2528 |
san => '5013546027173', |
| 2529 |
} |
| 2530 |
} |
| 2531 |
); |
| 2532 |
|
| 2533 |
# Create vendor contact with invalid email |
| 2534 |
my $vendor_contact = $builder->build( |
| 2535 |
{ |
| 2536 |
source => 'Aqcontact', |
| 2537 |
value => { |
| 2538 |
name => 'Test Vendor Contact', |
| 2539 |
email => 'not-an-email', |
| 2540 |
booksellerid => $account->{vendor_id}, |
| 2541 |
edi_error_notification => 1, |
| 2542 |
} |
| 2543 |
} |
| 2544 |
); |
| 2545 |
|
| 2546 |
# Create test invoice that already exists |
| 2547 |
my $existing_invoice = $builder->build( |
| 2548 |
{ |
| 2549 |
source => 'Aqinvoice', |
| 2550 |
value => { |
| 2551 |
invoicenumber => 'INV00003', |
| 2552 |
booksellerid => $account->{vendor_id}, |
| 2553 |
} |
| 2554 |
} |
| 2555 |
); |
| 2556 |
|
| 2557 |
# Create test basket and order |
| 2558 |
my $basket = $builder->build_object( |
| 2559 |
{ |
| 2560 |
class => 'Koha::Acquisition::Baskets', |
| 2561 |
value => { |
| 2562 |
booksellerid => $account->{vendor_id}, |
| 2563 |
basketname => 'Test Basket', |
| 2564 |
} |
| 2565 |
} |
| 2566 |
); |
| 2567 |
my $order = $builder->build_object( |
| 2568 |
{ |
| 2569 |
class => 'Koha::Acquisition::Orders', |
| 2570 |
value => { |
| 2571 |
basketno => $basket->id, |
| 2572 |
orderstatus => 'new', |
| 2573 |
biblionumber => undef, |
| 2574 |
} |
| 2575 |
} |
| 2576 |
); |
| 2577 |
|
| 2578 |
# Prepare invoice message |
| 2579 |
my $filename = 'INVOICE.CEI'; |
| 2580 |
my $trans = Koha::Edifact::Transport->new( $account->{id} ); |
| 2581 |
$trans->working_directory($dirname); |
| 2582 |
|
| 2583 |
my $mhash = $trans->message_hash(); |
| 2584 |
$mhash->{message_type} = 'INVOICE'; |
| 2585 |
$trans->ingest( $mhash, $filename ); |
| 2586 |
|
| 2587 |
my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); |
| 2588 |
my $raw_msg = $invoice_message->raw_msg; |
| 2589 |
$raw_msg =~ s/ORDERNUMBER1/$order->ordernumber/g; |
| 2590 |
$raw_msg =~ s/ORDERNUMBER2/$order->ordernumber/g; |
| 2591 |
$invoice_message->update( { raw_msg => $raw_msg } ); |
| 2592 |
|
| 2593 |
# Clear logger |
| 2594 |
$logger->clear(); |
| 2595 |
|
| 2596 |
# Process the invoice |
| 2597 |
process_invoice($invoice_message); |
| 2598 |
|
| 2599 |
# Verify only valid email was queued (invalid email skipped) |
| 2600 |
my $valid_messages = $schema->resultset('MessageQueue')->search( |
| 2601 |
{ |
| 2602 |
to_address => 'valid@example.com', |
| 2603 |
status => [ 'sent', 'pending', 'failed' ], # Accept any status - queuing is what matters |
| 2604 |
} |
| 2605 |
); |
| 2606 |
is( $valid_messages->count, 1, 'Only valid library email queued' ); |
| 2607 |
|
| 2608 |
# Verify no message for invalid email |
| 2609 |
my $invalid_messages = $schema->resultset('MessageQueue')->search( |
| 2610 |
{ |
| 2611 |
to_address => 'invalid-email', |
| 2612 |
} |
| 2613 |
); |
| 2614 |
is( $invalid_messages->count, 0, 'No message queued for invalid email' ); |
| 2615 |
|
| 2616 |
# Verify invalid vendor contact email was logged |
| 2617 |
$logger->warn_like( |
| 2618 |
qr/Invalid vendor contact email address/, |
| 2619 |
'Warning logged for invalid vendor contact email' |
| 2620 |
); |
| 2621 |
|
| 2622 |
$logger->clear(); |
| 2623 |
$schema->storage->txn_rollback; |
| 2624 |
}; |
| 2625 |
|
| 2626 |
$schema->storage->txn_rollback; |
| 2627 |
}; |