use strict;

return sub
{
	my $dbh = shift;

	{
		my $sth = $dbh->prepare("select distinct c.id as ccs_id,v.order_id from jin_ccs c inner join jin_funnel_visit v on c.user_id=v.user_id and (c.ts=v.tstamp or c.ts=date_add(v.tstamp, interval 1 second)) and action='collectccdata'");
		$sth->execute;
	
		my $sth1 = $dbh->prepare("update jin_sale set ccs_id=? where order_id=?");
	
		while (my $row = $sth->fetchrow_hashref)
		{
			$sth1->execute($row->{ccs_id}, $row->{order_id});
		}
	}

	{
		my $sth = $dbh->prepare("select j.subscr_id,s.txn_code from jin_jvmsub j inner join jin_sale s on j.subscr_id=s.subscr_id and s.paid=1 and s.subscr_refsale=1 and j.refsale_next_ref_id='' order by j.id,s.subscr_inst_number desc");
		$sth->execute;
	
		my $sth1 = $dbh->prepare("update jin_jvmsub set refsale_next_ref_id=? where subscr_id=?");
	
		my $curr_subscr_id;
	
		while (my $row = $sth->fetchrow_hashref)
		{
			next
				if $row->{subscr_id} eq $curr_subscr_id;
				
			$curr_subscr_id = $row->{subscr_id};
			
			$sth1->execute($row->{txn_code}, $row->{subscr_id});
		}
	}

	return;
};


