You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
7.6 KiB

  1. <?php
  2. namespace backend\modules\shop\logic\delivery;
  3. use backend\modules\shop\models\ars\Delivery;
  4. use backend\modules\shop\models\ars\DeliveryGoods;
  5. use backend\modules\shop\models\ars\ExpressTemplate;
  6. use backend\modules\shop\models\ars\Order;
  7. use backend\modules\shop\models\ars\OrderGoods;
  8. use Yii;
  9. use yii\db\Exception;
  10. use yii\helpers\ArrayHelper;
  11. class DeliveryManager
  12. {
  13. /**
  14. * @param $order
  15. * @param $delivery
  16. * @return array
  17. * 订单发货
  18. */
  19. public static function orderDelivery($order, $delivery)
  20. {
  21. $transaction = Yii::$app->db->beginTransaction();
  22. try {
  23. $data = Yii::$app->request->post();
  24. if (empty($data['deliveryGoods'])) {
  25. throw new Exception('缺少发货商品信息');
  26. }
  27. $delivery->load($data);
  28. $delivery->order_id = $order->id;
  29. if (!$delivery->save()) {
  30. throw new Exception('保存物流信息失败');
  31. }
  32. /*发货商品数据*/
  33. $orderStatus = self::saveDeliveryGoods($data['deliveryGoods'], $delivery->id);
  34. $order->status = $orderStatus;
  35. if (!$order->save()) {
  36. throw new Exception('order shipping_status update false');
  37. }
  38. if ($orderStatus == Order::STATUS_SHIPMENT_PORTION || Delivery::findOne(['!=', 'id', $delivery->id])) {
  39. $delivery->type = Delivery::TYPE_SHIPMENT_PORTION;
  40. } else {
  41. $delivery->type = Delivery::TYPE_SHIPMENT_ALL;
  42. }
  43. if (!$delivery->save()) {
  44. throw new Exception('保存物流信息失败');
  45. }
  46. $transaction->commit();
  47. return ['status' => true];
  48. } catch (Exception $e) {
  49. $transaction->rollBack();
  50. return ['status' => false, 'info' => $e->getMessage()];
  51. }
  52. }
  53. /**
  54. * @param $deliveryGoods
  55. * @param $delivery_id
  56. * @return int
  57. * @throws Exception
  58. * 保存发货商品信息
  59. */
  60. private static function saveDeliveryGoods($deliveryGoods, $delivery_id)
  61. {
  62. $status = Order::STATUS_SHIPMENT_ALL;
  63. foreach ($deliveryGoods as $id => $goodsCount) {
  64. $orderGoods = OrderGoods::findOne($id);
  65. /*如果是发货数量不是全部,订单状态为部分发货*/
  66. $deliveryGoodsCount = DeliveryGoods::find()->where(['order_goods_id' => $id])->sum('goods_count');
  67. if (($deliveryGoodsCount + $goodsCount) < $orderGoods->goods_count) { //如果已发货数量未达到order_goods里的数量
  68. $status = Order::STATUS_SHIPMENT_PORTION;
  69. } elseif (($deliveryGoodsCount + $goodsCount) > $orderGoods->goods_count) {
  70. throw new Exception('操作异常,现发货数量超过之前缺少的发货数量');
  71. }
  72. $model = new DeliveryGoods();
  73. $model->delivery_id = $delivery_id;
  74. $model->order_goods_id = $orderGoods->id;
  75. $model->goods_id = $orderGoods->goods_id;
  76. $model->goods_name = $orderGoods->goods_name;
  77. $model->goods_count = $goodsCount;
  78. if (!$model->save()) {
  79. throw new Exception('delivery_goods save false');
  80. }
  81. }
  82. return $status;
  83. }
  84. /**
  85. * @param $order_id
  86. * @return array
  87. * @throws Exception
  88. * 获取发货全部商品信息
  89. */
  90. public static function deliveryGoodsInfo($order_id)
  91. {
  92. $delivery = Delivery::find()->select('id')->where(['order_id' => $order_id])->all();
  93. /*如果该订单是首次发货*/
  94. if (!$delivery) {
  95. $unShippedGoods = OrderGoods::find()->where(['order_id' => $order_id])->asArray()->all();
  96. return ['unShipped' => $unShippedGoods];
  97. }
  98. $filter = [];
  99. $deliveryIds = [];
  100. foreach ($delivery as $value) {
  101. $deliveryIds[] = $value->id;
  102. }
  103. $deliveryGoods = DeliveryGoods::find()->where(['delivery_id' => $deliveryIds])->all();
  104. for ($i = 0; $i < count($deliveryGoods); $i++) {
  105. $orderGoodsId = $deliveryGoods[$i]['order_goods_id'];
  106. //以orderGoods的id为键名,以orderGoods的数量为键值,先保存到filter数组中
  107. $filter[$orderGoodsId] = $deliveryGoods[$i]['goods_count'];
  108. for ($j = 0; $j < count($deliveryGoods); $j++) {
  109. //如果发货商品中有其他相同orderGoods的商品
  110. if ($orderGoodsId == $deliveryGoods[$j]['order_goods_id'] && $i != $j) {
  111. //商品数量叠加起来并保存
  112. $filter[$orderGoodsId] += $deliveryGoods[$j]['goods_count'];
  113. }
  114. }
  115. }
  116. /*获取订单已发的商品和未发的商品*/
  117. return self::getDeliveryGoodsInfo($filter, $order_id);
  118. }
  119. /**
  120. * @param $filter
  121. * @param $order_id
  122. * @return array
  123. * @throws Exception
  124. * 获取订单已发的商品和未发的商品
  125. */
  126. public static function getDeliveryGoodsInfo($filter, $order_id)
  127. {
  128. $unShipped = []; //未发货商品
  129. foreach ($filter as $k => $value) { //键名为order_goods_id,键值为对应的已发货数量
  130. $goodsData = self::getOrderGoodsInfo($k);
  131. if ($value < $goodsData['goods_count']) { //如果已发货数量未达到order_goods里的数量
  132. $lack_number = $goodsData['goods_count'] - $value;
  133. $goodsData['lack_number'] = $lack_number;
  134. $goodsData['goods_count'] = $lack_number;
  135. $unShipped[] = $goodsData;
  136. }
  137. }
  138. $shipped = [];
  139. $delivery = Delivery::findAll(['order_id' => $order_id]);
  140. foreach ($delivery as $k => $value) {
  141. $shipped[$k]['logisticInfo'] = [
  142. 'exp_name' => '',
  143. 'invoice_no' => '',
  144. 'created_at' => '',
  145. ]; //物流公司和运单号
  146. $shipped[$k]['goodsInfo'] = self::getDeliverGoodsInfo($value->id); //获取商品信息
  147. }
  148. return ['shipped' => $shipped, 'unShipped' => $unShipped];
  149. }
  150. /**
  151. * @param $delivery_id
  152. * @return array
  153. * @throws Exception
  154. * 通过获取订单已发货的商品信息
  155. */
  156. public static function getDeliverGoodsInfo($delivery_id)
  157. {
  158. $goodsInfo = [];
  159. $deliveryGoods = DeliveryGoods::find()
  160. ->where(['delivery_id' => $delivery_id])
  161. ->andWhere(['>', 'goods_count', 0])
  162. ->all();
  163. foreach ($deliveryGoods as $value) {
  164. $goodsData = self::getOrderGoodsInfo($value->order_goods_id);
  165. $goodsData['delivery_number'] = $value->goods_count;
  166. $goodsInfo[] = $goodsData;
  167. }
  168. return $goodsInfo;
  169. }
  170. /**
  171. * @param $order_goods_id
  172. * @return array
  173. * @throws Exception
  174. * 获取订单商品信息
  175. */
  176. private static function getOrderGoodsInfo($order_goods_id)
  177. {
  178. $orderGoods = OrderGoods::findOne($order_goods_id); //通过orderGoods_id找到订单商品
  179. if ($orderGoods) {
  180. $goodsData = [
  181. 'id' => $orderGoods->id,
  182. 'goods_id' => $orderGoods->goods_id,
  183. 'goods_name' => $orderGoods->goods_name,
  184. 'goods_count' => $orderGoods->goods_count,
  185. 'goods_img' => $orderGoods->goods_img,
  186. 'sku_value' => $orderGoods->sku_value,
  187. ];
  188. return $goodsData;
  189. } else {
  190. throw new Exception('order_goods not found');
  191. }
  192. }
  193. }