Assign.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Scm\Lib;
  3. use Dever;
  4. class Assign
  5. {
  6. public function __construct()
  7. {
  8. Dever::load('manage/auth.init');
  9. }
  10. public function get(&$goods, &$order, $shop, $state = false)
  11. {
  12. if (strstr($order['order_num'], '_')) {
  13. $temp = explode('_', $order['order_num']);
  14. $order['order_num'] = $temp[0];
  15. $order['order_index'] = $temp[1];
  16. } else {
  17. $order['order_index'] = 0;
  18. }
  19. # 获取门店分配的仓库和供应商
  20. $data = $source = array();
  21. $source[] = array
  22. (
  23. 'type' => -1,
  24. 'id' => -1,
  25. 'name' => '未分配供货商',
  26. );
  27. $this->assign_store($data, $goods, $source, $order, $shop);
  28. $this->assign_factory($data, $goods, $source, $order, $shop);
  29. if ($state) {
  30. return array($data, $source);
  31. }
  32. return $data;
  33. }
  34. private function assign_store(&$data, &$goods, &$source, $order, $shop)
  35. {
  36. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  37. $store = array();
  38. $distance = array();
  39. # 按照距离排序
  40. if ($shop_store) {
  41. foreach ($shop_store as $k => $v) {
  42. $info = Dever::db('store/info')->find($v['store_id']);
  43. if ($info && $info['status'] == 1) {
  44. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  45. $store[] = $info;
  46. }
  47. }
  48. if ($store) {
  49. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  50. foreach ($store as $k => $v) {
  51. $store[$k]['type'] = 2;
  52. //$source[] = $store[$k];
  53. if ($goods) {
  54. foreach ($goods as $gk => $gv) {
  55. $where = array();
  56. $where['store_id'] = $v['id'];
  57. $where['goods_id'] = $gv['goods_id'];
  58. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  59. $sku = Dever::db('store/goods_sku')->getOne($where);
  60. if ($sku) {
  61. $source[$v['id']] = $store[$k];
  62. if ($gv['num'] <= $sku['total']) {
  63. # 库存符合
  64. $data['2_' . $v['id']][] = array
  65. (
  66. 'area' => $shop['area'],
  67. 'order_id' => $order['id'],
  68. 'order_type' => $order['type'],
  69. 'order_num' => $order['order_num'],
  70. 'order_index' => $order['order_index'],
  71. 'type' => 1,
  72. 'type_id' => $shop['id'],
  73. 'source_type' => 2,
  74. 'source_id' => $v['id'],
  75. 'source_name' => $v['name'],
  76. 'order_goods_id' => $gv['id'],
  77. 'goods_id' => $where['goods_id'],
  78. 'sku_id' => $where['sku_id'],
  79. 'price' => $gv['price'],
  80. 'p_price' => $gv['price'],
  81. 'num' => $gv['num'],
  82. );
  83. unset($goods[$gk]);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return $store;
  91. }
  92. return false;
  93. }
  94. private function assign_factory(&$data, &$goods, &$source, $order, $shop)
  95. {
  96. $where = array('shop_id' => $shop['id']);
  97. $lng = $shop['lng'];
  98. $lat = $shop['lat'];
  99. $address = array();
  100. if (isset($order['parent_type'])) {
  101. if ($order['parent_type'] == 2) {
  102. # 平台商城
  103. $sell_order = Dever::db('shop/sell_order')->find($order['parent_order_id']);
  104. if ($sell_order) {
  105. $address = Dever::db('passport/address')->find($sell_order['address_id']);
  106. }
  107. } elseif ($order['parent_type'] == 3) {
  108. # 代理商下单
  109. $dh_order = Dever::db('agent/dh_order')->find($order['parent_order_id']);
  110. if ($dh_order) {
  111. $address = Dever::db('agent/member_address')->find($dh_order['address_id']);
  112. }
  113. }
  114. } elseif (isset($order['type'])) {
  115. if ($order['type'] == 2) {
  116. # 平台商城
  117. $address = Dever::db('passport/address')->find($order['address_id']);
  118. } elseif ($order['type'] == 3) {
  119. # 代理商下单
  120. $address = Dever::db('agent/member_address')->find($order['address_id']);
  121. }
  122. }
  123. $w = array();
  124. $state = false;
  125. if ($goods) {
  126. foreach ($goods as $gk => $gv) {
  127. if ($gv['goods_id'] == 10000289) {
  128. $state = true;
  129. }
  130. }
  131. }
  132. if ($state && $address && $address['province'] && $address['city']) {
  133. $area_ship = Dever::db('shop/area_ship')->find(array('area' => $address['province'] . ',' . $address['city']));
  134. if (!$area_ship) {
  135. $area_ship = Dever::db('shop/area_ship')->find(array('area' => $address['province'] . ',-1'));
  136. }
  137. if ($area_ship) {
  138. $w['area_ship'] = $area_ship['area_id'];
  139. }
  140. }
  141. $shop_factory = Dever::db('shop/factory')->select($where);
  142. $factory = array();
  143. $distance = array();
  144. # 按照距离排序
  145. if ($shop_factory && $goods) {
  146. foreach ($shop_factory as $k => $v) {
  147. $w['id'] = $v['factory_id'];
  148. $info = Dever::db('factory/info')->find($w);
  149. if ($info && $info['status'] == 1) {
  150. $distance[] = $info['distance'] = Dever::distance($lng, $lat, $info['lng'], $info['lat']);
  151. $factory[] = $info;
  152. }
  153. }
  154. if ($factory) {
  155. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  156. foreach ($factory as $k => $v) {
  157. $factory[$k]['type'] = 3;
  158. //$source[] = $factory[$k];
  159. if ($goods) {
  160. foreach ($goods as $gk => $gv) {
  161. $where = array();
  162. $where['factory_id'] = $v['id'];
  163. $where['goods_id'] = $gv['goods_id'];
  164. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  165. $sku = Dever::db('factory/goods_sku')->getOne($where);
  166. if ($sku) {
  167. $source[$v['id']] = $factory[$k];
  168. $data['3_' . $v['id']][] = array
  169. (
  170. 'area' => $shop['area'],
  171. 'order_id' => $order['id'],
  172. 'order_type' => $order['type'],
  173. 'order_num' => $order['order_num'],
  174. 'order_index' => $order['order_index'],
  175. 'type' => 1,
  176. 'type_id' => $shop['id'],
  177. 'source_type' => 3,
  178. 'source_id' => $v['id'],
  179. 'source_name' => $v['name'],
  180. 'source_info_type' => $v['type'],
  181. 'source_info_shopinfo' => $v['shopinfo'],
  182. 'order_goods_id' => $gv['id'],
  183. 'goods_id' => $where['goods_id'],
  184. 'sku_id' => $where['sku_id'],
  185. 'price' => $gv['price'],
  186. 'p_price' => $sku['p_price'],
  187. 'num' => $gv['num'],
  188. );
  189. unset($goods[$gk]);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. return $factory;
  196. }
  197. return false;
  198. }
  199. }