WastageController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. //损耗
  3. namespace ghs\controllers;
  4. use Yii;
  5. use bizGhs\order\classes\StockWastageOrderClass;
  6. use common\components\util;
  7. use bizGhs\product\classes\ProductClass;
  8. class WastageController extends BaseController
  9. {
  10. //新增损耗
  11. public function actionCreateOrder()
  12. {
  13. $post = Yii::$app->request->post();
  14. $post['shopId'] = $this->shopId;
  15. $post['sjId'] = $this->sjId;
  16. $post['adminId'] = $this->adminId;
  17. $productJson = $post['product'] ?? '';
  18. if (empty($productJson)) {
  19. util::fail('请选择花材');
  20. }
  21. $productList = json_decode($productJson, true);
  22. if (empty($productList)) {
  23. util::fail('请选择花材');
  24. }
  25. //判断花材里的信息
  26. foreach ($productList as $v) {
  27. $bigNum = $v['bigNum'] ?? 0;
  28. $smallNum = $v['smallNum'] ?? 0;
  29. if ($bigNum <= 0 && $smallNum <= 0) {
  30. util::fail('花材数量不能为0');
  31. }
  32. }
  33. $connection = Yii::$app->db;//事务处理
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. //判断花材有效性
  37. ProductClass::valid($productList, $this->shopId);
  38. $post['product'] = $productList;
  39. $return = StockWastageOrderClass::addOrder($post);
  40. $transaction->commit();
  41. util::success($return);
  42. } catch (\Exception $e) {
  43. $transaction->rollBack();
  44. Yii::info("报损失败原因:" . $e->getMessage());
  45. util::fail('报损保存失败');
  46. }
  47. }
  48. }