ClearClass.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace bizGhs\clear\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizGhs\base\classes\BaseClass;
  6. use bizGhs\cg\classes\CgClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\classes\PurchaseOrderClass;
  10. use bizGhs\shop\classes\MainClass;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\classes\PurchaseClearClass;
  13. use common\components\dict;
  14. use common\components\orderSn;
  15. use common\components\util;
  16. use Yii;
  17. class ClearClass extends BaseClass
  18. {
  19. public static $baseFile = '\bizGhs\clear\models\Clear';
  20. const STATUS_AWAIT_PAY = 1;
  21. const STATUS_HAS_PAY = 2;
  22. const STATUS_EXPIRE = 3;
  23. public static function addOrder($data)
  24. {
  25. $orderSn = orderSn::getPurchaseClearSn();
  26. $idJson = $data['id'];
  27. $idArr = json_decode($idJson, true);
  28. $arr = array_column($idArr, 'id');
  29. if (empty($arr)) {
  30. util::fail('请选择采购单');
  31. }
  32. $purchaseList = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, ['id', 'actPrice', 'realPrice', 'debt', 'ghsId'], null, true);
  33. if (empty($purchaseList)) {
  34. util::fail('请选择采购单');
  35. }
  36. $totalPrice = 0;
  37. $currentGhsId = $data['ghsId'] ?? 0;
  38. foreach ($purchaseList as $purchase) {
  39. if ($purchase->debt != PurchaseOrderClass::DEBT_YES) {
  40. util::fail('请选择有欠款的采购单');
  41. }
  42. if ($purchase->ghsId != $currentGhsId) {
  43. util::fail('请选择同个供货商的订单');
  44. }
  45. $totalPrice = bcadd($totalPrice, $purchase->actPrice, 2);
  46. }
  47. $ghs = GhsClass::getById($currentGhsId, true);
  48. if (empty($ghs)) {
  49. util::fail('没有找到供货商');
  50. }
  51. GhsClass::valid($ghs, $data['shopId']);
  52. $ghsName = $ghs->name ?? '';
  53. $ghsAvatar = $ghs->avatar ?? '';
  54. $ghsMobile = $ghs->mobile ?? '';
  55. $customId = $ghs->customId ?? 0;
  56. $custom = CustomClass::getById($customId, true);
  57. $customName = $custom->name ?? '';
  58. $customAvatar = $custom->avatar ?? '';
  59. $customMobile = $custom->mobile ?? '';
  60. $data['ghsName'] = $ghsName;
  61. $data['ghsAvatar'] = $ghsAvatar;
  62. $data['ghsMobile'] = $ghsMobile;
  63. $data['customName'] = $customName;
  64. $data['customAvatar'] = $customAvatar;
  65. $data['customMobile'] = $customMobile;
  66. $data['prePrice'] = $totalPrice;
  67. $realPrice = $totalPrice;
  68. if (isset($data['modifyPrice']) && !empty($data['modifyPrice'])) {
  69. if (is_numeric($data['modifyPrice']) == false) {
  70. util::fail('实收金额错误');
  71. }
  72. $modifyPrice = $data['modifyPrice'] ?? 0.00;
  73. if ($modifyPrice > $totalPrice) {
  74. util::fail('实收金额不能大于订单金额');
  75. }
  76. if ($modifyPrice < $totalPrice) {
  77. $data['discountAmount'] = bcsub($totalPrice, $modifyPrice, 2);
  78. $data['discountType'] = dict::getDict('discountType', 'discount');
  79. }
  80. $realPrice = $modifyPrice;
  81. }
  82. $data['actPrice'] = $realPrice;
  83. $data['realPrice'] = $realPrice;
  84. $data['orderSn'] = $orderSn;
  85. $data['purchaseIds'] = implode(',', $arr);
  86. $deadTime = time() + 120;
  87. $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
  88. $data['status'] = self::STATUS_AWAIT_PAY;
  89. $order = self::add($data, true);
  90. $payWay = dict::getDict('payWay', 'wxPay');
  91. self::complete($order, $payWay);
  92. return $order;
  93. }
  94. //支付成功后的流程 ssh 2021.4.28
  95. public static function complete($order, $payWay)
  96. {
  97. if (empty($order)) {
  98. util::fail('没有找到订单');
  99. }
  100. if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
  101. util::fail('订单不是待付款状态');
  102. }
  103. $order->status = self::STATUS_HAS_PAY;
  104. $order->save();
  105. $clearId = $order->id;
  106. $purchaseIds = $order->purchaseIds;
  107. $arr = explode(',', $purchaseIds);
  108. if (empty($arr)) {
  109. $msg = "没有找到采购订单";
  110. util::fail($msg);
  111. }
  112. $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  113. if (empty($list)) {
  114. $msg = "没有找到采购订单!";
  115. util::fail($msg);
  116. }
  117. //供货商欠款变更
  118. $ghsId = $order->ghsId;
  119. $ghs = GhsClass::getLockById($ghsId);
  120. if (empty($ghs)) {
  121. util::fail('没有找到供货商信息');
  122. }
  123. $currentAmount = bcsub($ghs->debtAmount, $order->actPrice, 2);
  124. $ghs->debtAmount = $currentAmount;
  125. $ghs->debtNum -= count($arr);
  126. $ghs->debt = $currentAmount > 0 ? 2 : 1;
  127. $ghs->save();
  128. $shopId = $ghs->shopId;
  129. $shop = ShopClass::getLockById($shopId);
  130. if (empty($shop)) {
  131. util::fail('没有找到供货商门店');
  132. }
  133. $mainId = $shop->mainId ?? 0;
  134. $main = MainClass::getLockById($mainId);
  135. if (empty($main)) {
  136. util::fail('没有main信息');
  137. }
  138. //门店应收客户款减少
  139. $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
  140. $main->mayGathering = $currentGathering;
  141. $main->save();
  142. //采购单状态变更
  143. foreach ($list as $purchase) {
  144. $purchase->debt = PurchaseOrderClass::DEBT_NO;
  145. $purchase->payWay = $payWay;
  146. $purchase->clearId = $clearId;
  147. $purchase->save();
  148. }
  149. //客户欠款变化
  150. $customId = $ghs->customId;
  151. $custom = CustomClass::getLockById($customId);
  152. if (empty($custom)) {
  153. util::fail('没有找到客户信息');
  154. }
  155. $remain = bcsub($custom->debtAmount, $order->actPrice, 2);
  156. $custom->debtAmount = $remain;
  157. $custom->debtNum -= count($arr);
  158. $custom->isDebt = $remain > 0 ? 1 : 0;
  159. $custom->save();
  160. if ($custom->debtNum == 0) {
  161. $main->mayGatheringNum -= 1;
  162. $main->save();
  163. }
  164. //应付供货商款减少
  165. $customShopId = $custom->shopId;
  166. $customShop = ShopClass::getLockById($customShopId);
  167. if (empty($customShop)) {
  168. util::fail('没有找到客户门店');
  169. }
  170. $customMainId = $customShop->mainId ?? 0;
  171. $customMain = MainClass::getLockById($customMainId);
  172. if (empty($customMain)) {
  173. util::fail('没有main信息');
  174. }
  175. $currentMayPay = bcsub($customMain->mayPay, $order->actPrice, 2);
  176. $customMain->mayPay = $currentMayPay;
  177. if ($ghs->debtNum == 0) {
  178. $customMain->mayPayNum -= 1;
  179. }
  180. $customMain->save();
  181. }
  182. //供货商的采购信息 ssh 20210625
  183. public static function getGhsCgInfo($info)
  184. {
  185. $purchaseIds = $info['purchaseIds'] ?? [];
  186. $cgArr = explode(',', $purchaseIds);
  187. $cgList = CgClass::getAllByCondition(['id' => ['in', $cgArr]], null, 'id,orderSn,bigNum,smallNum,realPrice,addTime', null);
  188. $info['orderList'] = $cgList;
  189. $info['orderNum'] = count($cgArr);
  190. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  191. $operator = $info['customShopAdminName'] ?? '';
  192. if ($clearStyle == dict::getDict('clearStyle', 'kmGys2gys')) {
  193. $operator = '商家';
  194. }
  195. $info['operator'] = $operator;
  196. return $info;
  197. }
  198. //零售的采购信息 ssh 20210625
  199. public static function getCgInfo($info)
  200. {
  201. $id = $info['id'] ?? 0;
  202. $deadline = $info['deadline'] ?? '';
  203. $deadTime = strtotime($deadline);
  204. $current = time();
  205. if ($current >= $deadTime) {
  206. $info['status'] = PurchaseClearClass::STATUS_EXPIRE;
  207. self::updateById($id, ['status' => PurchaseClearClass::STATUS_EXPIRE]);
  208. }
  209. $purchaseIds = $info['purchaseIds'] ?? [];
  210. $cgArr = explode(',', $purchaseIds);
  211. $cgList = PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null);
  212. $info['orderList'] = $cgList;
  213. $info['orderNum'] = count($cgArr);
  214. $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'hd2Gys');
  215. $operator = $info['customShopAdminName'] ?? '';
  216. if ($clearStyle == dict::getDict('clearStyle', 'gys2Hd')) {
  217. $operator = '商家';
  218. }
  219. $info['operator'] = $operator;
  220. return $info;
  221. }
  222. //完整信息 ssh 20210625
  223. public static function getSaleInfo($info)
  224. {
  225. $saleIds = $info['saleIds'] ?? [];
  226. $saleArr = explode(',', $saleIds);
  227. $saleList = OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null);
  228. $info['orderList'] = $saleList;
  229. $info['orderNum'] = count($saleArr);
  230. $type = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
  231. $operator = $info['ghsShopAdminName'] ?? '';
  232. if ($type == dict::getDict('clearStyle', 'hd2Gys')) {
  233. $operator = '客户';
  234. }
  235. $info['operator'] = $operator;
  236. return $info;
  237. }
  238. }