PurchaseClass.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. <?php
  2. namespace bizHd\purchase\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\product\classes\XjClass;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopCouponClass;
  8. use biz\sj\classes\SjClass;
  9. use biz\stat\classes\StatCgClass;
  10. use biz\stat\classes\StatCgGhsClass;
  11. use biz\stat\classes\StatOutClass;
  12. use biz\stock\classes\StockClass;
  13. use biz\wx\classes\WxMessageClass;
  14. use bizGhs\admin\classes\AdminClass;
  15. use bizGhs\custom\classes\CustomClass;
  16. use bizGhs\item\classes\CostChangeClass;
  17. use bizGhs\order\classes\OrderClass;
  18. use bizGhs\order\traits\OrderTrait;
  19. use bizGhs\product\classes\ProductClass;
  20. use bizGhs\stock\classes\OnStockRecordClass;
  21. use bizGhs\stock\classes\StockRecordClass;
  22. use bizGhs\stock\services\StockRecordService;
  23. use bizHd\cg\classes\CgRefundClass;
  24. use bizHd\cg\classes\CgXjClass;
  25. use bizHd\shop\classes\MainClass;
  26. use bizHd\stat\classes\StatIncomeClass;
  27. use bizHd\wx\classes\WxOpenClass;
  28. use common\components\dict;
  29. use common\components\imgUtil;
  30. use common\components\mapUtil;
  31. use common\components\noticeUtil;
  32. use common\components\orderSn;
  33. use common\components\util;
  34. use bizHd\base\classes\BaseClass;
  35. use common\components\lakala\Lakala;
  36. use Yii;
  37. use yii\helpers\ArrayHelper;
  38. class PurchaseClass extends BaseClass
  39. {
  40. use OrderTrait;
  41. public static $baseFile = '\bizHd\purchase\models\Purchase';
  42. //订单状态 1待付款,待确认,2待配送,3配送中,4已完成,5取消,失败
  43. const STATUS_UN_PAY = 1;
  44. const STATUS_UN_SEND = 2;
  45. const STATUS_SENDING = 3;
  46. const STATUS_COMPLETE = 4;
  47. const STATUS_CANCEL = 5;
  48. //未知
  49. const DEBT_UNKNOWN = 0;
  50. //欠款
  51. const DEBT_YES = 1;
  52. //没有欠款
  53. const DEBT_NO = 2;
  54. //配送,这里的数值是错的,请参考dict.php的数值
  55. const GET_TYPE_SEND = 1;
  56. //自取,这里的数值是错的,请参考dict.php的数值
  57. const GET_TYPE_SELF_GET = 2;
  58. const REFUND_NO = 1;
  59. const REFUND_YES = 2;
  60. //获取打包费
  61. public static function getKmPackCost($mainId)
  62. {
  63. $cost = dict::getDict('kmPackCost');
  64. if (getenv('YII_ENV') == 'production') {
  65. //云漫梦金鹏 徐记花卉 康多鲜 旭海 打包费0元
  66. if (in_array($mainId, [10728, 20528, 10866, 38657, 41121])) {
  67. $cost = dict::getDict('kmPackCost2');
  68. }
  69. //花瀚鲜花供应链
  70. if (in_array($mainId, [50, 14499])) {
  71. $cost = dict::getDict('kmPackCost3');
  72. }
  73. } else {
  74. }
  75. return $cost;
  76. }
  77. //订单发货主方法 ssh 20231119
  78. public static function orderSend($cg, $params, $allowRepeat)
  79. {
  80. $respond = self::orderFh($cg, $params, $allowRepeat);
  81. $orderId = $cg->saleId ?? 0;
  82. $orderInfo = OrderClass::getById($orderId, true);
  83. if (empty($orderInfo)) {
  84. util::fail('没有找到销售单信息');
  85. }
  86. OrderClass::orderFh($orderInfo, $params, $allowRepeat);
  87. return $respond;
  88. }
  89. //订单发货子方法 ssh 20231119
  90. public static function orderFh($cg, $params, $allowRepeat = false, $notice = false)
  91. {
  92. if ($notice == true) {
  93. $noticeText = json_encode(['id' => $cg->id]);
  94. $noticeKey = "ghsKdNoticeHd";
  95. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  96. }
  97. if ($cg->status == 3) {
  98. if ($allowRepeat == false) {
  99. util::fail('订单已发货');
  100. } else {
  101. return true;
  102. }
  103. }
  104. $fhType = $params['fhType'] ?? 0;
  105. if (is_numeric($fhType) == false) {
  106. util::fail('请选择发货类型');
  107. }
  108. if ($fhType == 1) {
  109. $fhWl = $params['fhWl'] ?? '';
  110. $fhWlNo = $params['fhWlNo'] ?? '';
  111. if (empty($fhWl)) {
  112. util::fail('请填写物流名称');
  113. }
  114. if (empty($fhWlNo)) {
  115. util::fail('请填写物流编号');
  116. }
  117. $cg->fhWl = $fhWl;
  118. $cg->fhWlNo = $fhWlNo;
  119. }
  120. $cg->fhType = $fhType;
  121. $cg->status = 3;
  122. $cg->fhTime = date("Y-m-d H:i:s");
  123. $cg->save();
  124. $orderSn = $cg->orderSn ?? '';
  125. $itemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  126. if (!empty($itemList)) {
  127. $sjId = $cg->sjId ?? 0;
  128. $shopId = $cg->shopId ?? 0;
  129. $mainId = $cg->mainId ?? 0;
  130. $ghsName = $cg->ghsName ?? '';
  131. foreach ($itemList as $key => $val) {
  132. $productId = $val->productId ?? 0;
  133. $itemId = $val->itemId ?? 0;
  134. $num = $val->xhNum ?? 0;
  135. $product = ProductClass::getLockById($productId);
  136. if (empty($product)) {
  137. util::fail('没有找到花材呢');
  138. }
  139. //增加路上库存
  140. $onRecord = [];
  141. $onRecord['sjId'] = $sjId;
  142. $onRecord['shopId'] = $shopId;
  143. $onRecord['mainId'] = $mainId;
  144. $onRecord['orderSn'] = $orderSn;
  145. $onRecord['productId'] = $productId;
  146. $onRecord['itemId'] = $itemId;
  147. $onRecord['relateName'] = $ghsName;
  148. $onRecord['itemNum'] = $num;
  149. $oldOnStock = $product->onStock ?? 0;
  150. $onRecord['oldStock'] = $oldOnStock;
  151. $newOnStock = bcadd($oldOnStock, $num);
  152. $onRecord['newStock'] = $newOnStock;
  153. $onRecord['ptStyle'] = 1;
  154. OnStockRecordClass::addPurchaseOrderRecord($onRecord);
  155. ProductClass::addOnStockByItemNum($productId, $num);
  156. }
  157. }
  158. return $cg;
  159. }
  160. //确认收货 ssh 20231119
  161. public static function confirmTake($cg)
  162. {
  163. //订单如果没有发货,则先发货
  164. if ($cg->status == self::STATUS_UN_SEND) {
  165. $cg = self::orderSend($cg, [], true);
  166. }
  167. // 构建“冲销”函数的参数 -- 变动前的花材存库数据
  168. $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
  169. $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
  170. $oldStocks = [];
  171. foreach ($productIds as $productId) {
  172. $product = ProductClass::getById($productId, true);
  173. $oldStocks[$productId] = $product;
  174. }
  175. self::takeToPutIn($cg);
  176. $shop = ShopClass::getLockById($cg->shopId);
  177. if (empty($shop)) {
  178. util::fail('没有找到门店14');
  179. }
  180. $adminId = $shop->adminId;
  181. $mainId = $shop->mainId;
  182. PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $mainId, '系统');
  183. }
  184. //确认收货并入库 ssh 20231119
  185. public static function takeToPutIn($cg)
  186. {
  187. if ($cg->status != 3) {
  188. util::fail('订单不是待收货状态哦');
  189. }
  190. $saleId = $cg->saleId ?? 0;
  191. $order = OrderClass::getById($saleId, true);
  192. if (empty($order)) {
  193. util::fail('没有找到订单');
  194. }
  195. if ($order->status != 3) {
  196. util::fail('订单不是待收货状态,订单状态:' . $order->status . ',订单ID:' . $saleId);
  197. }
  198. $order->status = 4;
  199. $order->save();
  200. //加库存 lqh 2021-05-10
  201. $orderSn = $cg->orderSn;
  202. $sjId = $cg->sjId ?? 0;
  203. $shopId = $cg->shopId ?? 0;
  204. $mainId = $cg->mainId ?? 0;
  205. $ghsName = $cg->ghsName ?? '';
  206. $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  207. foreach ($purchaseItem as $v) {
  208. //小单位库存不考虑入库 ssh 202310
  209. $itemNum = $v['itemNum'];
  210. if ($itemNum > 0) {
  211. $productId = $v['productId'];
  212. $stockInfo = ProductClass::addStockByItemNum($v['productId'], $itemNum);
  213. //记录库存流水
  214. $recordData = [];
  215. $recordData['sjId'] = $cg->sjId;
  216. $recordData['shopId'] = $cg->shopId;
  217. $recordData['mainId'] = $cg->mainId;
  218. $recordData['itemId'] = $v['itemId'] ?? 0;
  219. $recordData['itemNum'] = $itemNum;
  220. $recordData['oldStock'] = $stockInfo['oldStock'];
  221. $recordData['newStock'] = $stockInfo['newStock'];
  222. $recordData['productId'] = $v['productId'];
  223. $recordData['orderSn'] = $orderSn;
  224. $recordData['relateName'] = $cg->ghsName ?? '';
  225. StockRecordClass::addPurchaseOrderRecord($recordData);
  226. $product = ProductClass::getLockById($productId);
  227. if (empty($product)) {
  228. util::fail('没有找到花材哈');
  229. }
  230. //减少路上库存
  231. $onRecord = [];
  232. $onRecord['sjId'] = $sjId;
  233. $onRecord['shopId'] = $shopId;
  234. $onRecord['mainId'] = $mainId;
  235. $onRecord['orderSn'] = $orderSn;
  236. $onRecord['productId'] = $productId;
  237. $onRecord['itemId'] = $v['itemId'] ?? 0;
  238. $onRecord['relateName'] = $ghsName;
  239. $onRecord['itemNum'] = $itemNum;
  240. $oldOnStock = $product->onStock ?? 0;
  241. $onRecord['oldStock'] = $oldOnStock;
  242. $onRecord['newStock'] = bcsub($oldOnStock, $itemNum);
  243. $onRecord['ptStyle'] = 1;
  244. OnStockRecordClass::addInStockOrderRecord($onRecord);
  245. ProductClass::decreaseOnStockByItemNum($productId, $itemNum);
  246. $unitPrice = $v['xhUnitPrice'] ?? 0;
  247. $product->cost = $unitPrice;
  248. $product->save();
  249. $ptItemId = $product->itemId ?? 0;
  250. $mainId = $product->mainId ?? 0;
  251. $staffId = $cg->shopAdminId ?? 0;
  252. $staffName = $cg->shopAdminName ?? '';
  253. //成本变动记录
  254. $changeData = [
  255. 'ptStyle' => 2,
  256. 'sjId' => 0,
  257. 'mainId' => $mainId,
  258. 'itemId' => $productId,
  259. 'ptItemId' => $ptItemId,
  260. 'cost' => $unitPrice,
  261. 'staffId' => $staffId,
  262. 'staffName' => $staffName,
  263. 'name' => $product->name ?? '',
  264. 'cover' => $product->cover ?? '',
  265. 'targetId' => $cg->id ?? 0,
  266. 'event' => '采购',
  267. ];
  268. CostChangeClass::addData($changeData);
  269. }
  270. }
  271. $cg->status = 4;
  272. $cg->stockChange = 1;
  273. $cg->save();
  274. }
  275. // 库存回滚为入库前的状态
  276. public static function rollbackProductStock($oldStocks, $shop, $adminId, $mainId, $staffName)
  277. {
  278. $itemInfo = [];
  279. foreach ($oldStocks as $productId => $product) {
  280. $ratio = $product->ratio;
  281. $formNum = ProductClass::formatStock($product->stock, $ratio); //花材的库存转换
  282. $itemInfo[] = [
  283. 'productId' => $productId,
  284. 'itemNum' => $product->stock, // 回滚为入库前的库存
  285. 'itemId' => $product->itemId,
  286. 'bigNum' => $formNum['bigNum'],
  287. 'smallNum' => $formNum['smallNum'],
  288. 'price' => $product->price,
  289. ];
  290. }
  291. $data = [
  292. 'sjId' => $shop->sjId,
  293. 'shopId' => $shop->id,
  294. 'adminId' => $adminId,
  295. 'mainId' => $mainId,
  296. 'staffName' => $staffName,
  297. 'itemInfo' => $itemInfo,
  298. 'pdType' => 'rollbackStock',
  299. 'remark' => '',
  300. ];
  301. \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
  302. }
  303. // 指定花材新增库存
  304. public static function increaseProductStock($itemDatas, $shop, $adminId, $mainId, $staffName)
  305. {
  306. $itemInfo = [];
  307. foreach ($itemDatas as $productId => $arr) {
  308. $addNum = $arr['addNum']; //新增的数值
  309. $product = $arr['product'];
  310. $ratio = $product->ratio;
  311. $newStock = bcadd($product->stock, $addNum, 2);
  312. $formNum = ProductClass::formatStock($newStock, $ratio); //花材的库存转换
  313. $itemInfo[] = [
  314. 'productId' => $productId,
  315. 'itemNum' => $newStock, // 新库存数
  316. 'itemId' => $product->itemId,
  317. 'bigNum' => $formNum['bigNum'],
  318. 'smallNum' => $formNum['smallNum'],
  319. 'price' => $product->price,
  320. ];
  321. }
  322. $data = [
  323. 'sjId' => $shop->sjId,
  324. 'shopId' => $shop->id,
  325. 'adminId' => $adminId,
  326. 'mainId' => $mainId,
  327. 'staffName' => $staffName,
  328. 'itemInfo' => $itemInfo,
  329. 'pdType' => 'rollbackStock',
  330. 'remark' => '',
  331. ];
  332. \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
  333. }
  334. public static function notSameCity($shop, $ghsShop)
  335. {
  336. return true;
  337. $shopLat = $shop->lat ?? '';
  338. $shopLong = $shop->long ?? '';
  339. if (empty($shopLat) || empty($shopLong)) {
  340. util::fail('请完善门店地址');
  341. }
  342. $ghsShopLat = $ghsShop->lat ?? '';
  343. $ghsShopLong = $ghsShop->long ?? '';
  344. $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
  345. $formatDistance = ceil($distance / 1000);
  346. if ($formatDistance > 150) {
  347. return false;
  348. }
  349. return true;
  350. }
  351. //计算距离和运费【另外还有个地方要同步修改,关键词calc_freight_distance】 ssh 20221003
  352. public static function getDistanceFee($shop, $ghsShop, $weight)
  353. {
  354. return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
  355. $shopLat = isset($shop->lat) ? $shop->lat : '';
  356. $shopLong = isset($shop->long) ? $shop->long : '';
  357. if (empty($shopLat) || empty($shopLong)) {
  358. util::fail('请完善门店地址哦');
  359. }
  360. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  361. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  362. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  363. util::fail('商家地址有问题');
  364. }
  365. //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  366. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  367. //00:00 - 06:00 +6元,22:00-24:00 +4元
  368. //恶劣天气,下单高峰期会临时调整价格
  369. //起步价
  370. $baseFee = 10;
  371. $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
  372. //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  373. $formatDistance = ceil($distance / 1000);
  374. $startKiloFee = 6;
  375. if ($formatDistance < 5) {
  376. $addDistanceFee = bcmul($formatDistance, 2);
  377. } elseif ($formatDistance == 5) {
  378. $addDistanceFee = $startKiloFee;
  379. } else {
  380. $overKilo = bcsub($formatDistance, 5);
  381. $overFee = bcmul($overKilo, 3);
  382. $addDistanceFee = bcadd($startKiloFee, $overFee);
  383. }
  384. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  385. $formatWeight = ceil($weight);
  386. if ($formatWeight > 15) {
  387. $addWeightFee = 20;
  388. } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
  389. $addWeightFee = 12;
  390. } elseif ($formatWeight >= 5 && $formatWeight < 10) {
  391. $overWeight = bcsub($formatWeight, 4);
  392. $addWeightFee = bcmul($overWeight, 2);
  393. } else {
  394. $addWeightFee = 0;
  395. }
  396. $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
  397. $totalFee = bcadd($baseFee, $addFee, 2);
  398. //特殊时段费
  399. $timeFee = 0;
  400. $hour = date("G");
  401. //22:00 - 24:00 +4元
  402. if (in_array($hour, [22, 23])) {
  403. $timeFee = 4;
  404. }
  405. //00:00 - 06:00 +6元
  406. if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
  407. $timeFee = 6;
  408. }
  409. $totalFee = bcadd($totalFee, $timeFee, 2);
  410. //恶劣天气和下单高峰费用
  411. $weatherFee = 0;
  412. $totalFee = bcadd($totalFee, $weatherFee, 2);
  413. $showDistance = sprintf("%.1f", ($distance / 1000));
  414. //noticeUtil::push("距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
  415. //万丽花行 跑腿不算运费
  416. if ($ghsShop->id == 12003) {
  417. return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
  418. }
  419. return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
  420. }
  421. //到货之后 ssh 20220321
  422. public static function arrival($cg, $data)
  423. {
  424. if ($cg->stockChange == 1) {
  425. util::fail('您已操作过了');
  426. }
  427. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  428. $packCost = isset($data['packCost']) && is_numeric($data['packCost']) ? $data['packCost'] : 0;
  429. $cg->sendCost = $sendCost;
  430. $cg->packCost = $packCost;
  431. $cg->save();
  432. $sjId = $data['sjId'] ?? 0;
  433. $shopId = $data['shopId'] ?? 0;
  434. //product [{"productId":"3674","num":0,"price":1}]
  435. $product = $data['product'] ?? '';
  436. if (empty($product)) {
  437. util::fail('请填写成本价和到货数量');
  438. }
  439. $ghsName = $cg->ghsName ?? '';
  440. $orderSn = $cg->orderSn ?? '';
  441. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  442. if (empty($list)) {
  443. util::fail('没有找到花材哦');
  444. }
  445. $weight = 0;
  446. $itemPrice = 0;
  447. foreach ($list as $key => $val) {
  448. $productId = $val->productId ?? 0;
  449. if (isset($product[$productId]) == false) {
  450. util::fail('没有找到对应采购花材');
  451. }
  452. $num = $product[$productId]['num'] ?? 0;
  453. $price = $product[$productId]['price'] ?? 0;
  454. if ($price <= 0) {
  455. util::fail('花材金额需要大于0');
  456. }
  457. $totalPrice = bcmul($num, $price, 2);
  458. $itemPrice = bcadd($itemPrice, $totalPrice, 2);
  459. $val->price = $price;
  460. $val->itemNum = $num;
  461. $val->preTotalPrice = $totalPrice;
  462. $val->totalPrice = $totalPrice;
  463. $val->xhNum = $num;
  464. $val->xhUnitPrice = $price;
  465. $val->xhPrice = $totalPrice;
  466. $val->save();
  467. $unitWeight = $val->unitWeight ?? 0;
  468. $currentWeight = bcmul($num, $unitWeight, 2);
  469. $weight = bcadd($weight, $currentWeight, 2);
  470. $unitType = $val->xhUnitType ?? 0;
  471. $bigNum = 0;
  472. $smallNum = 0;
  473. if ($unitType == dict::getDict('unitType', 'big')) {
  474. $bigNum = $num;
  475. } else {
  476. $smallNum = $num;
  477. }
  478. //增加库存
  479. $option = ['relateName' => $ghsName];
  480. StockClass::purchaseItem($orderSn, $sjId, $shopId, $productId, $bigNum, $smallNum, $option);
  481. }
  482. $cg->itemPrice = $itemPrice;
  483. $sendCost = $cg->sendCost ?? 0;
  484. $currentPrice = bcadd($itemPrice, $sendCost, 2);
  485. $packCost = $cg->packCost ?? 0;
  486. $currentPrice = bcadd($currentPrice, $packCost, 2);
  487. $kiloFee = $data['kiloFee'] ?? 0;
  488. $miniKilo = $data['miniKilo'] ?? 0;
  489. $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
  490. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  491. $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
  492. $cg->prePrice = $currentPrice;
  493. $cg->orderPrice = $currentPrice;
  494. $cg->actPrice = $currentPrice;
  495. $cg->realPrice = $currentPrice;
  496. $cg->stockChange = 1;
  497. $cg->save();
  498. $mainId = $cg->mainId ?? 0;
  499. $sjId = $cg->sjId ?? 0;
  500. $shopId = $cg->shopId ?? 0;
  501. $cgId = $cg->id ?? 0;
  502. $saleRefundId = $data['saleRefundId'] ?? 0;
  503. $shop = ShopClass::getLockById($shopId);
  504. if (empty($shop)) {
  505. util::fail('没有找到零售店的门店');
  506. }
  507. $main = MainClass::getLockById($mainId);
  508. if (empty($main)) {
  509. util::fail('没有main信息12');
  510. }
  511. $main->cgUnSend -= 1;
  512. $main->cgFinish += 1;
  513. $main->save();
  514. $cgRefund = null;
  515. $bookPrice = $cg->bookPrice ?? 0;
  516. if ($bookPrice == $currentPrice) {
  517. return ['refund' => $cgRefund];
  518. } elseif ($bookPrice > $currentPrice) {
  519. //退款
  520. $refundPrice = bcsub($bookPrice, $currentPrice, 2);
  521. $book = 1;
  522. $refundOrderSn = orderSn::getCgRefundSn();
  523. $refundType = CgRefundClass::REFUND_TYPE_MONEY;
  524. $refundData = [
  525. 'book' => $book,
  526. 'mainId' => $mainId,
  527. 'orderSn' => $refundOrderSn,
  528. 'cgId' => $cgId,
  529. 'status' => CgRefundClass::STATUS_COMPLETE,
  530. 'shopAdminId' => 0,
  531. 'shopAdminName' => '',
  532. 'refundType' => $refundType,
  533. 'refundPrice' => $refundPrice,
  534. 'saleRefundId' => $saleRefundId,
  535. 'sjId' => $sjId,
  536. 'shopId' => $shopId,
  537. ];
  538. $cgRefund = CgRefundClass::add($refundData, true);
  539. //门店收入增加
  540. $currentTotalIncome = bcadd($main->totalIncome, $refundPrice, 2);
  541. $main->totalIncome = $currentTotalIncome;
  542. $main->save();
  543. //供货商退款,向供货商采购总金额减少
  544. StatCgClass::replace($main, $shop, $refundPrice, 0, false);
  545. //供货商退款,按供货商分别采购的金额减少
  546. StatCgGhsClass::hdCgGhsReplace(false, $cg, $refundPrice, 0);
  547. $currentType = dict::getDict('capitalType', 'hdBookRefund', 'id');
  548. $sjId = $cg->sjId ?? 0;
  549. $shopId = $cg->shopId ?? 0;
  550. $event = "预订单{$orderSn}多退少补({$ghsName})";
  551. $capitalData = [
  552. 'capitalType' => $currentType,
  553. 'io' => 1,
  554. 'totalIncome' => $currentTotalIncome,
  555. 'payWay' => 0,
  556. 'amount' => $refundPrice,
  557. 'sjId' => $sjId,
  558. 'shopId' => $shopId,
  559. 'event' => $event,
  560. 'mainId' => $mainId,
  561. ];
  562. ShopCapitalClass::addCapital($capitalData);
  563. //收入统计
  564. StatIncomeClass::updateOrInsert($main, $shop, $refundPrice);
  565. $ghsId = $cg->ghsId ?? 0;
  566. $ghs = GhsClass::getLockById($ghsId);
  567. if (empty($ghs)) {
  568. util::fail('没有找到供货商');
  569. }
  570. $ghsShopId = $ghs->shopId ?? 0;
  571. $ghsShop = ShopClass::getById($ghsShopId, true);
  572. if (empty($ghsShop)) {
  573. util::fail('没有找到供货商的门店');
  574. }
  575. $refundFee = bcmul($refundPrice, 100);
  576. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  577. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  578. $params = [
  579. 'appid' => 'OP00002119',
  580. 'serial_no' => '018b08cfddbd',
  581. 'merchant_no' => $ghsShop->lklSjNo,
  582. 'term_no' => $ghsShop->lklScanTermNo,
  583. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  584. 'lklCertificatePath' => $lklCertificatePath,
  585. ];
  586. $laResource = new Lakala($params);
  587. $refundReason = '';
  588. $aliParams = [
  589. 'refundSn' => $refundOrderSn,
  590. 'orderSn' => $orderSn,
  591. 'refundAmount' => $refundFee,
  592. 'refundReason' => $refundReason,
  593. ];
  594. $response = $laResource->refund($aliParams);
  595. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  596. $errMsg = $response['msg'] ?? '';
  597. noticeUtil::push('退款失败了哦!请注意:' . $errMsg . ' 金额:' . $refundFee, '15280215347');
  598. util::fail('退款失败');
  599. }
  600. $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
  601. $cgRefund->thirdRefundNo = $refundTradeNo;
  602. $cgRefund->save();
  603. $cg->refundPrice = $refundPrice;
  604. $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
  605. $cg->refund = self::REFUND_YES;
  606. $cg->refundLog = 1;
  607. $cg->save();
  608. } else {
  609. //欠款
  610. $currentDebt = bcsub($currentPrice, $bookPrice, 2);
  611. $cg->debtPrice = $currentDebt;
  612. $cg->remainDebtPrice = $currentDebt;
  613. $cg->debt = PurchaseClass::DEBT_YES;
  614. $cg->save();
  615. $payWay = dict::getDict('payWay', 'debtPay');
  616. $ghsId = $cg->ghsId ?? 0;
  617. $ghs = GhsClass::getLockById($ghsId);
  618. if (empty($ghs)) {
  619. util::fail('没有找到供货商');
  620. }
  621. \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $cg, true);
  622. //支出统计
  623. StatOutClass::updateOrInsert($main, $shop, $currentDebt);
  624. //支出流水
  625. $capitalType = dict::getDict('capitalType', 'hdBookFinalPay', 'id');
  626. $sjId = $cg->sjId ?? 0;
  627. $shopId = $cg->shopId ?? 0;
  628. $currentExpend = bcadd($main->totalExpend, $currentDebt, 2);
  629. $main->totalExpend = $currentExpend;
  630. $main->save();
  631. $event = "预订单{$orderSn}补尾款欠款";
  632. $capitalData = [
  633. 'capitalType' => $capitalType,
  634. 'io' => 0,
  635. 'totalExpend' => $currentExpend,
  636. 'payWay' => $payWay,
  637. 'amount' => $currentDebt,
  638. 'sjId' => $sjId,
  639. 'shopId' => $shopId,
  640. 'event' => $event,
  641. 'mainId' => $mainId,
  642. ];
  643. ShopCapitalClass::addCapital($capitalData);
  644. //补款给供货商,采购总金额增加
  645. StatCgClass::replace($main, $shop, $currentDebt, 0, true);
  646. //补款给供货商,按供货商分别统计采购量增加
  647. StatCgGhsClass::hdCgGhsReplace(true, $cg, $currentDebt, 0);
  648. }
  649. return ['refund' => $cgRefund];
  650. }
  651. //添加采购 ssh 2021.1.18
  652. public static function addPurchase($data, $ghs)
  653. {
  654. $shopId = $data['shopId'] ?? 0;
  655. $sjId = $data['sjId'] ?? 0;
  656. $mainId = $data['mainId'] ?? 0;
  657. $ghsId = $data['ghsId'] ?? 0;
  658. $book = $data['book'] ?? 0;
  659. $shopAdminId = $data['shopAdminId'] ?? 0;
  660. $customId = $data['customId'] ?? 0;
  661. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'ghsId' => $ghsId, 'customId' => $customId];
  662. $orderSn = orderSn::getPurchaseSn($snData);
  663. $data['orderSn'] = $orderSn;
  664. $prePrice = 0;
  665. $live = $ghs['live'] ?? 1;
  666. $sendTimeWant = !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  667. if ($sendTimeWant < date("Y-m-d")) {
  668. util::fail('您选了过去时间');
  669. }
  670. $data['sendTimeWant'] = $sendTimeWant;
  671. if (empty($data['product'])) {
  672. util::fail('请选择花材17');
  673. }
  674. $productList = $data['product'];
  675. $productList = self::mergeItemInfo($productList);
  676. $data['kindNum'] = count($productList); //多少种花材
  677. //传过来的是供货商的productId,需要转换为零售端的productId
  678. //数据格式 [{"productId":"113","bigNum":1,"smallNum":0,"classId":"-2","hdProductId":1}]
  679. if ($live == 1) {
  680. //花店真实向批发店采购花材,因为拿的供货商的ID,所以需要转化花店的id
  681. $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId, $ghs);
  682. $ghsProductData = self::getProductMapData($productList, "ghsProductId");
  683. } else {
  684. //虚拟采购用的自己的ID所以不需要转化
  685. $ghsProductData = [];
  686. }
  687. $hdProductData = self::getProductMapData($productList, "productId");
  688. $giveLevel = $ghs['giveLevel'] ?? 0;
  689. $priceMap = CustomClass::$levelPriceKeyMap;
  690. $addPriceMap = CustomClass::$levelAddPriceKeyMap;
  691. //累计满减
  692. $totalReachDiscount = 0;
  693. //计算价格= 各个productId的price*num(bigNum,smallNum) + sendCost(配送费)
  694. $bigNum = 0; //总共多少扎
  695. $smallNum = 0; //总共多少支
  696. $totalWeight = 0;
  697. foreach ($productList as $key => $product) {
  698. $currentBigNum = $product['bigNum'] ?? 0;
  699. $currentSmallNum = $product['smallNum'] ?? 0;
  700. $bigNum += $product['bigNum'] ?? 0;
  701. $smallNum += $product['smallNum'] ?? 0;
  702. $productList[$key]['orderSn'] = $orderSn;
  703. if ($live == 1) {
  704. $ghsProductId = $product['ghsProductId'];
  705. $hdProductId = $product['productId'];
  706. } else {
  707. $hdProductId = $product['productId'] ?? 0;
  708. $ghsProductId = 0;
  709. }
  710. $unitWeight = $hdProductData[$hdProductId]['weight'] ?? 0;
  711. $variety = $hdProductData[$hdProductId]['variety'] ?? 0;
  712. $ratio = $hdProductData[$hdProductId]['ratio'] ?? 0;
  713. $itemId = $hdProductData[$hdProductId]['itemId'] ?? 0;
  714. $rank = $hdProductData[$hdProductId]['rank'] ?? 'B';
  715. $stock = $hdProductData[$hdProductId]['stock'] ?? 0;
  716. $stockFormat = ProductClass::formatStock($stock, $ratio);
  717. $itemNum = ProductClass::mergeItemNum($product['bigNum'], $product['smallNum'], $ratio);
  718. $weight = $hdProductData[$hdProductId]['weight'] ?? 0;
  719. $currentWeight = bcmul($weight, $itemNum, 2);
  720. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  721. //1、供货商开单改价 2、花店向虚拟供货商采购录入价格
  722. $userPrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : 0;
  723. $currentGhsProduct = $ghsProductData[$ghsProductId] ?? [];
  724. if (!empty($currentGhsProduct)) {
  725. //用批发店的花材名称
  726. $itemName = $currentGhsProduct['name'] ?? '';
  727. $cover = $currentGhsProduct['cover'] ?? '';
  728. //如果零售店自己下预订单,预付0.1元,有二个地方要同步修改,搜索关键词 pre0.1
  729. if ($book == 1 && $shopAdminId > 0) {
  730. $price = 0.1;
  731. } else {
  732. //用批发店花材的价格
  733. $price = ProductClass::getFinalPrice($currentGhsProduct, $giveLevel, $priceMap, $addPriceMap, $userPrice, $itemNum);
  734. }
  735. $thisPrice = $price;
  736. $reachNum = $currentGhsProduct['reachNum'] ?? 0;
  737. if ($reachNum > 0) {
  738. if ($itemNum >= $reachNum) {
  739. $reachNumDiscount = $currentGhsProduct['reachNumDiscount'] ?? 0;
  740. $currentReachDiscount = bcmul($reachNumDiscount, $itemNum, 2);
  741. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  742. }
  743. }
  744. } else {
  745. $currentHdProduct = $hdProductData[$hdProductId] ?? [];
  746. if (empty($currentHdProduct)) {
  747. util::fail('花材有问题哦');
  748. }
  749. $itemName = $currentHdProduct['name'] ?? '';
  750. $cover = $currentHdProduct['cover'] ?? '';
  751. $price = $userPrice;
  752. $thisPrice = $userPrice;
  753. }
  754. if ($currentSmallNum > 0) {
  755. $productTotalPrice = bcmul($currentSmallNum, $price, 2);
  756. } else {
  757. $productTotalPrice = bcmul($currentBigNum, $price, 2);
  758. }
  759. $itemInfo = [
  760. 'itemName' => $itemName,
  761. 'itemCover' => $cover,
  762. 'bigNum' => $product['bigNum'] ?? 0,
  763. 'smallNum' => $product['smallNum'] ?? 0,
  764. 'bigNumStock' => $stockFormat['bigNum'],
  765. 'smallNumStock' => $stockFormat['smallNum'],
  766. 'itemUnit' => $ratio ? 2 : 1,
  767. 'ratio' => $ratio,
  768. 'rank' => $rank,
  769. 'bigUnit' => $hdProductData[$hdProductId]['bigUnit'],
  770. 'smallUnit' => $hdProductData[$hdProductId]['smallUnit'],
  771. ];
  772. if ($itemNum <= 0) {
  773. util::fail($itemName . ' 数量要大于零');
  774. }
  775. $productList[$key]['itemInfo'] = json_encode($itemInfo);
  776. $productList[$key]['variety'] = $variety;
  777. $productList[$key]['itemId'] = $itemId;
  778. $productList[$key]['preItemNum'] = $itemNum;
  779. $productList[$key]['itemNum'] = $itemNum;
  780. $productList[$key]['bigNum'] = $product['bigNum'] ?? 0;
  781. $productList[$key]['smallNum'] = $product['smallNum'] ?? 0;
  782. $productList[$key]['price'] = $thisPrice;
  783. $productList[$key]['ratio'] = $ratio;
  784. $productList[$key]['unitWeight'] = $unitWeight;
  785. $productList[$key]['totalPrice'] = $productTotalPrice;
  786. $productList[$key]['preTotalPrice'] = $productTotalPrice;
  787. $productList[$key]['productId'] = $hdProductId;
  788. $productList[$key]['ghsProductId'] = $ghsProductId;
  789. $productList[$key]['smallPrice'] = $price;
  790. $productList[$key]['mainId'] = $mainId;
  791. $productList[$key]['ghsId'] = $ghsId;
  792. $productList[$key]['name'] = $itemName;
  793. $productList[$key]['cover'] = $cover;
  794. $currentBigUnit = $hdProductData[$hdProductId]['bigUnit'] ?? '';
  795. $currentSmallUnit = $hdProductData[$hdProductId]['smallUnit'] ?? '';
  796. if ($currentBigNum > 0) {
  797. $xhNum = $currentBigNum;
  798. $xhUnitName = $currentBigUnit;
  799. $xhUnitType = dict::getDict('unitType', 'big');
  800. } else {
  801. $xhNum = $currentSmallNum;
  802. $xhUnitName = $currentSmallUnit;
  803. $xhUnitType = dict::getDict('unitType', 'small');
  804. }
  805. $itemPrice = $price;
  806. $price = bcmul($itemPrice, $xhNum, 2);
  807. //客户下单时会出现同时选了扎和支,很奇怪,前端没有找到原因,后端限制
  808. if ($currentBigNum > 0 && $currentSmallNum > 0) {
  809. util::fail($itemName . " {$currentBigUnit}和{$currentSmallUnit}只能选一种");
  810. }
  811. $productList[$key]['xhNum'] = $xhNum;
  812. $productList[$key]['xhUnitName'] = $xhUnitName;
  813. $productList[$key]['xhUnitType'] = $xhUnitType;
  814. $productList[$key]['xhUnitPrice'] = $itemPrice;
  815. $productList[$key]['xhPrice'] = $price;
  816. $productList[$key]['xhPreNum'] = $xhNum;
  817. $productList[$key]['xhPreUnitName'] = $xhUnitName;
  818. $productList[$key]['xhPreUnitType'] = $xhUnitType;
  819. $productList[$key]['xhPreUnitPrice'] = $itemPrice;
  820. $productList[$key]['xhPrePrice'] = $price;
  821. $prePrice = bcadd($prePrice, $productTotalPrice, 2);
  822. }
  823. $data['itemPrice'] = $prePrice;
  824. $sendCost = $data['sendCost'] ?? 0.00;
  825. $prePrice = bcadd($prePrice, $sendCost, 2);
  826. $packCost = $data['packCost'] ?? 0;
  827. $prePrice = bcadd($prePrice, $packCost, 2);
  828. $data['weight'] = $totalWeight;
  829. //预订单需要增加服务费
  830. if ($book == 1) {
  831. $kiloFee = $ghs['kiloFee'] ?? 0;
  832. $miniKilo = $ghs['miniKilo'] ?? 0;
  833. $calcWeight = $totalWeight > $miniKilo ? $totalWeight : $miniKilo;
  834. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  835. $prePrice = bcadd($prePrice, $allKiloFee, 2);
  836. $data['allKiloFee'] = $allKiloFee;
  837. $data['kiloFee'] = $kiloFee;
  838. $data['miniKilo'] = $miniKilo;
  839. $data['stockChange'] = 0;
  840. }
  841. $actPrice = $prePrice;
  842. if (isset($data['modifyPrice'])) {
  843. $modifyPrice = $data['modifyPrice'] ?? 0;
  844. if ($modifyPrice < $prePrice) {
  845. //优惠金额
  846. $data['discountAmount'] = bcsub($prePrice, $modifyPrice, 2);
  847. $data['discountType'] = dict::getDict('discountType', 'discount');
  848. }
  849. if ($modifyPrice > $prePrice) {
  850. //人工和资材费用
  851. $data['labourCost'] = bcsub($modifyPrice, $prePrice, 2);
  852. }
  853. $actPrice = $modifyPrice;
  854. if ($book == 1) {
  855. $actPrice = 0;
  856. }
  857. } else {
  858. //整单满减
  859. if ($book == 0) {
  860. $ghsShopId = $ghs['shopId'] ?? 0;
  861. $ghsShop = ShopClass::getById($ghsShopId, true);
  862. if (!empty($ghsShop)) {
  863. $meetNum = $ghsShop->meetNum ?? 0;
  864. $meetAmount = $ghsShop->meetAmount ?? 0;
  865. $cutStyle = $ghsShop->cutStyle ?? 0;
  866. $cutOption = $ghsShop->cutAmount ?? 0;
  867. if ($cutStyle == 0) {
  868. $cutAmount = $cutOption;
  869. } else {
  870. $miniOption = bcsub(1, $cutOption, 2);
  871. $cutAmount = bcmul($actPrice, $miniOption, 2);
  872. }
  873. if ($cutOption > 0) {
  874. if ($meetNum > 0 && $bigNum >= $meetNum) {
  875. if ($cutAmount > $actPrice) {
  876. util::fail('整单满减为负');
  877. }
  878. $actPrice = bcsub($actPrice, $cutAmount, 2);
  879. $data['orderReachDiscountPrice'] = $cutAmount;
  880. } else {
  881. if ($meetAmount > 0 && isset($data['itemPrice']) && $data['itemPrice'] >= $meetAmount) {
  882. if ($cutAmount > $actPrice) {
  883. util::fail('整单满减为负');
  884. }
  885. $actPrice = bcsub($actPrice, $cutAmount, 2);
  886. $data['orderReachDiscountPrice'] = $cutAmount;
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. $data['reachDiscountPrice'] = $totalReachDiscount;
  894. $data['prePrice'] = $prePrice;
  895. $data['bookPrice'] = $actPrice;
  896. $data['orderPrice'] = $actPrice;
  897. $data['actPrice'] = $actPrice;
  898. $data['realPrice'] = $actPrice;
  899. $data['bigNum'] = $bigNum;
  900. $data['smallNum'] = $smallNum;
  901. $data['discount'] = 1;
  902. $data['status'] = self::STATUS_UN_PAY;
  903. //采购单有效期
  904. $currentTime = time();
  905. $validTime = dict::getDict('order_pay_has_time');
  906. $current = $currentTime + $validTime;
  907. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  908. if ($book == 1) {
  909. $fiveTime = bcmul(86400, 5);
  910. $bookDeadline = bcadd($currentTime, $fiveTime);
  911. $data['deadline'] = date("Y-m-d H:i:s", $bookDeadline);
  912. }
  913. //采购单自动记为欠款单时间
  914. $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
  915. $currentSet = $currentTime + $localConfirmTime;
  916. $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
  917. //保存订单
  918. $respond = self::add($data, true);
  919. foreach ($productList as $productKey => $productVal) {
  920. //增加采购单的花材
  921. $returnItem = PurchaseItemClass::add($productVal, true);
  922. $unitPrice = $returnItem->price ?? 0;
  923. $currentId = $returnItem->id ?? 0;
  924. $currentPtItemId = $returnItem->itemId ?? 0;
  925. if (isset($data['xj'][$currentPtItemId]) && !empty($data['xj'][$currentPtItemId])) {
  926. $xjArr = $data['xj'][$currentPtItemId];
  927. if (is_array($xjArr)) {
  928. $ids = array_column($xjArr, 'id');
  929. $sjInfo = XjClass::getByIds($ids, null, 'id');
  930. $xjData = [];
  931. foreach ($xjArr as $xjItem) {
  932. $id = $xjItem['id'] ?? 0;
  933. $num = $xjItem['num'] ?? 0;
  934. $name = $sjInfo[$id]['name'] ?? '';
  935. $cover = $sjInfo[$id]['cover'] ?? '';
  936. $xjData[] = ['xjId' => $id, 'name' => $name, 'ptItemId' => $currentPtItemId,
  937. 'num' => $num, 'price' => $unitPrice, 'itemId' => $currentId, 'cover' => $cover];
  938. }
  939. CgXjClass::batchAdd($xjData);
  940. $returnItem->variety = 1;
  941. $returnItem->save();
  942. }
  943. }
  944. }
  945. $shop = ShopClass::getLockById($shopId);
  946. if (empty($shop)) {
  947. util::fail('没有找到门店59 ' . $shopId);
  948. }
  949. $mainId = $shop->mainId ?? 0;
  950. $main = MainClass::getLockById($mainId);
  951. if (empty($main)) {
  952. util::fail('没有main信息13');
  953. }
  954. $main->totalPurchaseOrder += 1;
  955. $main->cgUnPay += 1;
  956. $main->save();
  957. return $respond;
  958. }
  959. //添加采购 ssh 2021.1.18
  960. public static function updatePurchase($purchase, $data)
  961. {
  962. util::fail('已删除');
  963. }
  964. //组合数据 ssh 2021.1.25
  965. public static function groupPurchase($list, $showButton = false, $showProduct = false)
  966. {
  967. $ghsIds = array_unique(array_filter(array_column($list, 'ghsId')));
  968. $ghsInfo = GhsClass::getGhsByIds($ghsIds);
  969. $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
  970. $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
  971. $time = time();
  972. foreach ($list as $key => $val) {
  973. //剩余支付时间
  974. $remainPayTime = 0;
  975. $deadline = $val['deadline'] ?? '';
  976. if (!empty($deadline)) {
  977. $deadTime = strtotime($deadline);
  978. if (($deadTime - $time - 30) > 0) {
  979. $remainPayTime = $deadTime - $time;
  980. $remainPayTime = $remainPayTime - 30;
  981. }
  982. }
  983. $list[$key]['remainPayTime'] = $remainPayTime;
  984. $adminId = $val['adminId'] ?? 0;
  985. $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
  986. $ghsId = $val['ghsId'] ?? 0;
  987. $list[$key]['supplierName'] = $ghsInfo[$ghsId]['name'] ?? '';
  988. $list[$key]['supplierAvatar'] = $ghsInfo[$ghsId]['smallLogoUrl'] ?? '';
  989. $list[$key]['supplierMobile'] = $ghsInfo[$ghsId]['mobile'] ?? '';
  990. $list[$key]['supplierAddress'] = $ghsInfo[$ghsId]['address'] ?? '';
  991. $list[$key]['debtAmount'] = $ghsInfo[$ghsId]['debtAmount'] ?? '';
  992. //剩余欠款
  993. $balance = $ghsInfo[$ghsId]['balance'] ?? 0;
  994. $debtAmount = $ghsInfo[$ghsId]['debtAmount'] ?? 0;
  995. $ghsSalt = $ghsInfo[$ghsId]['salt'] ?? '';
  996. $remainDebtAmount = bcsub($debtAmount, $balance, 2);
  997. $list[$key]['remainDebtAmount'] = floatval($remainDebtAmount);
  998. $list[$key]['inMyGhsBalance'] = floatval($balance);
  999. $list[$key]['ghsSalt'] = $ghsSalt;
  1000. $list[$key]['lat'] = $ghsInfo[$ghsId]['lat'] ?? '';
  1001. $list[$key]['long'] = $ghsInfo[$ghsId]['long'] ?? '';
  1002. $list[$key]['supplierId'] = $ghsInfo[$ghsId]['id'] ?? '';
  1003. if ($showButton) {
  1004. $list[$key]['buttonList'] = [
  1005. ['type' => 'pay', 'show' => 1, 'disable' => 1],
  1006. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  1007. ];
  1008. }
  1009. $list[$key]['sendProgress'] = [];
  1010. if ($showProduct) {
  1011. $orderSn = $val['orderSn'];
  1012. $orderItem = PurchaseItemClass::getOrderItemDetail($orderSn);
  1013. $itemData = [];
  1014. $xjData = [];
  1015. if ($orderItem) {
  1016. foreach ($orderItem as $v) {
  1017. $info = json_decode($v['itemInfo'], true);
  1018. $tmp = $info;
  1019. unset($v['itemInfo']);
  1020. if (!isset($tmp['rank'])) {
  1021. $tmp['rank'] = 'B';
  1022. }
  1023. $tmp['name'] = $tmp['itemName'] ?? '';
  1024. $tmp = array_merge($tmp, $v);
  1025. $shortCover = $info['itemCover'] ?? '';
  1026. $tmp['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1027. if (isset($tmp['variety']) && $tmp['variety'] == 1) {
  1028. $currentItemId = $tmp['id'] ?? 0;
  1029. $xjList = CgXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
  1030. if (!empty($xjList)) {
  1031. foreach ($xjList as $xjKey => $xj) {
  1032. $xjBase = $tmp;
  1033. $num = $xj['num'] ?? 0;
  1034. $name = $xj['name'] ?? '';
  1035. $cover = $xj['cover'] ?? '';
  1036. $price = $xj['price'] ?? '';
  1037. $totalPrice = bcmul($num, $price, 2);
  1038. if ($num > 0) {
  1039. $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1040. $xjBase['name'] = $name;
  1041. $xjBase['itemNum'] = floatval($num);
  1042. $xjBase['bigNum'] = floatval($num);
  1043. $xjBase['smallNum'] = 0;
  1044. $xjBase['price'] = $price;
  1045. $xjBase['totalPrice'] = $totalPrice;
  1046. $xjBase['xhNum'] = floatval($num);
  1047. $xjAddList[] = $xjBase;
  1048. }
  1049. $xjData[] = $xjBase;
  1050. }
  1051. } else {
  1052. $itemData[] = $tmp;
  1053. }
  1054. } else {
  1055. $itemData[] = $tmp;
  1056. }
  1057. }
  1058. if (!empty($xjData)) {
  1059. $itemData = array_merge($itemData, $xjData);
  1060. }
  1061. }
  1062. $list[$key]['product'] = $itemData;
  1063. }
  1064. }
  1065. return $list;
  1066. }
  1067. //订单取消,变更状态,占用库存释放 ssh 2021.5.8
  1068. public static function cancel($id)
  1069. {
  1070. $purchase = PurchaseClass::getLockById($id);
  1071. $purchase->status = self::STATUS_CANCEL;
  1072. $purchase->cancelTime = date("Y-m-d H:i:s");
  1073. $purchase->save();
  1074. $shopId = $purchase->shopId;
  1075. $orderSn = $purchase->orderSn ?? '';
  1076. $shop = ShopClass::getLockById($shopId);
  1077. if (empty($shop)) {
  1078. util::fail('没有找到门店60');
  1079. }
  1080. $mainId = $shop->mainId ?? 0;
  1081. $main = MainClass::getLockById($mainId);
  1082. if (empty($main)) {
  1083. util::fail('没有main信息14');
  1084. }
  1085. $main->cgUnPay -= 1;
  1086. $main->cgCancel += 1;
  1087. $main->save();
  1088. $ghsId = $purchase->ghsId;
  1089. $ghs = GhsClass::getById($ghsId, true);
  1090. if (empty($ghs)) {
  1091. util::fail('没有找到供货商');
  1092. }
  1093. $ghsShopId = $ghs->shopId ?? 0;
  1094. $ghsShop = ShopClass::getById($ghsShopId, true);
  1095. if (empty($ghsShop)) {
  1096. util::fail('没有找到供货商门店');
  1097. }
  1098. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1099. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1100. $params = [
  1101. 'appid' => 'OP00002119',
  1102. 'serial_no' => '018b08cfddbd',
  1103. 'merchant_no' => $ghsShop->lklSjNo,
  1104. 'term_no' => $ghsShop->lklScanTermNo,
  1105. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1106. 'lklCertificatePath' => $lklCertificatePath,
  1107. ];
  1108. $laResource = new Lakala($params);
  1109. $closeParams = ['orderSn' => $orderSn];
  1110. $response = $laResource->close($closeParams);
  1111. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1112. $msg = $response['msg'] ?? '';
  1113. $code = $response['code'] ?? 0;
  1114. //主扫(手机上微信下单拉取微信支付)调用这个方法有效,如果收银机上扫客户收款码的,调用这个方法无效,具体说明看 https://o.lakala.com/#/home/document/detail?id=115 所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
  1115. //noticeUtil::push('花店采购 ' . $orderSn . ' 关单没有成功哦' . $msg . ' ' . $code, '15280215347');
  1116. }
  1117. //收银台关单
  1118. $closeParams = ['orderSn' => $orderSn];
  1119. $response = $laResource->cashClose($closeParams);
  1120. if (!isset($response['code']) || $response['code'] != '000000') {
  1121. $msg = $response['msg'] ?? '';
  1122. $code = $response['code'] ?? 0;
  1123. //聚合收银台(APP发起的微信和支付支付)调用这个方法有效,说明 https://o.lakala.com/#/home/document/detail?id=722,其他情况调用无法,所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
  1124. //noticeUtil::push('花店采购 ' . $orderSn . ' 收银台关单的提示:' . $msg . ' ' . $code, '15280215347');
  1125. }
  1126. return $purchase;
  1127. }
  1128. //采购统计2021-05-08
  1129. //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
  1130. public static function statOrderInfo($shopId, $statType)
  1131. {
  1132. $start = '';
  1133. $end = '';
  1134. switch ($statType) {
  1135. case "today":
  1136. $start = date('Y-m-d');
  1137. $end = $start;
  1138. break;
  1139. case "yesterday" :
  1140. $start = date('Y-m-d', strtotime("-1 day"));
  1141. $end = $start;
  1142. break;
  1143. case "seven":
  1144. $start = date('Y-m-d', strtotime("-6 day"));
  1145. $end = date('Y-m-d');
  1146. break;
  1147. case "thirty":
  1148. $start = date('Y-m-d', strtotime("-29 day"));
  1149. $end = date('Y-m-d');
  1150. break;
  1151. default:
  1152. }
  1153. $where = [];
  1154. $where['shopId'] = $shopId;
  1155. $where['addTime'] = ['between', [$start, $end]];
  1156. $res = self::getAllByCondition($where, null, "id,actPrice");
  1157. $totalAmount = 0;
  1158. $totalNum = count($res);
  1159. foreach ($res as $v) {
  1160. $totalAmount = bcadd($totalAmount, $v['actPrice'], 2);
  1161. }
  1162. return [
  1163. 'totalAmount' => $totalAmount,
  1164. 'totalNum' => $totalNum,
  1165. ];
  1166. }
  1167. //采购单完成 ssh 2021.5.15
  1168. public static function complete($purchase)
  1169. {
  1170. $purchase->status = self::STATUS_COMPLETE;
  1171. $purchase->save();
  1172. $shopId = $purchase->shopId;
  1173. $shop = ShopClass::getLockById($shopId);
  1174. if (empty($shop)) {
  1175. util::fail('没有找到门店61');
  1176. }
  1177. $mainId = $shop->mainId ?? 0;
  1178. $main = MainClass::getLockById($mainId);
  1179. if (empty($main)) {
  1180. util::fail('没有main信息15');
  1181. }
  1182. $main->cgSending -= 1;
  1183. $main->cgFinish += 1;
  1184. $main->save();
  1185. /********订单完成后加库存 lqh 2021-05-10*********/
  1186. $orderSn = $purchase->orderSn;
  1187. $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  1188. foreach ($purchaseItem as $v) {
  1189. $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
  1190. //记录库存流水
  1191. $recordData = [];
  1192. $recordData['sjId'] = $purchase->sjId;
  1193. $recordData['shopId'] = $purchase->shopId;
  1194. $recordData['mainId'] = $purchase->mainId;
  1195. $recordData['itemId'] = $v['itemId'] ?? 0;
  1196. $recordData['itemNum'] = $v['itemNum'];
  1197. $recordData['oldStock'] = $stockInfo['oldStock'];
  1198. $recordData['newStock'] = $stockInfo['newStock'];
  1199. $recordData['productId'] = $v['productId'];
  1200. $recordData['orderSn'] = $orderSn;
  1201. StockRecordClass::addPurchaseOrderRecord($recordData);
  1202. }
  1203. }
  1204. //订单信息变更为配送中 ssh 2021.5.15
  1205. public static function sending($purchaseId)
  1206. {
  1207. $purchase = PurchaseClass::getById($purchaseId, true);
  1208. if (empty($purchase)) {
  1209. util::fail('没有采购单信息');
  1210. }
  1211. $purchase->status = PurchaseClass::STATUS_SENDING;
  1212. $purchase->save();
  1213. $shopId = $purchase->shopId;
  1214. $shop = ShopClass::getLockById($shopId);
  1215. if (empty($shop)) {
  1216. util::fail('没有找到门店62');
  1217. }
  1218. $shop->cgUnSend -= 1;
  1219. $shop->cgSending += 1;
  1220. $shop->save();
  1221. //自动完成订单时不需要通知
  1222. $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
  1223. if ($noNeedWxNotice == 0) {
  1224. WxMessageClass::cgOrderHasSend($shop, $purchase);
  1225. }
  1226. }
  1227. public static function valid($cg, $shopId)
  1228. {
  1229. if (isset($cg->shopId) == false || $cg->shopId != $shopId) {
  1230. util::fail('无法访问');
  1231. }
  1232. }
  1233. }