PurchaseOrderClass.php 106 KB

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