PurchaseOrderClass.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\MainClass;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  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 bizGhs\base\classes\BaseClass;
  13. use bizGhs\custom\classes\CustomClass;
  14. use bizGhs\item\classes\CostChangeClass;
  15. use bizGhs\order\traits\OrderTrait;
  16. use bizGhs\shop\classes\ShopAdminClass;
  17. use bizGhs\stock\classes\OnStockRecordClass;
  18. use bizGhs\stock\classes\StockRecordClass;
  19. use bizGhs\stock\services\StockRecordService;
  20. use bizGhs\product\classes\ProductClass;
  21. use common\components\dict;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use common\components\printUtil;
  26. use common\components\util;
  27. use Yii;
  28. //采购单 lqh 2021.1.20
  29. class PurchaseOrderClass extends BaseClass
  30. {
  31. use OrderTrait;
  32. public static $baseFile = '\bizGhs\order\models\PurchaseOrder';
  33. const PURCHASE_ORDER_STATUS_WAIT = 1; //待确认
  34. const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送,待发货
  35. const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中,待入库
  36. const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已入库
  37. const PURCHASE_ORDER_STATUS_CANCEL = 5; //已取消
  38. public static $statusMap = [
  39. self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
  40. self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
  41. self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
  42. self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
  43. self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
  44. ];
  45. const DEBT_UNKNOWN = 0;
  46. const DEBT_YES = 1;
  47. const DEBT_NO = 2;
  48. //寄付与到付
  49. const YF_PAY_WAY_JF = 1;
  50. const YF_PAY_WAY_DF = 2;
  51. //稍后入库和直接入库
  52. const IN_TYPE_LATER = 0;
  53. const IN_TYPE_NOW = 1;
  54. //取消采购入库 ssh 20231218
  55. public static function cancel($cg)
  56. {
  57. $cg->status = self::PURCHASE_ORDER_STATUS_CANCEL;
  58. $cg->save();
  59. $orderSn = $cg->orderSn ?? '';
  60. $mainId = $cg->mainId ?? 0;
  61. $sjId = $cg->sjId ?? 0;
  62. $shopId = $cg->shopId ?? 0;
  63. $ghsName = $cg->ghsName ?? '';
  64. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  65. if (empty($itemList)) {
  66. util::fail('没有找到花材');
  67. }
  68. foreach ($itemList as $cgKey => $cgItem) {
  69. $productId = $cgItem['productId'] ?? 0;
  70. $currentNum = $cgItem['itemNum'] ?? 0;
  71. $ptItemId = $cgItem['itemId'] ?? 0;
  72. $presell = $cgItem['presell'] ?? 0;
  73. $product = ProductClass::getLockById($productId);
  74. if (empty($product)) {
  75. util::fail('没有找到花材哦');
  76. }
  77. if ($presell == 0) {
  78. //减少路上库存
  79. $onRecord = [];
  80. $onRecord['sjId'] = $sjId;
  81. $onRecord['shopId'] = $shopId;
  82. $onRecord['mainId'] = $mainId;
  83. $onRecord['orderSn'] = $orderSn;
  84. $onRecord['productId'] = $productId;
  85. $onRecord['itemId'] = $ptItemId;
  86. $onRecord['relateName'] = $ghsName;
  87. $onRecord['itemNum'] = $currentNum;
  88. $oldOnStock = $product->onStock ?? 0;
  89. $onRecord['oldStock'] = $oldOnStock;
  90. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  91. $onRecord['ptStyle'] = 2;
  92. OnStockRecordClass::cancelCg($onRecord);
  93. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  94. }
  95. }
  96. }
  97. public static function printGroup($current, $content)
  98. {
  99. //58mm的机器,一行打印16个汉字,32个字母
  100. $bigPrice = $current['bigPrice'] ?? '';
  101. $bigPrice = floatval($bigPrice);
  102. $bigUnit = $current['bigUnit'] ?? '扎';
  103. $name = $current['name'] ?? '';
  104. if (floatval($bigPrice) == 0.01) {
  105. $name = $name . '(送)';
  106. }
  107. $count = $current['itemNum'] ?? '';
  108. $content .= '<B>' . $name . ' ' . $count . $bigUnit . '</B><BR>';
  109. $totalPrice = bcmul($count, $bigPrice, 2);
  110. $totalPrice = floatval($totalPrice);
  111. $content .= $count . $bigUnit . 'x' . $bigPrice . '元' . '=' . $totalPrice . '元<BR>';
  112. if (isset($current['refundNum']) && $current['refundNum'] > 0) {
  113. $content .= '<B>已退' . $current['refundNum'] . $bigUnit . '</B><BR>';
  114. }
  115. $content .= '--------------------------------<BR>';
  116. return $content;
  117. }
  118. //打印小票 ssh 20230608
  119. public static function printTicket($orderInfo, $firstPrint = true, $order = 1)
  120. {
  121. $orderInfo->printNum += 1;
  122. $orderInfo->save();
  123. $ghsId = $orderInfo->ghsId ?? 0;
  124. $debtCount = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES]);
  125. $debtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  126. $orderSn = $orderInfo->orderSn ?? '';
  127. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  128. $shopId = $orderInfo->shopId ?? 0;
  129. $shop = ShopClass::getById($shopId, true);
  130. $telephone = $shop->telephone ?? '';
  131. $sjId = $shop->sjId ?? 0;
  132. $sj = SjClass::getById($sjId, true);
  133. $sjName = $sj->name ?? '';
  134. $shopName = $shop->shopName ?? '';
  135. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  136. $content = "<CB>" . $currentName . "</CB>";
  137. $content .= "<CB>采购单</CB>";
  138. $content .= '--------------------------------<BR>';
  139. $content .= '<C>供货商</C><BR>';
  140. $content .= '<CB>' . $orderInfo->ghsName . '</CB><BR>';
  141. $shortOrderSn = substr($orderSn, -4);
  142. $content .= '<CB>' . $shortOrderSn . '</CB><BR>';
  143. $currentTime = date("Y-m-d H:i", strtotime($orderInfo->entryTime));
  144. $content .= '<C>' . $currentTime . '</C><BR>';
  145. if (isset($orderInfo->remark) && !empty($orderInfo->remark)) {
  146. $content .= '<BR>';
  147. $content .= '备注:<BR>';
  148. $content .= '<B>' . $orderInfo->remark . '</B><BR>';
  149. }
  150. $content .= '<BR>';
  151. $content .= '商品 数量/单价 金额 <BR>';
  152. $content .= '--------------------------------<BR>';
  153. if (isset($itemList) && !empty($itemList)) {
  154. foreach ($itemList as $current) {
  155. $content = self::printGroup($current, $content);
  156. }
  157. }
  158. $kindNum = $orderInfo->kind ?? 0;
  159. $bigNum = $orderInfo->itemNum ? floatval($orderInfo->itemNum) : 0;
  160. $content .= '<BR>********************************<BR>';
  161. $content .= "<CB>共{$kindNum}种";
  162. if ($bigNum > 0) {
  163. $content .= ",{$bigNum}扎";
  164. }
  165. $content .= "</CB>";
  166. $content .= '********************************<BR><BR>';
  167. if (isset($orderInfo['itemPrice']) && $orderInfo['itemPrice'] > 0) {
  168. $content .= '商品金额:' . floatval($orderInfo['itemPrice']) . '元<BR>';
  169. }
  170. if (isset($orderInfo['sendCost']) && $orderInfo['sendCost'] > 0) {
  171. $content .= ' 运费:' . floatval($orderInfo['sendCost']) . '元<BR>';
  172. }
  173. if (isset($orderInfo['packCost']) && $orderInfo['packCost'] > 0) {
  174. $content .= ' 打包费:' . floatval($orderInfo['packCost']) . '元<BR>';
  175. }
  176. $content .= '合计金额:' . floatval($orderInfo['prePrice']) . '元<BR>';
  177. if (isset($orderInfo['discountAmount']) && !empty($orderInfo['discountAmount']) && $orderInfo['discountAmount'] > 0) {
  178. $content .= '优惠扣除:-' . floatval($orderInfo['discountAmount']) . '元<BR>';
  179. }
  180. if (isset($orderInfo['tkPrice']) && $orderInfo['tkPrice'] > 0) {
  181. $content .= '退款金额:' . floatval($orderInfo['tkPrice']) . '元<BR>';
  182. }
  183. $content .= '<B>订单金额:' . floatval($orderInfo['realPrice']) . '</B><BR>';
  184. if (isset($orderInfo['debt']) && $orderInfo['debt'] == 1) {
  185. $content .= '实付金额:0元<BR>';
  186. } else {
  187. $content .= '实付金额:' . floatval($orderInfo['realPrice']) . '元<BR>';
  188. }
  189. // if ($debtAmount > 0) {
  190. // $content .= '累计赊账:' . $debtCount . '笔 ' . floatval($debtAmount) . '元<BR>';
  191. // }
  192. $content .= '--------------------------------<BR>';
  193. $content .= '订单编号:' . $orderInfo['orderSn'] . '<BR>';
  194. if (!empty($telephone)) {
  195. $content .= '联系电话:' . $telephone . '<BR>';
  196. }
  197. $content .= "<BR>";
  198. $content .= "<BR>";
  199. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  200. $printSn = $ext['wcPrintSn'] ?? '';
  201. if ($firstPrint) {
  202. if (isset($orderInfo->fistWcPrintNum) && $orderInfo->fistWcPrintNum > 1) {
  203. if ($orderInfo->fistWcPrintNum == 2) {
  204. $printSn = $ext['wcPrintSn2'] ?? '';
  205. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  206. }
  207. if ($orderInfo->fistWcPrintNum == 3) {
  208. $printSn = $ext['wcPrintSn3'] ?? '';
  209. noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  210. }
  211. }
  212. } else {
  213. if ($order > 1) {
  214. if ($order == 2) {
  215. $printSn = $ext['wcPrintSn2'] ?? '';
  216. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  217. }
  218. if ($order == 3) {
  219. $printSn = $ext['wcPrintSn3'] ?? '';
  220. noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  221. }
  222. }
  223. }
  224. if (!empty($printSn)) {
  225. $p = new printUtil($printSn);
  226. $p->printMsg($content, $shop);
  227. }
  228. }
  229. public static function putIn($cg, $data)
  230. {
  231. if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
  232. util::fail('已经入过库了');
  233. }
  234. if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
  235. util::fail('已取消');
  236. }
  237. $orderSn = $cg->orderSn ?? '';
  238. $mainId = $cg->mainId ?? 0;
  239. $sjId = $cg->sjId ?? 0;
  240. $shopId = $cg->shopId ?? 0;
  241. $staffId = $data['staffId'] ?? 0;
  242. $staffName = $data['staffName'] ?? '';
  243. $adminId = $data['adminId'] ?? 0;
  244. $shop = ShopClass::getLockById($shopId);
  245. if (empty($shop)) {
  246. util::fail('没有找到门店');
  247. }
  248. $ghsName = $cg->ghsName ?? '';
  249. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  250. if (empty($itemList)) {
  251. util::fail('没有找到花材');
  252. }
  253. foreach ($itemList as $cgKey => $cgItem) {
  254. $productId = $cgItem['productId'] ?? 0;
  255. $currentNum = $cgItem['itemNum'] ?? 0;
  256. $ptItemId = $cgItem['itemId'] ?? 0;
  257. $unitCost = $cgItem['cost'] ?? 0;
  258. $presell = $cgItem['presell'] ?? 0;
  259. $product = ProductClass::getLockById($productId);
  260. if (empty($product)) {
  261. util::fail('没有找到花材哦');
  262. }
  263. if ($presell == 0) {
  264. //加库存和流水记录
  265. $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum);
  266. $record = [];
  267. $record['sjId'] = $sjId;
  268. $record['shopId'] = $shopId;
  269. $record['mainId'] = $mainId;
  270. $record['orderSn'] = $orderSn;
  271. $record['productId'] = $productId;
  272. $record['itemId'] = $ptItemId;
  273. $record['relateName'] = $ghsName;
  274. $record['itemNum'] = $currentNum;
  275. $record['oldStock'] = $stockInfo['oldStock'];
  276. $record['newStock'] = $stockInfo['newStock'];
  277. StockRecordClass::addPurchaseOrderRecord($record);
  278. //减少路上库存
  279. $onRecord = [];
  280. $onRecord['sjId'] = $sjId;
  281. $onRecord['shopId'] = $shopId;
  282. $onRecord['mainId'] = $mainId;
  283. $onRecord['orderSn'] = $orderSn;
  284. $onRecord['productId'] = $productId;
  285. $onRecord['itemId'] = $ptItemId;
  286. $onRecord['relateName'] = $ghsName;
  287. $onRecord['itemNum'] = $currentNum;
  288. $oldOnStock = $product->onStock ?? 0;
  289. $onRecord['oldStock'] = $oldOnStock;
  290. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  291. $onRecord['ptStyle'] = 2;
  292. OnStockRecordClass::addInStockOrderRecord($onRecord);
  293. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  294. }
  295. $product->cost = $unitCost;
  296. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  297. $product->save();
  298. //成本变动记录
  299. $changeData = [
  300. 'ptStyle' => 2,
  301. 'sjId' => $sjId,
  302. 'mainId' => $mainId,
  303. 'itemId' => $productId,
  304. 'ptItemId' => $ptItemId,
  305. 'cost' => $unitCost,
  306. 'staffId' => $staffId,
  307. 'staffName' => $staffName,
  308. 'name' => $product->name ?? '',
  309. 'cover' => $product->cover ?? '',
  310. 'targetId' => $order->id ?? 0,
  311. 'event' => '采购',
  312. ];
  313. CostChangeClass::addData($changeData);
  314. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  315. //直营门店的成本价也要一起改掉
  316. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  317. if (!empty($allShop)) {
  318. $currentItemId = $product->itemId ?? 0;
  319. foreach ($allShop as $currentShop) {
  320. $currentId = $currentShop->mainId ?? 0;
  321. if ($currentId == $mainId) {
  322. continue;
  323. }
  324. if (isset($currentShop->join) && $currentShop->join == 1) {
  325. continue;
  326. }
  327. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  328. if (!empty($currentProduct)) {
  329. $currentProduct->cost = $unitCost;
  330. $currentProduct->save();
  331. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  332. //成本变动记录
  333. $changeData = [
  334. 'ptStyle' => 2,
  335. 'sjId' => $currentProduct->sjId ?? 0,
  336. 'mainId' => $currentProduct->mainId ?? 0,
  337. 'itemId' => $currentProduct->id ?? 0,
  338. 'ptItemId' => $currentProduct->itemId ?? 0,
  339. 'cost' => $unitCost,
  340. 'staffId' => $currentStaff->id ?? 0,
  341. 'staffName' => $currentStaff->name ?? '',
  342. 'name' => $currentProduct->name ?? '',
  343. 'cover' => $currentProduct->cover ?? '',
  344. 'targetId' => $order->id ?? 0,
  345. 'event' => $shop->shopName . '采购',
  346. ];
  347. CostChangeClass::addData($changeData);
  348. }
  349. }
  350. }
  351. }
  352. }
  353. //门店支出增加
  354. $currentPrice = $cg->actPrice ?? 0;
  355. $mainId = $shop->mainId ?? 0;
  356. $main = MainClass::getLockById($mainId);
  357. if (empty($main)) {
  358. util::fail('没有资产信息');
  359. }
  360. //支出增加
  361. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  362. $main->totalExpend = $currentTotalExpend;
  363. $main->save();
  364. //采购增加
  365. $main->cgFinish += 1;
  366. $main->totalPurchaseOrder += 1;
  367. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  368. $main->totalPurchase = $currentTotalPurchase;
  369. $main->save();
  370. //供货商资产增加
  371. $ghsId = $cg->ghsId ?? 0;
  372. $ghs = GhsClass::getLockById($ghsId);
  373. if (empty($ghs)) {
  374. util::fail('没有找到供货商');
  375. }
  376. $ghs->debt = GhsClass::DEBT_YES;
  377. $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
  378. $ghs->debtNum += 1;
  379. $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
  380. $ghs->expendNum += 1;
  381. $ghs->save();
  382. //采购统计
  383. $cgNum = $order->itemNum ?? 0;
  384. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  385. //采购按供货商统计
  386. StatCgGhsClass::ghsReplace($cg);
  387. //门店应收客户款增加
  388. $ghsShopId = $ghs->shopId;
  389. $ghsShop = ShopClass::getLockById($ghsShopId);
  390. if (empty($ghsShop)) {
  391. util::fail('没有找到门店');
  392. }
  393. $ghsMainId = $ghsShop->mainId ?? 0;
  394. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  395. if (empty($ghsMain)) {
  396. util::fail('没有main信息');
  397. }
  398. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  399. $ghsMain->mayGathering = $currentMayGathering;
  400. //客户资产增加
  401. $customId = $ghs->customId;
  402. $custom = CustomClass::getLockById($customId);
  403. if (empty($custom)) {
  404. util::fail('没有找到客户信息');
  405. }
  406. $custom->isDebt = CustomClass::IS_DEBT_YES;
  407. $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
  408. if ($custom->debtNum == 0) {
  409. $ghsMain->mayGatheringNum += 1;
  410. }
  411. $ghsMain->save();
  412. $custom->debtNum += 1;
  413. $custom->buyNum += 1;
  414. $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
  415. $custom->save();
  416. //当天和当月支出统计
  417. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  418. //支出流水
  419. $payWay = dict::getDict('payWay', 'unknown');
  420. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  421. $sjId = $order->sjId ?? 0;
  422. $shopId = $order->shopId ?? 0;
  423. $event = '采购';
  424. $mainId = $shop->mainId ?? 0;
  425. $capitalData = [
  426. 'capitalType' => $capitalType,
  427. 'io' => 0,
  428. 'totalExpend' => $currentTotalExpend,
  429. 'payWay' => $payWay,
  430. 'amount' => $currentPrice,
  431. 'sjId' => $sjId,
  432. 'shopId' => $shopId,
  433. 'event' => $event,
  434. 'mainId' => $mainId
  435. ];
  436. ShopCapitalClass::addCapital($capitalData);
  437. $cg->debt = self::DEBT_YES;
  438. $cg->status = self::PURCHASE_ORDER_STATUS_COMPLETE;
  439. $cg->entryTime = date("Y-m-d H:i:s");
  440. $cg->save();
  441. return $cg;
  442. }
  443. //添加采购单 lqh 2021.1.19
  444. public static function addOrder($data)
  445. {
  446. $sjId = $data['sjId'] ?? 0;
  447. $shopId = $data['shopId'] ?? 0;
  448. $mainId = $data['mainId'] ?? 0;
  449. $ghsName = $data['ghsName'] ?? '';
  450. $staffId = $data['staffId'] ?? 0;
  451. $staffName = $data['staffName'] ?? '';
  452. $inType = $data['inType'] ?? self::IN_TYPE_NOW;
  453. $adminId = $data['adminId'] ?? 0;
  454. $orderSn = orderSn::getGhsPurchaseSn();
  455. $data['orderSn'] = $orderSn;
  456. $status = $inType == self::IN_TYPE_NOW ? self::PURCHASE_ORDER_STATUS_COMPLETE : self::PURCHASE_ORDER_STATUS_SENDING;
  457. $data['status'] = $status;
  458. //数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
  459. $ghsItemInfo = $data['itemInfo'];
  460. $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
  461. //总共多少扎
  462. $totalBigNum = 0;
  463. //总共多少支
  464. $totalSmallNum = 0;
  465. //总数量
  466. $totalNum = 0;
  467. $kindNum = 0;
  468. $totalItemPrice = 0;
  469. $totalWeight = 0;
  470. $productIds = array_column($ghsItemInfo, 'productId');
  471. $productIds = array_unique(array_filter($productIds));
  472. $productList = ProductClass::getByIds($productIds, null, 'id');
  473. foreach ($ghsItemInfo as $v) {
  474. $ratioType = $v['ratioType'] ?? 1;
  475. $ratio = $v['ratio'] ?? 1;
  476. $currentProductId = $v['productId'] ?? 0;
  477. $unitWeight = $productList[$currentProductId]['weight'] ?? 0;
  478. if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
  479. util::fail('请填写价格');
  480. }
  481. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  482. $thisBigNum = $v['bigNum'];
  483. $totalBigNum = bcadd($totalBigNum, $thisBigNum, 2);
  484. $totalNum = bcadd($totalNum, $thisBigNum, 2);
  485. $currentTotalPrice = bcmul($thisBigNum, $v['itemPrice'], 2);
  486. $weight = bcmul($unitWeight, $thisBigNum, 2);
  487. } else {
  488. if (isset($v['smallNum']) == false || $v['smallNum'] <= 0) {
  489. util::fail('请填写数量');
  490. }
  491. if ($ratioType == 1) {
  492. util::fail('小单位采购,单位比必须固定');
  493. }
  494. if ($ratio <= 1) {
  495. util::fail('小单位采购,单位比必须大于1');
  496. }
  497. $thisSmallNum = $v['smallNum'];
  498. $changeToBigNum = bcdiv($thisSmallNum, $ratio, 2);
  499. $totalNum = bcadd($totalNum, $changeToBigNum, 2);
  500. $totalSmallNum = bcadd($totalSmallNum, $thisSmallNum, 2);
  501. $currentTotalPrice = bcmul($thisSmallNum, $v['itemPrice'], 2);
  502. $weight = bcmul($unitWeight, $changeToBigNum, 2);
  503. }
  504. $kindNum++;
  505. $totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
  506. $totalWeight = bcadd($totalWeight, $weight, 2);
  507. }
  508. $data['bigNum'] = $totalBigNum;
  509. $data['smallNum'] = $totalSmallNum;
  510. $data['itemNum'] = $totalNum;
  511. $data['kind'] = $kindNum;
  512. $data['price'] = $totalItemPrice;
  513. $data['totalWeight'] = $totalWeight;
  514. //计算订单总价
  515. $data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
  516. $data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
  517. $data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
  518. $data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
  519. $data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
  520. $cgModel = $data['cgModel'] ?? 0;
  521. if ($cgModel == 1) {
  522. //简约采购模式
  523. $customAvgKgWeight = $data['avgKgWeight'] ?? 0;
  524. $data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
  525. unset($data['modifyPrice']);
  526. } else {
  527. //正常采购模式不要使用传过来的每公斤运费
  528. unset($data['avgKgWeight']);
  529. }
  530. //直接入库的有入库时间
  531. if ($inType == self::IN_TYPE_NOW) {
  532. $data['entryTime'] = empty($data['entryTime']) ? date('Y-m-d H:i:s') : $data['entryTime'];
  533. } else {
  534. $data['entryTime'] = '0000-00-00 00:00:00';
  535. }
  536. $packingCharge = $data['packingCharge'] ?? 0;
  537. $shortCharge = $data['shortCharge'] ?? 0;
  538. $longCharge = $data['longCharge'] ?? 0;
  539. $pickCharge = $data['pickCharge'] ?? 0;
  540. $localCharge = $data['localCharge'] ?? 0;
  541. //提货费和本地运费
  542. $paidPrice = bcadd($localCharge, $pickCharge, 2);
  543. $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
  544. //打包费
  545. $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
  546. //寄付才算长途和短途运费
  547. if ($yfPayWay == self::YF_PAY_WAY_JF) {
  548. $currentPrice = bcadd($currentPrice, $shortCharge, 2);
  549. $currentPrice = bcadd($currentPrice, $longCharge, 2);
  550. } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
  551. $paidPrice = bcadd($paidPrice, $shortCharge, 2);
  552. $paidPrice = bcadd($paidPrice, $longCharge, 2);
  553. } else {
  554. util::fail('没有找到这个运费支付方式');
  555. }
  556. $data['prePrice'] = $currentPrice;
  557. if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
  558. //报损减免后金额
  559. $modifyPrice = $data['modifyPrice'] ?? 0;
  560. if ($modifyPrice > $currentPrice) {
  561. util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
  562. }
  563. if ($modifyPrice < $currentPrice) {
  564. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  565. $data['discountType'] = dict::getDict('discountType', 'discount');
  566. $currentPrice = $modifyPrice;
  567. }
  568. }
  569. $data['orderPrice'] = $currentPrice;
  570. $data['actPrice'] = $currentPrice;
  571. $data['realPrice'] = $currentPrice;
  572. $data['paidPrice'] = $paidPrice;
  573. //总运费
  574. $totalFreight = bcadd($packingCharge, $shortCharge, 2);
  575. $totalFreight = bcadd($totalFreight, $longCharge, 2);
  576. $totalFreight = bcadd($totalFreight, $pickCharge, 2);
  577. $totalFreight = bcadd($totalFreight, $localCharge, 2);
  578. $data['totalFreight'] = $totalFreight;
  579. $connection = Yii::$app->db;
  580. $transaction = $connection->beginTransaction();
  581. try {
  582. //总运费/总公斤数,计算每公斤的运费
  583. if ($totalWeight > 0) {
  584. $avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
  585. } else {
  586. $avgKgWeight = 0;
  587. }
  588. $avgKgWeight = round($avgKgWeight, 2);
  589. $data['avgKgWeight'] = $avgKgWeight;
  590. $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
  591. $order = self::add($data, true);
  592. $shop = ShopClass::getLockById($shopId);
  593. if (empty($shop)) {
  594. util::fail('没有找到门店');
  595. }
  596. //组装详情表数据
  597. $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
  598. print_r($batchData);
  599. die;
  600. //加库存
  601. foreach ($batchData as $currentKey => $v) {
  602. $itemId = $v['itemId'];
  603. $productId = $v['productId'];
  604. $presell = $productList[$productId]['presell'] ?? 0;
  605. //普通入库增加库存,预售入库不增加库存 ssh 20220109
  606. if ($presell == 0) {
  607. //增加路上库存
  608. $onRecord = [];
  609. $onRecord['sjId'] = $sjId;
  610. $onRecord['shopId'] = $shopId;
  611. $onRecord['mainId'] = $mainId;
  612. $onRecord['orderSn'] = $orderSn;
  613. $onRecord['productId'] = $productId;
  614. $onRecord['itemId'] = $itemId;
  615. $onRecord['relateName'] = $ghsName;
  616. $onRecord['itemNum'] = $v['itemNum'];
  617. $oldOnStock = $v['onStock'] ?? 0;
  618. $onRecord['oldStock'] = $oldOnStock;
  619. $newOnStock = bcadd($oldOnStock, $v['itemNum']);
  620. $onRecord['newStock'] = $newOnStock;
  621. $onRecord['ptStyle'] = 2;
  622. OnStockRecordClass::addPurchaseOrderRecord($onRecord);
  623. ProductClass::addOnStockByItemNum($productId, $v['itemNum']);
  624. if ($inType == self::IN_TYPE_NOW) {
  625. //增加库存
  626. $record = [];
  627. $record['sjId'] = $sjId;
  628. $record['shopId'] = $shopId;
  629. $record['mainId'] = $mainId;
  630. $record['orderSn'] = $orderSn;
  631. $record['productId'] = $productId;
  632. $record['itemId'] = $itemId;
  633. $record['relateName'] = $ghsName;
  634. $record['itemNum'] = $v['itemNum'];
  635. $record['oldStock'] = $v['itemStock'];
  636. $record['newStock'] = bcadd($v['itemStock'], $v['itemNum'], 2);
  637. StockRecordClass::addPurchaseOrderRecord($record);
  638. ProductClass::addStockByItemNum($productId, $v['itemNum']);
  639. //减少路上库存
  640. $onRecord = [];
  641. $onRecord['sjId'] = $sjId;
  642. $onRecord['shopId'] = $shopId;
  643. $onRecord['mainId'] = $mainId;
  644. $onRecord['orderSn'] = $orderSn;
  645. $onRecord['productId'] = $productId;
  646. $onRecord['itemId'] = $itemId;
  647. $onRecord['relateName'] = $ghsName;
  648. $onRecord['itemNum'] = $v['itemNum'];
  649. $onRecord['oldStock'] = $newOnStock;
  650. $onRecord['newStock'] = bcsub($newOnStock, $v['itemNum']);
  651. $onRecord['ptStyle'] = 2;
  652. OnStockRecordClass::addInStockOrderRecord($onRecord);
  653. ProductClass::decreaseOnStockByItemNum($productId, $v['itemNum']);
  654. }
  655. }
  656. //单位采购价
  657. $unitCgPrice = $v['bigPrice'] ?? 0;
  658. $productWeight = $productList[$productId]['weight'] ?? 0;
  659. $unitFreight = bcmul($avgKgWeight, $productWeight, 3);
  660. $unitCost = bcadd($unitCgPrice, $unitFreight, 3);
  661. $unitCost = round($unitCost, 2);
  662. $batchData[$currentKey]['bigFreight'] = $unitFreight;
  663. $batchData[$currentKey]['cost'] = $unitCost;
  664. $currentCover = $productList[$productId]['cover'] ?? '';
  665. $currentName = $productList[$productId]['name'] ?? '';
  666. $batchData[$currentKey]['cover'] = $currentCover;
  667. $batchData[$currentKey]['name'] = $currentName;
  668. $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
  669. $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
  670. $currentRatio = $productList[$productId]['ratio'] ?? 0;
  671. $currentRatioType = $productList[$productId]['ratioType'] ?? 0;
  672. $currentClassId = $productList[$productId]['classId'] ?? 0;
  673. $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
  674. $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
  675. $batchData[$currentKey]['ratio'] = $currentRatio;
  676. $batchData[$currentKey]['ratioType'] = $currentRatioType;
  677. $batchData[$currentKey]['presell'] = $presell;
  678. $batchData[$currentKey]['classId'] = $currentClassId;
  679. if ($inType == self::IN_TYPE_NOW) {
  680. $product = ProductClass::getLockById($productId);
  681. $product->cost = $unitCost;
  682. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  683. $product->save();
  684. //成本变动记录
  685. $changeData = [
  686. 'ptStyle' => 2,
  687. 'sjId' => $sjId,
  688. 'mainId' => $mainId,
  689. 'itemId' => $productId,
  690. 'ptItemId' => $itemId,
  691. 'cost' => $unitCost,
  692. 'staffId' => $staffId,
  693. 'staffName' => $staffName,
  694. 'name' => $product->name ?? '',
  695. 'cover' => $product->cover ?? '',
  696. 'targetId' => $order->id ?? 0,
  697. 'event' => '采购',
  698. ];
  699. CostChangeClass::addData($changeData);
  700. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  701. //直营门店的成本价也要一起改掉
  702. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  703. if (!empty($allShop)) {
  704. $currentItemId = $product->itemId ?? 0;
  705. foreach ($allShop as $currentShop) {
  706. $currentId = $currentShop->mainId ?? 0;
  707. if ($currentId == $mainId) {
  708. continue;
  709. }
  710. if (isset($currentShop->join) && $currentShop->join == 1) {
  711. continue;
  712. }
  713. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  714. if (!empty($currentProduct)) {
  715. $currentProduct->cost = $unitCost;
  716. $currentProduct->save();
  717. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  718. //成本变动记录
  719. $changeData = [
  720. 'ptStyle' => 2,
  721. 'sjId' => $currentProduct->sjId ?? 0,
  722. 'mainId' => $currentProduct->mainId ?? 0,
  723. 'itemId' => $currentProduct->id ?? 0,
  724. 'ptItemId' => $currentProduct->itemId ?? 0,
  725. 'cost' => $unitCost,
  726. 'staffId' => $currentStaff->id ?? 0,
  727. 'staffName' => $currentStaff->name ?? '',
  728. 'name' => $currentProduct->name ?? '',
  729. 'cover' => $currentProduct->cover ?? '',
  730. 'targetId' => $order->id ?? 0,
  731. 'event' => $shop->shopName . '采购',
  732. ];
  733. CostChangeClass::addData($changeData);
  734. }
  735. }
  736. }
  737. }
  738. }
  739. }
  740. //写入详情表
  741. PurchaseOrderItemClass::batchAddOrderItem($batchData);
  742. if ($inType == self::IN_TYPE_NOW) {
  743. //门店支出增加
  744. $mainId = $shop->mainId ?? 0;
  745. $main = MainClass::getLockById($mainId);
  746. if (empty($main)) {
  747. util::fail('没有资产信息');
  748. }
  749. //支出增加
  750. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  751. $main->totalExpend = $currentTotalExpend;
  752. $main->save();
  753. //采购增加
  754. $main->cgFinish += 1;
  755. $main->totalPurchaseOrder += 1;
  756. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  757. $main->totalPurchase = $currentTotalPurchase;
  758. $main->save();
  759. //供货商资产增加
  760. $ghsId = $data['ghsId'] ?? 0;
  761. $ghs = GhsClass::getLockById($ghsId);
  762. if (empty($ghs)) {
  763. util::fail('没有找到供货商');
  764. }
  765. $ghs->debt = GhsClass::DEBT_YES;
  766. $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
  767. $ghs->debtNum += 1;
  768. $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
  769. $ghs->expendNum += 1;
  770. $ghs->save();
  771. //采购统计
  772. $cgNum = $order->itemNum ?? 0;
  773. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  774. //采购按供货商统计
  775. StatCgGhsClass::ghsReplace($order);
  776. //门店应收客户款增加
  777. $ghsShopId = $ghs->shopId;
  778. $ghsShop = ShopClass::getLockById($ghsShopId);
  779. if (empty($ghsShop)) {
  780. util::fail('没有找到门店');
  781. }
  782. $ghsMainId = $ghsShop->mainId ?? 0;
  783. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  784. if (empty($ghsMain)) {
  785. util::fail('没有main信息');
  786. }
  787. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  788. $ghsMain->mayGathering = $currentMayGathering;
  789. //客户资产增加
  790. $customId = $ghs->customId;
  791. $custom = CustomClass::getLockById($customId);
  792. if (empty($custom)) {
  793. util::fail('没有找到客户信息');
  794. }
  795. $custom->isDebt = CustomClass::IS_DEBT_YES;
  796. $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
  797. if ($custom->debtNum == 0) {
  798. $ghsMain->mayGatheringNum += 1;
  799. }
  800. $ghsMain->save();
  801. $custom->debtNum += 1;
  802. $custom->buyNum += 1;
  803. $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
  804. $custom->save();
  805. //当天和当月支出统计
  806. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  807. //支出流水
  808. $payWay = dict::getDict('payWay', 'unknown');
  809. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  810. $sjId = $order->sjId ?? 0;
  811. $shopId = $order->shopId ?? 0;
  812. $event = '采购';
  813. $mainId = $shop->mainId ?? 0;
  814. $capitalData = [
  815. 'capitalType' => $capitalType,
  816. 'io' => 0,
  817. 'totalExpend' => $currentTotalExpend,
  818. 'payWay' => $payWay,
  819. 'amount' => $currentPrice,
  820. 'sjId' => $sjId,
  821. 'shopId' => $shopId,
  822. 'event' => $event,
  823. 'mainId' => $mainId
  824. ];
  825. ShopCapitalClass::addCapital($capitalData);
  826. }
  827. $transaction->commit();
  828. return $order;
  829. } catch (\Exception $exception) {
  830. $transaction->rollBack();
  831. util::fail('保存失败:' . $exception->getMessage());
  832. }
  833. return false;
  834. }
  835. //采购单列表 lqh 2021.1.19
  836. public static function getOrderList($where)
  837. {
  838. $data = self::getList('*', $where, 'addTime DESC');
  839. $list = $data['list'] ?? [];
  840. $data['list'] = self::groupSupplierList($list);
  841. $data['list'] = self::groupAdminList($data['list']);
  842. $data['list'] = self::groupStatusName($data['list']);
  843. return $data;
  844. }
  845. //获取订单详情信息 lqh 2021.1.20
  846. public static function getOrderDetail($orderSn, $shopId)
  847. {
  848. $where = ['orderSn' => $orderSn];
  849. $orderData = self::getByCondition($where);
  850. if (empty($orderData)) {
  851. util::fail('订单不存在');
  852. }
  853. if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
  854. util::fail('无法访问');
  855. }
  856. $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
  857. if ($orderInfo) {
  858. foreach ($orderInfo as $key => $val) {
  859. $shortCover = $val['cover'] ?? '';
  860. $orderInfo[$key]['ShortCover'] = $shortCover;
  861. $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  862. $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  863. }
  864. }
  865. $orderData = self::groupAdmin($orderData);
  866. //供货商信息
  867. $ghsId = $orderData['ghsId'] ?? 0;
  868. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  869. $orderData['supplierName'] = $ghsInfo['name'] ?? '';
  870. $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
  871. $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
  872. $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
  873. $orderData['itemInfo'] = $orderInfo;
  874. $orderData['statusName'] = self::getStatusName($orderData['status']);
  875. //1寄付 2到付
  876. $yfPayWay = $orderData['yfPayWay'] ?? 1;
  877. $actPrice = $orderData['actPrice'] ?? 0;
  878. $shortCharge = $orderData['shortCharge'] ?? 0;
  879. $longCharge = $orderData['longCharge'] ?? 0;
  880. $pickCharge = $orderData['pickCharge'] ?? 0;
  881. $localCharge = $orderData['localCharge'] ?? 0;
  882. $totalCgCost = $actPrice;
  883. if ($yfPayWay == 2) {
  884. $totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
  885. $totalCgCost = bcadd($totalCgCost, $longCharge, 2);
  886. }
  887. $totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
  888. $totalCgCost = bcadd($totalCgCost, $localCharge, 2);
  889. $orderData['totalCgCost'] = $totalCgCost;
  890. return $orderData;
  891. }
  892. // 组装 orderItem lqh 2021.1.25
  893. public static function groupOrderItem($ghsItemInfo, $orderSn)
  894. {
  895. $productData = self::getProductMapData($ghsItemInfo);
  896. $batchData = [];
  897. foreach ($ghsItemInfo as $v) {
  898. $tmp = [];
  899. $productId = $v['productId'];
  900. $itemId = $productData[$productId]['itemId'];
  901. $tmp['orderSn'] = $orderSn;
  902. $tmp['itemId'] = $itemId;
  903. $tmp['productId'] = $v['productId'];
  904. $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;
  905. $tmp['onStock'] = $productData[$productId]['onStock'] ?? 0;
  906. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  907. $tmp['itemNum'] = $v['bigNum'];
  908. $tmp['bigPrice'] = $v['itemPrice'];
  909. $tmp['totalPrice'] = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
  910. } else {
  911. $smallNum = $v['smallNum'] ?? 0;
  912. if ($smallNum <= 0) {
  913. util::fail('没有数量');
  914. }
  915. $ratioType = $v['ratioType'] ?? 1;
  916. if ($ratioType == 1) {
  917. util::fail('小单位,单位比必须固定的哈');
  918. }
  919. $ratio = $v['ratio'] ?? 1;
  920. if ($ratio <= 1) {
  921. util::fail('小单位,单位比必须大于1的哦');
  922. }
  923. $changeToBigNum = bcdiv($smallNum, $ratio, 2);
  924. $tmp['itemNum'] = $changeToBigNum;
  925. $bigPrice = bcmul($v['itemPrice'], $ratio, 2);
  926. $tmp['bigPrice'] = $bigPrice;
  927. $tmp['totalPrice'] = bcmul($v['itemPrice'], $smallNum, 2);
  928. }
  929. $batchData[] = $tmp;
  930. }
  931. return $batchData;
  932. }
  933. // 组装状态码 lqh 2021.1.30
  934. public static function groupStatusName($list)
  935. {
  936. foreach ($list as $k => $val) {
  937. $status = $val['status'] ?? 0;
  938. $list[$k]['statusName'] = self::getStatusName($status);
  939. }
  940. return $list;
  941. }
  942. // 获取状态码名称
  943. public static function getStatusName($status)
  944. {
  945. return self::$statusMap[$status] ?? '';
  946. }
  947. //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
  948. public static function statNum($shopId)
  949. {
  950. $data = [
  951. 'allNum' => 0,
  952. 'unPayNum' => 0,
  953. 'unSendNum' => 0,
  954. 'sendingNum' => 0,
  955. 'finishNum' => 0,
  956. ];
  957. $res = self::getAllList("status", ['shopId' => $shopId]);
  958. foreach ($res as $v) {
  959. $data['allNum']++;
  960. $status = $v['status'];
  961. switch ($status) {
  962. case self::PURCHASE_ORDER_STATUS_WAIT:
  963. //待支付
  964. $data['unPayNum']++;
  965. break;
  966. case self::PURCHASE_ORDER_STATUS_UN_SEND:
  967. $data['unSendNum']++;
  968. break;
  969. case self::PURCHASE_ORDER_STATUS_SENDING:
  970. $data['sendingNum']++;
  971. break;
  972. case self::PURCHASE_ORDER_STATUS_COMPLETE:
  973. $data['finishNum']++;
  974. break;
  975. }
  976. }
  977. return $data;
  978. }
  979. //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
  980. public static function statOrderInfo($shopId, $statType)
  981. {
  982. $start = '';
  983. $end = '';
  984. switch ($statType) {
  985. case "today":
  986. $start = date('Y-m-d');
  987. $end = $start;
  988. break;
  989. case "yesterday" :
  990. $start = date('Y-m-d', strtotime("-1 day"));
  991. $end = $start;
  992. break;
  993. case "seven":
  994. $start = date('Y-m-d', strtotime("-6 day"));
  995. $end = date('Y-m-d');
  996. break;
  997. case "thirty":
  998. $start = date('Y-m-d', strtotime("-29 day"));
  999. $end = date('Y-m-d');
  1000. break;
  1001. default:
  1002. }
  1003. $where = [];
  1004. $where['shopId'] = $shopId;
  1005. $where['addTime'] = ['between', [$start, $end]];
  1006. $res = self::getAllByCondition($where, null, "id,actPrice");
  1007. $totalAmount = 0;
  1008. $totalNum = count($res);
  1009. foreach ($res as $v) {
  1010. $actPrice = $v['actPrice'] ?? 0;
  1011. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  1012. }
  1013. return [
  1014. 'totalAmount' => $totalAmount,
  1015. 'totalNum' => $totalNum,
  1016. ];
  1017. }
  1018. }