OrderCreateCommonData.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\store\event;
  13. use addon\cashier\model\order\CashierOrderPay;
  14. use app\model\express\Express;
  15. use app\model\express\Local;
  16. use app\model\order\OrderCommon;
  17. use app\model\order\OrderCreateTool;
  18. use app\model\store\Store;
  19. /**
  20. * 订单创建相关
  21. */
  22. class OrderCreateCommonData
  23. {
  24. use OrderCreateTool;
  25. // 行为扩展的执行入口必须是run
  26. public function handle($params)
  27. {
  28. $type = $params['type'];
  29. $calculate_data = $params['data'];
  30. $store_id = $calculate_data['store_id'] ?? 0;
  31. $site_id = $calculate_data['site_id'];
  32. if($store_id == 0)
  33. return null;
  34. $store_model = new Store();
  35. $store_condition = array(
  36. ['store_id', '=', $store_id]
  37. );
  38. $store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
  39. $store_is_express = $store_info['is_express'];
  40. $store_is_o2o = $store_info['is_o2o'];
  41. $store_is_pickup = $store_info['is_pickup'];
  42. $store_config_model = new \addon\store\model\Config();
  43. $store_config = $store_config_model->getStoreBusinessConfig($site_id)['data']['value'] ?? [];
  44. $order_model = new OrderCommon();
  45. switch($type){
  46. case 'trade':
  47. $shop_goods_list = $calculate_data[ 'shop_goods_list' ];
  48. //必须是连锁模式 还需要判断某一个插件是否存在
  49. if($store_id > 0 && $store_config['store_business'] == 'store'){
  50. foreach ($shop_goods_list[ 'deliver_sort' ] as $type) {
  51. // 物流
  52. if ($type == 'express') {
  53. $store_is_express = $store_is_express ?? 0;
  54. if($store_is_express == 1){
  55. $title = $shop_goods_list[ 'express_config' ][ 'value' ][ 'express_name' ];
  56. if ($title == '') {
  57. $title = Express::express_type[ 'express' ][ 'title' ];
  58. }
  59. $express_type[] = [ 'title' => $title, 'name' => 'express' ];
  60. }
  61. }
  62. // 自提
  63. if ($type == 'store') {
  64. $store_is_pickup = $store_is_pickup ?? 0;
  65. if($store_is_pickup == 1){
  66. //根据坐标查询门店
  67. $store_model = new Store();
  68. $store_condition = array (
  69. [ 'site_id', '=', $site_id ],
  70. [ 'is_pickup', '=', 1 ],
  71. [ 'status', '=', 1 ],
  72. [ 'is_frozen', '=', 0 ],
  73. );
  74. $store_condition[] = ['store_id', '=', $store_id];
  75. $latlng = array (
  76. 'lat' => $calculate_data[ 'latitude' ],
  77. 'lng' => $calculate_data[ 'longitude' ],
  78. );
  79. $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
  80. $store_list = $store_list_result[ 'data' ];
  81. $title = $shop_goods_list[ 'store_config' ][ 'value' ][ 'store_name' ];
  82. if ($title == '') {
  83. $title = Express::express_type[ 'store' ][ 'title' ];
  84. }
  85. $express_type[] = [ 'title' => $title, 'name' => 'store', 'store_list' => $store_list ];
  86. }
  87. }
  88. // 外卖
  89. if ($type == 'local') {
  90. $store_is_o2o = $store_is_o2o ?? 0;
  91. if($store_is_o2o == 1){
  92. //查询本店的通讯地址
  93. $title = $shop_goods_list[ 'local_config' ][ 'value' ][ 'local_name' ];
  94. if ($title == '') {
  95. $title = '外卖配送';
  96. }
  97. $store_model = new Store();
  98. $store_condition = array (
  99. [ 'site_id', '=', $site_id ],
  100. );
  101. // if (addon_is_exit('store', $site_id)) {
  102. $store_condition[] = [ 'is_o2o', '=', 1 ];
  103. $store_condition[] = [ 'status', '=', 1 ];
  104. $store_condition[] = [ 'is_frozen', '=', 0 ];
  105. // } else {
  106. // $store_condition[] = ['is_default', '=', 1];
  107. // }
  108. $store_condition[] = ['store_id', '=', $store_id];
  109. $latlng = array (
  110. 'lat' => $calculate_data[ 'latitude' ],
  111. 'lng' => $calculate_data[ 'longitude' ],
  112. );
  113. $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
  114. $store_list = $store_list_result[ 'data' ];
  115. $express_type[] = [ 'title' => $title, 'name' => 'local', 'store_list' => $store_list ];
  116. }
  117. }
  118. }
  119. return $order_model->success($express_type ?? []);
  120. }
  121. break;
  122. case 'trade_calc':
  123. if($store_id > 0 && $store_config['store_business'] == 'store'){
  124. $shop_goods = $params['shop_goods'];
  125. //如果本地配送开启, 则查询出本地配送的配置
  126. $local_model = new Local();
  127. $local_info_result = $local_model->getLocalInfo([ [ 'site_id', '=', $site_id ], ['store_id', '=', $store_id ] ]);
  128. $local_info = $local_info_result[ 'data' ] ?? [];
  129. $shop_goods[ 'local_config' ][ 'info' ] = $local_info;
  130. $delivery_array = $calculate_data[ 'delivery' ] ?? [];
  131. $delivery_type = $delivery_array[ 'delivery_type' ] ?? 'express';
  132. if ($delivery_type == 'store') {
  133. //门店自提
  134. $delivery_money = 0;
  135. $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'store';
  136. if ($store_is_pickup == 0) {
  137. $error = ['error_msg' => '门店自提方式未开启!'];
  138. }
  139. $shop_goods[ 'delivery' ][ 'store_id' ] = $calculate_data[ 'delivery' ][ 'store_id' ];
  140. $shop_goods[ 'buyer_ask_delivery_time' ] = $calculate_data[ 'buyer_ask_delivery_time' ];
  141. $shop_goods = $this->storeOrderData($shop_goods, $calculate_data);
  142. } else {
  143. if (empty($calculate_data[ 'member_address' ])) {
  144. $delivery_money = 0;
  145. $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';
  146. $error = ['error_msg' => '未配置默认收货地址!'];
  147. } else {
  148. if ($delivery_type == 'express') {
  149. if ($store_is_express == 1) {
  150. //物流配送
  151. $express = new Express();
  152. $express_fee_result = $express->calculate($shop_goods, $calculate_data);
  153. if ($express_fee_result[ 'code' ] < 0) {
  154. $error = ['error_msg' => $express_fee_result[ 'message' ]];
  155. $delivery_fee = 0;
  156. } else {
  157. $delivery_fee = $express_fee_result[ 'data' ][ 'delivery_fee' ];
  158. }
  159. } else {
  160. $error = ['error_msg' => '物流配送方式未开启!'];
  161. $delivery_fee = 0;
  162. }
  163. $delivery_money = $delivery_fee;
  164. $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';
  165. } else if ($delivery_type == 'local') {
  166. //外卖配送
  167. $delivery_money = 0;
  168. $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'local';
  169. if ($store_is_o2o == 0) {
  170. $error = ['error_msg' => '外卖配送方式未开启!'];
  171. } else {
  172. if (empty($calculate_data[ 'delivery' ][ 'store_id' ])) {
  173. $error = ['error_msg' => '门店未选择!'];
  174. }
  175. $local_delivery_time = 0;
  176. if (!empty($calculate_data[ 'buyer_ask_delivery_time' ])) {
  177. $local_delivery_time = $calculate_data[ 'buyer_ask_delivery_time' ];
  178. }
  179. $shop_goods[ 'buyer_ask_delivery_time' ] = $local_delivery_time;
  180. $local_model = new Local();
  181. $local_result = $local_model->calculate($shop_goods, $calculate_data);
  182. $shop_goods[ 'delivery' ][ 'start_money' ] = 0;
  183. if ($local_result[ 'code' ] < 0) {
  184. $shop_goods[ 'delivery' ][ 'start_money' ] = $local_result[ 'data' ][ 'start_money_array' ][ 0 ] ?? 0;
  185. $error = ['error_msg' => $local_result[ 'message' ], 'priority' => 1, 'error' => $local_result[ 'data' ][ 'code' ]];
  186. } else {
  187. $delivery_money = $local_result[ 'data' ][ 'delivery_money' ];
  188. if (!empty($local_result[ 'data' ][ 'error_code' ])) {
  189. $error = ['error_msg' => $local_result[ 'data' ][ 'error' ], 'priority' => 1, 'error' => $local_result[ 'data' ][ 'error' ]];
  190. }
  191. }
  192. $shop_goods[ 'delivery' ][ 'error' ] = $error['error'] ?? 0;
  193. $shop_goods[ 'delivery' ][ 'error_msg' ] = $error['error_msg'] ?? '';
  194. }
  195. }
  196. }
  197. }
  198. return $order_model->success(['shop_goods' => $shop_goods, 'delivery_money' => $delivery_money, 'error' => $error ?? []]);
  199. }
  200. break;
  201. }
  202. }
  203. }