StatItemClass.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace biz\stat\classes;
  3. use biz\base\classes\BaseClass;
  4. use biz\stat\models\StatCg;
  5. use common\components\util;
  6. class StatItemClass extends BaseClass
  7. {
  8. public static $baseFile = '\biz\stat\models\StatItem';
  9. //每日花材统计 shish 2021012
  10. public static function replace($item, $date = null)
  11. {
  12. $sjId = $item->sjId ?? 0;
  13. $shopId = $item->shopId ?? 0;
  14. $productId = $item->productId ?? 0;
  15. $time = $date == null ? date('Ymd') : $date;
  16. $stat = self::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'itemId' => $productId, 'time' => $time], true);
  17. if (empty($stat)) {
  18. $stat = self::add(['shopId' => $shopId, 'sjId' => $sjId, 'itemId' => $productId, 'time' => $time], true);
  19. }
  20. $id = $stat->id;
  21. $unique = self::getLockById($id);
  22. $amount = $item->price ?? 0;
  23. $num = $item->num ?? 0;
  24. $currentNum = bcadd($unique->num, $num, 2);
  25. $unique->num = $currentNum;
  26. $currentAmount = bcadd($unique->amount, $amount, 2);
  27. $unique->amount = $currentAmount;
  28. $unique->save();
  29. StatItemMonthClass::replace($item, $time);
  30. }
  31. //退款花材
  32. public static function refundReplace($item, $date = null)
  33. {
  34. $mainId = $item['mainId'] ?? 0;
  35. $productId = $item['productId'] ?? 0;
  36. $time = $date == null ? date('Ymd') : $date;
  37. $stat = self::getByCondition(['mainId' => $mainId, 'itemId' => $productId, 'time' => $time], true);
  38. if (empty($stat)) {
  39. $stat = self::add(['mainId' => $mainId, 'itemId' => $productId, 'time' => $time], true);
  40. }
  41. $id = $stat->id;
  42. $unique = self::getLockById($id);
  43. $num = $item['itemNum'] ?? 0;
  44. $unitePrice = $item['itemPrice'] ?? 0;
  45. $price = bcmul($unitePrice, $num, 2);
  46. $currentRefundNum = bcadd($unique->refundNum, $num, 2);
  47. $currentRefundAmount = bcadd($unique->refundAmount, $price, 2);
  48. $unique->refundNum = $currentRefundNum;
  49. $unique->refundAmount = $currentRefundAmount;
  50. $unique->save();
  51. StatItemMonthClass::refundReplace($item, $time);
  52. }
  53. }