$contract_balance=$dbh->selectrow_hashref('select * from contract_balances where id=?',undef,$dbh->last_insert_id(undef,'billing','contract_balances','id'));
#$contract_balance = $dbh->selectrow_hashref('select * from contract_balances where id=?',undef,);
$contract_balance=$dbh->selectrow_hashref('select * from contract_balances where contract_id=? and date(start)=? and date(end)=?',undef,$client_contract->{id},$stime->ymd,$etime->ymd);
$invoice=$dbh->selectrow_hashref('select * from invoices where contract_id=? and date(period_start)=? and date(period_end)=?',undef,$contract_id,$stime->ymd,$etime->ymd);
}
if(!$invoice){
$dbh->do('insert into invoices(contract_id,period_start,period_end)values(?,?,?)',undef,$contract_id,$stime->ymd,$stime->ymd);
my$serial_tmp="tmp".time.int(rand(99999));
$dbh->do('insert into invoices(contract_id,period_start,period_end,serial)values(?,?,?,?)',undef,$contract_id,$stime->ymd,$stime->ymd,$serial_tmp);