#!/usr/bin/perl

package API::Warehouse::Picking;

use strict;
use warnings;

use Date::Format;

use API::Order::Cart;

our $Config;
BEGIN {
	use API::Config;
	$Config = $API::Config::Config;
}
use lib ($Config->{'Website_libs'});

use ECS::Config;
use ECS::Order;
use ECS::Shipping::Courier;
use ECS::Site;
use ECS::SQL;
use ECS::Time;
use ECS::Warehouse::Picking;
use ECS::Warehouse::Picking::OSOOB;
use ECS::Warehouse::LPN;

my %STATION_PRINTERS = (
	1 => 'pickstationsouth',
	2 => 'pickstationnorth',
	3 => 'Eric_Fox',
);

return 1;

###############################################################################

sub getOrdersSummary { 
	my $self = shift;
	my $input = shift;
	
	my $response = {
		'success'   => 1,
		'error'     => undef,
		'update_DB' => $Config->{'update_DB'},
	};
	
	unless ($input->{'picker'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No picker specified';
		return $response;
	}
	
	my $p = {
		'DBH'    => ECS::SQL::connect(),
		'DBHr'   => ECS::SQL::connect('SLAVE'),
		'picker' => $input->{'picker'},
	};
	
	my $orderIDs = ECS::Warehouse::Picking::getPickerOrderIDs($p);
	
	unless (@{ $orderIDs }) {
		$response->{'data'}->{'orders'} = [];
		return $response;
	}
	
	my $orders = ECS::Order->loadMulti({
		'DBH'     => $p->{'DBHr'},
		'orderID' => $orderIDs,
	});
	
	$p->{'input'}->{'orderIDs'} = $orderIDs;
	my $orders_cart_products = API::Order::Cart::getCartProductsSummary($p);
	
	my @summaries = ();
	for my $orderID (@{ $orderIDs }) {
		my $order = $orders->{$orderID};
		
		$order->scrub;
		
		my $first_productID;
		my $cart_prods =
			($orders_cart_products->{$order->orderID} || []);
		
		if (my $cart_prod = $cart_prods->[0]) {
			$first_productID = $cart_prod->{'productID'};
		}
		
		push(@summaries, {
			'orderID'        => $order->orderID,
			'site'           => $order->site->name,
			'pretty_refID'   => $order->pretty_refID,
			'status'         => $order->status,
			'order_time'     => time2str(
				'%L/%d %l:%M%p',
				ECS::Time::datetimeToEpoch($order->{'order_time'})
			),
			'first_name'     => $order->{'billing_firstname'},
			'last_name'      => $order->{'billing_lastname'},
			'ECS_notes'      => $order->{'ECS_notes'},
			'shipping_type'  => ECS::Shipping::Courier::getServiceLabel({
				'DBH'   => $p->{'DBH'},
				'input' => $order->shipping,
			}),
			'firstProductID' => $first_productID,
		});
	}
	
	$response->{'data'}->{'orders'} = \@summaries;
	
	return $response;
}

###############################################################################

sub getPickLists {
	my $self = shift;
	my $input = shift;
	
	my $response = {
		'success'   => 1,
		'error'     => undef,
		'update_DB' => $Config->{'update_DB'},
	};
	
	unless ($input->{'picker'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No picker specified';
		return $response;
	}
	
	unless ($input->{'warehousePickStationID'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No Warehouse Pick Station ID specified';
		return $response;
	}
	
	my $p = {
		'DBH'                    => ECS::SQL::connect(),
		'DBHr'                   => ECS::SQL::connect('SLAVE'),
		'picker'                 => $input->{'picker'},
		'warehousePickStationID' => $input->{'warehousePickStationID'},
	};
	
	my $orderIDs = ECS::Warehouse::Picking::getPickerOrderIDs($p);
	
	unless (@{ $orderIDs }) {
		$response->{'data'}->{'pickLists'} = [];
		return $response;
	}
	
	my $orders = ECS::Order->loadMulti({
		'DBH'     => $p->{'DBHr'},
		'orderID' => $orderIDs,
	});
	
	if ($ECS::Config::c{'sandbox'}) {
		$response->{'data'}->{'labelPrinter'} = 'labelproduct';
	} else {
		$response->{'data'}->{'labelPrinter'} =
			$STATION_PRINTERS{$input->{'warehousePickStationID'}};
	}
	
	$p->{'input'}->{'orderIDs'} = $orderIDs;
	local $p->{picking} = 1;
	my $orders_cart_products = API::Order::Cart::getCartProductsSummary($p);
	my $orders_indv_products = API::Order::Cart::getIndividualProducts($p);
	
	my %orders_picklist_labels = ();
	
	for my $orderID (@{ $orderIDs }) {
		my $order = $orders->{$orderID};
		
		$order->scrub;
		
		local $p->{'order'} = $order;
		local $p->{'cartProducts'} = (
			$orders_cart_products->{$order->orderID} || []
		);
		local $p->{'individualProducts'} = (
			$orders_indv_products->{$order->orderID} || []
		);
		local $p->{'pickItems'} =
			ECS::Warehouse::Picking::getOrderProductLocations($p);
		
		$orders_picklist_labels{$order->orderID} =
			ECS::Warehouse::Picking::getOrderPickListLabel($p);
	}
	
	$response->{'data'}->{'pickLists'} = [map {{
		'orderID'       => $_,
		'pickListLabel' => $orders_picklist_labels{$_},
	}} @{ $orderIDs }];
	
	return $response;
}

###############################################################################

sub getOsoobTicket { 
	my $self = shift;
	my $input = shift;
	
	my $response = {
		'success'   => 1,
		'error'     => undef,
		'update_DB' => $Config->{'update_DB'},
	};
	
	$input->{site} ||= $ECS::Site::PRIMARY_SITE;
	unless ($input->{'picker'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No picker specified';
		return $response;
	}
	
	unless ($input->{'warehousePickStationID'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No Warehouse Pick Station ID specified';
		return $response;
	}
	
	my $p = {
		'DBH'    => ECS::SQL::connect(),
		'DBHr'   => ECS::SQL::connect('SLAVE'),
		'picker' => $input->{'picker'},
	};
	
	my $orderIDs = ECS::Warehouse::Picking::getPickerOrderIDs($p);
	
	if (@{ $orderIDs }) {
		
		my $orders = ECS::Order->loadMulti({
			'DBH'     => $p->{'DBH'},
			'orderID' => $orderIDs,
		});
		
		my @filtered_orderIDs = ();
		
		for my $orderID (keys %{ $orders }) {
			push(@filtered_orderIDs, $orderID)
				if $orders->{$orderID}->status eq 'Picking OSOOB'
					and $orders->{$orderID}->site->name eq $input->{site};
		}
		
		$orderIDs = \@filtered_orderIDs;
	}
	
	unless (@{ $orderIDs }) {
		$response->{'success'} = 0;
		$response->{'error'} = "No OSOOB ($input->{site}) Orders for Ticket";
		return $response;
	}
	
	if ($ECS::Config::c{'sandbox'}) {
		$response->{'data'}->{'labelPrinter'} = 'labelproduct';
	} else {
		$response->{'data'}->{'labelPrinter'} =
			$STATION_PRINTERS{$input->{'warehousePickStationID'}};
	}
	
	$p->{'input'}->{'orderIDs'} = $orderIDs;
	$p->{'individualProducts'} = API::Order::Cart::getIndividualProducts($p);
	$p->{site} = $input->{site};
	
	$response->{'data'}->{'osoobTicketLabel'} =
		ECS::Warehouse::Picking::OSOOB::getTicketLabel($p);
	
	return $response;
}

###############################################################################

sub getParkingSpotTicket{
	my $self = shift;
	my $input = shift;
	
	my $response = {
		'success'   => 1,
		'error'     => undef,
		'update_DB' => $Config->{'update_DB'},
	};
	
	$input->{site} ||= $ECS::Site::PRIMARY_SITE;
	unless ($input->{'picker'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No picker specified';
		return $response;
	}
	
	unless ($input->{'warehousePickStationID'}) {
		$response->{'success'} = 0;
		$response->{'error'} = 'No Warehouse Pick Station ID specified';
		return $response;
	}
	
	my $p = {
		'DBH'    => ECS::SQL::connect(),
		'DBHr'   => ECS::SQL::connect('SLAVE'),
		'picker' => $input->{'picker'},
	};
	
	my $orderIDs = ECS::Warehouse::Picking::getPickerOrderIDs($p);
	
	unless (@{ $orderIDs }) {
		$response->{'success'} = 0;
		$response->{'error'} = "No Parking Spot Orders for Ticket";
		return $response;
	}
	
	if ($ECS::Config::c{'sandbox'}) {
		$response->{'data'}->{'labelPrinter'} = 'labelproduct';
	} else {
		$response->{'data'}->{'labelPrinter'} =
			$STATION_PRINTERS{$input->{'warehousePickStationID'}};
	}
	
	my $orders = ECS::Order->loadMulti({
		'DBH'     => $p->{'DBHr'},
		'orderID' => $orderIDs,
	});
	
	my @orderIDs = keys %$orders;
	my $lpns =  ECS::Warehouse::LPN::getOrderLPN({
		'dbh'=> $p->{DBHr}, 
		'orderIDs' => \@orderIDs 
	});
	
	unless (scalar keys %$lpns) {
		$response->{'success'} = 0;
		$response->{'error'} = 'Orders are not associated with any LPN';
		return $response;
	}
	
	my @data = ();
	foreach my $orderID ( keys %$orders ){
		next if(!exists $lpns->{$orderID} );
		next if( !defined $orders->{$orderID}->orderLocation 
				 || !defined $orders->{$orderID}->orderLocation->locID );
		
		my $loc = ECS::Warehouse::Location->new({
			'dbh'    => $p->{'DBH'},
			'locID'  => $orders->{$orderID}->orderLocation->locID,			
		});
		
		push @data, {
			'locString' => $loc->toString,
			'lpnID'     => $lpns->{$orderID}->{lpnID},
			'site'      => $orders->{$orderID}->site->name,
		};	
	}
	
	$p->{'input'}->{'data'} = \@data;
	
	$response->{'data'}->{'PKSTicketLabel'} =
		ECS::Warehouse::Picking::getParkingSpotTicketLabel($p);
	
	return $response;
}

###############################################################################

