WastageController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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) == 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) {
  58. util::fail('花材数量不能为0');
  59. }
  60. if($smallNum > 0){
  61. util::fail('小单位不能报损');
  62. }
  63. }
  64. $connection = Yii::$app->db;
  65. $transaction = $connection->beginTransaction();
  66. try {
  67. //判断花材有效性
  68. ProductClass::valid($productList, $this->mainId);
  69. $post['product'] = $productList;
  70. $return = StockWastageOrderClass::addOrder($post,$this->shop,true);
  71. $transaction->commit();
  72. util::success($return);
  73. } catch (\Exception $e) {
  74. $transaction->rollBack();
  75. Yii::info("报损失败原因:" . $e->getMessage());
  76. util::fail('报损保存失败');
  77. }
  78. }
  79. //列表
  80. public function actionList()
  81. {
  82. $where = [];
  83. $where['mainId'] = $this->mainId;
  84. $list = StockWastageOrderClass::getOrderList($where);
  85. util::success($list);
  86. }
  87. public function actionDetail()
  88. {
  89. $orderSn = Yii::$app->request->get('orderSn', '');
  90. $orderData = StockWastageOrderClass::getOrderDetail($orderSn, $this->mainId);
  91. util::success($orderData);
  92. }
  93. }