WastageController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace hd\controllers;
  3. use bizGhs\order\classes\StockWastageOrderClass;
  4. use bizGhs\product\classes\ProductClass;
  5. use common\components\dict;
  6. use common\components\util;
  7. use Yii;
  8. //已中央化 ssh 20220319
  9. class WastageController extends BaseController
  10. {
  11. public $guestAccess = [];
  12. //新增损耗
  13. public function actionCreateOrder()
  14. {
  15. $shopAdmin = $this->shopAdmin;
  16. if (isset($shopAdmin->super) == false || $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. $connection = Yii::$app->db;//事务处理
  62. $transaction = $connection->beginTransaction();
  63. try {
  64. ProductClass::valid($productList, $this->mainId);
  65. $post['product'] = $productList;
  66. $return = StockWastageOrderClass::addOrder($post);
  67. $transaction->commit();
  68. util::success($return);
  69. } catch (\Exception $e) {
  70. $transaction->rollBack();
  71. Yii::info("报损失败原因:" . $e->getMessage());
  72. util::fail('报损保存失败');
  73. }
  74. }
  75. //列表
  76. public function actionList()
  77. {
  78. $where = [];
  79. $where['mainId'] = $this->mainId;
  80. $list = StockWastageOrderClass::getOrderList($where);
  81. util::success($list);
  82. }
  83. public function actionDetail()
  84. {
  85. $orderSn = Yii::$app->request->get('orderSn', '');
  86. $orderData = StockWastageOrderClass::getOrderDetail($orderSn, $this->mainId);
  87. util::success($orderData);
  88. }
  89. }