PurchaseClass.php 54 KB

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