use strict; use warnings;
my $NEW_MSG_RE = '^UNH';
+my $NEW_LIN_RE = '^LIN\+';
my $MSG_TYPE_RE = '^UNH\+\d+\+(\S{6}):.*'; # ORDRDP, INVOIC, ...
my $INV_IDENT_RE = '^BGM\+380\+(.*)\+.*';
my $PO_NUM_RE = '^RFF\+ON:(\S+)';
my $LIN_INDEX_RE = '^LIN\+([^\+]+).*';
my $LIN_IDENT_RE = '^LIN\+\S+\++([^:]+).*'; # e.g. ISBN LIN+1++9780786222735:EN
my $LIN_IDENT_2RE = '^PIA\+0*5\+([^:]+).*'; # e.g. ISBN PIA+05+1594097801:IB
-my $LIN_QUANTITY_RE = '^QTY\+47:(\d+).*';
-my $LIN_AMOUNT_RE = '^MOA\+203:(\d+)';
+my $LIN_NUM_INVOICED_RE = '^QTY\+47:(\d+).*';
+my $LIN_NUM_ORDERED_RE = '^QTY\+21:(\d+).*';
+my $LIN_NUM_DISPATCH_RE = '^QTY\+12:(\d+).*';
+my $LIN_NUM_BACKORDER_RE= '^QTY\+83:(\d+).*';
+my $LIN_NUM_DELIVERED_RE= '^QTY\+46:(\d+).*';
+my $LIN_AMOUNT_BILLED_RE = '^MOA\+203:(\d+)';
+my $LIN_UNIT_PRICE_RE = '^PRI\+AAB:(\d+).*'; # PRI+AAB:49::SRP
my $LIN_ID_RE = '^RFF\+LI:\S+\/(\S+)';
my $TOTAL_BILLED_RE = '^MOA\+86:(\d+)';
my $MISC_CHARGE_TYPE_RE = '^ALC\+C\++([^\+]+).*';
($msg->{buyer_san} = $_) =~ s/$BUYER_SAN_RE/$1/g if /$BUYER_SAN_RE/;
($msg->{vendor_san} = $_) =~ s/$VENDOR_SAN_RE/$1/g if /$VENDOR_SAN_RE/;
+ ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/;
- if ($msg->{msg_type} eq 'INVOIC') {
+ if ($_ =~ /$NEW_LIN_RE/) { # starting a new lineitem
- ($msg->{inv_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/;
- ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/;
+ $msg->{_current_li} = {};
+ ($msg->{_current_li}->{index} = $_) =~ s/$LIN_INDEX_RE/$1/g;
+ ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_RE/$1/g if /$LIN_IDENT_RE/;
- if ($_ =~ /^LIN\+/) { # starting a new lineitem
+ push(@{$msg->{lineitems}}, $msg->{_current_li});
+ }
+
+ if ($msg->{_current_li}) {
+ ($msg->{_current_li}->{id} = $_) =~ s/$LIN_ID_RE/$1/g if /$LIN_ID_RE/;
+ ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_2RE/$1/g if /$LIN_IDENT_2RE/;
- $msg->{_current_li} = {};
- ($msg->{_current_li}->{index} = $_) =~ s/$LIN_INDEX_RE/$1/g;
- ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_RE/$1/g if /$LIN_IDENT_RE/;
+ # counts
+ ($msg->{_current_li}->{invoice_count} = $_) =~ s/$LIN_NUM_INVOICED_RE/$1/g if /$LIN_NUM_INVOICED_RE/;
+ ($msg->{_current_li}->{order_count} = $_) =~ s/$LIN_NUM_ORDERED_RE/$1/g if /$LIN_NUM_ORDERED_RE/;
+ ($msg->{_current_li}->{dispatch_count} = $_) =~ s/$LIN_NUM_DISPATCH_RE/$1/g if /$LIN_NUM_DISPATCH_RE/;
+ ($msg->{_current_li}->{backorder_count} = $_) =~ s/$LIN_NUM_BACKORDER_RE/$1/g if /$LIN_NUM_BACKORDER_RE/;
+ ($msg->{_current_li}->{delivered_count} = $_) =~ s/$LIN_NUM_DELIVERED_RE/$1/g if /$LIN_NUM_DELIVERED_RE/;
- push(@{$msg->{lineitems}}, $msg->{_current_li});
- }
+ # prices
+ ($msg->{_current_li}->{amount_billed} = $_) =~ s/$LIN_AMOUNT_BILLED_RE/$1/g if /$LIN_AMOUNT_BILLED_RE/;
+ ($msg->{_current_li}->{unit_price} = $_) =~ s/$LIN_UNIT_PRICE_RE/$1/g if /$LIN_UNIT_PRICE_RE/;
+ }
- if ($msg->{_current_li}) {
- ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_2RE/$1/g if /$LIN_IDENT_2RE/;
- ($msg->{_current_li}->{quantity} = $_) =~ s/$LIN_QUANTITY_RE/$1/g if /$LIN_QUANTITY_RE/;
- ($msg->{_current_li}->{amount} = $_) =~ s/$LIN_AMOUNT_RE/$1/g if /$LIN_AMOUNT_RE/;
- ($msg->{_current_li}->{id} = $_) =~ s/$LIN_ID_RE/$1/g if /$LIN_ID_RE/;
- }
+ # primarily for invoices
- ($msg->{total_billed} = $_) =~ s/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/;
+ ($msg->{invoice_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/;
+ ($msg->{total_billed} = $_) =~ s/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/;
- if (/$MISC_CHARGE_TYPE_RE/) {
- (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g;
- push (@{$msg->{misc_charges}}, {type => $type});
- }
+ if (/$MISC_CHARGE_TYPE_RE/) {
+ (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g;
+ push (@{$msg->{misc_charges}}, {type => $type});
+ }
- if (/$MISC_CHARGE_AMT_RE/) {
- my $chg = $msg->{misc_charges}[-1];
- ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g;
- }
+ if (/$MISC_CHARGE_AMT_RE/) {
+ my $chg = $msg->{misc_charges}[-1];
+ ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g;
}
}