PurchaseOrderClass.php 39 KB

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