WastController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\stat\classes\StatWastClass;
  5. use bizGhs\order\classes\StockWastageOrderClass;
  6. use bizGhs\order\classes\StockWastageOrderItemClass;
  7. use yii\console\Controller;
  8. use Yii;
  9. class WastController extends Controller
  10. {
  11. //报损数据更新 ./yii wast/update
  12. public function actionUpdate()
  13. {
  14. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], 'id asc', '*', null, true);
  15. if (!empty($list)) {
  16. foreach ($list as $item) {
  17. $orderSn = $item->orderSn ?? '';
  18. $shopId = $item->shopId ?? 0;
  19. $addTime = $item->addTime ?? '';
  20. $connection = Yii::$app->db;//事务处理
  21. $transaction = $connection->beginTransaction();
  22. try {
  23. $shop = ShopClass::getLockById($shopId);
  24. if (empty($shop)) {
  25. echo '没有找到店铺 ' . $shopId . ' ' . $orderSn . "\n";
  26. continue;
  27. }
  28. $productList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  29. if (!empty($productList)) {
  30. $wast = 0;
  31. foreach ($productList as $product) {
  32. $num = $product->itemNum ?? 0;
  33. $cost = $product->itemCost ?? 0;
  34. $amount = bcmul($num, $cost, 2);
  35. $wast = bcadd($amount, $wast, 2);
  36. }
  37. $totalWast = bcadd($wast, $shop->totalWast, 2);
  38. $shop->totalWast = $totalWast;
  39. $shop->save();
  40. $date = date("Ymd", strtotime($addTime));
  41. StatWastClass::replace($shop, $wast, $date);
  42. }
  43. $transaction->commit();
  44. } catch (\Exception $e) {
  45. $transaction->rollBack();
  46. $msg = $e->getMessage();
  47. echo "报错了:" . $msg . "\n";
  48. }
  49. }
  50. }
  51. }
  52. }