PurchaseClass.php 56 KB

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