PurchaseOrderClass.php 40 KB

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