OrderChangePrice.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/20
  6. * Time: 下午5:03
  7. */
  8. namespace app\frontend\modules\order\services\behavior;
  9. use app\common\exceptions\AppException;
  10. use app\common\models\Order;
  11. use app\common\models\order\OrderChangePriceLog;
  12. use app\common\models\order\OrderGoodsChangePriceLog;
  13. use app\common\models\OrderGoods;
  14. use app\frontend\modules\goods\services\models\CreatedOrderGoodsModel;
  15. use app\frontend\modules\order\services\models\CreatedOrderModel;
  16. use function foo\func;
  17. class OrderChangePrice extends OrderOperation
  18. {
  19. protected $statusBeforeChange = [ORDER::WAIT_PAY];
  20. protected $name = '改价';
  21. protected $past_tense_class_name = 'OrderChangedPrice';
  22. /**
  23. * 更新数据库表
  24. * @return bool
  25. */
  26. protected function updateTable()
  27. {
  28. $this->push();
  29. return true;
  30. }
  31. /**
  32. * 订单改价操作
  33. * @return bool|void
  34. * @throws \app\common\exceptions\AppException
  35. */
  36. public function handle()
  37. {
  38. parent::handle();
  39. $this->changeOrderGoodsPrice();
  40. $this->calculateOrderChangePriceLog();
  41. $this->changePrice();
  42. $this->updateTable();
  43. }
  44. /**
  45. * 设置订单改价记录
  46. */
  47. public function setOrderChangePriceLog()
  48. {
  49. $orderChangePriceLog = new OrderChangePriceLog();
  50. $this->setRelation('orderChangePriceLog', $orderChangePriceLog);
  51. }
  52. /**
  53. * 设置核销员ID
  54. */
  55. public function setClerkId($clerk_id)
  56. {
  57. return $this->orderChangePriceLog->clerk_id = $clerk_id;
  58. }
  59. /**
  60. * 设置核销员来源
  61. */
  62. public function setClerkType($clerk_type)
  63. {
  64. return $this->orderChangePriceLog->clerk_type = $clerk_type;
  65. }
  66. /**
  67. * 订单改价记录
  68. */
  69. private function calculateOrderChangePriceLog()
  70. {
  71. $orderChangePriceLog = $this->orderChangePriceLog;
  72. $orderChangePriceLog->change_price = $this->hasManyOrderGoods->sum(function ($orderGoods) {
  73. return $orderGoods->orderGoodsChangePriceLog->change_price;
  74. });
  75. $orderChangePriceLog->old_price = $this->price;
  76. $orderChangePriceLog->new_price = max($this->price + $orderChangePriceLog->change_price + $this->getChangeDispatchPrice(),0);
  77. $orderChangePriceLog->username = \Yunshop::app()->username ? : '';
  78. $orderChangePriceLog->clerk_type = $orderChangePriceLog->clerk_type ? : '';
  79. $orderChangePriceLog->clerk_id = $orderChangePriceLog->clerk_id ? : 0;
  80. $orderChangePriceLog->order_id = $this->id;
  81. //return $orderChangePriceLog;
  82. }
  83. /**
  84. * 设置运费改价金额
  85. * @param $dispatch_price
  86. * @return mixed
  87. */
  88. public function setDispatchChangePrice($dispatch_price)
  89. {
  90. $dispatch_price = $dispatch_price > 0 ?$dispatch_price:0;
  91. return $this->orderChangePriceLog->change_dispatch_price = $dispatch_price - $this->dispatch_price;
  92. }
  93. /**
  94. * 获取运费改价金额
  95. * @return mixed
  96. */
  97. private function getDispatchChangePrice()
  98. {
  99. return $this->orderChangePriceLog->change_dispatch_price;
  100. }
  101. /**
  102. * 获取运费价格
  103. * @return mixed
  104. */
  105. private function getDispatchPrice()
  106. {
  107. return max($this->dispatch_price + $this->getDispatchChangePrice(),0);
  108. }
  109. /**
  110. * 获取订单改价金额
  111. * @return mixed
  112. */
  113. private function getChangePrice()
  114. {
  115. return $this->orderChangePriceLog->change_price;
  116. }
  117. /**
  118. * 获取订单商品小计
  119. * @return mixed
  120. */
  121. private function getOrderGoodsPrice()
  122. {
  123. return $this->order_goods_price + $this->getChangePrice();
  124. }
  125. /**
  126. * 获取订单价格
  127. * @return mixed
  128. */
  129. private function getPrice()
  130. {
  131. return max($this->price + $this->getChangePrice() + $this->getDispatchChangePrice(),0);
  132. }
  133. private function getChangeDispatchPrice()
  134. {
  135. return $this->orderChangePriceLog->change_dispatch_price;
  136. }
  137. /**
  138. * 更新订单价格
  139. */
  140. public function changePrice()
  141. {
  142. $this->price = $this->getPrice();
  143. $this->dispatch_price = $this->getDispatchPrice();
  144. $this->order_goods_price = $this->getOrderGoodsPrice();
  145. $this->change_price += $this->getChangePrice();
  146. $this->change_dispatch_price += $this->getChangeDispatchPrice();
  147. }
  148. /**
  149. * 设置订单商品改价记录
  150. * @param $orderGoodsChangePriceLogs
  151. */
  152. public function setOrderGoodsChangePriceLogs($orderGoodsChangePriceLogs)
  153. {
  154. $this->hasManyOrderGoods->map(function ($orderGoods) use ($orderGoodsChangePriceLogs) {
  155. $orderGoodsChangePriceLog = $orderGoodsChangePriceLogs->where('order_goods_id', $orderGoods->id)->first();
  156. //实例化改价记录
  157. $orderGoodsChangePriceLog->old_price = $orderGoods->payment_amount;
  158. if (($orderGoods->payment_amount + $orderGoodsChangePriceLog->change_price) < 0) {
  159. $msg = $orderGoods->goods_option_title? '('.$orderGoods->goods_option_title.')':'';
  160. throw new AppException(''.$orderGoods->title.$msg.':改价金额大于实付金额');
  161. }
  162. $orderGoodsChangePriceLog->new_price = max($orderGoods->payment_amount + $orderGoodsChangePriceLog->change_price, 0);
  163. $orderGoods->setRelation('orderGoodsChangePriceLog', $orderGoodsChangePriceLog);
  164. });
  165. }
  166. /**
  167. * 更新订单商品model
  168. */
  169. private function changeOrderGoodsPrice()
  170. {
  171. $this->hasManyOrderGoods->map(function ($orderGoods) {
  172. //更新商品信息
  173. // $orderGoods->price = $orderGoods->orderGoodsChangePriceLog->new_price;
  174. $orderGoods->payment_amount = $orderGoods->orderGoodsChangePriceLog->new_price; //改价修改商品实付金额
  175. $orderGoods->change_price += $orderGoods->orderGoodsChangePriceLog->change_price;
  176. });
  177. }
  178. }