Passed
Push — master ( 7c1346...db1ea0 )
by Aimeos
05:00
created

OrderUpdateInvoiceNo   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 1
b 0
f 0
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A after() 0 3 1
A up() 0 29 4
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023-2024
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
class OrderUpdateInvoiceNo extends Base
13
{
14
	public function after() : array
15
	{
16
		return ['Locale', 'Order'];
17
	}
18
19
20
	public function up()
21
	{
22
		$db = $this->db( 'db-order' );
23
		$db2 = $this->db( 'db-locale' );
24
25
		if( !$db->hasColumn( 'mshop_order', 'invoiceno' ) || !$db2->hasColumn( 'mshop_locale_site', 'invoiceno' ) ) {
26
			return;
27
		}
28
29
		$this->info( 'Set invoice ID in order table if empty', 'vv' );
30
31
		$db->stmt()->update( 'mshop_order' )
32
			->set( $db->qi( 'invoiceno' ), $db->qi( 'id' ) )
33
			->where( $db->qi( 'invoiceno' ) . ' = \'\'' )
34
			->executeStatement();
35
36
		$result = $db->stmt()
37
			->select( 'siteid', 'MAX(' . $db->qi( 'invoiceno' ) . ') AS maxnum' )
38
			->from( 'mshop_order' )
39
			->groupBy( 'siteid' )
40
			->executeQuery();
41
42
		while( $row = $result->fetchAssociative() )
43
		{
44
			$db2->stmt()->update( 'mshop_locale_site' )
45
				->set( 'invoiceno', '?' )
46
				->where( 'siteid = ?' )->andWhere( 'invoiceno = ?' )
47
				->setParameters( [$row['maxnum'], $row['siteid'], 1] )
48
				->executeStatement();
49
		}
50
	}
51
}
52