2019-07-02 15:21:01 +01:00
package Pear::LocalLoop::Import::LCCCsv::Transactions ;
use Moo ;
2019-07-05 16:52:32 +01:00
use DateTime ;
use DateTime::Format::Strptime ;
2019-07-02 15:21:01 +01:00
2019-09-02 15:34:28 +01:00
use Geo::UK::Postcode::Regex ;
2019-07-02 15:21:01 +01:00
extends qw/Pear::LocalLoop::Import::LCCCsv/ ;
2019-07-14 15:15:14 +01:00
has target_entity_id = > (
is = > 'ro' ,
required = > 1 ,
) ;
has target_entity = > (
is = > 'lazy' ,
builder = > sub {
my $ self = shift ;
my $ entity = $ self - > schema - > resultset ( 'Entity' ) - > find ( $ self - > target_entity_id ) ;
Pear::LocalLoop::Error - > throw ( "Cannot find LCC Entity, did you pass the right id?" ) unless $ entity ;
return $ entity ;
} ,
) ;
2019-07-05 16:52:32 +01:00
has '+csv_required_columns' = > (
2019-07-14 15:15:14 +01:00
builder = > sub { return [ (
'transaction_id' ,
'supplier_id' ,
'net_amount' ,
'vat amount' ,
'gross_amount' ,
) ] } ,
2019-07-05 16:52:32 +01:00
) ;
sub import_csv {
my ( $ self ) = @ _ ;
2019-09-11 14:19:12 +01:00
$ self - > check_headers ;
2019-07-14 15:15:14 +01:00
my $ lcc_org = $ self - > target_entity ;
2019-09-11 14:19:12 +01:00
while ( my $ row = $ self - > get_csv_line ) {
2019-07-10 17:23:27 +01:00
$ self - > _row_to_result ( $ row , $ lcc_org ) ;
2019-07-05 16:52:32 +01:00
}
}
sub _row_to_result {
2019-07-14 15:15:14 +01:00
my ( $ self , $ row , $ lcc_org ) = @ _ ;
2019-07-05 16:52:32 +01:00
2019-07-14 15:15:14 +01:00
my $ supplier_id = $ row - > { supplier_id } ;
2019-07-05 17:56:21 +01:00
2019-07-14 15:15:14 +01:00
my $ organisation = $ self - > schema - > resultset ( 'Organisation' ) - > find ( {
'external_reference.external_id' = > $ supplier_id
} , { join = > 'external_reference' } ) ;
2019-07-05 17:56:21 +01:00
2019-07-14 15:15:14 +01:00
unless ( $ organisation ) {
2019-09-02 15:34:28 +01:00
# Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id");
2019-09-06 17:31:09 +01:00
return unless $ row - > { 'Company Name (WHO)' } ;
2019-09-02 15:34:28 +01:00
my $ town = $ row - > { post_town } ;
unless ( $ town ) {
my $ postcode_obj = Geo::UK::Postcode::Regex - > parse ( $ row - > { post_code } ) ;
$ town = Geo::UK::Postcode::Regex - > outcode_to_posttowns ( $ postcode_obj - > { outcode } ) ;
$ town = $ town - > [ 0 ] ;
}
return if $ self - > external_result - > organisations - > find ( { external_id = > $ row - > { supplier_id } } ) ;
$ organisation = $ self - > schema - > resultset ( 'Entity' ) - > create ( {
type = > 'organisation' ,
organisation = > {
2019-09-06 17:31:09 +01:00
name = > $ row - > { 'Company Name (WHO)' } ,
2019-09-02 15:34:28 +01:00
street_name = > $ row - > { "address line 1" } ,
town = > $ town ,
postcode = > $ row - > { post_code } ,
country = > $ row - > { country_code } ,
external_reference = > [ {
external_reference = > $ self - > external_result ,
external_id = > $ row - > { supplier_id } ,
} ] ,
}
} ) ;
2019-07-14 15:15:14 +01:00
}
2019-07-05 16:52:32 +01:00
2019-07-14 15:15:14 +01:00
my $ date_formatter = DateTime::Format::Strptime - > new (
pattern = > '%m/%d/%Y' ,
time_zone = > 'Europe/London'
) ;
2019-09-11 15:02:08 +01:00
2019-09-06 17:31:09 +01:00
my $ paid_date = ( $ row - > { paid_date } ?
$ date_formatter - > parse_datetime ( $ row - > { paid_date } ) :
$ date_formatter - > parse_datetime ( $ row - > { invoice_date } ) ) ;
2019-07-05 16:52:32 +01:00
2019-07-14 15:15:14 +01:00
my $ gross_value = $ row - > { gross_amount } ;
$ gross_value =~ s/,//g ;
my $ sales_tax_value = $ row - > { "vat amount" } ;
$ sales_tax_value =~ s/,//g ;
my $ net_value = $ row - > { net_amount } ;
$ net_value =~ s/,//g ;
# TODO negative values are sometimes present
2019-09-06 17:31:09 +01:00
my $ external_transaction = $ self - > external_result - > update_or_create_related ( 'transactions' , { # This is a TransactionExternal result
2019-07-14 15:15:14 +01:00
external_id = > $ row - > { transaction_id } ,
2019-09-06 17:31:09 +01:00
} ) ;
my $ transaction_result = $ external_transaction - > update_or_create_related ( 'transaction' , {
seller = > $ organisation - > entity ,
buyer = > $ lcc_org ,
purchase_time = > $ paid_date ,
value = > $ gross_value * 100000 ,
} ) ;
my $ meta_result = $ transaction_result - > update_or_create_related ( 'meta' , {
gross_value = > $ gross_value * 100000 ,
sales_tax_value = > $ sales_tax_value * 100000 ,
net_value = > $ net_value * 100000 ,
( $ row - > { "local service" } ? ( local_service = > $ row - > { "local service" } ) : ( ) ) ,
( $ row - > { "regional service" } ? ( regional_service = > $ row - > { "regional service" } ) : ( ) ) ,
( $ row - > { "national service" } ? ( national_service = > $ row - > { "national service" } ) : ( ) ) ,
( $ row - > { "private household rebate" } ? ( private_household_rebate = > $ row - > { "private household rebate" } ) : ( ) ) ,
( $ row - > { "business tax and rebate" } ? ( business_tax_and_rebate = > $ row - > { "business tax and rebate" } ) : ( ) ) ,
( $ row - > { "stat loc gov" } ? ( stat_loc_gov = > $ row - > { "stat loc gov" } ) : ( ) ) ,
( $ row - > { "central loc gov" } ? ( central_loc_gov = > $ row - > { "central loc gov" } ) : ( ) ) ,
2019-07-14 15:15:14 +01:00
} ) ;
2019-07-02 15:21:01 +01:00
}
2019-07-05 16:52:32 +01:00
1 ;