PurchaseOrderClass.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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 biz\wx\classes\WxMessageClass;
  13. use bizGhs\base\classes\BaseClass;
  14. use bizGhs\book\classes\BookCustomClass;
  15. use bizGhs\book\classes\BookItemClass;
  16. use bizGhs\book\classes\BookItemCustomClass;
  17. use bizGhs\book\classes\BookItemGhsClass;
  18. use bizGhs\cg\classes\CgOrderItemClass;
  19. use bizGhs\cg\classes\CgOrderItemSendClass;
  20. use bizGhs\custom\classes\AccountMoneyClass;
  21. use bizGhs\custom\classes\CustomClass;
  22. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  23. use bizGhs\item\classes\CostChangeClass;
  24. use bizGhs\item\classes\ItemClassClass;
  25. use bizGhs\order\traits\OrderTrait;
  26. use bizGhs\shop\classes\ShopAdminClass;
  27. use bizGhs\stock\classes\OnStockRecordClass;
  28. use bizGhs\stock\classes\StockRecordClass;
  29. use bizGhs\product\classes\ProductClass;
  30. use common\components\dict;
  31. use common\components\imgUtil;
  32. use common\components\noticeUtil;
  33. use common\components\orderSn;
  34. use common\components\printUtil;
  35. use common\components\stringUtil;
  36. use common\components\util;
  37. use Yii;
  38. //采购单 lqh 2021.1.20
  39. class PurchaseOrderClass extends BaseClass
  40. {
  41. use OrderTrait;
  42. public static $baseFile = '\bizGhs\order\models\PurchaseOrder';
  43. const PURCHASE_ORDER_STATUS_WAIT = 1; //待确认
  44. const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送,待发货
  45. const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中,待入库
  46. const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已入库
  47. const PURCHASE_ORDER_STATUS_CANCEL = 5; //已取消
  48. public static $statusMap = [
  49. self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
  50. self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
  51. self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
  52. self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
  53. self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
  54. ];
  55. const DEBT_UNKNOWN = 0;
  56. const DEBT_YES = 1;
  57. const DEBT_NO = 2;
  58. //寄付与到付
  59. const YF_PAY_WAY_JF = 1;
  60. const YF_PAY_WAY_DF = 2;
  61. //稍后入库和直接入库
  62. const IN_TYPE_LATER = 0;
  63. const IN_TYPE_NOW = 1;
  64. /**
  65. * 采购挂账:买方 xhGhs 净 balance 减少,并记余额变动(合并后模型,不再写欠款字段)
  66. */
  67. public static function applyPurchaseDebtOnBuyerGhs($ghs, $amount, $purchase, $sjId, $shopId)
  68. {
  69. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  70. $ghsNewBalance = bcsub($ghs->balance ?? '0.00', $amount, 2);
  71. $ghs->debt = GhsClass::DEBT_YES;
  72. $ghs->balance = $ghsNewBalance;
  73. $ghs->debtNum += 1;
  74. $ghs->expendAmount = bcadd($ghs->expendAmount ?? '0.00', $amount, 2);
  75. $ghs->expendNum += 1;
  76. $ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
  77. $orderSn = $purchase->orderSn ?? '';
  78. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  79. GhsBalanceChangeClass::add([
  80. 'ghsId' => $ghs->id ?? 0,
  81. 'relateId' => $purchase->id ?? 0,
  82. 'ptStyle' => 2,
  83. 'capitalType' => $capitalType,
  84. 'amount' => $amount,
  85. 'balance' => $ghsNewBalance,
  86. 'staffId' => $purchase->shopAdminId ?? 0,
  87. 'staffName' => $purchase->shopAdminName ?? '',
  88. 'io' => 0,
  89. 'event' => '新增采购 ' . $orderSn,
  90. 'sjId' => $sjId,
  91. 'shopId' => $shopId,
  92. ], true);
  93. return $ghs;
  94. }
  95. /**
  96. * 采购挂账:供货方 custom 净 balance 同步减少(双边关系)
  97. */
  98. public static function applyPurchaseDebtOnSupplierCustom($ghs, $amount, $ghsMain = null)
  99. {
  100. $customId = $ghs->customId ?? 0;
  101. $custom = CustomClass::getLockById($customId);
  102. if (empty($custom)) {
  103. util::fail('没有找到客户信息');
  104. }
  105. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  106. $custom->isDebt = CustomClass::IS_DEBT_YES;
  107. $custom->balance = bcsub($custom->balance ?? '0.00', $amount, 2);
  108. if ($custom->debtNum == 0 && $ghsMain !== null) {
  109. $ghsMain->mayGatheringNum += 1;
  110. }
  111. $custom->debtNum += 1;
  112. $custom->buyNum += 1;
  113. $custom->buyAmount = bcadd($custom->buyAmount ?? '0.00', $amount, 2);
  114. $custom->save();
  115. return $custom;
  116. }
  117. /**
  118. * 采购售后退款:买方 xhGhs 净 balance 增加 + 流水;供货方 custom 同步
  119. */
  120. public static function applyPurchaseRefundOnAccounts($ghs, $amount, $refund, $order, $sjId, $shopId)
  121. {
  122. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  123. $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
  124. $ghs->balance = $ghsNewBalance;
  125. // 待结采购单售后:净 balance 增加;若该单待结金额已退尽则 debtNum 减 1
  126. $wasDebtOrder = intval($order->debt ?? 0) === self::DEBT_YES;
  127. $clearOrderDebt = $wasDebtOrder && bccomp($order->actPrice ?? '0', '0', 2) <= 0;
  128. if ($clearOrderDebt) {
  129. $ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - 1);
  130. $order->debt = self::DEBT_NO;
  131. $order->save(false, ['debt']);
  132. }
  133. $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
  134. $ghs->save(false, ['balance', 'debtNum', 'debt']);
  135. $orderSn = $order->orderSn ?? '';
  136. $refundSn = $refund->refundSn ?? '';
  137. $capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
  138. GhsBalanceChangeClass::add([
  139. 'ghsId' => $ghs->id ?? 0,
  140. 'relateId' => $refund->id ?? 0,
  141. 'ptStyle' => 2,
  142. 'capitalType' => $capitalType,
  143. 'amount' => $amount,
  144. 'balance' => $ghsNewBalance,
  145. 'io' => 1,
  146. 'staffId' => $refund->shopAdminId ?? 0,
  147. 'staffName' => $refund->shopAdminName ?? '',
  148. 'event' => '采购单 ' . $orderSn . ' 申请售后 ' . $refundSn,
  149. 'sjId' => $sjId,
  150. 'shopId' => $shopId,
  151. ], true);
  152. $customId = $ghs->customId ?? 0;
  153. if ($customId > 0) {
  154. $custom = CustomClass::getLockById($customId);
  155. if (!empty($custom)) {
  156. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  157. $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
  158. $custom->balance = $customNewBalance;
  159. if ($clearOrderDebt) {
  160. $custom->debtNum = max(0, intval($custom->debtNum ?? 0) - 1);
  161. }
  162. $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  163. $saveAttrs = ['balance', 'isDebt'];
  164. if ($clearOrderDebt) {
  165. $saveAttrs[] = 'debtNum';
  166. }
  167. $custom->save(false, $saveAttrs);
  168. }
  169. }
  170. return $ghs;
  171. }
  172. /**
  173. * 采购结账:买方 xhGhs 净 balance 增加 + 流水
  174. */
  175. public static function applyPurchaseClearOnBuyerGhs($ghs, $amount, $clear, $orderCount)
  176. {
  177. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  178. $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
  179. $ghs->balance = $ghsNewBalance;
  180. $ghs->debtNum -= $orderCount;
  181. $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
  182. $ghs->save(false, ['balance', 'debtNum', 'debt']);
  183. $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
  184. GhsBalanceChangeClass::add([
  185. 'ghsId' => $ghs->id ?? 0,
  186. 'relateId' => $clear->id ?? 0,
  187. 'ptStyle' => 2,
  188. 'capitalType' => $capitalType,
  189. 'amount' => $amount,
  190. 'balance' => $ghsNewBalance,
  191. 'staffId' => $clear->customShopAdminId ?? 0,
  192. 'staffName' => $clear->customShopAdminName ?? '',
  193. 'io' => 1,
  194. 'event' => '采购单结账 ' . ($clear->orderSn ?? ''),
  195. 'sjId' => $clear->sjId ?? 0,
  196. 'shopId' => $clear->shopId ?? 0,
  197. ], true);
  198. return $ghs;
  199. }
  200. /**
  201. * 采购结账:供货方 custom 净 balance 同步增加
  202. */
  203. public static function applyPurchaseClearOnSupplierCustom($ghs, $amount, $orderCount, $ghsMain = null)
  204. {
  205. $customId = $ghs->customId ?? 0;
  206. $custom = CustomClass::getLockById($customId);
  207. if (empty($custom)) {
  208. util::fail('没有找到客户信息');
  209. }
  210. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  211. $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
  212. $custom->balance = $customNewBalance;
  213. $custom->debtNum -= $orderCount;
  214. $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  215. $custom->save(false, ['balance', 'debtNum', 'isDebt']);
  216. if ($custom->debtNum == 0 && $ghsMain !== null) {
  217. $ghsMain->mayGatheringNum -= 1;
  218. $ghsMain->save();
  219. }
  220. return $custom;
  221. }
  222. public static function assign($cg, $shop, $params)
  223. {
  224. $bookSn = $cg->bookSn ?? 0;
  225. $orderSn = $cg->orderSn ?? '';
  226. $cgId = $cg->id ?? 0;
  227. $mainId = $cg->mainId ?? 0;
  228. $ghsId = $cg->ghsId ?? 0;
  229. $ghsName = $cg->ghsName ?? '';
  230. $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  231. if (empty($cgItemList)) {
  232. return [];
  233. }
  234. $ids = [];
  235. foreach ($cgItemList as $cgItem) {
  236. $ids[] = $cgItem->productId;
  237. }
  238. $productInfoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  239. $hasChange = 0;
  240. foreach ($cgItemList as $cgItem) {
  241. $cgItemId = $cgItem->id ?? 0;
  242. $cgItemNum = $cgItem->itemNum ?? 0;
  243. $productId = $cgItem->productId ?? 0;
  244. $ptItemId = $cgItem->itemId ?? 0;
  245. $assignSeat = $cgItem->assignSeat;
  246. $noAssignHasDelNeedCg = $cgItem->noAssignHasDelNeedCg;
  247. //不需要分配货位号的花材,已经减待采数
  248. if ($assignSeat == 0 && $noAssignHasDelNeedCg == 1) {
  249. continue;
  250. }
  251. if ($cgItemNum <= 0) {
  252. continue;
  253. }
  254. $currentItemInfo = $productInfoList[$productId] ?? null;
  255. if (empty($currentItemInfo)) {
  256. util::fail('花材信息缺失');
  257. }
  258. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
  259. if (empty($bookItemRes)) {
  260. //没有预订花材则不需要分配货位
  261. continue;
  262. }
  263. $bookItemCustomList = BookItemCustomClass::getAllByCondition(['bookSn' => $bookSn, 'itemId' => $productId], null, '*', null, true);
  264. if (empty($bookItemCustomList)) {
  265. //没有预订的花材和客户则不需要分配货位
  266. continue;
  267. }
  268. $customIds = [];
  269. foreach ($bookItemCustomList as $bookItemCustomRes) {
  270. $customIds[] = $bookItemCustomRes->customId;
  271. }
  272. $customList = CustomClass::getAllByCondition(['id' => ['in', $customIds]], null, '*', 'id', true);
  273. foreach ($bookItemCustomList as $bookItemCustomRes) {
  274. $customId = $bookItemCustomRes->customId ?? 0;
  275. $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
  276. if (empty($bookCustomRes)) {
  277. noticeUtil::push("采购单 {$orderSn} 分配货位时,发现 bookCustom 数据有问题", '15280215347');
  278. util::fail("bookCustom 数据有问题");
  279. }
  280. $confirmSendNum = $cgItem->confirmSendNum ?? 0;
  281. $remainCouldSendNum = bcsub($cgItemNum, $confirmSendNum, 2);
  282. if ($remainCouldSendNum <= 0) {
  283. //剩余应分配数量不足则退出本循环
  284. break;
  285. }
  286. $needCgNum = $bookItemCustomRes->needCgNum ?? 0;
  287. if ($needCgNum <= 0) {
  288. //此客户花材,没有要采购的数量,则找下一个
  289. continue;
  290. }
  291. if ($needCgNum >= $remainCouldSendNum) {
  292. $remainNeedSendNum = $remainCouldSendNum;
  293. } else {
  294. $remainNeedSendNum = $needCgNum;
  295. }
  296. if ($remainNeedSendNum <= 0) {
  297. //剩余要分配数量不足则退出本循环
  298. break;
  299. }
  300. //待采数量减少
  301. $params['currentChangeType'] = 'addCg';
  302. $params['customName'] = $bookItemCustomRes->customName ?? '';
  303. $params['customId'] = $bookItemCustomRes->customId ?? '';
  304. $params['customNamePy'] = $bookItemCustomRes->customNamePy ?? '';
  305. if ($assignSeat == 1) {
  306. $bookItemCustomRes = BookItemCustomClass::delNeedCgNum($bookItemCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  307. $bookCustomRes = BookCustomClass::delNeedCgNum($bookCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  308. }
  309. $bookItemRes = BookItemClass::delNeedCgNum($bookItemRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  310. $hasChange = 1;
  311. if ($assignSeat == 1) {
  312. //路上数量增加
  313. $bookItemCustomRes = BookItemCustomClass::addRoadNum($bookItemCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  314. $bookCustomRes = BookCustomClass::addRoadNum($bookCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  315. $bookItemRes = BookItemClass::addRoadNum($bookItemRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  316. //确认已分配数量增加
  317. $cgItem->confirmSendNum = bcadd($cgItem->confirmSendNum, $remainNeedSendNum);
  318. $cgItem->save();
  319. $sendInfo = CgOrderItemSendClass::getByCondition(['parentId' => $cgItemId, 'customId' => $customId, 'itemId' => $productId], true);
  320. $bookItemCustomId = $bookItemCustomRes->id ?? 0;
  321. $currentCustom = $customList[$customId] ?? null;
  322. if (empty($currentCustom)) {
  323. util::fail('客户信息缺失,编号545');
  324. }
  325. if (empty($sendInfo)) {
  326. $sendData = [
  327. 'mainId' => $mainId,
  328. 'parentOrderId' => $cgId,
  329. 'parentId' => $cgItemId,
  330. 'bookGhsGiveId' => 0,
  331. 'name' => $currentItemInfo->name ?? '',
  332. 'py' => $currentItemInfo->py ?? '',
  333. 'itemId' => $productId,
  334. 'ptItemId' => $ptItemId,
  335. 'customId' => $customId,
  336. 'customName' => $currentCustom->name ?? '',
  337. 'customPy' => $currentCustom->py ?? '',
  338. 'seatSn' => $currentCustom->seatSn ?? 0,
  339. 'num' => 0,
  340. 'bookItemCustomId' => $bookItemCustomId,
  341. 'status' => 0,
  342. 'staffId' => $currentItemInfo->cgStaffId ?? 0,
  343. 'staffName' => $currentItemInfo->cgStaffName ?? '',
  344. ];
  345. $sendInfo = CgOrderItemSendClass::add($sendData, true);
  346. }
  347. $sendInfo->num = bcadd($sendInfo->num, $remainNeedSendNum);
  348. $sendInfo->save();
  349. $sendInfoId = $sendInfo->id ?? 0;
  350. $giveGhs = BookItemGhsClass::getByCondition(['parentId' => $bookItemCustomId, 'ghsId' => $ghsId, 'itemId' => $productId,], true);
  351. if (empty($giveGhs)) {
  352. $giveGhsData = [
  353. 'mainId' => $mainId,
  354. 'bookSn' => $bookSn,
  355. 'parentId' => $bookItemCustomId,
  356. 'cgSendId' => $sendInfoId,
  357. 'name' => $currentItemInfo->name ?? '',
  358. 'py' => $currentItemInfo->py ?? '',
  359. 'itemId' => $productId,
  360. 'ptItemId' => $ptItemId,
  361. 'ghsId' => $ghsId,
  362. 'ghsName' => $ghsName,
  363. 'cgId' => $cgId,
  364. 'cgItemId' => $cgItemId,
  365. 'customId' => $customId,
  366. 'customName' => $currentCustom->name ?? '',
  367. 'staffId' => $currentItemInfo->cgStaffId ?? 0,
  368. 'staffName' => $currentItemInfo->cgStaffName ?? '',
  369. 'status' => 0,
  370. ];
  371. $giveGhs = BookItemGhsClass::add($giveGhsData, true);
  372. }
  373. $giveGhs->num = bcadd($giveGhs->num, $remainNeedSendNum);
  374. $giveGhs->save();
  375. $giveGhsId = $giveGhs->id ?? 0;
  376. $sendInfo->bookGhsGiveId = $giveGhsId;
  377. $sendInfo->save();
  378. } else {
  379. $cgItem->noAssignHasDelNeedCg = 1;
  380. $cgItem->save();
  381. break;
  382. }
  383. }
  384. }
  385. return ['hasChange' => $hasChange];
  386. }
  387. //取消采购入库 ssh 20231218
  388. public static function cancel($cg, $shop, $params)
  389. {
  390. $cg->status = self::PURCHASE_ORDER_STATUS_CANCEL;
  391. $cg->save();
  392. $orderSn = $cg->orderSn ?? '';
  393. $mainId = $cg->mainId ?? 0;
  394. $sjId = $cg->sjId ?? 0;
  395. $shopId = $cg->shopId ?? 0;
  396. $ghsName = $cg->ghsName ?? '';
  397. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  398. if (empty($itemList)) {
  399. util::fail('没有找到花材');
  400. }
  401. foreach ($itemList as $cgKey => $cgItem) {
  402. $productId = $cgItem['productId'] ?? 0;
  403. $currentNum = $cgItem['itemNum'] ?? 0;
  404. $ptItemId = $cgItem['itemId'] ?? 0;
  405. $presell = $cgItem['presell'] ?? 0;
  406. $product = ProductClass::getLockById($productId);
  407. if (empty($product)) {
  408. util::fail('没有找到花材哦');
  409. }
  410. if ($presell == 0) {
  411. //减少路上库存
  412. $onRecord = [];
  413. $onRecord['sjId'] = $sjId;
  414. $onRecord['shopId'] = $shopId;
  415. $onRecord['mainId'] = $mainId;
  416. $onRecord['orderSn'] = $orderSn;
  417. $onRecord['productId'] = $productId;
  418. $onRecord['itemId'] = $ptItemId;
  419. $onRecord['relateName'] = $ghsName;
  420. $onRecord['itemNum'] = $currentNum;
  421. $oldOnStock = $product->onStock ?? 0;
  422. $onRecord['oldStock'] = $oldOnStock;
  423. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  424. $onRecord['ptStyle'] = 2;
  425. OnStockRecordClass::cancelCg($onRecord);
  426. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  427. }
  428. }
  429. $bookSn = $cg->bookSn ?? 0;
  430. if ($shop->pfLevel == 1 && !empty($shop->bookSn)) {
  431. if ($shop->bookSn == $bookSn) {
  432. //取消分配的货位
  433. $key = 'ghs_cg_order_scan_seat_' . $cg->id;
  434. $noLock = util::lock($key);
  435. if ($noLock) {
  436. CgOrderItemClass::cancelAssign($cg, $shop, $params);
  437. util::unlock($key);
  438. } else {
  439. util::fail('正在分配货位,请1秒后再取消');
  440. }
  441. }
  442. }
  443. }
  444. public static function printGroup($current, $content)
  445. {
  446. //58mm的机器,一行打印16个汉字,32个字母
  447. $bigPrice = $current['bigPrice'] ?? '';
  448. $bigPrice = floatval($bigPrice);
  449. $bigUnit = $current['bigUnit'] ?? '扎';
  450. $aboutPrice = $current['aboutPrice'] ?? 0;
  451. $aboutString = $aboutPrice == 1 ? '约' : '';
  452. $name = $current['name'] ?? '';
  453. if (floatval($bigPrice) == 0.01) {
  454. $name = $name . '(送)';
  455. }
  456. $count = $current['itemNum'] ?? '';
  457. $count = floatval($count);
  458. $content .= '<B>' . $name . ' ' . $count . $bigUnit . '</B><BR>';
  459. $totalPrice = $current['totalPrice'] ?? 0;
  460. $content .= $count . $bigUnit . 'x' . $aboutString . $bigPrice . '元' . '=' . $totalPrice . '元<BR>';
  461. if (isset($current['refundNum']) && $current['refundNum'] > 0) {
  462. $content .= '<B>已退' . $current['refundNum'] . $bigUnit . '</B><BR>';
  463. }
  464. $content .= '--------------------------------<BR>';
  465. return $content;
  466. }
  467. public static function addPrintNum($cg, $num = 1)
  468. {
  469. $cg->printNum += $num;
  470. $cg->save();
  471. if ($cg->printNum > 1) {
  472. $mainId = $cg->mainId ?? 0;
  473. if (getenv('YII_ENV') == 'production') {
  474. //小向花卉采购单第二次打通要通知老板
  475. if ($mainId == 23390) {
  476. WxMessageClass::ghsCgRepeatPrintInform($cg);
  477. }
  478. if ($mainId == 50) {
  479. //WxMessageClass::ghsCgRepeatPrintInform($cg);
  480. }
  481. } else {
  482. if ($mainId == 644) {
  483. //WxMessageClass::ghsCgRepeatPrintInform($cg);
  484. }
  485. }
  486. }
  487. return $cg;
  488. }
  489. //打印小票 ssh 20230608
  490. public static function printTicket($orderInfo, $firstPrint = true, $order = 1)
  491. {
  492. $ghsId = $orderInfo->ghsId ?? 0;
  493. $debtCount = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES]);
  494. $debtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  495. $orderSn = $orderInfo->orderSn ?? '';
  496. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  497. $shopId = $orderInfo->shopId ?? 0;
  498. $mainId = $orderInfo->mainId ?? 0;
  499. $shop = ShopClass::getById($shopId, true);
  500. $telephone = $shop->telephone ?? '';
  501. $sjId = $shop->sjId ?? 0;
  502. $sj = SjClass::getById($sjId, true);
  503. $sjName = $sj->name ?? '';
  504. $shopName = $shop->shopName ?? '';
  505. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  506. if (getenv('YII_ENV') == 'production') {
  507. //小向的采购不允许打小票
  508. if (in_array($mainId, [23390])) {
  509. return true;
  510. }
  511. }
  512. $content = "<CB>" . $currentName . "</CB>";
  513. $content .= "<CB>采购单</CB>";
  514. $content .= '--------------------------------<BR>';
  515. $content .= '<C>供货商</C><BR>';
  516. $content .= '<CB>' . $orderInfo->ghsName . '</CB><BR>';
  517. $shortOrderSn = substr($orderSn, -4);
  518. $content .= '<CB>核销码:' . $shortOrderSn . '</CB><BR>';
  519. $seatSn = $orderInfo->seatSn ?? '';
  520. if (!empty($seatSn)) {
  521. $content .= '<CB>货位号:' . $seatSn . '</CB><BR>';
  522. }
  523. $currentTime = date("Y-m-d H:i", strtotime($orderInfo->entryTime));
  524. $content .= '<C>' . $currentTime . '</C><BR>';
  525. if (isset($orderInfo->remark) && !empty($orderInfo->remark)) {
  526. $content .= '<BR>';
  527. $content .= '备注:<BR>';
  528. $content .= '<B>' . $orderInfo->remark . '</B><BR>';
  529. }
  530. $content .= '<BR>';
  531. $content .= '商品 数量/单价 金额 <BR>';
  532. $content .= '--------------------------------<BR>';
  533. if (isset($itemList) && !empty($itemList)) {
  534. foreach ($itemList as $current) {
  535. $content = self::printGroup($current, $content);
  536. }
  537. }
  538. $kindNum = $orderInfo->kind ?? 0;
  539. $bigNum = $orderInfo->itemNum ? floatval($orderInfo->itemNum) : 0;
  540. if (!empty($itemList)) {
  541. $kindNum = 0;
  542. $bigNum = 0;
  543. foreach ($itemList as $row) {
  544. $itemNumRow = floatval($row['itemNum'] ?? 0);
  545. $refundNumRow = floatval($row['refundNum'] ?? 0);
  546. $netNum = max(0, $itemNumRow - $refundNumRow);
  547. if ($netNum > 0) {
  548. $kindNum++;
  549. $bigNum += $netNum;
  550. }
  551. }
  552. }
  553. $content .= '<BR>********************************<BR>';
  554. $content .= "<CB>共{$kindNum}种";
  555. $content .= " 数量:";
  556. if ($bigNum > 0) {
  557. $content .= "{$bigNum}";
  558. }
  559. $content .= "</CB>";
  560. $content .= '********************************<BR><BR>';
  561. if (isset($orderInfo['itemPrice']) && $orderInfo['itemPrice'] > 0) {
  562. $content .= '商品金额:' . floatval($orderInfo['itemPrice']) . '元<BR>';
  563. }
  564. if (isset($orderInfo['sendCost']) && $orderInfo['sendCost'] > 0) {
  565. $content .= ' 运费:' . floatval($orderInfo['sendCost']) . '元<BR>';
  566. }
  567. if (isset($orderInfo['packCost']) && $orderInfo['packCost'] > 0) {
  568. $content .= ' 打包费:' . floatval($orderInfo['packCost']) . '元<BR>';
  569. }
  570. $content .= '合计金额:' . floatval($orderInfo['prePrice']) . '元<BR>';
  571. if (isset($orderInfo['discountAmount']) && !empty($orderInfo['discountAmount']) && $orderInfo['discountAmount'] > 0) {
  572. $content .= '优惠扣除:-' . floatval($orderInfo['discountAmount']) . '元<BR>';
  573. }
  574. if (isset($orderInfo['tkPrice']) && $orderInfo['tkPrice'] > 0) {
  575. $content .= '退款金额:' . floatval($orderInfo['tkPrice']) . '元<BR>';
  576. }
  577. $content .= '<B>订单金额:' . floatval($orderInfo['realPrice']) . '</B><BR>';
  578. if (isset($orderInfo['debt']) && $orderInfo['debt'] == 1) {
  579. $content .= '实付金额:0元<BR>';
  580. } else {
  581. $content .= '实付金额:' . floatval($orderInfo['realPrice']) . '元<BR>';
  582. }
  583. // if ($debtAmount > 0) {
  584. // $content .= '累计待结:' . $debtCount . '笔 ' . floatval($debtAmount) . '元<BR>';
  585. // }
  586. $content .= '--------------------------------<BR>';
  587. $content .= '订单编号:' . $orderInfo['orderSn'] . '<BR>';
  588. if (!empty($telephone)) {
  589. $content .= '联系电话:' . $telephone . '<BR>';
  590. }
  591. if (isset($orderInfo['shopAdminName']) && !empty($orderInfo['shopAdminName'])) {
  592. $content .= '录入人员:' . $orderInfo['shopAdminName'] . '<BR>';
  593. }
  594. $content .= "<BR>";
  595. $bookSn = $shop->bookSn ?? 0;
  596. if (!empty($bookSn)) {
  597. $content .= "<CB>扫码查货位号</CB>";
  598. $content .= "<BR>";
  599. $host = Yii::$app->params['ghsHost'];
  600. $orderSn = $orderInfo->orderSn ?? '';
  601. $salt = $orderInfo->salt ?? '';
  602. $content .= "<QR>" . $host . "/#/pagesPurchase/info?orderSn=" . $orderSn . "&salt=" . $salt . "</QR>";
  603. }
  604. $content .= "<BR>";
  605. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  606. $printSn = $ext['wcPrintSn'] ?? '';
  607. if ($firstPrint) {
  608. if (isset($orderInfo->fistWcPrintNum) && $orderInfo->fistWcPrintNum > 1) {
  609. if ($orderInfo->fistWcPrintNum == 2) {
  610. $printSn = $ext['wcPrintSn2'] ?? '';
  611. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  612. }
  613. if ($orderInfo->fistWcPrintNum == 3) {
  614. $printSn = $ext['wcPrintSn3'] ?? '';
  615. //noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  616. }
  617. }
  618. } else {
  619. if ($order > 1) {
  620. if ($order == 2) {
  621. $printSn = $ext['wcPrintSn2'] ?? '';
  622. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  623. }
  624. if ($order == 3) {
  625. $printSn = $ext['wcPrintSn3'] ?? '';
  626. //noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  627. }
  628. }
  629. }
  630. if (!empty($printSn)) {
  631. self::addPrintNum($orderInfo);
  632. $p = new printUtil($printSn);
  633. $p->printMsg($content, $shop);
  634. }
  635. }
  636. public static function putIn($cg, $data)
  637. {
  638. if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
  639. util::fail('已经入过库了');
  640. }
  641. if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
  642. util::fail('已取消');
  643. }
  644. $orderSn = $cg->orderSn ?? '';
  645. $mainId = $cg->mainId ?? 0;
  646. $sjId = $cg->sjId ?? 0;
  647. $shopId = $cg->shopId ?? 0;
  648. $staffId = $data['staffId'] ?? 0;
  649. $staffName = $data['staffName'] ?? '';
  650. $adminId = $data['adminId'] ?? 0;
  651. $shop = ShopClass::getLockById($shopId);
  652. if (empty($shop)) {
  653. util::fail('没有找到门店26');
  654. }
  655. $ghsName = $cg->ghsName ?? '';
  656. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  657. if (empty($itemList)) {
  658. util::fail('没有找到花材');
  659. }
  660. $bookSn = $shop->bookSn ?? 0;
  661. $pfLevel = $shop->pfLevel ?? 0;
  662. if ($pfLevel == 1 && !empty($bookSn)) {
  663. if ($cg->bookSn == $bookSn) {
  664. //入库前再确认分配货位
  665. $key = 'ghs_cg_order_scan_seat_' . $cg->id;
  666. $noLock = util::lock($key);
  667. if ($noLock) {
  668. //可以获得锁,没有在分配货位,可以进行货位分配
  669. $params = ['staffId' => $staffId, 'staffName' => $staffName];
  670. self::assign($cg, $shop, $params);
  671. util::unlock($key);
  672. //分配货位后上架和入库
  673. $shop = $data['shop'] ?? null;
  674. CgOrderItemClass::assignToPutOn($cg, $shop, $params);
  675. } else {
  676. util::fail('正在分配货位,请1秒后再提交');
  677. }
  678. }
  679. }
  680. foreach ($itemList as $cgKey => $cgItem) {
  681. $productId = $cgItem['productId'] ?? 0;
  682. $currentNum = $cgItem['itemNum'] ?? 0;
  683. $ptItemId = $cgItem['itemId'] ?? 0;
  684. $unitCost = $cgItem['cost'] ?? 0;
  685. $presell = $cgItem['presell'] ?? 0;
  686. $currentCost = $cgItem['cost'] ?? 0;
  687. $product = ProductClass::getLockById($productId);
  688. if (empty($product)) {
  689. util::fail('没有找到花材哦');
  690. }
  691. //有新入库且 limitBuy=0,则清空限购记录缓存
  692. if ($product->limitBuy == 0) {
  693. ProductClass::clearLimitBuyCache($productId);
  694. \bizHd\product\classes\ProductClass::clearLimitBuyCache($productId);
  695. }
  696. //库存为0时重置特价
  697. if (intval($product->stock) == 0) {
  698. $product->discountPrice = 0;
  699. $product->skDiscountPrice = 0;
  700. $product->hjDiscountPrice = 0;
  701. }
  702. if ($presell == 0) {
  703. //加库存和流水记录
  704. $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum, ['cost' => $currentCost]);
  705. $record = [];
  706. $record['sjId'] = $sjId;
  707. $record['shopId'] = $shopId;
  708. $record['mainId'] = $mainId;
  709. $record['orderSn'] = $orderSn;
  710. $record['productId'] = $productId;
  711. $record['itemId'] = $ptItemId;
  712. $record['relateName'] = $ghsName;
  713. $record['itemNum'] = $currentNum;
  714. $record['oldStock'] = $stockInfo['oldStock'];
  715. $record['newStock'] = $stockInfo['newStock'];
  716. $record['totalCost'] = $stockInfo['totalCost'] ?? 0;
  717. $record['totalStock'] = $stockInfo['totalStock'] ?? 0;
  718. $record['avCost'] = $stockInfo['avCost'] ?? 0;
  719. $record['changeCost'] = $stockInfo['changeCost'] ?? 0;
  720. $record['unitCost'] = $stockInfo['unitCost'] ?? 0;
  721. StockRecordClass::addPurchaseOrderRecord($record);
  722. //减少路上库存
  723. $onRecord = [];
  724. $onRecord['sjId'] = $sjId;
  725. $onRecord['shopId'] = $shopId;
  726. $onRecord['mainId'] = $mainId;
  727. $onRecord['orderSn'] = $orderSn;
  728. $onRecord['productId'] = $productId;
  729. $onRecord['itemId'] = $ptItemId;
  730. $onRecord['relateName'] = $ghsName;
  731. $onRecord['itemNum'] = $currentNum;
  732. $oldOnStock = $product->onStock ?? 0;
  733. $onRecord['oldStock'] = $oldOnStock;
  734. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  735. $onRecord['ptStyle'] = 2;
  736. OnStockRecordClass::addInStockOrderRecord($onRecord);
  737. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  738. }
  739. $product->cost = $unitCost;
  740. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  741. $product->save();
  742. //成本变动记录
  743. $changeData = [
  744. 'ptStyle' => 2,
  745. 'sjId' => $sjId,
  746. 'mainId' => $mainId,
  747. 'itemId' => $productId,
  748. 'ptItemId' => $ptItemId,
  749. 'cost' => $unitCost,
  750. 'staffId' => $staffId,
  751. 'staffName' => $staffName,
  752. 'name' => $product->name ?? '',
  753. 'cover' => $product->cover ?? '',
  754. 'targetId' => $order->id ?? 0,
  755. 'event' => '采购',
  756. ];
  757. CostChangeClass::addData($changeData);
  758. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  759. //直营门店的成本价也要一起改掉
  760. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  761. if (!empty($allShop)) {
  762. $currentItemId = $product->itemId ?? 0;
  763. foreach ($allShop as $currentShop) {
  764. $currentId = $currentShop->mainId ?? 0;
  765. if ($currentId == $mainId) {
  766. continue;
  767. }
  768. if (isset($currentShop->join) && $currentShop->join == 1) {
  769. continue;
  770. }
  771. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  772. if (!empty($currentProduct)) {
  773. $currentProduct->cost = $unitCost;
  774. $currentProduct->save();
  775. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  776. //成本变动记录
  777. $changeData = [
  778. 'ptStyle' => 2,
  779. 'sjId' => $currentProduct->sjId ?? 0,
  780. 'mainId' => $currentProduct->mainId ?? 0,
  781. 'itemId' => $currentProduct->id ?? 0,
  782. 'ptItemId' => $currentProduct->itemId ?? 0,
  783. 'cost' => $unitCost,
  784. 'staffId' => $currentStaff->id ?? 0,
  785. 'staffName' => $currentStaff->name ?? '',
  786. 'name' => $currentProduct->name ?? '',
  787. 'cover' => $currentProduct->cover ?? '',
  788. 'targetId' => $order->id ?? 0,
  789. 'event' => $shop->shopName . '采购',
  790. ];
  791. CostChangeClass::addData($changeData);
  792. }
  793. }
  794. }
  795. }
  796. }
  797. //门店支出增加
  798. $currentPrice = $cg->actPrice ?? 0;
  799. $mainId = $shop->mainId ?? 0;
  800. $main = MainClass::getLockById($mainId);
  801. if (empty($main)) {
  802. util::fail('没有资产信息');
  803. }
  804. //支出增加
  805. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  806. $main->totalExpend = $currentTotalExpend;
  807. $main->save();
  808. //采购增加
  809. $main->cgFinish += 1;
  810. $main->totalPurchaseOrder += 1;
  811. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  812. $main->totalPurchase = $currentTotalPurchase;
  813. $main->save();
  814. //供货商资产增加
  815. $ghsId = $cg->ghsId ?? 0;
  816. $ghs = GhsClass::getLockById($ghsId);
  817. if (empty($ghs)) {
  818. util::fail('没有找到供货商');
  819. }
  820. self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $cg, $sjId, $shopId);
  821. //采购统计
  822. $cgNum = $cg->itemNum ?? 0;
  823. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  824. //采购按供货商统计
  825. StatCgGhsClass::ghsReplace($cg);
  826. //门店应收客户款增加
  827. $ghsShopId = $ghs->shopId;
  828. $ghsShop = ShopClass::getLockById($ghsShopId);
  829. if (empty($ghsShop)) {
  830. util::fail('没有找到门店27');
  831. }
  832. $ghsMainId = $ghsShop->mainId ?? 0;
  833. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  834. if (empty($ghsMain)) {
  835. util::fail('没有main信息22');
  836. }
  837. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  838. $ghsMain->mayGathering = $currentMayGathering;
  839. self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
  840. $ghsMain->save();
  841. //当天和当月支出统计
  842. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  843. //支出流水
  844. $payWay = dict::getDict('payWay', 'unknown');
  845. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  846. $event = '采购';
  847. $mainId = $shop->mainId ?? 0;
  848. $capitalData = [
  849. 'capitalType' => $capitalType,
  850. 'io' => 0,
  851. 'totalExpend' => $currentTotalExpend,
  852. 'payWay' => $payWay,
  853. 'amount' => $currentPrice,
  854. 'sjId' => $sjId,
  855. 'shopId' => $shopId,
  856. 'event' => $event,
  857. 'mainId' => $mainId
  858. ];
  859. ShopCapitalClass::addCapital($capitalData);
  860. $status = self::PURCHASE_ORDER_STATUS_COMPLETE;
  861. $cg->debt = self::DEBT_YES;
  862. $cg->status = $status;
  863. $update = ['status' => $status];
  864. if (isset($data['entryTime']) && !empty($data['entryTime'])) {
  865. $cg->entryTime = $data['entryTime'];
  866. $update['entryTime'] = $data['entryTime'];
  867. }
  868. PurchaseOrderItemClass::updateByCondition(['orderSn' => $orderSn], $update);
  869. $cg->save();
  870. return $cg;
  871. }
  872. //添加采购单 lqh 2021.1.20
  873. public static function addOrder($data)
  874. {
  875. $sjId = $data['sjId'] ?? 0;
  876. $shopId = $data['shopId'] ?? 0;
  877. $mainId = $data['mainId'] ?? 0;
  878. $ghsName = $data['ghsName'] ?? '';
  879. $staffId = $data['staffId'] ?? 0;
  880. $staffName = $data['staffName'] ?? '';
  881. $inType = $data['inType'] ?? self::IN_TYPE_NOW;
  882. $adminId = $data['adminId'] ?? 0;
  883. $orderSn = orderSn::getGhsPurchaseSn();
  884. $data['orderSn'] = $orderSn;
  885. $checkCode = substr($orderSn, -4);
  886. $data['checkCode'] = $checkCode;
  887. $status = $inType == self::IN_TYPE_NOW ? self::PURCHASE_ORDER_STATUS_COMPLETE : self::PURCHASE_ORDER_STATUS_SENDING;
  888. $data['status'] = $status;
  889. //数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
  890. $ghsItemInfo = $data['itemInfo'];
  891. $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
  892. //总共多少扎
  893. $totalBigNum = 0;
  894. //总共多少支
  895. $totalSmallNum = 0;
  896. //总数量
  897. $totalNum = 0;
  898. $kindNum = 0;
  899. $totalItemPrice = 0;
  900. $totalWeight = 0;
  901. $data['confirm'] = 1;
  902. if (getenv('YII_ENV') == 'production') {
  903. if (in_array($mainId, [23390])) {
  904. $data['confirm'] = 0;
  905. }
  906. } else {
  907. if (in_array($mainId, [644])) {
  908. $data['confirm'] = 0;
  909. }
  910. }
  911. $productIds = array_column($ghsItemInfo, 'productId');
  912. $productIds = array_unique(array_filter($productIds));
  913. $productList = ProductClass::getByIds($productIds, null, 'id');
  914. foreach ($ghsItemInfo as $v) {
  915. $ratioType = $v['ratioType'] ?? 1;
  916. $ratio = $v['ratio'] ?? 1;
  917. $currentProductId = $v['productId'] ?? 0;
  918. $currentTotal = $v['totalPrice'] ?? 0;
  919. $currentAboutPrice = $v['aboutPrice'] ?? 0;
  920. $unitWeight = $productList[$currentProductId]['weight'] ?? 0;
  921. if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
  922. util::fail('请填写价格');
  923. }
  924. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  925. $thisBigNum = $v['bigNum'];
  926. $totalBigNum = bcadd($totalBigNum, $thisBigNum, 2);
  927. $totalNum = bcadd($totalNum, $thisBigNum, 2);
  928. if ($currentAboutPrice == 1) {
  929. if ($currentTotal <= 0) {
  930. util::fail('请填写总金额');
  931. }
  932. $currentTotalPrice = $currentTotal;
  933. } else {
  934. $currentTotalPrice = bcmul($thisBigNum, $v['itemPrice'], 2);
  935. }
  936. $weight = bcmul($unitWeight, $thisBigNum, 2);
  937. } else {
  938. if (isset($v['smallNum']) == false || $v['smallNum'] <= 0) {
  939. util::fail('请填写数量');
  940. }
  941. if ($ratioType == 1) {
  942. //util::fail('小单位采购,单位比必须固定');
  943. }
  944. if ($ratio <= 1) {
  945. //util::fail('小单位采购,单位比必须大于1');
  946. }
  947. $thisSmallNum = $v['smallNum'];
  948. $changeToBigNum = bcdiv($thisSmallNum, $ratio, 2);
  949. $totalNum = bcadd($totalNum, $changeToBigNum, 2);
  950. $totalSmallNum = bcadd($totalSmallNum, $thisSmallNum, 2);
  951. if ($currentAboutPrice == 1) {
  952. if ($currentTotal <= 0) {
  953. util::fail('请填写总金额');
  954. }
  955. $currentTotalPrice = $currentTotal;
  956. } else {
  957. $currentTotalPrice = bcmul($thisSmallNum, $v['itemPrice'], 2);
  958. }
  959. $weight = bcmul($unitWeight, $changeToBigNum, 2);
  960. }
  961. $kindNum++;
  962. $totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
  963. $totalWeight = bcadd($totalWeight, $weight, 2);
  964. }
  965. $data['bigNum'] = $totalBigNum;
  966. $data['smallNum'] = $totalSmallNum;
  967. $data['itemNum'] = $totalNum;
  968. $data['kind'] = $kindNum;
  969. $data['price'] = $totalItemPrice;
  970. $data['totalWeight'] = $totalWeight;
  971. //计算订单总价
  972. $data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
  973. $data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
  974. $data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
  975. $data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
  976. $data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
  977. $cgModel = $data['cgModel'] ?? 0;
  978. if ($cgModel == 1) {
  979. //简约采购模式
  980. $customAvgKgWeight = $data['avgKgWeight'] ?? 0;
  981. $data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
  982. unset($data['modifyPrice']);
  983. } else {
  984. //正常采购模式不要使用传过来的每公斤运费
  985. unset($data['avgKgWeight']);
  986. }
  987. $data['entryTime'] = isset($data['entryTime']) && !empty($data['entryTime']) ? $data['entryTime'] . ' 12:00:00' : date('Y-m-d H:i:s');
  988. $packingCharge = $data['packingCharge'] ?? 0;
  989. $shortCharge = $data['shortCharge'] ?? 0;
  990. $longCharge = $data['longCharge'] ?? 0;
  991. $pickCharge = $data['pickCharge'] ?? 0;
  992. $localCharge = $data['localCharge'] ?? 0;
  993. //提货费和本地运费
  994. $paidPrice = bcadd($localCharge, $pickCharge, 2);
  995. $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
  996. //打包费
  997. $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
  998. //寄付才算长途和短途运费
  999. if ($yfPayWay == self::YF_PAY_WAY_JF) {
  1000. $currentPrice = bcadd($currentPrice, $shortCharge, 2);
  1001. $currentPrice = bcadd($currentPrice, $longCharge, 2);
  1002. } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
  1003. $paidPrice = bcadd($paidPrice, $shortCharge, 2);
  1004. $paidPrice = bcadd($paidPrice, $longCharge, 2);
  1005. } else {
  1006. util::fail('没有找到这个运费支付方式');
  1007. }
  1008. // $paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  1009. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  1010. $data['prePrice'] = $currentPrice;
  1011. if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
  1012. //报损减免后金额
  1013. $modifyPrice = $data['modifyPrice'] ?? 0;
  1014. if ($modifyPrice > $currentPrice) {
  1015. util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
  1016. }
  1017. if ($modifyPrice < $currentPrice) {
  1018. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  1019. $data['discountType'] = dict::getDict('discountType', 'discount');
  1020. $currentPrice = $modifyPrice;
  1021. }
  1022. }
  1023. $data['orderPrice'] = $currentPrice;
  1024. $data['actPrice'] = $currentPrice;
  1025. $data['realPrice'] = $currentPrice;
  1026. $data['paidPrice'] = $paidPrice;
  1027. //总运费
  1028. $totalFreight = bcadd($packingCharge, $shortCharge, 2);
  1029. $totalFreight = bcadd($totalFreight, $longCharge, 2);
  1030. $totalFreight = bcadd($totalFreight, $pickCharge, 2);
  1031. $totalFreight = bcadd($totalFreight, $localCharge, 2);
  1032. $data['totalFreight'] = $totalFreight;
  1033. $connection = Yii::$app->db;
  1034. $transaction = $connection->beginTransaction();
  1035. try {
  1036. //总运费/总公斤数,计算每公斤的运费
  1037. if ($totalWeight > 0) {
  1038. $avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
  1039. } else {
  1040. $avgKgWeight = 0;
  1041. }
  1042. $avgKgWeight = round($avgKgWeight, 2);
  1043. $data['avgKgWeight'] = $avgKgWeight;
  1044. $data['salt'] = stringUtil::charsShuffleLowerCase(8);
  1045. $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
  1046. $order = self::add($data, true);
  1047. $shop = ShopClass::getLockById($shopId);
  1048. if (empty($shop)) {
  1049. util::fail('没有找到门店28');
  1050. }
  1051. //组装详情表数据
  1052. $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
  1053. //加库存
  1054. foreach ($batchData as $currentKey => $v) {
  1055. $itemId = $v['itemId'];
  1056. $productId = $v['productId'];
  1057. //单位采购价
  1058. $unitCgPrice = $v['bigPrice'] ?? 0;
  1059. $presell = $productList[$productId]['presell'] ?? 0;
  1060. //普通入库增加库存,预售入库不增加库存 ssh 20220109
  1061. if ($presell == 0) {
  1062. //增加路上库存
  1063. $onRecord = [];
  1064. $onRecord['sjId'] = $sjId;
  1065. $onRecord['shopId'] = $shopId;
  1066. $onRecord['mainId'] = $mainId;
  1067. $onRecord['orderSn'] = $orderSn;
  1068. $onRecord['productId'] = $productId;
  1069. $onRecord['itemId'] = $itemId;
  1070. $onRecord['relateName'] = $ghsName;
  1071. $onRecord['itemNum'] = $v['itemNum'];
  1072. $oldOnStock = $v['onStock'] ?? 0;
  1073. $onRecord['oldStock'] = $oldOnStock;
  1074. $newOnStock = bcadd($oldOnStock, $v['itemNum']);
  1075. $onRecord['newStock'] = $newOnStock;
  1076. $onRecord['ptStyle'] = 2;
  1077. OnStockRecordClass::addPurchaseOrderRecord($onRecord);
  1078. ProductClass::addOnStockByItemNum($productId, $v['itemNum']);
  1079. if ($inType == self::IN_TYPE_NOW) {
  1080. //增加库存
  1081. $stockInfo = ProductClass::addStockByItemNum($productId, $v['itemNum'], ['cost' => $unitCgPrice]);
  1082. $record = [];
  1083. $record['sjId'] = $sjId;
  1084. $record['shopId'] = $shopId;
  1085. $record['mainId'] = $mainId;
  1086. $record['orderSn'] = $orderSn;
  1087. $record['productId'] = $productId;
  1088. $record['itemId'] = $itemId;
  1089. $record['relateName'] = $ghsName;
  1090. $record['itemNum'] = $v['itemNum'];
  1091. $record['oldStock'] = $stockInfo['oldStock'];
  1092. $record['newStock'] = $stockInfo['newStock'];
  1093. $record['unitCost'] = $stockInfo['unitCost'];
  1094. $record['changeCost'] = $stockInfo['changeCost'];
  1095. $record['totalStock'] = $stockInfo['totalStock'];
  1096. $record['totalCost'] = $stockInfo['totalCost'];
  1097. $record['avCost'] = $stockInfo['avCost'];
  1098. StockRecordClass::addPurchaseOrderRecord($record);
  1099. //减少路上库存
  1100. $onRecord = [];
  1101. $onRecord['sjId'] = $sjId;
  1102. $onRecord['shopId'] = $shopId;
  1103. $onRecord['mainId'] = $mainId;
  1104. $onRecord['orderSn'] = $orderSn;
  1105. $onRecord['productId'] = $productId;
  1106. $onRecord['itemId'] = $itemId;
  1107. $onRecord['relateName'] = $ghsName;
  1108. $onRecord['itemNum'] = $v['itemNum'];
  1109. $onRecord['oldStock'] = $newOnStock;
  1110. $onRecord['newStock'] = bcsub($newOnStock, $v['itemNum']);
  1111. $onRecord['ptStyle'] = 2;
  1112. OnStockRecordClass::addInStockOrderRecord($onRecord);
  1113. ProductClass::decreaseOnStockByItemNum($productId, $v['itemNum']);
  1114. }
  1115. }
  1116. $productWeight = $productList[$productId]['weight'] ?? 0;
  1117. $unitFreight = bcmul($avgKgWeight, $productWeight, 3);
  1118. $unitCost = bcadd($unitCgPrice, $unitFreight, 3);
  1119. $unitCost = round($unitCost, 2);
  1120. $batchData[$currentKey]['bigFreight'] = $unitFreight;
  1121. $batchData[$currentKey]['cost'] = $unitCost;
  1122. $currentCover = $productList[$productId]['cover'] ?? '';
  1123. $currentName = $productList[$productId]['name'] ?? '';
  1124. $batchData[$currentKey]['cover'] = $currentCover;
  1125. $batchData[$currentKey]['name'] = $currentName;
  1126. $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
  1127. $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
  1128. $currentRatio = $productList[$productId]['ratio'] ?? 0;
  1129. $currentRatioType = $productList[$productId]['ratioType'] ?? 0;
  1130. $currentClassId = $productList[$productId]['classId'] ?? 0;
  1131. $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
  1132. $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
  1133. $batchData[$currentKey]['ratio'] = $currentRatio;
  1134. $batchData[$currentKey]['ratioType'] = $currentRatioType;
  1135. $batchData[$currentKey]['presell'] = $presell;
  1136. $batchData[$currentKey]['classId'] = $currentClassId;
  1137. $batchData[$currentKey]['entryTime'] = $order->entryTime ?? date("Y-m-d H:i:s");
  1138. $batchData[$currentKey]['addTime'] = $order->addTime ?? date("Y-m-d H:i:s");
  1139. $batchData[$currentKey]['status'] = $order->status ?? 1;
  1140. $batchData[$currentKey]['cgStaffId'] = $order->cgStaffId ?? 0;
  1141. $batchData[$currentKey]['cgStaffName'] = $order->cgStaffName ?? '';
  1142. $batchData[$currentKey]['staffId'] = $order->shopAdminId ?? 0;
  1143. $batchData[$currentKey]['staffName'] = $order->shopAdminName ?? '';
  1144. if ($inType == self::IN_TYPE_NOW) {
  1145. $product = ProductClass::getLockById($productId);
  1146. $product->cost = $unitCost;
  1147. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  1148. $product->save();
  1149. //成本变动记录
  1150. $changeData = [
  1151. 'ptStyle' => 2,
  1152. 'sjId' => $sjId,
  1153. 'mainId' => $mainId,
  1154. 'itemId' => $productId,
  1155. 'ptItemId' => $itemId,
  1156. 'cost' => $unitCost,
  1157. 'staffId' => $staffId,
  1158. 'staffName' => $staffName,
  1159. 'name' => $product->name ?? '',
  1160. 'cover' => $product->cover ?? '',
  1161. 'targetId' => $order->id ?? 0,
  1162. 'event' => '采购',
  1163. ];
  1164. CostChangeClass::addData($changeData);
  1165. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1166. //直营门店的成本价也要一起改掉
  1167. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1168. if (!empty($allShop)) {
  1169. $currentItemId = $product->itemId ?? 0;
  1170. foreach ($allShop as $currentShop) {
  1171. $currentId = $currentShop->mainId ?? 0;
  1172. if ($currentId == $mainId) {
  1173. continue;
  1174. }
  1175. if (isset($currentShop->join) && $currentShop->join == 1) {
  1176. continue;
  1177. }
  1178. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  1179. if (!empty($currentProduct)) {
  1180. $currentProduct->cost = $unitCost;
  1181. $currentProduct->save();
  1182. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  1183. //成本变动记录
  1184. $changeData = [
  1185. 'ptStyle' => 2,
  1186. 'sjId' => $currentProduct->sjId ?? 0,
  1187. 'mainId' => $currentProduct->mainId ?? 0,
  1188. 'itemId' => $currentProduct->id ?? 0,
  1189. 'ptItemId' => $currentProduct->itemId ?? 0,
  1190. 'cost' => $unitCost,
  1191. 'staffId' => $currentStaff->id ?? 0,
  1192. 'staffName' => $currentStaff->name ?? '',
  1193. 'name' => $currentProduct->name ?? '',
  1194. 'cover' => $currentProduct->cover ?? '',
  1195. 'targetId' => $order->id ?? 0,
  1196. 'event' => $shop->shopName . '采购',
  1197. ];
  1198. CostChangeClass::addData($changeData);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. //写入详情表
  1206. PurchaseOrderItemClass::batchAddOrderItem($batchData);
  1207. if ($inType == self::IN_TYPE_NOW) {
  1208. //门店支出增加
  1209. $mainId = $shop->mainId ?? 0;
  1210. $main = MainClass::getLockById($mainId);
  1211. if (empty($main)) {
  1212. util::fail('没有资产信息');
  1213. }
  1214. //支出增加
  1215. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  1216. $main->totalExpend = $currentTotalExpend;
  1217. $main->save();
  1218. //采购增加
  1219. $main->cgFinish += 1;
  1220. $main->totalPurchaseOrder += 1;
  1221. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  1222. $main->totalPurchase = $currentTotalPurchase;
  1223. $main->save();
  1224. //供货商资产增加
  1225. $ghsId = $data['ghsId'] ?? 0;
  1226. $ghs = GhsClass::getLockById($ghsId);
  1227. if (empty($ghs)) {
  1228. util::fail('没有找到供货商');
  1229. }
  1230. self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $order, $sjId, $shopId);
  1231. //采购统计
  1232. $cgNum = $order->itemNum ?? 0;
  1233. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  1234. //采购按供货商统计
  1235. StatCgGhsClass::ghsReplace($order);
  1236. //门店应收客户款增加
  1237. $ghsShopId = $ghs->shopId;
  1238. $ghsShop = ShopClass::getLockById($ghsShopId);
  1239. if (empty($ghsShop)) {
  1240. util::fail('没有找到门店29');
  1241. }
  1242. $ghsMainId = $ghsShop->mainId ?? 0;
  1243. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  1244. if (empty($ghsMain)) {
  1245. util::fail('没有main信息23');
  1246. }
  1247. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  1248. $ghsMain->mayGathering = $currentMayGathering;
  1249. self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
  1250. $ghsMain->save();
  1251. $order->debt = self::DEBT_YES;
  1252. $order->save(false, ['debt']);
  1253. //当天和当月支出统计
  1254. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  1255. //支出流水
  1256. $payWay = dict::getDict('payWay', 'unknown');
  1257. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  1258. $sjId = $order->sjId ?? 0;
  1259. $shopId = $order->shopId ?? 0;
  1260. $event = '采购';
  1261. $mainId = $shop->mainId ?? 0;
  1262. $capitalData = [
  1263. 'capitalType' => $capitalType,
  1264. 'io' => 0,
  1265. 'totalExpend' => $currentTotalExpend,
  1266. 'payWay' => $payWay,
  1267. 'amount' => $currentPrice,
  1268. 'sjId' => $sjId,
  1269. 'shopId' => $shopId,
  1270. 'event' => $event,
  1271. 'mainId' => $mainId
  1272. ];
  1273. ShopCapitalClass::addCapital($capitalData);
  1274. }
  1275. $transaction->commit();
  1276. return $order;
  1277. } catch (\Exception $exception) {
  1278. $transaction->rollBack();
  1279. util::fail('保存失败:' . $exception->getMessage());
  1280. }
  1281. return false;
  1282. }
  1283. //采购单列表 lqh 2021.1.19
  1284. public static function getOrderList($where)
  1285. {
  1286. $data = self::getList('*', $where, 'addTime DESC');
  1287. $list = $data['list'] ?? [];
  1288. $data['list'] = self::groupSupplierList($list);
  1289. $data['list'] = self::groupAdminList($data['list']);
  1290. $data['list'] = self::groupStatusName($data['list']);
  1291. //订单列表页需要显示花材
  1292. if (!empty($list)) {
  1293. foreach ($list as $key => $order) {
  1294. $orderSn = $order['orderSn'] ?? '';
  1295. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, 'id,name,itemNum,refundNum,aboutPrice,bigPrice');
  1296. if (!empty($itemList)) {
  1297. $data['list'][$key]['itemList'] = $itemList;
  1298. }
  1299. }
  1300. }
  1301. return $data;
  1302. }
  1303. //获取订单详情信息 lqh 2021.1.20
  1304. public static function getOrderDetail($orderSn, $shopId)
  1305. {
  1306. $where = ['orderSn' => $orderSn];
  1307. $orderData = self::getByCondition($where);
  1308. if (empty($orderData)) {
  1309. util::fail('订单不存在');
  1310. }
  1311. if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
  1312. util::fail('无法访问,编号669');
  1313. }
  1314. $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
  1315. if ($orderInfo) {
  1316. foreach ($orderInfo as $key => $val) {
  1317. $id = $val['id'] ?? 0;
  1318. $shortCover = $val['cover'] ?? '';
  1319. $orderInfo[$key]['ShortCover'] = $shortCover;
  1320. $orderInfo[$key]['itemNum'] = $val['itemNum'] ? floatval($val['itemNum']) : 0;
  1321. $orderInfo[$key]['bigPrice'] = $val['bigPrice'] ? floatval($val['bigPrice']) : 0;
  1322. $orderInfo[$key]['totalPrice'] = $val['totalPrice'] ? floatval($val['totalPrice']) : 0;
  1323. $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1324. $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  1325. $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $id], null, '*');
  1326. if (!empty($sendList)) {
  1327. foreach ($sendList as $kk => $vv) {
  1328. //前端使用
  1329. $sendList[$kk]['currentRefundNum'] = '';
  1330. }
  1331. }
  1332. $orderInfo[$key]['sendList'] = $sendList;
  1333. $num = $val['itemNum'] ?? 0;
  1334. $confirmSendNum = $val['confirmSendNum'] ?? 0;
  1335. $orderInfo[$key]['noSeatNum'] = 0;
  1336. if ($num > $confirmSendNum) {
  1337. $orderInfo[$key]['noSeatNum'] = floatval(bcsub($num, $confirmSendNum, 2));
  1338. }
  1339. }
  1340. }
  1341. $orderData = self::groupAdmin($orderData);
  1342. //供货商信息
  1343. $ghsId = $orderData['ghsId'] ?? 0;
  1344. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  1345. $orderData['supplierName'] = $ghsInfo['name'] ?? '';
  1346. $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
  1347. $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
  1348. $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
  1349. $orderData['itemInfo'] = $orderInfo;
  1350. $orderData['statusName'] = self::getStatusName($orderData['status']);
  1351. //1寄付 2到付
  1352. $yfPayWay = $orderData['yfPayWay'] ?? 1;
  1353. $actPrice = $orderData['actPrice'] ?? 0;
  1354. $shortCharge = $orderData['shortCharge'] ?? 0;
  1355. $longCharge = $orderData['longCharge'] ?? 0;
  1356. $pickCharge = $orderData['pickCharge'] ?? 0;
  1357. $localCharge = $orderData['localCharge'] ?? 0;
  1358. $totalCgCost = $actPrice;
  1359. if ($yfPayWay == 2) {
  1360. $totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
  1361. $totalCgCost = bcadd($totalCgCost, $longCharge, 2);
  1362. }
  1363. $totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
  1364. $totalCgCost = bcadd($totalCgCost, $localCharge, 2);
  1365. $orderData['totalCgCost'] = $totalCgCost;
  1366. $shop = ShopClass::getById($shopId, true);
  1367. $shopBookSn = $shop->bookSn ?? 0;
  1368. $bookSn = $orderData['bookSn'] ?? 0;
  1369. //是否需要提醒更新货位
  1370. $needRemindRefreshSeat = 0;
  1371. $inBooking = 0;
  1372. if (!empty($shopBookSn) && $shopBookSn == $bookSn) {
  1373. //在预订状态,用于采购单售后时判断是否也要退货位上花材
  1374. $inBooking = 1;
  1375. if (isset($orderData['status']) && $orderData['status'] == 3) {
  1376. $needRemindRefreshSeat = 1;
  1377. }
  1378. }
  1379. $orderData['needRemindRefreshSeat'] = $needRemindRefreshSeat;
  1380. $orderData['inBooking'] = $inBooking;
  1381. return $orderData;
  1382. }
  1383. // 组装 orderItem lqh 2021.1.25
  1384. public static function groupOrderItem($ghsItemInfo, $orderSn)
  1385. {
  1386. $productData = self::getProductMapData($ghsItemInfo);
  1387. $batchData = [];
  1388. foreach ($ghsItemInfo as $v) {
  1389. $tmp = [];
  1390. $productId = $v['productId'];
  1391. $currentAboutPrice = $v['aboutPrice'] ?? 0;
  1392. $currentTotal = $v['totalPrice'] ?? 0;
  1393. $assignSeat = $v['assignSeat'] ?? 1;
  1394. $itemId = $productData[$productId]['itemId'];
  1395. $tmp['orderSn'] = $orderSn;
  1396. $tmp['assignSeat'] = $assignSeat;
  1397. $tmp['itemId'] = $itemId;
  1398. $tmp['belongCost'] = $productData[$productId]['belongCost'] ?? 0;
  1399. $tmp['productId'] = $v['productId'];
  1400. $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;
  1401. $tmp['onStock'] = $productData[$productId]['onStock'] ?? 0;
  1402. $tmp['aboutPrice'] = $currentAboutPrice;
  1403. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  1404. $tmp['itemNum'] = $v['bigNum'];
  1405. $tmp['bigPrice'] = $v['itemPrice'];
  1406. if ($currentAboutPrice == 1) {
  1407. $totalPrice = $currentTotal;
  1408. } else {
  1409. $totalPrice = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
  1410. }
  1411. $tmp['totalPrice'] = $totalPrice;
  1412. } else {
  1413. $smallNum = $v['smallNum'] ?? 0;
  1414. if ($smallNum <= 0) {
  1415. util::fail('没有数量');
  1416. }
  1417. $ratioType = $v['ratioType'] ?? 1;
  1418. if ($ratioType == 1) {
  1419. //util::fail('小单位,单位比必须固定的哈');
  1420. }
  1421. $ratio = $v['ratio'] ?? 1;
  1422. if ($ratio <= 1) {
  1423. //util::fail('小单位,单位比必须大于1的哦');
  1424. }
  1425. $changeToBigNum = bcdiv($smallNum, $ratio, 2);
  1426. $tmp['itemNum'] = $changeToBigNum;
  1427. $bigPrice = bcmul($v['itemPrice'], $ratio, 2);
  1428. $tmp['bigPrice'] = $bigPrice;
  1429. if ($currentAboutPrice == 1) {
  1430. $totalPrice = $currentTotal;
  1431. } else {
  1432. $totalPrice = bcmul($v['itemPrice'], $smallNum, 2);
  1433. }
  1434. $tmp['totalPrice'] = $totalPrice;
  1435. }
  1436. $batchData[] = $tmp;
  1437. }
  1438. return $batchData;
  1439. }
  1440. // 组装状态码 lqh 2021.1.30
  1441. public static function groupStatusName($list)
  1442. {
  1443. foreach ($list as $k => $val) {
  1444. $status = $val['status'] ?? 0;
  1445. $list[$k]['statusName'] = self::getStatusName($status);
  1446. }
  1447. return $list;
  1448. }
  1449. // 获取状态码名称
  1450. public static function getStatusName($status)
  1451. {
  1452. return self::$statusMap[$status] ?? '';
  1453. }
  1454. //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
  1455. public static function statNum($shopId)
  1456. {
  1457. $data = [
  1458. 'allNum' => 0,
  1459. 'unPayNum' => 0,
  1460. 'unSendNum' => 0,
  1461. 'sendingNum' => 0,
  1462. 'finishNum' => 0,
  1463. ];
  1464. $res = self::getAllList("status", ['shopId' => $shopId]);
  1465. foreach ($res as $v) {
  1466. $data['allNum']++;
  1467. $status = $v['status'];
  1468. switch ($status) {
  1469. case self::PURCHASE_ORDER_STATUS_WAIT:
  1470. //待支付
  1471. $data['unPayNum']++;
  1472. break;
  1473. case self::PURCHASE_ORDER_STATUS_UN_SEND:
  1474. $data['unSendNum']++;
  1475. break;
  1476. case self::PURCHASE_ORDER_STATUS_SENDING:
  1477. $data['sendingNum']++;
  1478. break;
  1479. case self::PURCHASE_ORDER_STATUS_COMPLETE:
  1480. $data['finishNum']++;
  1481. break;
  1482. }
  1483. }
  1484. return $data;
  1485. }
  1486. //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
  1487. public static function statOrderInfo($shopId, $statType)
  1488. {
  1489. $start = '';
  1490. $end = '';
  1491. switch ($statType) {
  1492. case "today":
  1493. $start = date('Y-m-d');
  1494. $end = $start;
  1495. break;
  1496. case "yesterday" :
  1497. $start = date('Y-m-d', strtotime("-1 day"));
  1498. $end = $start;
  1499. break;
  1500. case "seven":
  1501. $start = date('Y-m-d', strtotime("-6 day"));
  1502. $end = date('Y-m-d');
  1503. break;
  1504. case "thirty":
  1505. $start = date('Y-m-d', strtotime("-29 day"));
  1506. $end = date('Y-m-d');
  1507. break;
  1508. default:
  1509. }
  1510. $where = [];
  1511. $where['shopId'] = $shopId;
  1512. $where['addTime'] = ['between', [$start, $end]];
  1513. $res = self::getAllByCondition($where, null, "id,actPrice");
  1514. $totalAmount = 0;
  1515. $totalNum = count($res);
  1516. foreach ($res as $v) {
  1517. $actPrice = $v['actPrice'] ?? 0;
  1518. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  1519. }
  1520. return [
  1521. 'totalAmount' => $totalAmount,
  1522. 'totalNum' => $totalNum,
  1523. ];
  1524. }
  1525. public static function exportDueList($list, $mainId)
  1526. {
  1527. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1528. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1529. $objPHPExcel = new \PHPExcel();
  1530. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1531. ->setLastModifiedBy("Maarten Balliauw")
  1532. ->setTitle("Office 2007 XLSX Document")
  1533. ->setSubject("Office 2007 XLSX Document")
  1534. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1535. ->setKeywords("office 2007 openxml php")
  1536. ->setCategory("file");
  1537. $ghsTitle = '供货商';
  1538. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1539. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1540. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1541. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1542. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1543. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1544. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1545. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  1546. $objPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
  1547. $objPHPExcel->getActiveSheet()->setCellValue('C1', '金额');
  1548. //设置宽度
  1549. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  1550. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1551. //加粗
  1552. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1553. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1554. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1555. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1556. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1557. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1558. $baseRow = 2;
  1559. foreach ($list as $key => $custom) {
  1560. $i = $baseRow + $key;
  1561. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  1562. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  1563. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1564. //居左
  1565. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1566. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1567. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1568. }
  1569. $fileName = '应付统计-' . date("m-d");
  1570. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1571. $objPHPExcel->setActiveSheetIndex(0);
  1572. $dir = './priceTable/' . $mainId;
  1573. if (file_exists($dir) == false) {
  1574. mkdir($dir, 0777, true);
  1575. }
  1576. $date = $fileName;
  1577. $file = $date . '.xls';
  1578. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1579. if (file_exists($dir . '/' . $file)) {
  1580. unlink($dir . '/' . $file);
  1581. }
  1582. $objWriter->save($dir . '/' . $file);
  1583. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1584. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1585. }
  1586. public static function exportOrderData($respond, $mainId)
  1587. {
  1588. $orderList = $respond['list'] ?? [];
  1589. if (empty($orderList)) {
  1590. util::fail('没有数据需要导出');
  1591. }
  1592. $orderSnList = array_column($orderList, 'orderSn');
  1593. $orderItemData = PurchaseOrderItemClass::getAllByCondition(['orderSn' => ['in', $orderSnList]], null, '*');
  1594. $orderItemList = [];
  1595. if (!empty($orderItemData)) {
  1596. foreach ($orderItemData as $itemInfo) {
  1597. $orderSn = $itemInfo['orderSn'] ?? '';
  1598. $orderItemList[$orderSn][] = $itemInfo;
  1599. }
  1600. }
  1601. foreach ($orderList as $key => $orderInfo) {
  1602. $orderSn = $orderInfo['orderSn'] ?? '';
  1603. $status = $orderInfo['status'] ?? 0;
  1604. //增加项
  1605. $actPrice = $orderInfo['actPrice'] ?? 0;
  1606. if ($status != 4) {
  1607. unset($orderList[$key]);
  1608. continue;
  1609. }
  1610. //增加项
  1611. if ($actPrice <= 0) {
  1612. unset($orderList[$key]);
  1613. continue;
  1614. }
  1615. $itemList = $orderItemList[$orderSn] ?? [];
  1616. $orderList[$key]['itemList'] = $itemList;
  1617. }
  1618. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
  1619. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1620. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1621. $objPHPExcel = new \PHPExcel();
  1622. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1623. ->setLastModifiedBy("Maarten Balliauw")
  1624. ->setTitle("Office 2007 XLSX Document")
  1625. ->setSubject("Office 2007 XLSX Document")
  1626. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1627. ->setKeywords("office 2007 openxml php")
  1628. ->setCategory("file");
  1629. $ghsTitle = '供货商';
  1630. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1631. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1632. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1633. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1634. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1635. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1636. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1637. $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
  1638. $objPHPExcel->getActiveSheet()->setCellValue('B1', '创建时间');
  1639. $objPHPExcel->getActiveSheet()->setCellValue('C1', '入库时间');
  1640. $objPHPExcel->getActiveSheet()->setCellValue('D1', '订单金额');
  1641. $objPHPExcel->getActiveSheet()->setCellValue('E1', '退款金额');
  1642. $objPHPExcel->getActiveSheet()->setCellValue('F1', '实际金额');
  1643. $objPHPExcel->getActiveSheet()->setCellValue('G1', '供货商名称');
  1644. $objPHPExcel->getActiveSheet()->setCellValue('H1', '录入人员');
  1645. $objPHPExcel->getActiveSheet()->setCellValue('I1', '采购人员');
  1646. $objPHPExcel->getActiveSheet()->setCellValue('J1', '状态');
  1647. $objPHPExcel->getActiveSheet()->setCellValue('K1', '打印次数');
  1648. $objPHPExcel->getActiveSheet()->setCellValue('L1', '结清状态');
  1649. $objPHPExcel->getActiveSheet()->setCellValue('M1', '备注');
  1650. $objPHPExcel->getActiveSheet()->setCellValue('N1', '花材Id');
  1651. $objPHPExcel->getActiveSheet()->setCellValue('O1', '花材名称');
  1652. $objPHPExcel->getActiveSheet()->setCellValue('P1', '单价');
  1653. $objPHPExcel->getActiveSheet()->setCellValue('Q1', '数量');
  1654. $objPHPExcel->getActiveSheet()->setCellValue('R1', '已退数量');
  1655. $objPHPExcel->getActiveSheet()->setCellValue('S1', '金额');
  1656. $objPHPExcel->getActiveSheet()->setCellValue('T1', '分类');
  1657. //设置宽度
  1658. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1659. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1660. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1661. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1662. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1663. $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
  1664. $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
  1665. //加粗
  1666. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1667. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1668. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1669. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1670. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1671. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1672. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  1673. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  1674. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  1675. $objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setSize(9)->setBold(true);
  1676. $objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setSize(9)->setBold(true);
  1677. $objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setSize(9)->setBold(true);
  1678. $objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setSize(9)->setBold(true);
  1679. $objPHPExcel->getActiveSheet()->getStyle('N1')->getFont()->setSize(9)->setBold(true);
  1680. $objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setSize(9)->setBold(true);
  1681. $objPHPExcel->getActiveSheet()->getStyle('P1')->getFont()->setSize(9)->setBold(true);
  1682. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getFont()->setSize(9)->setBold(true);
  1683. $objPHPExcel->getActiveSheet()->getStyle('R1')->getFont()->setSize(9)->setBold(true);
  1684. $objPHPExcel->getActiveSheet()->getStyle('S1')->getFont()->setSize(9)->setBold(true);
  1685. $objPHPExcel->getActiveSheet()->getStyle('T1')->getFont()->setSize(9)->setBold(true);
  1686. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1687. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1688. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1689. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1690. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1691. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1692. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1693. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1694. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1695. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1696. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1697. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1698. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1699. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1700. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1701. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1702. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1703. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1704. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1705. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1706. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1707. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1708. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1709. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1710. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1711. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1712. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1713. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1714. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1715. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1716. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1717. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1718. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1719. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1720. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1721. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1722. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1723. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1724. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1725. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1726. $baseRow = 2;
  1727. $x = 0;
  1728. foreach ($orderList as $key => $orderData) {
  1729. $currentList = $orderData['itemList'] ?? [];
  1730. if (!empty($currentList)) {
  1731. foreach ($currentList as $k => $orderInfo) {
  1732. $i = $baseRow + $x;
  1733. $x++;
  1734. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $orderData['orderSn']);
  1735. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $orderData['addTime']);
  1736. if ($orderData['status'] == 4) {
  1737. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $orderData['entryTime']);
  1738. } else {
  1739. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, '');
  1740. }
  1741. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $orderData['orderPrice']);
  1742. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $orderData['tkPrice']);
  1743. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $orderData['actPrice']);
  1744. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $orderData['ghsName']);
  1745. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $orderData['shopAdminName']);
  1746. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $orderData['cgStaffName']);
  1747. $map = [1 => '待确认', 2 => '待发货', 3 => '待入库', 4 => '已入库', 5 => '已取消'];
  1748. $statusName = isset($orderData['status']) && isset($map[$orderData['status']]) ? $map[$orderData['status']] : '未知';
  1749. $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $statusName);
  1750. $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $orderData['printNum'] ?? 0);
  1751. $debtName = '';
  1752. if ($orderData['debt'] == 1) {
  1753. $debtName = '未结清';
  1754. }
  1755. if ($orderData['debt'] == 2) {
  1756. $debtName = '已结清';
  1757. }
  1758. $objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $debtName);
  1759. $objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $orderData['remark']);
  1760. $objPHPExcel->getActiveSheet()->setCellValue('N' . $i, $orderInfo['productId']);
  1761. $objPHPExcel->getActiveSheet()->setCellValue('O' . $i, $orderInfo['name']);
  1762. $objPHPExcel->getActiveSheet()->setCellValue('P' . $i, $orderInfo['bigPrice']);
  1763. $remainNum = bcsub($orderInfo['itemNum'], $orderInfo['refundNum']);
  1764. $price = bcmul($remainNum, $orderInfo['bigPrice'], 2);
  1765. $price = floatval($price);
  1766. $objPHPExcel->getActiveSheet()->setCellValue('Q' . $i, $remainNum);
  1767. $objPHPExcel->getActiveSheet()->setCellValue('R' . $i, $orderInfo['refundNum']);
  1768. $objPHPExcel->getActiveSheet()->setCellValue('S' . $i, $price);
  1769. $classId = $orderInfo['classId'] ?? 0;
  1770. $className = isset($classList[$classId]) && isset($classList[$classId]['name']) ? $classList[$classId]['name'] : '';
  1771. $objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $className);
  1772. //居左
  1773. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1774. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1775. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1776. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1777. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1778. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1779. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1780. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1781. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1782. $objPHPExcel->getActiveSheet()->getStyle('J' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1783. $objPHPExcel->getActiveSheet()->getStyle('K' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1784. $objPHPExcel->getActiveSheet()->getStyle('L' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1785. $objPHPExcel->getActiveSheet()->getStyle('M' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1786. $objPHPExcel->getActiveSheet()->getStyle('N' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1787. $objPHPExcel->getActiveSheet()->getStyle('O' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1788. $objPHPExcel->getActiveSheet()->getStyle('P' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1789. $objPHPExcel->getActiveSheet()->getStyle('Q' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1790. $objPHPExcel->getActiveSheet()->getStyle('R' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1791. $objPHPExcel->getActiveSheet()->getStyle('S' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1792. $objPHPExcel->getActiveSheet()->getStyle('T' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1793. }
  1794. }
  1795. }
  1796. $fileName = '采购单明细-' . time();
  1797. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1798. $objPHPExcel->setActiveSheetIndex(0);
  1799. $dir = './priceTable/' . $mainId;
  1800. if (file_exists($dir) == false) {
  1801. mkdir($dir, 0777, true);
  1802. }
  1803. $date = $fileName;
  1804. $file = $date . '.xls';
  1805. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1806. if (file_exists($dir . '/' . $file)) {
  1807. unlink($dir . '/' . $file);
  1808. }
  1809. $objWriter->save($dir . '/' . $file);
  1810. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1811. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1812. }
  1813. /**
  1814. * 按 productId 升序排列,用于库存行锁顺序(降低多商品并发死锁)
  1815. * @param array $list
  1816. */
  1817. private static function sortRowsByProductIdAsc(&$list)
  1818. {
  1819. usort($list, function ($left, $right) {
  1820. $leftId = isset($left['productId']) ? intval($left['productId']) : 0;
  1821. $rightId = isset($right['productId']) ? intval($right['productId']) : 0;
  1822. if ($leftId == $rightId) {
  1823. return 0;
  1824. }
  1825. return $leftId < $rightId ? -1 : 1;
  1826. });
  1827. }
  1828. }