OrderItemController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\order\classes\OrderClass;
  4. use bizGhs\order\classes\OrderItemClass;
  5. use bizGhs\order\services\OrderItemService;
  6. use bizGhs\order\services\OrderService;
  7. use bizGhs\product\classes\ProductClass;
  8. use common\components\imgUtil;
  9. use common\components\util;
  10. use Yii;
  11. class OrderItemController extends BaseController
  12. {
  13. public $guestAccess = [];
  14. //赠送花材 ssh 20230411
  15. public function actionGiveItem()
  16. {
  17. $post = Yii::$app->request->post();
  18. $id = $post['id'] ?? 0;
  19. $num = $post['num'] ?? 0;
  20. if (is_numeric($num) == false || $num <= 0) {
  21. util::fail('请填写赠送数量');
  22. }
  23. $orderItem = OrderItemClass::getById($id, true);
  24. if (empty($orderItem)) {
  25. util::fail('没有找到花材');
  26. }
  27. if ($orderItem->mainId != $this->mainId) {
  28. util::fail('没有权限操作');
  29. }
  30. $connection = Yii::$app->db;//事务处理
  31. $transaction = $connection->beginTransaction();
  32. try {
  33. $staff = $this->shopAdmin;
  34. $staffName = $staff->name ?? '';
  35. $staffId = $staff->id ?? 0;
  36. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  37. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  38. $transaction->commit();
  39. util::success(['info' => $respond], '赠送成功');
  40. } catch (\Exception $e) {
  41. $transaction->rollBack();
  42. Yii::info("赠送出错了:" . $e->getMessage());
  43. util::fail('出错了');
  44. }
  45. }
  46. //重选花材 ssh 2021.3.2
  47. public function actionReset()
  48. {
  49. $post = Yii::$app->request->post();
  50. $id = $post['id'] ?? 0;
  51. $product = $post['product'] ?? '[]';
  52. $product = json_decode($product, true);
  53. //检查花材是否都是同家门店的
  54. ProductClass::valid($product, $this->mainId);
  55. $info = OrderService::getOrderInfo($id);
  56. OrderClass::valid($info, $this->shopId);
  57. //添加修改花材
  58. $orderSn = $info['orderSn'] ?? '';
  59. OrderItemClass::replaceItem($orderSn, $product);
  60. util::complete();
  61. }
  62. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  63. public function actionGetOrderProduct()
  64. {
  65. $id = Yii::$app->request->get('id', 0);
  66. $order = OrderClass::getById($id, true);
  67. OrderClass::valid($order, $this->shopId);
  68. $orderSn = $order->orderSn ?? '';
  69. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  70. $ids = array_column($itemList, 'productId');
  71. if (empty($ids)) {
  72. util::fail('没有找到花材');
  73. }
  74. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  75. if (empty($productList)) {
  76. util::fail('没有找到花材信息');
  77. }
  78. foreach ($productList as $key => $product) {
  79. $productId = $product['id'] ?? 0;
  80. $currentItem = $itemList[$productId] ?? [];
  81. $bigCount = $currentItem['bigNum'] ?? 0;
  82. $smallCount = $currentItem['smallNum'] ?? 0;
  83. $userPrice = $currentItem['userPrice'] ?? 0;
  84. $productList[$key]['bigCount'] = $bigCount;
  85. $productList[$key]['smallCount'] = $smallCount;
  86. $productList[$key]['userPrice'] = $userPrice;
  87. $cover = imgUtil::groupImg($product['cover']);
  88. $productList[$key]['cover'] = $cover;
  89. $bigPrice = $product['unitPrice'] ?? 0;
  90. $smallPrice = $product['smallUnitPrice'] ?? 0;
  91. $productList[$key]['bigPrice'] = $bigPrice;
  92. $productList[$key]['smallPrice'] = $smallPrice;
  93. $productList[$key]['cover'] = $cover;
  94. }
  95. util::success(['productList' => array_values($productList)]);
  96. }
  97. //根据花材id取订单列表 ssh 20211011
  98. public function actionGetOrderInfoList()
  99. {
  100. $get = Yii::$app->request->get();
  101. $productId = $get['productId'] ?? 0;
  102. $product = ProductClass::getById($productId, true);
  103. ProductClass::check($product, $this->mainId);
  104. $where = ['productId' => $productId];
  105. $respond = OrderItemClass::getOrderInfoList($where);
  106. util::success($respond);
  107. }
  108. }