Product.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace addons\TinyShop\common\widgets\product;
  3. use Yii;
  4. use common\helpers\BcHelper;
  5. use common\helpers\Html;
  6. use common\enums\StatusEnum;
  7. use common\helpers\StringHelper;
  8. use common\helpers\ArrayHelper;
  9. use unclead\multipleinput\MultipleInputColumn;
  10. use addons\TinyShop\common\enums\DiscountTypeEnum;
  11. use addons\TinyShop\common\models\marketing\MarketingProduct;
  12. /**
  13. * Class Product
  14. * @package addons\TinyShop\common\widgets\product
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class Product extends \yii\widgets\InputWidget
  18. {
  19. /**
  20. * 唯一ID
  21. *
  22. * @var string
  23. */
  24. public $box_id = '';
  25. /**
  26. * 判断可选的商品上限
  27. *
  28. * 0 为不限制
  29. *
  30. * @var int
  31. */
  32. public $max = 5000;
  33. /**
  34. * 判断可选的商品最低
  35. *
  36. * 0 为不限制
  37. */
  38. public $min = 0;
  39. /**
  40. * 多选开启
  41. *
  42. * @var bool
  43. */
  44. public $multiple = true;
  45. /**
  46. * 设置 skus
  47. *
  48. * @var bool
  49. */
  50. public $setting_sku = true;
  51. /**
  52. * @var array|MarketingProduct
  53. */
  54. public $value = [];
  55. /**
  56. * 虚拟商品可选开启
  57. *
  58. * @var bool
  59. */
  60. public $is_virtual = false;
  61. /**
  62. * 字段列表
  63. *
  64. * @var array
  65. */
  66. public $columns = [];
  67. /**
  68. * @var string
  69. */
  70. public $url = '';
  71. /**
  72. * 计算
  73. *
  74. * @var string
  75. */
  76. public $calculation = [
  77. // 计算规格 price * number 的 sku 累加
  78. 'moneyField' => 'price',
  79. // 计算总数量字段
  80. 'numberField' => 'number',
  81. ];
  82. /**
  83. * 字段差异对比
  84. *
  85. * @var string[]
  86. */
  87. public $difference = [
  88. 'from' => '',
  89. 'to' => '',
  90. 'relevancy' => '',
  91. ];
  92. /**
  93. * 默认字段值
  94. *
  95. * @var array
  96. */
  97. protected $column = [
  98. 'label' => '折扣(1-100)%',
  99. 'name' => 'discount',
  100. 'value' => '',
  101. 'valueFieldMap' => '', // 映射默认SKU值, 如果是SKU里面存在的默认用SKU的
  102. 'type' => MultipleInputColumn::TYPE_TEXT_INPUT,
  103. 'items' => [],
  104. // 属性
  105. 'options' => [
  106. 'sku' => true, // 规格内也显示列表
  107. 'placeholder' => '', // 输入框默认值
  108. 'calculation' => false, // 参与营销计算
  109. 'discountType' => DiscountTypeEnum::FIXATION, // 营销类型
  110. 'style' => '', // 样式
  111. ],
  112. // 字段规则验证
  113. 'rule' => [
  114. 'comparisonFieldMin' => '', // 比较字段(不能低于) 多个字段支持 , 分割
  115. 'comparisonFieldMax' => '', // 比较字段(不能超出) 多个字段支持 , 分割
  116. 'min' => 0, // 最小值
  117. 'max' => '', // 最大值
  118. ],
  119. // 触发清零字段
  120. 'emptyField' => [],
  121. ];
  122. /**
  123. * @var string
  124. */
  125. public $theme = 'product';
  126. /**
  127. * @return string
  128. * @throws \Exception
  129. */
  130. public function run()
  131. {
  132. $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  133. $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  134. empty($value) && $value = [];
  135. foreach ($this->columns as &$column) {
  136. $column = ArrayHelper::merge($this->column, $column);
  137. }
  138. foreach ($value as &$product) {
  139. $product['name_segmentation'] = $product['name'];
  140. foreach ($this->columns as &$column) {
  141. // 自定义值
  142. if (isset($product['marketing_data'][$column['name']])) {
  143. $product[$column['name']] = $product['marketing_data'][$column['name']];
  144. } else {
  145. if ($column['options']['sku'] == true) {
  146. $product[$column['name']] = '';
  147. }
  148. }
  149. }
  150. unset($product['marketing_data'], $product['delivery_type']);
  151. empty($product['sku']) && $product['sku'] = [];
  152. // 只有一个规格, 重新处理库存
  153. if (count($product['sku']) == 1) {
  154. $product['marketing_stock'] = $product['marketing_total_stock'] ?? 0;
  155. }
  156. // 处理 sku
  157. foreach ($product['sku'] as &$sku) {
  158. foreach ($this->columns as &$column) {
  159. if (!isset($sku[$column['name']])) {
  160. // 自定义值
  161. if (isset($sku['marketing_data'][$column['name']])) {
  162. $sku[$column['name']] = $sku['marketing_data'][$column['name']];
  163. }
  164. }
  165. // 自动计算价格
  166. if ($column['options']['calculation'] == true && isset($sku['discount_type'])) {
  167. switch ($sku['discount_type']) {
  168. case DiscountTypeEnum::MONEY :
  169. $sku['tmp_money'] = BcHelper::sub($sku['price'], $sku['discount']);
  170. break;
  171. case DiscountTypeEnum::DISCOUNT :
  172. $sku['tmp_money'] = BcHelper::mul($sku['price'], $sku['discount'] / 10);
  173. break;
  174. case DiscountTypeEnum::FIXATION :
  175. $sku['tmp_money'] = $sku['discount'];
  176. break;
  177. }
  178. $sku['tmp_money'] = floatval($sku['tmp_money']);
  179. $sku['tmp_discount_type'] = $sku['discount_type'];
  180. }
  181. }
  182. unset($sku['marketing_data']);
  183. }
  184. }
  185. return $this->render('discount', [
  186. 'products' => $value,
  187. 'name' => $name,
  188. 'calculation' => $this->calculation,
  189. 'difference' => $this->difference,
  190. 'columns' => $this->columns,
  191. 'min' => $this->min,
  192. 'max' => $this->max,
  193. 'multiple' => $this->multiple,
  194. 'is_virtual' => $this->is_virtual,
  195. 'url' => $this->url,
  196. 'setting_sku' => !empty($this->setting_sku) ? StatusEnum::ENABLED : StatusEnum::DISABLED,
  197. 'box_id' => !empty($this->box_id) ? $this->box_id : StringHelper::uuid('uniqid'),
  198. ]);
  199. }
  200. }
粤ICP备19079148号