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.

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