GoodsOptionService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/6
  6. * Time: 11:08
  7. */
  8. namespace app\backend\modules\goods\services;
  9. use app\backend\modules\goods\models\GoodsOption;
  10. use Illuminate\Support\Facades\DB;
  11. class GoodsOptionService
  12. {
  13. public static function getOptions($goods_id, $allSpecs)
  14. {
  15. $options = GoodsOption::where('goods_id', $goods_id)->get();
  16. foreach ($options as &$option) {
  17. $options->withhold_stock = $option->withhold_stock;
  18. }
  19. $specs = [];
  20. $html = '';
  21. if (count($options) > 0) {
  22. $specitemids = explode("_", $options[0]['specs']);
  23. foreach ($specitemids as $itemid) {
  24. foreach ($allSpecs as $spec) {
  25. $specItems = $spec['items'];
  26. foreach ($specItems as $specItem) {
  27. if ($specItem['id'] == $itemid) {
  28. $specs[] = $spec;
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. $html .= '<table class="table table-bordered table-condensed">';
  35. $html .= '<thead>';
  36. $html .= '<tr class="active">';
  37. $specs_len = count($specs);
  38. $newlen = 1;
  39. $h = [];
  40. $rowspans = [];
  41. for ($i = 0; $i < $specs_len; $i++) {
  42. $html .= "<th style='width:18%;'>" . $specs[$i]['title'] . "</th>";
  43. $itemlen = count($specs[$i]['items']);
  44. if ($itemlen <= 0) {
  45. $itemlen = 1;
  46. }
  47. $newlen *= $itemlen;
  48. $h = array();
  49. for ($j = 0; $j < $newlen; $j++) {
  50. $h[$i][$j] = [];
  51. }
  52. $l = count($specs[$i]['items']);
  53. $rowspans[$i] = 1;
  54. for ($j = $i + 1; $j < $specs_len; $j++) {
  55. $rowspans[$i] *= count($specs[$j]['items']);
  56. }
  57. }
  58. $html .= '<th class="info" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">库存</div><div class="input-group"><input type="text" class="form-control option_stock_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_stock\');"></a></span></div></div></th>';
  59. $html .= '<th class="info" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">预扣库存</div><div class="input-group"></div></div></th>';
  60. $html .= '<th class="success" style="width:10%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">市场价格</div><div class="input-group"><input type="text" class="form-control option_marketprice_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_marketprice\');"></a></span></div></div></th>';
  61. $html .= '<th class="warning" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">销售价格</div><div class="input-group"><input type="text" class="form-control option_productprice_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_productprice\');"></a></span></div></div></th>';
  62. $html .= '<th class="danger" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">成本价格</div><div class="input-group"><input type="text" class="form-control option_costprice_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_costprice\');"></a></span></div></div></th>';
  63. //$html .= '<th class="warning" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">红包价格</div><div class="input-group"><input type="text" class="form-control option_redprice_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_redprice\');"></a></span></div></div></th>';
  64. $html .= '<th class="primary" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">商品编码</div><div class="input-group"><input type="text" class="form-control option_goodssn_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_goodssn\');"></a></span></div></div></th>';
  65. $html .= '<th class="danger" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">商品条码</div><div class="input-group"><input type="text" class="form-control option_productsn_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_productsn\');"></a></span></div></div></th>';
  66. $html .= '<th class="info" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">重量(克)</div><div class="input-group"><input type="text" class="form-control option_weight_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_weight\');"></a></span></div></div></th>';
  67. $html .= '<th class="info" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">体积(m³)</div><div class="input-group"><input type="text" class="form-control option_volume_all" VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_volume\');"></a></span></div></div></th>';
  68. $html .= '<th class="info" style="width:13%;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">点击图片上传<br /> 推荐(100*100)</div></div></th>';
  69. $html .= '</tr></thead>';
  70. for ($m = 0; $m < $specs_len; $m++) {
  71. $k = 0;
  72. $kid = 0;
  73. $n = 0;
  74. for ($j = 0; $j < $newlen; $j++) {
  75. $rowspan = $rowspans[$m];
  76. if ($j % $rowspan == 0) {
  77. $h[$m][$j] = array(
  78. "html" => "<td rowspan='" . $rowspan . "'>" . $specs[$m]['items'][$kid]['title'] . "</td>",
  79. "id" => $specs[$m]['items'][$kid]['id']
  80. );
  81. } else {
  82. $h[$m][$j] = array(
  83. "html" => "",
  84. "id" => $specs[$m]['items'][$kid]['id']
  85. );
  86. }
  87. $n++;
  88. if ($n == $rowspan) {
  89. $kid++;
  90. if ($kid > count($specs[$m]['items']) - 1) {
  91. $kid = 0;
  92. }
  93. $n = 0;
  94. }
  95. }
  96. }
  97. $hh = "";
  98. for ($i = 0; $i < $newlen; $i++) {
  99. $hh .= "<tr>";
  100. $ids = [];
  101. for ($j = 0; $j < $specs_len; $j++) {
  102. $hh .= $h[$j][$i]['html'];
  103. $ids[] = $h[$j][$i]['id'];
  104. }
  105. $ids = implode("_", $ids);
  106. $val = [
  107. "id" => "",
  108. "title" => "",
  109. "stock" => "",
  110. "withhold_stock" => "",
  111. "cost_price" => "",
  112. "product_price" => "",
  113. "market_price" => "",
  114. "weight" => "",
  115. "volume",
  116. 'virtual' => '',
  117. "red_price" => '',
  118. 'thumb' => '',
  119. ];
  120. foreach ($options as $option) {
  121. if ($ids === $option['specs']) {
  122. $val = [
  123. "id" => $option['id'],
  124. "title" => $option['title'],
  125. "stock" => $option['stock'],
  126. "withhold_stock" => $option['withhold_stock'],
  127. "cost_price" => $option['cost_price'],
  128. "product_price" => $option['product_price'],
  129. "market_price" => $option['market_price'],
  130. "goods_sn" => $option['goods_sn'],
  131. "product_sn" => $option['product_sn'],
  132. "weight" => $option['weight'],
  133. "volume" => $option['volume'],
  134. 'virtual' => $option['virtual'],
  135. 'red_price' => $option['red_price'],
  136. 'thumb' => $option['thumb'],
  137. 'url' => yz_tomedia($option['thumb']),
  138. //'option_ladder' => unserialize($o['option_ladders'])
  139. ];
  140. break;
  141. }
  142. }
  143. $hh .= '<td class="info">';
  144. $hh .= '<input name="option_stock_' . $ids . '[]" type="text" class="form-control option_stock option_stock_' . $ids . '" value="' . $val['stock'] . '"/>';
  145. $hh .= '<input name="option_id_' . $ids . '[]" type="hidden" class="form-control option_id option_id_' . $ids . '" value="' . $val['id'] . '"/>';
  146. $hh .= '<input name="option_ids[]" type="hidden" class="form-control option_ids option_ids_' . $ids . '" value="' . $ids . '"/>';
  147. $hh .= '<input name="option_title_' . $ids . '[]" type="hidden" class="form-control option_title option_title_' . $ids . '" value="' . $val['title'] . '"/>';
  148. $hh .= '<input name="option_virtual_' . $ids . '[]" type="hidden" class="form-control option_title option_virtual_' . $ids . '" value="' . $val['virtual'] . '"/>';
  149. $hh .= '</td>';
  150. //$hh .= '<td class="success"><input name="option_marketprice_' . $ids . '[]" type="text" class="form-control option_marketprice option_marketprice_' . $ids . '" value="' . $val['marketprice'] . '"/></td>';
  151. $hh .= '<td class="info"><input type="text" disabled="disabled" class="form-control option_withhold_stock option_withhold_stock_' . $ids . '" value="' . $val['withhold_stock'] . '"/>';
  152. $hh .= '</td>';
  153. $hh .= '<td class="success"><input name="option_marketprice_' . $ids . '[]" type="text" class="form-control option_marketprice option_marketprice_' . $ids . '" value="' . $val['market_price'] . '"/>';
  154. $hh .= '</td>';
  155. $hh .= '<td class="warning"><input name="option_productprice_' . $ids . '[]" type="text" class="form-control option_productprice option_productprice_' . $ids . '" " value="' . $val['product_price'] . '"/></td>';
  156. $hh .= '<td class="danger"><input name="option_costprice_' . $ids . '[]" type="text" class="form-control option_costprice option_costprice_' . $ids . '" " value="' . $val['cost_price'] . '"/></td>';
  157. //$hh .= '<td class="warning"><input name="option_redprice_' . $ids . '[]" type="text" class="form-control option_redprice option_redprice_' . $ids . '" " value="' . $val['red_price'] . '"/></td>';
  158. $hh .= '<td class="primary"><input name="option_goodssn_' . $ids . '[]" type="text" class="form-control option_goodssn option_goodssn_' . $ids . '" " value="' . $val['goods_sn'] . '"/></td>';
  159. $hh .= '<td class="danger"><input name="option_productsn_' . $ids . '[]" type="text" class="form-control option_productsn option_productsn_' . $ids . '" " value="' . $val['product_sn'] . '"/></td>';
  160. $hh .= '<td class="info"><input name="option_weight_' . $ids . '[]" type="text" class="form-control option_weight option_weight_' . $ids . '" " value="' . $val['weight'] . '"/></td>';
  161. $hh .= '<td class="info"><input name="option_volume_' . $ids . '[]" type="text" class="form-control option_volume option_volume_' . $ids . '" " value="' . $val['volume'] . '"/></td>';
  162. $hh .= '<td class="info"><div class="input-group"><input name="option_thumb_' . $ids . '[]" type="hidden" class="option_thumb_' . $ids . '" url="' . $val['url'] . '" value="' . $val['thumb'] . '"/><span><button style="display:none" class="btn btn-default" onclick="showImageDialog(this);" type="button">上传图片</button></span></div><div class="input-group" onclick="tu(this)" style="margin-top:.5em;"><img src="' . $val['url'] . '" onerror="nofind()" class="img-responsive img-thumbnail" style="width:50px;height:50px"></div></td>';
  163. $hh .= '</tr>';
  164. }
  165. $html .= $hh;
  166. $html .= "</table>";
  167. }
  168. return $html;
  169. }
  170. }