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.

216 lines
7.8 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. $deliveryIds = [];
  99. foreach ($delivery as $value) {
  100. $deliveryIds[] = $value->id;
  101. }
  102. $filter = [];
  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. // $expressTemplate = ExpressTemplate::find()
  142. // ->select('name')
  143. // ->where(['id' => $value->shipping_id])
  144. // ->one();
  145. // $deliveryInfo[$k]['logisticInfo'] = [
  146. // 'exp_name'=> $expressTemplate ? $expressTemplate->name : '',
  147. // 'invoice_no'=> $value->invoice_no
  148. // ]; //物流公司和运单号
  149. $shipped[$k]['goodsInfo'] = self::getDeliverGoodsInfo($value->id); //获取商品信息
  150. }
  151. return ['shipped' => $shipped, 'unShipped' => $unShipped];
  152. }
  153. /**
  154. * @param $delivery_id
  155. * @return array
  156. * @throws Exception
  157. * 通过获取订单已发货的商品信息
  158. */
  159. public static function getDeliverGoodsInfo($delivery_id)
  160. {
  161. $goodsInfo = [];
  162. $deliveryGoods = DeliveryGoods::find()
  163. ->where(['delivery_id' => $delivery_id])
  164. ->andWhere(['>', 'goods_count', 0])
  165. ->all();
  166. foreach ($deliveryGoods as $value) {
  167. $goodsData = self::getOrderGoodsInfo($value->order_goods_id);
  168. $goodsData['delivery_number'] = $value->goods_count;
  169. $goodsInfo[] = $goodsData;
  170. }
  171. return $goodsInfo;
  172. }
  173. /**
  174. * @param $order_goods_id
  175. * @return array
  176. * @throws Exception
  177. * 获取订单商品信息
  178. */
  179. private static function getOrderGoodsInfo($order_goods_id)
  180. {
  181. $orderGoods = OrderGoods::findOne($order_goods_id); //通过orderGoods_id找到订单商品
  182. if ($orderGoods) {
  183. $goodsData = [
  184. 'id' => $orderGoods->id,
  185. 'goods_id' => $orderGoods->goods_id,
  186. 'goods_name' => $orderGoods->goods_name,
  187. 'goods_count' => $orderGoods->goods_count,
  188. 'goods_img' => $orderGoods->goods_img,
  189. 'sku_value' => $orderGoods->sku_value,
  190. ];
  191. return $goodsData;
  192. } else {
  193. throw new Exception('order_goods not found');
  194. }
  195. }
  196. }