|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Item offer |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ecommerce\Item; |
|
13
|
|
|
|
|
14
|
|
|
class Offer extends \Model |
|
15
|
|
|
{ |
|
16
|
|
|
public static $objectName = 'Торговое предложение'; |
|
17
|
|
|
public static $cols = [ |
|
18
|
|
|
//Основные параметры |
|
19
|
|
|
'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'], |
|
20
|
|
|
'name' => ['type' => 'text'], |
|
21
|
|
|
'article' => ['type' => 'text'], |
|
22
|
|
|
//Системные |
|
23
|
|
|
'weight' => ['type' => 'number'], |
|
24
|
|
|
'date_create' => ['type' => 'dateTime'], |
|
25
|
|
|
//Менеджеры |
|
26
|
|
|
'warehouse' => ['type' => 'dataManager', 'relation' => 'warehouses'], |
|
27
|
|
|
'price' => ['type' => 'dataManager', 'relation' => 'prices'], |
|
28
|
|
|
]; |
|
29
|
|
|
public static $labels = [ |
|
30
|
|
|
'name' => 'Название', |
|
31
|
|
|
'article' => 'Артикул', |
|
32
|
|
|
'warehouse' => 'Наличие на складах', |
|
33
|
|
|
'price' => 'Цены', |
|
34
|
|
|
]; |
|
35
|
|
|
public static $dataManagers = [ |
|
36
|
|
|
'manager' => [ |
|
37
|
|
|
'cols' => [ |
|
38
|
|
|
'name', 'article', 'warehouse', 'price' |
|
39
|
|
|
] |
|
40
|
|
|
] |
|
41
|
|
|
]; |
|
42
|
|
|
public static $forms = [ |
|
43
|
|
|
'manager' => [ |
|
44
|
|
|
'map' => [ |
|
45
|
|
|
['name', 'article'], |
|
46
|
|
|
['warehouse'], |
|
47
|
|
|
['price'] |
|
48
|
|
|
] |
|
49
|
|
|
] |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
public static function relations() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
'warehouses' => [ |
|
56
|
|
|
'type' => 'many', |
|
57
|
|
|
'model' => 'Ecommerce\Item\Offer\Warehouse', |
|
58
|
|
|
'col' => 'item_offer_id' |
|
59
|
|
|
], |
|
60
|
|
|
'prices' => [ |
|
61
|
|
|
'type' => 'many', |
|
62
|
|
|
'model' => 'Ecommerce\Item\Offer\Price', |
|
63
|
|
|
'col' => 'item_offer_id', |
|
64
|
|
|
], |
|
65
|
|
|
'bonuses' => [ |
|
66
|
|
|
'type' => 'many', |
|
67
|
|
|
'model' => 'Ecommerce\Item\Offer\Bonus', |
|
68
|
|
|
'col' => 'item_offer_id', |
|
69
|
|
|
], |
|
70
|
|
|
'item' => [ |
|
71
|
|
|
'model' => 'Ecommerce\Item', |
|
72
|
|
|
'col' => 'item_id' |
|
73
|
|
|
] |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function changeWarehouse($count) |
|
78
|
|
|
{ |
|
79
|
|
|
$warehouse = Offer\Warehouse::get([['count', '0', '>'], ['item_offer_id', $this->id]]); |
|
80
|
|
|
if ($warehouse) { |
|
81
|
|
|
$warehouse->count +=(float) $count; |
|
82
|
|
|
$warehouse->save(); |
|
83
|
|
|
} else { |
|
84
|
|
|
$warehouse = Offer\Warehouse::get([['item_offer_id', $this->id]]); |
|
85
|
|
|
if ($warehouse) { |
|
86
|
|
|
$warehouse->count +=(float) $count; |
|
87
|
|
|
$warehouse->save(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function warehouseCount($cart_id = 0) |
|
93
|
|
|
{ |
|
94
|
|
|
$warehouseIds = []; |
|
95
|
|
View Code Duplication |
if (class_exists('Geography\City\Data')) { |
|
|
|
|
|
|
96
|
|
|
$warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]); |
|
97
|
|
|
if ($warehouses && $warehouses->data) { |
|
98
|
|
|
foreach (explode(',', $warehouses->data) as $id) { |
|
99
|
|
|
$warehouseIds[$id] = $id; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if ($warehouseIds) { |
|
104
|
|
|
\App::$cur->db->where(\Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index(), $warehouseIds, 'IN'); |
|
105
|
|
|
} |
|
106
|
|
|
\App::$cur->db->where(\Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index(), $this->id); |
|
107
|
|
|
\App::$cur->db->cols = 'COALESCE(sum(' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count),0) as `sum` '; |
|
108
|
|
|
$warehouse = \App::$cur->db->select(\Ecommerce\Item\Offer\Warehouse::table())->fetch(); |
|
109
|
|
|
|
|
110
|
|
|
\App::$cur->db->cols = 'COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) as `sum` '; |
|
111
|
|
|
\App::$cur->db->where(\Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index(), $this->id); |
|
112
|
|
|
if ($cart_id) { |
|
113
|
|
|
\App::$cur->db->where(\Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index(), (int) $cart_id, '!='); |
|
114
|
|
|
} |
|
115
|
|
|
$on = ' |
|
116
|
|
|
' . \Ecommerce\Cart::index() . ' = ' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index() . ' AND ( |
|
117
|
|
|
(`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
|
118
|
|
|
(`' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
|
119
|
|
|
) |
|
120
|
|
|
'; |
|
121
|
|
|
\App::$cur->db->join(\Ecommerce\Cart::table(), $on, 'inner'); |
|
122
|
|
|
|
|
123
|
|
|
$blocked = \App::$cur->db->select(\Ecommerce\Warehouse\Block::table())->fetch(); |
|
124
|
|
|
return (float) $warehouse['sum'] - (float) $blocked['sum']; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function beforeDelete() |
|
128
|
|
|
{ |
|
129
|
|
|
if ($this->id) { |
|
130
|
|
|
if ($this->warehouses) { |
|
131
|
|
|
foreach ($this->warehouses as $warehouse) { |
|
132
|
|
|
$warehouse->delete(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
if ($this->prices) { |
|
136
|
|
|
foreach ($this->prices as $price) { |
|
137
|
|
|
$price->delete(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
if ($this->bonuses) { |
|
141
|
|
|
foreach ($this->bonuses as $bonus) { |
|
142
|
|
|
$bonus->delete(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.