PurchaseOrderClass.php 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  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. // 送货时间写入 sendTime(datetime),允许补录过去日期;空则当天
  1271. $sendTimeInput = isset($data['sendTime']) ? str_replace('/', '-', trim((string)$data['sendTime'])) : '';
  1272. if ($sendTimeInput === '') {
  1273. $data['sendTime'] = date('Y-m-d H:i:s');
  1274. } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $sendTimeInput)) {
  1275. $data['sendTime'] = $sendTimeInput . ' ' . date('H:i:s');
  1276. } elseif (!preg_match('/^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}/', $sendTimeInput)) {
  1277. $data['sendTime'] = date('Y-m-d H:i:s');
  1278. } else {
  1279. $data['sendTime'] = $sendTimeInput;
  1280. }
  1281. $packingCharge = $data['packingCharge'] ?? 0;
  1282. $shortCharge = $data['shortCharge'] ?? 0;
  1283. $longCharge = $data['longCharge'] ?? 0;
  1284. $pickCharge = $data['pickCharge'] ?? 0;
  1285. $localCharge = $data['localCharge'] ?? 0;
  1286. //提货费和本地运费
  1287. $paidPrice = bcadd($localCharge, $pickCharge, 2);
  1288. $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
  1289. //打包费
  1290. $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
  1291. //寄付才算长途和短途运费
  1292. if ($yfPayWay == self::YF_PAY_WAY_JF) {
  1293. $currentPrice = bcadd($currentPrice, $shortCharge, 2);
  1294. $currentPrice = bcadd($currentPrice, $longCharge, 2);
  1295. } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
  1296. $paidPrice = bcadd($paidPrice, $shortCharge, 2);
  1297. $paidPrice = bcadd($paidPrice, $longCharge, 2);
  1298. } else {
  1299. util::fail('没有找到这个运费支付方式');
  1300. }
  1301. // $paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  1302. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  1303. $data['prePrice'] = $currentPrice;
  1304. if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
  1305. //报损减免后金额
  1306. $modifyPrice = $data['modifyPrice'] ?? 0;
  1307. if ($modifyPrice > $currentPrice) {
  1308. util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
  1309. }
  1310. if ($modifyPrice < $currentPrice) {
  1311. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  1312. $data['discountType'] = dict::getDict('discountType', 'discount');
  1313. $currentPrice = $modifyPrice;
  1314. }
  1315. }
  1316. $data['orderPrice'] = $currentPrice;
  1317. $data['actPrice'] = $currentPrice;
  1318. $data['realPrice'] = $currentPrice;
  1319. $data['paidPrice'] = $paidPrice;
  1320. //总运费
  1321. $totalFreight = bcadd($packingCharge, $shortCharge, 2);
  1322. $totalFreight = bcadd($totalFreight, $longCharge, 2);
  1323. $totalFreight = bcadd($totalFreight, $pickCharge, 2);
  1324. $totalFreight = bcadd($totalFreight, $localCharge, 2);
  1325. $data['totalFreight'] = $totalFreight;
  1326. $connection = Yii::$app->db;
  1327. $transaction = $connection->beginTransaction();
  1328. try {
  1329. //总运费/总公斤数,计算每公斤的运费
  1330. if ($totalWeight > 0) {
  1331. $avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
  1332. } else {
  1333. $avgKgWeight = 0;
  1334. }
  1335. $avgKgWeight = round($avgKgWeight, 2);
  1336. $data['avgKgWeight'] = $avgKgWeight;
  1337. $data['salt'] = stringUtil::charsShuffleLowerCase(8);
  1338. $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
  1339. $order = self::add($data, true);
  1340. $shop = ShopClass::getLockById($shopId);
  1341. if (empty($shop)) {
  1342. util::fail('没有找到门店28');
  1343. }
  1344. //组装详情表数据
  1345. $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
  1346. //加库存
  1347. foreach ($batchData as $currentKey => $v) {
  1348. $itemId = $v['itemId'];
  1349. $productId = $v['productId'];
  1350. //单位采购价
  1351. $unitCgPrice = $v['bigPrice'] ?? 0;
  1352. $presell = $productList[$productId]['presell'] ?? 0;
  1353. //普通入库增加库存,预售入库不增加库存 ssh 20220109
  1354. if ($presell == 0) {
  1355. //增加路上库存
  1356. $onRecord = [];
  1357. $onRecord['sjId'] = $sjId;
  1358. $onRecord['shopId'] = $shopId;
  1359. $onRecord['mainId'] = $mainId;
  1360. $onRecord['orderSn'] = $orderSn;
  1361. $onRecord['productId'] = $productId;
  1362. $onRecord['itemId'] = $itemId;
  1363. $onRecord['relateName'] = $ghsName;
  1364. $onRecord['itemNum'] = $v['itemNum'];
  1365. $oldOnStock = $v['onStock'] ?? 0;
  1366. $onRecord['oldStock'] = $oldOnStock;
  1367. $newOnStock = bcadd($oldOnStock, $v['itemNum']);
  1368. $onRecord['newStock'] = $newOnStock;
  1369. $onRecord['ptStyle'] = 2;
  1370. OnStockRecordClass::addPurchaseOrderRecord($onRecord);
  1371. ProductClass::addOnStockByItemNum($productId, $v['itemNum']);
  1372. if ($inType == self::IN_TYPE_NOW) {
  1373. //增加库存
  1374. $stockInfo = ProductClass::addStockByItemNum($productId, $v['itemNum'], ['cost' => $unitCgPrice]);
  1375. $record = [];
  1376. $record['sjId'] = $sjId;
  1377. $record['shopId'] = $shopId;
  1378. $record['mainId'] = $mainId;
  1379. $record['orderSn'] = $orderSn;
  1380. $record['productId'] = $productId;
  1381. $record['itemId'] = $itemId;
  1382. $record['relateName'] = $ghsName;
  1383. $record['itemNum'] = $v['itemNum'];
  1384. $record['oldStock'] = $stockInfo['oldStock'];
  1385. $record['newStock'] = $stockInfo['newStock'];
  1386. $record['unitCost'] = $stockInfo['unitCost'];
  1387. $record['changeCost'] = $stockInfo['changeCost'];
  1388. $record['totalStock'] = $stockInfo['totalStock'];
  1389. $record['totalCost'] = $stockInfo['totalCost'];
  1390. $record['avCost'] = $stockInfo['avCost'];
  1391. StockRecordClass::addPurchaseOrderRecord($record);
  1392. //减少路上库存
  1393. $onRecord = [];
  1394. $onRecord['sjId'] = $sjId;
  1395. $onRecord['shopId'] = $shopId;
  1396. $onRecord['mainId'] = $mainId;
  1397. $onRecord['orderSn'] = $orderSn;
  1398. $onRecord['productId'] = $productId;
  1399. $onRecord['itemId'] = $itemId;
  1400. $onRecord['relateName'] = $ghsName;
  1401. $onRecord['itemNum'] = $v['itemNum'];
  1402. $onRecord['oldStock'] = $newOnStock;
  1403. $onRecord['newStock'] = bcsub($newOnStock, $v['itemNum']);
  1404. $onRecord['ptStyle'] = 2;
  1405. OnStockRecordClass::addInStockOrderRecord($onRecord);
  1406. ProductClass::decreaseOnStockByItemNum($productId, $v['itemNum']);
  1407. }
  1408. }
  1409. $productWeight = $productList[$productId]['weight'] ?? 0;
  1410. $unitFreight = bcmul($avgKgWeight, $productWeight, 3);
  1411. $unitCost = bcadd($unitCgPrice, $unitFreight, 3);
  1412. $unitCost = round($unitCost, 2);
  1413. $batchData[$currentKey]['bigFreight'] = $unitFreight;
  1414. $batchData[$currentKey]['cost'] = $unitCost;
  1415. $currentCover = $productList[$productId]['cover'] ?? '';
  1416. $currentName = $productList[$productId]['name'] ?? '';
  1417. $batchData[$currentKey]['cover'] = $currentCover;
  1418. $batchData[$currentKey]['name'] = $currentName;
  1419. $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
  1420. $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
  1421. $currentRatio = $productList[$productId]['ratio'] ?? 0;
  1422. $currentRatioType = $productList[$productId]['ratioType'] ?? 0;
  1423. $currentClassId = $productList[$productId]['classId'] ?? 0;
  1424. $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
  1425. $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
  1426. $batchData[$currentKey]['ratio'] = $currentRatio;
  1427. $batchData[$currentKey]['ratioType'] = $currentRatioType;
  1428. $batchData[$currentKey]['presell'] = $presell;
  1429. $batchData[$currentKey]['classId'] = $currentClassId;
  1430. $batchData[$currentKey]['entryTime'] = $order->entryTime ?? date("Y-m-d H:i:s");
  1431. $batchData[$currentKey]['addTime'] = $order->addTime ?? date("Y-m-d H:i:s");
  1432. $batchData[$currentKey]['status'] = $order->status ?? 1;
  1433. $batchData[$currentKey]['cgStaffId'] = $order->cgStaffId ?? 0;
  1434. $batchData[$currentKey]['cgStaffName'] = $order->cgStaffName ?? '';
  1435. $batchData[$currentKey]['staffId'] = $order->shopAdminId ?? 0;
  1436. $batchData[$currentKey]['staffName'] = $order->shopAdminName ?? '';
  1437. if ($inType == self::IN_TYPE_NOW) {
  1438. $product = ProductClass::getLockById($productId);
  1439. $product->cost = $unitCost;
  1440. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  1441. $product->save();
  1442. //成本变动记录
  1443. $changeData = [
  1444. 'ptStyle' => 2,
  1445. 'sjId' => $sjId,
  1446. 'mainId' => $mainId,
  1447. 'itemId' => $productId,
  1448. 'ptItemId' => $itemId,
  1449. 'cost' => $unitCost,
  1450. 'staffId' => $staffId,
  1451. 'staffName' => $staffName,
  1452. 'name' => $product->name ?? '',
  1453. 'cover' => $product->cover ?? '',
  1454. 'targetId' => $order->id ?? 0,
  1455. 'event' => '采购',
  1456. ];
  1457. CostChangeClass::addData($changeData);
  1458. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1459. //直营门店的成本价也要一起改掉
  1460. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1461. if (!empty($allShop)) {
  1462. $currentItemId = $product->itemId ?? 0;
  1463. foreach ($allShop as $currentShop) {
  1464. $currentId = $currentShop->mainId ?? 0;
  1465. if ($currentId == $mainId) {
  1466. continue;
  1467. }
  1468. if (isset($currentShop->join) && $currentShop->join == 1) {
  1469. continue;
  1470. }
  1471. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  1472. if (!empty($currentProduct)) {
  1473. $currentProduct->cost = $unitCost;
  1474. $currentProduct->save();
  1475. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  1476. //成本变动记录
  1477. $changeData = [
  1478. 'ptStyle' => 2,
  1479. 'sjId' => $currentProduct->sjId ?? 0,
  1480. 'mainId' => $currentProduct->mainId ?? 0,
  1481. 'itemId' => $currentProduct->id ?? 0,
  1482. 'ptItemId' => $currentProduct->itemId ?? 0,
  1483. 'cost' => $unitCost,
  1484. 'staffId' => $currentStaff->id ?? 0,
  1485. 'staffName' => $currentStaff->name ?? '',
  1486. 'name' => $currentProduct->name ?? '',
  1487. 'cover' => $currentProduct->cover ?? '',
  1488. 'targetId' => $order->id ?? 0,
  1489. 'event' => $shop->shopName . '采购',
  1490. ];
  1491. CostChangeClass::addData($changeData);
  1492. }
  1493. }
  1494. }
  1495. }
  1496. }
  1497. }
  1498. //写入详情表
  1499. PurchaseOrderItemClass::batchAddOrderItem($batchData);
  1500. if ($inType == self::IN_TYPE_NOW) {
  1501. //门店支出增加
  1502. $mainId = $shop->mainId ?? 0;
  1503. $main = MainClass::getLockById($mainId);
  1504. if (empty($main)) {
  1505. util::fail('没有资产信息');
  1506. }
  1507. //支出增加
  1508. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  1509. $main->totalExpend = $currentTotalExpend;
  1510. $main->save();
  1511. //采购增加
  1512. $main->cgFinish += 1;
  1513. $main->totalPurchaseOrder += 1;
  1514. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  1515. $main->totalPurchase = $currentTotalPurchase;
  1516. $main->save();
  1517. //供货商资产增加
  1518. $ghsId = $data['ghsId'] ?? 0;
  1519. $ghs = GhsClass::getLockById($ghsId);
  1520. if (empty($ghs)) {
  1521. util::fail('没有找到供货商');
  1522. }
  1523. self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $order, $sjId, $shopId);
  1524. //采购统计
  1525. $cgNum = $order->itemNum ?? 0;
  1526. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  1527. //采购按供货商统计
  1528. StatCgGhsClass::ghsReplace($order);
  1529. //门店应收客户款增加
  1530. $ghsShopId = $ghs->shopId;
  1531. $ghsShop = ShopClass::getLockById($ghsShopId);
  1532. if (empty($ghsShop)) {
  1533. util::fail('没有找到门店29');
  1534. }
  1535. $ghsMainId = $ghsShop->mainId ?? 0;
  1536. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  1537. if (empty($ghsMain)) {
  1538. util::fail('没有main信息23');
  1539. }
  1540. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  1541. $ghsMain->mayGathering = $currentMayGathering;
  1542. self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
  1543. $ghsMain->save();
  1544. $order->debt = self::DEBT_YES;
  1545. // 稍后入库→入库完成:校准/写入挂账原欠与剩余待结
  1546. self::initDebtPrices($order, $currentPrice);
  1547. $order->save(false, ['debt', 'debtPrice', 'remainDebtPrice']);
  1548. //当天和当月支出统计
  1549. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  1550. //支出流水
  1551. $payWay = dict::getDict('payWay', 'unknown');
  1552. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  1553. $sjId = $order->sjId ?? 0;
  1554. $shopId = $order->shopId ?? 0;
  1555. $event = '采购';
  1556. $mainId = $shop->mainId ?? 0;
  1557. $capitalData = [
  1558. 'capitalType' => $capitalType,
  1559. 'io' => 0,
  1560. 'totalExpend' => $currentTotalExpend,
  1561. 'payWay' => $payWay,
  1562. 'amount' => $currentPrice,
  1563. 'sjId' => $sjId,
  1564. 'shopId' => $shopId,
  1565. 'event' => $event,
  1566. 'mainId' => $mainId
  1567. ];
  1568. ShopCapitalClass::addCapital($capitalData);
  1569. }
  1570. $transaction->commit();
  1571. return $order;
  1572. } catch (\Exception $exception) {
  1573. $transaction->rollBack();
  1574. util::fail('保存失败:' . $exception->getMessage());
  1575. }
  1576. return false;
  1577. }
  1578. //采购单列表 lqh 2021.1.19
  1579. public static function getOrderList($where)
  1580. {
  1581. $data = self::getList('*', $where, 'addTime DESC');
  1582. $list = $data['list'] ?? [];
  1583. $data['list'] = self::groupSupplierList($list);
  1584. $data['list'] = self::groupAdminList($data['list']);
  1585. $data['list'] = self::groupStatusName($data['list']);
  1586. //订单列表页需要显示花材
  1587. if (!empty($list)) {
  1588. foreach ($list as $key => $order) {
  1589. $orderSn = $order['orderSn'] ?? '';
  1590. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, 'id,name,itemNum,refundNum,aboutPrice,bigPrice');
  1591. if (!empty($itemList)) {
  1592. $data['list'][$key]['itemList'] = $itemList;
  1593. }
  1594. }
  1595. }
  1596. return $data;
  1597. }
  1598. //获取订单详情信息 lqh 2021.1.20
  1599. public static function getOrderDetail($orderSn, $shopId)
  1600. {
  1601. $where = ['orderSn' => $orderSn];
  1602. $orderData = self::getByCondition($where);
  1603. if (empty($orderData)) {
  1604. util::fail('订单不存在');
  1605. }
  1606. if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
  1607. util::fail('无法访问,编号669');
  1608. }
  1609. $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
  1610. if ($orderInfo) {
  1611. foreach ($orderInfo as $key => $val) {
  1612. $id = $val['id'] ?? 0;
  1613. $shortCover = $val['cover'] ?? '';
  1614. $orderInfo[$key]['ShortCover'] = $shortCover;
  1615. $orderInfo[$key]['itemNum'] = $val['itemNum'] ? floatval($val['itemNum']) : 0;
  1616. $orderInfo[$key]['bigPrice'] = $val['bigPrice'] ? floatval($val['bigPrice']) : 0;
  1617. $orderInfo[$key]['totalPrice'] = $val['totalPrice'] ? floatval($val['totalPrice']) : 0;
  1618. $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1619. $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  1620. $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $id], null, '*');
  1621. if (!empty($sendList)) {
  1622. foreach ($sendList as $kk => $vv) {
  1623. //前端使用
  1624. $sendList[$kk]['currentRefundNum'] = '';
  1625. }
  1626. }
  1627. $orderInfo[$key]['sendList'] = $sendList;
  1628. $num = $val['itemNum'] ?? 0;
  1629. $confirmSendNum = $val['confirmSendNum'] ?? 0;
  1630. $orderInfo[$key]['noSeatNum'] = 0;
  1631. if ($num > $confirmSendNum) {
  1632. $orderInfo[$key]['noSeatNum'] = floatval(bcsub($num, $confirmSendNum, 2));
  1633. }
  1634. }
  1635. }
  1636. $orderData = self::groupAdmin($orderData);
  1637. //供货商信息
  1638. $ghsId = $orderData['ghsId'] ?? 0;
  1639. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  1640. $orderData['supplierName'] = $ghsInfo['name'] ?? '';
  1641. $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
  1642. $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
  1643. $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
  1644. $orderData['itemInfo'] = $orderInfo;
  1645. $orderData['statusName'] = self::getStatusName($orderData['status']);
  1646. //1寄付 2到付
  1647. $yfPayWay = $orderData['yfPayWay'] ?? 1;
  1648. $actPrice = $orderData['actPrice'] ?? 0;
  1649. $shortCharge = $orderData['shortCharge'] ?? 0;
  1650. $longCharge = $orderData['longCharge'] ?? 0;
  1651. $pickCharge = $orderData['pickCharge'] ?? 0;
  1652. $localCharge = $orderData['localCharge'] ?? 0;
  1653. $totalCgCost = $actPrice;
  1654. if ($yfPayWay == 2) {
  1655. $totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
  1656. $totalCgCost = bcadd($totalCgCost, $longCharge, 2);
  1657. }
  1658. $totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
  1659. $totalCgCost = bcadd($totalCgCost, $localCharge, 2);
  1660. $orderData['totalCgCost'] = $totalCgCost;
  1661. $shop = ShopClass::getById($shopId, true);
  1662. $shopBookSn = $shop->bookSn ?? 0;
  1663. $bookSn = $orderData['bookSn'] ?? 0;
  1664. //是否需要提醒更新货位
  1665. $needRemindRefreshSeat = 0;
  1666. $inBooking = 0;
  1667. if (!empty($shopBookSn) && $shopBookSn == $bookSn) {
  1668. //在预订状态,用于采购单售后时判断是否也要退货位上花材
  1669. $inBooking = 1;
  1670. if (isset($orderData['status']) && $orderData['status'] == 3) {
  1671. $needRemindRefreshSeat = 1;
  1672. }
  1673. }
  1674. $orderData['needRemindRefreshSeat'] = $needRemindRefreshSeat;
  1675. $orderData['inBooking'] = $inBooking;
  1676. // 结账信息:仅本采购单 cgId 下、且结账方式为「供货商向昆明供货商结账」(clearStyle=3) 的记录,
  1677. // 避免 xhOrderCgClear.cgId 与花店采购(bizHd\purchase\classes\PurchaseClass) 的 id 撞号,混入销售单结账
  1678. $cgId = intval($orderData['id'] ?? 0);
  1679. $orderData['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase(
  1680. $cgId,
  1681. dict::getDict('clearStyle', 'gys2KmGys')
  1682. );
  1683. return $orderData;
  1684. }
  1685. // 组装 orderItem lqh 2021.1.25
  1686. public static function groupOrderItem($ghsItemInfo, $orderSn)
  1687. {
  1688. $productData = self::getProductMapData($ghsItemInfo);
  1689. $batchData = [];
  1690. foreach ($ghsItemInfo as $v) {
  1691. $tmp = [];
  1692. $productId = $v['productId'];
  1693. $currentAboutPrice = $v['aboutPrice'] ?? 0;
  1694. $currentTotal = $v['totalPrice'] ?? 0;
  1695. $assignSeat = $v['assignSeat'] ?? 1;
  1696. $itemId = $productData[$productId]['itemId'];
  1697. $tmp['orderSn'] = $orderSn;
  1698. $tmp['assignSeat'] = $assignSeat;
  1699. $tmp['itemId'] = $itemId;
  1700. $tmp['belongCost'] = $productData[$productId]['belongCost'] ?? 0;
  1701. $tmp['productId'] = $v['productId'];
  1702. $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;
  1703. $tmp['onStock'] = $productData[$productId]['onStock'] ?? 0;
  1704. $tmp['aboutPrice'] = $currentAboutPrice;
  1705. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  1706. $tmp['itemNum'] = $v['bigNum'];
  1707. $tmp['bigPrice'] = $v['itemPrice'];
  1708. if ($currentAboutPrice == 1) {
  1709. $totalPrice = $currentTotal;
  1710. } else {
  1711. $totalPrice = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
  1712. }
  1713. $tmp['totalPrice'] = $totalPrice;
  1714. } else {
  1715. $smallNum = $v['smallNum'] ?? 0;
  1716. if ($smallNum <= 0) {
  1717. util::fail('没有数量');
  1718. }
  1719. $ratioType = $v['ratioType'] ?? 1;
  1720. if ($ratioType == 1) {
  1721. //util::fail('小单位,单位比必须固定的哈');
  1722. }
  1723. $ratio = $v['ratio'] ?? 1;
  1724. if ($ratio <= 1) {
  1725. //util::fail('小单位,单位比必须大于1的哦');
  1726. }
  1727. $changeToBigNum = bcdiv($smallNum, $ratio, 2);
  1728. $tmp['itemNum'] = $changeToBigNum;
  1729. $bigPrice = bcmul($v['itemPrice'], $ratio, 2);
  1730. $tmp['bigPrice'] = $bigPrice;
  1731. if ($currentAboutPrice == 1) {
  1732. $totalPrice = $currentTotal;
  1733. } else {
  1734. $totalPrice = bcmul($v['itemPrice'], $smallNum, 2);
  1735. }
  1736. $tmp['totalPrice'] = $totalPrice;
  1737. }
  1738. $batchData[] = $tmp;
  1739. }
  1740. return $batchData;
  1741. }
  1742. // 组装状态码 lqh 2021.1.30
  1743. public static function groupStatusName($list)
  1744. {
  1745. foreach ($list as $k => $val) {
  1746. $status = $val['status'] ?? 0;
  1747. $list[$k]['statusName'] = self::getStatusName($status);
  1748. }
  1749. return $list;
  1750. }
  1751. // 获取状态码名称
  1752. public static function getStatusName($status)
  1753. {
  1754. return self::$statusMap[$status] ?? '';
  1755. }
  1756. //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
  1757. public static function statNum($shopId)
  1758. {
  1759. $data = [
  1760. 'allNum' => 0,
  1761. 'unPayNum' => 0,
  1762. 'unSendNum' => 0,
  1763. 'sendingNum' => 0,
  1764. 'finishNum' => 0,
  1765. ];
  1766. $res = self::getAllList("status", ['shopId' => $shopId]);
  1767. foreach ($res as $v) {
  1768. $data['allNum']++;
  1769. $status = $v['status'];
  1770. switch ($status) {
  1771. case self::PURCHASE_ORDER_STATUS_WAIT:
  1772. //待支付
  1773. $data['unPayNum']++;
  1774. break;
  1775. case self::PURCHASE_ORDER_STATUS_UN_SEND:
  1776. $data['unSendNum']++;
  1777. break;
  1778. case self::PURCHASE_ORDER_STATUS_SENDING:
  1779. $data['sendingNum']++;
  1780. break;
  1781. case self::PURCHASE_ORDER_STATUS_COMPLETE:
  1782. $data['finishNum']++;
  1783. break;
  1784. }
  1785. }
  1786. return $data;
  1787. }
  1788. //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
  1789. public static function statOrderInfo($shopId, $statType)
  1790. {
  1791. $start = '';
  1792. $end = '';
  1793. switch ($statType) {
  1794. case "today":
  1795. $start = date('Y-m-d');
  1796. $end = $start;
  1797. break;
  1798. case "yesterday" :
  1799. $start = date('Y-m-d', strtotime("-1 day"));
  1800. $end = $start;
  1801. break;
  1802. case "seven":
  1803. $start = date('Y-m-d', strtotime("-6 day"));
  1804. $end = date('Y-m-d');
  1805. break;
  1806. case "thirty":
  1807. $start = date('Y-m-d', strtotime("-29 day"));
  1808. $end = date('Y-m-d');
  1809. break;
  1810. default:
  1811. }
  1812. $where = [];
  1813. $where['shopId'] = $shopId;
  1814. $where['addTime'] = ['between', [$start, $end]];
  1815. $res = self::getAllByCondition($where, null, "id,actPrice");
  1816. $totalAmount = 0;
  1817. $totalNum = count($res);
  1818. foreach ($res as $v) {
  1819. $actPrice = $v['actPrice'] ?? 0;
  1820. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  1821. }
  1822. return [
  1823. 'totalAmount' => $totalAmount,
  1824. 'totalNum' => $totalNum,
  1825. ];
  1826. }
  1827. public static function exportDueList($list, $mainId)
  1828. {
  1829. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1830. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1831. $objPHPExcel = new \PHPExcel();
  1832. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1833. ->setLastModifiedBy("Maarten Balliauw")
  1834. ->setTitle("Office 2007 XLSX Document")
  1835. ->setSubject("Office 2007 XLSX Document")
  1836. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1837. ->setKeywords("office 2007 openxml php")
  1838. ->setCategory("file");
  1839. $ghsTitle = '供货商';
  1840. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1841. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1842. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1843. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1844. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1845. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1846. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1847. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  1848. $objPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
  1849. $objPHPExcel->getActiveSheet()->setCellValue('C1', '未扣隔天金额');
  1850. $objPHPExcel->getActiveSheet()->setCellValue('D1', '跨天售后金额');
  1851. $objPHPExcel->getActiveSheet()->setCellValue('E1', '已扣隔天金额');
  1852. $objPHPExcel->getActiveSheet()->setCellValue('F1', '订单数');
  1853. //设置宽度
  1854. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  1855. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18);
  1856. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
  1857. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  1858. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  1859. //加粗
  1860. foreach (['A1', 'B1', 'C1', 'D1', 'E1', 'F1'] as $cell) {
  1861. $objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->setSize(9)->setBold(true);
  1862. $objPHPExcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1863. }
  1864. $baseRow = 2;
  1865. foreach ($list as $key => $custom) {
  1866. $i = $baseRow + $key;
  1867. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  1868. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  1869. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['originAmount'] ?? $custom['amount']);
  1870. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['nextDayAmount'] ?? 0);
  1871. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['amount']);
  1872. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['num'] ?? 0);
  1873. foreach (['A', 'B', 'C', 'D', 'E', 'F'] as $col) {
  1874. $objPHPExcel->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1875. }
  1876. }
  1877. $fileName = '应付统计-' . date("m-d");
  1878. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1879. $objPHPExcel->setActiveSheetIndex(0);
  1880. $dir = './priceTable/' . $mainId;
  1881. if (file_exists($dir) == false) {
  1882. mkdir($dir, 0777, true);
  1883. }
  1884. $date = $fileName;
  1885. $file = $date . '.xls';
  1886. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1887. if (file_exists($dir . '/' . $file)) {
  1888. unlink($dir . '/' . $file);
  1889. }
  1890. $objWriter->save($dir . '/' . $file);
  1891. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1892. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1893. }
  1894. /**
  1895. * 导出采购单明细 Excel。
  1896. * $searchTimeType 与列表一致:0 开单 entryTime、2 发货 sendTime,只影响文件名里的时间类型标注。
  1897. */
  1898. public static function exportOrderData($respond, $mainId, $searchTimeType = 0)
  1899. {
  1900. $orderList = $respond['list'] ?? [];
  1901. if (empty($orderList)) {
  1902. util::fail('没有数据需要导出');
  1903. }
  1904. $orderSnList = array_column($orderList, 'orderSn');
  1905. $orderItemData = PurchaseOrderItemClass::getAllByCondition(['orderSn' => ['in', $orderSnList]], null, '*');
  1906. $orderItemList = [];
  1907. if (!empty($orderItemData)) {
  1908. foreach ($orderItemData as $itemInfo) {
  1909. $orderSn = $itemInfo['orderSn'] ?? '';
  1910. $orderItemList[$orderSn][] = $itemInfo;
  1911. }
  1912. }
  1913. foreach ($orderList as $key => $orderInfo) {
  1914. $orderSn = $orderInfo['orderSn'] ?? '';
  1915. $status = $orderInfo['status'] ?? 0;
  1916. //增加项
  1917. $actPrice = $orderInfo['actPrice'] ?? 0;
  1918. if ($status != 4) {
  1919. unset($orderList[$key]);
  1920. continue;
  1921. }
  1922. //增加项
  1923. if ($actPrice <= 0) {
  1924. unset($orderList[$key]);
  1925. continue;
  1926. }
  1927. $itemList = $orderItemList[$orderSn] ?? [];
  1928. $orderList[$key]['itemList'] = $itemList;
  1929. }
  1930. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
  1931. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1932. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1933. $objPHPExcel = new \PHPExcel();
  1934. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1935. ->setLastModifiedBy("Maarten Balliauw")
  1936. ->setTitle("Office 2007 XLSX Document")
  1937. ->setSubject("Office 2007 XLSX Document")
  1938. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1939. ->setKeywords("office 2007 openxml php")
  1940. ->setCategory("file");
  1941. $ghsTitle = '供货商';
  1942. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1943. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1944. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1945. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1946. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1947. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1948. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1949. $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
  1950. $objPHPExcel->getActiveSheet()->setCellValue('B1', '创建时间');
  1951. $objPHPExcel->getActiveSheet()->setCellValue('C1', '开单时间');
  1952. $objPHPExcel->getActiveSheet()->setCellValue('D1', '发货时间');
  1953. $objPHPExcel->getActiveSheet()->setCellValue('E1', '订单金额');
  1954. $objPHPExcel->getActiveSheet()->setCellValue('F1', '退款金额');
  1955. $objPHPExcel->getActiveSheet()->setCellValue('G1', '实际金额');
  1956. $objPHPExcel->getActiveSheet()->setCellValue('H1', '供货商名称');
  1957. $objPHPExcel->getActiveSheet()->setCellValue('I1', '录入人员');
  1958. $objPHPExcel->getActiveSheet()->setCellValue('J1', '采购人员');
  1959. $objPHPExcel->getActiveSheet()->setCellValue('K1', '状态');
  1960. $objPHPExcel->getActiveSheet()->setCellValue('L1', '打印次数');
  1961. $objPHPExcel->getActiveSheet()->setCellValue('M1', '结清状态');
  1962. $objPHPExcel->getActiveSheet()->setCellValue('N1', '备注');
  1963. $objPHPExcel->getActiveSheet()->setCellValue('O1', '花材Id');
  1964. $objPHPExcel->getActiveSheet()->setCellValue('P1', '花材名称');
  1965. $objPHPExcel->getActiveSheet()->setCellValue('Q1', '单价');
  1966. $objPHPExcel->getActiveSheet()->setCellValue('R1', '数量');
  1967. $objPHPExcel->getActiveSheet()->setCellValue('S1', '已退数量');
  1968. $objPHPExcel->getActiveSheet()->setCellValue('T1', '金额');
  1969. $objPHPExcel->getActiveSheet()->setCellValue('U1', '分类');
  1970. //设置宽度
  1971. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1972. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1973. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1974. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1975. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1976. $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
  1977. $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
  1978. //加粗
  1979. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1980. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1981. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1982. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1983. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1984. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1985. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  1986. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  1987. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  1988. $objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setSize(9)->setBold(true);
  1989. $objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setSize(9)->setBold(true);
  1990. $objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setSize(9)->setBold(true);
  1991. $objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setSize(9)->setBold(true);
  1992. $objPHPExcel->getActiveSheet()->getStyle('N1')->getFont()->setSize(9)->setBold(true);
  1993. $objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setSize(9)->setBold(true);
  1994. $objPHPExcel->getActiveSheet()->getStyle('P1')->getFont()->setSize(9)->setBold(true);
  1995. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getFont()->setSize(9)->setBold(true);
  1996. $objPHPExcel->getActiveSheet()->getStyle('R1')->getFont()->setSize(9)->setBold(true);
  1997. $objPHPExcel->getActiveSheet()->getStyle('S1')->getFont()->setSize(9)->setBold(true);
  1998. $objPHPExcel->getActiveSheet()->getStyle('T1')->getFont()->setSize(9)->setBold(true);
  1999. $objPHPExcel->getActiveSheet()->getStyle('U1')->getFont()->setSize(9)->setBold(true);
  2000. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2001. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2002. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2003. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2004. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2005. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2006. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2007. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2008. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2009. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2010. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2011. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2012. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2013. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2014. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2015. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2016. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2017. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2018. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2019. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2020. $objPHPExcel->getActiveSheet()->getStyle('U1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2021. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2022. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2023. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2024. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2025. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2026. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2027. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2028. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2029. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2030. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2031. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2032. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2033. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2034. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2035. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2036. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2037. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2038. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2039. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2040. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2041. $objPHPExcel->getActiveSheet()->getStyle('U1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2042. $baseRow = 2;
  2043. $x = 0;
  2044. foreach ($orderList as $key => $orderData) {
  2045. $currentList = $orderData['itemList'] ?? [];
  2046. if (!empty($currentList)) {
  2047. foreach ($currentList as $k => $orderInfo) {
  2048. $i = $baseRow + $x;
  2049. $x++;
  2050. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $orderData['orderSn']);
  2051. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $orderData['addTime']);
  2052. if ($orderData['status'] == 4) {
  2053. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $orderData['entryTime']);
  2054. } else {
  2055. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, '');
  2056. }
  2057. $sendTime = $orderData['sendTime'] ?? '';
  2058. if ($sendTime === '' || strpos($sendTime, '0000-00-00') !== false) {
  2059. $sendTime = '';
  2060. }
  2061. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sendTime);
  2062. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $orderData['orderPrice']);
  2063. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $orderData['tkPrice']);
  2064. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $orderData['actPrice']);
  2065. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $orderData['ghsName']);
  2066. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $orderData['shopAdminName']);
  2067. $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $orderData['cgStaffName']);
  2068. $map = [1 => '待确认', 2 => '待发货', 3 => '待入库', 4 => '已入库', 5 => '已取消'];
  2069. $statusName = isset($orderData['status']) && isset($map[$orderData['status']]) ? $map[$orderData['status']] : '未知';
  2070. $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $statusName);
  2071. $objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $orderData['printNum'] ?? 0);
  2072. $debtName = '';
  2073. if ($orderData['debt'] == 1) {
  2074. $debtName = '未结清';
  2075. }
  2076. if ($orderData['debt'] == 2) {
  2077. $debtName = '已结清';
  2078. }
  2079. $objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $debtName);
  2080. $objPHPExcel->getActiveSheet()->setCellValue('N' . $i, $orderData['remark']);
  2081. $objPHPExcel->getActiveSheet()->setCellValue('O' . $i, $orderInfo['productId']);
  2082. $objPHPExcel->getActiveSheet()->setCellValue('P' . $i, $orderInfo['name']);
  2083. $objPHPExcel->getActiveSheet()->setCellValue('Q' . $i, $orderInfo['bigPrice']);
  2084. $remainNum = bcsub($orderInfo['itemNum'], $orderInfo['refundNum']);
  2085. $price = bcmul($remainNum, $orderInfo['bigPrice'], 2);
  2086. $price = floatval($price);
  2087. $objPHPExcel->getActiveSheet()->setCellValue('R' . $i, $remainNum);
  2088. $objPHPExcel->getActiveSheet()->setCellValue('S' . $i, $orderInfo['refundNum']);
  2089. $objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $price);
  2090. $classId = $orderInfo['classId'] ?? 0;
  2091. $className = isset($classList[$classId]) && isset($classList[$classId]['name']) ? $classList[$classId]['name'] : '';
  2092. $objPHPExcel->getActiveSheet()->setCellValue('U' . $i, $className);
  2093. //居左
  2094. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2095. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2096. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2097. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2098. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2099. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2100. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2101. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2102. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2103. $objPHPExcel->getActiveSheet()->getStyle('J' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2104. $objPHPExcel->getActiveSheet()->getStyle('K' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2105. $objPHPExcel->getActiveSheet()->getStyle('L' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2106. $objPHPExcel->getActiveSheet()->getStyle('M' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2107. $objPHPExcel->getActiveSheet()->getStyle('N' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2108. $objPHPExcel->getActiveSheet()->getStyle('O' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2109. $objPHPExcel->getActiveSheet()->getStyle('P' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2110. $objPHPExcel->getActiveSheet()->getStyle('Q' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2111. $objPHPExcel->getActiveSheet()->getStyle('R' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2112. $objPHPExcel->getActiveSheet()->getStyle('S' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2113. $objPHPExcel->getActiveSheet()->getStyle('T' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2114. $objPHPExcel->getActiveSheet()->getStyle('U' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2115. }
  2116. }
  2117. }
  2118. $timeTypeName = $searchTimeType === 2 ? '发货时间' : '开单时间';
  2119. $fileName = '采购单明细-' . $timeTypeName . '-' . time();
  2120. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  2121. $objPHPExcel->setActiveSheetIndex(0);
  2122. $dir = './priceTable/' . $mainId;
  2123. if (file_exists($dir) == false) {
  2124. mkdir($dir, 0777, true);
  2125. }
  2126. $date = $fileName;
  2127. $file = $date . '.xls';
  2128. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  2129. if (file_exists($dir . '/' . $file)) {
  2130. unlink($dir . '/' . $file);
  2131. }
  2132. $objWriter->save($dir . '/' . $file);
  2133. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  2134. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  2135. }
  2136. /**
  2137. * 按 productId 升序排列,用于库存行锁顺序(降低多商品并发死锁)
  2138. * @param array $list
  2139. */
  2140. private static function sortRowsByProductIdAsc(&$list)
  2141. {
  2142. usort($list, function ($left, $right) {
  2143. $leftId = isset($left['productId']) ? intval($left['productId']) : 0;
  2144. $rightId = isset($right['productId']) ? intval($right['productId']) : 0;
  2145. if ($leftId == $rightId) {
  2146. return 0;
  2147. }
  2148. return $leftId < $rightId ? -1 : 1;
  2149. });
  2150. }
  2151. }