123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace Scm\Lib;
- use Dever;
- class Assign
- {
- public function __construct()
- {
- Dever::load('manage/auth.init');
- }
- public function get(&$goods, &$order, $shop, $state = false)
- {
- if (strstr($order['order_num'], '_')) {
- $temp = explode('_', $order['order_num']);
- $order['order_num'] = $temp[0];
- $order['order_index'] = $temp[1];
- } else {
- $order['order_index'] = 0;
- }
- # 获取门店分配的仓库和供应商
- $data = $source = array();
- $source[] = array
- (
- 'type' => -1,
- 'id' => -1,
- 'name' => '未分配供货商',
- );
- $this->assign_store($data, $goods, $source, $order, $shop);
- $this->assign_factory($data, $goods, $source, $order, $shop);
- if ($state) {
- return array($data, $source);
- }
- return $data;
- }
- private function assign_store(&$data, &$goods, &$source, $order, $shop)
- {
- $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
- $store = array();
- $distance = array();
- # 按照距离排序
- if ($shop_store) {
- foreach ($shop_store as $k => $v) {
- $info = Dever::db('store/info')->find($v['store_id']);
- if ($info && $info['status'] == 1) {
- $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
- $store[] = $info;
- }
- }
- if ($store) {
- array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
- foreach ($store as $k => $v) {
- $store[$k]['type'] = 2;
- //$source[] = $store[$k];
- if ($goods) {
- foreach ($goods as $gk => $gv) {
- $where = array();
- $where['store_id'] = $v['id'];
- $where['goods_id'] = $gv['goods_id'];
- $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
- $sku = Dever::db('store/goods_sku')->getOne($where);
- if ($sku) {
- $source[$v['id']] = $store[$k];
- if ($gv['num'] <= $sku['total']) {
- # 库存符合
- $data['2_' . $v['id']][] = array
- (
- 'area' => $shop['area'],
- 'order_id' => $order['id'],
- 'order_type' => $order['type'],
- 'order_num' => $order['order_num'],
- 'order_index' => $order['order_index'],
- 'type' => 1,
- 'type_id' => $shop['id'],
- 'source_type' => 2,
- 'source_id' => $v['id'],
- 'source_name' => $v['name'],
- 'order_goods_id' => $gv['id'],
- 'goods_id' => $where['goods_id'],
- 'sku_id' => $where['sku_id'],
- 'price' => $gv['price'],
- 'p_price' => $gv['price'],
- 'num' => $gv['num'],
- );
- unset($goods[$gk]);
- }
- }
- }
- }
- }
- }
- return $store;
- }
- return false;
- }
- private function assign_factory(&$data, &$goods, &$source, $order, $shop)
- {
- $where = array('shop_id' => $shop['id']);
- $lng = $shop['lng'];
- $lat = $shop['lat'];
- $address = array();
- if (isset($order['parent_type'])) {
- if ($order['parent_type'] == 2) {
- # 平台商城
- $sell_order = Dever::db('shop/sell_order')->find($order['parent_order_id']);
- if ($sell_order) {
- $address = Dever::db('passport/address')->find($sell_order['address_id']);
- }
- } elseif ($order['parent_type'] == 3) {
- # 代理商下单
- $dh_order = Dever::db('agent/dh_order')->find($order['parent_order_id']);
- if ($dh_order) {
- $address = Dever::db('agent/member_address')->find($dh_order['address_id']);
- }
- }
- } elseif (isset($order['type'])) {
- if ($order['type'] == 2) {
- # 平台商城
- $address = Dever::db('passport/address')->find($order['address_id']);
- } elseif ($order['type'] == 3) {
- # 代理商下单
- $address = Dever::db('agent/member_address')->find($order['address_id']);
- }
- }
-
- $w = array();
- $state = false;
- if ($goods) {
- foreach ($goods as $gk => $gv) {
- if ($gv['goods_id'] == 10000289) {
- $state = true;
- }
- }
- }
- if ($state && $address && $address['province'] && $address['city']) {
- $area_ship = Dever::db('shop/area_ship')->find(array('area' => $address['province'] . ',' . $address['city']));
- if (!$area_ship) {
- $area_ship = Dever::db('shop/area_ship')->find(array('area' => $address['province'] . ',-1'));
- }
- if ($area_ship) {
- $w['area_ship'] = $area_ship['area_id'];
- }
- }
- $shop_factory = Dever::db('shop/factory')->select($where);
- $factory = array();
- $distance = array();
- # 按照距离排序
- if ($shop_factory && $goods) {
- foreach ($shop_factory as $k => $v) {
- $w['id'] = $v['factory_id'];
- $info = Dever::db('factory/info')->find($w);
- if ($info && $info['status'] == 1) {
- $distance[] = $info['distance'] = Dever::distance($lng, $lat, $info['lng'], $info['lat']);
- $factory[] = $info;
- }
- }
- if ($factory) {
- array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
- foreach ($factory as $k => $v) {
- $factory[$k]['type'] = 3;
- //$source[] = $factory[$k];
- if ($goods) {
- foreach ($goods as $gk => $gv) {
- $where = array();
- $where['factory_id'] = $v['id'];
- $where['goods_id'] = $gv['goods_id'];
- $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
- $sku = Dever::db('factory/goods_sku')->getOne($where);
- if ($sku) {
- $source[$v['id']] = $factory[$k];
- $data['3_' . $v['id']][] = array
- (
- 'area' => $shop['area'],
- 'order_id' => $order['id'],
- 'order_type' => $order['type'],
- 'order_num' => $order['order_num'],
- 'order_index' => $order['order_index'],
- 'type' => 1,
- 'type_id' => $shop['id'],
- 'source_type' => 3,
- 'source_id' => $v['id'],
- 'source_name' => $v['name'],
- 'source_info_type' => $v['type'],
- 'source_info_shopinfo' => $v['shopinfo'],
- 'order_goods_id' => $gv['id'],
- 'goods_id' => $where['goods_id'],
- 'sku_id' => $where['sku_id'],
- 'price' => $gv['price'],
- 'p_price' => $sku['p_price'],
- 'num' => $gv['num'],
- );
- unset($goods[$gk]);
- }
- }
- }
- }
- }
- return $factory;
- }
- return false;
- }
- }
|