OrderTransit.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\utils\OrderNo;
  9. use Yii;
  10. use yii\helpers\ArrayHelper;
  11. /**
  12. * This is the model class for table "{{%order_share}}".
  13. *
  14. * @property integer $id
  15. * @property integer $order_id
  16. * @property integer $cloud_order_id
  17. * @property integer $cloud_supplier_id
  18. * @property string $goods_info
  19. * @property string $order_detail_ids
  20. * @property integer $province_id
  21. * @property integer $city_id
  22. * @property integer $district_id
  23. * @property string $address
  24. * @property string $name
  25. * @property string $tel
  26. * @property integer $status
  27. * @property integer $is_delete
  28. * @property string $express_name
  29. * @property string $express_no
  30. * @property integer $send_type
  31. * @property integer $order_type
  32. * @property string $order_price
  33. * @property integer $confirm_time
  34. * @property integer $is_sale
  35. * @property integer $cancel_examine
  36. * @property string $cancel_examine_time
  37. * @property integer $created_at
  38. * @property integer $deleted_at
  39. * @property integer $is_use_platform_mch
  40. * @property integer $store_id
  41. * @property integer $goods_send_type
  42. */
  43. class OrderTransit extends \yii\db\ActiveRecord
  44. {
  45. /**
  46. * @inheritdoc
  47. */
  48. public static function tableName()
  49. {
  50. return '{{%order_transit}}';
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['id', 'order_id', 'cloud_order_id', 'cloud_supplier_id', 'province_id', 'city_id', 'confirm_time',
  59. 'district_id', 'status', 'is_delete', 'send_type', 'order_type', 'is_sale', 'cancel_examine', 'created_at', 'deleted_at', 'is_use_platform_mch', 'store_id', 'goods_send_type'],
  60. 'integer'],
  61. [['goods_info', 'order_detail_ids' , 'address' , 'name' , 'tel' , 'express_name' , 'express_no' ,
  62. 'order_price' , 'cancel_examine_time'], 'string'],
  63. ];
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'id' => '',
  72. 'order_id' => '订单id',
  73. 'cloud_order_id' => '云仓转单id',
  74. 'cloud_supplier_id' => '云仓供货商id',
  75. 'goods_info' => '转单的商品信息',
  76. 'order_detail_ids' => 'oder_detail_id关联信息',
  77. 'province_id' => '省',
  78. 'city_id' => '市',
  79. 'district_id' => '区',
  80. 'address' => '地址详情',
  81. 'name' => '联系人',
  82. 'tel' => '联系电话',
  83. 'status' => '转单状态 -1取消 0 转单创建成功1 供货商已发货 2 已确认收货',
  84. 'is_delete' => '0未删除 1已删除',
  85. 'created_at' => '创建时间',
  86. 'deleted_at' => '删除时间',
  87. 'express_name' => '',
  88. 'express_no' => '',
  89. 'send_type' => '',
  90. 'order_type' => '订单类型',
  91. 'order_price' => '',
  92. 'confirm_time' => '确认收货时间',
  93. 'is_sale' => '是否过售后期',
  94. 'cancel_examine' => '是否申请售后 0未申请 1已申请 ',
  95. 'cancel_examine_time' => '取消时间',
  96. ];
  97. }
  98. public function afterOtherDataSave()
  99. {
  100. \app\modules\admin\models\jushuitan\JuShuiTanForm::afterTransitOrderSave($this);
  101. }
  102. public function afterSave($insert, $changedAttributes)
  103. {
  104. \app\modules\admin\models\jushuitan\JuShuiTanForm::afterTransitOrderSave($this, $insert, $changedAttributes);
  105. }
  106. public static function checkMdFrontBind($mch, $md_id = 0) {
  107. try {
  108. // $md_id = get_md_id();
  109. $mdAgentFrontBind = AgentFrontBind::findOne(['md_id' => $md_id, 'is_delete' => 0, 'status' => 1]);
  110. foreach ($mch['goods_list'] as $goods_item) {
  111. $goods_item['goods_name'] = mb_substr($goods_item['goods_name'], 0, 10);
  112. $supplierWhere = [
  113. 'is_delete' => 0,
  114. 'status' => 1,
  115. 'type' => AgentFrontBind::TYPE_SUPPLIER,
  116. ];
  117. //如果是云仓商品 且 配送方式为仓库配送 或集采
  118. if (intval($goods_item['cloud_goods_id']) && (intval($goods_item['is_front_delivery']) || intval($goods_item['is_front_centralize']))) {
  119. if (intval($goods_item['is_front_delivery'])) {
  120. if (!in_array($mch['send'], ['shop', 'delivery'])) {
  121. throw new \Exception('只能选择自提配送或同城配送');
  122. }
  123. }
  124. if (intval($goods_item['is_front_centralize'])) {
  125. if ($mch['send'] != 'shop') {
  126. throw new \Exception('只能选择自提配送');
  127. }
  128. }
  129. if ($mch['send'] === 'shop') {
  130. if ($md_id <= 0) {
  131. throw new \Exception('自提订单请选择门店');
  132. }
  133. //判断当前门店是否绑定仓库
  134. if (!$mdAgentFrontBind) {
  135. throw new \Exception('当前门店未拥有商品:'.$goods_item['goods_name'].'...的售卖权限,请尝试其他门店下单');
  136. }
  137. $supplierWhere['front_agent_admin_id'] = $mdAgentFrontBind->front_agent_admin_id;
  138. }
  139. if ($mch['send'] === 'delivery') {
  140. if ($md_id <= 0) {
  141. $goods = Goods::findOne($goods_item['goods_id']);
  142. if (!$goods) {
  143. throw new \Exception('商城商品' .$goods_item['goods_name']. '...状态异常');
  144. }
  145. $md = Md::findOne(['store_id' => $goods->store_id, 'is_delete' => 0, 'shop_audit' => Md::SHOP_AUDIT_YES]);
  146. if (!$md) {
  147. throw new \Exception('当前商城暂未创建门店不支持仓库同城配送');
  148. }
  149. $mdAgentFrontBind = AgentFrontBind::find()->where(['type' => AgentFrontBind::TYPE_STORE, 'type_id' => $goods->store_id, 'is_delete' => 0, 'status' => 1])
  150. ->andWhere(['>', 'md_id', 0])->select('front_agent_admin_id')->column();
  151. if (!$mdAgentFrontBind) {
  152. throw new \Exception('当前商城门店状态异常不能下单,请联系客服');
  153. }
  154. $supplierWhere['front_agent_admin_id'] = $mdAgentFrontBind;
  155. } else {
  156. //判断当前门店是否绑定仓库
  157. if (!$mdAgentFrontBind) {
  158. throw new \Exception('当前门店未拥有商品:'.$goods_item['goods_name'].'...的售卖权限,请尝试其他门店下单');
  159. }
  160. $supplierWhere['front_agent_admin_id'] = $mdAgentFrontBind->front_agent_admin_id;
  161. }
  162. }
  163. if (intval($goods_item['is_front_delivery'])) {
  164. $erpInventory = AgentFrontErpInventory::findOne(['goods_no' => $goods_item['goods_no'], 'goods_id' => $goods_item['cloud_goods_id']]);
  165. if (!$erpInventory) {
  166. throw new \Exception('商品' .$goods_item['goods_name']. '...仓库暂无货源');
  167. }
  168. if ($erpInventory->num <= $goods_item['num']) {
  169. throw new \Exception('商品' .$goods_item['goods_name']. '...仓库货源不足');
  170. }
  171. }
  172. $cloud_supplier_id = $goods_item['cloud_supplier_id'];
  173. //如果下单的产品的供货商绑定的仓库和门店没有绑定,那么不能下单
  174. if (!$cloud_supplier_id) {
  175. throw new \Exception('商品' .$goods_item['goods_name']. '...状态异常');
  176. }
  177. $supplier = Supplier::findOne(['cloud_supplier_id' => $cloud_supplier_id, 'is_delete' => 0]);
  178. if (!$supplier) {
  179. throw new \Exception('供货商' .$goods_item['goods_name']. '...状态异常');
  180. }
  181. $supplierWhere['type_id'] = $supplier->id;
  182. //判断当前商品供货商是否和门店仓库关联
  183. $supplierAgentFrontBind = AgentFrontBind::findOne($supplierWhere);
  184. if (!$supplierAgentFrontBind) {
  185. throw new \Exception('商品' .$goods_item['goods_name']. '...状态异常 不能下单 请联系客服');
  186. }
  187. }
  188. }
  189. return [
  190. 'code' => 0,
  191. 'msg' => 'success'
  192. ];
  193. } catch (\Exception $e) {
  194. return [
  195. 'code' => 1,
  196. 'msg' => $e->getMessage()
  197. ];
  198. }
  199. }
  200. }