LiveOrderClass.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace bizGhs\live\classes;
  3. use bizGhs\base\classes\BaseClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\product\classes\ProductClass;
  6. use common\components\imgUtil;
  7. use common\components\orderSn;
  8. use common\components\util;
  9. class LiveOrderClass extends BaseClass
  10. {
  11. public static $baseFile = '\bizGhs\live\models\LiveOrder';
  12. public static function updateCustom($customData, $live)
  13. {
  14. $orderSn = $live->orderSn ?? '';
  15. $mainId = $live->mainId ?? 0;
  16. $customMap = [];
  17. $ids = [];
  18. foreach ($customData as $key => $val) {
  19. $customId = $val['customId'] ?? 0;
  20. $customMap[$customId] = $val;
  21. $ids[] = $customId;
  22. }
  23. $currentList = CustomClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  24. if (!empty($currentList)) {
  25. foreach ($currentList as $current) {
  26. if ($current->ownMainId != $mainId) {
  27. util::fail('不是你的客户');
  28. }
  29. }
  30. }
  31. $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  32. if (empty($customList)) {
  33. return false;
  34. }
  35. $totalNum = 0;
  36. $totalAmount = 0;
  37. foreach ($customList as $k => $info) {
  38. $customId = $info->customId ?? 0;
  39. $num = isset($customMap[$customId]) && isset($customMap[$customId]['num']) ? $customMap[$customId]['num'] : 0;
  40. $unitPrice = isset($customMap[$customId]) && isset($customMap[$customId]['unitPrice']) ? $customMap[$customId]['unitPrice'] : 0;
  41. $price = bcmul($num, $unitPrice, 2);
  42. $info->num = $num;
  43. $info->unitPrice = $unitPrice;
  44. $info->price = $price;
  45. $info->save();
  46. $totalNum = bcadd($totalNum, $num);
  47. $totalAmount = bcadd($totalAmount, $price, 2);
  48. }
  49. $live->itemNum = $totalNum;
  50. $live->itemAmount = $totalAmount;
  51. $live->save();
  52. }
  53. public static function delCustom($custom, $live)
  54. {
  55. $customId = $custom->id ?? 0;
  56. $orderSn = $live->orderSn ?? '';
  57. $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  58. $num = 0;
  59. $price = 0;
  60. if (!empty($customList)) {
  61. foreach ($customList as $it) {
  62. $currentCustomId = $it->customId ?? 0;
  63. if ($currentCustomId == $customId) {
  64. $num = $it->num ?? 0;
  65. $price = $it->price ?? 0;
  66. $it->delete();
  67. }
  68. }
  69. }
  70. $live->customNum = bcsub($live->customNum, 1);
  71. $live->itemNum = bcsub($live->itemNum, $num);
  72. $live->itemAmount = bcsub($live->itemAmount, $price, 2);
  73. $live->save();
  74. }
  75. public static function addCustom($params, $custom, $live)
  76. {
  77. if ($custom->mainId == $live->mainId) {
  78. util::fail('自己的店,不能添加');
  79. }
  80. $customId = $custom->id ?? 0;
  81. $orderSn = $live->orderSn ?? '';
  82. $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  83. if (!empty($customList)) {
  84. foreach ($customList as $it) {
  85. $currentCustomId = $it->customId ?? 0;
  86. if ($currentCustomId == $customId) {
  87. util::fail('客户已经存在');
  88. }
  89. }
  90. }
  91. $customName = $custom->name ?? '';
  92. $seatSn = $custom->seatSn ?? 0;
  93. //分配货位
  94. if (empty($seatSn)) {
  95. $mainId = $live->mainId ?? 0;
  96. $seatRes = LiveSeatClass::getByCondition(['mainId' => $mainId, 'customId' => 0], true);
  97. if (empty($seatRes)) {
  98. util::fail('没有货位可分配哦');
  99. }
  100. $seatResId = $seatRes->id ?? 0;
  101. $seatRes = LiveSeatClass::getLockById($seatResId);
  102. $newSeatSn = $seatRes->no;
  103. $custom->seatSn = $newSeatSn;
  104. $custom->visitTime = date("Y-m-d H:i:s");
  105. $custom->save();
  106. $seatRes->customId = $custom->id;
  107. $seatRes->save();
  108. }
  109. $num = $params['num'] ?? 0;
  110. $unitPrice = $params['price'] ?? 0;
  111. $price = bcmul($unitPrice, $num, 2);
  112. $data = [
  113. 'mainId' => $live->mainId,
  114. 'orderSn' => $orderSn,
  115. 'customName' => $customName,
  116. 'customId' => $customId,
  117. 'num' => $num,
  118. 'unitPrice' => $unitPrice,
  119. 'unitName' => '',
  120. 'price' => $price,
  121. ];
  122. LiveOrderCustomClass::add($data, true);
  123. $live->customNum = bcadd($live->customNum, 1);
  124. $live->itemNum = bcadd($live->itemNum, $num);
  125. $live->itemAmount = bcadd($live->itemAmount, $price, 2);
  126. $live->save();
  127. }
  128. public static function getOrderList($where)
  129. {
  130. $data = self::getList('*', $where, 'addTime DESC');
  131. if (isset($data['list']) && !empty($data['list'])) {
  132. $ids = array_column($data['list'], 'itemId');
  133. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,stock', 'id');
  134. foreach ($data['list'] as $key => $val) {
  135. $shortCover = $val['itemCover'] ?? '';
  136. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  137. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  138. $largeCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_990,w_990";
  139. $data['list'][$key]['shortCover'] = $shortCover;
  140. $data['list'][$key]['cover'] = $cover;
  141. $data['list'][$key]['bigCover'] = $bigCover;
  142. $data['list'][$key]['largeCover'] = $largeCover;
  143. $itemId = $val['itemId'] ?? 0;
  144. $stock = isset($productList[$itemId]) && isset($productList[$itemId]['stock']) ? floatval($productList[$itemId]['stock']) : 0;
  145. $data['list'][$key]['stock'] = $stock;
  146. }
  147. }
  148. return $data;
  149. }
  150. public static function addOrder($params, $mainId)
  151. {
  152. $product = $params['product'];
  153. $itemId = $product->id ?? 0;
  154. $ptItemId = $product->itemId ?? 0;
  155. $itemName = $product->name ?? '';
  156. $itemPy = $product->py ?? '';
  157. $itemCover = $product->cover ?? '';
  158. $customList = $params['customList'];
  159. $staffId = $params['staffId'] ?? 0;
  160. $staffName = $params['staffName'] ?? '';
  161. $itemPrice = $params['itemPrice'] ?? 0;
  162. $itemRemark = $params['itemRemark'] ?? '';
  163. $customNum = count($customList);
  164. $itemNum = 0;
  165. $itemAmount = 0;
  166. foreach ($customList as $custom) {
  167. $num = $custom['num'] ?? 0;
  168. $unitPrice = $custom['price'] ?? 0;
  169. $itemNum = bcadd($num, $itemNum);
  170. $price = bcmul($unitPrice, $num, 2);
  171. $itemAmount = bcadd($itemAmount, $price, 2);
  172. }
  173. $orderSn = orderSn::getLiveOrderSn();
  174. $liveData = [
  175. 'mainId' => $mainId,
  176. 'itemId' => $itemId,
  177. 'ptItemId' => $ptItemId,
  178. 'itemName' => $itemName,
  179. 'itemPy' => $itemPy,
  180. 'itemCover' => $itemCover,
  181. 'customNum' => $customNum,
  182. 'itemNum' => $itemNum,
  183. 'itemAmount' => $itemAmount,
  184. 'orderSn' => $orderSn,
  185. 'staffId' => $staffId,
  186. 'staffName' => $staffName,
  187. 'itemPrice' => $itemPrice,
  188. 'itemRemark' => $itemRemark,
  189. ];
  190. self::add($liveData, true);
  191. $ids = array_column($customList, 'customId');
  192. $infoList = CustomClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  193. foreach ($customList as $it) {
  194. $customId = $it['customId'] ?? 0;
  195. $num = $it['num'] ?? 0;
  196. $unitName = $product->bigUnit ?? '扎';
  197. $unitPrice = $it['price'] ?? 0;
  198. $price = bcmul($num, $unitPrice, 2);
  199. $customInfo = $infoList[$customId] ?? null;
  200. if (empty($customInfo)) {
  201. util::fail('客户信息缺失,编号673');
  202. }
  203. $customName = $customInfo->name ?? '';
  204. $customMainId = $customInfo->mainId ?? 0;
  205. if ($mainId == $customMainId) {
  206. util::fail($customName . ' 是自己的店');
  207. }
  208. $seatSn = $customInfo->seatSn ?? 0;
  209. //分配货位
  210. if (empty($seatSn)) {
  211. $seatRes = LiveSeatClass::getByCondition(['mainId' => $mainId, 'customId' => 0], true);
  212. if (empty($seatRes)) {
  213. util::fail('没有货位可分配哈');
  214. }
  215. $seatResId = $seatRes->id ?? 0;
  216. $seatRes = LiveSeatClass::getLockById($seatResId);
  217. $newSeatSn = $seatRes->no;
  218. $customInfo->seatSn = $newSeatSn;
  219. $customInfo->visitTime = date("Y-m-d H:i:s");
  220. $customInfo->save();
  221. $seatRes->customId = $customInfo->id;
  222. $seatRes->save();
  223. }
  224. $customData = [
  225. 'mainId' => $mainId,
  226. 'orderSn' => $orderSn,
  227. 'customName' => $customName,
  228. 'customId' => $customId,
  229. 'num' => $num,
  230. 'unitName' => $unitName,
  231. 'unitPrice' => $unitPrice,
  232. 'price' => $price,
  233. ];
  234. LiveOrderCustomClass::add($customData, true);
  235. }
  236. }
  237. }