WastageController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. //损耗
  3. namespace ghs\controllers;
  4. use common\components\dict;
  5. use Yii;
  6. use bizGhs\order\classes\StockWastageOrderClass;
  7. use common\components\util;
  8. use bizGhs\product\classes\ProductClass;
  9. //已中央化
  10. class WastageController extends BaseController
  11. {
  12. //新增损耗
  13. public function actionCreateOrder()
  14. {
  15. $shopAdmin = $this->shopAdmin;
  16. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  17. util::fail('超管才能报损');
  18. }
  19. $post = Yii::$app->request->post();
  20. $post['shopId'] = $this->shopId;
  21. $post['sjId'] = $this->sjId;
  22. $post['mainId'] = $this->mainId;
  23. $post['shopAdminId'] = $this->shopAdminId;
  24. $shopAdmin = $this->shopAdmin;
  25. $adminName = $shopAdmin['name'] ?? '';
  26. $post['shopAdminName'] = $adminName;
  27. $productJson = $post['product'] ?? '';
  28. if (empty($productJson)) {
  29. util::fail('请选择花材');
  30. }
  31. $productList = json_decode($productJson, true);
  32. if (empty($productList)) {
  33. util::fail('请选择花材');
  34. }
  35. //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
  36. $hasUnitType = array_column($productList, 'unitType');
  37. if (!empty($hasUnitType)) {
  38. $newProductList = [];
  39. foreach ($productList as $key => $val) {
  40. $unitType = $val['unitType'] ?? 0;
  41. $bigNum = 0;
  42. $smallNum = 0;
  43. $num = $val['num'] ?? 0;
  44. $id = $val['productId'] ?? 0;
  45. if ($unitType == dict::getDict('unitType', 'big')) {
  46. $bigNum = $num;
  47. } else {
  48. $smallNum = $num;
  49. }
  50. $newProductList[] = ['productId' => $id, 'bigNum' => $bigNum, 'smallNum' => $smallNum, 'classId' => 0, 'itemId' => 0];
  51. }
  52. $productList = $newProductList;
  53. }
  54. foreach ($productList as $v) {
  55. $bigNum = $v['bigNum'] ?? 0;
  56. $smallNum = $v['smallNum'] ?? 0;
  57. if ($bigNum <= 0 && $smallNum <= 0) {
  58. util::fail('花材数量不能为0');
  59. }
  60. }
  61. try {
  62. $return = util::runWithDbConcurrencyRetry(function () use ($post, $productList) {
  63. $connection = Yii::$app->db;
  64. $transaction = $connection->beginTransaction();
  65. try {
  66. //判断花材有效性
  67. ProductClass::valid($productList, $this->mainId);
  68. $post['product'] = $productList;
  69. $return = StockWastageOrderClass::addOrder($post, $this->shop, true);
  70. $transaction->commit();
  71. return $return;
  72. } catch (\Exception $exception) {
  73. if ($transaction->isActive) {
  74. $transaction->rollBack();
  75. }
  76. throw $exception;
  77. }
  78. });
  79. util::success($return);
  80. } catch (\Exception $e) {
  81. Yii::info("报损失败原因:" . $e->getMessage());
  82. if (util::isDbConcurrencyException($e)) {
  83. util::fail('系统繁忙中,请稍后再试');
  84. } else {
  85. util::fail('报损保存失败');
  86. }
  87. }
  88. }
  89. //列表
  90. public function actionList()
  91. {
  92. $where = [];
  93. $where['mainId'] = $this->mainId;
  94. $list = StockWastageOrderClass::getOrderList($where);
  95. util::success($list);
  96. }
  97. public function actionDetail()
  98. {
  99. $orderSn = Yii::$app->request->get('orderSn', '');
  100. $orderData = StockWastageOrderClass::getOrderDetail($orderSn, $this->mainId);
  101. util::success($orderData);
  102. }
  103. }